blob: dea6322e5b8dc536ed7d5d8349bc6c06ca9befb1 [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
40// We need extra temporary/scratch registers (in addition to AT) in some cases.
Alexey Frunze4dda3372015-06-01 18:31:49 -070041static constexpr FpuRegister FTMP = F8;
42
Alexey Frunze4dda3372015-06-01 18:31:49 -070043Location Mips64ReturnLocation(Primitive::Type return_type) {
44 switch (return_type) {
45 case Primitive::kPrimBoolean:
46 case Primitive::kPrimByte:
47 case Primitive::kPrimChar:
48 case Primitive::kPrimShort:
49 case Primitive::kPrimInt:
50 case Primitive::kPrimNot:
51 case Primitive::kPrimLong:
52 return Location::RegisterLocation(V0);
53
54 case Primitive::kPrimFloat:
55 case Primitive::kPrimDouble:
56 return Location::FpuRegisterLocation(F0);
57
58 case Primitive::kPrimVoid:
59 return Location();
60 }
61 UNREACHABLE();
62}
63
64Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
65 return Mips64ReturnLocation(type);
66}
67
68Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
69 return Location::RegisterLocation(kMethodRegisterArgument);
70}
71
72Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
73 Location next_location;
74 if (type == Primitive::kPrimVoid) {
75 LOG(FATAL) << "Unexpected parameter type " << type;
76 }
77
78 if (Primitive::IsFloatingPointType(type) &&
79 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
80 next_location = Location::FpuRegisterLocation(
81 calling_convention.GetFpuRegisterAt(float_index_++));
82 gp_index_++;
83 } else if (!Primitive::IsFloatingPointType(type) &&
84 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
85 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
86 float_index_++;
87 } else {
88 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
89 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
90 : Location::StackSlot(stack_offset);
91 }
92
93 // Space on the stack is reserved for all arguments.
94 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
95
96 // TODO: review
97
98 // TODO: shouldn't we use a whole machine word per argument on the stack?
99 // Implicit 4-byte method pointer (and such) will cause misalignment.
100
101 return next_location;
102}
103
104Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
105 return Mips64ReturnLocation(type);
106}
107
108#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
Lazar Trsicd9672662015-09-03 17:33:01 +0200109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700110
111class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
112 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000113 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114
115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100116 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
118 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000119 if (instruction_->CanThrowIntoCatchBlock()) {
120 // Live registers will be restored in the catch block if caught.
121 SaveLiveRegisters(codegen, instruction_->GetLocations());
122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700123 // We're moving two locations to locations that could overlap, so we need a parallel
124 // move resolver.
125 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
128 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100129 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700130 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
131 Primitive::kPrimInt);
132 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
133 instruction_,
134 instruction_->GetDexPc(),
135 this);
136 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
137 }
138
Alexandre Rames8158f282015-08-07 10:26:17 +0100139 bool IsFatal() const OVERRIDE { return true; }
140
Roland Levillain46648892015-06-19 16:07:18 +0100141 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
142
Alexey Frunze4dda3372015-06-01 18:31:49 -0700143 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700144 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
145};
146
147class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
148 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000149 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700150
151 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
152 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
153 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000154 if (instruction_->CanThrowIntoCatchBlock()) {
155 // Live registers will be restored in the catch block if caught.
156 SaveLiveRegisters(codegen, instruction_->GetLocations());
157 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700158 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
159 instruction_,
160 instruction_->GetDexPc(),
161 this);
162 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
163 }
164
Alexandre Rames8158f282015-08-07 10:26:17 +0100165 bool IsFatal() const OVERRIDE { return true; }
166
Roland Levillain46648892015-06-19 16:07:18 +0100167 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
168
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700170 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
171};
172
173class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
174 public:
175 LoadClassSlowPathMIPS64(HLoadClass* cls,
176 HInstruction* at,
177 uint32_t dex_pc,
178 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000179 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700180 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
181 }
182
183 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
184 LocationSummary* locations = at_->GetLocations();
185 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
186
187 __ Bind(GetEntryLabel());
188 SaveLiveRegisters(codegen, locations);
189
190 InvokeRuntimeCallingConvention calling_convention;
191 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
192 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
193 : QUICK_ENTRY_POINT(pInitializeType);
194 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
195 if (do_clinit_) {
196 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
197 } else {
198 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
199 }
200
201 // Move the class to the desired location.
202 Location out = locations->Out();
203 if (out.IsValid()) {
204 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
205 Primitive::Type type = at_->GetType();
206 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
207 }
208
209 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700210 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700211 }
212
Roland Levillain46648892015-06-19 16:07:18 +0100213 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
214
Alexey Frunze4dda3372015-06-01 18:31:49 -0700215 private:
216 // The class this slow path will load.
217 HLoadClass* const cls_;
218
219 // The instruction where this slow path is happening.
220 // (Might be the load class or an initialization check).
221 HInstruction* const at_;
222
223 // The dex PC of `at_`.
224 const uint32_t dex_pc_;
225
226 // Whether to initialize the class.
227 const bool do_clinit_;
228
229 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
230};
231
232class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
233 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000234 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700235
236 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
237 LocationSummary* locations = instruction_->GetLocations();
238 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
239 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
240
241 __ Bind(GetEntryLabel());
242 SaveLiveRegisters(codegen, locations);
243
244 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000245 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
246 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
248 instruction_,
249 instruction_->GetDexPc(),
250 this);
251 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
252 Primitive::Type type = instruction_->GetType();
253 mips64_codegen->MoveLocation(locations->Out(),
254 calling_convention.GetReturnLocation(type),
255 type);
256
257 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700258 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700259 }
260
Roland Levillain46648892015-06-19 16:07:18 +0100261 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
262
Alexey Frunze4dda3372015-06-01 18:31:49 -0700263 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700264 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
265};
266
267class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
268 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000269 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700270
271 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
272 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
273 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000274 if (instruction_->CanThrowIntoCatchBlock()) {
275 // Live registers will be restored in the catch block if caught.
276 SaveLiveRegisters(codegen, instruction_->GetLocations());
277 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700278 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
279 instruction_,
280 instruction_->GetDexPc(),
281 this);
282 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
283 }
284
Alexandre Rames8158f282015-08-07 10:26:17 +0100285 bool IsFatal() const OVERRIDE { return true; }
286
Roland Levillain46648892015-06-19 16:07:18 +0100287 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
288
Alexey Frunze4dda3372015-06-01 18:31:49 -0700289 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700290 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
291};
292
293class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
294 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100295 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000296 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700297
298 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
299 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
300 __ Bind(GetEntryLabel());
301 SaveLiveRegisters(codegen, instruction_->GetLocations());
302 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
303 instruction_,
304 instruction_->GetDexPc(),
305 this);
306 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
307 RestoreLiveRegisters(codegen, instruction_->GetLocations());
308 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700309 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700311 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 }
313 }
314
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 DCHECK(successor_ == nullptr);
317 return &return_label_;
318 }
319
Roland Levillain46648892015-06-19 16:07:18 +0100320 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
321
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700323 // If not null, the block to branch to after the suspend check.
324 HBasicBlock* const successor_;
325
326 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700327 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328
329 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
330};
331
332class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
333 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000334 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
337 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200338 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100339 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700340 DCHECK(instruction_->IsCheckCast()
341 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
342 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
343
344 __ Bind(GetEntryLabel());
345 SaveLiveRegisters(codegen, locations);
346
347 // We're moving two locations to locations that could overlap, so we need a parallel
348 // move resolver.
349 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700351 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
352 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100353 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700354 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
355 Primitive::kPrimNot);
356
357 if (instruction_->IsInstanceOf()) {
358 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
359 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100360 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000362 CheckEntrypointTypes<
363 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Primitive::Type ret_type = instruction_->GetType();
365 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
366 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 } else {
368 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100369 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
371 }
372
373 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700374 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700375 }
376
Roland Levillain46648892015-06-19 16:07:18 +0100377 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
378
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
381};
382
383class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
384 public:
Aart Bik42249c32016-01-07 15:33:50 -0800385 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000386 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387
388 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800389 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700390 __ Bind(GetEntryLabel());
391 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800392 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
393 instruction_,
394 instruction_->GetDexPc(),
395 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000396 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700397 }
398
Roland Levillain46648892015-06-19 16:07:18 +0100399 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
400
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700402 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
403};
404
405CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
406 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100407 const CompilerOptions& compiler_options,
408 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 : CodeGenerator(graph,
410 kNumberOfGpuRegisters,
411 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000412 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
414 arraysize(kCoreCalleeSaves)),
415 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
416 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100417 compiler_options,
418 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100419 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700420 location_builder_(graph, this),
421 instruction_visitor_(graph, this),
422 move_resolver_(graph->GetArena(), this),
423 isa_features_(isa_features) {
424 // Save RA (containing the return address) to mimic Quick.
425 AddAllocatedRegister(Location::RegisterLocation(RA));
426}
427
428#undef __
429#define __ down_cast<Mips64Assembler*>(GetAssembler())->
Lazar Trsicd9672662015-09-03 17:33:01 +0200430#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700431
432void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700433 // Ensure that we fix up branches.
434 __ FinalizeCode();
435
436 // Adjust native pc offsets in stack maps.
437 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
438 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
439 uint32_t new_position = __ GetAdjustedPosition(old_position);
440 DCHECK_GE(new_position, old_position);
441 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
442 }
443
444 // Adjust pc offsets for the disassembly information.
445 if (disasm_info_ != nullptr) {
446 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
447 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
448 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
449 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
450 it.second.start = __ GetAdjustedPosition(it.second.start);
451 it.second.end = __ GetAdjustedPosition(it.second.end);
452 }
453 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
454 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
455 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
456 }
457 }
458
Alexey Frunze4dda3372015-06-01 18:31:49 -0700459 CodeGenerator::Finalize(allocator);
460}
461
462Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
463 return codegen_->GetAssembler();
464}
465
466void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100467 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700468 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
469}
470
471void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100472 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700473 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
474}
475
476void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
477 // Pop reg
478 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200479 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700480}
481
482void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
483 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200484 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700485 __ Sd(GpuRegister(reg), SP, 0);
486}
487
488void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
489 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
490 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
491 // Allocate a scratch register other than TMP, if available.
492 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
493 // automatically unspilled when the scratch scope object is destroyed).
494 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
495 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200496 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700497 __ LoadFromOffset(load_type,
498 GpuRegister(ensure_scratch.GetRegister()),
499 SP,
500 index1 + stack_offset);
501 __ LoadFromOffset(load_type,
502 TMP,
503 SP,
504 index2 + stack_offset);
505 __ StoreToOffset(store_type,
506 GpuRegister(ensure_scratch.GetRegister()),
507 SP,
508 index2 + stack_offset);
509 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
510}
511
512static dwarf::Reg DWARFReg(GpuRegister reg) {
513 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
514}
515
David Srbeckyba702002016-02-01 18:15:29 +0000516static dwarf::Reg DWARFReg(FpuRegister reg) {
517 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
518}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700519
520void CodeGeneratorMIPS64::GenerateFrameEntry() {
521 __ Bind(&frame_entry_label_);
522
523 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
524
525 if (do_overflow_check) {
526 __ LoadFromOffset(kLoadWord,
527 ZERO,
528 SP,
529 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
530 RecordPcInfo(nullptr, 0);
531 }
532
533 // TODO: anything related to T9/GP/GOT/PIC/.so's?
534
535 if (HasEmptyFrame()) {
536 return;
537 }
538
539 // Make sure the frame size isn't unreasonably large. Per the various APIs
540 // it looks like it should always be less than 2GB in size, which allows
541 // us using 32-bit signed offsets from the stack pointer.
542 if (GetFrameSize() > 0x7FFFFFFF)
543 LOG(FATAL) << "Stack frame larger than 2GB";
544
545 // Spill callee-saved registers.
546 // Note that their cumulative size is small and they can be indexed using
547 // 16-bit offsets.
548
549 // TODO: increment/decrement SP in one step instead of two or remove this comment.
550
551 uint32_t ofs = FrameEntrySpillSize();
552 __ IncreaseFrameSize(ofs);
553
554 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
555 GpuRegister reg = kCoreCalleeSaves[i];
556 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200557 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700558 __ Sd(reg, SP, ofs);
559 __ cfi().RelOffset(DWARFReg(reg), ofs);
560 }
561 }
562
563 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
564 FpuRegister reg = kFpuCalleeSaves[i];
565 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200566 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700567 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000568 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700569 }
570 }
571
572 // Allocate the rest of the frame and store the current method pointer
573 // at its end.
574
575 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
576
577 static_assert(IsInt<16>(kCurrentMethodStackOffset),
578 "kCurrentMethodStackOffset must fit into int16_t");
579 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
580}
581
582void CodeGeneratorMIPS64::GenerateFrameExit() {
583 __ cfi().RememberState();
584
585 // TODO: anything related to T9/GP/GOT/PIC/.so's?
586
587 if (!HasEmptyFrame()) {
588 // Deallocate the rest of the frame.
589
590 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
591
592 // Restore callee-saved registers.
593 // Note that their cumulative size is small and they can be indexed using
594 // 16-bit offsets.
595
596 // TODO: increment/decrement SP in one step instead of two or remove this comment.
597
598 uint32_t ofs = 0;
599
600 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
601 FpuRegister reg = kFpuCalleeSaves[i];
602 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
603 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200604 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000605 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700606 }
607 }
608
609 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
610 GpuRegister reg = kCoreCalleeSaves[i];
611 if (allocated_registers_.ContainsCoreRegister(reg)) {
612 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200613 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700614 __ cfi().Restore(DWARFReg(reg));
615 }
616 }
617
618 DCHECK_EQ(ofs, FrameEntrySpillSize());
619 __ DecreaseFrameSize(ofs);
620 }
621
622 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700623 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700624
625 __ cfi().RestoreState();
626 __ cfi().DefCFAOffset(GetFrameSize());
627}
628
629void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
630 __ Bind(GetLabelOf(block));
631}
632
633void CodeGeneratorMIPS64::MoveLocation(Location destination,
634 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100635 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700636 if (source.Equals(destination)) {
637 return;
638 }
639
640 // A valid move can always be inferred from the destination and source
641 // locations. When moving from and to a register, the argument type can be
642 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100643 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700644 DCHECK_EQ(unspecified_type, false);
645
646 if (destination.IsRegister() || destination.IsFpuRegister()) {
647 if (unspecified_type) {
648 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
649 if (source.IsStackSlot() ||
650 (src_cst != nullptr && (src_cst->IsIntConstant()
651 || src_cst->IsFloatConstant()
652 || src_cst->IsNullConstant()))) {
653 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100654 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700655 } else {
656 // If the source is a double stack slot or a 64bit constant, a 64bit
657 // type is appropriate. Else the source is a register, and since the
658 // type has not been specified, we chose a 64bit type to force a 64bit
659 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100660 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700661 }
662 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100663 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
664 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700665 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
666 // Move to GPR/FPR from stack
667 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100668 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700669 __ LoadFpuFromOffset(load_type,
670 destination.AsFpuRegister<FpuRegister>(),
671 SP,
672 source.GetStackIndex());
673 } else {
674 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
675 __ LoadFromOffset(load_type,
676 destination.AsRegister<GpuRegister>(),
677 SP,
678 source.GetStackIndex());
679 }
680 } else if (source.IsConstant()) {
681 // Move to GPR/FPR from constant
682 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100683 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700684 gpr = destination.AsRegister<GpuRegister>();
685 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100686 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700687 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
688 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
689 gpr = ZERO;
690 } else {
691 __ LoadConst32(gpr, value);
692 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700693 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700694 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
695 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
696 gpr = ZERO;
697 } else {
698 __ LoadConst64(gpr, value);
699 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700700 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100701 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700702 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100703 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700704 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
705 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100706 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700707 if (destination.IsRegister()) {
708 // Move to GPR from GPR
709 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
710 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 DCHECK(destination.IsFpuRegister());
712 if (Primitive::Is64BitType(dst_type)) {
713 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
714 } else {
715 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
716 }
717 }
718 } else if (source.IsFpuRegister()) {
719 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700720 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100721 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700722 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
723 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100724 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700725 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
726 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100727 } else {
728 DCHECK(destination.IsRegister());
729 if (Primitive::Is64BitType(dst_type)) {
730 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
731 } else {
732 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
733 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700734 }
735 }
736 } else { // The destination is not a register. It must be a stack slot.
737 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
738 if (source.IsRegister() || source.IsFpuRegister()) {
739 if (unspecified_type) {
740 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100741 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700742 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100743 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700744 }
745 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100746 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
747 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700748 // Move to stack from GPR/FPR
749 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
750 if (source.IsRegister()) {
751 __ StoreToOffset(store_type,
752 source.AsRegister<GpuRegister>(),
753 SP,
754 destination.GetStackIndex());
755 } else {
756 __ StoreFpuToOffset(store_type,
757 source.AsFpuRegister<FpuRegister>(),
758 SP,
759 destination.GetStackIndex());
760 }
761 } else if (source.IsConstant()) {
762 // Move to stack from constant
763 HConstant* src_cst = source.GetConstant();
764 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700765 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700766 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700767 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
768 if (value != 0) {
769 gpr = TMP;
770 __ LoadConst32(gpr, value);
771 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700772 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700773 DCHECK(destination.IsDoubleStackSlot());
774 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
775 if (value != 0) {
776 gpr = TMP;
777 __ LoadConst64(gpr, value);
778 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700779 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700780 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700781 } else {
782 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
783 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
784 // Move to stack from stack
785 if (destination.IsStackSlot()) {
786 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
787 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
788 } else {
789 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
790 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
791 }
792 }
793 }
794}
795
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700796void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700797 DCHECK(!loc1.IsConstant());
798 DCHECK(!loc2.IsConstant());
799
800 if (loc1.Equals(loc2)) {
801 return;
802 }
803
804 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
805 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
806 bool is_fp_reg1 = loc1.IsFpuRegister();
807 bool is_fp_reg2 = loc2.IsFpuRegister();
808
809 if (loc2.IsRegister() && loc1.IsRegister()) {
810 // Swap 2 GPRs
811 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
812 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
813 __ Move(TMP, r2);
814 __ Move(r2, r1);
815 __ Move(r1, TMP);
816 } else if (is_fp_reg2 && is_fp_reg1) {
817 // Swap 2 FPRs
818 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
819 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700820 if (type == Primitive::kPrimFloat) {
821 __ MovS(FTMP, r1);
822 __ MovS(r1, r2);
823 __ MovS(r2, FTMP);
824 } else {
825 DCHECK_EQ(type, Primitive::kPrimDouble);
826 __ MovD(FTMP, r1);
827 __ MovD(r1, r2);
828 __ MovD(r2, FTMP);
829 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700830 } else if (is_slot1 != is_slot2) {
831 // Swap GPR/FPR and stack slot
832 Location reg_loc = is_slot1 ? loc2 : loc1;
833 Location mem_loc = is_slot1 ? loc1 : loc2;
834 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
835 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
836 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
837 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
838 if (reg_loc.IsFpuRegister()) {
839 __ StoreFpuToOffset(store_type,
840 reg_loc.AsFpuRegister<FpuRegister>(),
841 SP,
842 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700843 if (mem_loc.IsStackSlot()) {
844 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
845 } else {
846 DCHECK(mem_loc.IsDoubleStackSlot());
847 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
848 }
849 } else {
850 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
851 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
852 }
853 } else if (is_slot1 && is_slot2) {
854 move_resolver_.Exchange(loc1.GetStackIndex(),
855 loc2.GetStackIndex(),
856 loc1.IsDoubleStackSlot());
857 } else {
858 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
859 }
860}
861
Calin Juravle175dc732015-08-25 15:42:32 +0100862void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
863 DCHECK(location.IsRegister());
864 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
865}
866
Calin Juravlee460d1d2015-09-29 04:52:17 +0100867void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
868 if (location.IsRegister()) {
869 locations->AddTemp(location);
870 } else {
871 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
872 }
873}
874
Alexey Frunze4dda3372015-06-01 18:31:49 -0700875Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
876 Primitive::Type type = load->GetType();
877
878 switch (type) {
879 case Primitive::kPrimNot:
880 case Primitive::kPrimInt:
881 case Primitive::kPrimFloat:
882 return Location::StackSlot(GetStackSlot(load->GetLocal()));
883
884 case Primitive::kPrimLong:
885 case Primitive::kPrimDouble:
886 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
887
888 case Primitive::kPrimBoolean:
889 case Primitive::kPrimByte:
890 case Primitive::kPrimChar:
891 case Primitive::kPrimShort:
892 case Primitive::kPrimVoid:
893 LOG(FATAL) << "Unexpected type " << type;
894 }
895
896 LOG(FATAL) << "Unreachable";
897 return Location::NoLocation();
898}
899
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100900void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
901 GpuRegister value,
902 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700903 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700904 GpuRegister card = AT;
905 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100906 if (value_can_be_null) {
907 __ Beqzc(value, &done);
908 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700909 __ LoadFromOffset(kLoadDoubleword,
910 card,
911 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200912 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700913 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
914 __ Daddu(temp, card, temp);
915 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100916 if (value_can_be_null) {
917 __ Bind(&done);
918 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700919}
920
David Brazdil58282f42016-01-14 12:45:10 +0000921void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700922 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
923 blocked_core_registers_[ZERO] = true;
924 blocked_core_registers_[K0] = true;
925 blocked_core_registers_[K1] = true;
926 blocked_core_registers_[GP] = true;
927 blocked_core_registers_[SP] = true;
928 blocked_core_registers_[RA] = true;
929
Lazar Trsicd9672662015-09-03 17:33:01 +0200930 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
931 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700932 blocked_core_registers_[AT] = true;
933 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200934 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700935 blocked_fpu_registers_[FTMP] = true;
936
937 // Reserve suspend and thread registers.
938 blocked_core_registers_[S0] = true;
939 blocked_core_registers_[TR] = true;
940
941 // Reserve T9 for function calls
942 blocked_core_registers_[T9] = true;
943
944 // TODO: review; anything else?
945
David Brazdil58282f42016-01-14 12:45:10 +0000946 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
948 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
949 }
950
951 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
952 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
953 }
954}
955
Alexey Frunze4dda3372015-06-01 18:31:49 -0700956size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
957 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200958 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700959}
960
961size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
962 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200963 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700964}
965
966size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
967 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200968 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700969}
970
971size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
972 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200973 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700974}
975
976void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100977 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700978}
979
980void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100981 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700982}
983
Calin Juravle175dc732015-08-25 15:42:32 +0100984void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
985 HInstruction* instruction,
986 uint32_t dex_pc,
987 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200988 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100989 instruction,
990 dex_pc,
991 slow_path);
992}
993
Alexey Frunze4dda3372015-06-01 18:31:49 -0700994void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
995 HInstruction* instruction,
996 uint32_t dex_pc,
997 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100998 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700999 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1000 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1001 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001002 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001003 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001004}
1005
1006void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1007 GpuRegister class_reg) {
1008 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1009 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1010 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1011 // TODO: barrier needed?
1012 __ Bind(slow_path->GetExitLabel());
1013}
1014
1015void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1016 __ Sync(0); // only stype 0 is supported
1017}
1018
1019void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1020 HBasicBlock* successor) {
1021 SuspendCheckSlowPathMIPS64* slow_path =
1022 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1023 codegen_->AddSlowPath(slow_path);
1024
1025 __ LoadFromOffset(kLoadUnsignedHalfword,
1026 TMP,
1027 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001028 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001029 if (successor == nullptr) {
1030 __ Bnezc(TMP, slow_path->GetEntryLabel());
1031 __ Bind(slow_path->GetReturnLabel());
1032 } else {
1033 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001034 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001035 // slow_path will return to GetLabelOf(successor).
1036 }
1037}
1038
1039InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1040 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001041 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001042 assembler_(codegen->GetAssembler()),
1043 codegen_(codegen) {}
1044
1045void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1046 DCHECK_EQ(instruction->InputCount(), 2U);
1047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1048 Primitive::Type type = instruction->GetResultType();
1049 switch (type) {
1050 case Primitive::kPrimInt:
1051 case Primitive::kPrimLong: {
1052 locations->SetInAt(0, Location::RequiresRegister());
1053 HInstruction* right = instruction->InputAt(1);
1054 bool can_use_imm = false;
1055 if (right->IsConstant()) {
1056 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1057 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1058 can_use_imm = IsUint<16>(imm);
1059 } else if (instruction->IsAdd()) {
1060 can_use_imm = IsInt<16>(imm);
1061 } else {
1062 DCHECK(instruction->IsSub());
1063 can_use_imm = IsInt<16>(-imm);
1064 }
1065 }
1066 if (can_use_imm)
1067 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1068 else
1069 locations->SetInAt(1, Location::RequiresRegister());
1070 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1071 }
1072 break;
1073
1074 case Primitive::kPrimFloat:
1075 case Primitive::kPrimDouble:
1076 locations->SetInAt(0, Location::RequiresFpuRegister());
1077 locations->SetInAt(1, Location::RequiresFpuRegister());
1078 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1079 break;
1080
1081 default:
1082 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1083 }
1084}
1085
1086void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1087 Primitive::Type type = instruction->GetType();
1088 LocationSummary* locations = instruction->GetLocations();
1089
1090 switch (type) {
1091 case Primitive::kPrimInt:
1092 case Primitive::kPrimLong: {
1093 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1094 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1095 Location rhs_location = locations->InAt(1);
1096
1097 GpuRegister rhs_reg = ZERO;
1098 int64_t rhs_imm = 0;
1099 bool use_imm = rhs_location.IsConstant();
1100 if (use_imm) {
1101 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1102 } else {
1103 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1104 }
1105
1106 if (instruction->IsAnd()) {
1107 if (use_imm)
1108 __ Andi(dst, lhs, rhs_imm);
1109 else
1110 __ And(dst, lhs, rhs_reg);
1111 } else if (instruction->IsOr()) {
1112 if (use_imm)
1113 __ Ori(dst, lhs, rhs_imm);
1114 else
1115 __ Or(dst, lhs, rhs_reg);
1116 } else if (instruction->IsXor()) {
1117 if (use_imm)
1118 __ Xori(dst, lhs, rhs_imm);
1119 else
1120 __ Xor(dst, lhs, rhs_reg);
1121 } else if (instruction->IsAdd()) {
1122 if (type == Primitive::kPrimInt) {
1123 if (use_imm)
1124 __ Addiu(dst, lhs, rhs_imm);
1125 else
1126 __ Addu(dst, lhs, rhs_reg);
1127 } else {
1128 if (use_imm)
1129 __ Daddiu(dst, lhs, rhs_imm);
1130 else
1131 __ Daddu(dst, lhs, rhs_reg);
1132 }
1133 } else {
1134 DCHECK(instruction->IsSub());
1135 if (type == Primitive::kPrimInt) {
1136 if (use_imm)
1137 __ Addiu(dst, lhs, -rhs_imm);
1138 else
1139 __ Subu(dst, lhs, rhs_reg);
1140 } else {
1141 if (use_imm)
1142 __ Daddiu(dst, lhs, -rhs_imm);
1143 else
1144 __ Dsubu(dst, lhs, rhs_reg);
1145 }
1146 }
1147 break;
1148 }
1149 case Primitive::kPrimFloat:
1150 case Primitive::kPrimDouble: {
1151 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1152 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1153 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1154 if (instruction->IsAdd()) {
1155 if (type == Primitive::kPrimFloat)
1156 __ AddS(dst, lhs, rhs);
1157 else
1158 __ AddD(dst, lhs, rhs);
1159 } else if (instruction->IsSub()) {
1160 if (type == Primitive::kPrimFloat)
1161 __ SubS(dst, lhs, rhs);
1162 else
1163 __ SubD(dst, lhs, rhs);
1164 } else {
1165 LOG(FATAL) << "Unexpected floating-point binary operation";
1166 }
1167 break;
1168 }
1169 default:
1170 LOG(FATAL) << "Unexpected binary operation type " << type;
1171 }
1172}
1173
1174void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001175 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001176
1177 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1178 Primitive::Type type = instr->GetResultType();
1179 switch (type) {
1180 case Primitive::kPrimInt:
1181 case Primitive::kPrimLong: {
1182 locations->SetInAt(0, Location::RequiresRegister());
1183 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001184 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001185 break;
1186 }
1187 default:
1188 LOG(FATAL) << "Unexpected shift type " << type;
1189 }
1190}
1191
1192void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001193 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001194 LocationSummary* locations = instr->GetLocations();
1195 Primitive::Type type = instr->GetType();
1196
1197 switch (type) {
1198 case Primitive::kPrimInt:
1199 case Primitive::kPrimLong: {
1200 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1201 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1202 Location rhs_location = locations->InAt(1);
1203
1204 GpuRegister rhs_reg = ZERO;
1205 int64_t rhs_imm = 0;
1206 bool use_imm = rhs_location.IsConstant();
1207 if (use_imm) {
1208 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1209 } else {
1210 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1211 }
1212
1213 if (use_imm) {
1214 uint32_t shift_value = (type == Primitive::kPrimInt)
1215 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1216 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1217
Alexey Frunze92d90602015-12-18 18:16:36 -08001218 if (shift_value == 0) {
1219 if (dst != lhs) {
1220 __ Move(dst, lhs);
1221 }
1222 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001223 if (instr->IsShl()) {
1224 __ Sll(dst, lhs, shift_value);
1225 } else if (instr->IsShr()) {
1226 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001227 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001228 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001229 } else {
1230 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001231 }
1232 } else {
1233 if (shift_value < 32) {
1234 if (instr->IsShl()) {
1235 __ Dsll(dst, lhs, shift_value);
1236 } else if (instr->IsShr()) {
1237 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001238 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001239 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001240 } else {
1241 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001242 }
1243 } else {
1244 shift_value -= 32;
1245 if (instr->IsShl()) {
1246 __ Dsll32(dst, lhs, shift_value);
1247 } else if (instr->IsShr()) {
1248 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001249 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001250 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001251 } else {
1252 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001253 }
1254 }
1255 }
1256 } else {
1257 if (type == Primitive::kPrimInt) {
1258 if (instr->IsShl()) {
1259 __ Sllv(dst, lhs, rhs_reg);
1260 } else if (instr->IsShr()) {
1261 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001262 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001263 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001264 } else {
1265 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001266 }
1267 } else {
1268 if (instr->IsShl()) {
1269 __ Dsllv(dst, lhs, rhs_reg);
1270 } else if (instr->IsShr()) {
1271 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001272 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001273 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001274 } else {
1275 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001276 }
1277 }
1278 }
1279 break;
1280 }
1281 default:
1282 LOG(FATAL) << "Unexpected shift operation type " << type;
1283 }
1284}
1285
1286void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1287 HandleBinaryOp(instruction);
1288}
1289
1290void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1291 HandleBinaryOp(instruction);
1292}
1293
1294void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1295 HandleBinaryOp(instruction);
1296}
1297
1298void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1299 HandleBinaryOp(instruction);
1300}
1301
1302void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1303 LocationSummary* locations =
1304 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1305 locations->SetInAt(0, Location::RequiresRegister());
1306 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1307 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1308 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1309 } else {
1310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1311 }
1312}
1313
1314void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1315 LocationSummary* locations = instruction->GetLocations();
1316 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1317 Location index = locations->InAt(1);
1318 Primitive::Type type = instruction->GetType();
1319
1320 switch (type) {
1321 case Primitive::kPrimBoolean: {
1322 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1323 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1324 if (index.IsConstant()) {
1325 size_t offset =
1326 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1327 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1328 } else {
1329 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1330 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1331 }
1332 break;
1333 }
1334
1335 case Primitive::kPrimByte: {
1336 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1337 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1338 if (index.IsConstant()) {
1339 size_t offset =
1340 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1341 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1342 } else {
1343 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1344 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1345 }
1346 break;
1347 }
1348
1349 case Primitive::kPrimShort: {
1350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1351 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1352 if (index.IsConstant()) {
1353 size_t offset =
1354 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1355 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1356 } else {
1357 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1358 __ Daddu(TMP, obj, TMP);
1359 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1360 }
1361 break;
1362 }
1363
1364 case Primitive::kPrimChar: {
1365 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1366 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1367 if (index.IsConstant()) {
1368 size_t offset =
1369 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1370 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1371 } else {
1372 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1373 __ Daddu(TMP, obj, TMP);
1374 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1375 }
1376 break;
1377 }
1378
1379 case Primitive::kPrimInt:
1380 case Primitive::kPrimNot: {
1381 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1382 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1383 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1384 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1385 if (index.IsConstant()) {
1386 size_t offset =
1387 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1388 __ LoadFromOffset(load_type, out, obj, offset);
1389 } else {
1390 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1391 __ Daddu(TMP, obj, TMP);
1392 __ LoadFromOffset(load_type, out, TMP, data_offset);
1393 }
1394 break;
1395 }
1396
1397 case Primitive::kPrimLong: {
1398 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1399 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1400 if (index.IsConstant()) {
1401 size_t offset =
1402 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1403 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1404 } else {
1405 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1406 __ Daddu(TMP, obj, TMP);
1407 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1408 }
1409 break;
1410 }
1411
1412 case Primitive::kPrimFloat: {
1413 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1414 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1415 if (index.IsConstant()) {
1416 size_t offset =
1417 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1418 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1419 } else {
1420 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1421 __ Daddu(TMP, obj, TMP);
1422 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1423 }
1424 break;
1425 }
1426
1427 case Primitive::kPrimDouble: {
1428 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1429 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1430 if (index.IsConstant()) {
1431 size_t offset =
1432 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1433 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1434 } else {
1435 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1436 __ Daddu(TMP, obj, TMP);
1437 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1438 }
1439 break;
1440 }
1441
1442 case Primitive::kPrimVoid:
1443 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1444 UNREACHABLE();
1445 }
1446 codegen_->MaybeRecordImplicitNullCheck(instruction);
1447}
1448
1449void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1451 locations->SetInAt(0, Location::RequiresRegister());
1452 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1453}
1454
1455void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1456 LocationSummary* locations = instruction->GetLocations();
1457 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1458 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1459 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1460 __ LoadFromOffset(kLoadWord, out, obj, offset);
1461 codegen_->MaybeRecordImplicitNullCheck(instruction);
1462}
1463
1464void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001465 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001466 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1467 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001468 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1469 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001470 InvokeRuntimeCallingConvention calling_convention;
1471 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1472 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1473 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1474 } else {
1475 locations->SetInAt(0, Location::RequiresRegister());
1476 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1477 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1478 locations->SetInAt(2, Location::RequiresFpuRegister());
1479 } else {
1480 locations->SetInAt(2, Location::RequiresRegister());
1481 }
1482 }
1483}
1484
1485void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1486 LocationSummary* locations = instruction->GetLocations();
1487 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1488 Location index = locations->InAt(1);
1489 Primitive::Type value_type = instruction->GetComponentType();
1490 bool needs_runtime_call = locations->WillCall();
1491 bool needs_write_barrier =
1492 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1493
1494 switch (value_type) {
1495 case Primitive::kPrimBoolean:
1496 case Primitive::kPrimByte: {
1497 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1498 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1499 if (index.IsConstant()) {
1500 size_t offset =
1501 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1502 __ StoreToOffset(kStoreByte, value, obj, offset);
1503 } else {
1504 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1505 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1506 }
1507 break;
1508 }
1509
1510 case Primitive::kPrimShort:
1511 case Primitive::kPrimChar: {
1512 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1513 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1514 if (index.IsConstant()) {
1515 size_t offset =
1516 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1517 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1518 } else {
1519 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1520 __ Daddu(TMP, obj, TMP);
1521 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1522 }
1523 break;
1524 }
1525
1526 case Primitive::kPrimInt:
1527 case Primitive::kPrimNot: {
1528 if (!needs_runtime_call) {
1529 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1530 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1531 if (index.IsConstant()) {
1532 size_t offset =
1533 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1534 __ StoreToOffset(kStoreWord, value, obj, offset);
1535 } else {
1536 DCHECK(index.IsRegister()) << index;
1537 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1538 __ Daddu(TMP, obj, TMP);
1539 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1540 }
1541 codegen_->MaybeRecordImplicitNullCheck(instruction);
1542 if (needs_write_barrier) {
1543 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001544 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001545 }
1546 } else {
1547 DCHECK_EQ(value_type, Primitive::kPrimNot);
1548 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1549 instruction,
1550 instruction->GetDexPc(),
1551 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001552 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001553 }
1554 break;
1555 }
1556
1557 case Primitive::kPrimLong: {
1558 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1559 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1560 if (index.IsConstant()) {
1561 size_t offset =
1562 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1563 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1564 } else {
1565 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1566 __ Daddu(TMP, obj, TMP);
1567 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1568 }
1569 break;
1570 }
1571
1572 case Primitive::kPrimFloat: {
1573 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1574 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1575 DCHECK(locations->InAt(2).IsFpuRegister());
1576 if (index.IsConstant()) {
1577 size_t offset =
1578 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1579 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1580 } else {
1581 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1582 __ Daddu(TMP, obj, TMP);
1583 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1584 }
1585 break;
1586 }
1587
1588 case Primitive::kPrimDouble: {
1589 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1590 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1591 DCHECK(locations->InAt(2).IsFpuRegister());
1592 if (index.IsConstant()) {
1593 size_t offset =
1594 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1595 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1596 } else {
1597 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1598 __ Daddu(TMP, obj, TMP);
1599 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1600 }
1601 break;
1602 }
1603
1604 case Primitive::kPrimVoid:
1605 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1606 UNREACHABLE();
1607 }
1608
1609 // Ints and objects are handled in the switch.
1610 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1611 codegen_->MaybeRecordImplicitNullCheck(instruction);
1612 }
1613}
1614
1615void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001616 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1617 ? LocationSummary::kCallOnSlowPath
1618 : LocationSummary::kNoCall;
1619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001620 locations->SetInAt(0, Location::RequiresRegister());
1621 locations->SetInAt(1, Location::RequiresRegister());
1622 if (instruction->HasUses()) {
1623 locations->SetOut(Location::SameAsFirstInput());
1624 }
1625}
1626
1627void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1628 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001629 BoundsCheckSlowPathMIPS64* slow_path =
1630 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001631 codegen_->AddSlowPath(slow_path);
1632
1633 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1634 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1635
1636 // length is limited by the maximum positive signed 32-bit integer.
1637 // Unsigned comparison of length and index checks for index < 0
1638 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001639 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001640}
1641
1642void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1643 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1644 instruction,
1645 LocationSummary::kCallOnSlowPath);
1646 locations->SetInAt(0, Location::RequiresRegister());
1647 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001648 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001649 locations->AddTemp(Location::RequiresRegister());
1650}
1651
1652void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1653 LocationSummary* locations = instruction->GetLocations();
1654 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1655 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1656 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1657
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001658 SlowPathCodeMIPS64* slow_path =
1659 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001660 codegen_->AddSlowPath(slow_path);
1661
1662 // TODO: avoid this check if we know obj is not null.
1663 __ Beqzc(obj, slow_path->GetExitLabel());
1664 // Compare the class of `obj` with `cls`.
1665 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1666 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1667 __ Bind(slow_path->GetExitLabel());
1668}
1669
1670void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1671 LocationSummary* locations =
1672 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1673 locations->SetInAt(0, Location::RequiresRegister());
1674 if (check->HasUses()) {
1675 locations->SetOut(Location::SameAsFirstInput());
1676 }
1677}
1678
1679void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1680 // We assume the class is not null.
1681 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1682 check->GetLoadClass(),
1683 check,
1684 check->GetDexPc(),
1685 true);
1686 codegen_->AddSlowPath(slow_path);
1687 GenerateClassInitializationCheck(slow_path,
1688 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1689}
1690
1691void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1692 Primitive::Type in_type = compare->InputAt(0)->GetType();
1693
Alexey Frunze299a9392015-12-08 16:08:02 -08001694 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001695
1696 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08001697 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001698 case Primitive::kPrimLong:
1699 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001700 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1702 break;
1703
1704 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001705 case Primitive::kPrimDouble:
1706 locations->SetInAt(0, Location::RequiresFpuRegister());
1707 locations->SetInAt(1, Location::RequiresFpuRegister());
1708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001709 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001710
1711 default:
1712 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1713 }
1714}
1715
1716void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1717 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001718 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001719 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1720
1721 // 0 if: left == right
1722 // 1 if: left > right
1723 // -1 if: left < right
1724 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08001725 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001726 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001727 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001728 Location rhs_location = locations->InAt(1);
1729 bool use_imm = rhs_location.IsConstant();
1730 GpuRegister rhs = ZERO;
1731 if (use_imm) {
Aart Bika19616e2016-02-01 18:57:58 -08001732 if (in_type == Primitive::kPrimInt) {
1733 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1734 if (value != 0) {
1735 rhs = AT;
1736 __ LoadConst32(rhs, value);
1737 }
1738 } else {
1739 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1740 if (value != 0) {
1741 rhs = AT;
1742 __ LoadConst64(rhs, value);
1743 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001744 }
1745 } else {
1746 rhs = rhs_location.AsRegister<GpuRegister>();
1747 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001748 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001749 __ Slt(res, rhs, lhs);
1750 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001751 break;
1752 }
1753
Alexey Frunze299a9392015-12-08 16:08:02 -08001754 case Primitive::kPrimFloat: {
1755 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1756 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1757 Mips64Label done;
1758 __ CmpEqS(FTMP, lhs, rhs);
1759 __ LoadConst32(res, 0);
1760 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001761 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001762 __ CmpLtS(FTMP, lhs, rhs);
1763 __ LoadConst32(res, -1);
1764 __ Bc1nez(FTMP, &done);
1765 __ LoadConst32(res, 1);
1766 } else {
1767 __ CmpLtS(FTMP, rhs, lhs);
1768 __ LoadConst32(res, 1);
1769 __ Bc1nez(FTMP, &done);
1770 __ LoadConst32(res, -1);
1771 }
1772 __ Bind(&done);
1773 break;
1774 }
1775
Alexey Frunze4dda3372015-06-01 18:31:49 -07001776 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001777 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1778 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1779 Mips64Label done;
1780 __ CmpEqD(FTMP, lhs, rhs);
1781 __ LoadConst32(res, 0);
1782 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001783 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001784 __ CmpLtD(FTMP, lhs, rhs);
1785 __ LoadConst32(res, -1);
1786 __ Bc1nez(FTMP, &done);
1787 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001788 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001789 __ CmpLtD(FTMP, rhs, lhs);
1790 __ LoadConst32(res, 1);
1791 __ Bc1nez(FTMP, &done);
1792 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001793 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001794 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001795 break;
1796 }
1797
1798 default:
1799 LOG(FATAL) << "Unimplemented compare type " << in_type;
1800 }
1801}
1802
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001804 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001805 switch (instruction->InputAt(0)->GetType()) {
1806 default:
1807 case Primitive::kPrimLong:
1808 locations->SetInAt(0, Location::RequiresRegister());
1809 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1810 break;
1811
1812 case Primitive::kPrimFloat:
1813 case Primitive::kPrimDouble:
1814 locations->SetInAt(0, Location::RequiresFpuRegister());
1815 locations->SetInAt(1, Location::RequiresFpuRegister());
1816 break;
1817 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001818 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001819 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1820 }
1821}
1822
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001824 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001825 return;
1826 }
1827
Alexey Frunze299a9392015-12-08 16:08:02 -08001828 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001829 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001830 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001831 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001832
Alexey Frunze299a9392015-12-08 16:08:02 -08001833 switch (type) {
1834 default:
1835 // Integer case.
1836 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1837 return;
1838 case Primitive::kPrimLong:
1839 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1840 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001841
Alexey Frunze299a9392015-12-08 16:08:02 -08001842 case Primitive::kPrimFloat:
1843 case Primitive::kPrimDouble:
1844 // TODO: don't use branches.
1845 GenerateFpCompareAndBranch(instruction->GetCondition(),
1846 instruction->IsGtBias(),
1847 type,
1848 locations,
1849 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001850 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001851 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001852
1853 // Convert the branches into the result.
1854 Mips64Label done;
1855
1856 // False case: result = 0.
1857 __ LoadConst32(dst, 0);
1858 __ Bc(&done);
1859
1860 // True case: result = 1.
1861 __ Bind(&true_label);
1862 __ LoadConst32(dst, 1);
1863 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001864}
1865
Alexey Frunzec857c742015-09-23 15:12:39 -07001866void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1867 DCHECK(instruction->IsDiv() || instruction->IsRem());
1868 Primitive::Type type = instruction->GetResultType();
1869
1870 LocationSummary* locations = instruction->GetLocations();
1871 Location second = locations->InAt(1);
1872 DCHECK(second.IsConstant());
1873
1874 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1875 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1876 int64_t imm = Int64FromConstant(second.GetConstant());
1877 DCHECK(imm == 1 || imm == -1);
1878
1879 if (instruction->IsRem()) {
1880 __ Move(out, ZERO);
1881 } else {
1882 if (imm == -1) {
1883 if (type == Primitive::kPrimInt) {
1884 __ Subu(out, ZERO, dividend);
1885 } else {
1886 DCHECK_EQ(type, Primitive::kPrimLong);
1887 __ Dsubu(out, ZERO, dividend);
1888 }
1889 } else if (out != dividend) {
1890 __ Move(out, dividend);
1891 }
1892 }
1893}
1894
1895void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1896 DCHECK(instruction->IsDiv() || instruction->IsRem());
1897 Primitive::Type type = instruction->GetResultType();
1898
1899 LocationSummary* locations = instruction->GetLocations();
1900 Location second = locations->InAt(1);
1901 DCHECK(second.IsConstant());
1902
1903 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1904 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1905 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001906 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001907 int ctz_imm = CTZ(abs_imm);
1908
1909 if (instruction->IsDiv()) {
1910 if (type == Primitive::kPrimInt) {
1911 if (ctz_imm == 1) {
1912 // Fast path for division by +/-2, which is very common.
1913 __ Srl(TMP, dividend, 31);
1914 } else {
1915 __ Sra(TMP, dividend, 31);
1916 __ Srl(TMP, TMP, 32 - ctz_imm);
1917 }
1918 __ Addu(out, dividend, TMP);
1919 __ Sra(out, out, ctz_imm);
1920 if (imm < 0) {
1921 __ Subu(out, ZERO, out);
1922 }
1923 } else {
1924 DCHECK_EQ(type, Primitive::kPrimLong);
1925 if (ctz_imm == 1) {
1926 // Fast path for division by +/-2, which is very common.
1927 __ Dsrl32(TMP, dividend, 31);
1928 } else {
1929 __ Dsra32(TMP, dividend, 31);
1930 if (ctz_imm > 32) {
1931 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1932 } else {
1933 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1934 }
1935 }
1936 __ Daddu(out, dividend, TMP);
1937 if (ctz_imm < 32) {
1938 __ Dsra(out, out, ctz_imm);
1939 } else {
1940 __ Dsra32(out, out, ctz_imm - 32);
1941 }
1942 if (imm < 0) {
1943 __ Dsubu(out, ZERO, out);
1944 }
1945 }
1946 } else {
1947 if (type == Primitive::kPrimInt) {
1948 if (ctz_imm == 1) {
1949 // Fast path for modulo +/-2, which is very common.
1950 __ Sra(TMP, dividend, 31);
1951 __ Subu(out, dividend, TMP);
1952 __ Andi(out, out, 1);
1953 __ Addu(out, out, TMP);
1954 } else {
1955 __ Sra(TMP, dividend, 31);
1956 __ Srl(TMP, TMP, 32 - ctz_imm);
1957 __ Addu(out, dividend, TMP);
1958 if (IsUint<16>(abs_imm - 1)) {
1959 __ Andi(out, out, abs_imm - 1);
1960 } else {
1961 __ Sll(out, out, 32 - ctz_imm);
1962 __ Srl(out, out, 32 - ctz_imm);
1963 }
1964 __ Subu(out, out, TMP);
1965 }
1966 } else {
1967 DCHECK_EQ(type, Primitive::kPrimLong);
1968 if (ctz_imm == 1) {
1969 // Fast path for modulo +/-2, which is very common.
1970 __ Dsra32(TMP, dividend, 31);
1971 __ Dsubu(out, dividend, TMP);
1972 __ Andi(out, out, 1);
1973 __ Daddu(out, out, TMP);
1974 } else {
1975 __ Dsra32(TMP, dividend, 31);
1976 if (ctz_imm > 32) {
1977 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1978 } else {
1979 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1980 }
1981 __ Daddu(out, dividend, TMP);
1982 if (IsUint<16>(abs_imm - 1)) {
1983 __ Andi(out, out, abs_imm - 1);
1984 } else {
1985 if (ctz_imm > 32) {
1986 __ Dsll(out, out, 64 - ctz_imm);
1987 __ Dsrl(out, out, 64 - ctz_imm);
1988 } else {
1989 __ Dsll32(out, out, 32 - ctz_imm);
1990 __ Dsrl32(out, out, 32 - ctz_imm);
1991 }
1992 }
1993 __ Dsubu(out, out, TMP);
1994 }
1995 }
1996 }
1997}
1998
1999void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2000 DCHECK(instruction->IsDiv() || instruction->IsRem());
2001
2002 LocationSummary* locations = instruction->GetLocations();
2003 Location second = locations->InAt(1);
2004 DCHECK(second.IsConstant());
2005
2006 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2007 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2008 int64_t imm = Int64FromConstant(second.GetConstant());
2009
2010 Primitive::Type type = instruction->GetResultType();
2011 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2012
2013 int64_t magic;
2014 int shift;
2015 CalculateMagicAndShiftForDivRem(imm,
2016 (type == Primitive::kPrimLong),
2017 &magic,
2018 &shift);
2019
2020 if (type == Primitive::kPrimInt) {
2021 __ LoadConst32(TMP, magic);
2022 __ MuhR6(TMP, dividend, TMP);
2023
2024 if (imm > 0 && magic < 0) {
2025 __ Addu(TMP, TMP, dividend);
2026 } else if (imm < 0 && magic > 0) {
2027 __ Subu(TMP, TMP, dividend);
2028 }
2029
2030 if (shift != 0) {
2031 __ Sra(TMP, TMP, shift);
2032 }
2033
2034 if (instruction->IsDiv()) {
2035 __ Sra(out, TMP, 31);
2036 __ Subu(out, TMP, out);
2037 } else {
2038 __ Sra(AT, TMP, 31);
2039 __ Subu(AT, TMP, AT);
2040 __ LoadConst32(TMP, imm);
2041 __ MulR6(TMP, AT, TMP);
2042 __ Subu(out, dividend, TMP);
2043 }
2044 } else {
2045 __ LoadConst64(TMP, magic);
2046 __ Dmuh(TMP, dividend, TMP);
2047
2048 if (imm > 0 && magic < 0) {
2049 __ Daddu(TMP, TMP, dividend);
2050 } else if (imm < 0 && magic > 0) {
2051 __ Dsubu(TMP, TMP, dividend);
2052 }
2053
2054 if (shift >= 32) {
2055 __ Dsra32(TMP, TMP, shift - 32);
2056 } else if (shift > 0) {
2057 __ Dsra(TMP, TMP, shift);
2058 }
2059
2060 if (instruction->IsDiv()) {
2061 __ Dsra32(out, TMP, 31);
2062 __ Dsubu(out, TMP, out);
2063 } else {
2064 __ Dsra32(AT, TMP, 31);
2065 __ Dsubu(AT, TMP, AT);
2066 __ LoadConst64(TMP, imm);
2067 __ Dmul(TMP, AT, TMP);
2068 __ Dsubu(out, dividend, TMP);
2069 }
2070 }
2071}
2072
2073void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2074 DCHECK(instruction->IsDiv() || instruction->IsRem());
2075 Primitive::Type type = instruction->GetResultType();
2076 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2077
2078 LocationSummary* locations = instruction->GetLocations();
2079 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2080 Location second = locations->InAt(1);
2081
2082 if (second.IsConstant()) {
2083 int64_t imm = Int64FromConstant(second.GetConstant());
2084 if (imm == 0) {
2085 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2086 } else if (imm == 1 || imm == -1) {
2087 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002088 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002089 DivRemByPowerOfTwo(instruction);
2090 } else {
2091 DCHECK(imm <= -2 || imm >= 2);
2092 GenerateDivRemWithAnyConstant(instruction);
2093 }
2094 } else {
2095 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2096 GpuRegister divisor = second.AsRegister<GpuRegister>();
2097 if (instruction->IsDiv()) {
2098 if (type == Primitive::kPrimInt)
2099 __ DivR6(out, dividend, divisor);
2100 else
2101 __ Ddiv(out, dividend, divisor);
2102 } else {
2103 if (type == Primitive::kPrimInt)
2104 __ ModR6(out, dividend, divisor);
2105 else
2106 __ Dmod(out, dividend, divisor);
2107 }
2108 }
2109}
2110
Alexey Frunze4dda3372015-06-01 18:31:49 -07002111void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2112 LocationSummary* locations =
2113 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2114 switch (div->GetResultType()) {
2115 case Primitive::kPrimInt:
2116 case Primitive::kPrimLong:
2117 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002118 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002119 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2120 break;
2121
2122 case Primitive::kPrimFloat:
2123 case Primitive::kPrimDouble:
2124 locations->SetInAt(0, Location::RequiresFpuRegister());
2125 locations->SetInAt(1, Location::RequiresFpuRegister());
2126 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2127 break;
2128
2129 default:
2130 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2131 }
2132}
2133
2134void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2135 Primitive::Type type = instruction->GetType();
2136 LocationSummary* locations = instruction->GetLocations();
2137
2138 switch (type) {
2139 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002140 case Primitive::kPrimLong:
2141 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002142 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002143 case Primitive::kPrimFloat:
2144 case Primitive::kPrimDouble: {
2145 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2146 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2147 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2148 if (type == Primitive::kPrimFloat)
2149 __ DivS(dst, lhs, rhs);
2150 else
2151 __ DivD(dst, lhs, rhs);
2152 break;
2153 }
2154 default:
2155 LOG(FATAL) << "Unexpected div type " << type;
2156 }
2157}
2158
2159void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002160 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2161 ? LocationSummary::kCallOnSlowPath
2162 : LocationSummary::kNoCall;
2163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002164 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2165 if (instruction->HasUses()) {
2166 locations->SetOut(Location::SameAsFirstInput());
2167 }
2168}
2169
2170void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2171 SlowPathCodeMIPS64* slow_path =
2172 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2173 codegen_->AddSlowPath(slow_path);
2174 Location value = instruction->GetLocations()->InAt(0);
2175
2176 Primitive::Type type = instruction->GetType();
2177
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002178 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002179 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002180 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002181 }
2182
2183 if (value.IsConstant()) {
2184 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2185 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002186 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002187 } else {
2188 // A division by a non-null constant is valid. We don't need to perform
2189 // any check, so simply fall through.
2190 }
2191 } else {
2192 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2193 }
2194}
2195
2196void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2197 LocationSummary* locations =
2198 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2199 locations->SetOut(Location::ConstantLocation(constant));
2200}
2201
2202void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2203 // Will be generated at use site.
2204}
2205
2206void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2207 exit->SetLocations(nullptr);
2208}
2209
2210void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2211}
2212
2213void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2214 LocationSummary* locations =
2215 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2216 locations->SetOut(Location::ConstantLocation(constant));
2217}
2218
2219void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2220 // Will be generated at use site.
2221}
2222
David Brazdilfc6a86a2015-06-26 10:33:45 +00002223void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002224 DCHECK(!successor->IsExitBlock());
2225 HBasicBlock* block = got->GetBlock();
2226 HInstruction* previous = got->GetPrevious();
2227 HLoopInformation* info = block->GetLoopInformation();
2228
2229 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2230 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2231 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2232 return;
2233 }
2234 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2235 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2236 }
2237 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002238 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002239 }
2240}
2241
David Brazdilfc6a86a2015-06-26 10:33:45 +00002242void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2243 got->SetLocations(nullptr);
2244}
2245
2246void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2247 HandleGoto(got, got->GetSuccessor());
2248}
2249
2250void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2251 try_boundary->SetLocations(nullptr);
2252}
2253
2254void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2255 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2256 if (!successor->IsExitBlock()) {
2257 HandleGoto(try_boundary, successor);
2258 }
2259}
2260
Alexey Frunze299a9392015-12-08 16:08:02 -08002261void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2262 bool is64bit,
2263 LocationSummary* locations) {
2264 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2265 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2266 Location rhs_location = locations->InAt(1);
2267 GpuRegister rhs_reg = ZERO;
2268 int64_t rhs_imm = 0;
2269 bool use_imm = rhs_location.IsConstant();
2270 if (use_imm) {
2271 if (is64bit) {
2272 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2273 } else {
2274 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2275 }
2276 } else {
2277 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2278 }
2279 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2280
2281 switch (cond) {
2282 case kCondEQ:
2283 case kCondNE:
2284 if (use_imm && IsUint<16>(rhs_imm)) {
2285 __ Xori(dst, lhs, rhs_imm);
2286 } else {
2287 if (use_imm) {
2288 rhs_reg = TMP;
2289 __ LoadConst64(rhs_reg, rhs_imm);
2290 }
2291 __ Xor(dst, lhs, rhs_reg);
2292 }
2293 if (cond == kCondEQ) {
2294 __ Sltiu(dst, dst, 1);
2295 } else {
2296 __ Sltu(dst, ZERO, dst);
2297 }
2298 break;
2299
2300 case kCondLT:
2301 case kCondGE:
2302 if (use_imm && IsInt<16>(rhs_imm)) {
2303 __ Slti(dst, lhs, rhs_imm);
2304 } else {
2305 if (use_imm) {
2306 rhs_reg = TMP;
2307 __ LoadConst64(rhs_reg, rhs_imm);
2308 }
2309 __ Slt(dst, lhs, rhs_reg);
2310 }
2311 if (cond == kCondGE) {
2312 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2313 // only the slt instruction but no sge.
2314 __ Xori(dst, dst, 1);
2315 }
2316 break;
2317
2318 case kCondLE:
2319 case kCondGT:
2320 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2321 // Simulate lhs <= rhs via lhs < rhs + 1.
2322 __ Slti(dst, lhs, rhs_imm_plus_one);
2323 if (cond == kCondGT) {
2324 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2325 // only the slti instruction but no sgti.
2326 __ Xori(dst, dst, 1);
2327 }
2328 } else {
2329 if (use_imm) {
2330 rhs_reg = TMP;
2331 __ LoadConst64(rhs_reg, rhs_imm);
2332 }
2333 __ Slt(dst, rhs_reg, lhs);
2334 if (cond == kCondLE) {
2335 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2336 // only the slt instruction but no sle.
2337 __ Xori(dst, dst, 1);
2338 }
2339 }
2340 break;
2341
2342 case kCondB:
2343 case kCondAE:
2344 if (use_imm && IsInt<16>(rhs_imm)) {
2345 // Sltiu sign-extends its 16-bit immediate operand before
2346 // the comparison and thus lets us compare directly with
2347 // unsigned values in the ranges [0, 0x7fff] and
2348 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2349 __ Sltiu(dst, lhs, rhs_imm);
2350 } else {
2351 if (use_imm) {
2352 rhs_reg = TMP;
2353 __ LoadConst64(rhs_reg, rhs_imm);
2354 }
2355 __ Sltu(dst, lhs, rhs_reg);
2356 }
2357 if (cond == kCondAE) {
2358 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2359 // only the sltu instruction but no sgeu.
2360 __ Xori(dst, dst, 1);
2361 }
2362 break;
2363
2364 case kCondBE:
2365 case kCondA:
2366 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2367 // Simulate lhs <= rhs via lhs < rhs + 1.
2368 // Note that this only works if rhs + 1 does not overflow
2369 // to 0, hence the check above.
2370 // Sltiu sign-extends its 16-bit immediate operand before
2371 // the comparison and thus lets us compare directly with
2372 // unsigned values in the ranges [0, 0x7fff] and
2373 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2374 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2375 if (cond == kCondA) {
2376 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2377 // only the sltiu instruction but no sgtiu.
2378 __ Xori(dst, dst, 1);
2379 }
2380 } else {
2381 if (use_imm) {
2382 rhs_reg = TMP;
2383 __ LoadConst64(rhs_reg, rhs_imm);
2384 }
2385 __ Sltu(dst, rhs_reg, lhs);
2386 if (cond == kCondBE) {
2387 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2388 // only the sltu instruction but no sleu.
2389 __ Xori(dst, dst, 1);
2390 }
2391 }
2392 break;
2393 }
2394}
2395
2396void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2397 bool is64bit,
2398 LocationSummary* locations,
2399 Mips64Label* label) {
2400 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2401 Location rhs_location = locations->InAt(1);
2402 GpuRegister rhs_reg = ZERO;
2403 int64_t rhs_imm = 0;
2404 bool use_imm = rhs_location.IsConstant();
2405 if (use_imm) {
2406 if (is64bit) {
2407 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2408 } else {
2409 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2410 }
2411 } else {
2412 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2413 }
2414
2415 if (use_imm && rhs_imm == 0) {
2416 switch (cond) {
2417 case kCondEQ:
2418 case kCondBE: // <= 0 if zero
2419 __ Beqzc(lhs, label);
2420 break;
2421 case kCondNE:
2422 case kCondA: // > 0 if non-zero
2423 __ Bnezc(lhs, label);
2424 break;
2425 case kCondLT:
2426 __ Bltzc(lhs, label);
2427 break;
2428 case kCondGE:
2429 __ Bgezc(lhs, label);
2430 break;
2431 case kCondLE:
2432 __ Blezc(lhs, label);
2433 break;
2434 case kCondGT:
2435 __ Bgtzc(lhs, label);
2436 break;
2437 case kCondB: // always false
2438 break;
2439 case kCondAE: // always true
2440 __ Bc(label);
2441 break;
2442 }
2443 } else {
2444 if (use_imm) {
2445 rhs_reg = TMP;
2446 __ LoadConst64(rhs_reg, rhs_imm);
2447 }
2448 switch (cond) {
2449 case kCondEQ:
2450 __ Beqc(lhs, rhs_reg, label);
2451 break;
2452 case kCondNE:
2453 __ Bnec(lhs, rhs_reg, label);
2454 break;
2455 case kCondLT:
2456 __ Bltc(lhs, rhs_reg, label);
2457 break;
2458 case kCondGE:
2459 __ Bgec(lhs, rhs_reg, label);
2460 break;
2461 case kCondLE:
2462 __ Bgec(rhs_reg, lhs, label);
2463 break;
2464 case kCondGT:
2465 __ Bltc(rhs_reg, lhs, label);
2466 break;
2467 case kCondB:
2468 __ Bltuc(lhs, rhs_reg, label);
2469 break;
2470 case kCondAE:
2471 __ Bgeuc(lhs, rhs_reg, label);
2472 break;
2473 case kCondBE:
2474 __ Bgeuc(rhs_reg, lhs, label);
2475 break;
2476 case kCondA:
2477 __ Bltuc(rhs_reg, lhs, label);
2478 break;
2479 }
2480 }
2481}
2482
2483void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2484 bool gt_bias,
2485 Primitive::Type type,
2486 LocationSummary* locations,
2487 Mips64Label* label) {
2488 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2489 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2490 if (type == Primitive::kPrimFloat) {
2491 switch (cond) {
2492 case kCondEQ:
2493 __ CmpEqS(FTMP, lhs, rhs);
2494 __ Bc1nez(FTMP, label);
2495 break;
2496 case kCondNE:
2497 __ CmpEqS(FTMP, lhs, rhs);
2498 __ Bc1eqz(FTMP, label);
2499 break;
2500 case kCondLT:
2501 if (gt_bias) {
2502 __ CmpLtS(FTMP, lhs, rhs);
2503 } else {
2504 __ CmpUltS(FTMP, lhs, rhs);
2505 }
2506 __ Bc1nez(FTMP, label);
2507 break;
2508 case kCondLE:
2509 if (gt_bias) {
2510 __ CmpLeS(FTMP, lhs, rhs);
2511 } else {
2512 __ CmpUleS(FTMP, lhs, rhs);
2513 }
2514 __ Bc1nez(FTMP, label);
2515 break;
2516 case kCondGT:
2517 if (gt_bias) {
2518 __ CmpUltS(FTMP, rhs, lhs);
2519 } else {
2520 __ CmpLtS(FTMP, rhs, lhs);
2521 }
2522 __ Bc1nez(FTMP, label);
2523 break;
2524 case kCondGE:
2525 if (gt_bias) {
2526 __ CmpUleS(FTMP, rhs, lhs);
2527 } else {
2528 __ CmpLeS(FTMP, rhs, lhs);
2529 }
2530 __ Bc1nez(FTMP, label);
2531 break;
2532 default:
2533 LOG(FATAL) << "Unexpected non-floating-point condition";
2534 }
2535 } else {
2536 DCHECK_EQ(type, Primitive::kPrimDouble);
2537 switch (cond) {
2538 case kCondEQ:
2539 __ CmpEqD(FTMP, lhs, rhs);
2540 __ Bc1nez(FTMP, label);
2541 break;
2542 case kCondNE:
2543 __ CmpEqD(FTMP, lhs, rhs);
2544 __ Bc1eqz(FTMP, label);
2545 break;
2546 case kCondLT:
2547 if (gt_bias) {
2548 __ CmpLtD(FTMP, lhs, rhs);
2549 } else {
2550 __ CmpUltD(FTMP, lhs, rhs);
2551 }
2552 __ Bc1nez(FTMP, label);
2553 break;
2554 case kCondLE:
2555 if (gt_bias) {
2556 __ CmpLeD(FTMP, lhs, rhs);
2557 } else {
2558 __ CmpUleD(FTMP, lhs, rhs);
2559 }
2560 __ Bc1nez(FTMP, label);
2561 break;
2562 case kCondGT:
2563 if (gt_bias) {
2564 __ CmpUltD(FTMP, rhs, lhs);
2565 } else {
2566 __ CmpLtD(FTMP, rhs, lhs);
2567 }
2568 __ Bc1nez(FTMP, label);
2569 break;
2570 case kCondGE:
2571 if (gt_bias) {
2572 __ CmpUleD(FTMP, rhs, lhs);
2573 } else {
2574 __ CmpLeD(FTMP, rhs, lhs);
2575 }
2576 __ Bc1nez(FTMP, label);
2577 break;
2578 default:
2579 LOG(FATAL) << "Unexpected non-floating-point condition";
2580 }
2581 }
2582}
2583
Alexey Frunze4dda3372015-06-01 18:31:49 -07002584void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002585 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002586 Mips64Label* true_target,
2587 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002588 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002589
David Brazdil0debae72015-11-12 18:37:00 +00002590 if (true_target == nullptr && false_target == nullptr) {
2591 // Nothing to do. The code always falls through.
2592 return;
2593 } else if (cond->IsIntConstant()) {
2594 // Constant condition, statically compared against 1.
2595 if (cond->AsIntConstant()->IsOne()) {
2596 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002597 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002598 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002599 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002600 DCHECK(cond->AsIntConstant()->IsZero());
2601 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002602 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002603 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002604 }
David Brazdil0debae72015-11-12 18:37:00 +00002605 return;
2606 }
2607
2608 // The following code generates these patterns:
2609 // (1) true_target == nullptr && false_target != nullptr
2610 // - opposite condition true => branch to false_target
2611 // (2) true_target != nullptr && false_target == nullptr
2612 // - condition true => branch to true_target
2613 // (3) true_target != nullptr && false_target != nullptr
2614 // - condition true => branch to true_target
2615 // - branch to false_target
2616 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002617 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002618 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002619 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002620 if (true_target == nullptr) {
2621 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2622 } else {
2623 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2624 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002625 } else {
2626 // The condition instruction has not been materialized, use its inputs as
2627 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002628 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002629 Primitive::Type type = condition->InputAt(0)->GetType();
2630 LocationSummary* locations = cond->GetLocations();
2631 IfCondition if_cond = condition->GetCondition();
2632 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002633
David Brazdil0debae72015-11-12 18:37:00 +00002634 if (true_target == nullptr) {
2635 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002636 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002637 }
2638
Alexey Frunze299a9392015-12-08 16:08:02 -08002639 switch (type) {
2640 default:
2641 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2642 break;
2643 case Primitive::kPrimLong:
2644 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2645 break;
2646 case Primitive::kPrimFloat:
2647 case Primitive::kPrimDouble:
2648 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2649 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002650 }
2651 }
David Brazdil0debae72015-11-12 18:37:00 +00002652
2653 // If neither branch falls through (case 3), the conditional branch to `true_target`
2654 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2655 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002656 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002657 }
2658}
2659
2660void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002662 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002663 locations->SetInAt(0, Location::RequiresRegister());
2664 }
2665}
2666
2667void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002668 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2669 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002670 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002671 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002672 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002673 nullptr : codegen_->GetLabelOf(false_successor);
2674 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002675}
2676
2677void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2678 LocationSummary* locations = new (GetGraph()->GetArena())
2679 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002680 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002681 locations->SetInAt(0, Location::RequiresRegister());
2682 }
2683}
2684
2685void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002686 SlowPathCodeMIPS64* slow_path =
2687 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002688 GenerateTestAndBranch(deoptimize,
2689 /* condition_input_index */ 0,
2690 slow_path->GetEntryLabel(),
2691 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002692}
2693
David Brazdil74eb1b22015-12-14 11:44:01 +00002694void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2695 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2696 if (Primitive::IsFloatingPointType(select->GetType())) {
2697 locations->SetInAt(0, Location::RequiresFpuRegister());
2698 locations->SetInAt(1, Location::RequiresFpuRegister());
2699 } else {
2700 locations->SetInAt(0, Location::RequiresRegister());
2701 locations->SetInAt(1, Location::RequiresRegister());
2702 }
2703 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2704 locations->SetInAt(2, Location::RequiresRegister());
2705 }
2706 locations->SetOut(Location::SameAsFirstInput());
2707}
2708
2709void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2710 LocationSummary* locations = select->GetLocations();
2711 Mips64Label false_target;
2712 GenerateTestAndBranch(select,
2713 /* condition_input_index */ 2,
2714 /* true_target */ nullptr,
2715 &false_target);
2716 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2717 __ Bind(&false_target);
2718}
2719
David Srbecky0cf44932015-12-09 14:09:59 +00002720void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2721 new (GetGraph()->GetArena()) LocationSummary(info);
2722}
2723
2724void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyc7098ff2016-02-09 14:30:11 +00002725 codegen_->MaybeRecordNativeDebugInfo(info, info->GetDexPc());
2726}
2727
2728void CodeGeneratorMIPS64::GenerateNop() {
2729 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002730}
2731
Alexey Frunze4dda3372015-06-01 18:31:49 -07002732void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2733 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2734 LocationSummary* locations =
2735 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2736 locations->SetInAt(0, Location::RequiresRegister());
2737 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2738 locations->SetOut(Location::RequiresFpuRegister());
2739 } else {
2740 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2741 }
2742}
2743
2744void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2745 const FieldInfo& field_info) {
2746 Primitive::Type type = field_info.GetFieldType();
2747 LocationSummary* locations = instruction->GetLocations();
2748 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2749 LoadOperandType load_type = kLoadUnsignedByte;
2750 switch (type) {
2751 case Primitive::kPrimBoolean:
2752 load_type = kLoadUnsignedByte;
2753 break;
2754 case Primitive::kPrimByte:
2755 load_type = kLoadSignedByte;
2756 break;
2757 case Primitive::kPrimShort:
2758 load_type = kLoadSignedHalfword;
2759 break;
2760 case Primitive::kPrimChar:
2761 load_type = kLoadUnsignedHalfword;
2762 break;
2763 case Primitive::kPrimInt:
2764 case Primitive::kPrimFloat:
2765 load_type = kLoadWord;
2766 break;
2767 case Primitive::kPrimLong:
2768 case Primitive::kPrimDouble:
2769 load_type = kLoadDoubleword;
2770 break;
2771 case Primitive::kPrimNot:
2772 load_type = kLoadUnsignedWord;
2773 break;
2774 case Primitive::kPrimVoid:
2775 LOG(FATAL) << "Unreachable type " << type;
2776 UNREACHABLE();
2777 }
2778 if (!Primitive::IsFloatingPointType(type)) {
2779 DCHECK(locations->Out().IsRegister());
2780 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2781 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2782 } else {
2783 DCHECK(locations->Out().IsFpuRegister());
2784 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2785 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2786 }
2787
2788 codegen_->MaybeRecordImplicitNullCheck(instruction);
2789 // TODO: memory barrier?
2790}
2791
2792void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2793 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2794 LocationSummary* locations =
2795 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2796 locations->SetInAt(0, Location::RequiresRegister());
2797 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2798 locations->SetInAt(1, Location::RequiresFpuRegister());
2799 } else {
2800 locations->SetInAt(1, Location::RequiresRegister());
2801 }
2802}
2803
2804void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002805 const FieldInfo& field_info,
2806 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002807 Primitive::Type type = field_info.GetFieldType();
2808 LocationSummary* locations = instruction->GetLocations();
2809 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2810 StoreOperandType store_type = kStoreByte;
2811 switch (type) {
2812 case Primitive::kPrimBoolean:
2813 case Primitive::kPrimByte:
2814 store_type = kStoreByte;
2815 break;
2816 case Primitive::kPrimShort:
2817 case Primitive::kPrimChar:
2818 store_type = kStoreHalfword;
2819 break;
2820 case Primitive::kPrimInt:
2821 case Primitive::kPrimFloat:
2822 case Primitive::kPrimNot:
2823 store_type = kStoreWord;
2824 break;
2825 case Primitive::kPrimLong:
2826 case Primitive::kPrimDouble:
2827 store_type = kStoreDoubleword;
2828 break;
2829 case Primitive::kPrimVoid:
2830 LOG(FATAL) << "Unreachable type " << type;
2831 UNREACHABLE();
2832 }
2833 if (!Primitive::IsFloatingPointType(type)) {
2834 DCHECK(locations->InAt(1).IsRegister());
2835 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2836 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2837 } else {
2838 DCHECK(locations->InAt(1).IsFpuRegister());
2839 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2840 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2841 }
2842
2843 codegen_->MaybeRecordImplicitNullCheck(instruction);
2844 // TODO: memory barriers?
2845 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2846 DCHECK(locations->InAt(1).IsRegister());
2847 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002848 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002849 }
2850}
2851
2852void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2853 HandleFieldGet(instruction, instruction->GetFieldInfo());
2854}
2855
2856void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2857 HandleFieldGet(instruction, instruction->GetFieldInfo());
2858}
2859
2860void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2861 HandleFieldSet(instruction, instruction->GetFieldInfo());
2862}
2863
2864void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002865 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002866}
2867
2868void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2869 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002870 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002871 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2872 locations->SetInAt(0, Location::RequiresRegister());
2873 locations->SetInAt(1, Location::RequiresRegister());
2874 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002875 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002876 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2877}
2878
2879void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2880 LocationSummary* locations = instruction->GetLocations();
2881 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2882 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2883 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2884
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002885 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002886
2887 // Return 0 if `obj` is null.
2888 // TODO: Avoid this check if we know `obj` is not null.
2889 __ Move(out, ZERO);
2890 __ Beqzc(obj, &done);
2891
2892 // Compare the class of `obj` with `cls`.
2893 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002894 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002895 // Classes must be equal for the instanceof to succeed.
2896 __ Xor(out, out, cls);
2897 __ Sltiu(out, out, 1);
2898 } else {
2899 // If the classes are not equal, we go into a slow path.
2900 DCHECK(locations->OnlyCallsOnSlowPath());
2901 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002902 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002903 codegen_->AddSlowPath(slow_path);
2904 __ Bnec(out, cls, slow_path->GetEntryLabel());
2905 __ LoadConst32(out, 1);
2906 __ Bind(slow_path->GetExitLabel());
2907 }
2908
2909 __ Bind(&done);
2910}
2911
2912void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2913 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2914 locations->SetOut(Location::ConstantLocation(constant));
2915}
2916
2917void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2918 // Will be generated at use site.
2919}
2920
2921void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2922 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2923 locations->SetOut(Location::ConstantLocation(constant));
2924}
2925
2926void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2927 // Will be generated at use site.
2928}
2929
Calin Juravle175dc732015-08-25 15:42:32 +01002930void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2931 // The trampoline uses the same calling convention as dex calling conventions,
2932 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2933 // the method_idx.
2934 HandleInvoke(invoke);
2935}
2936
2937void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2938 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2939}
2940
Alexey Frunze4dda3372015-06-01 18:31:49 -07002941void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2942 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2943 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2944}
2945
2946void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2947 HandleInvoke(invoke);
2948 // The register T0 is required to be used for the hidden argument in
2949 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2950 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2951}
2952
2953void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2954 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2955 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2956 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2957 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2958 Location receiver = invoke->GetLocations()->InAt(0);
2959 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02002960 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002961
2962 // Set the hidden argument.
2963 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2964 invoke->GetDexMethodIndex());
2965
2966 // temp = object->GetClass();
2967 if (receiver.IsStackSlot()) {
2968 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2969 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2970 } else {
2971 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2972 }
2973 codegen_->MaybeRecordImplicitNullCheck(invoke);
2974 // temp = temp->GetImtEntryAt(method_offset);
2975 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2976 // T9 = temp->GetEntryPoint();
2977 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2978 // T9();
2979 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002980 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002981 DCHECK(!codegen_->IsLeafMethod());
2982 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2983}
2984
2985void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002986 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2987 if (intrinsic.TryDispatch(invoke)) {
2988 return;
2989 }
2990
Alexey Frunze4dda3372015-06-01 18:31:49 -07002991 HandleInvoke(invoke);
2992}
2993
2994void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002995 // Explicit clinit checks triggered by static invokes must have been pruned by
2996 // art::PrepareForRegisterAllocation.
2997 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002998
Chris Larsen3039e382015-08-26 07:54:08 -07002999 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3000 if (intrinsic.TryDispatch(invoke)) {
3001 return;
3002 }
3003
Alexey Frunze4dda3372015-06-01 18:31:49 -07003004 HandleInvoke(invoke);
3005
3006 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
3007 // clobbering somewhere else, reduce further register pressure by avoiding
3008 // allocation of a register for the current method pointer like on x86 baseline.
3009 // TODO: remove this once all the issues with register saving/restoring are
3010 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003011 if (invoke->HasCurrentMethodInput()) {
3012 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00003013 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003014 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003015 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003016 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003017 }
3018}
3019
Chris Larsen3039e382015-08-26 07:54:08 -07003020static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003021 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003022 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3023 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003024 return true;
3025 }
3026 return false;
3027}
3028
Vladimir Markodc151b22015-10-15 18:02:30 +01003029HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3030 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3031 MethodReference target_method ATTRIBUTE_UNUSED) {
3032 switch (desired_dispatch_info.method_load_kind) {
3033 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3034 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3035 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3036 return HInvokeStaticOrDirect::DispatchInfo {
3037 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3038 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3039 0u,
3040 0u
3041 };
3042 default:
3043 break;
3044 }
3045 switch (desired_dispatch_info.code_ptr_location) {
3046 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3047 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3048 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3049 return HInvokeStaticOrDirect::DispatchInfo {
3050 desired_dispatch_info.method_load_kind,
3051 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3052 desired_dispatch_info.method_load_data,
3053 0u
3054 };
3055 default:
3056 return desired_dispatch_info;
3057 }
3058}
3059
Alexey Frunze4dda3372015-06-01 18:31:49 -07003060void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3061 // All registers are assumed to be correctly set up per the calling convention.
3062
Vladimir Marko58155012015-08-19 12:49:41 +00003063 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3064 switch (invoke->GetMethodLoadKind()) {
3065 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3066 // temp = thread->string_init_entrypoint
3067 __ LoadFromOffset(kLoadDoubleword,
3068 temp.AsRegister<GpuRegister>(),
3069 TR,
3070 invoke->GetStringInitOffset());
3071 break;
3072 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003073 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003074 break;
3075 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3076 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3077 break;
3078 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003079 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003080 // TODO: Implement these types.
3081 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3082 LOG(FATAL) << "Unsupported";
3083 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003084 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003085 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003086 GpuRegister reg = temp.AsRegister<GpuRegister>();
3087 GpuRegister method_reg;
3088 if (current_method.IsRegister()) {
3089 method_reg = current_method.AsRegister<GpuRegister>();
3090 } else {
3091 // TODO: use the appropriate DCHECK() here if possible.
3092 // DCHECK(invoke->GetLocations()->Intrinsified());
3093 DCHECK(!current_method.IsValid());
3094 method_reg = reg;
3095 __ Ld(reg, SP, kCurrentMethodStackOffset);
3096 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003097
Vladimir Marko58155012015-08-19 12:49:41 +00003098 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003099 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003100 reg,
3101 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003102 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003103 // temp = temp[index_in_cache]
3104 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3105 __ LoadFromOffset(kLoadDoubleword,
3106 reg,
3107 reg,
3108 CodeGenerator::GetCachePointerOffset(index_in_cache));
3109 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003110 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003111 }
3112
Vladimir Marko58155012015-08-19 12:49:41 +00003113 switch (invoke->GetCodePtrLocation()) {
3114 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003115 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003116 break;
3117 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3118 // LR = invoke->GetDirectCodePtr();
3119 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3120 // LR()
3121 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003122 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003123 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003124 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003125 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3126 // TODO: Implement these types.
3127 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3128 LOG(FATAL) << "Unsupported";
3129 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003130 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3131 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3132 __ LoadFromOffset(kLoadDoubleword,
3133 T9,
3134 callee_method.AsRegister<GpuRegister>(),
3135 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003136 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003137 // T9()
3138 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003139 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003140 break;
3141 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003142 DCHECK(!IsLeafMethod());
3143}
3144
3145void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003146 // Explicit clinit checks triggered by static invokes must have been pruned by
3147 // art::PrepareForRegisterAllocation.
3148 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003149
3150 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3151 return;
3152 }
3153
3154 LocationSummary* locations = invoke->GetLocations();
3155 codegen_->GenerateStaticOrDirectCall(invoke,
3156 locations->HasTemps()
3157 ? locations->GetTemp(0)
3158 : Location::NoLocation());
3159 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3160}
3161
Alexey Frunze53afca12015-11-05 16:34:23 -08003162void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003163 // Use the calling convention instead of the location of the receiver, as
3164 // intrinsics may have put the receiver in a different register. In the intrinsics
3165 // slow path, the arguments have been moved to the right place, so here we are
3166 // guaranteed that the receiver is the first register of the calling convention.
3167 InvokeDexCallingConvention calling_convention;
3168 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3169
Alexey Frunze53afca12015-11-05 16:34:23 -08003170 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003171 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3172 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3173 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003174 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003175
3176 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003177 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003178 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003179 // temp = temp->GetMethodAt(method_offset);
3180 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3181 // T9 = temp->GetEntryPoint();
3182 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3183 // T9();
3184 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003185 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003186}
3187
3188void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3189 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3190 return;
3191 }
3192
3193 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003194 DCHECK(!codegen_->IsLeafMethod());
3195 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3196}
3197
3198void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003199 InvokeRuntimeCallingConvention calling_convention;
3200 CodeGenerator::CreateLoadClassLocationSummary(
3201 cls,
3202 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003203 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003204}
3205
3206void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3207 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003208 if (cls->NeedsAccessCheck()) {
3209 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3210 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3211 cls,
3212 cls->GetDexPc(),
3213 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003214 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003215 return;
3216 }
3217
3218 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3219 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3220 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003221 DCHECK(!cls->CanCallRuntime());
3222 DCHECK(!cls->MustGenerateClinitCheck());
3223 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3224 ArtMethod::DeclaringClassOffset().Int32Value());
3225 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003226 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3227 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003228 __ LoadFromOffset(
3229 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003230 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003231 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3232 DCHECK(cls->CanCallRuntime());
3233 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3234 cls,
3235 cls,
3236 cls->GetDexPc(),
3237 cls->MustGenerateClinitCheck());
3238 codegen_->AddSlowPath(slow_path);
3239 if (!cls->IsInDexCache()) {
3240 __ Beqzc(out, slow_path->GetEntryLabel());
3241 }
3242 if (cls->MustGenerateClinitCheck()) {
3243 GenerateClassInitializationCheck(slow_path, out);
3244 } else {
3245 __ Bind(slow_path->GetExitLabel());
3246 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003247 }
3248 }
3249}
3250
David Brazdilcb1c0552015-08-04 16:22:25 +01003251static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003252 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003253}
3254
Alexey Frunze4dda3372015-06-01 18:31:49 -07003255void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3256 LocationSummary* locations =
3257 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3258 locations->SetOut(Location::RequiresRegister());
3259}
3260
3261void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3262 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003263 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3264}
3265
3266void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3267 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3268}
3269
3270void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3271 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003272}
3273
3274void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3275 load->SetLocations(nullptr);
3276}
3277
3278void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3279 // Nothing to do, this is driven by the code generator.
3280}
3281
3282void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00003283 LocationSummary::CallKind call_kind = load->IsInDexCache()
3284 ? LocationSummary::kNoCall
3285 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003287 locations->SetInAt(0, Location::RequiresRegister());
3288 locations->SetOut(Location::RequiresRegister());
3289}
3290
3291void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003292 LocationSummary* locations = load->GetLocations();
3293 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3294 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3295 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3296 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003297 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003298 __ LoadFromOffset(
3299 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003300 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003301
3302 if (!load->IsInDexCache()) {
3303 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3304 codegen_->AddSlowPath(slow_path);
3305 __ Beqzc(out, slow_path->GetEntryLabel());
3306 __ Bind(slow_path->GetExitLabel());
3307 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003308}
3309
3310void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3311 local->SetLocations(nullptr);
3312}
3313
3314void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3315 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3316}
3317
3318void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3319 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3320 locations->SetOut(Location::ConstantLocation(constant));
3321}
3322
3323void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3324 // Will be generated at use site.
3325}
3326
3327void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3328 LocationSummary* locations =
3329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3330 InvokeRuntimeCallingConvention calling_convention;
3331 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3332}
3333
3334void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3335 codegen_->InvokeRuntime(instruction->IsEnter()
3336 ? QUICK_ENTRY_POINT(pLockObject)
3337 : QUICK_ENTRY_POINT(pUnlockObject),
3338 instruction,
3339 instruction->GetDexPc(),
3340 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003341 if (instruction->IsEnter()) {
3342 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3343 } else {
3344 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3345 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003346}
3347
3348void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3349 LocationSummary* locations =
3350 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3351 switch (mul->GetResultType()) {
3352 case Primitive::kPrimInt:
3353 case Primitive::kPrimLong:
3354 locations->SetInAt(0, Location::RequiresRegister());
3355 locations->SetInAt(1, Location::RequiresRegister());
3356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3357 break;
3358
3359 case Primitive::kPrimFloat:
3360 case Primitive::kPrimDouble:
3361 locations->SetInAt(0, Location::RequiresFpuRegister());
3362 locations->SetInAt(1, Location::RequiresFpuRegister());
3363 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3364 break;
3365
3366 default:
3367 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3368 }
3369}
3370
3371void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3372 Primitive::Type type = instruction->GetType();
3373 LocationSummary* locations = instruction->GetLocations();
3374
3375 switch (type) {
3376 case Primitive::kPrimInt:
3377 case Primitive::kPrimLong: {
3378 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3379 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3380 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3381 if (type == Primitive::kPrimInt)
3382 __ MulR6(dst, lhs, rhs);
3383 else
3384 __ Dmul(dst, lhs, rhs);
3385 break;
3386 }
3387 case Primitive::kPrimFloat:
3388 case Primitive::kPrimDouble: {
3389 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3390 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3391 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3392 if (type == Primitive::kPrimFloat)
3393 __ MulS(dst, lhs, rhs);
3394 else
3395 __ MulD(dst, lhs, rhs);
3396 break;
3397 }
3398 default:
3399 LOG(FATAL) << "Unexpected mul type " << type;
3400 }
3401}
3402
3403void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3404 LocationSummary* locations =
3405 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3406 switch (neg->GetResultType()) {
3407 case Primitive::kPrimInt:
3408 case Primitive::kPrimLong:
3409 locations->SetInAt(0, Location::RequiresRegister());
3410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3411 break;
3412
3413 case Primitive::kPrimFloat:
3414 case Primitive::kPrimDouble:
3415 locations->SetInAt(0, Location::RequiresFpuRegister());
3416 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3417 break;
3418
3419 default:
3420 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3421 }
3422}
3423
3424void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3425 Primitive::Type type = instruction->GetType();
3426 LocationSummary* locations = instruction->GetLocations();
3427
3428 switch (type) {
3429 case Primitive::kPrimInt:
3430 case Primitive::kPrimLong: {
3431 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3432 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3433 if (type == Primitive::kPrimInt)
3434 __ Subu(dst, ZERO, src);
3435 else
3436 __ Dsubu(dst, ZERO, src);
3437 break;
3438 }
3439 case Primitive::kPrimFloat:
3440 case Primitive::kPrimDouble: {
3441 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3442 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3443 if (type == Primitive::kPrimFloat)
3444 __ NegS(dst, src);
3445 else
3446 __ NegD(dst, src);
3447 break;
3448 }
3449 default:
3450 LOG(FATAL) << "Unexpected neg type " << type;
3451 }
3452}
3453
3454void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3455 LocationSummary* locations =
3456 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3457 InvokeRuntimeCallingConvention calling_convention;
3458 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3459 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3460 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3461 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3462}
3463
3464void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3465 LocationSummary* locations = instruction->GetLocations();
3466 // Move an uint16_t value to a register.
3467 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003468 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3469 instruction,
3470 instruction->GetDexPc(),
3471 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003472 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3473}
3474
3475void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3476 LocationSummary* locations =
3477 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3478 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003479 if (instruction->IsStringAlloc()) {
3480 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3481 } else {
3482 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3483 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3484 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003485 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3486}
3487
3488void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003489 if (instruction->IsStringAlloc()) {
3490 // String is allocated through StringFactory. Call NewEmptyString entry point.
3491 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003492 MemberOffset code_offset =
3493 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003494 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3495 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3496 __ Jalr(T9);
3497 __ Nop();
3498 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3499 } else {
3500 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3501 instruction,
3502 instruction->GetDexPc(),
3503 nullptr);
3504 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3505 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003506}
3507
3508void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3509 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3510 locations->SetInAt(0, Location::RequiresRegister());
3511 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3512}
3513
3514void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3515 Primitive::Type type = instruction->GetType();
3516 LocationSummary* locations = instruction->GetLocations();
3517
3518 switch (type) {
3519 case Primitive::kPrimInt:
3520 case Primitive::kPrimLong: {
3521 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3522 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3523 __ Nor(dst, src, ZERO);
3524 break;
3525 }
3526
3527 default:
3528 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3529 }
3530}
3531
3532void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3534 locations->SetInAt(0, Location::RequiresRegister());
3535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3536}
3537
3538void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3539 LocationSummary* locations = instruction->GetLocations();
3540 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3541 locations->InAt(0).AsRegister<GpuRegister>(),
3542 1);
3543}
3544
3545void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003546 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3547 ? LocationSummary::kCallOnSlowPath
3548 : LocationSummary::kNoCall;
3549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003550 locations->SetInAt(0, Location::RequiresRegister());
3551 if (instruction->HasUses()) {
3552 locations->SetOut(Location::SameAsFirstInput());
3553 }
3554}
3555
3556void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3557 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3558 return;
3559 }
3560 Location obj = instruction->GetLocations()->InAt(0);
3561
3562 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3563 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3564}
3565
3566void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3567 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3568 codegen_->AddSlowPath(slow_path);
3569
3570 Location obj = instruction->GetLocations()->InAt(0);
3571
3572 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3573}
3574
3575void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003576 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003577 GenerateImplicitNullCheck(instruction);
3578 } else {
3579 GenerateExplicitNullCheck(instruction);
3580 }
3581}
3582
3583void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3584 HandleBinaryOp(instruction);
3585}
3586
3587void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3588 HandleBinaryOp(instruction);
3589}
3590
3591void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3592 LOG(FATAL) << "Unreachable";
3593}
3594
3595void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3596 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3597}
3598
3599void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3600 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3601 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3602 if (location.IsStackSlot()) {
3603 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3604 } else if (location.IsDoubleStackSlot()) {
3605 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3606 }
3607 locations->SetOut(location);
3608}
3609
3610void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3611 ATTRIBUTE_UNUSED) {
3612 // Nothing to do, the parameter is already at its location.
3613}
3614
3615void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3616 LocationSummary* locations =
3617 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3618 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3619}
3620
3621void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3622 ATTRIBUTE_UNUSED) {
3623 // Nothing to do, the method is already at its location.
3624}
3625
3626void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3627 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3628 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3629 locations->SetInAt(i, Location::Any());
3630 }
3631 locations->SetOut(Location::Any());
3632}
3633
3634void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3635 LOG(FATAL) << "Unreachable";
3636}
3637
3638void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3639 Primitive::Type type = rem->GetResultType();
3640 LocationSummary::CallKind call_kind =
3641 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3643
3644 switch (type) {
3645 case Primitive::kPrimInt:
3646 case Primitive::kPrimLong:
3647 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003648 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3650 break;
3651
3652 case Primitive::kPrimFloat:
3653 case Primitive::kPrimDouble: {
3654 InvokeRuntimeCallingConvention calling_convention;
3655 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3656 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3657 locations->SetOut(calling_convention.GetReturnLocation(type));
3658 break;
3659 }
3660
3661 default:
3662 LOG(FATAL) << "Unexpected rem type " << type;
3663 }
3664}
3665
3666void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3667 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003668
3669 switch (type) {
3670 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003671 case Primitive::kPrimLong:
3672 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003673 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003674
3675 case Primitive::kPrimFloat:
3676 case Primitive::kPrimDouble: {
3677 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3678 : QUICK_ENTRY_POINT(pFmod);
3679 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003680 if (type == Primitive::kPrimFloat) {
3681 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3682 } else {
3683 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3684 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003685 break;
3686 }
3687 default:
3688 LOG(FATAL) << "Unexpected rem type " << type;
3689 }
3690}
3691
3692void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3693 memory_barrier->SetLocations(nullptr);
3694}
3695
3696void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3697 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3698}
3699
3700void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3701 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3702 Primitive::Type return_type = ret->InputAt(0)->GetType();
3703 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3704}
3705
3706void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3707 codegen_->GenerateFrameExit();
3708}
3709
3710void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3711 ret->SetLocations(nullptr);
3712}
3713
3714void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3715 codegen_->GenerateFrameExit();
3716}
3717
Alexey Frunze92d90602015-12-18 18:16:36 -08003718void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3719 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003720}
3721
Alexey Frunze92d90602015-12-18 18:16:36 -08003722void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3723 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003724}
3725
Alexey Frunze4dda3372015-06-01 18:31:49 -07003726void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3727 HandleShift(shl);
3728}
3729
3730void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3731 HandleShift(shl);
3732}
3733
3734void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3735 HandleShift(shr);
3736}
3737
3738void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3739 HandleShift(shr);
3740}
3741
3742void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3744 Primitive::Type field_type = store->InputAt(1)->GetType();
3745 switch (field_type) {
3746 case Primitive::kPrimNot:
3747 case Primitive::kPrimBoolean:
3748 case Primitive::kPrimByte:
3749 case Primitive::kPrimChar:
3750 case Primitive::kPrimShort:
3751 case Primitive::kPrimInt:
3752 case Primitive::kPrimFloat:
3753 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3754 break;
3755
3756 case Primitive::kPrimLong:
3757 case Primitive::kPrimDouble:
3758 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3759 break;
3760
3761 default:
3762 LOG(FATAL) << "Unimplemented local type " << field_type;
3763 }
3764}
3765
3766void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3767}
3768
3769void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3770 HandleBinaryOp(instruction);
3771}
3772
3773void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3774 HandleBinaryOp(instruction);
3775}
3776
3777void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3778 HandleFieldGet(instruction, instruction->GetFieldInfo());
3779}
3780
3781void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3782 HandleFieldGet(instruction, instruction->GetFieldInfo());
3783}
3784
3785void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3786 HandleFieldSet(instruction, instruction->GetFieldInfo());
3787}
3788
3789void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003790 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003791}
3792
Calin Juravlee460d1d2015-09-29 04:52:17 +01003793void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3794 HUnresolvedInstanceFieldGet* instruction) {
3795 FieldAccessCallingConventionMIPS64 calling_convention;
3796 codegen_->CreateUnresolvedFieldLocationSummary(
3797 instruction, instruction->GetFieldType(), calling_convention);
3798}
3799
3800void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3801 HUnresolvedInstanceFieldGet* instruction) {
3802 FieldAccessCallingConventionMIPS64 calling_convention;
3803 codegen_->GenerateUnresolvedFieldAccess(instruction,
3804 instruction->GetFieldType(),
3805 instruction->GetFieldIndex(),
3806 instruction->GetDexPc(),
3807 calling_convention);
3808}
3809
3810void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3811 HUnresolvedInstanceFieldSet* instruction) {
3812 FieldAccessCallingConventionMIPS64 calling_convention;
3813 codegen_->CreateUnresolvedFieldLocationSummary(
3814 instruction, instruction->GetFieldType(), calling_convention);
3815}
3816
3817void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3818 HUnresolvedInstanceFieldSet* instruction) {
3819 FieldAccessCallingConventionMIPS64 calling_convention;
3820 codegen_->GenerateUnresolvedFieldAccess(instruction,
3821 instruction->GetFieldType(),
3822 instruction->GetFieldIndex(),
3823 instruction->GetDexPc(),
3824 calling_convention);
3825}
3826
3827void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3828 HUnresolvedStaticFieldGet* instruction) {
3829 FieldAccessCallingConventionMIPS64 calling_convention;
3830 codegen_->CreateUnresolvedFieldLocationSummary(
3831 instruction, instruction->GetFieldType(), calling_convention);
3832}
3833
3834void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3835 HUnresolvedStaticFieldGet* instruction) {
3836 FieldAccessCallingConventionMIPS64 calling_convention;
3837 codegen_->GenerateUnresolvedFieldAccess(instruction,
3838 instruction->GetFieldType(),
3839 instruction->GetFieldIndex(),
3840 instruction->GetDexPc(),
3841 calling_convention);
3842}
3843
3844void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3845 HUnresolvedStaticFieldSet* instruction) {
3846 FieldAccessCallingConventionMIPS64 calling_convention;
3847 codegen_->CreateUnresolvedFieldLocationSummary(
3848 instruction, instruction->GetFieldType(), calling_convention);
3849}
3850
3851void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3852 HUnresolvedStaticFieldSet* instruction) {
3853 FieldAccessCallingConventionMIPS64 calling_convention;
3854 codegen_->GenerateUnresolvedFieldAccess(instruction,
3855 instruction->GetFieldType(),
3856 instruction->GetFieldIndex(),
3857 instruction->GetDexPc(),
3858 calling_convention);
3859}
3860
Alexey Frunze4dda3372015-06-01 18:31:49 -07003861void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3862 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3863}
3864
3865void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3866 HBasicBlock* block = instruction->GetBlock();
3867 if (block->GetLoopInformation() != nullptr) {
3868 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3869 // The back edge will generate the suspend check.
3870 return;
3871 }
3872 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3873 // The goto will generate the suspend check.
3874 return;
3875 }
3876 GenerateSuspendCheck(instruction, nullptr);
3877}
3878
Alexey Frunze4dda3372015-06-01 18:31:49 -07003879void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3880 LocationSummary* locations =
3881 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3882 InvokeRuntimeCallingConvention calling_convention;
3883 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3884}
3885
3886void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3887 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3888 instruction,
3889 instruction->GetDexPc(),
3890 nullptr);
3891 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3892}
3893
3894void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3895 Primitive::Type input_type = conversion->GetInputType();
3896 Primitive::Type result_type = conversion->GetResultType();
3897 DCHECK_NE(input_type, result_type);
3898
3899 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3900 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3901 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3902 }
3903
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003904 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3905
3906 if (Primitive::IsFloatingPointType(input_type)) {
3907 locations->SetInAt(0, Location::RequiresFpuRegister());
3908 } else {
3909 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003910 }
3911
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003912 if (Primitive::IsFloatingPointType(result_type)) {
3913 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003914 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003915 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003916 }
3917}
3918
3919void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3920 LocationSummary* locations = conversion->GetLocations();
3921 Primitive::Type result_type = conversion->GetResultType();
3922 Primitive::Type input_type = conversion->GetInputType();
3923
3924 DCHECK_NE(input_type, result_type);
3925
3926 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3927 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3928 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3929
3930 switch (result_type) {
3931 case Primitive::kPrimChar:
3932 __ Andi(dst, src, 0xFFFF);
3933 break;
3934 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003935 if (input_type == Primitive::kPrimLong) {
3936 // Type conversion from long to types narrower than int is a result of code
3937 // transformations. To avoid unpredictable results for SEB and SEH, we first
3938 // need to sign-extend the low 32-bit value into bits 32 through 63.
3939 __ Sll(dst, src, 0);
3940 __ Seb(dst, dst);
3941 } else {
3942 __ Seb(dst, src);
3943 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003944 break;
3945 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003946 if (input_type == Primitive::kPrimLong) {
3947 // Type conversion from long to types narrower than int is a result of code
3948 // transformations. To avoid unpredictable results for SEB and SEH, we first
3949 // need to sign-extend the low 32-bit value into bits 32 through 63.
3950 __ Sll(dst, src, 0);
3951 __ Seh(dst, dst);
3952 } else {
3953 __ Seh(dst, src);
3954 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003955 break;
3956 case Primitive::kPrimInt:
3957 case Primitive::kPrimLong:
3958 // Sign-extend 32-bit int into bits 32 through 63 for
3959 // int-to-long and long-to-int conversions
3960 __ Sll(dst, src, 0);
3961 break;
3962
3963 default:
3964 LOG(FATAL) << "Unexpected type conversion from " << input_type
3965 << " to " << result_type;
3966 }
3967 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003968 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3969 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3970 if (input_type == Primitive::kPrimLong) {
3971 __ Dmtc1(src, FTMP);
3972 if (result_type == Primitive::kPrimFloat) {
3973 __ Cvtsl(dst, FTMP);
3974 } else {
3975 __ Cvtdl(dst, FTMP);
3976 }
3977 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003978 __ Mtc1(src, FTMP);
3979 if (result_type == Primitive::kPrimFloat) {
3980 __ Cvtsw(dst, FTMP);
3981 } else {
3982 __ Cvtdw(dst, FTMP);
3983 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003984 }
3985 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3986 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003987 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3988 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3989 Mips64Label truncate;
3990 Mips64Label done;
3991
3992 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3993 // value when the input is either a NaN or is outside of the range of the output type
3994 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3995 // the same result.
3996 //
3997 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3998 // value of the output type if the input is outside of the range after the truncation or
3999 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4000 // results. This matches the desired float/double-to-int/long conversion exactly.
4001 //
4002 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4003 //
4004 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4005 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4006 // even though it must be NAN2008=1 on R6.
4007 //
4008 // The code takes care of the different behaviors by first comparing the input to the
4009 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4010 // If the input is greater than or equal to the minimum, it procedes to the truncate
4011 // instruction, which will handle such an input the same way irrespective of NAN2008.
4012 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4013 // in order to return either zero or the minimum value.
4014 //
4015 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4016 // truncate instruction for MIPS64R6.
4017 if (input_type == Primitive::kPrimFloat) {
4018 uint32_t min_val = (result_type == Primitive::kPrimLong)
4019 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4020 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4021 __ LoadConst32(TMP, min_val);
4022 __ Mtc1(TMP, FTMP);
4023 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004024 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004025 uint64_t min_val = (result_type == Primitive::kPrimLong)
4026 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4027 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4028 __ LoadConst64(TMP, min_val);
4029 __ Dmtc1(TMP, FTMP);
4030 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004031 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004032
4033 __ Bc1nez(FTMP, &truncate);
4034
4035 if (input_type == Primitive::kPrimFloat) {
4036 __ CmpEqS(FTMP, src, src);
4037 } else {
4038 __ CmpEqD(FTMP, src, src);
4039 }
4040 if (result_type == Primitive::kPrimLong) {
4041 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4042 } else {
4043 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4044 }
4045 __ Mfc1(TMP, FTMP);
4046 __ And(dst, dst, TMP);
4047
4048 __ Bc(&done);
4049
4050 __ Bind(&truncate);
4051
4052 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004053 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004054 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004055 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004056 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004057 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004058 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004059 } else {
4060 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004061 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004062 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004063 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004064 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004065 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004066 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004067
4068 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004069 } else if (Primitive::IsFloatingPointType(result_type) &&
4070 Primitive::IsFloatingPointType(input_type)) {
4071 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4072 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4073 if (result_type == Primitive::kPrimFloat) {
4074 __ Cvtsd(dst, src);
4075 } else {
4076 __ Cvtds(dst, src);
4077 }
4078 } else {
4079 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4080 << " to " << result_type;
4081 }
4082}
4083
4084void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4085 HandleShift(ushr);
4086}
4087
4088void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4089 HandleShift(ushr);
4090}
4091
4092void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4093 HandleBinaryOp(instruction);
4094}
4095
4096void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4097 HandleBinaryOp(instruction);
4098}
4099
4100void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4101 // Nothing to do, this should be removed during prepare for register allocator.
4102 LOG(FATAL) << "Unreachable";
4103}
4104
4105void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4106 // Nothing to do, this should be removed during prepare for register allocator.
4107 LOG(FATAL) << "Unreachable";
4108}
4109
4110void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004111 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004112}
4113
4114void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004115 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004116}
4117
4118void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004119 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004120}
4121
4122void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004123 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004124}
4125
4126void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004127 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004128}
4129
4130void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004131 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004132}
4133
4134void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004135 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004136}
4137
4138void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004139 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004140}
4141
4142void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004143 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004144}
4145
4146void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004147 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004148}
4149
4150void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004151 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004152}
4153
4154void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004155 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004156}
4157
Aart Bike9f37602015-10-09 11:15:55 -07004158void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004159 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004160}
4161
4162void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004163 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004164}
4165
4166void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004167 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004168}
4169
4170void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004171 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004172}
4173
4174void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004175 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004176}
4177
4178void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004179 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004180}
4181
4182void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004183 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004184}
4185
4186void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004187 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004188}
4189
Mark Mendellfe57faa2015-09-18 09:26:15 -04004190// Simple implementation of packed switch - generate cascaded compare/jumps.
4191void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4192 LocationSummary* locations =
4193 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4194 locations->SetInAt(0, Location::RequiresRegister());
4195}
4196
4197void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4198 int32_t lower_bound = switch_instr->GetStartValue();
4199 int32_t num_entries = switch_instr->GetNumEntries();
4200 LocationSummary* locations = switch_instr->GetLocations();
4201 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4202 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4203
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004204 // Create a set of compare/jumps.
4205 GpuRegister temp_reg = TMP;
4206 if (IsInt<16>(-lower_bound)) {
4207 __ Addiu(temp_reg, value_reg, -lower_bound);
4208 } else {
4209 __ LoadConst32(AT, -lower_bound);
4210 __ Addu(temp_reg, value_reg, AT);
4211 }
4212 // Jump to default if index is negative
4213 // Note: We don't check the case that index is positive while value < lower_bound, because in
4214 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4215 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4216
Mark Mendellfe57faa2015-09-18 09:26:15 -04004217 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004218 // Jump to successors[0] if value == lower_bound.
4219 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4220 int32_t last_index = 0;
4221 for (; num_entries - last_index > 2; last_index += 2) {
4222 __ Addiu(temp_reg, temp_reg, -2);
4223 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4224 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4225 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4226 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4227 }
4228 if (num_entries - last_index == 2) {
4229 // The last missing case_value.
4230 __ Addiu(temp_reg, temp_reg, -1);
4231 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004232 }
4233
4234 // And the default for any other value.
4235 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004236 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004237 }
4238}
4239
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004240void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4241 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4242}
4243
4244void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4245 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4246}
4247
Alexey Frunze4dda3372015-06-01 18:31:49 -07004248} // namespace mips64
4249} // namespace art