blob: 010bf24232c60a2502bcbf1d4f192ddb6541d928 [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
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100559 // Save the current method if we need it. Note that we do not
560 // do this in HCurrentMethod, as the instruction might have been removed
561 // in the SSA graph.
562 if (RequiresCurrentMethod()) {
563 static_assert(IsInt<16>(kCurrentMethodStackOffset),
564 "kCurrentMethodStackOffset must fit into int16_t");
565 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
566 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700567}
568
569void CodeGeneratorMIPS64::GenerateFrameExit() {
570 __ cfi().RememberState();
571
572 // TODO: anything related to T9/GP/GOT/PIC/.so's?
573
574 if (!HasEmptyFrame()) {
575 // Deallocate the rest of the frame.
576
577 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
578
579 // Restore callee-saved registers.
580 // Note that their cumulative size is small and they can be indexed using
581 // 16-bit offsets.
582
583 // TODO: increment/decrement SP in one step instead of two or remove this comment.
584
585 uint32_t ofs = 0;
586
587 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
588 FpuRegister reg = kFpuCalleeSaves[i];
589 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
590 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200591 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000592 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700593 }
594 }
595
596 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
597 GpuRegister reg = kCoreCalleeSaves[i];
598 if (allocated_registers_.ContainsCoreRegister(reg)) {
599 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200600 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700601 __ cfi().Restore(DWARFReg(reg));
602 }
603 }
604
605 DCHECK_EQ(ofs, FrameEntrySpillSize());
606 __ DecreaseFrameSize(ofs);
607 }
608
609 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700610 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700611
612 __ cfi().RestoreState();
613 __ cfi().DefCFAOffset(GetFrameSize());
614}
615
616void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
617 __ Bind(GetLabelOf(block));
618}
619
620void CodeGeneratorMIPS64::MoveLocation(Location destination,
621 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100622 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700623 if (source.Equals(destination)) {
624 return;
625 }
626
627 // A valid move can always be inferred from the destination and source
628 // locations. When moving from and to a register, the argument type can be
629 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100630 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700631 DCHECK_EQ(unspecified_type, false);
632
633 if (destination.IsRegister() || destination.IsFpuRegister()) {
634 if (unspecified_type) {
635 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
636 if (source.IsStackSlot() ||
637 (src_cst != nullptr && (src_cst->IsIntConstant()
638 || src_cst->IsFloatConstant()
639 || src_cst->IsNullConstant()))) {
640 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100641 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700642 } else {
643 // If the source is a double stack slot or a 64bit constant, a 64bit
644 // type is appropriate. Else the source is a register, and since the
645 // type has not been specified, we chose a 64bit type to force a 64bit
646 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100647 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700648 }
649 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100650 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
651 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700652 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
653 // Move to GPR/FPR from stack
654 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100655 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700656 __ LoadFpuFromOffset(load_type,
657 destination.AsFpuRegister<FpuRegister>(),
658 SP,
659 source.GetStackIndex());
660 } else {
661 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
662 __ LoadFromOffset(load_type,
663 destination.AsRegister<GpuRegister>(),
664 SP,
665 source.GetStackIndex());
666 }
667 } else if (source.IsConstant()) {
668 // Move to GPR/FPR from constant
669 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100670 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700671 gpr = destination.AsRegister<GpuRegister>();
672 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100673 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700674 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
675 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
676 gpr = ZERO;
677 } else {
678 __ LoadConst32(gpr, value);
679 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700680 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700681 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
682 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
683 gpr = ZERO;
684 } else {
685 __ LoadConst64(gpr, value);
686 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700687 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100688 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700689 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100690 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700691 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
692 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100693 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700694 if (destination.IsRegister()) {
695 // Move to GPR from GPR
696 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
697 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100698 DCHECK(destination.IsFpuRegister());
699 if (Primitive::Is64BitType(dst_type)) {
700 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
701 } else {
702 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
703 }
704 }
705 } else if (source.IsFpuRegister()) {
706 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700707 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100708 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700709 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
710 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700712 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
713 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100714 } else {
715 DCHECK(destination.IsRegister());
716 if (Primitive::Is64BitType(dst_type)) {
717 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
718 } else {
719 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
720 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700721 }
722 }
723 } else { // The destination is not a register. It must be a stack slot.
724 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
725 if (source.IsRegister() || source.IsFpuRegister()) {
726 if (unspecified_type) {
727 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100728 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700729 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100730 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700731 }
732 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100733 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
734 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700735 // Move to stack from GPR/FPR
736 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
737 if (source.IsRegister()) {
738 __ StoreToOffset(store_type,
739 source.AsRegister<GpuRegister>(),
740 SP,
741 destination.GetStackIndex());
742 } else {
743 __ StoreFpuToOffset(store_type,
744 source.AsFpuRegister<FpuRegister>(),
745 SP,
746 destination.GetStackIndex());
747 }
748 } else if (source.IsConstant()) {
749 // Move to stack from constant
750 HConstant* src_cst = source.GetConstant();
751 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700752 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700753 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700754 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
755 if (value != 0) {
756 gpr = TMP;
757 __ LoadConst32(gpr, value);
758 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700759 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700760 DCHECK(destination.IsDoubleStackSlot());
761 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
762 if (value != 0) {
763 gpr = TMP;
764 __ LoadConst64(gpr, value);
765 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700766 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700767 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700768 } else {
769 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
770 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
771 // Move to stack from stack
772 if (destination.IsStackSlot()) {
773 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
774 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
775 } else {
776 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
777 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
778 }
779 }
780 }
781}
782
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700783void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700784 DCHECK(!loc1.IsConstant());
785 DCHECK(!loc2.IsConstant());
786
787 if (loc1.Equals(loc2)) {
788 return;
789 }
790
791 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
792 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
793 bool is_fp_reg1 = loc1.IsFpuRegister();
794 bool is_fp_reg2 = loc2.IsFpuRegister();
795
796 if (loc2.IsRegister() && loc1.IsRegister()) {
797 // Swap 2 GPRs
798 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
799 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
800 __ Move(TMP, r2);
801 __ Move(r2, r1);
802 __ Move(r1, TMP);
803 } else if (is_fp_reg2 && is_fp_reg1) {
804 // Swap 2 FPRs
805 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
806 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700807 if (type == Primitive::kPrimFloat) {
808 __ MovS(FTMP, r1);
809 __ MovS(r1, r2);
810 __ MovS(r2, FTMP);
811 } else {
812 DCHECK_EQ(type, Primitive::kPrimDouble);
813 __ MovD(FTMP, r1);
814 __ MovD(r1, r2);
815 __ MovD(r2, FTMP);
816 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700817 } else if (is_slot1 != is_slot2) {
818 // Swap GPR/FPR and stack slot
819 Location reg_loc = is_slot1 ? loc2 : loc1;
820 Location mem_loc = is_slot1 ? loc1 : loc2;
821 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
822 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
823 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
824 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
825 if (reg_loc.IsFpuRegister()) {
826 __ StoreFpuToOffset(store_type,
827 reg_loc.AsFpuRegister<FpuRegister>(),
828 SP,
829 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700830 if (mem_loc.IsStackSlot()) {
831 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
832 } else {
833 DCHECK(mem_loc.IsDoubleStackSlot());
834 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
835 }
836 } else {
837 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
838 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
839 }
840 } else if (is_slot1 && is_slot2) {
841 move_resolver_.Exchange(loc1.GetStackIndex(),
842 loc2.GetStackIndex(),
843 loc1.IsDoubleStackSlot());
844 } else {
845 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
846 }
847}
848
Calin Juravle175dc732015-08-25 15:42:32 +0100849void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
850 DCHECK(location.IsRegister());
851 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
852}
853
Calin Juravlee460d1d2015-09-29 04:52:17 +0100854void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
855 if (location.IsRegister()) {
856 locations->AddTemp(location);
857 } else {
858 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
859 }
860}
861
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100862void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
863 GpuRegister value,
864 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700865 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700866 GpuRegister card = AT;
867 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100868 if (value_can_be_null) {
869 __ Beqzc(value, &done);
870 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700871 __ LoadFromOffset(kLoadDoubleword,
872 card,
873 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700874 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700875 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
876 __ Daddu(temp, card, temp);
877 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100878 if (value_can_be_null) {
879 __ Bind(&done);
880 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700881}
882
David Brazdil58282f42016-01-14 12:45:10 +0000883void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700884 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
885 blocked_core_registers_[ZERO] = true;
886 blocked_core_registers_[K0] = true;
887 blocked_core_registers_[K1] = true;
888 blocked_core_registers_[GP] = true;
889 blocked_core_registers_[SP] = true;
890 blocked_core_registers_[RA] = true;
891
Lazar Trsicd9672662015-09-03 17:33:01 +0200892 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
893 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700894 blocked_core_registers_[AT] = true;
895 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200896 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700897 blocked_fpu_registers_[FTMP] = true;
898
899 // Reserve suspend and thread registers.
900 blocked_core_registers_[S0] = true;
901 blocked_core_registers_[TR] = true;
902
903 // Reserve T9 for function calls
904 blocked_core_registers_[T9] = true;
905
906 // TODO: review; anything else?
907
Goran Jakovljevic782be112016-06-21 12:39:04 +0200908 if (GetGraph()->IsDebuggable()) {
909 // Stubs do not save callee-save floating point registers. If the graph
910 // is debuggable, we need to deal with these registers differently. For
911 // now, just block them.
912 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
913 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
914 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700915 }
916}
917
Alexey Frunze4dda3372015-06-01 18:31:49 -0700918size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
919 __ StoreToOffset(kStoreDoubleword, 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::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
924 __ LoadFromOffset(kLoadDoubleword, GpuRegister(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::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
929 __ StoreFpuToOffset(kStoreDoubleword, 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
933size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
934 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200935 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700936}
937
938void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100939 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700940}
941
942void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100943 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944}
945
Calin Juravle175dc732015-08-25 15:42:32 +0100946void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947 HInstruction* instruction,
948 uint32_t dex_pc,
949 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100950 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951 // TODO: anything related to T9/GP/GOT/PIC/.so's?
Serban Constantinescufc734082016-07-19 17:18:07 +0100952 __ LoadFromOffset(kLoadDoubleword,
953 T9,
954 TR,
955 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700956 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700957 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +0100958 if (EntrypointRequiresStackMap(entrypoint)) {
959 RecordPcInfo(instruction, dex_pc, slow_path);
960 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700961}
962
963void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
964 GpuRegister class_reg) {
965 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
966 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
967 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
968 // TODO: barrier needed?
969 __ Bind(slow_path->GetExitLabel());
970}
971
972void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
973 __ Sync(0); // only stype 0 is supported
974}
975
976void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
977 HBasicBlock* successor) {
978 SuspendCheckSlowPathMIPS64* slow_path =
979 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
980 codegen_->AddSlowPath(slow_path);
981
982 __ LoadFromOffset(kLoadUnsignedHalfword,
983 TMP,
984 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700985 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700986 if (successor == nullptr) {
987 __ Bnezc(TMP, slow_path->GetEntryLabel());
988 __ Bind(slow_path->GetReturnLabel());
989 } else {
990 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700991 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700992 // slow_path will return to GetLabelOf(successor).
993 }
994}
995
996InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
997 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800998 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700999 assembler_(codegen->GetAssembler()),
1000 codegen_(codegen) {}
1001
1002void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1003 DCHECK_EQ(instruction->InputCount(), 2U);
1004 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1005 Primitive::Type type = instruction->GetResultType();
1006 switch (type) {
1007 case Primitive::kPrimInt:
1008 case Primitive::kPrimLong: {
1009 locations->SetInAt(0, Location::RequiresRegister());
1010 HInstruction* right = instruction->InputAt(1);
1011 bool can_use_imm = false;
1012 if (right->IsConstant()) {
1013 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1014 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1015 can_use_imm = IsUint<16>(imm);
1016 } else if (instruction->IsAdd()) {
1017 can_use_imm = IsInt<16>(imm);
1018 } else {
1019 DCHECK(instruction->IsSub());
1020 can_use_imm = IsInt<16>(-imm);
1021 }
1022 }
1023 if (can_use_imm)
1024 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1025 else
1026 locations->SetInAt(1, Location::RequiresRegister());
1027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1028 }
1029 break;
1030
1031 case Primitive::kPrimFloat:
1032 case Primitive::kPrimDouble:
1033 locations->SetInAt(0, Location::RequiresFpuRegister());
1034 locations->SetInAt(1, Location::RequiresFpuRegister());
1035 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1036 break;
1037
1038 default:
1039 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1040 }
1041}
1042
1043void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1044 Primitive::Type type = instruction->GetType();
1045 LocationSummary* locations = instruction->GetLocations();
1046
1047 switch (type) {
1048 case Primitive::kPrimInt:
1049 case Primitive::kPrimLong: {
1050 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1051 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1052 Location rhs_location = locations->InAt(1);
1053
1054 GpuRegister rhs_reg = ZERO;
1055 int64_t rhs_imm = 0;
1056 bool use_imm = rhs_location.IsConstant();
1057 if (use_imm) {
1058 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1059 } else {
1060 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1061 }
1062
1063 if (instruction->IsAnd()) {
1064 if (use_imm)
1065 __ Andi(dst, lhs, rhs_imm);
1066 else
1067 __ And(dst, lhs, rhs_reg);
1068 } else if (instruction->IsOr()) {
1069 if (use_imm)
1070 __ Ori(dst, lhs, rhs_imm);
1071 else
1072 __ Or(dst, lhs, rhs_reg);
1073 } else if (instruction->IsXor()) {
1074 if (use_imm)
1075 __ Xori(dst, lhs, rhs_imm);
1076 else
1077 __ Xor(dst, lhs, rhs_reg);
1078 } else if (instruction->IsAdd()) {
1079 if (type == Primitive::kPrimInt) {
1080 if (use_imm)
1081 __ Addiu(dst, lhs, rhs_imm);
1082 else
1083 __ Addu(dst, lhs, rhs_reg);
1084 } else {
1085 if (use_imm)
1086 __ Daddiu(dst, lhs, rhs_imm);
1087 else
1088 __ Daddu(dst, lhs, rhs_reg);
1089 }
1090 } else {
1091 DCHECK(instruction->IsSub());
1092 if (type == Primitive::kPrimInt) {
1093 if (use_imm)
1094 __ Addiu(dst, lhs, -rhs_imm);
1095 else
1096 __ Subu(dst, lhs, rhs_reg);
1097 } else {
1098 if (use_imm)
1099 __ Daddiu(dst, lhs, -rhs_imm);
1100 else
1101 __ Dsubu(dst, lhs, rhs_reg);
1102 }
1103 }
1104 break;
1105 }
1106 case Primitive::kPrimFloat:
1107 case Primitive::kPrimDouble: {
1108 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1109 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1110 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1111 if (instruction->IsAdd()) {
1112 if (type == Primitive::kPrimFloat)
1113 __ AddS(dst, lhs, rhs);
1114 else
1115 __ AddD(dst, lhs, rhs);
1116 } else if (instruction->IsSub()) {
1117 if (type == Primitive::kPrimFloat)
1118 __ SubS(dst, lhs, rhs);
1119 else
1120 __ SubD(dst, lhs, rhs);
1121 } else {
1122 LOG(FATAL) << "Unexpected floating-point binary operation";
1123 }
1124 break;
1125 }
1126 default:
1127 LOG(FATAL) << "Unexpected binary operation type " << type;
1128 }
1129}
1130
1131void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001132 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001133
1134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1135 Primitive::Type type = instr->GetResultType();
1136 switch (type) {
1137 case Primitive::kPrimInt:
1138 case Primitive::kPrimLong: {
1139 locations->SetInAt(0, Location::RequiresRegister());
1140 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001141 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001142 break;
1143 }
1144 default:
1145 LOG(FATAL) << "Unexpected shift type " << type;
1146 }
1147}
1148
1149void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001150 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001151 LocationSummary* locations = instr->GetLocations();
1152 Primitive::Type type = instr->GetType();
1153
1154 switch (type) {
1155 case Primitive::kPrimInt:
1156 case Primitive::kPrimLong: {
1157 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1158 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1159 Location rhs_location = locations->InAt(1);
1160
1161 GpuRegister rhs_reg = ZERO;
1162 int64_t rhs_imm = 0;
1163 bool use_imm = rhs_location.IsConstant();
1164 if (use_imm) {
1165 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1166 } else {
1167 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1168 }
1169
1170 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001171 uint32_t shift_value = rhs_imm &
1172 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001173
Alexey Frunze92d90602015-12-18 18:16:36 -08001174 if (shift_value == 0) {
1175 if (dst != lhs) {
1176 __ Move(dst, lhs);
1177 }
1178 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001179 if (instr->IsShl()) {
1180 __ Sll(dst, lhs, shift_value);
1181 } else if (instr->IsShr()) {
1182 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001183 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001184 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001185 } else {
1186 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001187 }
1188 } else {
1189 if (shift_value < 32) {
1190 if (instr->IsShl()) {
1191 __ Dsll(dst, lhs, shift_value);
1192 } else if (instr->IsShr()) {
1193 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001194 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001195 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001196 } else {
1197 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001198 }
1199 } else {
1200 shift_value -= 32;
1201 if (instr->IsShl()) {
1202 __ Dsll32(dst, lhs, shift_value);
1203 } else if (instr->IsShr()) {
1204 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001205 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001206 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001207 } else {
1208 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001209 }
1210 }
1211 }
1212 } else {
1213 if (type == Primitive::kPrimInt) {
1214 if (instr->IsShl()) {
1215 __ Sllv(dst, lhs, rhs_reg);
1216 } else if (instr->IsShr()) {
1217 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001218 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001219 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001220 } else {
1221 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001222 }
1223 } else {
1224 if (instr->IsShl()) {
1225 __ Dsllv(dst, lhs, rhs_reg);
1226 } else if (instr->IsShr()) {
1227 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001228 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001229 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001230 } else {
1231 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001232 }
1233 }
1234 }
1235 break;
1236 }
1237 default:
1238 LOG(FATAL) << "Unexpected shift operation type " << type;
1239 }
1240}
1241
1242void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1243 HandleBinaryOp(instruction);
1244}
1245
1246void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1247 HandleBinaryOp(instruction);
1248}
1249
1250void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1251 HandleBinaryOp(instruction);
1252}
1253
1254void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1255 HandleBinaryOp(instruction);
1256}
1257
1258void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1259 LocationSummary* locations =
1260 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1261 locations->SetInAt(0, Location::RequiresRegister());
1262 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1263 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1264 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1265 } else {
1266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1267 }
1268}
1269
1270void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1271 LocationSummary* locations = instruction->GetLocations();
1272 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1273 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001274 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001275
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001276 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001277 switch (type) {
1278 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001279 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1280 if (index.IsConstant()) {
1281 size_t offset =
1282 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1283 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1284 } else {
1285 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1286 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1287 }
1288 break;
1289 }
1290
1291 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001292 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1293 if (index.IsConstant()) {
1294 size_t offset =
1295 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1296 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1297 } else {
1298 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1299 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1300 }
1301 break;
1302 }
1303
1304 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001305 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1306 if (index.IsConstant()) {
1307 size_t offset =
1308 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1309 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1310 } else {
1311 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1312 __ Daddu(TMP, obj, TMP);
1313 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1314 }
1315 break;
1316 }
1317
1318 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001319 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1320 if (index.IsConstant()) {
1321 size_t offset =
1322 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1323 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1324 } else {
1325 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1326 __ Daddu(TMP, obj, TMP);
1327 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1328 }
1329 break;
1330 }
1331
1332 case Primitive::kPrimInt:
1333 case Primitive::kPrimNot: {
1334 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001335 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1336 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1337 if (index.IsConstant()) {
1338 size_t offset =
1339 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1340 __ LoadFromOffset(load_type, out, obj, offset);
1341 } else {
1342 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1343 __ Daddu(TMP, obj, TMP);
1344 __ LoadFromOffset(load_type, out, TMP, data_offset);
1345 }
1346 break;
1347 }
1348
1349 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001350 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1351 if (index.IsConstant()) {
1352 size_t offset =
1353 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1354 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1355 } else {
1356 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1357 __ Daddu(TMP, obj, TMP);
1358 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1359 }
1360 break;
1361 }
1362
1363 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001364 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1365 if (index.IsConstant()) {
1366 size_t offset =
1367 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1368 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1369 } else {
1370 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1371 __ Daddu(TMP, obj, TMP);
1372 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1373 }
1374 break;
1375 }
1376
1377 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001378 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1379 if (index.IsConstant()) {
1380 size_t offset =
1381 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1382 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1383 } else {
1384 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1385 __ Daddu(TMP, obj, TMP);
1386 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1387 }
1388 break;
1389 }
1390
1391 case Primitive::kPrimVoid:
1392 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1393 UNREACHABLE();
1394 }
1395 codegen_->MaybeRecordImplicitNullCheck(instruction);
1396}
1397
1398void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1399 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1400 locations->SetInAt(0, Location::RequiresRegister());
1401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1402}
1403
1404void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1405 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001406 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001407 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1408 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1409 __ LoadFromOffset(kLoadWord, out, obj, offset);
1410 codegen_->MaybeRecordImplicitNullCheck(instruction);
1411}
1412
1413void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001414 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1416 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001417 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001418 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001419 InvokeRuntimeCallingConvention calling_convention;
1420 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1421 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1422 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1423 } else {
1424 locations->SetInAt(0, Location::RequiresRegister());
1425 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1426 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1427 locations->SetInAt(2, Location::RequiresFpuRegister());
1428 } else {
1429 locations->SetInAt(2, Location::RequiresRegister());
1430 }
1431 }
1432}
1433
1434void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1435 LocationSummary* locations = instruction->GetLocations();
1436 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1437 Location index = locations->InAt(1);
1438 Primitive::Type value_type = instruction->GetComponentType();
1439 bool needs_runtime_call = locations->WillCall();
1440 bool needs_write_barrier =
1441 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1442
1443 switch (value_type) {
1444 case Primitive::kPrimBoolean:
1445 case Primitive::kPrimByte: {
1446 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1447 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1448 if (index.IsConstant()) {
1449 size_t offset =
1450 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1451 __ StoreToOffset(kStoreByte, value, obj, offset);
1452 } else {
1453 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1454 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1455 }
1456 break;
1457 }
1458
1459 case Primitive::kPrimShort:
1460 case Primitive::kPrimChar: {
1461 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1462 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1463 if (index.IsConstant()) {
1464 size_t offset =
1465 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1466 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1467 } else {
1468 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1469 __ Daddu(TMP, obj, TMP);
1470 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1471 }
1472 break;
1473 }
1474
1475 case Primitive::kPrimInt:
1476 case Primitive::kPrimNot: {
1477 if (!needs_runtime_call) {
1478 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1479 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1480 if (index.IsConstant()) {
1481 size_t offset =
1482 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1483 __ StoreToOffset(kStoreWord, value, obj, offset);
1484 } else {
1485 DCHECK(index.IsRegister()) << index;
1486 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1487 __ Daddu(TMP, obj, TMP);
1488 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1489 }
1490 codegen_->MaybeRecordImplicitNullCheck(instruction);
1491 if (needs_write_barrier) {
1492 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001493 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001494 }
1495 } else {
1496 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001497 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001498 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001499 }
1500 break;
1501 }
1502
1503 case Primitive::kPrimLong: {
1504 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1505 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1506 if (index.IsConstant()) {
1507 size_t offset =
1508 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1509 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1510 } else {
1511 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1512 __ Daddu(TMP, obj, TMP);
1513 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1514 }
1515 break;
1516 }
1517
1518 case Primitive::kPrimFloat: {
1519 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1520 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1521 DCHECK(locations->InAt(2).IsFpuRegister());
1522 if (index.IsConstant()) {
1523 size_t offset =
1524 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1525 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1526 } else {
1527 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1528 __ Daddu(TMP, obj, TMP);
1529 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1530 }
1531 break;
1532 }
1533
1534 case Primitive::kPrimDouble: {
1535 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1536 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1537 DCHECK(locations->InAt(2).IsFpuRegister());
1538 if (index.IsConstant()) {
1539 size_t offset =
1540 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1541 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1542 } else {
1543 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1544 __ Daddu(TMP, obj, TMP);
1545 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1546 }
1547 break;
1548 }
1549
1550 case Primitive::kPrimVoid:
1551 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1552 UNREACHABLE();
1553 }
1554
1555 // Ints and objects are handled in the switch.
1556 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1557 codegen_->MaybeRecordImplicitNullCheck(instruction);
1558 }
1559}
1560
1561void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001562 RegisterSet caller_saves = RegisterSet::Empty();
1563 InvokeRuntimeCallingConvention calling_convention;
1564 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1565 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1566 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001567 locations->SetInAt(0, Location::RequiresRegister());
1568 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001569}
1570
1571void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1572 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001573 BoundsCheckSlowPathMIPS64* slow_path =
1574 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001575 codegen_->AddSlowPath(slow_path);
1576
1577 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1578 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1579
1580 // length is limited by the maximum positive signed 32-bit integer.
1581 // Unsigned comparison of length and index checks for index < 0
1582 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001583 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001584}
1585
1586void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1588 instruction,
1589 LocationSummary::kCallOnSlowPath);
1590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001592 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001593 locations->AddTemp(Location::RequiresRegister());
1594}
1595
1596void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1597 LocationSummary* locations = instruction->GetLocations();
1598 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1599 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1600 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1601
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001602 SlowPathCodeMIPS64* slow_path =
1603 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001604 codegen_->AddSlowPath(slow_path);
1605
1606 // TODO: avoid this check if we know obj is not null.
1607 __ Beqzc(obj, slow_path->GetExitLabel());
1608 // Compare the class of `obj` with `cls`.
1609 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1610 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1611 __ Bind(slow_path->GetExitLabel());
1612}
1613
1614void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1615 LocationSummary* locations =
1616 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1617 locations->SetInAt(0, Location::RequiresRegister());
1618 if (check->HasUses()) {
1619 locations->SetOut(Location::SameAsFirstInput());
1620 }
1621}
1622
1623void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1624 // We assume the class is not null.
1625 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1626 check->GetLoadClass(),
1627 check,
1628 check->GetDexPc(),
1629 true);
1630 codegen_->AddSlowPath(slow_path);
1631 GenerateClassInitializationCheck(slow_path,
1632 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1633}
1634
1635void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1636 Primitive::Type in_type = compare->InputAt(0)->GetType();
1637
Alexey Frunze299a9392015-12-08 16:08:02 -08001638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001639
1640 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001641 case Primitive::kPrimBoolean:
1642 case Primitive::kPrimByte:
1643 case Primitive::kPrimShort:
1644 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001645 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001646 case Primitive::kPrimLong:
1647 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001648 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1650 break;
1651
1652 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001653 case Primitive::kPrimDouble:
1654 locations->SetInAt(0, Location::RequiresFpuRegister());
1655 locations->SetInAt(1, Location::RequiresFpuRegister());
1656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001657 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001658
1659 default:
1660 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1661 }
1662}
1663
1664void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1665 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001666 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001667 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1668
1669 // 0 if: left == right
1670 // 1 if: left > right
1671 // -1 if: left < right
1672 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001673 case Primitive::kPrimBoolean:
1674 case Primitive::kPrimByte:
1675 case Primitive::kPrimShort:
1676 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001677 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001678 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001679 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001680 Location rhs_location = locations->InAt(1);
1681 bool use_imm = rhs_location.IsConstant();
1682 GpuRegister rhs = ZERO;
1683 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001684 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001685 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1686 if (value != 0) {
1687 rhs = AT;
1688 __ LoadConst64(rhs, value);
1689 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001690 } else {
1691 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1692 if (value != 0) {
1693 rhs = AT;
1694 __ LoadConst32(rhs, value);
1695 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001696 }
1697 } else {
1698 rhs = rhs_location.AsRegister<GpuRegister>();
1699 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001700 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001701 __ Slt(res, rhs, lhs);
1702 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001703 break;
1704 }
1705
Alexey Frunze299a9392015-12-08 16:08:02 -08001706 case Primitive::kPrimFloat: {
1707 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1708 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1709 Mips64Label done;
1710 __ CmpEqS(FTMP, lhs, rhs);
1711 __ LoadConst32(res, 0);
1712 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001713 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001714 __ CmpLtS(FTMP, lhs, rhs);
1715 __ LoadConst32(res, -1);
1716 __ Bc1nez(FTMP, &done);
1717 __ LoadConst32(res, 1);
1718 } else {
1719 __ CmpLtS(FTMP, rhs, lhs);
1720 __ LoadConst32(res, 1);
1721 __ Bc1nez(FTMP, &done);
1722 __ LoadConst32(res, -1);
1723 }
1724 __ Bind(&done);
1725 break;
1726 }
1727
Alexey Frunze4dda3372015-06-01 18:31:49 -07001728 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001729 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1730 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1731 Mips64Label done;
1732 __ CmpEqD(FTMP, lhs, rhs);
1733 __ LoadConst32(res, 0);
1734 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001735 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001736 __ CmpLtD(FTMP, lhs, rhs);
1737 __ LoadConst32(res, -1);
1738 __ Bc1nez(FTMP, &done);
1739 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001740 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001741 __ CmpLtD(FTMP, rhs, lhs);
1742 __ LoadConst32(res, 1);
1743 __ Bc1nez(FTMP, &done);
1744 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001745 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001746 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001747 break;
1748 }
1749
1750 default:
1751 LOG(FATAL) << "Unimplemented compare type " << in_type;
1752 }
1753}
1754
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001755void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001756 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001757 switch (instruction->InputAt(0)->GetType()) {
1758 default:
1759 case Primitive::kPrimLong:
1760 locations->SetInAt(0, Location::RequiresRegister());
1761 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1762 break;
1763
1764 case Primitive::kPrimFloat:
1765 case Primitive::kPrimDouble:
1766 locations->SetInAt(0, Location::RequiresFpuRegister());
1767 locations->SetInAt(1, Location::RequiresFpuRegister());
1768 break;
1769 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001770 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1772 }
1773}
1774
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001775void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001776 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001777 return;
1778 }
1779
Alexey Frunze299a9392015-12-08 16:08:02 -08001780 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001781 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001782 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001783 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001784
Alexey Frunze299a9392015-12-08 16:08:02 -08001785 switch (type) {
1786 default:
1787 // Integer case.
1788 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1789 return;
1790 case Primitive::kPrimLong:
1791 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1792 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001793
Alexey Frunze299a9392015-12-08 16:08:02 -08001794 case Primitive::kPrimFloat:
1795 case Primitive::kPrimDouble:
1796 // TODO: don't use branches.
1797 GenerateFpCompareAndBranch(instruction->GetCondition(),
1798 instruction->IsGtBias(),
1799 type,
1800 locations,
1801 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001802 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001804
1805 // Convert the branches into the result.
1806 Mips64Label done;
1807
1808 // False case: result = 0.
1809 __ LoadConst32(dst, 0);
1810 __ Bc(&done);
1811
1812 // True case: result = 1.
1813 __ Bind(&true_label);
1814 __ LoadConst32(dst, 1);
1815 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001816}
1817
Alexey Frunzec857c742015-09-23 15:12:39 -07001818void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1819 DCHECK(instruction->IsDiv() || instruction->IsRem());
1820 Primitive::Type type = instruction->GetResultType();
1821
1822 LocationSummary* locations = instruction->GetLocations();
1823 Location second = locations->InAt(1);
1824 DCHECK(second.IsConstant());
1825
1826 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1827 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1828 int64_t imm = Int64FromConstant(second.GetConstant());
1829 DCHECK(imm == 1 || imm == -1);
1830
1831 if (instruction->IsRem()) {
1832 __ Move(out, ZERO);
1833 } else {
1834 if (imm == -1) {
1835 if (type == Primitive::kPrimInt) {
1836 __ Subu(out, ZERO, dividend);
1837 } else {
1838 DCHECK_EQ(type, Primitive::kPrimLong);
1839 __ Dsubu(out, ZERO, dividend);
1840 }
1841 } else if (out != dividend) {
1842 __ Move(out, dividend);
1843 }
1844 }
1845}
1846
1847void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1848 DCHECK(instruction->IsDiv() || instruction->IsRem());
1849 Primitive::Type type = instruction->GetResultType();
1850
1851 LocationSummary* locations = instruction->GetLocations();
1852 Location second = locations->InAt(1);
1853 DCHECK(second.IsConstant());
1854
1855 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1856 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1857 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001858 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001859 int ctz_imm = CTZ(abs_imm);
1860
1861 if (instruction->IsDiv()) {
1862 if (type == Primitive::kPrimInt) {
1863 if (ctz_imm == 1) {
1864 // Fast path for division by +/-2, which is very common.
1865 __ Srl(TMP, dividend, 31);
1866 } else {
1867 __ Sra(TMP, dividend, 31);
1868 __ Srl(TMP, TMP, 32 - ctz_imm);
1869 }
1870 __ Addu(out, dividend, TMP);
1871 __ Sra(out, out, ctz_imm);
1872 if (imm < 0) {
1873 __ Subu(out, ZERO, out);
1874 }
1875 } else {
1876 DCHECK_EQ(type, Primitive::kPrimLong);
1877 if (ctz_imm == 1) {
1878 // Fast path for division by +/-2, which is very common.
1879 __ Dsrl32(TMP, dividend, 31);
1880 } else {
1881 __ Dsra32(TMP, dividend, 31);
1882 if (ctz_imm > 32) {
1883 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1884 } else {
1885 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1886 }
1887 }
1888 __ Daddu(out, dividend, TMP);
1889 if (ctz_imm < 32) {
1890 __ Dsra(out, out, ctz_imm);
1891 } else {
1892 __ Dsra32(out, out, ctz_imm - 32);
1893 }
1894 if (imm < 0) {
1895 __ Dsubu(out, ZERO, out);
1896 }
1897 }
1898 } else {
1899 if (type == Primitive::kPrimInt) {
1900 if (ctz_imm == 1) {
1901 // Fast path for modulo +/-2, which is very common.
1902 __ Sra(TMP, dividend, 31);
1903 __ Subu(out, dividend, TMP);
1904 __ Andi(out, out, 1);
1905 __ Addu(out, out, TMP);
1906 } else {
1907 __ Sra(TMP, dividend, 31);
1908 __ Srl(TMP, TMP, 32 - ctz_imm);
1909 __ Addu(out, dividend, TMP);
1910 if (IsUint<16>(abs_imm - 1)) {
1911 __ Andi(out, out, abs_imm - 1);
1912 } else {
1913 __ Sll(out, out, 32 - ctz_imm);
1914 __ Srl(out, out, 32 - ctz_imm);
1915 }
1916 __ Subu(out, out, TMP);
1917 }
1918 } else {
1919 DCHECK_EQ(type, Primitive::kPrimLong);
1920 if (ctz_imm == 1) {
1921 // Fast path for modulo +/-2, which is very common.
1922 __ Dsra32(TMP, dividend, 31);
1923 __ Dsubu(out, dividend, TMP);
1924 __ Andi(out, out, 1);
1925 __ Daddu(out, out, TMP);
1926 } else {
1927 __ Dsra32(TMP, dividend, 31);
1928 if (ctz_imm > 32) {
1929 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1930 } else {
1931 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1932 }
1933 __ Daddu(out, dividend, TMP);
1934 if (IsUint<16>(abs_imm - 1)) {
1935 __ Andi(out, out, abs_imm - 1);
1936 } else {
1937 if (ctz_imm > 32) {
1938 __ Dsll(out, out, 64 - ctz_imm);
1939 __ Dsrl(out, out, 64 - ctz_imm);
1940 } else {
1941 __ Dsll32(out, out, 32 - ctz_imm);
1942 __ Dsrl32(out, out, 32 - ctz_imm);
1943 }
1944 }
1945 __ Dsubu(out, out, TMP);
1946 }
1947 }
1948 }
1949}
1950
1951void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1952 DCHECK(instruction->IsDiv() || instruction->IsRem());
1953
1954 LocationSummary* locations = instruction->GetLocations();
1955 Location second = locations->InAt(1);
1956 DCHECK(second.IsConstant());
1957
1958 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1959 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1960 int64_t imm = Int64FromConstant(second.GetConstant());
1961
1962 Primitive::Type type = instruction->GetResultType();
1963 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1964
1965 int64_t magic;
1966 int shift;
1967 CalculateMagicAndShiftForDivRem(imm,
1968 (type == Primitive::kPrimLong),
1969 &magic,
1970 &shift);
1971
1972 if (type == Primitive::kPrimInt) {
1973 __ LoadConst32(TMP, magic);
1974 __ MuhR6(TMP, dividend, TMP);
1975
1976 if (imm > 0 && magic < 0) {
1977 __ Addu(TMP, TMP, dividend);
1978 } else if (imm < 0 && magic > 0) {
1979 __ Subu(TMP, TMP, dividend);
1980 }
1981
1982 if (shift != 0) {
1983 __ Sra(TMP, TMP, shift);
1984 }
1985
1986 if (instruction->IsDiv()) {
1987 __ Sra(out, TMP, 31);
1988 __ Subu(out, TMP, out);
1989 } else {
1990 __ Sra(AT, TMP, 31);
1991 __ Subu(AT, TMP, AT);
1992 __ LoadConst32(TMP, imm);
1993 __ MulR6(TMP, AT, TMP);
1994 __ Subu(out, dividend, TMP);
1995 }
1996 } else {
1997 __ LoadConst64(TMP, magic);
1998 __ Dmuh(TMP, dividend, TMP);
1999
2000 if (imm > 0 && magic < 0) {
2001 __ Daddu(TMP, TMP, dividend);
2002 } else if (imm < 0 && magic > 0) {
2003 __ Dsubu(TMP, TMP, dividend);
2004 }
2005
2006 if (shift >= 32) {
2007 __ Dsra32(TMP, TMP, shift - 32);
2008 } else if (shift > 0) {
2009 __ Dsra(TMP, TMP, shift);
2010 }
2011
2012 if (instruction->IsDiv()) {
2013 __ Dsra32(out, TMP, 31);
2014 __ Dsubu(out, TMP, out);
2015 } else {
2016 __ Dsra32(AT, TMP, 31);
2017 __ Dsubu(AT, TMP, AT);
2018 __ LoadConst64(TMP, imm);
2019 __ Dmul(TMP, AT, TMP);
2020 __ Dsubu(out, dividend, TMP);
2021 }
2022 }
2023}
2024
2025void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2026 DCHECK(instruction->IsDiv() || instruction->IsRem());
2027 Primitive::Type type = instruction->GetResultType();
2028 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2029
2030 LocationSummary* locations = instruction->GetLocations();
2031 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2032 Location second = locations->InAt(1);
2033
2034 if (second.IsConstant()) {
2035 int64_t imm = Int64FromConstant(second.GetConstant());
2036 if (imm == 0) {
2037 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2038 } else if (imm == 1 || imm == -1) {
2039 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002040 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002041 DivRemByPowerOfTwo(instruction);
2042 } else {
2043 DCHECK(imm <= -2 || imm >= 2);
2044 GenerateDivRemWithAnyConstant(instruction);
2045 }
2046 } else {
2047 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2048 GpuRegister divisor = second.AsRegister<GpuRegister>();
2049 if (instruction->IsDiv()) {
2050 if (type == Primitive::kPrimInt)
2051 __ DivR6(out, dividend, divisor);
2052 else
2053 __ Ddiv(out, dividend, divisor);
2054 } else {
2055 if (type == Primitive::kPrimInt)
2056 __ ModR6(out, dividend, divisor);
2057 else
2058 __ Dmod(out, dividend, divisor);
2059 }
2060 }
2061}
2062
Alexey Frunze4dda3372015-06-01 18:31:49 -07002063void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2064 LocationSummary* locations =
2065 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2066 switch (div->GetResultType()) {
2067 case Primitive::kPrimInt:
2068 case Primitive::kPrimLong:
2069 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002070 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002071 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2072 break;
2073
2074 case Primitive::kPrimFloat:
2075 case Primitive::kPrimDouble:
2076 locations->SetInAt(0, Location::RequiresFpuRegister());
2077 locations->SetInAt(1, Location::RequiresFpuRegister());
2078 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2079 break;
2080
2081 default:
2082 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2083 }
2084}
2085
2086void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2087 Primitive::Type type = instruction->GetType();
2088 LocationSummary* locations = instruction->GetLocations();
2089
2090 switch (type) {
2091 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002092 case Primitive::kPrimLong:
2093 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002094 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002095 case Primitive::kPrimFloat:
2096 case Primitive::kPrimDouble: {
2097 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2098 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2099 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2100 if (type == Primitive::kPrimFloat)
2101 __ DivS(dst, lhs, rhs);
2102 else
2103 __ DivD(dst, lhs, rhs);
2104 break;
2105 }
2106 default:
2107 LOG(FATAL) << "Unexpected div type " << type;
2108 }
2109}
2110
2111void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002112 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002113 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002114}
2115
2116void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2117 SlowPathCodeMIPS64* slow_path =
2118 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2119 codegen_->AddSlowPath(slow_path);
2120 Location value = instruction->GetLocations()->InAt(0);
2121
2122 Primitive::Type type = instruction->GetType();
2123
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002124 if (!Primitive::IsIntegralType(type)) {
2125 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002126 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002127 }
2128
2129 if (value.IsConstant()) {
2130 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2131 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002132 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002133 } else {
2134 // A division by a non-null constant is valid. We don't need to perform
2135 // any check, so simply fall through.
2136 }
2137 } else {
2138 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2139 }
2140}
2141
2142void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2143 LocationSummary* locations =
2144 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2145 locations->SetOut(Location::ConstantLocation(constant));
2146}
2147
2148void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2149 // Will be generated at use site.
2150}
2151
2152void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2153 exit->SetLocations(nullptr);
2154}
2155
2156void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2157}
2158
2159void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2160 LocationSummary* locations =
2161 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2162 locations->SetOut(Location::ConstantLocation(constant));
2163}
2164
2165void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2166 // Will be generated at use site.
2167}
2168
David Brazdilfc6a86a2015-06-26 10:33:45 +00002169void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002170 DCHECK(!successor->IsExitBlock());
2171 HBasicBlock* block = got->GetBlock();
2172 HInstruction* previous = got->GetPrevious();
2173 HLoopInformation* info = block->GetLoopInformation();
2174
2175 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2176 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2177 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2178 return;
2179 }
2180 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2181 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2182 }
2183 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002184 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002185 }
2186}
2187
David Brazdilfc6a86a2015-06-26 10:33:45 +00002188void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2189 got->SetLocations(nullptr);
2190}
2191
2192void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2193 HandleGoto(got, got->GetSuccessor());
2194}
2195
2196void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2197 try_boundary->SetLocations(nullptr);
2198}
2199
2200void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2201 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2202 if (!successor->IsExitBlock()) {
2203 HandleGoto(try_boundary, successor);
2204 }
2205}
2206
Alexey Frunze299a9392015-12-08 16:08:02 -08002207void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2208 bool is64bit,
2209 LocationSummary* locations) {
2210 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2211 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2212 Location rhs_location = locations->InAt(1);
2213 GpuRegister rhs_reg = ZERO;
2214 int64_t rhs_imm = 0;
2215 bool use_imm = rhs_location.IsConstant();
2216 if (use_imm) {
2217 if (is64bit) {
2218 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2219 } else {
2220 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2221 }
2222 } else {
2223 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2224 }
2225 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2226
2227 switch (cond) {
2228 case kCondEQ:
2229 case kCondNE:
2230 if (use_imm && IsUint<16>(rhs_imm)) {
2231 __ Xori(dst, lhs, rhs_imm);
2232 } else {
2233 if (use_imm) {
2234 rhs_reg = TMP;
2235 __ LoadConst64(rhs_reg, rhs_imm);
2236 }
2237 __ Xor(dst, lhs, rhs_reg);
2238 }
2239 if (cond == kCondEQ) {
2240 __ Sltiu(dst, dst, 1);
2241 } else {
2242 __ Sltu(dst, ZERO, dst);
2243 }
2244 break;
2245
2246 case kCondLT:
2247 case kCondGE:
2248 if (use_imm && IsInt<16>(rhs_imm)) {
2249 __ Slti(dst, lhs, rhs_imm);
2250 } else {
2251 if (use_imm) {
2252 rhs_reg = TMP;
2253 __ LoadConst64(rhs_reg, rhs_imm);
2254 }
2255 __ Slt(dst, lhs, rhs_reg);
2256 }
2257 if (cond == kCondGE) {
2258 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2259 // only the slt instruction but no sge.
2260 __ Xori(dst, dst, 1);
2261 }
2262 break;
2263
2264 case kCondLE:
2265 case kCondGT:
2266 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2267 // Simulate lhs <= rhs via lhs < rhs + 1.
2268 __ Slti(dst, lhs, rhs_imm_plus_one);
2269 if (cond == kCondGT) {
2270 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2271 // only the slti instruction but no sgti.
2272 __ Xori(dst, dst, 1);
2273 }
2274 } else {
2275 if (use_imm) {
2276 rhs_reg = TMP;
2277 __ LoadConst64(rhs_reg, rhs_imm);
2278 }
2279 __ Slt(dst, rhs_reg, lhs);
2280 if (cond == kCondLE) {
2281 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2282 // only the slt instruction but no sle.
2283 __ Xori(dst, dst, 1);
2284 }
2285 }
2286 break;
2287
2288 case kCondB:
2289 case kCondAE:
2290 if (use_imm && IsInt<16>(rhs_imm)) {
2291 // Sltiu sign-extends its 16-bit immediate operand before
2292 // the comparison and thus lets us compare directly with
2293 // unsigned values in the ranges [0, 0x7fff] and
2294 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2295 __ Sltiu(dst, lhs, rhs_imm);
2296 } else {
2297 if (use_imm) {
2298 rhs_reg = TMP;
2299 __ LoadConst64(rhs_reg, rhs_imm);
2300 }
2301 __ Sltu(dst, lhs, rhs_reg);
2302 }
2303 if (cond == kCondAE) {
2304 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2305 // only the sltu instruction but no sgeu.
2306 __ Xori(dst, dst, 1);
2307 }
2308 break;
2309
2310 case kCondBE:
2311 case kCondA:
2312 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2313 // Simulate lhs <= rhs via lhs < rhs + 1.
2314 // Note that this only works if rhs + 1 does not overflow
2315 // to 0, hence the check above.
2316 // Sltiu sign-extends its 16-bit immediate operand before
2317 // the comparison and thus lets us compare directly with
2318 // unsigned values in the ranges [0, 0x7fff] and
2319 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2320 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2321 if (cond == kCondA) {
2322 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2323 // only the sltiu instruction but no sgtiu.
2324 __ Xori(dst, dst, 1);
2325 }
2326 } else {
2327 if (use_imm) {
2328 rhs_reg = TMP;
2329 __ LoadConst64(rhs_reg, rhs_imm);
2330 }
2331 __ Sltu(dst, rhs_reg, lhs);
2332 if (cond == kCondBE) {
2333 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2334 // only the sltu instruction but no sleu.
2335 __ Xori(dst, dst, 1);
2336 }
2337 }
2338 break;
2339 }
2340}
2341
2342void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2343 bool is64bit,
2344 LocationSummary* locations,
2345 Mips64Label* label) {
2346 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2347 Location rhs_location = locations->InAt(1);
2348 GpuRegister rhs_reg = ZERO;
2349 int64_t rhs_imm = 0;
2350 bool use_imm = rhs_location.IsConstant();
2351 if (use_imm) {
2352 if (is64bit) {
2353 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2354 } else {
2355 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2356 }
2357 } else {
2358 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2359 }
2360
2361 if (use_imm && rhs_imm == 0) {
2362 switch (cond) {
2363 case kCondEQ:
2364 case kCondBE: // <= 0 if zero
2365 __ Beqzc(lhs, label);
2366 break;
2367 case kCondNE:
2368 case kCondA: // > 0 if non-zero
2369 __ Bnezc(lhs, label);
2370 break;
2371 case kCondLT:
2372 __ Bltzc(lhs, label);
2373 break;
2374 case kCondGE:
2375 __ Bgezc(lhs, label);
2376 break;
2377 case kCondLE:
2378 __ Blezc(lhs, label);
2379 break;
2380 case kCondGT:
2381 __ Bgtzc(lhs, label);
2382 break;
2383 case kCondB: // always false
2384 break;
2385 case kCondAE: // always true
2386 __ Bc(label);
2387 break;
2388 }
2389 } else {
2390 if (use_imm) {
2391 rhs_reg = TMP;
2392 __ LoadConst64(rhs_reg, rhs_imm);
2393 }
2394 switch (cond) {
2395 case kCondEQ:
2396 __ Beqc(lhs, rhs_reg, label);
2397 break;
2398 case kCondNE:
2399 __ Bnec(lhs, rhs_reg, label);
2400 break;
2401 case kCondLT:
2402 __ Bltc(lhs, rhs_reg, label);
2403 break;
2404 case kCondGE:
2405 __ Bgec(lhs, rhs_reg, label);
2406 break;
2407 case kCondLE:
2408 __ Bgec(rhs_reg, lhs, label);
2409 break;
2410 case kCondGT:
2411 __ Bltc(rhs_reg, lhs, label);
2412 break;
2413 case kCondB:
2414 __ Bltuc(lhs, rhs_reg, label);
2415 break;
2416 case kCondAE:
2417 __ Bgeuc(lhs, rhs_reg, label);
2418 break;
2419 case kCondBE:
2420 __ Bgeuc(rhs_reg, lhs, label);
2421 break;
2422 case kCondA:
2423 __ Bltuc(rhs_reg, lhs, label);
2424 break;
2425 }
2426 }
2427}
2428
2429void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2430 bool gt_bias,
2431 Primitive::Type type,
2432 LocationSummary* locations,
2433 Mips64Label* label) {
2434 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2435 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2436 if (type == Primitive::kPrimFloat) {
2437 switch (cond) {
2438 case kCondEQ:
2439 __ CmpEqS(FTMP, lhs, rhs);
2440 __ Bc1nez(FTMP, label);
2441 break;
2442 case kCondNE:
2443 __ CmpEqS(FTMP, lhs, rhs);
2444 __ Bc1eqz(FTMP, label);
2445 break;
2446 case kCondLT:
2447 if (gt_bias) {
2448 __ CmpLtS(FTMP, lhs, rhs);
2449 } else {
2450 __ CmpUltS(FTMP, lhs, rhs);
2451 }
2452 __ Bc1nez(FTMP, label);
2453 break;
2454 case kCondLE:
2455 if (gt_bias) {
2456 __ CmpLeS(FTMP, lhs, rhs);
2457 } else {
2458 __ CmpUleS(FTMP, lhs, rhs);
2459 }
2460 __ Bc1nez(FTMP, label);
2461 break;
2462 case kCondGT:
2463 if (gt_bias) {
2464 __ CmpUltS(FTMP, rhs, lhs);
2465 } else {
2466 __ CmpLtS(FTMP, rhs, lhs);
2467 }
2468 __ Bc1nez(FTMP, label);
2469 break;
2470 case kCondGE:
2471 if (gt_bias) {
2472 __ CmpUleS(FTMP, rhs, lhs);
2473 } else {
2474 __ CmpLeS(FTMP, rhs, lhs);
2475 }
2476 __ Bc1nez(FTMP, label);
2477 break;
2478 default:
2479 LOG(FATAL) << "Unexpected non-floating-point condition";
2480 }
2481 } else {
2482 DCHECK_EQ(type, Primitive::kPrimDouble);
2483 switch (cond) {
2484 case kCondEQ:
2485 __ CmpEqD(FTMP, lhs, rhs);
2486 __ Bc1nez(FTMP, label);
2487 break;
2488 case kCondNE:
2489 __ CmpEqD(FTMP, lhs, rhs);
2490 __ Bc1eqz(FTMP, label);
2491 break;
2492 case kCondLT:
2493 if (gt_bias) {
2494 __ CmpLtD(FTMP, lhs, rhs);
2495 } else {
2496 __ CmpUltD(FTMP, lhs, rhs);
2497 }
2498 __ Bc1nez(FTMP, label);
2499 break;
2500 case kCondLE:
2501 if (gt_bias) {
2502 __ CmpLeD(FTMP, lhs, rhs);
2503 } else {
2504 __ CmpUleD(FTMP, lhs, rhs);
2505 }
2506 __ Bc1nez(FTMP, label);
2507 break;
2508 case kCondGT:
2509 if (gt_bias) {
2510 __ CmpUltD(FTMP, rhs, lhs);
2511 } else {
2512 __ CmpLtD(FTMP, rhs, lhs);
2513 }
2514 __ Bc1nez(FTMP, label);
2515 break;
2516 case kCondGE:
2517 if (gt_bias) {
2518 __ CmpUleD(FTMP, rhs, lhs);
2519 } else {
2520 __ CmpLeD(FTMP, rhs, lhs);
2521 }
2522 __ Bc1nez(FTMP, label);
2523 break;
2524 default:
2525 LOG(FATAL) << "Unexpected non-floating-point condition";
2526 }
2527 }
2528}
2529
Alexey Frunze4dda3372015-06-01 18:31:49 -07002530void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002531 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002532 Mips64Label* true_target,
2533 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002534 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002535
David Brazdil0debae72015-11-12 18:37:00 +00002536 if (true_target == nullptr && false_target == nullptr) {
2537 // Nothing to do. The code always falls through.
2538 return;
2539 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002540 // Constant condition, statically compared against "true" (integer value 1).
2541 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002542 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002543 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002544 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002545 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002546 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002547 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002548 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002549 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002550 }
David Brazdil0debae72015-11-12 18:37:00 +00002551 return;
2552 }
2553
2554 // The following code generates these patterns:
2555 // (1) true_target == nullptr && false_target != nullptr
2556 // - opposite condition true => branch to false_target
2557 // (2) true_target != nullptr && false_target == nullptr
2558 // - condition true => branch to true_target
2559 // (3) true_target != nullptr && false_target != nullptr
2560 // - condition true => branch to true_target
2561 // - branch to false_target
2562 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002563 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002564 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002565 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002566 if (true_target == nullptr) {
2567 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2568 } else {
2569 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2570 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002571 } else {
2572 // The condition instruction has not been materialized, use its inputs as
2573 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002574 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002575 Primitive::Type type = condition->InputAt(0)->GetType();
2576 LocationSummary* locations = cond->GetLocations();
2577 IfCondition if_cond = condition->GetCondition();
2578 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002579
David Brazdil0debae72015-11-12 18:37:00 +00002580 if (true_target == nullptr) {
2581 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002582 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002583 }
2584
Alexey Frunze299a9392015-12-08 16:08:02 -08002585 switch (type) {
2586 default:
2587 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2588 break;
2589 case Primitive::kPrimLong:
2590 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2591 break;
2592 case Primitive::kPrimFloat:
2593 case Primitive::kPrimDouble:
2594 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2595 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002596 }
2597 }
David Brazdil0debae72015-11-12 18:37:00 +00002598
2599 // If neither branch falls through (case 3), the conditional branch to `true_target`
2600 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2601 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002602 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002603 }
2604}
2605
2606void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2607 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002608 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002609 locations->SetInAt(0, Location::RequiresRegister());
2610 }
2611}
2612
2613void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002614 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2615 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002616 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002617 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002618 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002619 nullptr : codegen_->GetLabelOf(false_successor);
2620 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002621}
2622
2623void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2624 LocationSummary* locations = new (GetGraph()->GetArena())
2625 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002626 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002627 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002628 locations->SetInAt(0, Location::RequiresRegister());
2629 }
2630}
2631
2632void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002633 SlowPathCodeMIPS64* slow_path =
2634 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002635 GenerateTestAndBranch(deoptimize,
2636 /* condition_input_index */ 0,
2637 slow_path->GetEntryLabel(),
2638 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002639}
2640
David Brazdil74eb1b22015-12-14 11:44:01 +00002641void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2643 if (Primitive::IsFloatingPointType(select->GetType())) {
2644 locations->SetInAt(0, Location::RequiresFpuRegister());
2645 locations->SetInAt(1, Location::RequiresFpuRegister());
2646 } else {
2647 locations->SetInAt(0, Location::RequiresRegister());
2648 locations->SetInAt(1, Location::RequiresRegister());
2649 }
2650 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2651 locations->SetInAt(2, Location::RequiresRegister());
2652 }
2653 locations->SetOut(Location::SameAsFirstInput());
2654}
2655
2656void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2657 LocationSummary* locations = select->GetLocations();
2658 Mips64Label false_target;
2659 GenerateTestAndBranch(select,
2660 /* condition_input_index */ 2,
2661 /* true_target */ nullptr,
2662 &false_target);
2663 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2664 __ Bind(&false_target);
2665}
2666
David Srbecky0cf44932015-12-09 14:09:59 +00002667void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2668 new (GetGraph()->GetArena()) LocationSummary(info);
2669}
2670
David Srbeckyd28f4a02016-03-14 17:14:24 +00002671void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2672 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002673}
2674
2675void CodeGeneratorMIPS64::GenerateNop() {
2676 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002677}
2678
Alexey Frunze4dda3372015-06-01 18:31:49 -07002679void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2680 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2681 LocationSummary* locations =
2682 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2683 locations->SetInAt(0, Location::RequiresRegister());
2684 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2685 locations->SetOut(Location::RequiresFpuRegister());
2686 } else {
2687 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2688 }
2689}
2690
2691void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2692 const FieldInfo& field_info) {
2693 Primitive::Type type = field_info.GetFieldType();
2694 LocationSummary* locations = instruction->GetLocations();
2695 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2696 LoadOperandType load_type = kLoadUnsignedByte;
2697 switch (type) {
2698 case Primitive::kPrimBoolean:
2699 load_type = kLoadUnsignedByte;
2700 break;
2701 case Primitive::kPrimByte:
2702 load_type = kLoadSignedByte;
2703 break;
2704 case Primitive::kPrimShort:
2705 load_type = kLoadSignedHalfword;
2706 break;
2707 case Primitive::kPrimChar:
2708 load_type = kLoadUnsignedHalfword;
2709 break;
2710 case Primitive::kPrimInt:
2711 case Primitive::kPrimFloat:
2712 load_type = kLoadWord;
2713 break;
2714 case Primitive::kPrimLong:
2715 case Primitive::kPrimDouble:
2716 load_type = kLoadDoubleword;
2717 break;
2718 case Primitive::kPrimNot:
2719 load_type = kLoadUnsignedWord;
2720 break;
2721 case Primitive::kPrimVoid:
2722 LOG(FATAL) << "Unreachable type " << type;
2723 UNREACHABLE();
2724 }
2725 if (!Primitive::IsFloatingPointType(type)) {
2726 DCHECK(locations->Out().IsRegister());
2727 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2728 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2729 } else {
2730 DCHECK(locations->Out().IsFpuRegister());
2731 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2732 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2733 }
2734
2735 codegen_->MaybeRecordImplicitNullCheck(instruction);
2736 // TODO: memory barrier?
2737}
2738
2739void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2740 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2741 LocationSummary* locations =
2742 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2743 locations->SetInAt(0, Location::RequiresRegister());
2744 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2745 locations->SetInAt(1, Location::RequiresFpuRegister());
2746 } else {
2747 locations->SetInAt(1, Location::RequiresRegister());
2748 }
2749}
2750
2751void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002752 const FieldInfo& field_info,
2753 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002754 Primitive::Type type = field_info.GetFieldType();
2755 LocationSummary* locations = instruction->GetLocations();
2756 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2757 StoreOperandType store_type = kStoreByte;
2758 switch (type) {
2759 case Primitive::kPrimBoolean:
2760 case Primitive::kPrimByte:
2761 store_type = kStoreByte;
2762 break;
2763 case Primitive::kPrimShort:
2764 case Primitive::kPrimChar:
2765 store_type = kStoreHalfword;
2766 break;
2767 case Primitive::kPrimInt:
2768 case Primitive::kPrimFloat:
2769 case Primitive::kPrimNot:
2770 store_type = kStoreWord;
2771 break;
2772 case Primitive::kPrimLong:
2773 case Primitive::kPrimDouble:
2774 store_type = kStoreDoubleword;
2775 break;
2776 case Primitive::kPrimVoid:
2777 LOG(FATAL) << "Unreachable type " << type;
2778 UNREACHABLE();
2779 }
2780 if (!Primitive::IsFloatingPointType(type)) {
2781 DCHECK(locations->InAt(1).IsRegister());
2782 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2783 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2784 } else {
2785 DCHECK(locations->InAt(1).IsFpuRegister());
2786 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2787 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2788 }
2789
2790 codegen_->MaybeRecordImplicitNullCheck(instruction);
2791 // TODO: memory barriers?
2792 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2793 DCHECK(locations->InAt(1).IsRegister());
2794 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002795 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002796 }
2797}
2798
2799void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2800 HandleFieldGet(instruction, instruction->GetFieldInfo());
2801}
2802
2803void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2804 HandleFieldGet(instruction, instruction->GetFieldInfo());
2805}
2806
2807void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2808 HandleFieldSet(instruction, instruction->GetFieldInfo());
2809}
2810
2811void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002812 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002813}
2814
2815void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2816 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002817 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002818 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2819 locations->SetInAt(0, Location::RequiresRegister());
2820 locations->SetInAt(1, Location::RequiresRegister());
2821 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002822 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002823 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2824}
2825
2826void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2827 LocationSummary* locations = instruction->GetLocations();
2828 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2829 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2830 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2831
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002832 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002833
2834 // Return 0 if `obj` is null.
2835 // TODO: Avoid this check if we know `obj` is not null.
2836 __ Move(out, ZERO);
2837 __ Beqzc(obj, &done);
2838
2839 // Compare the class of `obj` with `cls`.
2840 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002841 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002842 // Classes must be equal for the instanceof to succeed.
2843 __ Xor(out, out, cls);
2844 __ Sltiu(out, out, 1);
2845 } else {
2846 // If the classes are not equal, we go into a slow path.
2847 DCHECK(locations->OnlyCallsOnSlowPath());
2848 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002849 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002850 codegen_->AddSlowPath(slow_path);
2851 __ Bnec(out, cls, slow_path->GetEntryLabel());
2852 __ LoadConst32(out, 1);
2853 __ Bind(slow_path->GetExitLabel());
2854 }
2855
2856 __ Bind(&done);
2857}
2858
2859void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2860 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2861 locations->SetOut(Location::ConstantLocation(constant));
2862}
2863
2864void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2865 // Will be generated at use site.
2866}
2867
2868void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2869 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2870 locations->SetOut(Location::ConstantLocation(constant));
2871}
2872
2873void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2874 // Will be generated at use site.
2875}
2876
Calin Juravle175dc732015-08-25 15:42:32 +01002877void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2878 // The trampoline uses the same calling convention as dex calling conventions,
2879 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2880 // the method_idx.
2881 HandleInvoke(invoke);
2882}
2883
2884void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2885 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2886}
2887
Alexey Frunze4dda3372015-06-01 18:31:49 -07002888void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2889 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2890 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2891}
2892
2893void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2894 HandleInvoke(invoke);
2895 // The register T0 is required to be used for the hidden argument in
2896 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2897 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2898}
2899
2900void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2901 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2902 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002903 Location receiver = invoke->GetLocations()->InAt(0);
2904 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07002905 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002906
2907 // Set the hidden argument.
2908 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2909 invoke->GetDexMethodIndex());
2910
2911 // temp = object->GetClass();
2912 if (receiver.IsStackSlot()) {
2913 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2914 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2915 } else {
2916 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2917 }
2918 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002919 __ LoadFromOffset(kLoadDoubleword, temp, temp,
2920 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
2921 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002922 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002923 // temp = temp->GetImtEntryAt(method_offset);
2924 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2925 // T9 = temp->GetEntryPoint();
2926 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2927 // T9();
2928 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002929 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002930 DCHECK(!codegen_->IsLeafMethod());
2931 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2932}
2933
2934void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002935 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2936 if (intrinsic.TryDispatch(invoke)) {
2937 return;
2938 }
2939
Alexey Frunze4dda3372015-06-01 18:31:49 -07002940 HandleInvoke(invoke);
2941}
2942
2943void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002944 // Explicit clinit checks triggered by static invokes must have been pruned by
2945 // art::PrepareForRegisterAllocation.
2946 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002947
Chris Larsen3039e382015-08-26 07:54:08 -07002948 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2949 if (intrinsic.TryDispatch(invoke)) {
2950 return;
2951 }
2952
Alexey Frunze4dda3372015-06-01 18:31:49 -07002953 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002954}
2955
Chris Larsen3039e382015-08-26 07:54:08 -07002956static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002957 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002958 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2959 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002960 return true;
2961 }
2962 return false;
2963}
2964
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002965HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
2966 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
2967 // TODO: Implement other kinds.
2968 return HLoadString::LoadKind::kDexCacheViaMethod;
2969}
2970
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002971HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
2972 HLoadClass::LoadKind desired_class_load_kind) {
2973 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
2974 // TODO: Implement other kinds.
2975 return HLoadClass::LoadKind::kDexCacheViaMethod;
2976}
2977
Vladimir Markodc151b22015-10-15 18:02:30 +01002978HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
2979 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01002980 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01002981 switch (desired_dispatch_info.method_load_kind) {
2982 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2983 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2984 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
2985 return HInvokeStaticOrDirect::DispatchInfo {
2986 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
2987 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2988 0u,
2989 0u
2990 };
2991 default:
2992 break;
2993 }
2994 switch (desired_dispatch_info.code_ptr_location) {
2995 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2996 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
2997 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
2998 return HInvokeStaticOrDirect::DispatchInfo {
2999 desired_dispatch_info.method_load_kind,
3000 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3001 desired_dispatch_info.method_load_data,
3002 0u
3003 };
3004 default:
3005 return desired_dispatch_info;
3006 }
3007}
3008
Alexey Frunze4dda3372015-06-01 18:31:49 -07003009void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3010 // All registers are assumed to be correctly set up per the calling convention.
3011
Vladimir Marko58155012015-08-19 12:49:41 +00003012 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3013 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003014 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003015 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003016 uint32_t offset =
3017 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003018 __ LoadFromOffset(kLoadDoubleword,
3019 temp.AsRegister<GpuRegister>(),
3020 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003021 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003022 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003023 }
Vladimir Marko58155012015-08-19 12:49:41 +00003024 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003025 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003026 break;
3027 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3028 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3029 break;
3030 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003031 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003032 // TODO: Implement these types.
3033 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3034 LOG(FATAL) << "Unsupported";
3035 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003036 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003037 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003038 GpuRegister reg = temp.AsRegister<GpuRegister>();
3039 GpuRegister method_reg;
3040 if (current_method.IsRegister()) {
3041 method_reg = current_method.AsRegister<GpuRegister>();
3042 } else {
3043 // TODO: use the appropriate DCHECK() here if possible.
3044 // DCHECK(invoke->GetLocations()->Intrinsified());
3045 DCHECK(!current_method.IsValid());
3046 method_reg = reg;
3047 __ Ld(reg, SP, kCurrentMethodStackOffset);
3048 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003049
Vladimir Marko58155012015-08-19 12:49:41 +00003050 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003051 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003052 reg,
3053 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003054 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003055 // temp = temp[index_in_cache];
3056 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3057 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003058 __ LoadFromOffset(kLoadDoubleword,
3059 reg,
3060 reg,
3061 CodeGenerator::GetCachePointerOffset(index_in_cache));
3062 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003063 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003064 }
3065
Vladimir Marko58155012015-08-19 12:49:41 +00003066 switch (invoke->GetCodePtrLocation()) {
3067 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003068 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003069 break;
3070 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3071 // LR = invoke->GetDirectCodePtr();
3072 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3073 // LR()
3074 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003075 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003076 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003077 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003078 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3079 // TODO: Implement these types.
3080 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3081 LOG(FATAL) << "Unsupported";
3082 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003083 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3084 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3085 __ LoadFromOffset(kLoadDoubleword,
3086 T9,
3087 callee_method.AsRegister<GpuRegister>(),
3088 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003089 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003090 // T9()
3091 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003092 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003093 break;
3094 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003095 DCHECK(!IsLeafMethod());
3096}
3097
3098void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003099 // Explicit clinit checks triggered by static invokes must have been pruned by
3100 // art::PrepareForRegisterAllocation.
3101 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003102
3103 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3104 return;
3105 }
3106
3107 LocationSummary* locations = invoke->GetLocations();
3108 codegen_->GenerateStaticOrDirectCall(invoke,
3109 locations->HasTemps()
3110 ? locations->GetTemp(0)
3111 : Location::NoLocation());
3112 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3113}
3114
Alexey Frunze53afca12015-11-05 16:34:23 -08003115void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003116 // Use the calling convention instead of the location of the receiver, as
3117 // intrinsics may have put the receiver in a different register. In the intrinsics
3118 // slow path, the arguments have been moved to the right place, so here we are
3119 // guaranteed that the receiver is the first register of the calling convention.
3120 InvokeDexCallingConvention calling_convention;
3121 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3122
Alexey Frunze53afca12015-11-05 16:34:23 -08003123 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003124 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3125 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3126 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003127 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003128
3129 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003130 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003131 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003132 // temp = temp->GetMethodAt(method_offset);
3133 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3134 // T9 = temp->GetEntryPoint();
3135 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3136 // T9();
3137 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003138 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003139}
3140
3141void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3142 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3143 return;
3144 }
3145
3146 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003147 DCHECK(!codegen_->IsLeafMethod());
3148 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3149}
3150
3151void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003152 InvokeRuntimeCallingConvention calling_convention;
3153 CodeGenerator::CreateLoadClassLocationSummary(
3154 cls,
3155 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003156 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003157}
3158
3159void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3160 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003161 if (cls->NeedsAccessCheck()) {
3162 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +01003163 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003164 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003165 return;
3166 }
3167
3168 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3169 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3170 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003171 DCHECK(!cls->CanCallRuntime());
3172 DCHECK(!cls->MustGenerateClinitCheck());
3173 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3174 ArtMethod::DeclaringClassOffset().Int32Value());
3175 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003176 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3177 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003178 __ LoadFromOffset(
3179 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003180 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003181 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3182 DCHECK(cls->CanCallRuntime());
3183 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3184 cls,
3185 cls,
3186 cls->GetDexPc(),
3187 cls->MustGenerateClinitCheck());
3188 codegen_->AddSlowPath(slow_path);
3189 if (!cls->IsInDexCache()) {
3190 __ Beqzc(out, slow_path->GetEntryLabel());
3191 }
3192 if (cls->MustGenerateClinitCheck()) {
3193 GenerateClassInitializationCheck(slow_path, out);
3194 } else {
3195 __ Bind(slow_path->GetExitLabel());
3196 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003197 }
3198 }
3199}
3200
David Brazdilcb1c0552015-08-04 16:22:25 +01003201static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003202 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003203}
3204
Alexey Frunze4dda3372015-06-01 18:31:49 -07003205void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3206 LocationSummary* locations =
3207 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3208 locations->SetOut(Location::RequiresRegister());
3209}
3210
3211void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3212 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003213 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3214}
3215
3216void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3217 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3218}
3219
3220void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3221 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003222}
3223
Alexey Frunze4dda3372015-06-01 18:31:49 -07003224void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003225 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3226 ? LocationSummary::kCallOnSlowPath
3227 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003228 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003229 locations->SetInAt(0, Location::RequiresRegister());
3230 locations->SetOut(Location::RequiresRegister());
3231}
3232
3233void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003234 // TODO: Re-add the compiler code to do string dex cache lookup again.
3235 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3236 codegen_->AddSlowPath(slow_path);
3237 __ Bc(slow_path->GetEntryLabel());
3238 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003239}
3240
Alexey Frunze4dda3372015-06-01 18:31:49 -07003241void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3242 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3243 locations->SetOut(Location::ConstantLocation(constant));
3244}
3245
3246void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3247 // Will be generated at use site.
3248}
3249
3250void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3251 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003253 InvokeRuntimeCallingConvention calling_convention;
3254 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3255}
3256
3257void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003258 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003259 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003260 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003261 if (instruction->IsEnter()) {
3262 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3263 } else {
3264 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3265 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003266}
3267
3268void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3269 LocationSummary* locations =
3270 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3271 switch (mul->GetResultType()) {
3272 case Primitive::kPrimInt:
3273 case Primitive::kPrimLong:
3274 locations->SetInAt(0, Location::RequiresRegister());
3275 locations->SetInAt(1, Location::RequiresRegister());
3276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3277 break;
3278
3279 case Primitive::kPrimFloat:
3280 case Primitive::kPrimDouble:
3281 locations->SetInAt(0, Location::RequiresFpuRegister());
3282 locations->SetInAt(1, Location::RequiresFpuRegister());
3283 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3284 break;
3285
3286 default:
3287 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3288 }
3289}
3290
3291void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3292 Primitive::Type type = instruction->GetType();
3293 LocationSummary* locations = instruction->GetLocations();
3294
3295 switch (type) {
3296 case Primitive::kPrimInt:
3297 case Primitive::kPrimLong: {
3298 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3299 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3300 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3301 if (type == Primitive::kPrimInt)
3302 __ MulR6(dst, lhs, rhs);
3303 else
3304 __ Dmul(dst, lhs, rhs);
3305 break;
3306 }
3307 case Primitive::kPrimFloat:
3308 case Primitive::kPrimDouble: {
3309 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3310 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3311 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3312 if (type == Primitive::kPrimFloat)
3313 __ MulS(dst, lhs, rhs);
3314 else
3315 __ MulD(dst, lhs, rhs);
3316 break;
3317 }
3318 default:
3319 LOG(FATAL) << "Unexpected mul type " << type;
3320 }
3321}
3322
3323void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3324 LocationSummary* locations =
3325 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3326 switch (neg->GetResultType()) {
3327 case Primitive::kPrimInt:
3328 case Primitive::kPrimLong:
3329 locations->SetInAt(0, Location::RequiresRegister());
3330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3331 break;
3332
3333 case Primitive::kPrimFloat:
3334 case Primitive::kPrimDouble:
3335 locations->SetInAt(0, Location::RequiresFpuRegister());
3336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3337 break;
3338
3339 default:
3340 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3341 }
3342}
3343
3344void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3345 Primitive::Type type = instruction->GetType();
3346 LocationSummary* locations = instruction->GetLocations();
3347
3348 switch (type) {
3349 case Primitive::kPrimInt:
3350 case Primitive::kPrimLong: {
3351 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3352 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3353 if (type == Primitive::kPrimInt)
3354 __ Subu(dst, ZERO, src);
3355 else
3356 __ Dsubu(dst, ZERO, src);
3357 break;
3358 }
3359 case Primitive::kPrimFloat:
3360 case Primitive::kPrimDouble: {
3361 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3362 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3363 if (type == Primitive::kPrimFloat)
3364 __ NegS(dst, src);
3365 else
3366 __ NegD(dst, src);
3367 break;
3368 }
3369 default:
3370 LOG(FATAL) << "Unexpected neg type " << type;
3371 }
3372}
3373
3374void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3375 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003377 InvokeRuntimeCallingConvention calling_convention;
3378 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3379 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3380 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3381 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3382}
3383
3384void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3385 LocationSummary* locations = instruction->GetLocations();
3386 // Move an uint16_t value to a register.
3387 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +01003388 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003389 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3390}
3391
3392void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3393 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003394 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003395 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003396 if (instruction->IsStringAlloc()) {
3397 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3398 } else {
3399 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3400 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3401 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003402 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3403}
3404
3405void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003406 if (instruction->IsStringAlloc()) {
3407 // String is allocated through StringFactory. Call NewEmptyString entry point.
3408 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003409 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003410 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003411 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3412 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3413 __ Jalr(T9);
3414 __ Nop();
3415 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3416 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003417 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003418 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3419 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003420}
3421
3422void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3423 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3424 locations->SetInAt(0, Location::RequiresRegister());
3425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3426}
3427
3428void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3429 Primitive::Type type = instruction->GetType();
3430 LocationSummary* locations = instruction->GetLocations();
3431
3432 switch (type) {
3433 case Primitive::kPrimInt:
3434 case Primitive::kPrimLong: {
3435 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3436 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3437 __ Nor(dst, src, ZERO);
3438 break;
3439 }
3440
3441 default:
3442 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3443 }
3444}
3445
3446void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3447 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3448 locations->SetInAt(0, Location::RequiresRegister());
3449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3450}
3451
3452void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3453 LocationSummary* locations = instruction->GetLocations();
3454 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3455 locations->InAt(0).AsRegister<GpuRegister>(),
3456 1);
3457}
3458
3459void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003460 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
3461 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003462}
3463
Calin Juravle2ae48182016-03-16 14:05:09 +00003464void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3465 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003466 return;
3467 }
3468 Location obj = instruction->GetLocations()->InAt(0);
3469
3470 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003471 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003472}
3473
Calin Juravle2ae48182016-03-16 14:05:09 +00003474void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003475 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003476 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003477
3478 Location obj = instruction->GetLocations()->InAt(0);
3479
3480 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3481}
3482
3483void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003484 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003485}
3486
3487void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3488 HandleBinaryOp(instruction);
3489}
3490
3491void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3492 HandleBinaryOp(instruction);
3493}
3494
3495void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3496 LOG(FATAL) << "Unreachable";
3497}
3498
3499void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3500 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3501}
3502
3503void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3504 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3505 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3506 if (location.IsStackSlot()) {
3507 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3508 } else if (location.IsDoubleStackSlot()) {
3509 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3510 }
3511 locations->SetOut(location);
3512}
3513
3514void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3515 ATTRIBUTE_UNUSED) {
3516 // Nothing to do, the parameter is already at its location.
3517}
3518
3519void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3520 LocationSummary* locations =
3521 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3522 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3523}
3524
3525void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3526 ATTRIBUTE_UNUSED) {
3527 // Nothing to do, the method is already at its location.
3528}
3529
3530void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3531 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003532 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003533 locations->SetInAt(i, Location::Any());
3534 }
3535 locations->SetOut(Location::Any());
3536}
3537
3538void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3539 LOG(FATAL) << "Unreachable";
3540}
3541
3542void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3543 Primitive::Type type = rem->GetResultType();
3544 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003545 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3546 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3548
3549 switch (type) {
3550 case Primitive::kPrimInt:
3551 case Primitive::kPrimLong:
3552 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003553 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003554 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3555 break;
3556
3557 case Primitive::kPrimFloat:
3558 case Primitive::kPrimDouble: {
3559 InvokeRuntimeCallingConvention calling_convention;
3560 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3561 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3562 locations->SetOut(calling_convention.GetReturnLocation(type));
3563 break;
3564 }
3565
3566 default:
3567 LOG(FATAL) << "Unexpected rem type " << type;
3568 }
3569}
3570
3571void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3572 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003573
3574 switch (type) {
3575 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003576 case Primitive::kPrimLong:
3577 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003578 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003579
3580 case Primitive::kPrimFloat:
3581 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01003582 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
3583 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003584 if (type == Primitive::kPrimFloat) {
3585 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3586 } else {
3587 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3588 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003589 break;
3590 }
3591 default:
3592 LOG(FATAL) << "Unexpected rem type " << type;
3593 }
3594}
3595
3596void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3597 memory_barrier->SetLocations(nullptr);
3598}
3599
3600void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3601 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3602}
3603
3604void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3605 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3606 Primitive::Type return_type = ret->InputAt(0)->GetType();
3607 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3608}
3609
3610void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3611 codegen_->GenerateFrameExit();
3612}
3613
3614void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3615 ret->SetLocations(nullptr);
3616}
3617
3618void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3619 codegen_->GenerateFrameExit();
3620}
3621
Alexey Frunze92d90602015-12-18 18:16:36 -08003622void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3623 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003624}
3625
Alexey Frunze92d90602015-12-18 18:16:36 -08003626void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3627 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003628}
3629
Alexey Frunze4dda3372015-06-01 18:31:49 -07003630void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3631 HandleShift(shl);
3632}
3633
3634void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3635 HandleShift(shl);
3636}
3637
3638void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3639 HandleShift(shr);
3640}
3641
3642void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3643 HandleShift(shr);
3644}
3645
Alexey Frunze4dda3372015-06-01 18:31:49 -07003646void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3647 HandleBinaryOp(instruction);
3648}
3649
3650void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3651 HandleBinaryOp(instruction);
3652}
3653
3654void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3655 HandleFieldGet(instruction, instruction->GetFieldInfo());
3656}
3657
3658void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3659 HandleFieldGet(instruction, instruction->GetFieldInfo());
3660}
3661
3662void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3663 HandleFieldSet(instruction, instruction->GetFieldInfo());
3664}
3665
3666void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003667 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003668}
3669
Calin Juravlee460d1d2015-09-29 04:52:17 +01003670void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3671 HUnresolvedInstanceFieldGet* instruction) {
3672 FieldAccessCallingConventionMIPS64 calling_convention;
3673 codegen_->CreateUnresolvedFieldLocationSummary(
3674 instruction, instruction->GetFieldType(), calling_convention);
3675}
3676
3677void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3678 HUnresolvedInstanceFieldGet* instruction) {
3679 FieldAccessCallingConventionMIPS64 calling_convention;
3680 codegen_->GenerateUnresolvedFieldAccess(instruction,
3681 instruction->GetFieldType(),
3682 instruction->GetFieldIndex(),
3683 instruction->GetDexPc(),
3684 calling_convention);
3685}
3686
3687void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3688 HUnresolvedInstanceFieldSet* instruction) {
3689 FieldAccessCallingConventionMIPS64 calling_convention;
3690 codegen_->CreateUnresolvedFieldLocationSummary(
3691 instruction, instruction->GetFieldType(), calling_convention);
3692}
3693
3694void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3695 HUnresolvedInstanceFieldSet* instruction) {
3696 FieldAccessCallingConventionMIPS64 calling_convention;
3697 codegen_->GenerateUnresolvedFieldAccess(instruction,
3698 instruction->GetFieldType(),
3699 instruction->GetFieldIndex(),
3700 instruction->GetDexPc(),
3701 calling_convention);
3702}
3703
3704void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3705 HUnresolvedStaticFieldGet* instruction) {
3706 FieldAccessCallingConventionMIPS64 calling_convention;
3707 codegen_->CreateUnresolvedFieldLocationSummary(
3708 instruction, instruction->GetFieldType(), calling_convention);
3709}
3710
3711void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3712 HUnresolvedStaticFieldGet* instruction) {
3713 FieldAccessCallingConventionMIPS64 calling_convention;
3714 codegen_->GenerateUnresolvedFieldAccess(instruction,
3715 instruction->GetFieldType(),
3716 instruction->GetFieldIndex(),
3717 instruction->GetDexPc(),
3718 calling_convention);
3719}
3720
3721void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3722 HUnresolvedStaticFieldSet* instruction) {
3723 FieldAccessCallingConventionMIPS64 calling_convention;
3724 codegen_->CreateUnresolvedFieldLocationSummary(
3725 instruction, instruction->GetFieldType(), calling_convention);
3726}
3727
3728void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3729 HUnresolvedStaticFieldSet* instruction) {
3730 FieldAccessCallingConventionMIPS64 calling_convention;
3731 codegen_->GenerateUnresolvedFieldAccess(instruction,
3732 instruction->GetFieldType(),
3733 instruction->GetFieldIndex(),
3734 instruction->GetDexPc(),
3735 calling_convention);
3736}
3737
Alexey Frunze4dda3372015-06-01 18:31:49 -07003738void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01003739 LocationSummary* locations =
3740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003741 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003742}
3743
3744void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3745 HBasicBlock* block = instruction->GetBlock();
3746 if (block->GetLoopInformation() != nullptr) {
3747 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3748 // The back edge will generate the suspend check.
3749 return;
3750 }
3751 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3752 // The goto will generate the suspend check.
3753 return;
3754 }
3755 GenerateSuspendCheck(instruction, nullptr);
3756}
3757
Alexey Frunze4dda3372015-06-01 18:31:49 -07003758void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3759 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003760 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003761 InvokeRuntimeCallingConvention calling_convention;
3762 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3763}
3764
3765void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003766 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003767 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3768}
3769
3770void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3771 Primitive::Type input_type = conversion->GetInputType();
3772 Primitive::Type result_type = conversion->GetResultType();
3773 DCHECK_NE(input_type, result_type);
3774
3775 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3776 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3777 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3778 }
3779
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003780 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3781
3782 if (Primitive::IsFloatingPointType(input_type)) {
3783 locations->SetInAt(0, Location::RequiresFpuRegister());
3784 } else {
3785 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003786 }
3787
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003788 if (Primitive::IsFloatingPointType(result_type)) {
3789 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003790 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003791 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003792 }
3793}
3794
3795void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3796 LocationSummary* locations = conversion->GetLocations();
3797 Primitive::Type result_type = conversion->GetResultType();
3798 Primitive::Type input_type = conversion->GetInputType();
3799
3800 DCHECK_NE(input_type, result_type);
3801
3802 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3803 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3804 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3805
3806 switch (result_type) {
3807 case Primitive::kPrimChar:
3808 __ Andi(dst, src, 0xFFFF);
3809 break;
3810 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003811 if (input_type == Primitive::kPrimLong) {
3812 // Type conversion from long to types narrower than int is a result of code
3813 // transformations. To avoid unpredictable results for SEB and SEH, we first
3814 // need to sign-extend the low 32-bit value into bits 32 through 63.
3815 __ Sll(dst, src, 0);
3816 __ Seb(dst, dst);
3817 } else {
3818 __ Seb(dst, src);
3819 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003820 break;
3821 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003822 if (input_type == Primitive::kPrimLong) {
3823 // Type conversion from long to types narrower than int is a result of code
3824 // transformations. To avoid unpredictable results for SEB and SEH, we first
3825 // need to sign-extend the low 32-bit value into bits 32 through 63.
3826 __ Sll(dst, src, 0);
3827 __ Seh(dst, dst);
3828 } else {
3829 __ Seh(dst, src);
3830 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003831 break;
3832 case Primitive::kPrimInt:
3833 case Primitive::kPrimLong:
3834 // Sign-extend 32-bit int into bits 32 through 63 for
3835 // int-to-long and long-to-int conversions
3836 __ Sll(dst, src, 0);
3837 break;
3838
3839 default:
3840 LOG(FATAL) << "Unexpected type conversion from " << input_type
3841 << " to " << result_type;
3842 }
3843 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003844 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3845 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3846 if (input_type == Primitive::kPrimLong) {
3847 __ Dmtc1(src, FTMP);
3848 if (result_type == Primitive::kPrimFloat) {
3849 __ Cvtsl(dst, FTMP);
3850 } else {
3851 __ Cvtdl(dst, FTMP);
3852 }
3853 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003854 __ Mtc1(src, FTMP);
3855 if (result_type == Primitive::kPrimFloat) {
3856 __ Cvtsw(dst, FTMP);
3857 } else {
3858 __ Cvtdw(dst, FTMP);
3859 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003860 }
3861 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3862 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003863 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3864 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3865 Mips64Label truncate;
3866 Mips64Label done;
3867
3868 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3869 // value when the input is either a NaN or is outside of the range of the output type
3870 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3871 // the same result.
3872 //
3873 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3874 // value of the output type if the input is outside of the range after the truncation or
3875 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3876 // results. This matches the desired float/double-to-int/long conversion exactly.
3877 //
3878 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3879 //
3880 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3881 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3882 // even though it must be NAN2008=1 on R6.
3883 //
3884 // The code takes care of the different behaviors by first comparing the input to the
3885 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3886 // If the input is greater than or equal to the minimum, it procedes to the truncate
3887 // instruction, which will handle such an input the same way irrespective of NAN2008.
3888 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3889 // in order to return either zero or the minimum value.
3890 //
3891 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3892 // truncate instruction for MIPS64R6.
3893 if (input_type == Primitive::kPrimFloat) {
3894 uint32_t min_val = (result_type == Primitive::kPrimLong)
3895 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3896 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3897 __ LoadConst32(TMP, min_val);
3898 __ Mtc1(TMP, FTMP);
3899 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003900 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003901 uint64_t min_val = (result_type == Primitive::kPrimLong)
3902 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3903 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3904 __ LoadConst64(TMP, min_val);
3905 __ Dmtc1(TMP, FTMP);
3906 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003907 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003908
3909 __ Bc1nez(FTMP, &truncate);
3910
3911 if (input_type == Primitive::kPrimFloat) {
3912 __ CmpEqS(FTMP, src, src);
3913 } else {
3914 __ CmpEqD(FTMP, src, src);
3915 }
3916 if (result_type == Primitive::kPrimLong) {
3917 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3918 } else {
3919 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3920 }
3921 __ Mfc1(TMP, FTMP);
3922 __ And(dst, dst, TMP);
3923
3924 __ Bc(&done);
3925
3926 __ Bind(&truncate);
3927
3928 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003929 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003930 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003931 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003932 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003933 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003934 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003935 } else {
3936 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003937 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003938 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003939 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003940 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003941 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003942 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003943
3944 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003945 } else if (Primitive::IsFloatingPointType(result_type) &&
3946 Primitive::IsFloatingPointType(input_type)) {
3947 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3948 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3949 if (result_type == Primitive::kPrimFloat) {
3950 __ Cvtsd(dst, src);
3951 } else {
3952 __ Cvtds(dst, src);
3953 }
3954 } else {
3955 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3956 << " to " << result_type;
3957 }
3958}
3959
3960void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3961 HandleShift(ushr);
3962}
3963
3964void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3965 HandleShift(ushr);
3966}
3967
3968void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3969 HandleBinaryOp(instruction);
3970}
3971
3972void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3973 HandleBinaryOp(instruction);
3974}
3975
3976void LocationsBuilderMIPS64::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 InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3982 // Nothing to do, this should be removed during prepare for register allocator.
3983 LOG(FATAL) << "Unreachable";
3984}
3985
3986void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003987 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003988}
3989
3990void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003991 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003992}
3993
3994void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003995 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003996}
3997
3998void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003999 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004000}
4001
4002void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004003 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004004}
4005
4006void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004007 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004008}
4009
4010void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004011 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004012}
4013
4014void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004015 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004016}
4017
4018void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004019 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004020}
4021
4022void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004023 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004024}
4025
4026void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004027 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004028}
4029
4030void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004031 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004032}
4033
Aart Bike9f37602015-10-09 11:15:55 -07004034void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004036}
4037
4038void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004040}
4041
4042void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004043 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004044}
4045
4046void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004047 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004048}
4049
4050void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004051 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004052}
4053
4054void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004055 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004056}
4057
4058void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004059 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004060}
4061
4062void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004063 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004064}
4065
Mark Mendellfe57faa2015-09-18 09:26:15 -04004066// Simple implementation of packed switch - generate cascaded compare/jumps.
4067void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4068 LocationSummary* locations =
4069 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4070 locations->SetInAt(0, Location::RequiresRegister());
4071}
4072
4073void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4074 int32_t lower_bound = switch_instr->GetStartValue();
4075 int32_t num_entries = switch_instr->GetNumEntries();
4076 LocationSummary* locations = switch_instr->GetLocations();
4077 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4078 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4079
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004080 // Create a set of compare/jumps.
4081 GpuRegister temp_reg = TMP;
4082 if (IsInt<16>(-lower_bound)) {
4083 __ Addiu(temp_reg, value_reg, -lower_bound);
4084 } else {
4085 __ LoadConst32(AT, -lower_bound);
4086 __ Addu(temp_reg, value_reg, AT);
4087 }
4088 // Jump to default if index is negative
4089 // Note: We don't check the case that index is positive while value < lower_bound, because in
4090 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4091 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4092
Mark Mendellfe57faa2015-09-18 09:26:15 -04004093 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004094 // Jump to successors[0] if value == lower_bound.
4095 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4096 int32_t last_index = 0;
4097 for (; num_entries - last_index > 2; last_index += 2) {
4098 __ Addiu(temp_reg, temp_reg, -2);
4099 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4100 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4101 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4102 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4103 }
4104 if (num_entries - last_index == 2) {
4105 // The last missing case_value.
4106 __ Addiu(temp_reg, temp_reg, -1);
4107 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004108 }
4109
4110 // And the default for any other value.
4111 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004112 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004113 }
4114}
4115
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004116void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4117 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4118}
4119
4120void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4121 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4122}
4123
Alexey Frunze4dda3372015-06-01 18:31:49 -07004124} // namespace mips64
4125} // namespace art