blob: 02576bda676e8b7be2d9713646be64952edbb825 [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 Frunze4dda3372015-06-01 18:31:49 -070021#include "entrypoints/quick/quick_entrypoints.h"
22#include "entrypoints/quick/quick_entrypoints_enum.h"
23#include "gc/accounting/card_table.h"
24#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070025#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070026#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "offsets.h"
29#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070030#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "utils/stack_checks.h"
33
34namespace art {
35namespace mips64 {
36
37static constexpr int kCurrentMethodStackOffset = 0;
38static constexpr GpuRegister kMethodRegisterArgument = A0;
39
Alexey Frunze4dda3372015-06-01 18:31:49 -070040Location Mips64ReturnLocation(Primitive::Type return_type) {
41 switch (return_type) {
42 case Primitive::kPrimBoolean:
43 case Primitive::kPrimByte:
44 case Primitive::kPrimChar:
45 case Primitive::kPrimShort:
46 case Primitive::kPrimInt:
47 case Primitive::kPrimNot:
48 case Primitive::kPrimLong:
49 return Location::RegisterLocation(V0);
50
51 case Primitive::kPrimFloat:
52 case Primitive::kPrimDouble:
53 return Location::FpuRegisterLocation(F0);
54
55 case Primitive::kPrimVoid:
56 return Location();
57 }
58 UNREACHABLE();
59}
60
61Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
62 return Mips64ReturnLocation(type);
63}
64
65Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
66 return Location::RegisterLocation(kMethodRegisterArgument);
67}
68
69Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
70 Location next_location;
71 if (type == Primitive::kPrimVoid) {
72 LOG(FATAL) << "Unexpected parameter type " << type;
73 }
74
75 if (Primitive::IsFloatingPointType(type) &&
76 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
77 next_location = Location::FpuRegisterLocation(
78 calling_convention.GetFpuRegisterAt(float_index_++));
79 gp_index_++;
80 } else if (!Primitive::IsFloatingPointType(type) &&
81 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
82 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
83 float_index_++;
84 } else {
85 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
86 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
87 : Location::StackSlot(stack_offset);
88 }
89
90 // Space on the stack is reserved for all arguments.
91 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
92
93 // TODO: review
94
95 // TODO: shouldn't we use a whole machine word per argument on the stack?
96 // Implicit 4-byte method pointer (and such) will cause misalignment.
97
98 return next_location;
99}
100
101Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
102 return Mips64ReturnLocation(type);
103}
104
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100105// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
106#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108
109class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
110 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000111 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700112
113 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100114 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700115 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
116 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000117 if (instruction_->CanThrowIntoCatchBlock()) {
118 // Live registers will be restored in the catch block if caught.
119 SaveLiveRegisters(codegen, instruction_->GetLocations());
120 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 // We're moving two locations to locations that could overlap, so we need a parallel
122 // move resolver.
123 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100124 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700125 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
126 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100127 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700128 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
129 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100130 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
131 ? kQuickThrowStringBounds
132 : kQuickThrowArrayBounds;
133 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100134 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700135 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
136 }
137
Alexandre Rames8158f282015-08-07 10:26:17 +0100138 bool IsFatal() const OVERRIDE { return true; }
139
Roland Levillain46648892015-06-19 16:07:18 +0100140 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
141
Alexey Frunze4dda3372015-06-01 18:31:49 -0700142 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700143 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
144};
145
146class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
147 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000148 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700149
150 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
151 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
152 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100153 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700154 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
155 }
156
Alexandre Rames8158f282015-08-07 10:26:17 +0100157 bool IsFatal() const OVERRIDE { return true; }
158
Roland Levillain46648892015-06-19 16:07:18 +0100159 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
160
Alexey Frunze4dda3372015-06-01 18:31:49 -0700161 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700162 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
163};
164
165class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
166 public:
167 LoadClassSlowPathMIPS64(HLoadClass* cls,
168 HInstruction* at,
169 uint32_t dex_pc,
170 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000171 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700172 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
173 }
174
175 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
176 LocationSummary* locations = at_->GetLocations();
177 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
178
179 __ Bind(GetEntryLabel());
180 SaveLiveRegisters(codegen, locations);
181
182 InvokeRuntimeCallingConvention calling_convention;
183 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +0100184 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
185 : kQuickInitializeType;
186 mips64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700187 if (do_clinit_) {
188 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
189 } else {
190 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
191 }
192
193 // Move the class to the desired location.
194 Location out = locations->Out();
195 if (out.IsValid()) {
196 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
197 Primitive::Type type = at_->GetType();
198 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
199 }
200
201 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700202 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700203 }
204
Roland Levillain46648892015-06-19 16:07:18 +0100205 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
206
Alexey Frunze4dda3372015-06-01 18:31:49 -0700207 private:
208 // The class this slow path will load.
209 HLoadClass* const cls_;
210
211 // The instruction where this slow path is happening.
212 // (Might be the load class or an initialization check).
213 HInstruction* const at_;
214
215 // The dex PC of `at_`.
216 const uint32_t dex_pc_;
217
218 // Whether to initialize the class.
219 const bool do_clinit_;
220
221 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
222};
223
224class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
225 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000226 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700227
228 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
229 LocationSummary* locations = instruction_->GetLocations();
230 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
231 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
232
233 __ Bind(GetEntryLabel());
234 SaveLiveRegisters(codegen, locations);
235
236 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000237 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
238 __ 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 Frunzea0e87b02015-09-24 22:57:20 -0700250 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700251 }
252
Roland Levillain46648892015-06-19 16:07:18 +0100253 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
254
Alexey Frunze4dda3372015-06-01 18:31:49 -0700255 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700256 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
257};
258
259class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
260 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000261 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700262
263 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
264 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
265 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000266 if (instruction_->CanThrowIntoCatchBlock()) {
267 // Live registers will be restored in the catch block if caught.
268 SaveLiveRegisters(codegen, instruction_->GetLocations());
269 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100270 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700271 instruction_,
272 instruction_->GetDexPc(),
273 this);
274 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
275 }
276
Alexandre Rames8158f282015-08-07 10:26:17 +0100277 bool IsFatal() const OVERRIDE { return true; }
278
Roland Levillain46648892015-06-19 16:07:18 +0100279 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
280
Alexey Frunze4dda3372015-06-01 18:31:49 -0700281 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
283};
284
285class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
286 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100287 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000288 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700289
290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
291 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
292 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100293 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700296 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700297 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700298 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700299 }
300 }
301
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700302 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700303 DCHECK(successor_ == nullptr);
304 return &return_label_;
305 }
306
Roland Levillain46648892015-06-19 16:07:18 +0100307 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
308
Alexey Frunze4dda3372015-06-01 18:31:49 -0700309 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 // If not null, the block to branch to after the suspend check.
311 HBasicBlock* const successor_;
312
313 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315
316 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
317};
318
319class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
320 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200325 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100326 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327 DCHECK(instruction_->IsCheckCast()
328 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
329 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
330
331 __ Bind(GetEntryLabel());
332 SaveLiveRegisters(codegen, locations);
333
334 // We're moving two locations to locations that could overlap, so we need a parallel
335 // move resolver.
336 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100337 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700338 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
339 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100340 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700341 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
342 Primitive::kPrimNot);
343
344 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100345 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000346 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700347 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700348 Primitive::Type ret_type = instruction_->GetType();
349 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
350 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700351 } else {
352 DCHECK(instruction_->IsCheckCast());
Serban Constantinescufc734082016-07-19 17:18:07 +0100353 mips64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700354 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
355 }
356
357 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700358 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700359 }
360
Roland Levillain46648892015-06-19 16:07:18 +0100361 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
362
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
365};
366
367class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
368 public:
Aart Bik42249c32016-01-07 15:33:50 -0800369 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000370 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700371
372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800373 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700374 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100375 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000376 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 }
378
Roland Levillain46648892015-06-19 16:07:18 +0100379 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
380
Alexey Frunze4dda3372015-06-01 18:31:49 -0700381 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
383};
384
385CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
386 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100387 const CompilerOptions& compiler_options,
388 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700389 : CodeGenerator(graph,
390 kNumberOfGpuRegisters,
391 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000392 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700393 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
394 arraysize(kCoreCalleeSaves)),
395 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
396 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100397 compiler_options,
398 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100399 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700400 location_builder_(graph, this),
401 instruction_visitor_(graph, this),
402 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100403 assembler_(graph->GetArena()),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700404 isa_features_(isa_features) {
405 // Save RA (containing the return address) to mimic Quick.
406 AddAllocatedRegister(Location::RegisterLocation(RA));
407}
408
409#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100410// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
411#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700412#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413
414void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700415 // Ensure that we fix up branches.
416 __ FinalizeCode();
417
418 // Adjust native pc offsets in stack maps.
419 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
420 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
421 uint32_t new_position = __ GetAdjustedPosition(old_position);
422 DCHECK_GE(new_position, old_position);
423 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
424 }
425
426 // Adjust pc offsets for the disassembly information.
427 if (disasm_info_ != nullptr) {
428 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
429 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
430 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
431 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
432 it.second.start = __ GetAdjustedPosition(it.second.start);
433 it.second.end = __ GetAdjustedPosition(it.second.end);
434 }
435 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
436 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
437 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
438 }
439 }
440
Alexey Frunze4dda3372015-06-01 18:31:49 -0700441 CodeGenerator::Finalize(allocator);
442}
443
444Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
445 return codegen_->GetAssembler();
446}
447
448void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100449 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700450 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
451}
452
453void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100454 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700455 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
456}
457
458void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
459 // Pop reg
460 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200461 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700462}
463
464void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
465 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200466 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700467 __ Sd(GpuRegister(reg), SP, 0);
468}
469
470void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
471 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
472 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
473 // Allocate a scratch register other than TMP, if available.
474 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
475 // automatically unspilled when the scratch scope object is destroyed).
476 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
477 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200478 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700479 __ LoadFromOffset(load_type,
480 GpuRegister(ensure_scratch.GetRegister()),
481 SP,
482 index1 + stack_offset);
483 __ LoadFromOffset(load_type,
484 TMP,
485 SP,
486 index2 + stack_offset);
487 __ StoreToOffset(store_type,
488 GpuRegister(ensure_scratch.GetRegister()),
489 SP,
490 index2 + stack_offset);
491 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
492}
493
494static dwarf::Reg DWARFReg(GpuRegister reg) {
495 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
496}
497
David Srbeckyba702002016-02-01 18:15:29 +0000498static dwarf::Reg DWARFReg(FpuRegister reg) {
499 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
500}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700501
502void CodeGeneratorMIPS64::GenerateFrameEntry() {
503 __ Bind(&frame_entry_label_);
504
505 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
506
507 if (do_overflow_check) {
508 __ LoadFromOffset(kLoadWord,
509 ZERO,
510 SP,
511 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
512 RecordPcInfo(nullptr, 0);
513 }
514
515 // TODO: anything related to T9/GP/GOT/PIC/.so's?
516
517 if (HasEmptyFrame()) {
518 return;
519 }
520
521 // Make sure the frame size isn't unreasonably large. Per the various APIs
522 // it looks like it should always be less than 2GB in size, which allows
523 // us using 32-bit signed offsets from the stack pointer.
524 if (GetFrameSize() > 0x7FFFFFFF)
525 LOG(FATAL) << "Stack frame larger than 2GB";
526
527 // Spill callee-saved registers.
528 // Note that their cumulative size is small and they can be indexed using
529 // 16-bit offsets.
530
531 // TODO: increment/decrement SP in one step instead of two or remove this comment.
532
533 uint32_t ofs = FrameEntrySpillSize();
534 __ IncreaseFrameSize(ofs);
535
536 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
537 GpuRegister reg = kCoreCalleeSaves[i];
538 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200539 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700540 __ Sd(reg, SP, ofs);
541 __ cfi().RelOffset(DWARFReg(reg), ofs);
542 }
543 }
544
545 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
546 FpuRegister reg = kFpuCalleeSaves[i];
547 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200548 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700549 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000550 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700551 }
552 }
553
554 // Allocate the rest of the frame and store the current method pointer
555 // at its end.
556
557 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
558
559 static_assert(IsInt<16>(kCurrentMethodStackOffset),
560 "kCurrentMethodStackOffset must fit into int16_t");
561 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
562}
563
564void CodeGeneratorMIPS64::GenerateFrameExit() {
565 __ cfi().RememberState();
566
567 // TODO: anything related to T9/GP/GOT/PIC/.so's?
568
569 if (!HasEmptyFrame()) {
570 // Deallocate the rest of the frame.
571
572 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
573
574 // Restore callee-saved registers.
575 // Note that their cumulative size is small and they can be indexed using
576 // 16-bit offsets.
577
578 // TODO: increment/decrement SP in one step instead of two or remove this comment.
579
580 uint32_t ofs = 0;
581
582 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
583 FpuRegister reg = kFpuCalleeSaves[i];
584 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
585 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200586 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000587 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700588 }
589 }
590
591 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
592 GpuRegister reg = kCoreCalleeSaves[i];
593 if (allocated_registers_.ContainsCoreRegister(reg)) {
594 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200595 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700596 __ cfi().Restore(DWARFReg(reg));
597 }
598 }
599
600 DCHECK_EQ(ofs, FrameEntrySpillSize());
601 __ DecreaseFrameSize(ofs);
602 }
603
604 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700605 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700606
607 __ cfi().RestoreState();
608 __ cfi().DefCFAOffset(GetFrameSize());
609}
610
611void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
612 __ Bind(GetLabelOf(block));
613}
614
615void CodeGeneratorMIPS64::MoveLocation(Location destination,
616 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100617 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700618 if (source.Equals(destination)) {
619 return;
620 }
621
622 // A valid move can always be inferred from the destination and source
623 // locations. When moving from and to a register, the argument type can be
624 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100625 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700626 DCHECK_EQ(unspecified_type, false);
627
628 if (destination.IsRegister() || destination.IsFpuRegister()) {
629 if (unspecified_type) {
630 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
631 if (source.IsStackSlot() ||
632 (src_cst != nullptr && (src_cst->IsIntConstant()
633 || src_cst->IsFloatConstant()
634 || src_cst->IsNullConstant()))) {
635 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100636 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700637 } else {
638 // If the source is a double stack slot or a 64bit constant, a 64bit
639 // type is appropriate. Else the source is a register, and since the
640 // type has not been specified, we chose a 64bit type to force a 64bit
641 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100642 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700643 }
644 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100645 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
646 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700647 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
648 // Move to GPR/FPR from stack
649 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100650 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700651 __ LoadFpuFromOffset(load_type,
652 destination.AsFpuRegister<FpuRegister>(),
653 SP,
654 source.GetStackIndex());
655 } else {
656 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
657 __ LoadFromOffset(load_type,
658 destination.AsRegister<GpuRegister>(),
659 SP,
660 source.GetStackIndex());
661 }
662 } else if (source.IsConstant()) {
663 // Move to GPR/FPR from constant
664 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100665 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700666 gpr = destination.AsRegister<GpuRegister>();
667 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100668 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700669 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
670 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
671 gpr = ZERO;
672 } else {
673 __ LoadConst32(gpr, value);
674 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700675 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700676 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
677 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
678 gpr = ZERO;
679 } else {
680 __ LoadConst64(gpr, value);
681 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700682 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100683 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700684 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100685 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700686 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
687 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100688 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700689 if (destination.IsRegister()) {
690 // Move to GPR from GPR
691 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
692 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100693 DCHECK(destination.IsFpuRegister());
694 if (Primitive::Is64BitType(dst_type)) {
695 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
696 } else {
697 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
698 }
699 }
700 } else if (source.IsFpuRegister()) {
701 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700702 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100703 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700704 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
705 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100706 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700707 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
708 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100709 } else {
710 DCHECK(destination.IsRegister());
711 if (Primitive::Is64BitType(dst_type)) {
712 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
713 } else {
714 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
715 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700716 }
717 }
718 } else { // The destination is not a register. It must be a stack slot.
719 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
720 if (source.IsRegister() || source.IsFpuRegister()) {
721 if (unspecified_type) {
722 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100723 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700724 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100725 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700726 }
727 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100728 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
729 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 // Move to stack from GPR/FPR
731 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
732 if (source.IsRegister()) {
733 __ StoreToOffset(store_type,
734 source.AsRegister<GpuRegister>(),
735 SP,
736 destination.GetStackIndex());
737 } else {
738 __ StoreFpuToOffset(store_type,
739 source.AsFpuRegister<FpuRegister>(),
740 SP,
741 destination.GetStackIndex());
742 }
743 } else if (source.IsConstant()) {
744 // Move to stack from constant
745 HConstant* src_cst = source.GetConstant();
746 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700747 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700748 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700749 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
750 if (value != 0) {
751 gpr = TMP;
752 __ LoadConst32(gpr, value);
753 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700754 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700755 DCHECK(destination.IsDoubleStackSlot());
756 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
757 if (value != 0) {
758 gpr = TMP;
759 __ LoadConst64(gpr, value);
760 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700761 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700762 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700763 } else {
764 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
765 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
766 // Move to stack from stack
767 if (destination.IsStackSlot()) {
768 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
769 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
770 } else {
771 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
772 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
773 }
774 }
775 }
776}
777
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700778void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700779 DCHECK(!loc1.IsConstant());
780 DCHECK(!loc2.IsConstant());
781
782 if (loc1.Equals(loc2)) {
783 return;
784 }
785
786 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
787 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
788 bool is_fp_reg1 = loc1.IsFpuRegister();
789 bool is_fp_reg2 = loc2.IsFpuRegister();
790
791 if (loc2.IsRegister() && loc1.IsRegister()) {
792 // Swap 2 GPRs
793 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
794 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
795 __ Move(TMP, r2);
796 __ Move(r2, r1);
797 __ Move(r1, TMP);
798 } else if (is_fp_reg2 && is_fp_reg1) {
799 // Swap 2 FPRs
800 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
801 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700802 if (type == Primitive::kPrimFloat) {
803 __ MovS(FTMP, r1);
804 __ MovS(r1, r2);
805 __ MovS(r2, FTMP);
806 } else {
807 DCHECK_EQ(type, Primitive::kPrimDouble);
808 __ MovD(FTMP, r1);
809 __ MovD(r1, r2);
810 __ MovD(r2, FTMP);
811 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700812 } else if (is_slot1 != is_slot2) {
813 // Swap GPR/FPR and stack slot
814 Location reg_loc = is_slot1 ? loc2 : loc1;
815 Location mem_loc = is_slot1 ? loc1 : loc2;
816 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
817 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
818 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
819 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
820 if (reg_loc.IsFpuRegister()) {
821 __ StoreFpuToOffset(store_type,
822 reg_loc.AsFpuRegister<FpuRegister>(),
823 SP,
824 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700825 if (mem_loc.IsStackSlot()) {
826 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
827 } else {
828 DCHECK(mem_loc.IsDoubleStackSlot());
829 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
830 }
831 } else {
832 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
833 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
834 }
835 } else if (is_slot1 && is_slot2) {
836 move_resolver_.Exchange(loc1.GetStackIndex(),
837 loc2.GetStackIndex(),
838 loc1.IsDoubleStackSlot());
839 } else {
840 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
841 }
842}
843
Calin Juravle175dc732015-08-25 15:42:32 +0100844void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
845 DCHECK(location.IsRegister());
846 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
847}
848
Calin Juravlee460d1d2015-09-29 04:52:17 +0100849void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
850 if (location.IsRegister()) {
851 locations->AddTemp(location);
852 } else {
853 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
854 }
855}
856
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100857void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
858 GpuRegister value,
859 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700860 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700861 GpuRegister card = AT;
862 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100863 if (value_can_be_null) {
864 __ Beqzc(value, &done);
865 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700866 __ LoadFromOffset(kLoadDoubleword,
867 card,
868 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700869 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700870 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
871 __ Daddu(temp, card, temp);
872 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100873 if (value_can_be_null) {
874 __ Bind(&done);
875 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700876}
877
David Brazdil58282f42016-01-14 12:45:10 +0000878void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700879 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
880 blocked_core_registers_[ZERO] = true;
881 blocked_core_registers_[K0] = true;
882 blocked_core_registers_[K1] = true;
883 blocked_core_registers_[GP] = true;
884 blocked_core_registers_[SP] = true;
885 blocked_core_registers_[RA] = true;
886
Lazar Trsicd9672662015-09-03 17:33:01 +0200887 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
888 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700889 blocked_core_registers_[AT] = true;
890 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200891 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700892 blocked_fpu_registers_[FTMP] = true;
893
894 // Reserve suspend and thread registers.
895 blocked_core_registers_[S0] = true;
896 blocked_core_registers_[TR] = true;
897
898 // Reserve T9 for function calls
899 blocked_core_registers_[T9] = true;
900
901 // TODO: review; anything else?
902
Goran Jakovljevic782be112016-06-21 12:39:04 +0200903 if (GetGraph()->IsDebuggable()) {
904 // Stubs do not save callee-save floating point registers. If the graph
905 // is debuggable, we need to deal with these registers differently. For
906 // now, just block them.
907 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
908 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
909 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700910 }
911}
912
Alexey Frunze4dda3372015-06-01 18:31:49 -0700913size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
914 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200915 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700916}
917
918size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
919 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200920 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700921}
922
923size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
924 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200925 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700926}
927
928size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
929 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200930 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700931}
932
933void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100934 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700935}
936
937void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100938 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700939}
940
Calin Juravle175dc732015-08-25 15:42:32 +0100941void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700942 HInstruction* instruction,
943 uint32_t dex_pc,
944 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100945 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700946 // TODO: anything related to T9/GP/GOT/PIC/.so's?
Serban Constantinescufc734082016-07-19 17:18:07 +0100947 __ LoadFromOffset(kLoadDoubleword,
948 T9,
949 TR,
950 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700952 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +0100953 if (EntrypointRequiresStackMap(entrypoint)) {
954 RecordPcInfo(instruction, dex_pc, slow_path);
955 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700956}
957
958void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
959 GpuRegister class_reg) {
960 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
961 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
962 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
963 // TODO: barrier needed?
964 __ Bind(slow_path->GetExitLabel());
965}
966
967void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
968 __ Sync(0); // only stype 0 is supported
969}
970
971void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
972 HBasicBlock* successor) {
973 SuspendCheckSlowPathMIPS64* slow_path =
974 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
975 codegen_->AddSlowPath(slow_path);
976
977 __ LoadFromOffset(kLoadUnsignedHalfword,
978 TMP,
979 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700980 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700981 if (successor == nullptr) {
982 __ Bnezc(TMP, slow_path->GetEntryLabel());
983 __ Bind(slow_path->GetReturnLabel());
984 } else {
985 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700986 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700987 // slow_path will return to GetLabelOf(successor).
988 }
989}
990
991InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
992 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800993 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700994 assembler_(codegen->GetAssembler()),
995 codegen_(codegen) {}
996
997void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
998 DCHECK_EQ(instruction->InputCount(), 2U);
999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1000 Primitive::Type type = instruction->GetResultType();
1001 switch (type) {
1002 case Primitive::kPrimInt:
1003 case Primitive::kPrimLong: {
1004 locations->SetInAt(0, Location::RequiresRegister());
1005 HInstruction* right = instruction->InputAt(1);
1006 bool can_use_imm = false;
1007 if (right->IsConstant()) {
1008 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1009 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1010 can_use_imm = IsUint<16>(imm);
1011 } else if (instruction->IsAdd()) {
1012 can_use_imm = IsInt<16>(imm);
1013 } else {
1014 DCHECK(instruction->IsSub());
1015 can_use_imm = IsInt<16>(-imm);
1016 }
1017 }
1018 if (can_use_imm)
1019 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1020 else
1021 locations->SetInAt(1, Location::RequiresRegister());
1022 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1023 }
1024 break;
1025
1026 case Primitive::kPrimFloat:
1027 case Primitive::kPrimDouble:
1028 locations->SetInAt(0, Location::RequiresFpuRegister());
1029 locations->SetInAt(1, Location::RequiresFpuRegister());
1030 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1031 break;
1032
1033 default:
1034 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1035 }
1036}
1037
1038void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1039 Primitive::Type type = instruction->GetType();
1040 LocationSummary* locations = instruction->GetLocations();
1041
1042 switch (type) {
1043 case Primitive::kPrimInt:
1044 case Primitive::kPrimLong: {
1045 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1046 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1047 Location rhs_location = locations->InAt(1);
1048
1049 GpuRegister rhs_reg = ZERO;
1050 int64_t rhs_imm = 0;
1051 bool use_imm = rhs_location.IsConstant();
1052 if (use_imm) {
1053 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1054 } else {
1055 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1056 }
1057
1058 if (instruction->IsAnd()) {
1059 if (use_imm)
1060 __ Andi(dst, lhs, rhs_imm);
1061 else
1062 __ And(dst, lhs, rhs_reg);
1063 } else if (instruction->IsOr()) {
1064 if (use_imm)
1065 __ Ori(dst, lhs, rhs_imm);
1066 else
1067 __ Or(dst, lhs, rhs_reg);
1068 } else if (instruction->IsXor()) {
1069 if (use_imm)
1070 __ Xori(dst, lhs, rhs_imm);
1071 else
1072 __ Xor(dst, lhs, rhs_reg);
1073 } else if (instruction->IsAdd()) {
1074 if (type == Primitive::kPrimInt) {
1075 if (use_imm)
1076 __ Addiu(dst, lhs, rhs_imm);
1077 else
1078 __ Addu(dst, lhs, rhs_reg);
1079 } else {
1080 if (use_imm)
1081 __ Daddiu(dst, lhs, rhs_imm);
1082 else
1083 __ Daddu(dst, lhs, rhs_reg);
1084 }
1085 } else {
1086 DCHECK(instruction->IsSub());
1087 if (type == Primitive::kPrimInt) {
1088 if (use_imm)
1089 __ Addiu(dst, lhs, -rhs_imm);
1090 else
1091 __ Subu(dst, lhs, rhs_reg);
1092 } else {
1093 if (use_imm)
1094 __ Daddiu(dst, lhs, -rhs_imm);
1095 else
1096 __ Dsubu(dst, lhs, rhs_reg);
1097 }
1098 }
1099 break;
1100 }
1101 case Primitive::kPrimFloat:
1102 case Primitive::kPrimDouble: {
1103 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1104 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1105 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1106 if (instruction->IsAdd()) {
1107 if (type == Primitive::kPrimFloat)
1108 __ AddS(dst, lhs, rhs);
1109 else
1110 __ AddD(dst, lhs, rhs);
1111 } else if (instruction->IsSub()) {
1112 if (type == Primitive::kPrimFloat)
1113 __ SubS(dst, lhs, rhs);
1114 else
1115 __ SubD(dst, lhs, rhs);
1116 } else {
1117 LOG(FATAL) << "Unexpected floating-point binary operation";
1118 }
1119 break;
1120 }
1121 default:
1122 LOG(FATAL) << "Unexpected binary operation type " << type;
1123 }
1124}
1125
1126void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001127 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001128
1129 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1130 Primitive::Type type = instr->GetResultType();
1131 switch (type) {
1132 case Primitive::kPrimInt:
1133 case Primitive::kPrimLong: {
1134 locations->SetInAt(0, Location::RequiresRegister());
1135 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001137 break;
1138 }
1139 default:
1140 LOG(FATAL) << "Unexpected shift type " << type;
1141 }
1142}
1143
1144void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001145 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001146 LocationSummary* locations = instr->GetLocations();
1147 Primitive::Type type = instr->GetType();
1148
1149 switch (type) {
1150 case Primitive::kPrimInt:
1151 case Primitive::kPrimLong: {
1152 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1153 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1154 Location rhs_location = locations->InAt(1);
1155
1156 GpuRegister rhs_reg = ZERO;
1157 int64_t rhs_imm = 0;
1158 bool use_imm = rhs_location.IsConstant();
1159 if (use_imm) {
1160 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1161 } else {
1162 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1163 }
1164
1165 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001166 uint32_t shift_value = rhs_imm &
1167 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001168
Alexey Frunze92d90602015-12-18 18:16:36 -08001169 if (shift_value == 0) {
1170 if (dst != lhs) {
1171 __ Move(dst, lhs);
1172 }
1173 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001174 if (instr->IsShl()) {
1175 __ Sll(dst, lhs, shift_value);
1176 } else if (instr->IsShr()) {
1177 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001178 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001179 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001180 } else {
1181 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001182 }
1183 } else {
1184 if (shift_value < 32) {
1185 if (instr->IsShl()) {
1186 __ Dsll(dst, lhs, shift_value);
1187 } else if (instr->IsShr()) {
1188 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001189 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001190 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001191 } else {
1192 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001193 }
1194 } else {
1195 shift_value -= 32;
1196 if (instr->IsShl()) {
1197 __ Dsll32(dst, lhs, shift_value);
1198 } else if (instr->IsShr()) {
1199 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001200 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001201 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001202 } else {
1203 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001204 }
1205 }
1206 }
1207 } else {
1208 if (type == Primitive::kPrimInt) {
1209 if (instr->IsShl()) {
1210 __ Sllv(dst, lhs, rhs_reg);
1211 } else if (instr->IsShr()) {
1212 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001213 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001214 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001215 } else {
1216 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001217 }
1218 } else {
1219 if (instr->IsShl()) {
1220 __ Dsllv(dst, lhs, rhs_reg);
1221 } else if (instr->IsShr()) {
1222 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001223 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001225 } else {
1226 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001227 }
1228 }
1229 }
1230 break;
1231 }
1232 default:
1233 LOG(FATAL) << "Unexpected shift operation type " << type;
1234 }
1235}
1236
1237void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1238 HandleBinaryOp(instruction);
1239}
1240
1241void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1242 HandleBinaryOp(instruction);
1243}
1244
1245void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1246 HandleBinaryOp(instruction);
1247}
1248
1249void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1250 HandleBinaryOp(instruction);
1251}
1252
1253void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1254 LocationSummary* locations =
1255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1256 locations->SetInAt(0, Location::RequiresRegister());
1257 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1258 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1259 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1260 } else {
1261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1262 }
1263}
1264
1265void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1266 LocationSummary* locations = instruction->GetLocations();
1267 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1268 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001269 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001270
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001271 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001272 switch (type) {
1273 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001274 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1275 if (index.IsConstant()) {
1276 size_t offset =
1277 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1278 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1279 } else {
1280 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1281 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1282 }
1283 break;
1284 }
1285
1286 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001287 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1288 if (index.IsConstant()) {
1289 size_t offset =
1290 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1291 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1292 } else {
1293 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1294 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1295 }
1296 break;
1297 }
1298
1299 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001300 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1301 if (index.IsConstant()) {
1302 size_t offset =
1303 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1304 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1305 } else {
1306 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1307 __ Daddu(TMP, obj, TMP);
1308 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1309 }
1310 break;
1311 }
1312
1313 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001314 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1315 if (index.IsConstant()) {
1316 size_t offset =
1317 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1318 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1319 } else {
1320 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1321 __ Daddu(TMP, obj, TMP);
1322 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1323 }
1324 break;
1325 }
1326
1327 case Primitive::kPrimInt:
1328 case Primitive::kPrimNot: {
1329 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001330 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1331 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1332 if (index.IsConstant()) {
1333 size_t offset =
1334 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1335 __ LoadFromOffset(load_type, out, obj, offset);
1336 } else {
1337 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1338 __ Daddu(TMP, obj, TMP);
1339 __ LoadFromOffset(load_type, out, TMP, data_offset);
1340 }
1341 break;
1342 }
1343
1344 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001345 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1346 if (index.IsConstant()) {
1347 size_t offset =
1348 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1349 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1350 } else {
1351 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1352 __ Daddu(TMP, obj, TMP);
1353 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1354 }
1355 break;
1356 }
1357
1358 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001359 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1360 if (index.IsConstant()) {
1361 size_t offset =
1362 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1363 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1364 } else {
1365 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1366 __ Daddu(TMP, obj, TMP);
1367 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1368 }
1369 break;
1370 }
1371
1372 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001373 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1374 if (index.IsConstant()) {
1375 size_t offset =
1376 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1377 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1378 } else {
1379 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1380 __ Daddu(TMP, obj, TMP);
1381 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1382 }
1383 break;
1384 }
1385
1386 case Primitive::kPrimVoid:
1387 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1388 UNREACHABLE();
1389 }
1390 codegen_->MaybeRecordImplicitNullCheck(instruction);
1391}
1392
1393void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1395 locations->SetInAt(0, Location::RequiresRegister());
1396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1397}
1398
1399void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1400 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001401 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001402 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1403 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1404 __ LoadFromOffset(kLoadWord, out, obj, offset);
1405 codegen_->MaybeRecordImplicitNullCheck(instruction);
1406}
1407
1408void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001409 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001410 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1411 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001412 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001413 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001414 InvokeRuntimeCallingConvention calling_convention;
1415 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1416 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1417 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1418 } else {
1419 locations->SetInAt(0, Location::RequiresRegister());
1420 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1421 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1422 locations->SetInAt(2, Location::RequiresFpuRegister());
1423 } else {
1424 locations->SetInAt(2, Location::RequiresRegister());
1425 }
1426 }
1427}
1428
1429void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1430 LocationSummary* locations = instruction->GetLocations();
1431 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1432 Location index = locations->InAt(1);
1433 Primitive::Type value_type = instruction->GetComponentType();
1434 bool needs_runtime_call = locations->WillCall();
1435 bool needs_write_barrier =
1436 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1437
1438 switch (value_type) {
1439 case Primitive::kPrimBoolean:
1440 case Primitive::kPrimByte: {
1441 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1442 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1443 if (index.IsConstant()) {
1444 size_t offset =
1445 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1446 __ StoreToOffset(kStoreByte, value, obj, offset);
1447 } else {
1448 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1449 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1450 }
1451 break;
1452 }
1453
1454 case Primitive::kPrimShort:
1455 case Primitive::kPrimChar: {
1456 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1457 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1458 if (index.IsConstant()) {
1459 size_t offset =
1460 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1461 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1462 } else {
1463 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1464 __ Daddu(TMP, obj, TMP);
1465 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1466 }
1467 break;
1468 }
1469
1470 case Primitive::kPrimInt:
1471 case Primitive::kPrimNot: {
1472 if (!needs_runtime_call) {
1473 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1474 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1475 if (index.IsConstant()) {
1476 size_t offset =
1477 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1478 __ StoreToOffset(kStoreWord, value, obj, offset);
1479 } else {
1480 DCHECK(index.IsRegister()) << index;
1481 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1482 __ Daddu(TMP, obj, TMP);
1483 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1484 }
1485 codegen_->MaybeRecordImplicitNullCheck(instruction);
1486 if (needs_write_barrier) {
1487 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001488 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001489 }
1490 } else {
1491 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001492 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001493 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001494 }
1495 break;
1496 }
1497
1498 case Primitive::kPrimLong: {
1499 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1500 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1501 if (index.IsConstant()) {
1502 size_t offset =
1503 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1504 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1505 } else {
1506 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1507 __ Daddu(TMP, obj, TMP);
1508 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1509 }
1510 break;
1511 }
1512
1513 case Primitive::kPrimFloat: {
1514 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1515 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1516 DCHECK(locations->InAt(2).IsFpuRegister());
1517 if (index.IsConstant()) {
1518 size_t offset =
1519 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1520 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1521 } else {
1522 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1523 __ Daddu(TMP, obj, TMP);
1524 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1525 }
1526 break;
1527 }
1528
1529 case Primitive::kPrimDouble: {
1530 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1531 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1532 DCHECK(locations->InAt(2).IsFpuRegister());
1533 if (index.IsConstant()) {
1534 size_t offset =
1535 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1536 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1537 } else {
1538 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1539 __ Daddu(TMP, obj, TMP);
1540 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1541 }
1542 break;
1543 }
1544
1545 case Primitive::kPrimVoid:
1546 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1547 UNREACHABLE();
1548 }
1549
1550 // Ints and objects are handled in the switch.
1551 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1552 codegen_->MaybeRecordImplicitNullCheck(instruction);
1553 }
1554}
1555
1556void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001557 RegisterSet caller_saves = RegisterSet::Empty();
1558 InvokeRuntimeCallingConvention calling_convention;
1559 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1560 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1561 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001562 locations->SetInAt(0, Location::RequiresRegister());
1563 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001564}
1565
1566void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1567 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001568 BoundsCheckSlowPathMIPS64* slow_path =
1569 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001570 codegen_->AddSlowPath(slow_path);
1571
1572 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1573 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1574
1575 // length is limited by the maximum positive signed 32-bit integer.
1576 // Unsigned comparison of length and index checks for index < 0
1577 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001578 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001579}
1580
1581void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1583 instruction,
1584 LocationSummary::kCallOnSlowPath);
1585 locations->SetInAt(0, Location::RequiresRegister());
1586 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001587 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001588 locations->AddTemp(Location::RequiresRegister());
1589}
1590
1591void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1592 LocationSummary* locations = instruction->GetLocations();
1593 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1594 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1595 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1596
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001597 SlowPathCodeMIPS64* slow_path =
1598 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001599 codegen_->AddSlowPath(slow_path);
1600
1601 // TODO: avoid this check if we know obj is not null.
1602 __ Beqzc(obj, slow_path->GetExitLabel());
1603 // Compare the class of `obj` with `cls`.
1604 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1605 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1606 __ Bind(slow_path->GetExitLabel());
1607}
1608
1609void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1610 LocationSummary* locations =
1611 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1612 locations->SetInAt(0, Location::RequiresRegister());
1613 if (check->HasUses()) {
1614 locations->SetOut(Location::SameAsFirstInput());
1615 }
1616}
1617
1618void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1619 // We assume the class is not null.
1620 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1621 check->GetLoadClass(),
1622 check,
1623 check->GetDexPc(),
1624 true);
1625 codegen_->AddSlowPath(slow_path);
1626 GenerateClassInitializationCheck(slow_path,
1627 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1628}
1629
1630void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1631 Primitive::Type in_type = compare->InputAt(0)->GetType();
1632
Alexey Frunze299a9392015-12-08 16:08:02 -08001633 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001634
1635 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001636 case Primitive::kPrimBoolean:
1637 case Primitive::kPrimByte:
1638 case Primitive::kPrimShort:
1639 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001640 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001641 case Primitive::kPrimLong:
1642 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001643 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001644 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1645 break;
1646
1647 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001648 case Primitive::kPrimDouble:
1649 locations->SetInAt(0, Location::RequiresFpuRegister());
1650 locations->SetInAt(1, Location::RequiresFpuRegister());
1651 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001652 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001653
1654 default:
1655 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1656 }
1657}
1658
1659void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1660 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001661 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001662 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1663
1664 // 0 if: left == right
1665 // 1 if: left > right
1666 // -1 if: left < right
1667 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001668 case Primitive::kPrimBoolean:
1669 case Primitive::kPrimByte:
1670 case Primitive::kPrimShort:
1671 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001672 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001673 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001674 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001675 Location rhs_location = locations->InAt(1);
1676 bool use_imm = rhs_location.IsConstant();
1677 GpuRegister rhs = ZERO;
1678 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001679 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001680 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1681 if (value != 0) {
1682 rhs = AT;
1683 __ LoadConst64(rhs, value);
1684 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001685 } else {
1686 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1687 if (value != 0) {
1688 rhs = AT;
1689 __ LoadConst32(rhs, value);
1690 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001691 }
1692 } else {
1693 rhs = rhs_location.AsRegister<GpuRegister>();
1694 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001695 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001696 __ Slt(res, rhs, lhs);
1697 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001698 break;
1699 }
1700
Alexey Frunze299a9392015-12-08 16:08:02 -08001701 case Primitive::kPrimFloat: {
1702 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1703 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1704 Mips64Label done;
1705 __ CmpEqS(FTMP, lhs, rhs);
1706 __ LoadConst32(res, 0);
1707 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001708 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001709 __ CmpLtS(FTMP, lhs, rhs);
1710 __ LoadConst32(res, -1);
1711 __ Bc1nez(FTMP, &done);
1712 __ LoadConst32(res, 1);
1713 } else {
1714 __ CmpLtS(FTMP, rhs, lhs);
1715 __ LoadConst32(res, 1);
1716 __ Bc1nez(FTMP, &done);
1717 __ LoadConst32(res, -1);
1718 }
1719 __ Bind(&done);
1720 break;
1721 }
1722
Alexey Frunze4dda3372015-06-01 18:31:49 -07001723 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001724 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1725 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1726 Mips64Label done;
1727 __ CmpEqD(FTMP, lhs, rhs);
1728 __ LoadConst32(res, 0);
1729 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001730 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001731 __ CmpLtD(FTMP, lhs, rhs);
1732 __ LoadConst32(res, -1);
1733 __ Bc1nez(FTMP, &done);
1734 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001735 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001736 __ CmpLtD(FTMP, rhs, lhs);
1737 __ LoadConst32(res, 1);
1738 __ Bc1nez(FTMP, &done);
1739 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001740 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001741 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001742 break;
1743 }
1744
1745 default:
1746 LOG(FATAL) << "Unimplemented compare type " << in_type;
1747 }
1748}
1749
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001752 switch (instruction->InputAt(0)->GetType()) {
1753 default:
1754 case Primitive::kPrimLong:
1755 locations->SetInAt(0, Location::RequiresRegister());
1756 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1757 break;
1758
1759 case Primitive::kPrimFloat:
1760 case Primitive::kPrimDouble:
1761 locations->SetInAt(0, Location::RequiresFpuRegister());
1762 locations->SetInAt(1, Location::RequiresFpuRegister());
1763 break;
1764 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001765 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001766 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1767 }
1768}
1769
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001771 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001772 return;
1773 }
1774
Alexey Frunze299a9392015-12-08 16:08:02 -08001775 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001776 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001777 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001778 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779
Alexey Frunze299a9392015-12-08 16:08:02 -08001780 switch (type) {
1781 default:
1782 // Integer case.
1783 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1784 return;
1785 case Primitive::kPrimLong:
1786 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1787 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001788
Alexey Frunze299a9392015-12-08 16:08:02 -08001789 case Primitive::kPrimFloat:
1790 case Primitive::kPrimDouble:
1791 // TODO: don't use branches.
1792 GenerateFpCompareAndBranch(instruction->GetCondition(),
1793 instruction->IsGtBias(),
1794 type,
1795 locations,
1796 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001797 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001798 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001799
1800 // Convert the branches into the result.
1801 Mips64Label done;
1802
1803 // False case: result = 0.
1804 __ LoadConst32(dst, 0);
1805 __ Bc(&done);
1806
1807 // True case: result = 1.
1808 __ Bind(&true_label);
1809 __ LoadConst32(dst, 1);
1810 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001811}
1812
Alexey Frunzec857c742015-09-23 15:12:39 -07001813void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1814 DCHECK(instruction->IsDiv() || instruction->IsRem());
1815 Primitive::Type type = instruction->GetResultType();
1816
1817 LocationSummary* locations = instruction->GetLocations();
1818 Location second = locations->InAt(1);
1819 DCHECK(second.IsConstant());
1820
1821 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1822 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1823 int64_t imm = Int64FromConstant(second.GetConstant());
1824 DCHECK(imm == 1 || imm == -1);
1825
1826 if (instruction->IsRem()) {
1827 __ Move(out, ZERO);
1828 } else {
1829 if (imm == -1) {
1830 if (type == Primitive::kPrimInt) {
1831 __ Subu(out, ZERO, dividend);
1832 } else {
1833 DCHECK_EQ(type, Primitive::kPrimLong);
1834 __ Dsubu(out, ZERO, dividend);
1835 }
1836 } else if (out != dividend) {
1837 __ Move(out, dividend);
1838 }
1839 }
1840}
1841
1842void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1843 DCHECK(instruction->IsDiv() || instruction->IsRem());
1844 Primitive::Type type = instruction->GetResultType();
1845
1846 LocationSummary* locations = instruction->GetLocations();
1847 Location second = locations->InAt(1);
1848 DCHECK(second.IsConstant());
1849
1850 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1851 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1852 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001853 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001854 int ctz_imm = CTZ(abs_imm);
1855
1856 if (instruction->IsDiv()) {
1857 if (type == Primitive::kPrimInt) {
1858 if (ctz_imm == 1) {
1859 // Fast path for division by +/-2, which is very common.
1860 __ Srl(TMP, dividend, 31);
1861 } else {
1862 __ Sra(TMP, dividend, 31);
1863 __ Srl(TMP, TMP, 32 - ctz_imm);
1864 }
1865 __ Addu(out, dividend, TMP);
1866 __ Sra(out, out, ctz_imm);
1867 if (imm < 0) {
1868 __ Subu(out, ZERO, out);
1869 }
1870 } else {
1871 DCHECK_EQ(type, Primitive::kPrimLong);
1872 if (ctz_imm == 1) {
1873 // Fast path for division by +/-2, which is very common.
1874 __ Dsrl32(TMP, dividend, 31);
1875 } else {
1876 __ Dsra32(TMP, dividend, 31);
1877 if (ctz_imm > 32) {
1878 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1879 } else {
1880 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1881 }
1882 }
1883 __ Daddu(out, dividend, TMP);
1884 if (ctz_imm < 32) {
1885 __ Dsra(out, out, ctz_imm);
1886 } else {
1887 __ Dsra32(out, out, ctz_imm - 32);
1888 }
1889 if (imm < 0) {
1890 __ Dsubu(out, ZERO, out);
1891 }
1892 }
1893 } else {
1894 if (type == Primitive::kPrimInt) {
1895 if (ctz_imm == 1) {
1896 // Fast path for modulo +/-2, which is very common.
1897 __ Sra(TMP, dividend, 31);
1898 __ Subu(out, dividend, TMP);
1899 __ Andi(out, out, 1);
1900 __ Addu(out, out, TMP);
1901 } else {
1902 __ Sra(TMP, dividend, 31);
1903 __ Srl(TMP, TMP, 32 - ctz_imm);
1904 __ Addu(out, dividend, TMP);
1905 if (IsUint<16>(abs_imm - 1)) {
1906 __ Andi(out, out, abs_imm - 1);
1907 } else {
1908 __ Sll(out, out, 32 - ctz_imm);
1909 __ Srl(out, out, 32 - ctz_imm);
1910 }
1911 __ Subu(out, out, TMP);
1912 }
1913 } else {
1914 DCHECK_EQ(type, Primitive::kPrimLong);
1915 if (ctz_imm == 1) {
1916 // Fast path for modulo +/-2, which is very common.
1917 __ Dsra32(TMP, dividend, 31);
1918 __ Dsubu(out, dividend, TMP);
1919 __ Andi(out, out, 1);
1920 __ Daddu(out, out, TMP);
1921 } else {
1922 __ Dsra32(TMP, dividend, 31);
1923 if (ctz_imm > 32) {
1924 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1925 } else {
1926 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1927 }
1928 __ Daddu(out, dividend, TMP);
1929 if (IsUint<16>(abs_imm - 1)) {
1930 __ Andi(out, out, abs_imm - 1);
1931 } else {
1932 if (ctz_imm > 32) {
1933 __ Dsll(out, out, 64 - ctz_imm);
1934 __ Dsrl(out, out, 64 - ctz_imm);
1935 } else {
1936 __ Dsll32(out, out, 32 - ctz_imm);
1937 __ Dsrl32(out, out, 32 - ctz_imm);
1938 }
1939 }
1940 __ Dsubu(out, out, TMP);
1941 }
1942 }
1943 }
1944}
1945
1946void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1947 DCHECK(instruction->IsDiv() || instruction->IsRem());
1948
1949 LocationSummary* locations = instruction->GetLocations();
1950 Location second = locations->InAt(1);
1951 DCHECK(second.IsConstant());
1952
1953 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1954 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1955 int64_t imm = Int64FromConstant(second.GetConstant());
1956
1957 Primitive::Type type = instruction->GetResultType();
1958 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1959
1960 int64_t magic;
1961 int shift;
1962 CalculateMagicAndShiftForDivRem(imm,
1963 (type == Primitive::kPrimLong),
1964 &magic,
1965 &shift);
1966
1967 if (type == Primitive::kPrimInt) {
1968 __ LoadConst32(TMP, magic);
1969 __ MuhR6(TMP, dividend, TMP);
1970
1971 if (imm > 0 && magic < 0) {
1972 __ Addu(TMP, TMP, dividend);
1973 } else if (imm < 0 && magic > 0) {
1974 __ Subu(TMP, TMP, dividend);
1975 }
1976
1977 if (shift != 0) {
1978 __ Sra(TMP, TMP, shift);
1979 }
1980
1981 if (instruction->IsDiv()) {
1982 __ Sra(out, TMP, 31);
1983 __ Subu(out, TMP, out);
1984 } else {
1985 __ Sra(AT, TMP, 31);
1986 __ Subu(AT, TMP, AT);
1987 __ LoadConst32(TMP, imm);
1988 __ MulR6(TMP, AT, TMP);
1989 __ Subu(out, dividend, TMP);
1990 }
1991 } else {
1992 __ LoadConst64(TMP, magic);
1993 __ Dmuh(TMP, dividend, TMP);
1994
1995 if (imm > 0 && magic < 0) {
1996 __ Daddu(TMP, TMP, dividend);
1997 } else if (imm < 0 && magic > 0) {
1998 __ Dsubu(TMP, TMP, dividend);
1999 }
2000
2001 if (shift >= 32) {
2002 __ Dsra32(TMP, TMP, shift - 32);
2003 } else if (shift > 0) {
2004 __ Dsra(TMP, TMP, shift);
2005 }
2006
2007 if (instruction->IsDiv()) {
2008 __ Dsra32(out, TMP, 31);
2009 __ Dsubu(out, TMP, out);
2010 } else {
2011 __ Dsra32(AT, TMP, 31);
2012 __ Dsubu(AT, TMP, AT);
2013 __ LoadConst64(TMP, imm);
2014 __ Dmul(TMP, AT, TMP);
2015 __ Dsubu(out, dividend, TMP);
2016 }
2017 }
2018}
2019
2020void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2021 DCHECK(instruction->IsDiv() || instruction->IsRem());
2022 Primitive::Type type = instruction->GetResultType();
2023 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2024
2025 LocationSummary* locations = instruction->GetLocations();
2026 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2027 Location second = locations->InAt(1);
2028
2029 if (second.IsConstant()) {
2030 int64_t imm = Int64FromConstant(second.GetConstant());
2031 if (imm == 0) {
2032 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2033 } else if (imm == 1 || imm == -1) {
2034 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002035 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002036 DivRemByPowerOfTwo(instruction);
2037 } else {
2038 DCHECK(imm <= -2 || imm >= 2);
2039 GenerateDivRemWithAnyConstant(instruction);
2040 }
2041 } else {
2042 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2043 GpuRegister divisor = second.AsRegister<GpuRegister>();
2044 if (instruction->IsDiv()) {
2045 if (type == Primitive::kPrimInt)
2046 __ DivR6(out, dividend, divisor);
2047 else
2048 __ Ddiv(out, dividend, divisor);
2049 } else {
2050 if (type == Primitive::kPrimInt)
2051 __ ModR6(out, dividend, divisor);
2052 else
2053 __ Dmod(out, dividend, divisor);
2054 }
2055 }
2056}
2057
Alexey Frunze4dda3372015-06-01 18:31:49 -07002058void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2059 LocationSummary* locations =
2060 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2061 switch (div->GetResultType()) {
2062 case Primitive::kPrimInt:
2063 case Primitive::kPrimLong:
2064 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002065 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2067 break;
2068
2069 case Primitive::kPrimFloat:
2070 case Primitive::kPrimDouble:
2071 locations->SetInAt(0, Location::RequiresFpuRegister());
2072 locations->SetInAt(1, Location::RequiresFpuRegister());
2073 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2074 break;
2075
2076 default:
2077 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2078 }
2079}
2080
2081void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2082 Primitive::Type type = instruction->GetType();
2083 LocationSummary* locations = instruction->GetLocations();
2084
2085 switch (type) {
2086 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002087 case Primitive::kPrimLong:
2088 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002089 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002090 case Primitive::kPrimFloat:
2091 case Primitive::kPrimDouble: {
2092 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2093 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2094 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2095 if (type == Primitive::kPrimFloat)
2096 __ DivS(dst, lhs, rhs);
2097 else
2098 __ DivD(dst, lhs, rhs);
2099 break;
2100 }
2101 default:
2102 LOG(FATAL) << "Unexpected div type " << type;
2103 }
2104}
2105
2106void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002107 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002108 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002109}
2110
2111void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2112 SlowPathCodeMIPS64* slow_path =
2113 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2114 codegen_->AddSlowPath(slow_path);
2115 Location value = instruction->GetLocations()->InAt(0);
2116
2117 Primitive::Type type = instruction->GetType();
2118
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002119 if (!Primitive::IsIntegralType(type)) {
2120 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002121 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002122 }
2123
2124 if (value.IsConstant()) {
2125 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2126 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002127 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002128 } else {
2129 // A division by a non-null constant is valid. We don't need to perform
2130 // any check, so simply fall through.
2131 }
2132 } else {
2133 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2134 }
2135}
2136
2137void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2138 LocationSummary* locations =
2139 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2140 locations->SetOut(Location::ConstantLocation(constant));
2141}
2142
2143void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2144 // Will be generated at use site.
2145}
2146
2147void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2148 exit->SetLocations(nullptr);
2149}
2150
2151void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2152}
2153
2154void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2155 LocationSummary* locations =
2156 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2157 locations->SetOut(Location::ConstantLocation(constant));
2158}
2159
2160void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2161 // Will be generated at use site.
2162}
2163
David Brazdilfc6a86a2015-06-26 10:33:45 +00002164void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002165 DCHECK(!successor->IsExitBlock());
2166 HBasicBlock* block = got->GetBlock();
2167 HInstruction* previous = got->GetPrevious();
2168 HLoopInformation* info = block->GetLoopInformation();
2169
2170 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2171 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2172 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2173 return;
2174 }
2175 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2176 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2177 }
2178 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002179 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002180 }
2181}
2182
David Brazdilfc6a86a2015-06-26 10:33:45 +00002183void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2184 got->SetLocations(nullptr);
2185}
2186
2187void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2188 HandleGoto(got, got->GetSuccessor());
2189}
2190
2191void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2192 try_boundary->SetLocations(nullptr);
2193}
2194
2195void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2196 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2197 if (!successor->IsExitBlock()) {
2198 HandleGoto(try_boundary, successor);
2199 }
2200}
2201
Alexey Frunze299a9392015-12-08 16:08:02 -08002202void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2203 bool is64bit,
2204 LocationSummary* locations) {
2205 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2206 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2207 Location rhs_location = locations->InAt(1);
2208 GpuRegister rhs_reg = ZERO;
2209 int64_t rhs_imm = 0;
2210 bool use_imm = rhs_location.IsConstant();
2211 if (use_imm) {
2212 if (is64bit) {
2213 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2214 } else {
2215 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2216 }
2217 } else {
2218 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2219 }
2220 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2221
2222 switch (cond) {
2223 case kCondEQ:
2224 case kCondNE:
2225 if (use_imm && IsUint<16>(rhs_imm)) {
2226 __ Xori(dst, lhs, rhs_imm);
2227 } else {
2228 if (use_imm) {
2229 rhs_reg = TMP;
2230 __ LoadConst64(rhs_reg, rhs_imm);
2231 }
2232 __ Xor(dst, lhs, rhs_reg);
2233 }
2234 if (cond == kCondEQ) {
2235 __ Sltiu(dst, dst, 1);
2236 } else {
2237 __ Sltu(dst, ZERO, dst);
2238 }
2239 break;
2240
2241 case kCondLT:
2242 case kCondGE:
2243 if (use_imm && IsInt<16>(rhs_imm)) {
2244 __ Slti(dst, lhs, rhs_imm);
2245 } else {
2246 if (use_imm) {
2247 rhs_reg = TMP;
2248 __ LoadConst64(rhs_reg, rhs_imm);
2249 }
2250 __ Slt(dst, lhs, rhs_reg);
2251 }
2252 if (cond == kCondGE) {
2253 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2254 // only the slt instruction but no sge.
2255 __ Xori(dst, dst, 1);
2256 }
2257 break;
2258
2259 case kCondLE:
2260 case kCondGT:
2261 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2262 // Simulate lhs <= rhs via lhs < rhs + 1.
2263 __ Slti(dst, lhs, rhs_imm_plus_one);
2264 if (cond == kCondGT) {
2265 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2266 // only the slti instruction but no sgti.
2267 __ Xori(dst, dst, 1);
2268 }
2269 } else {
2270 if (use_imm) {
2271 rhs_reg = TMP;
2272 __ LoadConst64(rhs_reg, rhs_imm);
2273 }
2274 __ Slt(dst, rhs_reg, lhs);
2275 if (cond == kCondLE) {
2276 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2277 // only the slt instruction but no sle.
2278 __ Xori(dst, dst, 1);
2279 }
2280 }
2281 break;
2282
2283 case kCondB:
2284 case kCondAE:
2285 if (use_imm && IsInt<16>(rhs_imm)) {
2286 // Sltiu sign-extends its 16-bit immediate operand before
2287 // the comparison and thus lets us compare directly with
2288 // unsigned values in the ranges [0, 0x7fff] and
2289 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2290 __ Sltiu(dst, lhs, rhs_imm);
2291 } else {
2292 if (use_imm) {
2293 rhs_reg = TMP;
2294 __ LoadConst64(rhs_reg, rhs_imm);
2295 }
2296 __ Sltu(dst, lhs, rhs_reg);
2297 }
2298 if (cond == kCondAE) {
2299 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2300 // only the sltu instruction but no sgeu.
2301 __ Xori(dst, dst, 1);
2302 }
2303 break;
2304
2305 case kCondBE:
2306 case kCondA:
2307 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2308 // Simulate lhs <= rhs via lhs < rhs + 1.
2309 // Note that this only works if rhs + 1 does not overflow
2310 // to 0, hence the check above.
2311 // Sltiu sign-extends its 16-bit immediate operand before
2312 // the comparison and thus lets us compare directly with
2313 // unsigned values in the ranges [0, 0x7fff] and
2314 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2315 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2316 if (cond == kCondA) {
2317 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2318 // only the sltiu instruction but no sgtiu.
2319 __ Xori(dst, dst, 1);
2320 }
2321 } else {
2322 if (use_imm) {
2323 rhs_reg = TMP;
2324 __ LoadConst64(rhs_reg, rhs_imm);
2325 }
2326 __ Sltu(dst, rhs_reg, lhs);
2327 if (cond == kCondBE) {
2328 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2329 // only the sltu instruction but no sleu.
2330 __ Xori(dst, dst, 1);
2331 }
2332 }
2333 break;
2334 }
2335}
2336
2337void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2338 bool is64bit,
2339 LocationSummary* locations,
2340 Mips64Label* label) {
2341 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2342 Location rhs_location = locations->InAt(1);
2343 GpuRegister rhs_reg = ZERO;
2344 int64_t rhs_imm = 0;
2345 bool use_imm = rhs_location.IsConstant();
2346 if (use_imm) {
2347 if (is64bit) {
2348 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2349 } else {
2350 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2351 }
2352 } else {
2353 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2354 }
2355
2356 if (use_imm && rhs_imm == 0) {
2357 switch (cond) {
2358 case kCondEQ:
2359 case kCondBE: // <= 0 if zero
2360 __ Beqzc(lhs, label);
2361 break;
2362 case kCondNE:
2363 case kCondA: // > 0 if non-zero
2364 __ Bnezc(lhs, label);
2365 break;
2366 case kCondLT:
2367 __ Bltzc(lhs, label);
2368 break;
2369 case kCondGE:
2370 __ Bgezc(lhs, label);
2371 break;
2372 case kCondLE:
2373 __ Blezc(lhs, label);
2374 break;
2375 case kCondGT:
2376 __ Bgtzc(lhs, label);
2377 break;
2378 case kCondB: // always false
2379 break;
2380 case kCondAE: // always true
2381 __ Bc(label);
2382 break;
2383 }
2384 } else {
2385 if (use_imm) {
2386 rhs_reg = TMP;
2387 __ LoadConst64(rhs_reg, rhs_imm);
2388 }
2389 switch (cond) {
2390 case kCondEQ:
2391 __ Beqc(lhs, rhs_reg, label);
2392 break;
2393 case kCondNE:
2394 __ Bnec(lhs, rhs_reg, label);
2395 break;
2396 case kCondLT:
2397 __ Bltc(lhs, rhs_reg, label);
2398 break;
2399 case kCondGE:
2400 __ Bgec(lhs, rhs_reg, label);
2401 break;
2402 case kCondLE:
2403 __ Bgec(rhs_reg, lhs, label);
2404 break;
2405 case kCondGT:
2406 __ Bltc(rhs_reg, lhs, label);
2407 break;
2408 case kCondB:
2409 __ Bltuc(lhs, rhs_reg, label);
2410 break;
2411 case kCondAE:
2412 __ Bgeuc(lhs, rhs_reg, label);
2413 break;
2414 case kCondBE:
2415 __ Bgeuc(rhs_reg, lhs, label);
2416 break;
2417 case kCondA:
2418 __ Bltuc(rhs_reg, lhs, label);
2419 break;
2420 }
2421 }
2422}
2423
2424void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2425 bool gt_bias,
2426 Primitive::Type type,
2427 LocationSummary* locations,
2428 Mips64Label* label) {
2429 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2430 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2431 if (type == Primitive::kPrimFloat) {
2432 switch (cond) {
2433 case kCondEQ:
2434 __ CmpEqS(FTMP, lhs, rhs);
2435 __ Bc1nez(FTMP, label);
2436 break;
2437 case kCondNE:
2438 __ CmpEqS(FTMP, lhs, rhs);
2439 __ Bc1eqz(FTMP, label);
2440 break;
2441 case kCondLT:
2442 if (gt_bias) {
2443 __ CmpLtS(FTMP, lhs, rhs);
2444 } else {
2445 __ CmpUltS(FTMP, lhs, rhs);
2446 }
2447 __ Bc1nez(FTMP, label);
2448 break;
2449 case kCondLE:
2450 if (gt_bias) {
2451 __ CmpLeS(FTMP, lhs, rhs);
2452 } else {
2453 __ CmpUleS(FTMP, lhs, rhs);
2454 }
2455 __ Bc1nez(FTMP, label);
2456 break;
2457 case kCondGT:
2458 if (gt_bias) {
2459 __ CmpUltS(FTMP, rhs, lhs);
2460 } else {
2461 __ CmpLtS(FTMP, rhs, lhs);
2462 }
2463 __ Bc1nez(FTMP, label);
2464 break;
2465 case kCondGE:
2466 if (gt_bias) {
2467 __ CmpUleS(FTMP, rhs, lhs);
2468 } else {
2469 __ CmpLeS(FTMP, rhs, lhs);
2470 }
2471 __ Bc1nez(FTMP, label);
2472 break;
2473 default:
2474 LOG(FATAL) << "Unexpected non-floating-point condition";
2475 }
2476 } else {
2477 DCHECK_EQ(type, Primitive::kPrimDouble);
2478 switch (cond) {
2479 case kCondEQ:
2480 __ CmpEqD(FTMP, lhs, rhs);
2481 __ Bc1nez(FTMP, label);
2482 break;
2483 case kCondNE:
2484 __ CmpEqD(FTMP, lhs, rhs);
2485 __ Bc1eqz(FTMP, label);
2486 break;
2487 case kCondLT:
2488 if (gt_bias) {
2489 __ CmpLtD(FTMP, lhs, rhs);
2490 } else {
2491 __ CmpUltD(FTMP, lhs, rhs);
2492 }
2493 __ Bc1nez(FTMP, label);
2494 break;
2495 case kCondLE:
2496 if (gt_bias) {
2497 __ CmpLeD(FTMP, lhs, rhs);
2498 } else {
2499 __ CmpUleD(FTMP, lhs, rhs);
2500 }
2501 __ Bc1nez(FTMP, label);
2502 break;
2503 case kCondGT:
2504 if (gt_bias) {
2505 __ CmpUltD(FTMP, rhs, lhs);
2506 } else {
2507 __ CmpLtD(FTMP, rhs, lhs);
2508 }
2509 __ Bc1nez(FTMP, label);
2510 break;
2511 case kCondGE:
2512 if (gt_bias) {
2513 __ CmpUleD(FTMP, rhs, lhs);
2514 } else {
2515 __ CmpLeD(FTMP, rhs, lhs);
2516 }
2517 __ Bc1nez(FTMP, label);
2518 break;
2519 default:
2520 LOG(FATAL) << "Unexpected non-floating-point condition";
2521 }
2522 }
2523}
2524
Alexey Frunze4dda3372015-06-01 18:31:49 -07002525void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002526 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002527 Mips64Label* true_target,
2528 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002529 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002530
David Brazdil0debae72015-11-12 18:37:00 +00002531 if (true_target == nullptr && false_target == nullptr) {
2532 // Nothing to do. The code always falls through.
2533 return;
2534 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002535 // Constant condition, statically compared against "true" (integer value 1).
2536 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002537 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002538 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002539 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002540 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002541 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002542 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002543 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002544 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002545 }
David Brazdil0debae72015-11-12 18:37:00 +00002546 return;
2547 }
2548
2549 // The following code generates these patterns:
2550 // (1) true_target == nullptr && false_target != nullptr
2551 // - opposite condition true => branch to false_target
2552 // (2) true_target != nullptr && false_target == nullptr
2553 // - condition true => branch to true_target
2554 // (3) true_target != nullptr && false_target != nullptr
2555 // - condition true => branch to true_target
2556 // - branch to false_target
2557 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002558 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002559 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002560 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002561 if (true_target == nullptr) {
2562 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2563 } else {
2564 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2565 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002566 } else {
2567 // The condition instruction has not been materialized, use its inputs as
2568 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002569 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002570 Primitive::Type type = condition->InputAt(0)->GetType();
2571 LocationSummary* locations = cond->GetLocations();
2572 IfCondition if_cond = condition->GetCondition();
2573 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002574
David Brazdil0debae72015-11-12 18:37:00 +00002575 if (true_target == nullptr) {
2576 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002577 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002578 }
2579
Alexey Frunze299a9392015-12-08 16:08:02 -08002580 switch (type) {
2581 default:
2582 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2583 break;
2584 case Primitive::kPrimLong:
2585 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2586 break;
2587 case Primitive::kPrimFloat:
2588 case Primitive::kPrimDouble:
2589 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2590 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002591 }
2592 }
David Brazdil0debae72015-11-12 18:37:00 +00002593
2594 // If neither branch falls through (case 3), the conditional branch to `true_target`
2595 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2596 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002597 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002598 }
2599}
2600
2601void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2602 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002603 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002604 locations->SetInAt(0, Location::RequiresRegister());
2605 }
2606}
2607
2608void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002609 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2610 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002611 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002612 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002613 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002614 nullptr : codegen_->GetLabelOf(false_successor);
2615 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002616}
2617
2618void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2619 LocationSummary* locations = new (GetGraph()->GetArena())
2620 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002621 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002622 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002623 locations->SetInAt(0, Location::RequiresRegister());
2624 }
2625}
2626
2627void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002628 SlowPathCodeMIPS64* slow_path =
2629 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002630 GenerateTestAndBranch(deoptimize,
2631 /* condition_input_index */ 0,
2632 slow_path->GetEntryLabel(),
2633 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002634}
2635
David Brazdil74eb1b22015-12-14 11:44:01 +00002636void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2637 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2638 if (Primitive::IsFloatingPointType(select->GetType())) {
2639 locations->SetInAt(0, Location::RequiresFpuRegister());
2640 locations->SetInAt(1, Location::RequiresFpuRegister());
2641 } else {
2642 locations->SetInAt(0, Location::RequiresRegister());
2643 locations->SetInAt(1, Location::RequiresRegister());
2644 }
2645 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2646 locations->SetInAt(2, Location::RequiresRegister());
2647 }
2648 locations->SetOut(Location::SameAsFirstInput());
2649}
2650
2651void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2652 LocationSummary* locations = select->GetLocations();
2653 Mips64Label false_target;
2654 GenerateTestAndBranch(select,
2655 /* condition_input_index */ 2,
2656 /* true_target */ nullptr,
2657 &false_target);
2658 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2659 __ Bind(&false_target);
2660}
2661
David Srbecky0cf44932015-12-09 14:09:59 +00002662void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2663 new (GetGraph()->GetArena()) LocationSummary(info);
2664}
2665
David Srbeckyd28f4a02016-03-14 17:14:24 +00002666void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2667 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002668}
2669
2670void CodeGeneratorMIPS64::GenerateNop() {
2671 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002672}
2673
Alexey Frunze4dda3372015-06-01 18:31:49 -07002674void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2675 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2676 LocationSummary* locations =
2677 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2678 locations->SetInAt(0, Location::RequiresRegister());
2679 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2680 locations->SetOut(Location::RequiresFpuRegister());
2681 } else {
2682 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2683 }
2684}
2685
2686void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2687 const FieldInfo& field_info) {
2688 Primitive::Type type = field_info.GetFieldType();
2689 LocationSummary* locations = instruction->GetLocations();
2690 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2691 LoadOperandType load_type = kLoadUnsignedByte;
2692 switch (type) {
2693 case Primitive::kPrimBoolean:
2694 load_type = kLoadUnsignedByte;
2695 break;
2696 case Primitive::kPrimByte:
2697 load_type = kLoadSignedByte;
2698 break;
2699 case Primitive::kPrimShort:
2700 load_type = kLoadSignedHalfword;
2701 break;
2702 case Primitive::kPrimChar:
2703 load_type = kLoadUnsignedHalfword;
2704 break;
2705 case Primitive::kPrimInt:
2706 case Primitive::kPrimFloat:
2707 load_type = kLoadWord;
2708 break;
2709 case Primitive::kPrimLong:
2710 case Primitive::kPrimDouble:
2711 load_type = kLoadDoubleword;
2712 break;
2713 case Primitive::kPrimNot:
2714 load_type = kLoadUnsignedWord;
2715 break;
2716 case Primitive::kPrimVoid:
2717 LOG(FATAL) << "Unreachable type " << type;
2718 UNREACHABLE();
2719 }
2720 if (!Primitive::IsFloatingPointType(type)) {
2721 DCHECK(locations->Out().IsRegister());
2722 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2723 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2724 } else {
2725 DCHECK(locations->Out().IsFpuRegister());
2726 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2727 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2728 }
2729
2730 codegen_->MaybeRecordImplicitNullCheck(instruction);
2731 // TODO: memory barrier?
2732}
2733
2734void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2735 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2736 LocationSummary* locations =
2737 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2738 locations->SetInAt(0, Location::RequiresRegister());
2739 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2740 locations->SetInAt(1, Location::RequiresFpuRegister());
2741 } else {
2742 locations->SetInAt(1, Location::RequiresRegister());
2743 }
2744}
2745
2746void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002747 const FieldInfo& field_info,
2748 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002749 Primitive::Type type = field_info.GetFieldType();
2750 LocationSummary* locations = instruction->GetLocations();
2751 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2752 StoreOperandType store_type = kStoreByte;
2753 switch (type) {
2754 case Primitive::kPrimBoolean:
2755 case Primitive::kPrimByte:
2756 store_type = kStoreByte;
2757 break;
2758 case Primitive::kPrimShort:
2759 case Primitive::kPrimChar:
2760 store_type = kStoreHalfword;
2761 break;
2762 case Primitive::kPrimInt:
2763 case Primitive::kPrimFloat:
2764 case Primitive::kPrimNot:
2765 store_type = kStoreWord;
2766 break;
2767 case Primitive::kPrimLong:
2768 case Primitive::kPrimDouble:
2769 store_type = kStoreDoubleword;
2770 break;
2771 case Primitive::kPrimVoid:
2772 LOG(FATAL) << "Unreachable type " << type;
2773 UNREACHABLE();
2774 }
2775 if (!Primitive::IsFloatingPointType(type)) {
2776 DCHECK(locations->InAt(1).IsRegister());
2777 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2778 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2779 } else {
2780 DCHECK(locations->InAt(1).IsFpuRegister());
2781 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2782 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2783 }
2784
2785 codegen_->MaybeRecordImplicitNullCheck(instruction);
2786 // TODO: memory barriers?
2787 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2788 DCHECK(locations->InAt(1).IsRegister());
2789 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002790 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002791 }
2792}
2793
2794void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2795 HandleFieldGet(instruction, instruction->GetFieldInfo());
2796}
2797
2798void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2799 HandleFieldGet(instruction, instruction->GetFieldInfo());
2800}
2801
2802void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2803 HandleFieldSet(instruction, instruction->GetFieldInfo());
2804}
2805
2806void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002807 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002808}
2809
2810void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2811 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002812 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2814 locations->SetInAt(0, Location::RequiresRegister());
2815 locations->SetInAt(1, Location::RequiresRegister());
2816 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002817 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002818 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2819}
2820
2821void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2822 LocationSummary* locations = instruction->GetLocations();
2823 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2824 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2825 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2826
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002827 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002828
2829 // Return 0 if `obj` is null.
2830 // TODO: Avoid this check if we know `obj` is not null.
2831 __ Move(out, ZERO);
2832 __ Beqzc(obj, &done);
2833
2834 // Compare the class of `obj` with `cls`.
2835 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002836 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002837 // Classes must be equal for the instanceof to succeed.
2838 __ Xor(out, out, cls);
2839 __ Sltiu(out, out, 1);
2840 } else {
2841 // If the classes are not equal, we go into a slow path.
2842 DCHECK(locations->OnlyCallsOnSlowPath());
2843 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002844 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845 codegen_->AddSlowPath(slow_path);
2846 __ Bnec(out, cls, slow_path->GetEntryLabel());
2847 __ LoadConst32(out, 1);
2848 __ Bind(slow_path->GetExitLabel());
2849 }
2850
2851 __ Bind(&done);
2852}
2853
2854void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2855 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2856 locations->SetOut(Location::ConstantLocation(constant));
2857}
2858
2859void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2860 // Will be generated at use site.
2861}
2862
2863void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2864 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2865 locations->SetOut(Location::ConstantLocation(constant));
2866}
2867
2868void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2869 // Will be generated at use site.
2870}
2871
Calin Juravle175dc732015-08-25 15:42:32 +01002872void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2873 // The trampoline uses the same calling convention as dex calling conventions,
2874 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2875 // the method_idx.
2876 HandleInvoke(invoke);
2877}
2878
2879void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2880 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2881}
2882
Alexey Frunze4dda3372015-06-01 18:31:49 -07002883void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2884 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2885 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2886}
2887
2888void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2889 HandleInvoke(invoke);
2890 // The register T0 is required to be used for the hidden argument in
2891 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2892 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2893}
2894
2895void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2896 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2897 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002898 Location receiver = invoke->GetLocations()->InAt(0);
2899 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07002900 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002901
2902 // Set the hidden argument.
2903 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2904 invoke->GetDexMethodIndex());
2905
2906 // temp = object->GetClass();
2907 if (receiver.IsStackSlot()) {
2908 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2909 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2910 } else {
2911 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2912 }
2913 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002914 __ LoadFromOffset(kLoadDoubleword, temp, temp,
2915 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
2916 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002917 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002918 // temp = temp->GetImtEntryAt(method_offset);
2919 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2920 // T9 = temp->GetEntryPoint();
2921 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2922 // T9();
2923 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002924 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002925 DCHECK(!codegen_->IsLeafMethod());
2926 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2927}
2928
2929void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002930 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2931 if (intrinsic.TryDispatch(invoke)) {
2932 return;
2933 }
2934
Alexey Frunze4dda3372015-06-01 18:31:49 -07002935 HandleInvoke(invoke);
2936}
2937
2938void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002939 // Explicit clinit checks triggered by static invokes must have been pruned by
2940 // art::PrepareForRegisterAllocation.
2941 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002942
Chris Larsen3039e382015-08-26 07:54:08 -07002943 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2944 if (intrinsic.TryDispatch(invoke)) {
2945 return;
2946 }
2947
Alexey Frunze4dda3372015-06-01 18:31:49 -07002948 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002949}
2950
Chris Larsen3039e382015-08-26 07:54:08 -07002951static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002952 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002953 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2954 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002955 return true;
2956 }
2957 return false;
2958}
2959
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002960HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
2961 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
2962 // TODO: Implement other kinds.
2963 return HLoadString::LoadKind::kDexCacheViaMethod;
2964}
2965
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002966HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
2967 HLoadClass::LoadKind desired_class_load_kind) {
2968 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
2969 // TODO: Implement other kinds.
2970 return HLoadClass::LoadKind::kDexCacheViaMethod;
2971}
2972
Vladimir Markodc151b22015-10-15 18:02:30 +01002973HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
2974 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01002975 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01002976 switch (desired_dispatch_info.method_load_kind) {
2977 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2978 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2979 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
2980 return HInvokeStaticOrDirect::DispatchInfo {
2981 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
2982 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2983 0u,
2984 0u
2985 };
2986 default:
2987 break;
2988 }
2989 switch (desired_dispatch_info.code_ptr_location) {
2990 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2991 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
2992 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
2993 return HInvokeStaticOrDirect::DispatchInfo {
2994 desired_dispatch_info.method_load_kind,
2995 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2996 desired_dispatch_info.method_load_data,
2997 0u
2998 };
2999 default:
3000 return desired_dispatch_info;
3001 }
3002}
3003
Alexey Frunze4dda3372015-06-01 18:31:49 -07003004void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3005 // All registers are assumed to be correctly set up per the calling convention.
3006
Vladimir Marko58155012015-08-19 12:49:41 +00003007 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3008 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003009 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003010 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003011 uint32_t offset =
3012 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003013 __ LoadFromOffset(kLoadDoubleword,
3014 temp.AsRegister<GpuRegister>(),
3015 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003016 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003017 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003018 }
Vladimir Marko58155012015-08-19 12:49:41 +00003019 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003020 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003021 break;
3022 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3023 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3024 break;
3025 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003026 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003027 // TODO: Implement these types.
3028 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3029 LOG(FATAL) << "Unsupported";
3030 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003031 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003032 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003033 GpuRegister reg = temp.AsRegister<GpuRegister>();
3034 GpuRegister method_reg;
3035 if (current_method.IsRegister()) {
3036 method_reg = current_method.AsRegister<GpuRegister>();
3037 } else {
3038 // TODO: use the appropriate DCHECK() here if possible.
3039 // DCHECK(invoke->GetLocations()->Intrinsified());
3040 DCHECK(!current_method.IsValid());
3041 method_reg = reg;
3042 __ Ld(reg, SP, kCurrentMethodStackOffset);
3043 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003044
Vladimir Marko58155012015-08-19 12:49:41 +00003045 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003046 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003047 reg,
3048 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003049 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003050 // temp = temp[index_in_cache];
3051 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3052 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003053 __ LoadFromOffset(kLoadDoubleword,
3054 reg,
3055 reg,
3056 CodeGenerator::GetCachePointerOffset(index_in_cache));
3057 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003058 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003059 }
3060
Vladimir Marko58155012015-08-19 12:49:41 +00003061 switch (invoke->GetCodePtrLocation()) {
3062 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003063 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003064 break;
3065 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3066 // LR = invoke->GetDirectCodePtr();
3067 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3068 // LR()
3069 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003070 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003071 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003072 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003073 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3074 // TODO: Implement these types.
3075 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3076 LOG(FATAL) << "Unsupported";
3077 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003078 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3079 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3080 __ LoadFromOffset(kLoadDoubleword,
3081 T9,
3082 callee_method.AsRegister<GpuRegister>(),
3083 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003084 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003085 // T9()
3086 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003087 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003088 break;
3089 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003090 DCHECK(!IsLeafMethod());
3091}
3092
3093void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003094 // Explicit clinit checks triggered by static invokes must have been pruned by
3095 // art::PrepareForRegisterAllocation.
3096 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003097
3098 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3099 return;
3100 }
3101
3102 LocationSummary* locations = invoke->GetLocations();
3103 codegen_->GenerateStaticOrDirectCall(invoke,
3104 locations->HasTemps()
3105 ? locations->GetTemp(0)
3106 : Location::NoLocation());
3107 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3108}
3109
Alexey Frunze53afca12015-11-05 16:34:23 -08003110void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003111 // Use the calling convention instead of the location of the receiver, as
3112 // intrinsics may have put the receiver in a different register. In the intrinsics
3113 // slow path, the arguments have been moved to the right place, so here we are
3114 // guaranteed that the receiver is the first register of the calling convention.
3115 InvokeDexCallingConvention calling_convention;
3116 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3117
Alexey Frunze53afca12015-11-05 16:34:23 -08003118 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003119 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3120 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3121 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003122 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003123
3124 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003125 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003126 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003127 // temp = temp->GetMethodAt(method_offset);
3128 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3129 // T9 = temp->GetEntryPoint();
3130 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3131 // T9();
3132 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003133 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003134}
3135
3136void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3137 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3138 return;
3139 }
3140
3141 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003142 DCHECK(!codegen_->IsLeafMethod());
3143 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3144}
3145
3146void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003147 InvokeRuntimeCallingConvention calling_convention;
3148 CodeGenerator::CreateLoadClassLocationSummary(
3149 cls,
3150 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003151 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003152}
3153
3154void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3155 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003156 if (cls->NeedsAccessCheck()) {
3157 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +01003158 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003159 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003160 return;
3161 }
3162
3163 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3164 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3165 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003166 DCHECK(!cls->CanCallRuntime());
3167 DCHECK(!cls->MustGenerateClinitCheck());
3168 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3169 ArtMethod::DeclaringClassOffset().Int32Value());
3170 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003171 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3172 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003173 __ LoadFromOffset(
3174 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003175 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003176 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3177 DCHECK(cls->CanCallRuntime());
3178 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3179 cls,
3180 cls,
3181 cls->GetDexPc(),
3182 cls->MustGenerateClinitCheck());
3183 codegen_->AddSlowPath(slow_path);
3184 if (!cls->IsInDexCache()) {
3185 __ Beqzc(out, slow_path->GetEntryLabel());
3186 }
3187 if (cls->MustGenerateClinitCheck()) {
3188 GenerateClassInitializationCheck(slow_path, out);
3189 } else {
3190 __ Bind(slow_path->GetExitLabel());
3191 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003192 }
3193 }
3194}
3195
David Brazdilcb1c0552015-08-04 16:22:25 +01003196static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003197 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003198}
3199
Alexey Frunze4dda3372015-06-01 18:31:49 -07003200void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3201 LocationSummary* locations =
3202 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3203 locations->SetOut(Location::RequiresRegister());
3204}
3205
3206void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3207 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003208 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3209}
3210
3211void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3212 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3213}
3214
3215void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3216 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003217}
3218
Alexey Frunze4dda3372015-06-01 18:31:49 -07003219void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003220 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3221 ? LocationSummary::kCallOnSlowPath
3222 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003224 locations->SetInAt(0, Location::RequiresRegister());
3225 locations->SetOut(Location::RequiresRegister());
3226}
3227
3228void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003229 // TODO: Re-add the compiler code to do string dex cache lookup again.
3230 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3231 codegen_->AddSlowPath(slow_path);
3232 __ Bc(slow_path->GetEntryLabel());
3233 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003234}
3235
Alexey Frunze4dda3372015-06-01 18:31:49 -07003236void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3238 locations->SetOut(Location::ConstantLocation(constant));
3239}
3240
3241void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3242 // Will be generated at use site.
3243}
3244
3245void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3246 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003247 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003248 InvokeRuntimeCallingConvention calling_convention;
3249 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3250}
3251
3252void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003253 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003254 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003255 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003256 if (instruction->IsEnter()) {
3257 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3258 } else {
3259 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3260 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003261}
3262
3263void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3264 LocationSummary* locations =
3265 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3266 switch (mul->GetResultType()) {
3267 case Primitive::kPrimInt:
3268 case Primitive::kPrimLong:
3269 locations->SetInAt(0, Location::RequiresRegister());
3270 locations->SetInAt(1, Location::RequiresRegister());
3271 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3272 break;
3273
3274 case Primitive::kPrimFloat:
3275 case Primitive::kPrimDouble:
3276 locations->SetInAt(0, Location::RequiresFpuRegister());
3277 locations->SetInAt(1, Location::RequiresFpuRegister());
3278 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3279 break;
3280
3281 default:
3282 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3283 }
3284}
3285
3286void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3287 Primitive::Type type = instruction->GetType();
3288 LocationSummary* locations = instruction->GetLocations();
3289
3290 switch (type) {
3291 case Primitive::kPrimInt:
3292 case Primitive::kPrimLong: {
3293 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3294 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3295 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3296 if (type == Primitive::kPrimInt)
3297 __ MulR6(dst, lhs, rhs);
3298 else
3299 __ Dmul(dst, lhs, rhs);
3300 break;
3301 }
3302 case Primitive::kPrimFloat:
3303 case Primitive::kPrimDouble: {
3304 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3305 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3306 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3307 if (type == Primitive::kPrimFloat)
3308 __ MulS(dst, lhs, rhs);
3309 else
3310 __ MulD(dst, lhs, rhs);
3311 break;
3312 }
3313 default:
3314 LOG(FATAL) << "Unexpected mul type " << type;
3315 }
3316}
3317
3318void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3319 LocationSummary* locations =
3320 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3321 switch (neg->GetResultType()) {
3322 case Primitive::kPrimInt:
3323 case Primitive::kPrimLong:
3324 locations->SetInAt(0, Location::RequiresRegister());
3325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3326 break;
3327
3328 case Primitive::kPrimFloat:
3329 case Primitive::kPrimDouble:
3330 locations->SetInAt(0, Location::RequiresFpuRegister());
3331 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3332 break;
3333
3334 default:
3335 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3336 }
3337}
3338
3339void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3340 Primitive::Type type = instruction->GetType();
3341 LocationSummary* locations = instruction->GetLocations();
3342
3343 switch (type) {
3344 case Primitive::kPrimInt:
3345 case Primitive::kPrimLong: {
3346 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3347 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3348 if (type == Primitive::kPrimInt)
3349 __ Subu(dst, ZERO, src);
3350 else
3351 __ Dsubu(dst, ZERO, src);
3352 break;
3353 }
3354 case Primitive::kPrimFloat:
3355 case Primitive::kPrimDouble: {
3356 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3357 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3358 if (type == Primitive::kPrimFloat)
3359 __ NegS(dst, src);
3360 else
3361 __ NegD(dst, src);
3362 break;
3363 }
3364 default:
3365 LOG(FATAL) << "Unexpected neg type " << type;
3366 }
3367}
3368
3369void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3370 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003372 InvokeRuntimeCallingConvention calling_convention;
3373 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3374 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3375 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3376 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3377}
3378
3379void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3380 LocationSummary* locations = instruction->GetLocations();
3381 // Move an uint16_t value to a register.
3382 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +01003383 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003384 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3385}
3386
3387void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3388 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003389 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003390 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003391 if (instruction->IsStringAlloc()) {
3392 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3393 } else {
3394 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3395 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3396 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003397 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3398}
3399
3400void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003401 if (instruction->IsStringAlloc()) {
3402 // String is allocated through StringFactory. Call NewEmptyString entry point.
3403 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003404 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003405 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003406 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3407 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3408 __ Jalr(T9);
3409 __ Nop();
3410 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3411 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003412 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003413 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3414 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003415}
3416
3417void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3418 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3419 locations->SetInAt(0, Location::RequiresRegister());
3420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3421}
3422
3423void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3424 Primitive::Type type = instruction->GetType();
3425 LocationSummary* locations = instruction->GetLocations();
3426
3427 switch (type) {
3428 case Primitive::kPrimInt:
3429 case Primitive::kPrimLong: {
3430 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3431 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3432 __ Nor(dst, src, ZERO);
3433 break;
3434 }
3435
3436 default:
3437 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3438 }
3439}
3440
3441void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3443 locations->SetInAt(0, Location::RequiresRegister());
3444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3445}
3446
3447void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3448 LocationSummary* locations = instruction->GetLocations();
3449 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3450 locations->InAt(0).AsRegister<GpuRegister>(),
3451 1);
3452}
3453
3454void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003455 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
3456 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003457}
3458
Calin Juravle2ae48182016-03-16 14:05:09 +00003459void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3460 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003461 return;
3462 }
3463 Location obj = instruction->GetLocations()->InAt(0);
3464
3465 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003466 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003467}
3468
Calin Juravle2ae48182016-03-16 14:05:09 +00003469void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003470 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003471 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003472
3473 Location obj = instruction->GetLocations()->InAt(0);
3474
3475 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3476}
3477
3478void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003479 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003480}
3481
3482void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3483 HandleBinaryOp(instruction);
3484}
3485
3486void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3487 HandleBinaryOp(instruction);
3488}
3489
3490void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3491 LOG(FATAL) << "Unreachable";
3492}
3493
3494void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3495 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3496}
3497
3498void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3499 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3500 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3501 if (location.IsStackSlot()) {
3502 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3503 } else if (location.IsDoubleStackSlot()) {
3504 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3505 }
3506 locations->SetOut(location);
3507}
3508
3509void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3510 ATTRIBUTE_UNUSED) {
3511 // Nothing to do, the parameter is already at its location.
3512}
3513
3514void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3515 LocationSummary* locations =
3516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3517 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3518}
3519
3520void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3521 ATTRIBUTE_UNUSED) {
3522 // Nothing to do, the method is already at its location.
3523}
3524
3525void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003527 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003528 locations->SetInAt(i, Location::Any());
3529 }
3530 locations->SetOut(Location::Any());
3531}
3532
3533void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3534 LOG(FATAL) << "Unreachable";
3535}
3536
3537void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3538 Primitive::Type type = rem->GetResultType();
3539 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003540 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3541 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3543
3544 switch (type) {
3545 case Primitive::kPrimInt:
3546 case Primitive::kPrimLong:
3547 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003548 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3550 break;
3551
3552 case Primitive::kPrimFloat:
3553 case Primitive::kPrimDouble: {
3554 InvokeRuntimeCallingConvention calling_convention;
3555 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3556 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3557 locations->SetOut(calling_convention.GetReturnLocation(type));
3558 break;
3559 }
3560
3561 default:
3562 LOG(FATAL) << "Unexpected rem type " << type;
3563 }
3564}
3565
3566void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3567 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003568
3569 switch (type) {
3570 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003571 case Primitive::kPrimLong:
3572 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003573 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003574
3575 case Primitive::kPrimFloat:
3576 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01003577 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
3578 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003579 if (type == Primitive::kPrimFloat) {
3580 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3581 } else {
3582 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3583 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003584 break;
3585 }
3586 default:
3587 LOG(FATAL) << "Unexpected rem type " << type;
3588 }
3589}
3590
3591void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3592 memory_barrier->SetLocations(nullptr);
3593}
3594
3595void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3596 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3597}
3598
3599void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3600 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3601 Primitive::Type return_type = ret->InputAt(0)->GetType();
3602 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3603}
3604
3605void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3606 codegen_->GenerateFrameExit();
3607}
3608
3609void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3610 ret->SetLocations(nullptr);
3611}
3612
3613void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3614 codegen_->GenerateFrameExit();
3615}
3616
Alexey Frunze92d90602015-12-18 18:16:36 -08003617void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3618 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003619}
3620
Alexey Frunze92d90602015-12-18 18:16:36 -08003621void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3622 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003623}
3624
Alexey Frunze4dda3372015-06-01 18:31:49 -07003625void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3626 HandleShift(shl);
3627}
3628
3629void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3630 HandleShift(shl);
3631}
3632
3633void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3634 HandleShift(shr);
3635}
3636
3637void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3638 HandleShift(shr);
3639}
3640
Alexey Frunze4dda3372015-06-01 18:31:49 -07003641void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3642 HandleBinaryOp(instruction);
3643}
3644
3645void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3646 HandleBinaryOp(instruction);
3647}
3648
3649void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3650 HandleFieldGet(instruction, instruction->GetFieldInfo());
3651}
3652
3653void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3654 HandleFieldGet(instruction, instruction->GetFieldInfo());
3655}
3656
3657void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3658 HandleFieldSet(instruction, instruction->GetFieldInfo());
3659}
3660
3661void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003662 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003663}
3664
Calin Juravlee460d1d2015-09-29 04:52:17 +01003665void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3666 HUnresolvedInstanceFieldGet* instruction) {
3667 FieldAccessCallingConventionMIPS64 calling_convention;
3668 codegen_->CreateUnresolvedFieldLocationSummary(
3669 instruction, instruction->GetFieldType(), calling_convention);
3670}
3671
3672void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3673 HUnresolvedInstanceFieldGet* instruction) {
3674 FieldAccessCallingConventionMIPS64 calling_convention;
3675 codegen_->GenerateUnresolvedFieldAccess(instruction,
3676 instruction->GetFieldType(),
3677 instruction->GetFieldIndex(),
3678 instruction->GetDexPc(),
3679 calling_convention);
3680}
3681
3682void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3683 HUnresolvedInstanceFieldSet* instruction) {
3684 FieldAccessCallingConventionMIPS64 calling_convention;
3685 codegen_->CreateUnresolvedFieldLocationSummary(
3686 instruction, instruction->GetFieldType(), calling_convention);
3687}
3688
3689void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3690 HUnresolvedInstanceFieldSet* instruction) {
3691 FieldAccessCallingConventionMIPS64 calling_convention;
3692 codegen_->GenerateUnresolvedFieldAccess(instruction,
3693 instruction->GetFieldType(),
3694 instruction->GetFieldIndex(),
3695 instruction->GetDexPc(),
3696 calling_convention);
3697}
3698
3699void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3700 HUnresolvedStaticFieldGet* instruction) {
3701 FieldAccessCallingConventionMIPS64 calling_convention;
3702 codegen_->CreateUnresolvedFieldLocationSummary(
3703 instruction, instruction->GetFieldType(), calling_convention);
3704}
3705
3706void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3707 HUnresolvedStaticFieldGet* instruction) {
3708 FieldAccessCallingConventionMIPS64 calling_convention;
3709 codegen_->GenerateUnresolvedFieldAccess(instruction,
3710 instruction->GetFieldType(),
3711 instruction->GetFieldIndex(),
3712 instruction->GetDexPc(),
3713 calling_convention);
3714}
3715
3716void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3717 HUnresolvedStaticFieldSet* instruction) {
3718 FieldAccessCallingConventionMIPS64 calling_convention;
3719 codegen_->CreateUnresolvedFieldLocationSummary(
3720 instruction, instruction->GetFieldType(), calling_convention);
3721}
3722
3723void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3724 HUnresolvedStaticFieldSet* instruction) {
3725 FieldAccessCallingConventionMIPS64 calling_convention;
3726 codegen_->GenerateUnresolvedFieldAccess(instruction,
3727 instruction->GetFieldType(),
3728 instruction->GetFieldIndex(),
3729 instruction->GetDexPc(),
3730 calling_convention);
3731}
3732
Alexey Frunze4dda3372015-06-01 18:31:49 -07003733void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01003734 LocationSummary* locations =
3735 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003736 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003737}
3738
3739void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3740 HBasicBlock* block = instruction->GetBlock();
3741 if (block->GetLoopInformation() != nullptr) {
3742 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3743 // The back edge will generate the suspend check.
3744 return;
3745 }
3746 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3747 // The goto will generate the suspend check.
3748 return;
3749 }
3750 GenerateSuspendCheck(instruction, nullptr);
3751}
3752
Alexey Frunze4dda3372015-06-01 18:31:49 -07003753void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3754 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003755 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003756 InvokeRuntimeCallingConvention calling_convention;
3757 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3758}
3759
3760void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003761 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003762 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3763}
3764
3765void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3766 Primitive::Type input_type = conversion->GetInputType();
3767 Primitive::Type result_type = conversion->GetResultType();
3768 DCHECK_NE(input_type, result_type);
3769
3770 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3771 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3772 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3773 }
3774
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003775 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3776
3777 if (Primitive::IsFloatingPointType(input_type)) {
3778 locations->SetInAt(0, Location::RequiresFpuRegister());
3779 } else {
3780 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003781 }
3782
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003783 if (Primitive::IsFloatingPointType(result_type)) {
3784 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003785 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003787 }
3788}
3789
3790void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3791 LocationSummary* locations = conversion->GetLocations();
3792 Primitive::Type result_type = conversion->GetResultType();
3793 Primitive::Type input_type = conversion->GetInputType();
3794
3795 DCHECK_NE(input_type, result_type);
3796
3797 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3798 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3799 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3800
3801 switch (result_type) {
3802 case Primitive::kPrimChar:
3803 __ Andi(dst, src, 0xFFFF);
3804 break;
3805 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003806 if (input_type == Primitive::kPrimLong) {
3807 // Type conversion from long to types narrower than int is a result of code
3808 // transformations. To avoid unpredictable results for SEB and SEH, we first
3809 // need to sign-extend the low 32-bit value into bits 32 through 63.
3810 __ Sll(dst, src, 0);
3811 __ Seb(dst, dst);
3812 } else {
3813 __ Seb(dst, src);
3814 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003815 break;
3816 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003817 if (input_type == Primitive::kPrimLong) {
3818 // Type conversion from long to types narrower than int is a result of code
3819 // transformations. To avoid unpredictable results for SEB and SEH, we first
3820 // need to sign-extend the low 32-bit value into bits 32 through 63.
3821 __ Sll(dst, src, 0);
3822 __ Seh(dst, dst);
3823 } else {
3824 __ Seh(dst, src);
3825 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003826 break;
3827 case Primitive::kPrimInt:
3828 case Primitive::kPrimLong:
3829 // Sign-extend 32-bit int into bits 32 through 63 for
3830 // int-to-long and long-to-int conversions
3831 __ Sll(dst, src, 0);
3832 break;
3833
3834 default:
3835 LOG(FATAL) << "Unexpected type conversion from " << input_type
3836 << " to " << result_type;
3837 }
3838 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003839 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3840 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3841 if (input_type == Primitive::kPrimLong) {
3842 __ Dmtc1(src, FTMP);
3843 if (result_type == Primitive::kPrimFloat) {
3844 __ Cvtsl(dst, FTMP);
3845 } else {
3846 __ Cvtdl(dst, FTMP);
3847 }
3848 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003849 __ Mtc1(src, FTMP);
3850 if (result_type == Primitive::kPrimFloat) {
3851 __ Cvtsw(dst, FTMP);
3852 } else {
3853 __ Cvtdw(dst, FTMP);
3854 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003855 }
3856 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3857 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003858 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3859 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3860 Mips64Label truncate;
3861 Mips64Label done;
3862
3863 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3864 // value when the input is either a NaN or is outside of the range of the output type
3865 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3866 // the same result.
3867 //
3868 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3869 // value of the output type if the input is outside of the range after the truncation or
3870 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3871 // results. This matches the desired float/double-to-int/long conversion exactly.
3872 //
3873 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3874 //
3875 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3876 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3877 // even though it must be NAN2008=1 on R6.
3878 //
3879 // The code takes care of the different behaviors by first comparing the input to the
3880 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3881 // If the input is greater than or equal to the minimum, it procedes to the truncate
3882 // instruction, which will handle such an input the same way irrespective of NAN2008.
3883 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3884 // in order to return either zero or the minimum value.
3885 //
3886 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3887 // truncate instruction for MIPS64R6.
3888 if (input_type == Primitive::kPrimFloat) {
3889 uint32_t min_val = (result_type == Primitive::kPrimLong)
3890 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3891 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3892 __ LoadConst32(TMP, min_val);
3893 __ Mtc1(TMP, FTMP);
3894 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003895 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003896 uint64_t min_val = (result_type == Primitive::kPrimLong)
3897 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3898 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3899 __ LoadConst64(TMP, min_val);
3900 __ Dmtc1(TMP, FTMP);
3901 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003902 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003903
3904 __ Bc1nez(FTMP, &truncate);
3905
3906 if (input_type == Primitive::kPrimFloat) {
3907 __ CmpEqS(FTMP, src, src);
3908 } else {
3909 __ CmpEqD(FTMP, src, src);
3910 }
3911 if (result_type == Primitive::kPrimLong) {
3912 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3913 } else {
3914 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3915 }
3916 __ Mfc1(TMP, FTMP);
3917 __ And(dst, dst, TMP);
3918
3919 __ Bc(&done);
3920
3921 __ Bind(&truncate);
3922
3923 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003924 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003925 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003926 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003927 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003928 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003929 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003930 } else {
3931 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003932 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003933 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003934 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003935 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003936 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003937 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003938
3939 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003940 } else if (Primitive::IsFloatingPointType(result_type) &&
3941 Primitive::IsFloatingPointType(input_type)) {
3942 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3943 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3944 if (result_type == Primitive::kPrimFloat) {
3945 __ Cvtsd(dst, src);
3946 } else {
3947 __ Cvtds(dst, src);
3948 }
3949 } else {
3950 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3951 << " to " << result_type;
3952 }
3953}
3954
3955void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3956 HandleShift(ushr);
3957}
3958
3959void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3960 HandleShift(ushr);
3961}
3962
3963void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3964 HandleBinaryOp(instruction);
3965}
3966
3967void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3968 HandleBinaryOp(instruction);
3969}
3970
3971void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3972 // Nothing to do, this should be removed during prepare for register allocator.
3973 LOG(FATAL) << "Unreachable";
3974}
3975
3976void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3977 // Nothing to do, this should be removed during prepare for register allocator.
3978 LOG(FATAL) << "Unreachable";
3979}
3980
3981void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003982 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003983}
3984
3985void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003986 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003987}
3988
3989void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003990 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003991}
3992
3993void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003994 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003995}
3996
3997void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003998 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003999}
4000
4001void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004002 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004003}
4004
4005void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004006 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004007}
4008
4009void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004010 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004011}
4012
4013void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004014 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004015}
4016
4017void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004018 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004019}
4020
4021void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004022 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004023}
4024
4025void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004026 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004027}
4028
Aart Bike9f37602015-10-09 11:15:55 -07004029void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004030 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004031}
4032
4033void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004034 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004035}
4036
4037void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004038 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004039}
4040
4041void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004042 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004043}
4044
4045void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004046 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004047}
4048
4049void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004050 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004051}
4052
4053void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004054 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004055}
4056
4057void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004058 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004059}
4060
Mark Mendellfe57faa2015-09-18 09:26:15 -04004061// Simple implementation of packed switch - generate cascaded compare/jumps.
4062void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4063 LocationSummary* locations =
4064 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4065 locations->SetInAt(0, Location::RequiresRegister());
4066}
4067
4068void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4069 int32_t lower_bound = switch_instr->GetStartValue();
4070 int32_t num_entries = switch_instr->GetNumEntries();
4071 LocationSummary* locations = switch_instr->GetLocations();
4072 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4073 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4074
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004075 // Create a set of compare/jumps.
4076 GpuRegister temp_reg = TMP;
4077 if (IsInt<16>(-lower_bound)) {
4078 __ Addiu(temp_reg, value_reg, -lower_bound);
4079 } else {
4080 __ LoadConst32(AT, -lower_bound);
4081 __ Addu(temp_reg, value_reg, AT);
4082 }
4083 // Jump to default if index is negative
4084 // Note: We don't check the case that index is positive while value < lower_bound, because in
4085 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4086 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4087
Mark Mendellfe57faa2015-09-18 09:26:15 -04004088 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004089 // Jump to successors[0] if value == lower_bound.
4090 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4091 int32_t last_index = 0;
4092 for (; num_entries - last_index > 2; last_index += 2) {
4093 __ Addiu(temp_reg, temp_reg, -2);
4094 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4095 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4096 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4097 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4098 }
4099 if (num_entries - last_index == 2) {
4100 // The last missing case_value.
4101 __ Addiu(temp_reg, temp_reg, -1);
4102 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004103 }
4104
4105 // And the default for any other value.
4106 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004107 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004108 }
4109}
4110
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004111void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4112 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4113}
4114
4115void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4116 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4117}
4118
Alexey Frunze4dda3372015-06-01 18:31:49 -07004119} // namespace mips64
4120} // namespace art