blob: 58a125cf1a73a539f790c01ff5909d9f41fccdf7 [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:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002380 if (use_imm && IsInt<16>(-rhs_imm)) {
2381 if (rhs_imm == 0) {
2382 if (cond == kCondEQ) {
2383 __ Sltiu(dst, lhs, 1);
2384 } else {
2385 __ Sltu(dst, ZERO, lhs);
2386 }
2387 } else {
2388 if (is64bit) {
2389 __ Daddiu(dst, lhs, -rhs_imm);
2390 } else {
2391 __ Addiu(dst, lhs, -rhs_imm);
2392 }
2393 if (cond == kCondEQ) {
2394 __ Sltiu(dst, dst, 1);
2395 } else {
2396 __ Sltu(dst, ZERO, dst);
2397 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002398 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002399 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002400 if (use_imm && IsUint<16>(rhs_imm)) {
2401 __ Xori(dst, lhs, rhs_imm);
2402 } else {
2403 if (use_imm) {
2404 rhs_reg = TMP;
2405 __ LoadConst64(rhs_reg, rhs_imm);
2406 }
2407 __ Xor(dst, lhs, rhs_reg);
2408 }
2409 if (cond == kCondEQ) {
2410 __ Sltiu(dst, dst, 1);
2411 } else {
2412 __ Sltu(dst, ZERO, dst);
2413 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002414 }
2415 break;
2416
2417 case kCondLT:
2418 case kCondGE:
2419 if (use_imm && IsInt<16>(rhs_imm)) {
2420 __ Slti(dst, lhs, rhs_imm);
2421 } else {
2422 if (use_imm) {
2423 rhs_reg = TMP;
2424 __ LoadConst64(rhs_reg, rhs_imm);
2425 }
2426 __ Slt(dst, lhs, rhs_reg);
2427 }
2428 if (cond == kCondGE) {
2429 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2430 // only the slt instruction but no sge.
2431 __ Xori(dst, dst, 1);
2432 }
2433 break;
2434
2435 case kCondLE:
2436 case kCondGT:
2437 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2438 // Simulate lhs <= rhs via lhs < rhs + 1.
2439 __ Slti(dst, lhs, rhs_imm_plus_one);
2440 if (cond == kCondGT) {
2441 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2442 // only the slti instruction but no sgti.
2443 __ Xori(dst, dst, 1);
2444 }
2445 } else {
2446 if (use_imm) {
2447 rhs_reg = TMP;
2448 __ LoadConst64(rhs_reg, rhs_imm);
2449 }
2450 __ Slt(dst, rhs_reg, lhs);
2451 if (cond == kCondLE) {
2452 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2453 // only the slt instruction but no sle.
2454 __ Xori(dst, dst, 1);
2455 }
2456 }
2457 break;
2458
2459 case kCondB:
2460 case kCondAE:
2461 if (use_imm && IsInt<16>(rhs_imm)) {
2462 // Sltiu sign-extends its 16-bit immediate operand before
2463 // the comparison and thus lets us compare directly with
2464 // unsigned values in the ranges [0, 0x7fff] and
2465 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2466 __ Sltiu(dst, lhs, rhs_imm);
2467 } else {
2468 if (use_imm) {
2469 rhs_reg = TMP;
2470 __ LoadConst64(rhs_reg, rhs_imm);
2471 }
2472 __ Sltu(dst, lhs, rhs_reg);
2473 }
2474 if (cond == kCondAE) {
2475 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2476 // only the sltu instruction but no sgeu.
2477 __ Xori(dst, dst, 1);
2478 }
2479 break;
2480
2481 case kCondBE:
2482 case kCondA:
2483 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2484 // Simulate lhs <= rhs via lhs < rhs + 1.
2485 // Note that this only works if rhs + 1 does not overflow
2486 // to 0, hence the check above.
2487 // Sltiu sign-extends its 16-bit immediate operand before
2488 // the comparison and thus lets us compare directly with
2489 // unsigned values in the ranges [0, 0x7fff] and
2490 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2491 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2492 if (cond == kCondA) {
2493 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2494 // only the sltiu instruction but no sgtiu.
2495 __ Xori(dst, dst, 1);
2496 }
2497 } else {
2498 if (use_imm) {
2499 rhs_reg = TMP;
2500 __ LoadConst64(rhs_reg, rhs_imm);
2501 }
2502 __ Sltu(dst, rhs_reg, lhs);
2503 if (cond == kCondBE) {
2504 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2505 // only the sltu instruction but no sleu.
2506 __ Xori(dst, dst, 1);
2507 }
2508 }
2509 break;
2510 }
2511}
2512
2513void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2514 bool is64bit,
2515 LocationSummary* locations,
2516 Mips64Label* label) {
2517 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2518 Location rhs_location = locations->InAt(1);
2519 GpuRegister rhs_reg = ZERO;
2520 int64_t rhs_imm = 0;
2521 bool use_imm = rhs_location.IsConstant();
2522 if (use_imm) {
2523 if (is64bit) {
2524 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2525 } else {
2526 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2527 }
2528 } else {
2529 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2530 }
2531
2532 if (use_imm && rhs_imm == 0) {
2533 switch (cond) {
2534 case kCondEQ:
2535 case kCondBE: // <= 0 if zero
2536 __ Beqzc(lhs, label);
2537 break;
2538 case kCondNE:
2539 case kCondA: // > 0 if non-zero
2540 __ Bnezc(lhs, label);
2541 break;
2542 case kCondLT:
2543 __ Bltzc(lhs, label);
2544 break;
2545 case kCondGE:
2546 __ Bgezc(lhs, label);
2547 break;
2548 case kCondLE:
2549 __ Blezc(lhs, label);
2550 break;
2551 case kCondGT:
2552 __ Bgtzc(lhs, label);
2553 break;
2554 case kCondB: // always false
2555 break;
2556 case kCondAE: // always true
2557 __ Bc(label);
2558 break;
2559 }
2560 } else {
2561 if (use_imm) {
2562 rhs_reg = TMP;
2563 __ LoadConst64(rhs_reg, rhs_imm);
2564 }
2565 switch (cond) {
2566 case kCondEQ:
2567 __ Beqc(lhs, rhs_reg, label);
2568 break;
2569 case kCondNE:
2570 __ Bnec(lhs, rhs_reg, label);
2571 break;
2572 case kCondLT:
2573 __ Bltc(lhs, rhs_reg, label);
2574 break;
2575 case kCondGE:
2576 __ Bgec(lhs, rhs_reg, label);
2577 break;
2578 case kCondLE:
2579 __ Bgec(rhs_reg, lhs, label);
2580 break;
2581 case kCondGT:
2582 __ Bltc(rhs_reg, lhs, label);
2583 break;
2584 case kCondB:
2585 __ Bltuc(lhs, rhs_reg, label);
2586 break;
2587 case kCondAE:
2588 __ Bgeuc(lhs, rhs_reg, label);
2589 break;
2590 case kCondBE:
2591 __ Bgeuc(rhs_reg, lhs, label);
2592 break;
2593 case kCondA:
2594 __ Bltuc(rhs_reg, lhs, label);
2595 break;
2596 }
2597 }
2598}
2599
2600void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2601 bool gt_bias,
2602 Primitive::Type type,
2603 LocationSummary* locations,
2604 Mips64Label* label) {
2605 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2606 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2607 if (type == Primitive::kPrimFloat) {
2608 switch (cond) {
2609 case kCondEQ:
2610 __ CmpEqS(FTMP, lhs, rhs);
2611 __ Bc1nez(FTMP, label);
2612 break;
2613 case kCondNE:
2614 __ CmpEqS(FTMP, lhs, rhs);
2615 __ Bc1eqz(FTMP, label);
2616 break;
2617 case kCondLT:
2618 if (gt_bias) {
2619 __ CmpLtS(FTMP, lhs, rhs);
2620 } else {
2621 __ CmpUltS(FTMP, lhs, rhs);
2622 }
2623 __ Bc1nez(FTMP, label);
2624 break;
2625 case kCondLE:
2626 if (gt_bias) {
2627 __ CmpLeS(FTMP, lhs, rhs);
2628 } else {
2629 __ CmpUleS(FTMP, lhs, rhs);
2630 }
2631 __ Bc1nez(FTMP, label);
2632 break;
2633 case kCondGT:
2634 if (gt_bias) {
2635 __ CmpUltS(FTMP, rhs, lhs);
2636 } else {
2637 __ CmpLtS(FTMP, rhs, lhs);
2638 }
2639 __ Bc1nez(FTMP, label);
2640 break;
2641 case kCondGE:
2642 if (gt_bias) {
2643 __ CmpUleS(FTMP, rhs, lhs);
2644 } else {
2645 __ CmpLeS(FTMP, rhs, lhs);
2646 }
2647 __ Bc1nez(FTMP, label);
2648 break;
2649 default:
2650 LOG(FATAL) << "Unexpected non-floating-point condition";
2651 }
2652 } else {
2653 DCHECK_EQ(type, Primitive::kPrimDouble);
2654 switch (cond) {
2655 case kCondEQ:
2656 __ CmpEqD(FTMP, lhs, rhs);
2657 __ Bc1nez(FTMP, label);
2658 break;
2659 case kCondNE:
2660 __ CmpEqD(FTMP, lhs, rhs);
2661 __ Bc1eqz(FTMP, label);
2662 break;
2663 case kCondLT:
2664 if (gt_bias) {
2665 __ CmpLtD(FTMP, lhs, rhs);
2666 } else {
2667 __ CmpUltD(FTMP, lhs, rhs);
2668 }
2669 __ Bc1nez(FTMP, label);
2670 break;
2671 case kCondLE:
2672 if (gt_bias) {
2673 __ CmpLeD(FTMP, lhs, rhs);
2674 } else {
2675 __ CmpUleD(FTMP, lhs, rhs);
2676 }
2677 __ Bc1nez(FTMP, label);
2678 break;
2679 case kCondGT:
2680 if (gt_bias) {
2681 __ CmpUltD(FTMP, rhs, lhs);
2682 } else {
2683 __ CmpLtD(FTMP, rhs, lhs);
2684 }
2685 __ Bc1nez(FTMP, label);
2686 break;
2687 case kCondGE:
2688 if (gt_bias) {
2689 __ CmpUleD(FTMP, rhs, lhs);
2690 } else {
2691 __ CmpLeD(FTMP, rhs, lhs);
2692 }
2693 __ Bc1nez(FTMP, label);
2694 break;
2695 default:
2696 LOG(FATAL) << "Unexpected non-floating-point condition";
2697 }
2698 }
2699}
2700
Alexey Frunze4dda3372015-06-01 18:31:49 -07002701void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002702 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002703 Mips64Label* true_target,
2704 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002705 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002706
David Brazdil0debae72015-11-12 18:37:00 +00002707 if (true_target == nullptr && false_target == nullptr) {
2708 // Nothing to do. The code always falls through.
2709 return;
2710 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002711 // Constant condition, statically compared against "true" (integer value 1).
2712 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002713 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002714 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002715 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002716 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002717 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002718 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002719 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002720 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002721 }
David Brazdil0debae72015-11-12 18:37:00 +00002722 return;
2723 }
2724
2725 // The following code generates these patterns:
2726 // (1) true_target == nullptr && false_target != nullptr
2727 // - opposite condition true => branch to false_target
2728 // (2) true_target != nullptr && false_target == nullptr
2729 // - condition true => branch to true_target
2730 // (3) true_target != nullptr && false_target != nullptr
2731 // - condition true => branch to true_target
2732 // - branch to false_target
2733 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002734 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002735 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002736 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002737 if (true_target == nullptr) {
2738 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2739 } else {
2740 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2741 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002742 } else {
2743 // The condition instruction has not been materialized, use its inputs as
2744 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002745 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002746 Primitive::Type type = condition->InputAt(0)->GetType();
2747 LocationSummary* locations = cond->GetLocations();
2748 IfCondition if_cond = condition->GetCondition();
2749 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002750
David Brazdil0debae72015-11-12 18:37:00 +00002751 if (true_target == nullptr) {
2752 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002753 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002754 }
2755
Alexey Frunze299a9392015-12-08 16:08:02 -08002756 switch (type) {
2757 default:
2758 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2759 break;
2760 case Primitive::kPrimLong:
2761 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2762 break;
2763 case Primitive::kPrimFloat:
2764 case Primitive::kPrimDouble:
2765 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2766 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002767 }
2768 }
David Brazdil0debae72015-11-12 18:37:00 +00002769
2770 // If neither branch falls through (case 3), the conditional branch to `true_target`
2771 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2772 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002773 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002774 }
2775}
2776
2777void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002779 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002780 locations->SetInAt(0, Location::RequiresRegister());
2781 }
2782}
2783
2784void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002785 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2786 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002787 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002788 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002789 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002790 nullptr : codegen_->GetLabelOf(false_successor);
2791 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002792}
2793
2794void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2795 LocationSummary* locations = new (GetGraph()->GetArena())
2796 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002797 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002798 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002799 locations->SetInAt(0, Location::RequiresRegister());
2800 }
2801}
2802
2803void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002804 SlowPathCodeMIPS64* slow_path =
2805 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002806 GenerateTestAndBranch(deoptimize,
2807 /* condition_input_index */ 0,
2808 slow_path->GetEntryLabel(),
2809 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002810}
2811
Goran Jakovljevicc6418422016-12-05 16:31:55 +01002812void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2813 LocationSummary* locations = new (GetGraph()->GetArena())
2814 LocationSummary(flag, LocationSummary::kNoCall);
2815 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07002816}
2817
Goran Jakovljevicc6418422016-12-05 16:31:55 +01002818void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2819 __ LoadFromOffset(kLoadWord,
2820 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
2821 SP,
2822 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07002823}
2824
David Brazdil74eb1b22015-12-14 11:44:01 +00002825void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2826 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2827 if (Primitive::IsFloatingPointType(select->GetType())) {
2828 locations->SetInAt(0, Location::RequiresFpuRegister());
2829 locations->SetInAt(1, Location::RequiresFpuRegister());
2830 } else {
2831 locations->SetInAt(0, Location::RequiresRegister());
2832 locations->SetInAt(1, Location::RequiresRegister());
2833 }
2834 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2835 locations->SetInAt(2, Location::RequiresRegister());
2836 }
2837 locations->SetOut(Location::SameAsFirstInput());
2838}
2839
2840void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2841 LocationSummary* locations = select->GetLocations();
2842 Mips64Label false_target;
2843 GenerateTestAndBranch(select,
2844 /* condition_input_index */ 2,
2845 /* true_target */ nullptr,
2846 &false_target);
2847 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2848 __ Bind(&false_target);
2849}
2850
David Srbecky0cf44932015-12-09 14:09:59 +00002851void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2852 new (GetGraph()->GetArena()) LocationSummary(info);
2853}
2854
David Srbeckyd28f4a02016-03-14 17:14:24 +00002855void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2856 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002857}
2858
2859void CodeGeneratorMIPS64::GenerateNop() {
2860 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002861}
2862
Alexey Frunze4dda3372015-06-01 18:31:49 -07002863void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2864 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2865 LocationSummary* locations =
2866 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2867 locations->SetInAt(0, Location::RequiresRegister());
2868 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2869 locations->SetOut(Location::RequiresFpuRegister());
2870 } else {
2871 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2872 }
2873}
2874
2875void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2876 const FieldInfo& field_info) {
2877 Primitive::Type type = field_info.GetFieldType();
2878 LocationSummary* locations = instruction->GetLocations();
2879 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2880 LoadOperandType load_type = kLoadUnsignedByte;
2881 switch (type) {
2882 case Primitive::kPrimBoolean:
2883 load_type = kLoadUnsignedByte;
2884 break;
2885 case Primitive::kPrimByte:
2886 load_type = kLoadSignedByte;
2887 break;
2888 case Primitive::kPrimShort:
2889 load_type = kLoadSignedHalfword;
2890 break;
2891 case Primitive::kPrimChar:
2892 load_type = kLoadUnsignedHalfword;
2893 break;
2894 case Primitive::kPrimInt:
2895 case Primitive::kPrimFloat:
2896 load_type = kLoadWord;
2897 break;
2898 case Primitive::kPrimLong:
2899 case Primitive::kPrimDouble:
2900 load_type = kLoadDoubleword;
2901 break;
2902 case Primitive::kPrimNot:
2903 load_type = kLoadUnsignedWord;
2904 break;
2905 case Primitive::kPrimVoid:
2906 LOG(FATAL) << "Unreachable type " << type;
2907 UNREACHABLE();
2908 }
2909 if (!Primitive::IsFloatingPointType(type)) {
2910 DCHECK(locations->Out().IsRegister());
2911 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2912 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2913 } else {
2914 DCHECK(locations->Out().IsFpuRegister());
2915 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2916 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2917 }
2918
2919 codegen_->MaybeRecordImplicitNullCheck(instruction);
2920 // TODO: memory barrier?
2921}
2922
2923void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2924 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2925 LocationSummary* locations =
2926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2927 locations->SetInAt(0, Location::RequiresRegister());
2928 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2929 locations->SetInAt(1, Location::RequiresFpuRegister());
2930 } else {
2931 locations->SetInAt(1, Location::RequiresRegister());
2932 }
2933}
2934
2935void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002936 const FieldInfo& field_info,
2937 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002938 Primitive::Type type = field_info.GetFieldType();
2939 LocationSummary* locations = instruction->GetLocations();
2940 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2941 StoreOperandType store_type = kStoreByte;
2942 switch (type) {
2943 case Primitive::kPrimBoolean:
2944 case Primitive::kPrimByte:
2945 store_type = kStoreByte;
2946 break;
2947 case Primitive::kPrimShort:
2948 case Primitive::kPrimChar:
2949 store_type = kStoreHalfword;
2950 break;
2951 case Primitive::kPrimInt:
2952 case Primitive::kPrimFloat:
2953 case Primitive::kPrimNot:
2954 store_type = kStoreWord;
2955 break;
2956 case Primitive::kPrimLong:
2957 case Primitive::kPrimDouble:
2958 store_type = kStoreDoubleword;
2959 break;
2960 case Primitive::kPrimVoid:
2961 LOG(FATAL) << "Unreachable type " << type;
2962 UNREACHABLE();
2963 }
2964 if (!Primitive::IsFloatingPointType(type)) {
2965 DCHECK(locations->InAt(1).IsRegister());
2966 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2967 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2968 } else {
2969 DCHECK(locations->InAt(1).IsFpuRegister());
2970 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2971 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2972 }
2973
2974 codegen_->MaybeRecordImplicitNullCheck(instruction);
2975 // TODO: memory barriers?
2976 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2977 DCHECK(locations->InAt(1).IsRegister());
2978 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002979 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002980 }
2981}
2982
2983void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2984 HandleFieldGet(instruction, instruction->GetFieldInfo());
2985}
2986
2987void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2988 HandleFieldGet(instruction, instruction->GetFieldInfo());
2989}
2990
2991void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2992 HandleFieldSet(instruction, instruction->GetFieldInfo());
2993}
2994
2995void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002996 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002997}
2998
Alexey Frunzef63f5692016-12-13 17:43:11 -08002999void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
3000 HInstruction* instruction ATTRIBUTE_UNUSED,
3001 Location root,
3002 GpuRegister obj,
3003 uint32_t offset) {
3004 // When handling HLoadClass::LoadKind::kDexCachePcRelative, the caller calls
3005 // EmitPcRelativeAddressPlaceholderHigh() and then GenerateGcRootFieldLoad().
3006 // The relative patcher expects the two methods to emit the following patchable
3007 // sequence of instructions in this case:
3008 // auipc reg1, 0x1234 // 0x1234 is a placeholder for offset_high.
3009 // lwu reg2, 0x5678(reg1) // 0x5678 is a placeholder for offset_low.
3010 // TODO: Adjust GenerateGcRootFieldLoad() and its caller when this method is
3011 // extended (e.g. for read barriers) so as not to break the relative patcher.
3012 GpuRegister root_reg = root.AsRegister<GpuRegister>();
3013 if (kEmitCompilerReadBarrier) {
3014 UNIMPLEMENTED(FATAL) << "for read barrier";
3015 } else {
3016 // Plain GC root load with no read barrier.
3017 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3018 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
3019 // Note that GC roots are not affected by heap poisoning, thus we
3020 // do not have to unpoison `root_reg` here.
3021 }
3022}
3023
Alexey Frunze4dda3372015-06-01 18:31:49 -07003024void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3025 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003026 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3028 locations->SetInAt(0, Location::RequiresRegister());
3029 locations->SetInAt(1, Location::RequiresRegister());
3030 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003031 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003032 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3033}
3034
3035void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3036 LocationSummary* locations = instruction->GetLocations();
3037 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3038 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
3039 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3040
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003041 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003042
3043 // Return 0 if `obj` is null.
3044 // TODO: Avoid this check if we know `obj` is not null.
3045 __ Move(out, ZERO);
3046 __ Beqzc(obj, &done);
3047
3048 // Compare the class of `obj` with `cls`.
3049 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003050 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003051 // Classes must be equal for the instanceof to succeed.
3052 __ Xor(out, out, cls);
3053 __ Sltiu(out, out, 1);
3054 } else {
3055 // If the classes are not equal, we go into a slow path.
3056 DCHECK(locations->OnlyCallsOnSlowPath());
3057 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003058 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003059 codegen_->AddSlowPath(slow_path);
3060 __ Bnec(out, cls, slow_path->GetEntryLabel());
3061 __ LoadConst32(out, 1);
3062 __ Bind(slow_path->GetExitLabel());
3063 }
3064
3065 __ Bind(&done);
3066}
3067
3068void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
3069 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3070 locations->SetOut(Location::ConstantLocation(constant));
3071}
3072
3073void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3074 // Will be generated at use site.
3075}
3076
3077void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
3078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3079 locations->SetOut(Location::ConstantLocation(constant));
3080}
3081
3082void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3083 // Will be generated at use site.
3084}
3085
Calin Juravle175dc732015-08-25 15:42:32 +01003086void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3087 // The trampoline uses the same calling convention as dex calling conventions,
3088 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3089 // the method_idx.
3090 HandleInvoke(invoke);
3091}
3092
3093void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3094 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3095}
3096
Alexey Frunze4dda3372015-06-01 18:31:49 -07003097void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3098 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3099 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3100}
3101
3102void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3103 HandleInvoke(invoke);
3104 // The register T0 is required to be used for the hidden argument in
3105 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3106 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3107}
3108
3109void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3110 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3111 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003112 Location receiver = invoke->GetLocations()->InAt(0);
3113 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003114 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003115
3116 // Set the hidden argument.
3117 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3118 invoke->GetDexMethodIndex());
3119
3120 // temp = object->GetClass();
3121 if (receiver.IsStackSlot()) {
3122 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3123 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3124 } else {
3125 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3126 }
3127 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003128 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3129 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3130 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003131 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003132 // temp = temp->GetImtEntryAt(method_offset);
3133 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3134 // T9 = temp->GetEntryPoint();
3135 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3136 // T9();
3137 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003138 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003139 DCHECK(!codegen_->IsLeafMethod());
3140 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3141}
3142
3143void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003144 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3145 if (intrinsic.TryDispatch(invoke)) {
3146 return;
3147 }
3148
Alexey Frunze4dda3372015-06-01 18:31:49 -07003149 HandleInvoke(invoke);
3150}
3151
3152void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003153 // Explicit clinit checks triggered by static invokes must have been pruned by
3154 // art::PrepareForRegisterAllocation.
3155 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003156
Chris Larsen3039e382015-08-26 07:54:08 -07003157 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3158 if (intrinsic.TryDispatch(invoke)) {
3159 return;
3160 }
3161
Alexey Frunze4dda3372015-06-01 18:31:49 -07003162 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003163}
3164
Chris Larsen3039e382015-08-26 07:54:08 -07003165static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003166 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003167 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3168 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003169 return true;
3170 }
3171 return false;
3172}
3173
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003174HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003175 HLoadString::LoadKind desired_string_load_kind) {
3176 if (kEmitCompilerReadBarrier) {
3177 UNIMPLEMENTED(FATAL) << "for read barrier";
3178 }
3179 bool fallback_load = false;
3180 switch (desired_string_load_kind) {
3181 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3182 DCHECK(!GetCompilerOptions().GetCompilePic());
3183 break;
3184 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3185 DCHECK(GetCompilerOptions().GetCompilePic());
3186 break;
3187 case HLoadString::LoadKind::kBootImageAddress:
3188 break;
3189 case HLoadString::LoadKind::kBssEntry:
3190 DCHECK(!Runtime::Current()->UseJitCompilation());
3191 break;
3192 case HLoadString::LoadKind::kDexCacheViaMethod:
3193 break;
3194 case HLoadString::LoadKind::kJitTableAddress:
3195 DCHECK(Runtime::Current()->UseJitCompilation());
3196 // TODO: implement.
3197 fallback_load = true;
3198 break;
3199 }
3200 if (fallback_load) {
3201 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
3202 }
3203 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003204}
3205
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003206HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3207 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003208 if (kEmitCompilerReadBarrier) {
3209 UNIMPLEMENTED(FATAL) << "for read barrier";
3210 }
3211 bool fallback_load = false;
3212 switch (desired_class_load_kind) {
3213 case HLoadClass::LoadKind::kReferrersClass:
3214 break;
3215 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3216 DCHECK(!GetCompilerOptions().GetCompilePic());
3217 break;
3218 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3219 DCHECK(GetCompilerOptions().GetCompilePic());
3220 break;
3221 case HLoadClass::LoadKind::kBootImageAddress:
3222 break;
3223 case HLoadClass::LoadKind::kJitTableAddress:
3224 DCHECK(Runtime::Current()->UseJitCompilation());
3225 // TODO: implement.
3226 fallback_load = true;
3227 break;
3228 case HLoadClass::LoadKind::kDexCachePcRelative:
3229 DCHECK(!Runtime::Current()->UseJitCompilation());
3230 break;
3231 case HLoadClass::LoadKind::kDexCacheViaMethod:
3232 break;
3233 }
3234 if (fallback_load) {
3235 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
3236 }
3237 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003238}
3239
Vladimir Markodc151b22015-10-15 18:02:30 +01003240HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3241 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003242 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003243 // On MIPS64 we support all dispatch types.
3244 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003245}
3246
Alexey Frunze4dda3372015-06-01 18:31:49 -07003247void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3248 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003249 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003250 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3251 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3252
Alexey Frunze19f6c692016-11-30 19:19:55 -08003253 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003254 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003255 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003256 uint32_t offset =
3257 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003258 __ LoadFromOffset(kLoadDoubleword,
3259 temp.AsRegister<GpuRegister>(),
3260 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003261 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003262 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003263 }
Vladimir Marko58155012015-08-19 12:49:41 +00003264 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003265 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003266 break;
3267 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003268 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3269 kLoadDoubleword,
3270 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003271 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003272 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3273 uint32_t offset = invoke->GetDexCacheArrayOffset();
3274 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3275 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset);
3276 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3277 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3278 break;
3279 }
Vladimir Marko58155012015-08-19 12:49:41 +00003280 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003281 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003282 GpuRegister reg = temp.AsRegister<GpuRegister>();
3283 GpuRegister method_reg;
3284 if (current_method.IsRegister()) {
3285 method_reg = current_method.AsRegister<GpuRegister>();
3286 } else {
3287 // TODO: use the appropriate DCHECK() here if possible.
3288 // DCHECK(invoke->GetLocations()->Intrinsified());
3289 DCHECK(!current_method.IsValid());
3290 method_reg = reg;
3291 __ Ld(reg, SP, kCurrentMethodStackOffset);
3292 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003293
Vladimir Marko58155012015-08-19 12:49:41 +00003294 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003295 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003296 reg,
3297 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003298 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003299 // temp = temp[index_in_cache];
3300 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3301 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003302 __ LoadFromOffset(kLoadDoubleword,
3303 reg,
3304 reg,
3305 CodeGenerator::GetCachePointerOffset(index_in_cache));
3306 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003307 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003308 }
3309
Alexey Frunze19f6c692016-11-30 19:19:55 -08003310 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003311 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003312 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003313 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003314 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3315 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3316 __ LoadFromOffset(kLoadDoubleword,
3317 T9,
3318 callee_method.AsRegister<GpuRegister>(),
3319 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003320 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003321 // T9()
3322 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003323 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003324 break;
3325 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003326 DCHECK(!IsLeafMethod());
3327}
3328
3329void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003330 // Explicit clinit checks triggered by static invokes must have been pruned by
3331 // art::PrepareForRegisterAllocation.
3332 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003333
3334 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3335 return;
3336 }
3337
3338 LocationSummary* locations = invoke->GetLocations();
3339 codegen_->GenerateStaticOrDirectCall(invoke,
3340 locations->HasTemps()
3341 ? locations->GetTemp(0)
3342 : Location::NoLocation());
3343 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3344}
3345
Alexey Frunze53afca12015-11-05 16:34:23 -08003346void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003347 // Use the calling convention instead of the location of the receiver, as
3348 // intrinsics may have put the receiver in a different register. In the intrinsics
3349 // slow path, the arguments have been moved to the right place, so here we are
3350 // guaranteed that the receiver is the first register of the calling convention.
3351 InvokeDexCallingConvention calling_convention;
3352 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3353
Alexey Frunze53afca12015-11-05 16:34:23 -08003354 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003355 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3356 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3357 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003358 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003359
3360 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003361 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003362 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003363 // temp = temp->GetMethodAt(method_offset);
3364 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3365 // T9 = temp->GetEntryPoint();
3366 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3367 // T9();
3368 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003369 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003370}
3371
3372void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3373 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3374 return;
3375 }
3376
3377 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003378 DCHECK(!codegen_->IsLeafMethod());
3379 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3380}
3381
3382void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003383 if (cls->NeedsAccessCheck()) {
3384 InvokeRuntimeCallingConvention calling_convention;
3385 CodeGenerator::CreateLoadClassLocationSummary(
3386 cls,
3387 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3388 calling_convention.GetReturnLocation(Primitive::kPrimNot),
3389 /* code_generator_supports_read_barrier */ false);
3390 return;
3391 }
3392
3393 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3394 ? LocationSummary::kCallOnSlowPath
3395 : LocationSummary::kNoCall;
3396 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
3397 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3398 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3399 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3400 locations->SetInAt(0, Location::RequiresRegister());
3401 }
3402 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003403}
3404
3405void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3406 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003407 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003408 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +01003409 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003410 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003411 return;
3412 }
3413
Alexey Frunzef63f5692016-12-13 17:43:11 -08003414 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3415 Location out_loc = locations->Out();
3416 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3417 GpuRegister current_method_reg = ZERO;
3418 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3419 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3420 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
3421 }
3422
3423 bool generate_null_check = false;
3424 switch (load_kind) {
3425 case HLoadClass::LoadKind::kReferrersClass:
3426 DCHECK(!cls->CanCallRuntime());
3427 DCHECK(!cls->MustGenerateClinitCheck());
3428 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3429 GenerateGcRootFieldLoad(cls,
3430 out_loc,
3431 current_method_reg,
3432 ArtMethod::DeclaringClassOffset().Int32Value());
3433 break;
3434 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3435 DCHECK(!kEmitCompilerReadBarrier);
3436 __ LoadLiteral(out,
3437 kLoadUnsignedWord,
3438 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3439 cls->GetTypeIndex()));
3440 break;
3441 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
3442 DCHECK(!kEmitCompilerReadBarrier);
3443 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3444 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
3445 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3446 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3447 break;
3448 }
3449 case HLoadClass::LoadKind::kBootImageAddress: {
3450 DCHECK(!kEmitCompilerReadBarrier);
3451 DCHECK_NE(cls->GetAddress(), 0u);
3452 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
3453 __ LoadLiteral(out,
3454 kLoadUnsignedWord,
3455 codegen_->DeduplicateBootImageAddressLiteral(address));
3456 break;
3457 }
3458 case HLoadClass::LoadKind::kJitTableAddress: {
3459 LOG(FATAL) << "Unimplemented";
3460 break;
3461 }
3462 case HLoadClass::LoadKind::kDexCachePcRelative: {
3463 uint32_t element_offset = cls->GetDexCacheElementOffset();
3464 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3465 codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), element_offset);
3466 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3467 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
3468 GenerateGcRootFieldLoad(cls, out_loc, AT, /* placeholder */ 0x5678);
3469 generate_null_check = !cls->IsInDexCache();
3470 break;
3471 }
3472 case HLoadClass::LoadKind::kDexCacheViaMethod: {
3473 // /* GcRoot<mirror::Class>[] */ out =
3474 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
3475 __ LoadFromOffset(kLoadDoubleword,
3476 out,
3477 current_method_reg,
3478 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
3479 // /* GcRoot<mirror::Class> */ out = out[type_index]
3480 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
3481 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
3482 generate_null_check = !cls->IsInDexCache();
3483 }
3484 }
3485
3486 if (generate_null_check || cls->MustGenerateClinitCheck()) {
3487 DCHECK(cls->CanCallRuntime());
3488 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3489 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3490 codegen_->AddSlowPath(slow_path);
3491 if (generate_null_check) {
3492 __ Beqzc(out, slow_path->GetEntryLabel());
3493 }
3494 if (cls->MustGenerateClinitCheck()) {
3495 GenerateClassInitializationCheck(slow_path, out);
3496 } else {
3497 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003498 }
3499 }
3500}
3501
David Brazdilcb1c0552015-08-04 16:22:25 +01003502static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003503 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003504}
3505
Alexey Frunze4dda3372015-06-01 18:31:49 -07003506void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3507 LocationSummary* locations =
3508 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3509 locations->SetOut(Location::RequiresRegister());
3510}
3511
3512void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3513 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003514 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3515}
3516
3517void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3518 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3519}
3520
3521void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3522 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003523}
3524
Alexey Frunze4dda3372015-06-01 18:31:49 -07003525void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003526 HLoadString::LoadKind load_kind = load->GetLoadKind();
3527 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003529 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
3530 InvokeRuntimeCallingConvention calling_convention;
3531 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
3532 } else {
3533 locations->SetOut(Location::RequiresRegister());
3534 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003535}
3536
3537void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003538 HLoadString::LoadKind load_kind = load->GetLoadKind();
3539 LocationSummary* locations = load->GetLocations();
3540 Location out_loc = locations->Out();
3541 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3542
3543 switch (load_kind) {
3544 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3545 __ LoadLiteral(out,
3546 kLoadUnsignedWord,
3547 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
3548 load->GetStringIndex()));
3549 return; // No dex cache slow path.
3550 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
3551 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
3552 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3553 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
3554 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3555 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3556 return; // No dex cache slow path.
3557 }
3558 case HLoadString::LoadKind::kBootImageAddress: {
3559 DCHECK_NE(load->GetAddress(), 0u);
3560 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
3561 __ LoadLiteral(out,
3562 kLoadUnsignedWord,
3563 codegen_->DeduplicateBootImageAddressLiteral(address));
3564 return; // No dex cache slow path.
3565 }
3566 case HLoadString::LoadKind::kBssEntry: {
3567 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
3568 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3569 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
3570 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3571 __ Lwu(out, AT, /* placeholder */ 0x5678);
3572 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3573 codegen_->AddSlowPath(slow_path);
3574 __ Beqzc(out, slow_path->GetEntryLabel());
3575 __ Bind(slow_path->GetExitLabel());
3576 return;
3577 }
3578 default:
3579 break;
3580 }
3581
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003582 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08003583 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
3584 InvokeRuntimeCallingConvention calling_convention;
3585 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
3586 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
3587 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003588}
3589
Alexey Frunze4dda3372015-06-01 18:31:49 -07003590void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3592 locations->SetOut(Location::ConstantLocation(constant));
3593}
3594
3595void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3596 // Will be generated at use site.
3597}
3598
3599void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3600 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003601 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003602 InvokeRuntimeCallingConvention calling_convention;
3603 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3604}
3605
3606void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003607 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003608 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003609 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003610 if (instruction->IsEnter()) {
3611 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3612 } else {
3613 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3614 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003615}
3616
3617void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3618 LocationSummary* locations =
3619 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3620 switch (mul->GetResultType()) {
3621 case Primitive::kPrimInt:
3622 case Primitive::kPrimLong:
3623 locations->SetInAt(0, Location::RequiresRegister());
3624 locations->SetInAt(1, Location::RequiresRegister());
3625 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3626 break;
3627
3628 case Primitive::kPrimFloat:
3629 case Primitive::kPrimDouble:
3630 locations->SetInAt(0, Location::RequiresFpuRegister());
3631 locations->SetInAt(1, Location::RequiresFpuRegister());
3632 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3633 break;
3634
3635 default:
3636 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3637 }
3638}
3639
3640void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3641 Primitive::Type type = instruction->GetType();
3642 LocationSummary* locations = instruction->GetLocations();
3643
3644 switch (type) {
3645 case Primitive::kPrimInt:
3646 case Primitive::kPrimLong: {
3647 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3648 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3649 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3650 if (type == Primitive::kPrimInt)
3651 __ MulR6(dst, lhs, rhs);
3652 else
3653 __ Dmul(dst, lhs, rhs);
3654 break;
3655 }
3656 case Primitive::kPrimFloat:
3657 case Primitive::kPrimDouble: {
3658 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3659 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3660 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3661 if (type == Primitive::kPrimFloat)
3662 __ MulS(dst, lhs, rhs);
3663 else
3664 __ MulD(dst, lhs, rhs);
3665 break;
3666 }
3667 default:
3668 LOG(FATAL) << "Unexpected mul type " << type;
3669 }
3670}
3671
3672void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3673 LocationSummary* locations =
3674 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3675 switch (neg->GetResultType()) {
3676 case Primitive::kPrimInt:
3677 case Primitive::kPrimLong:
3678 locations->SetInAt(0, Location::RequiresRegister());
3679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3680 break;
3681
3682 case Primitive::kPrimFloat:
3683 case Primitive::kPrimDouble:
3684 locations->SetInAt(0, Location::RequiresFpuRegister());
3685 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3686 break;
3687
3688 default:
3689 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3690 }
3691}
3692
3693void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3694 Primitive::Type type = instruction->GetType();
3695 LocationSummary* locations = instruction->GetLocations();
3696
3697 switch (type) {
3698 case Primitive::kPrimInt:
3699 case Primitive::kPrimLong: {
3700 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3701 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3702 if (type == Primitive::kPrimInt)
3703 __ Subu(dst, ZERO, src);
3704 else
3705 __ Dsubu(dst, ZERO, src);
3706 break;
3707 }
3708 case Primitive::kPrimFloat:
3709 case Primitive::kPrimDouble: {
3710 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3711 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3712 if (type == Primitive::kPrimFloat)
3713 __ NegS(dst, src);
3714 else
3715 __ NegD(dst, src);
3716 break;
3717 }
3718 default:
3719 LOG(FATAL) << "Unexpected neg type " << type;
3720 }
3721}
3722
3723void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3724 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003725 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003726 InvokeRuntimeCallingConvention calling_convention;
3727 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3728 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3729 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3730 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3731}
3732
3733void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3734 LocationSummary* locations = instruction->GetLocations();
3735 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08003736 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(),
3737 instruction->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +01003738 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003739 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3740}
3741
3742void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3743 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003745 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003746 if (instruction->IsStringAlloc()) {
3747 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3748 } else {
3749 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3750 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3751 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003752 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3753}
3754
3755void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003756 if (instruction->IsStringAlloc()) {
3757 // String is allocated through StringFactory. Call NewEmptyString entry point.
3758 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003759 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003760 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003761 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3762 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3763 __ Jalr(T9);
3764 __ Nop();
3765 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3766 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003767 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003768 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3769 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003770}
3771
3772void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3773 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3774 locations->SetInAt(0, Location::RequiresRegister());
3775 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3776}
3777
3778void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3779 Primitive::Type type = instruction->GetType();
3780 LocationSummary* locations = instruction->GetLocations();
3781
3782 switch (type) {
3783 case Primitive::kPrimInt:
3784 case Primitive::kPrimLong: {
3785 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3786 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3787 __ Nor(dst, src, ZERO);
3788 break;
3789 }
3790
3791 default:
3792 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3793 }
3794}
3795
3796void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3797 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3798 locations->SetInAt(0, Location::RequiresRegister());
3799 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3800}
3801
3802void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3803 LocationSummary* locations = instruction->GetLocations();
3804 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3805 locations->InAt(0).AsRegister<GpuRegister>(),
3806 1);
3807}
3808
3809void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003810 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
3811 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003812}
3813
Calin Juravle2ae48182016-03-16 14:05:09 +00003814void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3815 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003816 return;
3817 }
3818 Location obj = instruction->GetLocations()->InAt(0);
3819
3820 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003821 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003822}
3823
Calin Juravle2ae48182016-03-16 14:05:09 +00003824void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003825 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003826 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003827
3828 Location obj = instruction->GetLocations()->InAt(0);
3829
3830 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3831}
3832
3833void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003834 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003835}
3836
3837void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3838 HandleBinaryOp(instruction);
3839}
3840
3841void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3842 HandleBinaryOp(instruction);
3843}
3844
3845void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3846 LOG(FATAL) << "Unreachable";
3847}
3848
3849void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3850 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3851}
3852
3853void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3854 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3855 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3856 if (location.IsStackSlot()) {
3857 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3858 } else if (location.IsDoubleStackSlot()) {
3859 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3860 }
3861 locations->SetOut(location);
3862}
3863
3864void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3865 ATTRIBUTE_UNUSED) {
3866 // Nothing to do, the parameter is already at its location.
3867}
3868
3869void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3870 LocationSummary* locations =
3871 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3872 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3873}
3874
3875void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3876 ATTRIBUTE_UNUSED) {
3877 // Nothing to do, the method is already at its location.
3878}
3879
3880void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3881 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003882 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003883 locations->SetInAt(i, Location::Any());
3884 }
3885 locations->SetOut(Location::Any());
3886}
3887
3888void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3889 LOG(FATAL) << "Unreachable";
3890}
3891
3892void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3893 Primitive::Type type = rem->GetResultType();
3894 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003895 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3896 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003897 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3898
3899 switch (type) {
3900 case Primitive::kPrimInt:
3901 case Primitive::kPrimLong:
3902 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003903 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3905 break;
3906
3907 case Primitive::kPrimFloat:
3908 case Primitive::kPrimDouble: {
3909 InvokeRuntimeCallingConvention calling_convention;
3910 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3911 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3912 locations->SetOut(calling_convention.GetReturnLocation(type));
3913 break;
3914 }
3915
3916 default:
3917 LOG(FATAL) << "Unexpected rem type " << type;
3918 }
3919}
3920
3921void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3922 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003923
3924 switch (type) {
3925 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003926 case Primitive::kPrimLong:
3927 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003928 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003929
3930 case Primitive::kPrimFloat:
3931 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01003932 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
3933 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003934 if (type == Primitive::kPrimFloat) {
3935 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3936 } else {
3937 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3938 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003939 break;
3940 }
3941 default:
3942 LOG(FATAL) << "Unexpected rem type " << type;
3943 }
3944}
3945
3946void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3947 memory_barrier->SetLocations(nullptr);
3948}
3949
3950void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3951 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3952}
3953
3954void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3955 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3956 Primitive::Type return_type = ret->InputAt(0)->GetType();
3957 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3958}
3959
3960void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3961 codegen_->GenerateFrameExit();
3962}
3963
3964void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3965 ret->SetLocations(nullptr);
3966}
3967
3968void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3969 codegen_->GenerateFrameExit();
3970}
3971
Alexey Frunze92d90602015-12-18 18:16:36 -08003972void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3973 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003974}
3975
Alexey Frunze92d90602015-12-18 18:16:36 -08003976void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3977 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003978}
3979
Alexey Frunze4dda3372015-06-01 18:31:49 -07003980void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3981 HandleShift(shl);
3982}
3983
3984void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3985 HandleShift(shl);
3986}
3987
3988void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3989 HandleShift(shr);
3990}
3991
3992void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3993 HandleShift(shr);
3994}
3995
Alexey Frunze4dda3372015-06-01 18:31:49 -07003996void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3997 HandleBinaryOp(instruction);
3998}
3999
4000void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
4001 HandleBinaryOp(instruction);
4002}
4003
4004void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4005 HandleFieldGet(instruction, instruction->GetFieldInfo());
4006}
4007
4008void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4009 HandleFieldGet(instruction, instruction->GetFieldInfo());
4010}
4011
4012void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4013 HandleFieldSet(instruction, instruction->GetFieldInfo());
4014}
4015
4016void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004017 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004018}
4019
Calin Juravlee460d1d2015-09-29 04:52:17 +01004020void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
4021 HUnresolvedInstanceFieldGet* instruction) {
4022 FieldAccessCallingConventionMIPS64 calling_convention;
4023 codegen_->CreateUnresolvedFieldLocationSummary(
4024 instruction, instruction->GetFieldType(), calling_convention);
4025}
4026
4027void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
4028 HUnresolvedInstanceFieldGet* instruction) {
4029 FieldAccessCallingConventionMIPS64 calling_convention;
4030 codegen_->GenerateUnresolvedFieldAccess(instruction,
4031 instruction->GetFieldType(),
4032 instruction->GetFieldIndex(),
4033 instruction->GetDexPc(),
4034 calling_convention);
4035}
4036
4037void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
4038 HUnresolvedInstanceFieldSet* instruction) {
4039 FieldAccessCallingConventionMIPS64 calling_convention;
4040 codegen_->CreateUnresolvedFieldLocationSummary(
4041 instruction, instruction->GetFieldType(), calling_convention);
4042}
4043
4044void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
4045 HUnresolvedInstanceFieldSet* instruction) {
4046 FieldAccessCallingConventionMIPS64 calling_convention;
4047 codegen_->GenerateUnresolvedFieldAccess(instruction,
4048 instruction->GetFieldType(),
4049 instruction->GetFieldIndex(),
4050 instruction->GetDexPc(),
4051 calling_convention);
4052}
4053
4054void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
4055 HUnresolvedStaticFieldGet* instruction) {
4056 FieldAccessCallingConventionMIPS64 calling_convention;
4057 codegen_->CreateUnresolvedFieldLocationSummary(
4058 instruction, instruction->GetFieldType(), calling_convention);
4059}
4060
4061void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
4062 HUnresolvedStaticFieldGet* instruction) {
4063 FieldAccessCallingConventionMIPS64 calling_convention;
4064 codegen_->GenerateUnresolvedFieldAccess(instruction,
4065 instruction->GetFieldType(),
4066 instruction->GetFieldIndex(),
4067 instruction->GetDexPc(),
4068 calling_convention);
4069}
4070
4071void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
4072 HUnresolvedStaticFieldSet* instruction) {
4073 FieldAccessCallingConventionMIPS64 calling_convention;
4074 codegen_->CreateUnresolvedFieldLocationSummary(
4075 instruction, instruction->GetFieldType(), calling_convention);
4076}
4077
4078void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
4079 HUnresolvedStaticFieldSet* instruction) {
4080 FieldAccessCallingConventionMIPS64 calling_convention;
4081 codegen_->GenerateUnresolvedFieldAccess(instruction,
4082 instruction->GetFieldType(),
4083 instruction->GetFieldIndex(),
4084 instruction->GetDexPc(),
4085 calling_convention);
4086}
4087
Alexey Frunze4dda3372015-06-01 18:31:49 -07004088void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004089 LocationSummary* locations =
4090 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004091 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004092}
4093
4094void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
4095 HBasicBlock* block = instruction->GetBlock();
4096 if (block->GetLoopInformation() != nullptr) {
4097 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4098 // The back edge will generate the suspend check.
4099 return;
4100 }
4101 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4102 // The goto will generate the suspend check.
4103 return;
4104 }
4105 GenerateSuspendCheck(instruction, nullptr);
4106}
4107
Alexey Frunze4dda3372015-06-01 18:31:49 -07004108void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
4109 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004110 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004111 InvokeRuntimeCallingConvention calling_convention;
4112 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4113}
4114
4115void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004116 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004117 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4118}
4119
4120void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4121 Primitive::Type input_type = conversion->GetInputType();
4122 Primitive::Type result_type = conversion->GetResultType();
4123 DCHECK_NE(input_type, result_type);
4124
4125 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4126 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4127 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4128 }
4129
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004130 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
4131
4132 if (Primitive::IsFloatingPointType(input_type)) {
4133 locations->SetInAt(0, Location::RequiresFpuRegister());
4134 } else {
4135 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004136 }
4137
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004138 if (Primitive::IsFloatingPointType(result_type)) {
4139 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004140 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004141 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004142 }
4143}
4144
4145void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4146 LocationSummary* locations = conversion->GetLocations();
4147 Primitive::Type result_type = conversion->GetResultType();
4148 Primitive::Type input_type = conversion->GetInputType();
4149
4150 DCHECK_NE(input_type, result_type);
4151
4152 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4153 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4154 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4155
4156 switch (result_type) {
4157 case Primitive::kPrimChar:
4158 __ Andi(dst, src, 0xFFFF);
4159 break;
4160 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004161 if (input_type == Primitive::kPrimLong) {
4162 // Type conversion from long to types narrower than int is a result of code
4163 // transformations. To avoid unpredictable results for SEB and SEH, we first
4164 // need to sign-extend the low 32-bit value into bits 32 through 63.
4165 __ Sll(dst, src, 0);
4166 __ Seb(dst, dst);
4167 } else {
4168 __ Seb(dst, src);
4169 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004170 break;
4171 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004172 if (input_type == Primitive::kPrimLong) {
4173 // Type conversion from long to types narrower than int is a result of code
4174 // transformations. To avoid unpredictable results for SEB and SEH, we first
4175 // need to sign-extend the low 32-bit value into bits 32 through 63.
4176 __ Sll(dst, src, 0);
4177 __ Seh(dst, dst);
4178 } else {
4179 __ Seh(dst, src);
4180 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004181 break;
4182 case Primitive::kPrimInt:
4183 case Primitive::kPrimLong:
4184 // Sign-extend 32-bit int into bits 32 through 63 for
4185 // int-to-long and long-to-int conversions
4186 __ Sll(dst, src, 0);
4187 break;
4188
4189 default:
4190 LOG(FATAL) << "Unexpected type conversion from " << input_type
4191 << " to " << result_type;
4192 }
4193 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004194 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4195 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4196 if (input_type == Primitive::kPrimLong) {
4197 __ Dmtc1(src, FTMP);
4198 if (result_type == Primitive::kPrimFloat) {
4199 __ Cvtsl(dst, FTMP);
4200 } else {
4201 __ Cvtdl(dst, FTMP);
4202 }
4203 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004204 __ Mtc1(src, FTMP);
4205 if (result_type == Primitive::kPrimFloat) {
4206 __ Cvtsw(dst, FTMP);
4207 } else {
4208 __ Cvtdw(dst, FTMP);
4209 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004210 }
4211 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4212 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004213 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4214 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4215 Mips64Label truncate;
4216 Mips64Label done;
4217
4218 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4219 // value when the input is either a NaN or is outside of the range of the output type
4220 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4221 // the same result.
4222 //
4223 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4224 // value of the output type if the input is outside of the range after the truncation or
4225 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4226 // results. This matches the desired float/double-to-int/long conversion exactly.
4227 //
4228 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4229 //
4230 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4231 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4232 // even though it must be NAN2008=1 on R6.
4233 //
4234 // The code takes care of the different behaviors by first comparing the input to the
4235 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4236 // If the input is greater than or equal to the minimum, it procedes to the truncate
4237 // instruction, which will handle such an input the same way irrespective of NAN2008.
4238 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4239 // in order to return either zero or the minimum value.
4240 //
4241 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4242 // truncate instruction for MIPS64R6.
4243 if (input_type == Primitive::kPrimFloat) {
4244 uint32_t min_val = (result_type == Primitive::kPrimLong)
4245 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4246 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4247 __ LoadConst32(TMP, min_val);
4248 __ Mtc1(TMP, FTMP);
4249 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004250 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004251 uint64_t min_val = (result_type == Primitive::kPrimLong)
4252 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4253 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4254 __ LoadConst64(TMP, min_val);
4255 __ Dmtc1(TMP, FTMP);
4256 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004257 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004258
4259 __ Bc1nez(FTMP, &truncate);
4260
4261 if (input_type == Primitive::kPrimFloat) {
4262 __ CmpEqS(FTMP, src, src);
4263 } else {
4264 __ CmpEqD(FTMP, src, src);
4265 }
4266 if (result_type == Primitive::kPrimLong) {
4267 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4268 } else {
4269 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4270 }
4271 __ Mfc1(TMP, FTMP);
4272 __ And(dst, dst, TMP);
4273
4274 __ Bc(&done);
4275
4276 __ Bind(&truncate);
4277
4278 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004279 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004280 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004281 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004282 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004283 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004284 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004285 } else {
4286 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004287 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004288 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004289 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004290 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004291 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004292 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004293
4294 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004295 } else if (Primitive::IsFloatingPointType(result_type) &&
4296 Primitive::IsFloatingPointType(input_type)) {
4297 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4298 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4299 if (result_type == Primitive::kPrimFloat) {
4300 __ Cvtsd(dst, src);
4301 } else {
4302 __ Cvtds(dst, src);
4303 }
4304 } else {
4305 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4306 << " to " << result_type;
4307 }
4308}
4309
4310void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4311 HandleShift(ushr);
4312}
4313
4314void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4315 HandleShift(ushr);
4316}
4317
4318void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4319 HandleBinaryOp(instruction);
4320}
4321
4322void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4323 HandleBinaryOp(instruction);
4324}
4325
4326void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4327 // Nothing to do, this should be removed during prepare for register allocator.
4328 LOG(FATAL) << "Unreachable";
4329}
4330
4331void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4332 // Nothing to do, this should be removed during prepare for register allocator.
4333 LOG(FATAL) << "Unreachable";
4334}
4335
4336void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004337 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004338}
4339
4340void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004341 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004342}
4343
4344void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004345 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004346}
4347
4348void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004349 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004350}
4351
4352void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004353 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004354}
4355
4356void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004357 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004358}
4359
4360void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004361 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004362}
4363
4364void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004365 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004366}
4367
4368void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004369 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004370}
4371
4372void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004373 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004374}
4375
4376void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004377 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004378}
4379
4380void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004381 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004382}
4383
Aart Bike9f37602015-10-09 11:15:55 -07004384void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004385 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004386}
4387
4388void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004389 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004390}
4391
4392void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004393 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004394}
4395
4396void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004397 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004398}
4399
4400void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004401 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004402}
4403
4404void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004405 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004406}
4407
4408void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004409 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004410}
4411
4412void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004413 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004414}
4415
Mark Mendellfe57faa2015-09-18 09:26:15 -04004416// Simple implementation of packed switch - generate cascaded compare/jumps.
4417void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4418 LocationSummary* locations =
4419 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4420 locations->SetInAt(0, Location::RequiresRegister());
4421}
4422
4423void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4424 int32_t lower_bound = switch_instr->GetStartValue();
4425 int32_t num_entries = switch_instr->GetNumEntries();
4426 LocationSummary* locations = switch_instr->GetLocations();
4427 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4428 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4429
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004430 // Create a set of compare/jumps.
4431 GpuRegister temp_reg = TMP;
4432 if (IsInt<16>(-lower_bound)) {
4433 __ Addiu(temp_reg, value_reg, -lower_bound);
4434 } else {
4435 __ LoadConst32(AT, -lower_bound);
4436 __ Addu(temp_reg, value_reg, AT);
4437 }
4438 // Jump to default if index is negative
4439 // Note: We don't check the case that index is positive while value < lower_bound, because in
4440 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4441 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4442
Mark Mendellfe57faa2015-09-18 09:26:15 -04004443 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004444 // Jump to successors[0] if value == lower_bound.
4445 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4446 int32_t last_index = 0;
4447 for (; num_entries - last_index > 2; last_index += 2) {
4448 __ Addiu(temp_reg, temp_reg, -2);
4449 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4450 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4451 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4452 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4453 }
4454 if (num_entries - last_index == 2) {
4455 // The last missing case_value.
4456 __ Addiu(temp_reg, temp_reg, -1);
4457 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004458 }
4459
4460 // And the default for any other value.
4461 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004462 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004463 }
4464}
4465
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004466void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4467 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4468}
4469
4470void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4471 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4472}
4473
Alexey Frunze4dda3372015-06-01 18:31:49 -07004474} // namespace mips64
4475} // namespace art