blob: 990bbcc85b1f365ba1070e28073c04dbf3817704 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
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_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Alexey Frunzee3fb2452016-05-10 16:08:05 -070043// We'll maximize the range of a single load instruction for dex cache array accesses
44// by aligning offset -32768 with the offset of the first used element.
45static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
46
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020047Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
102 if (calling_convention.GetRegisterAt(gp_index) == A1) {
103 gp_index_++; // Skip A1, and use A2_A3 instead.
104 gp_index++;
105 }
106 Register low_even = calling_convention.GetRegisterAt(gp_index);
107 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
108 DCHECK_EQ(low_even + 1, high_odd);
109 next_location = Location::RegisterPairLocation(low_even, high_odd);
110 } else {
111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
112 next_location = Location::DoubleStackSlot(stack_offset);
113 }
114 break;
115 }
116
117 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
118 // will take up the even/odd pair, while floats are stored in even regs only.
119 // On 64 bit FPU, both double and float are stored in even registers only.
120 case Primitive::kPrimFloat:
121 case Primitive::kPrimDouble: {
122 uint32_t float_index = float_index_++;
123 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
124 next_location = Location::FpuRegisterLocation(
125 calling_convention.GetFpuRegisterAt(float_index));
126 } else {
127 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
128 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
129 : Location::StackSlot(stack_offset);
130 }
131 break;
132 }
133
134 case Primitive::kPrimVoid:
135 LOG(FATAL) << "Unexpected parameter type " << type;
136 break;
137 }
138
139 // Space on the stack is reserved for all arguments.
140 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
141
142 return next_location;
143}
144
145Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
146 return MipsReturnLocation(type);
147}
148
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100149// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
150#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700151#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200152
153class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
154 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000155 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200156
157 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
158 LocationSummary* locations = instruction_->GetLocations();
159 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
160 __ Bind(GetEntryLabel());
161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
167 InvokeRuntimeCallingConvention calling_convention;
168 codegen->EmitParallelMoves(locations->InAt(0),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
170 Primitive::kPrimInt,
171 locations->InAt(1),
172 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
173 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100174 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
175 ? kQuickThrowStringBounds
176 : kQuickThrowArrayBounds;
177 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100178 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200179 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
180 }
181
182 bool IsFatal() const OVERRIDE { return true; }
183
184 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
185
186 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
188};
189
190class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
191 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000192 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193
194 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
196 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100197 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200198 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
199 }
200
201 bool IsFatal() const OVERRIDE { return true; }
202
203 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
204
205 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
207};
208
209class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
210 public:
211 LoadClassSlowPathMIPS(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000215 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = at_->GetLocations();
221 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
222
223 __ Bind(GetEntryLabel());
224 SaveLiveRegisters(codegen, locations);
225
226 InvokeRuntimeCallingConvention calling_convention;
227 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
228
Serban Constantinescufca16662016-07-14 09:21:59 +0100229 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
230 : kQuickInitializeType;
231 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 if (do_clinit_) {
233 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
234 } else {
235 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
236 }
237
238 // Move the class to the desired location.
239 Location out = locations->Out();
240 if (out.IsValid()) {
241 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
242 Primitive::Type type = at_->GetType();
243 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
244 }
245
246 RestoreLiveRegisters(codegen, locations);
247 __ B(GetExitLabel());
248 }
249
250 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
251
252 private:
253 // The class this slow path will load.
254 HLoadClass* const cls_;
255
256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
267};
268
269class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
270 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000271 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200272
273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
274 LocationSummary* locations = instruction_->GetLocations();
275 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
276 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
277
278 __ Bind(GetEntryLabel());
279 SaveLiveRegisters(codegen, locations);
280
281 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000282 HLoadString* load = instruction_->AsLoadString();
283 const uint32_t string_index = load->GetStringIndex();
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufca16662016-07-14 09:21:59 +0100285 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
287 Primitive::Type type = instruction_->GetType();
288 mips_codegen->MoveLocation(locations->Out(),
289 calling_convention.GetReturnLocation(type),
290 type);
291
292 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000293
294 // Store the resolved String to the BSS entry.
295 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
296 // .bss entry address in the fast path, so that we can avoid another calculation here.
297 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
298 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
299 Register out = locations->Out().AsRegister<Register>();
300 DCHECK_NE(out, AT);
301 CodeGeneratorMIPS::PcRelativePatchInfo* info =
302 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
303 mips_codegen->EmitPcRelativeAddressPlaceholder(info, TMP, base);
304 __ StoreToOffset(kStoreWord, out, TMP, 0);
305
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
310
311 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200312 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
313};
314
315class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
316 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200318
319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
320 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
321 __ Bind(GetEntryLabel());
322 if (instruction_->CanThrowIntoCatchBlock()) {
323 // Live registers will be restored in the catch block if caught.
324 SaveLiveRegisters(codegen, instruction_->GetLocations());
325 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100326 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200327 instruction_,
328 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100329 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
331 }
332
333 bool IsFatal() const OVERRIDE { return true; }
334
335 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
336
337 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200338 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
339};
340
341class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
342 public:
343 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000344 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
348 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100349 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 if (successor_ == nullptr) {
352 __ B(GetReturnLabel());
353 } else {
354 __ B(mips_codegen->GetLabelOf(successor_));
355 }
356 }
357
358 MipsLabel* GetReturnLabel() {
359 DCHECK(successor_ == nullptr);
360 return &return_label_;
361 }
362
363 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
364
365 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200366 // If not null, the block to branch to after the suspend check.
367 HBasicBlock* const successor_;
368
369 // If `successor_` is null, the label to branch to after the suspend check.
370 MipsLabel return_label_;
371
372 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
373};
374
375class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
376 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000377 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200378
379 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
380 LocationSummary* locations = instruction_->GetLocations();
381 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
382 uint32_t dex_pc = instruction_->GetDexPc();
383 DCHECK(instruction_->IsCheckCast()
384 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
385 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
386
387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, locations);
389
390 // We're moving two locations to locations that could overlap, so we need a parallel
391 // move resolver.
392 InvokeRuntimeCallingConvention calling_convention;
393 codegen->EmitParallelMoves(locations->InAt(1),
394 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
395 Primitive::kPrimNot,
396 object_class,
397 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
398 Primitive::kPrimNot);
399
400 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100401 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000402 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700403 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404 Primitive::Type ret_type = instruction_->GetType();
405 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
406 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 } else {
408 DCHECK(instruction_->IsCheckCast());
Serban Constantinescufca16662016-07-14 09:21:59 +0100409 mips_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
411 }
412
413 RestoreLiveRegisters(codegen, locations);
414 __ B(GetExitLabel());
415 }
416
417 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
418
419 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
421};
422
423class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
424 public:
Aart Bik42249c32016-01-07 15:33:50 -0800425 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000426 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200427
428 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800429 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100431 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000432 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 }
434
435 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
436
437 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
439};
440
441CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
442 const MipsInstructionSetFeatures& isa_features,
443 const CompilerOptions& compiler_options,
444 OptimizingCompilerStats* stats)
445 : CodeGenerator(graph,
446 kNumberOfCoreRegisters,
447 kNumberOfFRegisters,
448 kNumberOfRegisterPairs,
449 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
450 arraysize(kCoreCalleeSaves)),
451 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
452 arraysize(kFpuCalleeSaves)),
453 compiler_options,
454 stats),
455 block_labels_(nullptr),
456 location_builder_(graph, this),
457 instruction_visitor_(graph, this),
458 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100459 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700460 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700461 uint32_literals_(std::less<uint32_t>(),
462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700463 method_patches_(MethodReferenceComparator(),
464 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
465 call_patches_(MethodReferenceComparator(),
466 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700467 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
468 boot_image_string_patches_(StringReferenceValueComparator(),
469 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
470 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
471 boot_image_type_patches_(TypeReferenceValueComparator(),
472 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
473 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
474 boot_image_address_patches_(std::less<uint32_t>(),
475 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
476 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200477 // Save RA (containing the return address) to mimic Quick.
478 AddAllocatedRegister(Location::RegisterLocation(RA));
479}
480
481#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100482// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
483#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700484#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200485
486void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
487 // Ensure that we fix up branches.
488 __ FinalizeCode();
489
490 // Adjust native pc offsets in stack maps.
491 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
492 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
493 uint32_t new_position = __ GetAdjustedPosition(old_position);
494 DCHECK_GE(new_position, old_position);
495 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
496 }
497
498 // Adjust pc offsets for the disassembly information.
499 if (disasm_info_ != nullptr) {
500 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
501 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
502 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
503 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
504 it.second.start = __ GetAdjustedPosition(it.second.start);
505 it.second.end = __ GetAdjustedPosition(it.second.end);
506 }
507 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
508 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
509 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
510 }
511 }
512
513 CodeGenerator::Finalize(allocator);
514}
515
516MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
517 return codegen_->GetAssembler();
518}
519
520void ParallelMoveResolverMIPS::EmitMove(size_t index) {
521 DCHECK_LT(index, moves_.size());
522 MoveOperands* move = moves_[index];
523 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
524}
525
526void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
527 DCHECK_LT(index, moves_.size());
528 MoveOperands* move = moves_[index];
529 Primitive::Type type = move->GetType();
530 Location loc1 = move->GetDestination();
531 Location loc2 = move->GetSource();
532
533 DCHECK(!loc1.IsConstant());
534 DCHECK(!loc2.IsConstant());
535
536 if (loc1.Equals(loc2)) {
537 return;
538 }
539
540 if (loc1.IsRegister() && loc2.IsRegister()) {
541 // Swap 2 GPRs.
542 Register r1 = loc1.AsRegister<Register>();
543 Register r2 = loc2.AsRegister<Register>();
544 __ Move(TMP, r2);
545 __ Move(r2, r1);
546 __ Move(r1, TMP);
547 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
548 FRegister f1 = loc1.AsFpuRegister<FRegister>();
549 FRegister f2 = loc2.AsFpuRegister<FRegister>();
550 if (type == Primitive::kPrimFloat) {
551 __ MovS(FTMP, f2);
552 __ MovS(f2, f1);
553 __ MovS(f1, FTMP);
554 } else {
555 DCHECK_EQ(type, Primitive::kPrimDouble);
556 __ MovD(FTMP, f2);
557 __ MovD(f2, f1);
558 __ MovD(f1, FTMP);
559 }
560 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
561 (loc1.IsFpuRegister() && loc2.IsRegister())) {
562 // Swap FPR and GPR.
563 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
564 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
565 : loc2.AsFpuRegister<FRegister>();
566 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
567 : loc2.AsRegister<Register>();
568 __ Move(TMP, r2);
569 __ Mfc1(r2, f1);
570 __ Mtc1(TMP, f1);
571 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
572 // Swap 2 GPR register pairs.
573 Register r1 = loc1.AsRegisterPairLow<Register>();
574 Register r2 = loc2.AsRegisterPairLow<Register>();
575 __ Move(TMP, r2);
576 __ Move(r2, r1);
577 __ Move(r1, TMP);
578 r1 = loc1.AsRegisterPairHigh<Register>();
579 r2 = loc2.AsRegisterPairHigh<Register>();
580 __ Move(TMP, r2);
581 __ Move(r2, r1);
582 __ Move(r1, TMP);
583 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
584 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
585 // Swap FPR and GPR register pair.
586 DCHECK_EQ(type, Primitive::kPrimDouble);
587 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
588 : loc2.AsFpuRegister<FRegister>();
589 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
590 : loc2.AsRegisterPairLow<Register>();
591 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
592 : loc2.AsRegisterPairHigh<Register>();
593 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
594 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
595 // unpredictable and the following mfch1 will fail.
596 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800597 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200598 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800599 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200600 __ Move(r2_l, TMP);
601 __ Move(r2_h, AT);
602 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
603 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
604 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
605 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000606 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
607 (loc1.IsStackSlot() && loc2.IsRegister())) {
608 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
609 : loc2.AsRegister<Register>();
610 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
611 : loc2.GetStackIndex();
612 __ Move(TMP, reg);
613 __ LoadFromOffset(kLoadWord, reg, SP, offset);
614 __ StoreToOffset(kStoreWord, TMP, SP, offset);
615 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
616 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
617 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
618 : loc2.AsRegisterPairLow<Register>();
619 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
620 : loc2.AsRegisterPairHigh<Register>();
621 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
622 : loc2.GetStackIndex();
623 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
624 : loc2.GetHighStackIndex(kMipsWordSize);
625 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000626 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000627 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000628 __ Move(TMP, reg_h);
629 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
630 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200631 } else {
632 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
633 }
634}
635
636void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
637 __ Pop(static_cast<Register>(reg));
638}
639
640void ParallelMoveResolverMIPS::SpillScratch(int reg) {
641 __ Push(static_cast<Register>(reg));
642}
643
644void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
645 // Allocate a scratch register other than TMP, if available.
646 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
647 // automatically unspilled when the scratch scope object is destroyed).
648 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
649 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
650 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
651 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
652 __ LoadFromOffset(kLoadWord,
653 Register(ensure_scratch.GetRegister()),
654 SP,
655 index1 + stack_offset);
656 __ LoadFromOffset(kLoadWord,
657 TMP,
658 SP,
659 index2 + stack_offset);
660 __ StoreToOffset(kStoreWord,
661 Register(ensure_scratch.GetRegister()),
662 SP,
663 index2 + stack_offset);
664 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
665 }
666}
667
Alexey Frunze73296a72016-06-03 22:51:46 -0700668void CodeGeneratorMIPS::ComputeSpillMask() {
669 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
670 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
671 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
672 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
673 // registers, include the ZERO register to force alignment of FPU callee-saved registers
674 // within the stack frame.
675 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
676 core_spill_mask_ |= (1 << ZERO);
677 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700678}
679
680bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700681 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700682 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
683 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
684 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700685 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
686 // saved in an unused temporary register) and saving of RA and the current method pointer
687 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700688 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700689}
690
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200691static dwarf::Reg DWARFReg(Register reg) {
692 return dwarf::Reg::MipsCore(static_cast<int>(reg));
693}
694
695// TODO: mapping of floating-point registers to DWARF.
696
697void CodeGeneratorMIPS::GenerateFrameEntry() {
698 __ Bind(&frame_entry_label_);
699
700 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
701
702 if (do_overflow_check) {
703 __ LoadFromOffset(kLoadWord,
704 ZERO,
705 SP,
706 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
707 RecordPcInfo(nullptr, 0);
708 }
709
710 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700711 CHECK_EQ(fpu_spill_mask_, 0u);
712 CHECK_EQ(core_spill_mask_, 1u << RA);
713 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200714 return;
715 }
716
717 // Make sure the frame size isn't unreasonably large.
718 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
719 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
720 }
721
722 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200723
Alexey Frunze73296a72016-06-03 22:51:46 -0700724 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200725 __ IncreaseFrameSize(ofs);
726
Alexey Frunze73296a72016-06-03 22:51:46 -0700727 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
728 Register reg = static_cast<Register>(MostSignificantBit(mask));
729 mask ^= 1u << reg;
730 ofs -= kMipsWordSize;
731 // The ZERO register is only included for alignment.
732 if (reg != ZERO) {
733 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200734 __ cfi().RelOffset(DWARFReg(reg), ofs);
735 }
736 }
737
Alexey Frunze73296a72016-06-03 22:51:46 -0700738 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
739 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
740 mask ^= 1u << reg;
741 ofs -= kMipsDoublewordSize;
742 __ StoreDToOffset(reg, SP, ofs);
743 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200744 }
745
Alexey Frunze73296a72016-06-03 22:51:46 -0700746 // Store the current method pointer.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700747 // TODO: can we not do this if RequiresCurrentMethod() returns false?
Alexey Frunze73296a72016-06-03 22:51:46 -0700748 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200749}
750
751void CodeGeneratorMIPS::GenerateFrameExit() {
752 __ cfi().RememberState();
753
754 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200755 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200756
Alexey Frunze73296a72016-06-03 22:51:46 -0700757 // For better instruction scheduling restore RA before other registers.
758 uint32_t ofs = GetFrameSize();
759 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
760 Register reg = static_cast<Register>(MostSignificantBit(mask));
761 mask ^= 1u << reg;
762 ofs -= kMipsWordSize;
763 // The ZERO register is only included for alignment.
764 if (reg != ZERO) {
765 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200766 __ cfi().Restore(DWARFReg(reg));
767 }
768 }
769
Alexey Frunze73296a72016-06-03 22:51:46 -0700770 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
771 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
772 mask ^= 1u << reg;
773 ofs -= kMipsDoublewordSize;
774 __ LoadDFromOffset(reg, SP, ofs);
775 // TODO: __ cfi().Restore(DWARFReg(reg));
776 }
777
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700778 size_t frame_size = GetFrameSize();
779 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
780 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
781 bool reordering = __ SetReorder(false);
782 if (exchange) {
783 __ Jr(RA);
784 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
785 } else {
786 __ DecreaseFrameSize(frame_size);
787 __ Jr(RA);
788 __ Nop(); // In delay slot.
789 }
790 __ SetReorder(reordering);
791 } else {
792 __ Jr(RA);
793 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200794 }
795
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200796 __ cfi().RestoreState();
797 __ cfi().DefCFAOffset(GetFrameSize());
798}
799
800void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
801 __ Bind(GetLabelOf(block));
802}
803
804void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
805 if (src.Equals(dst)) {
806 return;
807 }
808
809 if (src.IsConstant()) {
810 MoveConstant(dst, src.GetConstant());
811 } else {
812 if (Primitive::Is64BitType(dst_type)) {
813 Move64(dst, src);
814 } else {
815 Move32(dst, src);
816 }
817 }
818}
819
820void CodeGeneratorMIPS::Move32(Location destination, Location source) {
821 if (source.Equals(destination)) {
822 return;
823 }
824
825 if (destination.IsRegister()) {
826 if (source.IsRegister()) {
827 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
828 } else if (source.IsFpuRegister()) {
829 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
830 } else {
831 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
832 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
833 }
834 } else if (destination.IsFpuRegister()) {
835 if (source.IsRegister()) {
836 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
837 } else if (source.IsFpuRegister()) {
838 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
839 } else {
840 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
841 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
842 }
843 } else {
844 DCHECK(destination.IsStackSlot()) << destination;
845 if (source.IsRegister()) {
846 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
847 } else if (source.IsFpuRegister()) {
848 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
849 } else {
850 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
851 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
852 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
853 }
854 }
855}
856
857void CodeGeneratorMIPS::Move64(Location destination, Location source) {
858 if (source.Equals(destination)) {
859 return;
860 }
861
862 if (destination.IsRegisterPair()) {
863 if (source.IsRegisterPair()) {
864 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
865 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
866 } else if (source.IsFpuRegister()) {
867 Register dst_high = destination.AsRegisterPairHigh<Register>();
868 Register dst_low = destination.AsRegisterPairLow<Register>();
869 FRegister src = source.AsFpuRegister<FRegister>();
870 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800871 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200872 } else {
873 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
874 int32_t off = source.GetStackIndex();
875 Register r = destination.AsRegisterPairLow<Register>();
876 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
877 }
878 } else if (destination.IsFpuRegister()) {
879 if (source.IsRegisterPair()) {
880 FRegister dst = destination.AsFpuRegister<FRegister>();
881 Register src_high = source.AsRegisterPairHigh<Register>();
882 Register src_low = source.AsRegisterPairLow<Register>();
883 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800884 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200885 } else if (source.IsFpuRegister()) {
886 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
887 } else {
888 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
889 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
890 }
891 } else {
892 DCHECK(destination.IsDoubleStackSlot()) << destination;
893 int32_t off = destination.GetStackIndex();
894 if (source.IsRegisterPair()) {
895 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
896 } else if (source.IsFpuRegister()) {
897 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
898 } else {
899 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
900 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
901 __ StoreToOffset(kStoreWord, TMP, SP, off);
902 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
903 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
904 }
905 }
906}
907
908void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
909 if (c->IsIntConstant() || c->IsNullConstant()) {
910 // Move 32 bit constant.
911 int32_t value = GetInt32ValueOf(c);
912 if (destination.IsRegister()) {
913 Register dst = destination.AsRegister<Register>();
914 __ LoadConst32(dst, value);
915 } else {
916 DCHECK(destination.IsStackSlot())
917 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700918 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200919 }
920 } else if (c->IsLongConstant()) {
921 // Move 64 bit constant.
922 int64_t value = GetInt64ValueOf(c);
923 if (destination.IsRegisterPair()) {
924 Register r_h = destination.AsRegisterPairHigh<Register>();
925 Register r_l = destination.AsRegisterPairLow<Register>();
926 __ LoadConst64(r_h, r_l, value);
927 } else {
928 DCHECK(destination.IsDoubleStackSlot())
929 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700930 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200931 }
932 } else if (c->IsFloatConstant()) {
933 // Move 32 bit float constant.
934 int32_t value = GetInt32ValueOf(c);
935 if (destination.IsFpuRegister()) {
936 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
937 } else {
938 DCHECK(destination.IsStackSlot())
939 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700940 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200941 }
942 } else {
943 // Move 64 bit double constant.
944 DCHECK(c->IsDoubleConstant()) << c->DebugName();
945 int64_t value = GetInt64ValueOf(c);
946 if (destination.IsFpuRegister()) {
947 FRegister fd = destination.AsFpuRegister<FRegister>();
948 __ LoadDConst64(fd, value, TMP);
949 } else {
950 DCHECK(destination.IsDoubleStackSlot())
951 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700952 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200953 }
954 }
955}
956
957void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
958 DCHECK(destination.IsRegister());
959 Register dst = destination.AsRegister<Register>();
960 __ LoadConst32(dst, value);
961}
962
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200963void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
964 if (location.IsRegister()) {
965 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700966 } else if (location.IsRegisterPair()) {
967 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
968 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200969 } else {
970 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
971 }
972}
973
Vladimir Markoaad75c62016-10-03 08:46:48 +0000974template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
975inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
976 const ArenaDeque<PcRelativePatchInfo>& infos,
977 ArenaVector<LinkerPatch>* linker_patches) {
978 for (const PcRelativePatchInfo& info : infos) {
979 const DexFile& dex_file = info.target_dex_file;
980 size_t offset_or_index = info.offset_or_index;
981 DCHECK(info.high_label.IsBound());
982 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
983 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
984 // the assembler's base label used for PC-relative addressing.
985 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
986 ? __ GetLabelLocation(&info.pc_rel_label)
987 : __ GetPcRelBaseLabelLocation();
988 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
989 }
990}
991
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700992void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
993 DCHECK(linker_patches->empty());
994 size_t size =
995 method_patches_.size() +
996 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700997 pc_relative_dex_cache_patches_.size() +
998 pc_relative_string_patches_.size() +
999 pc_relative_type_patches_.size() +
1000 boot_image_string_patches_.size() +
1001 boot_image_type_patches_.size() +
1002 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001003 linker_patches->reserve(size);
1004 for (const auto& entry : method_patches_) {
1005 const MethodReference& target_method = entry.first;
1006 Literal* literal = entry.second;
1007 DCHECK(literal->GetLabel()->IsBound());
1008 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1009 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1010 target_method.dex_file,
1011 target_method.dex_method_index));
1012 }
1013 for (const auto& entry : call_patches_) {
1014 const MethodReference& target_method = entry.first;
1015 Literal* literal = entry.second;
1016 DCHECK(literal->GetLabel()->IsBound());
1017 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1018 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1019 target_method.dex_file,
1020 target_method.dex_method_index));
1021 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001022 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1023 linker_patches);
1024 if (!GetCompilerOptions().IsBootImage()) {
1025 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1026 linker_patches);
1027 } else {
1028 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1029 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001030 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001031 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1032 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001033 for (const auto& entry : boot_image_string_patches_) {
1034 const StringReference& target_string = entry.first;
1035 Literal* literal = entry.second;
1036 DCHECK(literal->GetLabel()->IsBound());
1037 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1038 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1039 target_string.dex_file,
1040 target_string.string_index));
1041 }
1042 for (const auto& entry : boot_image_type_patches_) {
1043 const TypeReference& target_type = entry.first;
1044 Literal* literal = entry.second;
1045 DCHECK(literal->GetLabel()->IsBound());
1046 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1047 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1048 target_type.dex_file,
1049 target_type.type_index));
1050 }
1051 for (const auto& entry : boot_image_address_patches_) {
1052 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1053 Literal* literal = entry.second;
1054 DCHECK(literal->GetLabel()->IsBound());
1055 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1056 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1057 }
1058}
1059
1060CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1061 const DexFile& dex_file, uint32_t string_index) {
1062 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1063}
1064
1065CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1066 const DexFile& dex_file, uint32_t type_index) {
1067 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001068}
1069
1070CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1071 const DexFile& dex_file, uint32_t element_offset) {
1072 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1073}
1074
1075CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1076 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1077 patches->emplace_back(dex_file, offset_or_index);
1078 return &patches->back();
1079}
1080
Alexey Frunze06a46c42016-07-19 15:00:40 -07001081Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1082 return map->GetOrCreate(
1083 value,
1084 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1085}
1086
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001087Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1088 MethodToLiteralMap* map) {
1089 return map->GetOrCreate(
1090 target_method,
1091 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1092}
1093
1094Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1095 return DeduplicateMethodLiteral(target_method, &method_patches_);
1096}
1097
1098Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1099 return DeduplicateMethodLiteral(target_method, &call_patches_);
1100}
1101
Alexey Frunze06a46c42016-07-19 15:00:40 -07001102Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1103 uint32_t string_index) {
1104 return boot_image_string_patches_.GetOrCreate(
1105 StringReference(&dex_file, string_index),
1106 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1107}
1108
1109Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1110 uint32_t type_index) {
1111 return boot_image_type_patches_.GetOrCreate(
1112 TypeReference(&dex_file, type_index),
1113 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1114}
1115
1116Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1117 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1118 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1119 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1120}
1121
Vladimir Markoaad75c62016-10-03 08:46:48 +00001122void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1123 PcRelativePatchInfo* info, Register out, Register base) {
1124 bool reordering = __ SetReorder(false);
1125 if (GetInstructionSetFeatures().IsR6()) {
1126 DCHECK_EQ(base, ZERO);
1127 __ Bind(&info->high_label);
1128 __ Bind(&info->pc_rel_label);
1129 // Add a 32-bit offset to PC.
1130 __ Auipc(out, /* placeholder */ 0x1234);
1131 __ Addiu(out, out, /* placeholder */ 0x5678);
1132 } else {
1133 // If base is ZERO, emit NAL to obtain the actual base.
1134 if (base == ZERO) {
1135 // Generate a dummy PC-relative call to obtain PC.
1136 __ Nal();
1137 }
1138 __ Bind(&info->high_label);
1139 __ Lui(out, /* placeholder */ 0x1234);
1140 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1141 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1142 if (base == ZERO) {
1143 __ Bind(&info->pc_rel_label);
1144 }
1145 __ Ori(out, out, /* placeholder */ 0x5678);
1146 // Add a 32-bit offset to PC.
1147 __ Addu(out, out, (base == ZERO) ? RA : base);
1148 }
1149 __ SetReorder(reordering);
1150}
1151
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001152void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1153 MipsLabel done;
1154 Register card = AT;
1155 Register temp = TMP;
1156 __ Beqz(value, &done);
1157 __ LoadFromOffset(kLoadWord,
1158 card,
1159 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001160 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001161 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1162 __ Addu(temp, card, temp);
1163 __ Sb(card, temp, 0);
1164 __ Bind(&done);
1165}
1166
David Brazdil58282f42016-01-14 12:45:10 +00001167void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001168 // Don't allocate the dalvik style register pair passing.
1169 blocked_register_pairs_[A1_A2] = true;
1170
1171 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1172 blocked_core_registers_[ZERO] = true;
1173 blocked_core_registers_[K0] = true;
1174 blocked_core_registers_[K1] = true;
1175 blocked_core_registers_[GP] = true;
1176 blocked_core_registers_[SP] = true;
1177 blocked_core_registers_[RA] = true;
1178
1179 // AT and TMP(T8) are used as temporary/scratch registers
1180 // (similar to how AT is used by MIPS assemblers).
1181 blocked_core_registers_[AT] = true;
1182 blocked_core_registers_[TMP] = true;
1183 blocked_fpu_registers_[FTMP] = true;
1184
1185 // Reserve suspend and thread registers.
1186 blocked_core_registers_[S0] = true;
1187 blocked_core_registers_[TR] = true;
1188
1189 // Reserve T9 for function calls
1190 blocked_core_registers_[T9] = true;
1191
1192 // Reserve odd-numbered FPU registers.
1193 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1194 blocked_fpu_registers_[i] = true;
1195 }
1196
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001197 if (GetGraph()->IsDebuggable()) {
1198 // Stubs do not save callee-save floating point registers. If the graph
1199 // is debuggable, we need to deal with these registers differently. For
1200 // now, just block them.
1201 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1202 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1203 }
1204 }
1205
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001206 UpdateBlockedPairRegisters();
1207}
1208
1209void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1210 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1211 MipsManagedRegister current =
1212 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1213 if (blocked_core_registers_[current.AsRegisterPairLow()]
1214 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1215 blocked_register_pairs_[i] = true;
1216 }
1217 }
1218}
1219
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001220size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1221 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1222 return kMipsWordSize;
1223}
1224
1225size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1226 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1227 return kMipsWordSize;
1228}
1229
1230size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1231 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1232 return kMipsDoublewordSize;
1233}
1234
1235size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1236 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1237 return kMipsDoublewordSize;
1238}
1239
1240void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001241 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242}
1243
1244void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001245 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001246}
1247
Serban Constantinescufca16662016-07-14 09:21:59 +01001248constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1249
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001250void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1251 HInstruction* instruction,
1252 uint32_t dex_pc,
1253 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001254 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001255 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001256 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001257 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001258 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001259 // Reserve argument space on stack (for $a0-$a3) for
1260 // entrypoints that directly reference native implementations.
1261 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001262 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001263 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001264 } else {
1265 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001266 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001267 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001268 if (EntrypointRequiresStackMap(entrypoint)) {
1269 RecordPcInfo(instruction, dex_pc, slow_path);
1270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001271}
1272
1273void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1274 Register class_reg) {
1275 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1276 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1277 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1278 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1279 __ Sync(0);
1280 __ Bind(slow_path->GetExitLabel());
1281}
1282
1283void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1284 __ Sync(0); // Only stype 0 is supported.
1285}
1286
1287void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1288 HBasicBlock* successor) {
1289 SuspendCheckSlowPathMIPS* slow_path =
1290 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1291 codegen_->AddSlowPath(slow_path);
1292
1293 __ LoadFromOffset(kLoadUnsignedHalfword,
1294 TMP,
1295 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001296 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 if (successor == nullptr) {
1298 __ Bnez(TMP, slow_path->GetEntryLabel());
1299 __ Bind(slow_path->GetReturnLabel());
1300 } else {
1301 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1302 __ B(slow_path->GetEntryLabel());
1303 // slow_path will return to GetLabelOf(successor).
1304 }
1305}
1306
1307InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1308 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001309 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310 assembler_(codegen->GetAssembler()),
1311 codegen_(codegen) {}
1312
1313void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1314 DCHECK_EQ(instruction->InputCount(), 2U);
1315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1316 Primitive::Type type = instruction->GetResultType();
1317 switch (type) {
1318 case Primitive::kPrimInt: {
1319 locations->SetInAt(0, Location::RequiresRegister());
1320 HInstruction* right = instruction->InputAt(1);
1321 bool can_use_imm = false;
1322 if (right->IsConstant()) {
1323 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1324 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1325 can_use_imm = IsUint<16>(imm);
1326 } else if (instruction->IsAdd()) {
1327 can_use_imm = IsInt<16>(imm);
1328 } else {
1329 DCHECK(instruction->IsSub());
1330 can_use_imm = IsInt<16>(-imm);
1331 }
1332 }
1333 if (can_use_imm)
1334 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1335 else
1336 locations->SetInAt(1, Location::RequiresRegister());
1337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1338 break;
1339 }
1340
1341 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001342 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001343 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345 break;
1346 }
1347
1348 case Primitive::kPrimFloat:
1349 case Primitive::kPrimDouble:
1350 DCHECK(instruction->IsAdd() || instruction->IsSub());
1351 locations->SetInAt(0, Location::RequiresFpuRegister());
1352 locations->SetInAt(1, Location::RequiresFpuRegister());
1353 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1354 break;
1355
1356 default:
1357 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1358 }
1359}
1360
1361void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1362 Primitive::Type type = instruction->GetType();
1363 LocationSummary* locations = instruction->GetLocations();
1364
1365 switch (type) {
1366 case Primitive::kPrimInt: {
1367 Register dst = locations->Out().AsRegister<Register>();
1368 Register lhs = locations->InAt(0).AsRegister<Register>();
1369 Location rhs_location = locations->InAt(1);
1370
1371 Register rhs_reg = ZERO;
1372 int32_t rhs_imm = 0;
1373 bool use_imm = rhs_location.IsConstant();
1374 if (use_imm) {
1375 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1376 } else {
1377 rhs_reg = rhs_location.AsRegister<Register>();
1378 }
1379
1380 if (instruction->IsAnd()) {
1381 if (use_imm)
1382 __ Andi(dst, lhs, rhs_imm);
1383 else
1384 __ And(dst, lhs, rhs_reg);
1385 } else if (instruction->IsOr()) {
1386 if (use_imm)
1387 __ Ori(dst, lhs, rhs_imm);
1388 else
1389 __ Or(dst, lhs, rhs_reg);
1390 } else if (instruction->IsXor()) {
1391 if (use_imm)
1392 __ Xori(dst, lhs, rhs_imm);
1393 else
1394 __ Xor(dst, lhs, rhs_reg);
1395 } else if (instruction->IsAdd()) {
1396 if (use_imm)
1397 __ Addiu(dst, lhs, rhs_imm);
1398 else
1399 __ Addu(dst, lhs, rhs_reg);
1400 } else {
1401 DCHECK(instruction->IsSub());
1402 if (use_imm)
1403 __ Addiu(dst, lhs, -rhs_imm);
1404 else
1405 __ Subu(dst, lhs, rhs_reg);
1406 }
1407 break;
1408 }
1409
1410 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1412 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1413 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1414 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001415 Location rhs_location = locations->InAt(1);
1416 bool use_imm = rhs_location.IsConstant();
1417 if (!use_imm) {
1418 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1419 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1420 if (instruction->IsAnd()) {
1421 __ And(dst_low, lhs_low, rhs_low);
1422 __ And(dst_high, lhs_high, rhs_high);
1423 } else if (instruction->IsOr()) {
1424 __ Or(dst_low, lhs_low, rhs_low);
1425 __ Or(dst_high, lhs_high, rhs_high);
1426 } else if (instruction->IsXor()) {
1427 __ Xor(dst_low, lhs_low, rhs_low);
1428 __ Xor(dst_high, lhs_high, rhs_high);
1429 } else if (instruction->IsAdd()) {
1430 if (lhs_low == rhs_low) {
1431 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1432 __ Slt(TMP, lhs_low, ZERO);
1433 __ Addu(dst_low, lhs_low, rhs_low);
1434 } else {
1435 __ Addu(dst_low, lhs_low, rhs_low);
1436 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1437 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1438 }
1439 __ Addu(dst_high, lhs_high, rhs_high);
1440 __ Addu(dst_high, dst_high, TMP);
1441 } else {
1442 DCHECK(instruction->IsSub());
1443 __ Sltu(TMP, lhs_low, rhs_low);
1444 __ Subu(dst_low, lhs_low, rhs_low);
1445 __ Subu(dst_high, lhs_high, rhs_high);
1446 __ Subu(dst_high, dst_high, TMP);
1447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001448 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001449 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1450 if (instruction->IsOr()) {
1451 uint32_t low = Low32Bits(value);
1452 uint32_t high = High32Bits(value);
1453 if (IsUint<16>(low)) {
1454 if (dst_low != lhs_low || low != 0) {
1455 __ Ori(dst_low, lhs_low, low);
1456 }
1457 } else {
1458 __ LoadConst32(TMP, low);
1459 __ Or(dst_low, lhs_low, TMP);
1460 }
1461 if (IsUint<16>(high)) {
1462 if (dst_high != lhs_high || high != 0) {
1463 __ Ori(dst_high, lhs_high, high);
1464 }
1465 } else {
1466 if (high != low) {
1467 __ LoadConst32(TMP, high);
1468 }
1469 __ Or(dst_high, lhs_high, TMP);
1470 }
1471 } else if (instruction->IsXor()) {
1472 uint32_t low = Low32Bits(value);
1473 uint32_t high = High32Bits(value);
1474 if (IsUint<16>(low)) {
1475 if (dst_low != lhs_low || low != 0) {
1476 __ Xori(dst_low, lhs_low, low);
1477 }
1478 } else {
1479 __ LoadConst32(TMP, low);
1480 __ Xor(dst_low, lhs_low, TMP);
1481 }
1482 if (IsUint<16>(high)) {
1483 if (dst_high != lhs_high || high != 0) {
1484 __ Xori(dst_high, lhs_high, high);
1485 }
1486 } else {
1487 if (high != low) {
1488 __ LoadConst32(TMP, high);
1489 }
1490 __ Xor(dst_high, lhs_high, TMP);
1491 }
1492 } else if (instruction->IsAnd()) {
1493 uint32_t low = Low32Bits(value);
1494 uint32_t high = High32Bits(value);
1495 if (IsUint<16>(low)) {
1496 __ Andi(dst_low, lhs_low, low);
1497 } else if (low != 0xFFFFFFFF) {
1498 __ LoadConst32(TMP, low);
1499 __ And(dst_low, lhs_low, TMP);
1500 } else if (dst_low != lhs_low) {
1501 __ Move(dst_low, lhs_low);
1502 }
1503 if (IsUint<16>(high)) {
1504 __ Andi(dst_high, lhs_high, high);
1505 } else if (high != 0xFFFFFFFF) {
1506 if (high != low) {
1507 __ LoadConst32(TMP, high);
1508 }
1509 __ And(dst_high, lhs_high, TMP);
1510 } else if (dst_high != lhs_high) {
1511 __ Move(dst_high, lhs_high);
1512 }
1513 } else {
1514 if (instruction->IsSub()) {
1515 value = -value;
1516 } else {
1517 DCHECK(instruction->IsAdd());
1518 }
1519 int32_t low = Low32Bits(value);
1520 int32_t high = High32Bits(value);
1521 if (IsInt<16>(low)) {
1522 if (dst_low != lhs_low || low != 0) {
1523 __ Addiu(dst_low, lhs_low, low);
1524 }
1525 if (low != 0) {
1526 __ Sltiu(AT, dst_low, low);
1527 }
1528 } else {
1529 __ LoadConst32(TMP, low);
1530 __ Addu(dst_low, lhs_low, TMP);
1531 __ Sltu(AT, dst_low, TMP);
1532 }
1533 if (IsInt<16>(high)) {
1534 if (dst_high != lhs_high || high != 0) {
1535 __ Addiu(dst_high, lhs_high, high);
1536 }
1537 } else {
1538 if (high != low) {
1539 __ LoadConst32(TMP, high);
1540 }
1541 __ Addu(dst_high, lhs_high, TMP);
1542 }
1543 if (low != 0) {
1544 __ Addu(dst_high, dst_high, AT);
1545 }
1546 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001547 }
1548 break;
1549 }
1550
1551 case Primitive::kPrimFloat:
1552 case Primitive::kPrimDouble: {
1553 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1554 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1555 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1556 if (instruction->IsAdd()) {
1557 if (type == Primitive::kPrimFloat) {
1558 __ AddS(dst, lhs, rhs);
1559 } else {
1560 __ AddD(dst, lhs, rhs);
1561 }
1562 } else {
1563 DCHECK(instruction->IsSub());
1564 if (type == Primitive::kPrimFloat) {
1565 __ SubS(dst, lhs, rhs);
1566 } else {
1567 __ SubD(dst, lhs, rhs);
1568 }
1569 }
1570 break;
1571 }
1572
1573 default:
1574 LOG(FATAL) << "Unexpected binary operation type " << type;
1575 }
1576}
1577
1578void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001579 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001580
1581 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1582 Primitive::Type type = instr->GetResultType();
1583 switch (type) {
1584 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001585 locations->SetInAt(0, Location::RequiresRegister());
1586 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1588 break;
1589 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1592 locations->SetOut(Location::RequiresRegister());
1593 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001594 default:
1595 LOG(FATAL) << "Unexpected shift type " << type;
1596 }
1597}
1598
1599static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1600
1601void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001602 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 LocationSummary* locations = instr->GetLocations();
1604 Primitive::Type type = instr->GetType();
1605
1606 Location rhs_location = locations->InAt(1);
1607 bool use_imm = rhs_location.IsConstant();
1608 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1609 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001610 const uint32_t shift_mask =
1611 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001612 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001613 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1614 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001615
1616 switch (type) {
1617 case Primitive::kPrimInt: {
1618 Register dst = locations->Out().AsRegister<Register>();
1619 Register lhs = locations->InAt(0).AsRegister<Register>();
1620 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001621 if (shift_value == 0) {
1622 if (dst != lhs) {
1623 __ Move(dst, lhs);
1624 }
1625 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001626 __ Sll(dst, lhs, shift_value);
1627 } else if (instr->IsShr()) {
1628 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001629 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001630 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001631 } else {
1632 if (has_ins_rotr) {
1633 __ Rotr(dst, lhs, shift_value);
1634 } else {
1635 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1636 __ Srl(dst, lhs, shift_value);
1637 __ Or(dst, dst, TMP);
1638 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001639 }
1640 } else {
1641 if (instr->IsShl()) {
1642 __ Sllv(dst, lhs, rhs_reg);
1643 } else if (instr->IsShr()) {
1644 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001645 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001646 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001647 } else {
1648 if (has_ins_rotr) {
1649 __ Rotrv(dst, lhs, rhs_reg);
1650 } else {
1651 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001652 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1653 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1654 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1655 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1656 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001657 __ Sllv(TMP, lhs, TMP);
1658 __ Srlv(dst, lhs, rhs_reg);
1659 __ Or(dst, dst, TMP);
1660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001661 }
1662 }
1663 break;
1664 }
1665
1666 case Primitive::kPrimLong: {
1667 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1668 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1669 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1670 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1671 if (use_imm) {
1672 if (shift_value == 0) {
1673 codegen_->Move64(locations->Out(), locations->InAt(0));
1674 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001675 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001676 if (instr->IsShl()) {
1677 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1678 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1679 __ Sll(dst_low, lhs_low, shift_value);
1680 } else if (instr->IsShr()) {
1681 __ Srl(dst_low, lhs_low, shift_value);
1682 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1683 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001684 } else if (instr->IsUShr()) {
1685 __ Srl(dst_low, lhs_low, shift_value);
1686 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1687 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001688 } else {
1689 __ Srl(dst_low, lhs_low, shift_value);
1690 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1691 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001692 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001693 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001694 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001695 if (instr->IsShl()) {
1696 __ Sll(dst_low, lhs_low, shift_value);
1697 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1698 __ Sll(dst_high, lhs_high, shift_value);
1699 __ Or(dst_high, dst_high, TMP);
1700 } else if (instr->IsShr()) {
1701 __ Sra(dst_high, lhs_high, shift_value);
1702 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1703 __ Srl(dst_low, lhs_low, shift_value);
1704 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001706 __ Srl(dst_high, lhs_high, shift_value);
1707 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1708 __ Srl(dst_low, lhs_low, shift_value);
1709 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001710 } else {
1711 __ Srl(TMP, lhs_low, shift_value);
1712 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1713 __ Or(dst_low, dst_low, TMP);
1714 __ Srl(TMP, lhs_high, shift_value);
1715 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1716 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001717 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 }
1719 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001720 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001722 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001723 __ Move(dst_low, ZERO);
1724 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001725 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001726 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001727 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001728 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001729 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001730 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001731 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001732 // 64-bit rotation by 32 is just a swap.
1733 __ Move(dst_low, lhs_high);
1734 __ Move(dst_high, lhs_low);
1735 } else {
1736 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001737 __ Srl(dst_low, lhs_high, shift_value_high);
1738 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1739 __ Srl(dst_high, lhs_low, shift_value_high);
1740 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001741 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001742 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1743 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001744 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001745 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1746 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001747 __ Or(dst_high, dst_high, TMP);
1748 }
1749 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001750 }
1751 }
1752 } else {
1753 MipsLabel done;
1754 if (instr->IsShl()) {
1755 __ Sllv(dst_low, lhs_low, rhs_reg);
1756 __ Nor(AT, ZERO, rhs_reg);
1757 __ Srl(TMP, lhs_low, 1);
1758 __ Srlv(TMP, TMP, AT);
1759 __ Sllv(dst_high, lhs_high, rhs_reg);
1760 __ Or(dst_high, dst_high, TMP);
1761 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1762 __ Beqz(TMP, &done);
1763 __ Move(dst_high, dst_low);
1764 __ Move(dst_low, ZERO);
1765 } else if (instr->IsShr()) {
1766 __ Srav(dst_high, lhs_high, rhs_reg);
1767 __ Nor(AT, ZERO, rhs_reg);
1768 __ Sll(TMP, lhs_high, 1);
1769 __ Sllv(TMP, TMP, AT);
1770 __ Srlv(dst_low, lhs_low, rhs_reg);
1771 __ Or(dst_low, dst_low, TMP);
1772 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1773 __ Beqz(TMP, &done);
1774 __ Move(dst_low, dst_high);
1775 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001776 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001777 __ Srlv(dst_high, lhs_high, rhs_reg);
1778 __ Nor(AT, ZERO, rhs_reg);
1779 __ Sll(TMP, lhs_high, 1);
1780 __ Sllv(TMP, TMP, AT);
1781 __ Srlv(dst_low, lhs_low, rhs_reg);
1782 __ Or(dst_low, dst_low, TMP);
1783 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1784 __ Beqz(TMP, &done);
1785 __ Move(dst_low, dst_high);
1786 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001787 } else {
1788 __ Nor(AT, ZERO, rhs_reg);
1789 __ Srlv(TMP, lhs_low, rhs_reg);
1790 __ Sll(dst_low, lhs_high, 1);
1791 __ Sllv(dst_low, dst_low, AT);
1792 __ Or(dst_low, dst_low, TMP);
1793 __ Srlv(TMP, lhs_high, rhs_reg);
1794 __ Sll(dst_high, lhs_low, 1);
1795 __ Sllv(dst_high, dst_high, AT);
1796 __ Or(dst_high, dst_high, TMP);
1797 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1798 __ Beqz(TMP, &done);
1799 __ Move(TMP, dst_high);
1800 __ Move(dst_high, dst_low);
1801 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001802 }
1803 __ Bind(&done);
1804 }
1805 break;
1806 }
1807
1808 default:
1809 LOG(FATAL) << "Unexpected shift operation type " << type;
1810 }
1811}
1812
1813void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1814 HandleBinaryOp(instruction);
1815}
1816
1817void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1818 HandleBinaryOp(instruction);
1819}
1820
1821void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1822 HandleBinaryOp(instruction);
1823}
1824
1825void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1826 HandleBinaryOp(instruction);
1827}
1828
1829void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1830 LocationSummary* locations =
1831 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1834 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1835 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1836 } else {
1837 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1838 }
1839}
1840
Alexey Frunze2923db72016-08-20 01:55:47 -07001841auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1842 auto null_checker = [this, instruction]() {
1843 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1844 };
1845 return null_checker;
1846}
1847
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1849 LocationSummary* locations = instruction->GetLocations();
1850 Register obj = locations->InAt(0).AsRegister<Register>();
1851 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001852 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001853 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001855 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001856 switch (type) {
1857 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 Register out = locations->Out().AsRegister<Register>();
1859 if (index.IsConstant()) {
1860 size_t offset =
1861 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001862 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 } else {
1864 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001865 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 Register out = locations->Out().AsRegister<Register>();
1872 if (index.IsConstant()) {
1873 size_t offset =
1874 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001875 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 } else {
1877 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001878 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879 }
1880 break;
1881 }
1882
1883 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884 Register out = locations->Out().AsRegister<Register>();
1885 if (index.IsConstant()) {
1886 size_t offset =
1887 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001888 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001889 } else {
1890 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1891 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001892 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893 }
1894 break;
1895 }
1896
1897 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001898 Register out = locations->Out().AsRegister<Register>();
1899 if (index.IsConstant()) {
1900 size_t offset =
1901 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001902 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001903 } else {
1904 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1905 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001906 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001907 }
1908 break;
1909 }
1910
1911 case Primitive::kPrimInt:
1912 case Primitive::kPrimNot: {
1913 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914 Register out = locations->Out().AsRegister<Register>();
1915 if (index.IsConstant()) {
1916 size_t offset =
1917 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001918 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 } else {
1920 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1921 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001922 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923 }
1924 break;
1925 }
1926
1927 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001928 Register out = locations->Out().AsRegisterPairLow<Register>();
1929 if (index.IsConstant()) {
1930 size_t offset =
1931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001932 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933 } else {
1934 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1935 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001936 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937 }
1938 break;
1939 }
1940
1941 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1943 if (index.IsConstant()) {
1944 size_t offset =
1945 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001946 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947 } else {
1948 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1949 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001950 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951 }
1952 break;
1953 }
1954
1955 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1957 if (index.IsConstant()) {
1958 size_t offset =
1959 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001960 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961 } else {
1962 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1963 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001964 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001965 }
1966 break;
1967 }
1968
1969 case Primitive::kPrimVoid:
1970 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1971 UNREACHABLE();
1972 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973}
1974
1975void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1977 locations->SetInAt(0, Location::RequiresRegister());
1978 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1979}
1980
1981void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1982 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001983 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 Register obj = locations->InAt(0).AsRegister<Register>();
1985 Register out = locations->Out().AsRegister<Register>();
1986 __ LoadFromOffset(kLoadWord, out, obj, offset);
1987 codegen_->MaybeRecordImplicitNullCheck(instruction);
1988}
1989
Alexey Frunzef58b2482016-09-02 22:14:06 -07001990Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1991 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1992 ? Location::ConstantLocation(instruction->AsConstant())
1993 : Location::RequiresRegister();
1994}
1995
1996Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1997 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1998 // We can store a non-zero float or double constant without first loading it into the FPU,
1999 // but we should only prefer this if the constant has a single use.
2000 if (instruction->IsConstant() &&
2001 (instruction->AsConstant()->IsZeroBitPattern() ||
2002 instruction->GetUses().HasExactlyOneElement())) {
2003 return Location::ConstantLocation(instruction->AsConstant());
2004 // Otherwise fall through and require an FPU register for the constant.
2005 }
2006 return Location::RequiresFpuRegister();
2007}
2008
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002009void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002010 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002011 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2012 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002013 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002014 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002015 InvokeRuntimeCallingConvention calling_convention;
2016 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2017 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2018 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2019 } else {
2020 locations->SetInAt(0, Location::RequiresRegister());
2021 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2022 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002023 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002025 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 }
2027 }
2028}
2029
2030void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2031 LocationSummary* locations = instruction->GetLocations();
2032 Register obj = locations->InAt(0).AsRegister<Register>();
2033 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002034 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 Primitive::Type value_type = instruction->GetComponentType();
2036 bool needs_runtime_call = locations->WillCall();
2037 bool needs_write_barrier =
2038 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002039 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002040 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041
2042 switch (value_type) {
2043 case Primitive::kPrimBoolean:
2044 case Primitive::kPrimByte: {
2045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002046 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002047 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002048 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002049 __ Addu(base_reg, obj, index.AsRegister<Register>());
2050 }
2051 if (value_location.IsConstant()) {
2052 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2053 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2054 } else {
2055 Register value = value_location.AsRegister<Register>();
2056 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002057 }
2058 break;
2059 }
2060
2061 case Primitive::kPrimShort:
2062 case Primitive::kPrimChar: {
2063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002065 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002067 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2068 __ Addu(base_reg, obj, base_reg);
2069 }
2070 if (value_location.IsConstant()) {
2071 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2072 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2073 } else {
2074 Register value = value_location.AsRegister<Register>();
2075 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 }
2077 break;
2078 }
2079
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimNot: {
2082 if (!needs_runtime_call) {
2083 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002084 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002085 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002087 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2088 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002090 if (value_location.IsConstant()) {
2091 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2092 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2093 DCHECK(!needs_write_barrier);
2094 } else {
2095 Register value = value_location.AsRegister<Register>();
2096 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2097 if (needs_write_barrier) {
2098 DCHECK_EQ(value_type, Primitive::kPrimNot);
2099 codegen_->MarkGCCard(obj, value);
2100 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002101 }
2102 } else {
2103 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002104 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002105 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2106 }
2107 break;
2108 }
2109
2110 case Primitive::kPrimLong: {
2111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002112 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002113 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002114 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002115 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2116 __ Addu(base_reg, obj, base_reg);
2117 }
2118 if (value_location.IsConstant()) {
2119 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2120 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2121 } else {
2122 Register value = value_location.AsRegisterPairLow<Register>();
2123 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 }
2125 break;
2126 }
2127
2128 case Primitive::kPrimFloat: {
2129 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002131 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002132 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002133 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2134 __ Addu(base_reg, obj, base_reg);
2135 }
2136 if (value_location.IsConstant()) {
2137 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2138 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2139 } else {
2140 FRegister value = value_location.AsFpuRegister<FRegister>();
2141 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142 }
2143 break;
2144 }
2145
2146 case Primitive::kPrimDouble: {
2147 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002149 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002150 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002151 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2152 __ Addu(base_reg, obj, base_reg);
2153 }
2154 if (value_location.IsConstant()) {
2155 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2156 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2157 } else {
2158 FRegister value = value_location.AsFpuRegister<FRegister>();
2159 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002160 }
2161 break;
2162 }
2163
2164 case Primitive::kPrimVoid:
2165 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2166 UNREACHABLE();
2167 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002168}
2169
2170void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002171 RegisterSet caller_saves = RegisterSet::Empty();
2172 InvokeRuntimeCallingConvention calling_convention;
2173 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2174 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2175 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002176 locations->SetInAt(0, Location::RequiresRegister());
2177 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002178}
2179
2180void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2181 LocationSummary* locations = instruction->GetLocations();
2182 BoundsCheckSlowPathMIPS* slow_path =
2183 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2184 codegen_->AddSlowPath(slow_path);
2185
2186 Register index = locations->InAt(0).AsRegister<Register>();
2187 Register length = locations->InAt(1).AsRegister<Register>();
2188
2189 // length is limited by the maximum positive signed 32-bit integer.
2190 // Unsigned comparison of length and index checks for index < 0
2191 // and for length <= index simultaneously.
2192 __ Bgeu(index, length, slow_path->GetEntryLabel());
2193}
2194
2195void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2197 instruction,
2198 LocationSummary::kCallOnSlowPath);
2199 locations->SetInAt(0, Location::RequiresRegister());
2200 locations->SetInAt(1, Location::RequiresRegister());
2201 // Note that TypeCheckSlowPathMIPS uses this register too.
2202 locations->AddTemp(Location::RequiresRegister());
2203}
2204
2205void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2206 LocationSummary* locations = instruction->GetLocations();
2207 Register obj = locations->InAt(0).AsRegister<Register>();
2208 Register cls = locations->InAt(1).AsRegister<Register>();
2209 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2210
2211 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2212 codegen_->AddSlowPath(slow_path);
2213
2214 // TODO: avoid this check if we know obj is not null.
2215 __ Beqz(obj, slow_path->GetExitLabel());
2216 // Compare the class of `obj` with `cls`.
2217 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2218 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2219 __ Bind(slow_path->GetExitLabel());
2220}
2221
2222void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2223 LocationSummary* locations =
2224 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2225 locations->SetInAt(0, Location::RequiresRegister());
2226 if (check->HasUses()) {
2227 locations->SetOut(Location::SameAsFirstInput());
2228 }
2229}
2230
2231void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2232 // We assume the class is not null.
2233 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2234 check->GetLoadClass(),
2235 check,
2236 check->GetDexPc(),
2237 true);
2238 codegen_->AddSlowPath(slow_path);
2239 GenerateClassInitializationCheck(slow_path,
2240 check->GetLocations()->InAt(0).AsRegister<Register>());
2241}
2242
2243void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2244 Primitive::Type in_type = compare->InputAt(0)->GetType();
2245
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002246 LocationSummary* locations =
2247 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002248
2249 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002250 case Primitive::kPrimBoolean:
2251 case Primitive::kPrimByte:
2252 case Primitive::kPrimShort:
2253 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002254 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002255 case Primitive::kPrimLong:
2256 locations->SetInAt(0, Location::RequiresRegister());
2257 locations->SetInAt(1, Location::RequiresRegister());
2258 // Output overlaps because it is written before doing the low comparison.
2259 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2260 break;
2261
2262 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002263 case Primitive::kPrimDouble:
2264 locations->SetInAt(0, Location::RequiresFpuRegister());
2265 locations->SetInAt(1, Location::RequiresFpuRegister());
2266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002267 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002268
2269 default:
2270 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2271 }
2272}
2273
2274void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2275 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002276 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002277 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002278 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002279
2280 // 0 if: left == right
2281 // 1 if: left > right
2282 // -1 if: left < right
2283 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002284 case Primitive::kPrimBoolean:
2285 case Primitive::kPrimByte:
2286 case Primitive::kPrimShort:
2287 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002288 case Primitive::kPrimInt: {
2289 Register lhs = locations->InAt(0).AsRegister<Register>();
2290 Register rhs = locations->InAt(1).AsRegister<Register>();
2291 __ Slt(TMP, lhs, rhs);
2292 __ Slt(res, rhs, lhs);
2293 __ Subu(res, res, TMP);
2294 break;
2295 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002296 case Primitive::kPrimLong: {
2297 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002298 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2299 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2300 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2301 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2302 // TODO: more efficient (direct) comparison with a constant.
2303 __ Slt(TMP, lhs_high, rhs_high);
2304 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2305 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2306 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2307 __ Sltu(TMP, lhs_low, rhs_low);
2308 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2309 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2310 __ Bind(&done);
2311 break;
2312 }
2313
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002314 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002315 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002316 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2317 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2318 MipsLabel done;
2319 if (isR6) {
2320 __ CmpEqS(FTMP, lhs, rhs);
2321 __ LoadConst32(res, 0);
2322 __ Bc1nez(FTMP, &done);
2323 if (gt_bias) {
2324 __ CmpLtS(FTMP, lhs, rhs);
2325 __ LoadConst32(res, -1);
2326 __ Bc1nez(FTMP, &done);
2327 __ LoadConst32(res, 1);
2328 } else {
2329 __ CmpLtS(FTMP, rhs, lhs);
2330 __ LoadConst32(res, 1);
2331 __ Bc1nez(FTMP, &done);
2332 __ LoadConst32(res, -1);
2333 }
2334 } else {
2335 if (gt_bias) {
2336 __ ColtS(0, lhs, rhs);
2337 __ LoadConst32(res, -1);
2338 __ Bc1t(0, &done);
2339 __ CeqS(0, lhs, rhs);
2340 __ LoadConst32(res, 1);
2341 __ Movt(res, ZERO, 0);
2342 } else {
2343 __ ColtS(0, rhs, lhs);
2344 __ LoadConst32(res, 1);
2345 __ Bc1t(0, &done);
2346 __ CeqS(0, lhs, rhs);
2347 __ LoadConst32(res, -1);
2348 __ Movt(res, ZERO, 0);
2349 }
2350 }
2351 __ Bind(&done);
2352 break;
2353 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002354 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002355 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002356 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2357 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2358 MipsLabel done;
2359 if (isR6) {
2360 __ CmpEqD(FTMP, lhs, rhs);
2361 __ LoadConst32(res, 0);
2362 __ Bc1nez(FTMP, &done);
2363 if (gt_bias) {
2364 __ CmpLtD(FTMP, lhs, rhs);
2365 __ LoadConst32(res, -1);
2366 __ Bc1nez(FTMP, &done);
2367 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002369 __ CmpLtD(FTMP, rhs, lhs);
2370 __ LoadConst32(res, 1);
2371 __ Bc1nez(FTMP, &done);
2372 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002373 }
2374 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002375 if (gt_bias) {
2376 __ ColtD(0, lhs, rhs);
2377 __ LoadConst32(res, -1);
2378 __ Bc1t(0, &done);
2379 __ CeqD(0, lhs, rhs);
2380 __ LoadConst32(res, 1);
2381 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002382 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002383 __ ColtD(0, rhs, lhs);
2384 __ LoadConst32(res, 1);
2385 __ Bc1t(0, &done);
2386 __ CeqD(0, lhs, rhs);
2387 __ LoadConst32(res, -1);
2388 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 }
2390 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002391 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002392 break;
2393 }
2394
2395 default:
2396 LOG(FATAL) << "Unimplemented compare type " << in_type;
2397 }
2398}
2399
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002400void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002401 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002402 switch (instruction->InputAt(0)->GetType()) {
2403 default:
2404 case Primitive::kPrimLong:
2405 locations->SetInAt(0, Location::RequiresRegister());
2406 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2407 break;
2408
2409 case Primitive::kPrimFloat:
2410 case Primitive::kPrimDouble:
2411 locations->SetInAt(0, Location::RequiresFpuRegister());
2412 locations->SetInAt(1, Location::RequiresFpuRegister());
2413 break;
2414 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002415 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2417 }
2418}
2419
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002420void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002421 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002422 return;
2423 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002425 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426 LocationSummary* locations = instruction->GetLocations();
2427 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002428 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002430 switch (type) {
2431 default:
2432 // Integer case.
2433 GenerateIntCompare(instruction->GetCondition(), locations);
2434 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002435
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002436 case Primitive::kPrimLong:
2437 // TODO: don't use branches.
2438 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002439 break;
2440
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002441 case Primitive::kPrimFloat:
2442 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002443 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2444 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002446
2447 // Convert the branches into the result.
2448 MipsLabel done;
2449
2450 // False case: result = 0.
2451 __ LoadConst32(dst, 0);
2452 __ B(&done);
2453
2454 // True case: result = 1.
2455 __ Bind(&true_label);
2456 __ LoadConst32(dst, 1);
2457 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002458}
2459
Alexey Frunze7e99e052015-11-24 19:28:01 -08002460void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2461 DCHECK(instruction->IsDiv() || instruction->IsRem());
2462 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2463
2464 LocationSummary* locations = instruction->GetLocations();
2465 Location second = locations->InAt(1);
2466 DCHECK(second.IsConstant());
2467
2468 Register out = locations->Out().AsRegister<Register>();
2469 Register dividend = locations->InAt(0).AsRegister<Register>();
2470 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2471 DCHECK(imm == 1 || imm == -1);
2472
2473 if (instruction->IsRem()) {
2474 __ Move(out, ZERO);
2475 } else {
2476 if (imm == -1) {
2477 __ Subu(out, ZERO, dividend);
2478 } else if (out != dividend) {
2479 __ Move(out, dividend);
2480 }
2481 }
2482}
2483
2484void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2485 DCHECK(instruction->IsDiv() || instruction->IsRem());
2486 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2487
2488 LocationSummary* locations = instruction->GetLocations();
2489 Location second = locations->InAt(1);
2490 DCHECK(second.IsConstant());
2491
2492 Register out = locations->Out().AsRegister<Register>();
2493 Register dividend = locations->InAt(0).AsRegister<Register>();
2494 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002495 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002496 int ctz_imm = CTZ(abs_imm);
2497
2498 if (instruction->IsDiv()) {
2499 if (ctz_imm == 1) {
2500 // Fast path for division by +/-2, which is very common.
2501 __ Srl(TMP, dividend, 31);
2502 } else {
2503 __ Sra(TMP, dividend, 31);
2504 __ Srl(TMP, TMP, 32 - ctz_imm);
2505 }
2506 __ Addu(out, dividend, TMP);
2507 __ Sra(out, out, ctz_imm);
2508 if (imm < 0) {
2509 __ Subu(out, ZERO, out);
2510 }
2511 } else {
2512 if (ctz_imm == 1) {
2513 // Fast path for modulo +/-2, which is very common.
2514 __ Sra(TMP, dividend, 31);
2515 __ Subu(out, dividend, TMP);
2516 __ Andi(out, out, 1);
2517 __ Addu(out, out, TMP);
2518 } else {
2519 __ Sra(TMP, dividend, 31);
2520 __ Srl(TMP, TMP, 32 - ctz_imm);
2521 __ Addu(out, dividend, TMP);
2522 if (IsUint<16>(abs_imm - 1)) {
2523 __ Andi(out, out, abs_imm - 1);
2524 } else {
2525 __ Sll(out, out, 32 - ctz_imm);
2526 __ Srl(out, out, 32 - ctz_imm);
2527 }
2528 __ Subu(out, out, TMP);
2529 }
2530 }
2531}
2532
2533void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2534 DCHECK(instruction->IsDiv() || instruction->IsRem());
2535 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2536
2537 LocationSummary* locations = instruction->GetLocations();
2538 Location second = locations->InAt(1);
2539 DCHECK(second.IsConstant());
2540
2541 Register out = locations->Out().AsRegister<Register>();
2542 Register dividend = locations->InAt(0).AsRegister<Register>();
2543 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2544
2545 int64_t magic;
2546 int shift;
2547 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2548
2549 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2550
2551 __ LoadConst32(TMP, magic);
2552 if (isR6) {
2553 __ MuhR6(TMP, dividend, TMP);
2554 } else {
2555 __ MultR2(dividend, TMP);
2556 __ Mfhi(TMP);
2557 }
2558 if (imm > 0 && magic < 0) {
2559 __ Addu(TMP, TMP, dividend);
2560 } else if (imm < 0 && magic > 0) {
2561 __ Subu(TMP, TMP, dividend);
2562 }
2563
2564 if (shift != 0) {
2565 __ Sra(TMP, TMP, shift);
2566 }
2567
2568 if (instruction->IsDiv()) {
2569 __ Sra(out, TMP, 31);
2570 __ Subu(out, TMP, out);
2571 } else {
2572 __ Sra(AT, TMP, 31);
2573 __ Subu(AT, TMP, AT);
2574 __ LoadConst32(TMP, imm);
2575 if (isR6) {
2576 __ MulR6(TMP, AT, TMP);
2577 } else {
2578 __ MulR2(TMP, AT, TMP);
2579 }
2580 __ Subu(out, dividend, TMP);
2581 }
2582}
2583
2584void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2585 DCHECK(instruction->IsDiv() || instruction->IsRem());
2586 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2587
2588 LocationSummary* locations = instruction->GetLocations();
2589 Register out = locations->Out().AsRegister<Register>();
2590 Location second = locations->InAt(1);
2591
2592 if (second.IsConstant()) {
2593 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2594 if (imm == 0) {
2595 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2596 } else if (imm == 1 || imm == -1) {
2597 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002598 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002599 DivRemByPowerOfTwo(instruction);
2600 } else {
2601 DCHECK(imm <= -2 || imm >= 2);
2602 GenerateDivRemWithAnyConstant(instruction);
2603 }
2604 } else {
2605 Register dividend = locations->InAt(0).AsRegister<Register>();
2606 Register divisor = second.AsRegister<Register>();
2607 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2608 if (instruction->IsDiv()) {
2609 if (isR6) {
2610 __ DivR6(out, dividend, divisor);
2611 } else {
2612 __ DivR2(out, dividend, divisor);
2613 }
2614 } else {
2615 if (isR6) {
2616 __ ModR6(out, dividend, divisor);
2617 } else {
2618 __ ModR2(out, dividend, divisor);
2619 }
2620 }
2621 }
2622}
2623
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2625 Primitive::Type type = div->GetResultType();
2626 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002627 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 : LocationSummary::kNoCall;
2629
2630 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2631
2632 switch (type) {
2633 case Primitive::kPrimInt:
2634 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002635 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002636 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2637 break;
2638
2639 case Primitive::kPrimLong: {
2640 InvokeRuntimeCallingConvention calling_convention;
2641 locations->SetInAt(0, Location::RegisterPairLocation(
2642 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2643 locations->SetInAt(1, Location::RegisterPairLocation(
2644 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2645 locations->SetOut(calling_convention.GetReturnLocation(type));
2646 break;
2647 }
2648
2649 case Primitive::kPrimFloat:
2650 case Primitive::kPrimDouble:
2651 locations->SetInAt(0, Location::RequiresFpuRegister());
2652 locations->SetInAt(1, Location::RequiresFpuRegister());
2653 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2654 break;
2655
2656 default:
2657 LOG(FATAL) << "Unexpected div type " << type;
2658 }
2659}
2660
2661void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2662 Primitive::Type type = instruction->GetType();
2663 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002664
2665 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002666 case Primitive::kPrimInt:
2667 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002668 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002670 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2672 break;
2673 }
2674 case Primitive::kPrimFloat:
2675 case Primitive::kPrimDouble: {
2676 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2677 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2678 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2679 if (type == Primitive::kPrimFloat) {
2680 __ DivS(dst, lhs, rhs);
2681 } else {
2682 __ DivD(dst, lhs, rhs);
2683 }
2684 break;
2685 }
2686 default:
2687 LOG(FATAL) << "Unexpected div type " << type;
2688 }
2689}
2690
2691void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002692 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002693 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002694}
2695
2696void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2697 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2698 codegen_->AddSlowPath(slow_path);
2699 Location value = instruction->GetLocations()->InAt(0);
2700 Primitive::Type type = instruction->GetType();
2701
2702 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002703 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002704 case Primitive::kPrimByte:
2705 case Primitive::kPrimChar:
2706 case Primitive::kPrimShort:
2707 case Primitive::kPrimInt: {
2708 if (value.IsConstant()) {
2709 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2710 __ B(slow_path->GetEntryLabel());
2711 } else {
2712 // A division by a non-null constant is valid. We don't need to perform
2713 // any check, so simply fall through.
2714 }
2715 } else {
2716 DCHECK(value.IsRegister()) << value;
2717 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2718 }
2719 break;
2720 }
2721 case Primitive::kPrimLong: {
2722 if (value.IsConstant()) {
2723 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2724 __ B(slow_path->GetEntryLabel());
2725 } else {
2726 // A division by a non-null constant is valid. We don't need to perform
2727 // any check, so simply fall through.
2728 }
2729 } else {
2730 DCHECK(value.IsRegisterPair()) << value;
2731 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2732 __ Beqz(TMP, slow_path->GetEntryLabel());
2733 }
2734 break;
2735 }
2736 default:
2737 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2738 }
2739}
2740
2741void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2742 LocationSummary* locations =
2743 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2744 locations->SetOut(Location::ConstantLocation(constant));
2745}
2746
2747void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2748 // Will be generated at use site.
2749}
2750
2751void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2752 exit->SetLocations(nullptr);
2753}
2754
2755void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2756}
2757
2758void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2759 LocationSummary* locations =
2760 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2761 locations->SetOut(Location::ConstantLocation(constant));
2762}
2763
2764void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2765 // Will be generated at use site.
2766}
2767
2768void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2769 got->SetLocations(nullptr);
2770}
2771
2772void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2773 DCHECK(!successor->IsExitBlock());
2774 HBasicBlock* block = got->GetBlock();
2775 HInstruction* previous = got->GetPrevious();
2776 HLoopInformation* info = block->GetLoopInformation();
2777
2778 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2779 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2780 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2781 return;
2782 }
2783 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2784 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2785 }
2786 if (!codegen_->GoesToNextBlock(block, successor)) {
2787 __ B(codegen_->GetLabelOf(successor));
2788 }
2789}
2790
2791void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2792 HandleGoto(got, got->GetSuccessor());
2793}
2794
2795void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2796 try_boundary->SetLocations(nullptr);
2797}
2798
2799void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2800 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2801 if (!successor->IsExitBlock()) {
2802 HandleGoto(try_boundary, successor);
2803 }
2804}
2805
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002806void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2807 LocationSummary* locations) {
2808 Register dst = locations->Out().AsRegister<Register>();
2809 Register lhs = locations->InAt(0).AsRegister<Register>();
2810 Location rhs_location = locations->InAt(1);
2811 Register rhs_reg = ZERO;
2812 int64_t rhs_imm = 0;
2813 bool use_imm = rhs_location.IsConstant();
2814 if (use_imm) {
2815 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2816 } else {
2817 rhs_reg = rhs_location.AsRegister<Register>();
2818 }
2819
2820 switch (cond) {
2821 case kCondEQ:
2822 case kCondNE:
2823 if (use_imm && IsUint<16>(rhs_imm)) {
2824 __ Xori(dst, lhs, rhs_imm);
2825 } else {
2826 if (use_imm) {
2827 rhs_reg = TMP;
2828 __ LoadConst32(rhs_reg, rhs_imm);
2829 }
2830 __ Xor(dst, lhs, rhs_reg);
2831 }
2832 if (cond == kCondEQ) {
2833 __ Sltiu(dst, dst, 1);
2834 } else {
2835 __ Sltu(dst, ZERO, dst);
2836 }
2837 break;
2838
2839 case kCondLT:
2840 case kCondGE:
2841 if (use_imm && IsInt<16>(rhs_imm)) {
2842 __ Slti(dst, lhs, rhs_imm);
2843 } else {
2844 if (use_imm) {
2845 rhs_reg = TMP;
2846 __ LoadConst32(rhs_reg, rhs_imm);
2847 }
2848 __ Slt(dst, lhs, rhs_reg);
2849 }
2850 if (cond == kCondGE) {
2851 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2852 // only the slt instruction but no sge.
2853 __ Xori(dst, dst, 1);
2854 }
2855 break;
2856
2857 case kCondLE:
2858 case kCondGT:
2859 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2860 // Simulate lhs <= rhs via lhs < rhs + 1.
2861 __ Slti(dst, lhs, rhs_imm + 1);
2862 if (cond == kCondGT) {
2863 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2864 // only the slti instruction but no sgti.
2865 __ Xori(dst, dst, 1);
2866 }
2867 } else {
2868 if (use_imm) {
2869 rhs_reg = TMP;
2870 __ LoadConst32(rhs_reg, rhs_imm);
2871 }
2872 __ Slt(dst, rhs_reg, lhs);
2873 if (cond == kCondLE) {
2874 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2875 // only the slt instruction but no sle.
2876 __ Xori(dst, dst, 1);
2877 }
2878 }
2879 break;
2880
2881 case kCondB:
2882 case kCondAE:
2883 if (use_imm && IsInt<16>(rhs_imm)) {
2884 // Sltiu sign-extends its 16-bit immediate operand before
2885 // the comparison and thus lets us compare directly with
2886 // unsigned values in the ranges [0, 0x7fff] and
2887 // [0xffff8000, 0xffffffff].
2888 __ Sltiu(dst, lhs, rhs_imm);
2889 } else {
2890 if (use_imm) {
2891 rhs_reg = TMP;
2892 __ LoadConst32(rhs_reg, rhs_imm);
2893 }
2894 __ Sltu(dst, lhs, rhs_reg);
2895 }
2896 if (cond == kCondAE) {
2897 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2898 // only the sltu instruction but no sgeu.
2899 __ Xori(dst, dst, 1);
2900 }
2901 break;
2902
2903 case kCondBE:
2904 case kCondA:
2905 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2906 // Simulate lhs <= rhs via lhs < rhs + 1.
2907 // Note that this only works if rhs + 1 does not overflow
2908 // to 0, hence the check above.
2909 // Sltiu sign-extends its 16-bit immediate operand before
2910 // the comparison and thus lets us compare directly with
2911 // unsigned values in the ranges [0, 0x7fff] and
2912 // [0xffff8000, 0xffffffff].
2913 __ Sltiu(dst, lhs, rhs_imm + 1);
2914 if (cond == kCondA) {
2915 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2916 // only the sltiu instruction but no sgtiu.
2917 __ Xori(dst, dst, 1);
2918 }
2919 } else {
2920 if (use_imm) {
2921 rhs_reg = TMP;
2922 __ LoadConst32(rhs_reg, rhs_imm);
2923 }
2924 __ Sltu(dst, rhs_reg, lhs);
2925 if (cond == kCondBE) {
2926 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2927 // only the sltu instruction but no sleu.
2928 __ Xori(dst, dst, 1);
2929 }
2930 }
2931 break;
2932 }
2933}
2934
2935void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2936 LocationSummary* locations,
2937 MipsLabel* label) {
2938 Register lhs = locations->InAt(0).AsRegister<Register>();
2939 Location rhs_location = locations->InAt(1);
2940 Register rhs_reg = ZERO;
2941 int32_t rhs_imm = 0;
2942 bool use_imm = rhs_location.IsConstant();
2943 if (use_imm) {
2944 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2945 } else {
2946 rhs_reg = rhs_location.AsRegister<Register>();
2947 }
2948
2949 if (use_imm && rhs_imm == 0) {
2950 switch (cond) {
2951 case kCondEQ:
2952 case kCondBE: // <= 0 if zero
2953 __ Beqz(lhs, label);
2954 break;
2955 case kCondNE:
2956 case kCondA: // > 0 if non-zero
2957 __ Bnez(lhs, label);
2958 break;
2959 case kCondLT:
2960 __ Bltz(lhs, label);
2961 break;
2962 case kCondGE:
2963 __ Bgez(lhs, label);
2964 break;
2965 case kCondLE:
2966 __ Blez(lhs, label);
2967 break;
2968 case kCondGT:
2969 __ Bgtz(lhs, label);
2970 break;
2971 case kCondB: // always false
2972 break;
2973 case kCondAE: // always true
2974 __ B(label);
2975 break;
2976 }
2977 } else {
2978 if (use_imm) {
2979 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2980 rhs_reg = TMP;
2981 __ LoadConst32(rhs_reg, rhs_imm);
2982 }
2983 switch (cond) {
2984 case kCondEQ:
2985 __ Beq(lhs, rhs_reg, label);
2986 break;
2987 case kCondNE:
2988 __ Bne(lhs, rhs_reg, label);
2989 break;
2990 case kCondLT:
2991 __ Blt(lhs, rhs_reg, label);
2992 break;
2993 case kCondGE:
2994 __ Bge(lhs, rhs_reg, label);
2995 break;
2996 case kCondLE:
2997 __ Bge(rhs_reg, lhs, label);
2998 break;
2999 case kCondGT:
3000 __ Blt(rhs_reg, lhs, label);
3001 break;
3002 case kCondB:
3003 __ Bltu(lhs, rhs_reg, label);
3004 break;
3005 case kCondAE:
3006 __ Bgeu(lhs, rhs_reg, label);
3007 break;
3008 case kCondBE:
3009 __ Bgeu(rhs_reg, lhs, label);
3010 break;
3011 case kCondA:
3012 __ Bltu(rhs_reg, lhs, label);
3013 break;
3014 }
3015 }
3016}
3017
3018void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3019 LocationSummary* locations,
3020 MipsLabel* label) {
3021 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3022 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3023 Location rhs_location = locations->InAt(1);
3024 Register rhs_high = ZERO;
3025 Register rhs_low = ZERO;
3026 int64_t imm = 0;
3027 uint32_t imm_high = 0;
3028 uint32_t imm_low = 0;
3029 bool use_imm = rhs_location.IsConstant();
3030 if (use_imm) {
3031 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3032 imm_high = High32Bits(imm);
3033 imm_low = Low32Bits(imm);
3034 } else {
3035 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3036 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3037 }
3038
3039 if (use_imm && imm == 0) {
3040 switch (cond) {
3041 case kCondEQ:
3042 case kCondBE: // <= 0 if zero
3043 __ Or(TMP, lhs_high, lhs_low);
3044 __ Beqz(TMP, label);
3045 break;
3046 case kCondNE:
3047 case kCondA: // > 0 if non-zero
3048 __ Or(TMP, lhs_high, lhs_low);
3049 __ Bnez(TMP, label);
3050 break;
3051 case kCondLT:
3052 __ Bltz(lhs_high, label);
3053 break;
3054 case kCondGE:
3055 __ Bgez(lhs_high, label);
3056 break;
3057 case kCondLE:
3058 __ Or(TMP, lhs_high, lhs_low);
3059 __ Sra(AT, lhs_high, 31);
3060 __ Bgeu(AT, TMP, label);
3061 break;
3062 case kCondGT:
3063 __ Or(TMP, lhs_high, lhs_low);
3064 __ Sra(AT, lhs_high, 31);
3065 __ Bltu(AT, TMP, label);
3066 break;
3067 case kCondB: // always false
3068 break;
3069 case kCondAE: // always true
3070 __ B(label);
3071 break;
3072 }
3073 } else if (use_imm) {
3074 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3075 switch (cond) {
3076 case kCondEQ:
3077 __ LoadConst32(TMP, imm_high);
3078 __ Xor(TMP, TMP, lhs_high);
3079 __ LoadConst32(AT, imm_low);
3080 __ Xor(AT, AT, lhs_low);
3081 __ Or(TMP, TMP, AT);
3082 __ Beqz(TMP, label);
3083 break;
3084 case kCondNE:
3085 __ LoadConst32(TMP, imm_high);
3086 __ Xor(TMP, TMP, lhs_high);
3087 __ LoadConst32(AT, imm_low);
3088 __ Xor(AT, AT, lhs_low);
3089 __ Or(TMP, TMP, AT);
3090 __ Bnez(TMP, label);
3091 break;
3092 case kCondLT:
3093 __ LoadConst32(TMP, imm_high);
3094 __ Blt(lhs_high, TMP, label);
3095 __ Slt(TMP, TMP, lhs_high);
3096 __ LoadConst32(AT, imm_low);
3097 __ Sltu(AT, lhs_low, AT);
3098 __ Blt(TMP, AT, label);
3099 break;
3100 case kCondGE:
3101 __ LoadConst32(TMP, imm_high);
3102 __ Blt(TMP, lhs_high, label);
3103 __ Slt(TMP, lhs_high, TMP);
3104 __ LoadConst32(AT, imm_low);
3105 __ Sltu(AT, lhs_low, AT);
3106 __ Or(TMP, TMP, AT);
3107 __ Beqz(TMP, label);
3108 break;
3109 case kCondLE:
3110 __ LoadConst32(TMP, imm_high);
3111 __ Blt(lhs_high, TMP, label);
3112 __ Slt(TMP, TMP, lhs_high);
3113 __ LoadConst32(AT, imm_low);
3114 __ Sltu(AT, AT, lhs_low);
3115 __ Or(TMP, TMP, AT);
3116 __ Beqz(TMP, label);
3117 break;
3118 case kCondGT:
3119 __ LoadConst32(TMP, imm_high);
3120 __ Blt(TMP, lhs_high, label);
3121 __ Slt(TMP, lhs_high, TMP);
3122 __ LoadConst32(AT, imm_low);
3123 __ Sltu(AT, AT, lhs_low);
3124 __ Blt(TMP, AT, label);
3125 break;
3126 case kCondB:
3127 __ LoadConst32(TMP, imm_high);
3128 __ Bltu(lhs_high, TMP, label);
3129 __ Sltu(TMP, TMP, lhs_high);
3130 __ LoadConst32(AT, imm_low);
3131 __ Sltu(AT, lhs_low, AT);
3132 __ Blt(TMP, AT, label);
3133 break;
3134 case kCondAE:
3135 __ LoadConst32(TMP, imm_high);
3136 __ Bltu(TMP, lhs_high, label);
3137 __ Sltu(TMP, lhs_high, TMP);
3138 __ LoadConst32(AT, imm_low);
3139 __ Sltu(AT, lhs_low, AT);
3140 __ Or(TMP, TMP, AT);
3141 __ Beqz(TMP, label);
3142 break;
3143 case kCondBE:
3144 __ LoadConst32(TMP, imm_high);
3145 __ Bltu(lhs_high, TMP, label);
3146 __ Sltu(TMP, TMP, lhs_high);
3147 __ LoadConst32(AT, imm_low);
3148 __ Sltu(AT, AT, lhs_low);
3149 __ Or(TMP, TMP, AT);
3150 __ Beqz(TMP, label);
3151 break;
3152 case kCondA:
3153 __ LoadConst32(TMP, imm_high);
3154 __ Bltu(TMP, lhs_high, label);
3155 __ Sltu(TMP, lhs_high, TMP);
3156 __ LoadConst32(AT, imm_low);
3157 __ Sltu(AT, AT, lhs_low);
3158 __ Blt(TMP, AT, label);
3159 break;
3160 }
3161 } else {
3162 switch (cond) {
3163 case kCondEQ:
3164 __ Xor(TMP, lhs_high, rhs_high);
3165 __ Xor(AT, lhs_low, rhs_low);
3166 __ Or(TMP, TMP, AT);
3167 __ Beqz(TMP, label);
3168 break;
3169 case kCondNE:
3170 __ Xor(TMP, lhs_high, rhs_high);
3171 __ Xor(AT, lhs_low, rhs_low);
3172 __ Or(TMP, TMP, AT);
3173 __ Bnez(TMP, label);
3174 break;
3175 case kCondLT:
3176 __ Blt(lhs_high, rhs_high, label);
3177 __ Slt(TMP, rhs_high, lhs_high);
3178 __ Sltu(AT, lhs_low, rhs_low);
3179 __ Blt(TMP, AT, label);
3180 break;
3181 case kCondGE:
3182 __ Blt(rhs_high, lhs_high, label);
3183 __ Slt(TMP, lhs_high, rhs_high);
3184 __ Sltu(AT, lhs_low, rhs_low);
3185 __ Or(TMP, TMP, AT);
3186 __ Beqz(TMP, label);
3187 break;
3188 case kCondLE:
3189 __ Blt(lhs_high, rhs_high, label);
3190 __ Slt(TMP, rhs_high, lhs_high);
3191 __ Sltu(AT, rhs_low, lhs_low);
3192 __ Or(TMP, TMP, AT);
3193 __ Beqz(TMP, label);
3194 break;
3195 case kCondGT:
3196 __ Blt(rhs_high, lhs_high, label);
3197 __ Slt(TMP, lhs_high, rhs_high);
3198 __ Sltu(AT, rhs_low, lhs_low);
3199 __ Blt(TMP, AT, label);
3200 break;
3201 case kCondB:
3202 __ Bltu(lhs_high, rhs_high, label);
3203 __ Sltu(TMP, rhs_high, lhs_high);
3204 __ Sltu(AT, lhs_low, rhs_low);
3205 __ Blt(TMP, AT, label);
3206 break;
3207 case kCondAE:
3208 __ Bltu(rhs_high, lhs_high, label);
3209 __ Sltu(TMP, lhs_high, rhs_high);
3210 __ Sltu(AT, lhs_low, rhs_low);
3211 __ Or(TMP, TMP, AT);
3212 __ Beqz(TMP, label);
3213 break;
3214 case kCondBE:
3215 __ Bltu(lhs_high, rhs_high, label);
3216 __ Sltu(TMP, rhs_high, lhs_high);
3217 __ Sltu(AT, rhs_low, lhs_low);
3218 __ Or(TMP, TMP, AT);
3219 __ Beqz(TMP, label);
3220 break;
3221 case kCondA:
3222 __ Bltu(rhs_high, lhs_high, label);
3223 __ Sltu(TMP, lhs_high, rhs_high);
3224 __ Sltu(AT, rhs_low, lhs_low);
3225 __ Blt(TMP, AT, label);
3226 break;
3227 }
3228 }
3229}
3230
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003231void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3232 bool gt_bias,
3233 Primitive::Type type,
3234 LocationSummary* locations) {
3235 Register dst = locations->Out().AsRegister<Register>();
3236 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3237 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3238 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3239 if (type == Primitive::kPrimFloat) {
3240 if (isR6) {
3241 switch (cond) {
3242 case kCondEQ:
3243 __ CmpEqS(FTMP, lhs, rhs);
3244 __ Mfc1(dst, FTMP);
3245 __ Andi(dst, dst, 1);
3246 break;
3247 case kCondNE:
3248 __ CmpEqS(FTMP, lhs, rhs);
3249 __ Mfc1(dst, FTMP);
3250 __ Addiu(dst, dst, 1);
3251 break;
3252 case kCondLT:
3253 if (gt_bias) {
3254 __ CmpLtS(FTMP, lhs, rhs);
3255 } else {
3256 __ CmpUltS(FTMP, lhs, rhs);
3257 }
3258 __ Mfc1(dst, FTMP);
3259 __ Andi(dst, dst, 1);
3260 break;
3261 case kCondLE:
3262 if (gt_bias) {
3263 __ CmpLeS(FTMP, lhs, rhs);
3264 } else {
3265 __ CmpUleS(FTMP, lhs, rhs);
3266 }
3267 __ Mfc1(dst, FTMP);
3268 __ Andi(dst, dst, 1);
3269 break;
3270 case kCondGT:
3271 if (gt_bias) {
3272 __ CmpUltS(FTMP, rhs, lhs);
3273 } else {
3274 __ CmpLtS(FTMP, rhs, lhs);
3275 }
3276 __ Mfc1(dst, FTMP);
3277 __ Andi(dst, dst, 1);
3278 break;
3279 case kCondGE:
3280 if (gt_bias) {
3281 __ CmpUleS(FTMP, rhs, lhs);
3282 } else {
3283 __ CmpLeS(FTMP, rhs, lhs);
3284 }
3285 __ Mfc1(dst, FTMP);
3286 __ Andi(dst, dst, 1);
3287 break;
3288 default:
3289 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3290 UNREACHABLE();
3291 }
3292 } else {
3293 switch (cond) {
3294 case kCondEQ:
3295 __ CeqS(0, lhs, rhs);
3296 __ LoadConst32(dst, 1);
3297 __ Movf(dst, ZERO, 0);
3298 break;
3299 case kCondNE:
3300 __ CeqS(0, lhs, rhs);
3301 __ LoadConst32(dst, 1);
3302 __ Movt(dst, ZERO, 0);
3303 break;
3304 case kCondLT:
3305 if (gt_bias) {
3306 __ ColtS(0, lhs, rhs);
3307 } else {
3308 __ CultS(0, lhs, rhs);
3309 }
3310 __ LoadConst32(dst, 1);
3311 __ Movf(dst, ZERO, 0);
3312 break;
3313 case kCondLE:
3314 if (gt_bias) {
3315 __ ColeS(0, lhs, rhs);
3316 } else {
3317 __ CuleS(0, lhs, rhs);
3318 }
3319 __ LoadConst32(dst, 1);
3320 __ Movf(dst, ZERO, 0);
3321 break;
3322 case kCondGT:
3323 if (gt_bias) {
3324 __ CultS(0, rhs, lhs);
3325 } else {
3326 __ ColtS(0, rhs, lhs);
3327 }
3328 __ LoadConst32(dst, 1);
3329 __ Movf(dst, ZERO, 0);
3330 break;
3331 case kCondGE:
3332 if (gt_bias) {
3333 __ CuleS(0, rhs, lhs);
3334 } else {
3335 __ ColeS(0, rhs, lhs);
3336 }
3337 __ LoadConst32(dst, 1);
3338 __ Movf(dst, ZERO, 0);
3339 break;
3340 default:
3341 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3342 UNREACHABLE();
3343 }
3344 }
3345 } else {
3346 DCHECK_EQ(type, Primitive::kPrimDouble);
3347 if (isR6) {
3348 switch (cond) {
3349 case kCondEQ:
3350 __ CmpEqD(FTMP, lhs, rhs);
3351 __ Mfc1(dst, FTMP);
3352 __ Andi(dst, dst, 1);
3353 break;
3354 case kCondNE:
3355 __ CmpEqD(FTMP, lhs, rhs);
3356 __ Mfc1(dst, FTMP);
3357 __ Addiu(dst, dst, 1);
3358 break;
3359 case kCondLT:
3360 if (gt_bias) {
3361 __ CmpLtD(FTMP, lhs, rhs);
3362 } else {
3363 __ CmpUltD(FTMP, lhs, rhs);
3364 }
3365 __ Mfc1(dst, FTMP);
3366 __ Andi(dst, dst, 1);
3367 break;
3368 case kCondLE:
3369 if (gt_bias) {
3370 __ CmpLeD(FTMP, lhs, rhs);
3371 } else {
3372 __ CmpUleD(FTMP, lhs, rhs);
3373 }
3374 __ Mfc1(dst, FTMP);
3375 __ Andi(dst, dst, 1);
3376 break;
3377 case kCondGT:
3378 if (gt_bias) {
3379 __ CmpUltD(FTMP, rhs, lhs);
3380 } else {
3381 __ CmpLtD(FTMP, rhs, lhs);
3382 }
3383 __ Mfc1(dst, FTMP);
3384 __ Andi(dst, dst, 1);
3385 break;
3386 case kCondGE:
3387 if (gt_bias) {
3388 __ CmpUleD(FTMP, rhs, lhs);
3389 } else {
3390 __ CmpLeD(FTMP, rhs, lhs);
3391 }
3392 __ Mfc1(dst, FTMP);
3393 __ Andi(dst, dst, 1);
3394 break;
3395 default:
3396 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3397 UNREACHABLE();
3398 }
3399 } else {
3400 switch (cond) {
3401 case kCondEQ:
3402 __ CeqD(0, lhs, rhs);
3403 __ LoadConst32(dst, 1);
3404 __ Movf(dst, ZERO, 0);
3405 break;
3406 case kCondNE:
3407 __ CeqD(0, lhs, rhs);
3408 __ LoadConst32(dst, 1);
3409 __ Movt(dst, ZERO, 0);
3410 break;
3411 case kCondLT:
3412 if (gt_bias) {
3413 __ ColtD(0, lhs, rhs);
3414 } else {
3415 __ CultD(0, lhs, rhs);
3416 }
3417 __ LoadConst32(dst, 1);
3418 __ Movf(dst, ZERO, 0);
3419 break;
3420 case kCondLE:
3421 if (gt_bias) {
3422 __ ColeD(0, lhs, rhs);
3423 } else {
3424 __ CuleD(0, lhs, rhs);
3425 }
3426 __ LoadConst32(dst, 1);
3427 __ Movf(dst, ZERO, 0);
3428 break;
3429 case kCondGT:
3430 if (gt_bias) {
3431 __ CultD(0, rhs, lhs);
3432 } else {
3433 __ ColtD(0, rhs, lhs);
3434 }
3435 __ LoadConst32(dst, 1);
3436 __ Movf(dst, ZERO, 0);
3437 break;
3438 case kCondGE:
3439 if (gt_bias) {
3440 __ CuleD(0, rhs, lhs);
3441 } else {
3442 __ ColeD(0, rhs, lhs);
3443 }
3444 __ LoadConst32(dst, 1);
3445 __ Movf(dst, ZERO, 0);
3446 break;
3447 default:
3448 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3449 UNREACHABLE();
3450 }
3451 }
3452 }
3453}
3454
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003455void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3456 bool gt_bias,
3457 Primitive::Type type,
3458 LocationSummary* locations,
3459 MipsLabel* label) {
3460 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3461 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3462 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3463 if (type == Primitive::kPrimFloat) {
3464 if (isR6) {
3465 switch (cond) {
3466 case kCondEQ:
3467 __ CmpEqS(FTMP, lhs, rhs);
3468 __ Bc1nez(FTMP, label);
3469 break;
3470 case kCondNE:
3471 __ CmpEqS(FTMP, lhs, rhs);
3472 __ Bc1eqz(FTMP, label);
3473 break;
3474 case kCondLT:
3475 if (gt_bias) {
3476 __ CmpLtS(FTMP, lhs, rhs);
3477 } else {
3478 __ CmpUltS(FTMP, lhs, rhs);
3479 }
3480 __ Bc1nez(FTMP, label);
3481 break;
3482 case kCondLE:
3483 if (gt_bias) {
3484 __ CmpLeS(FTMP, lhs, rhs);
3485 } else {
3486 __ CmpUleS(FTMP, lhs, rhs);
3487 }
3488 __ Bc1nez(FTMP, label);
3489 break;
3490 case kCondGT:
3491 if (gt_bias) {
3492 __ CmpUltS(FTMP, rhs, lhs);
3493 } else {
3494 __ CmpLtS(FTMP, rhs, lhs);
3495 }
3496 __ Bc1nez(FTMP, label);
3497 break;
3498 case kCondGE:
3499 if (gt_bias) {
3500 __ CmpUleS(FTMP, rhs, lhs);
3501 } else {
3502 __ CmpLeS(FTMP, rhs, lhs);
3503 }
3504 __ Bc1nez(FTMP, label);
3505 break;
3506 default:
3507 LOG(FATAL) << "Unexpected non-floating-point condition";
3508 }
3509 } else {
3510 switch (cond) {
3511 case kCondEQ:
3512 __ CeqS(0, lhs, rhs);
3513 __ Bc1t(0, label);
3514 break;
3515 case kCondNE:
3516 __ CeqS(0, lhs, rhs);
3517 __ Bc1f(0, label);
3518 break;
3519 case kCondLT:
3520 if (gt_bias) {
3521 __ ColtS(0, lhs, rhs);
3522 } else {
3523 __ CultS(0, lhs, rhs);
3524 }
3525 __ Bc1t(0, label);
3526 break;
3527 case kCondLE:
3528 if (gt_bias) {
3529 __ ColeS(0, lhs, rhs);
3530 } else {
3531 __ CuleS(0, lhs, rhs);
3532 }
3533 __ Bc1t(0, label);
3534 break;
3535 case kCondGT:
3536 if (gt_bias) {
3537 __ CultS(0, rhs, lhs);
3538 } else {
3539 __ ColtS(0, rhs, lhs);
3540 }
3541 __ Bc1t(0, label);
3542 break;
3543 case kCondGE:
3544 if (gt_bias) {
3545 __ CuleS(0, rhs, lhs);
3546 } else {
3547 __ ColeS(0, rhs, lhs);
3548 }
3549 __ Bc1t(0, label);
3550 break;
3551 default:
3552 LOG(FATAL) << "Unexpected non-floating-point condition";
3553 }
3554 }
3555 } else {
3556 DCHECK_EQ(type, Primitive::kPrimDouble);
3557 if (isR6) {
3558 switch (cond) {
3559 case kCondEQ:
3560 __ CmpEqD(FTMP, lhs, rhs);
3561 __ Bc1nez(FTMP, label);
3562 break;
3563 case kCondNE:
3564 __ CmpEqD(FTMP, lhs, rhs);
3565 __ Bc1eqz(FTMP, label);
3566 break;
3567 case kCondLT:
3568 if (gt_bias) {
3569 __ CmpLtD(FTMP, lhs, rhs);
3570 } else {
3571 __ CmpUltD(FTMP, lhs, rhs);
3572 }
3573 __ Bc1nez(FTMP, label);
3574 break;
3575 case kCondLE:
3576 if (gt_bias) {
3577 __ CmpLeD(FTMP, lhs, rhs);
3578 } else {
3579 __ CmpUleD(FTMP, lhs, rhs);
3580 }
3581 __ Bc1nez(FTMP, label);
3582 break;
3583 case kCondGT:
3584 if (gt_bias) {
3585 __ CmpUltD(FTMP, rhs, lhs);
3586 } else {
3587 __ CmpLtD(FTMP, rhs, lhs);
3588 }
3589 __ Bc1nez(FTMP, label);
3590 break;
3591 case kCondGE:
3592 if (gt_bias) {
3593 __ CmpUleD(FTMP, rhs, lhs);
3594 } else {
3595 __ CmpLeD(FTMP, rhs, lhs);
3596 }
3597 __ Bc1nez(FTMP, label);
3598 break;
3599 default:
3600 LOG(FATAL) << "Unexpected non-floating-point condition";
3601 }
3602 } else {
3603 switch (cond) {
3604 case kCondEQ:
3605 __ CeqD(0, lhs, rhs);
3606 __ Bc1t(0, label);
3607 break;
3608 case kCondNE:
3609 __ CeqD(0, lhs, rhs);
3610 __ Bc1f(0, label);
3611 break;
3612 case kCondLT:
3613 if (gt_bias) {
3614 __ ColtD(0, lhs, rhs);
3615 } else {
3616 __ CultD(0, lhs, rhs);
3617 }
3618 __ Bc1t(0, label);
3619 break;
3620 case kCondLE:
3621 if (gt_bias) {
3622 __ ColeD(0, lhs, rhs);
3623 } else {
3624 __ CuleD(0, lhs, rhs);
3625 }
3626 __ Bc1t(0, label);
3627 break;
3628 case kCondGT:
3629 if (gt_bias) {
3630 __ CultD(0, rhs, lhs);
3631 } else {
3632 __ ColtD(0, rhs, lhs);
3633 }
3634 __ Bc1t(0, label);
3635 break;
3636 case kCondGE:
3637 if (gt_bias) {
3638 __ CuleD(0, rhs, lhs);
3639 } else {
3640 __ ColeD(0, rhs, lhs);
3641 }
3642 __ Bc1t(0, label);
3643 break;
3644 default:
3645 LOG(FATAL) << "Unexpected non-floating-point condition";
3646 }
3647 }
3648 }
3649}
3650
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003651void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003652 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003653 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003654 MipsLabel* false_target) {
3655 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003656
David Brazdil0debae72015-11-12 18:37:00 +00003657 if (true_target == nullptr && false_target == nullptr) {
3658 // Nothing to do. The code always falls through.
3659 return;
3660 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003661 // Constant condition, statically compared against "true" (integer value 1).
3662 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003663 if (true_target != nullptr) {
3664 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003665 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003666 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003667 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003668 if (false_target != nullptr) {
3669 __ B(false_target);
3670 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003671 }
David Brazdil0debae72015-11-12 18:37:00 +00003672 return;
3673 }
3674
3675 // The following code generates these patterns:
3676 // (1) true_target == nullptr && false_target != nullptr
3677 // - opposite condition true => branch to false_target
3678 // (2) true_target != nullptr && false_target == nullptr
3679 // - condition true => branch to true_target
3680 // (3) true_target != nullptr && false_target != nullptr
3681 // - condition true => branch to true_target
3682 // - branch to false_target
3683 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003684 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003685 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003686 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003687 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003688 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3689 } else {
3690 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3691 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003692 } else {
3693 // The condition instruction has not been materialized, use its inputs as
3694 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003695 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003696 Primitive::Type type = condition->InputAt(0)->GetType();
3697 LocationSummary* locations = cond->GetLocations();
3698 IfCondition if_cond = condition->GetCondition();
3699 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003700
David Brazdil0debae72015-11-12 18:37:00 +00003701 if (true_target == nullptr) {
3702 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003703 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003704 }
3705
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003706 switch (type) {
3707 default:
3708 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3709 break;
3710 case Primitive::kPrimLong:
3711 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3712 break;
3713 case Primitive::kPrimFloat:
3714 case Primitive::kPrimDouble:
3715 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3716 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003717 }
3718 }
David Brazdil0debae72015-11-12 18:37:00 +00003719
3720 // If neither branch falls through (case 3), the conditional branch to `true_target`
3721 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3722 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003723 __ B(false_target);
3724 }
3725}
3726
3727void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3728 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003729 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003730 locations->SetInAt(0, Location::RequiresRegister());
3731 }
3732}
3733
3734void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003735 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3736 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3737 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3738 nullptr : codegen_->GetLabelOf(true_successor);
3739 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3740 nullptr : codegen_->GetLabelOf(false_successor);
3741 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003742}
3743
3744void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3745 LocationSummary* locations = new (GetGraph()->GetArena())
3746 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003747 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003748 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003749 locations->SetInAt(0, Location::RequiresRegister());
3750 }
3751}
3752
3753void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003754 SlowPathCodeMIPS* slow_path =
3755 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003756 GenerateTestAndBranch(deoptimize,
3757 /* condition_input_index */ 0,
3758 slow_path->GetEntryLabel(),
3759 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003760}
3761
David Brazdil74eb1b22015-12-14 11:44:01 +00003762void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3763 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3764 if (Primitive::IsFloatingPointType(select->GetType())) {
3765 locations->SetInAt(0, Location::RequiresFpuRegister());
3766 locations->SetInAt(1, Location::RequiresFpuRegister());
3767 } else {
3768 locations->SetInAt(0, Location::RequiresRegister());
3769 locations->SetInAt(1, Location::RequiresRegister());
3770 }
3771 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3772 locations->SetInAt(2, Location::RequiresRegister());
3773 }
3774 locations->SetOut(Location::SameAsFirstInput());
3775}
3776
3777void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3778 LocationSummary* locations = select->GetLocations();
3779 MipsLabel false_target;
3780 GenerateTestAndBranch(select,
3781 /* condition_input_index */ 2,
3782 /* true_target */ nullptr,
3783 &false_target);
3784 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3785 __ Bind(&false_target);
3786}
3787
David Srbecky0cf44932015-12-09 14:09:59 +00003788void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3789 new (GetGraph()->GetArena()) LocationSummary(info);
3790}
3791
David Srbeckyd28f4a02016-03-14 17:14:24 +00003792void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3793 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003794}
3795
3796void CodeGeneratorMIPS::GenerateNop() {
3797 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003798}
3799
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003800void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3801 Primitive::Type field_type = field_info.GetFieldType();
3802 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3803 bool generate_volatile = field_info.IsVolatile() && is_wide;
3804 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003805 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003806
3807 locations->SetInAt(0, Location::RequiresRegister());
3808 if (generate_volatile) {
3809 InvokeRuntimeCallingConvention calling_convention;
3810 // need A0 to hold base + offset
3811 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3812 if (field_type == Primitive::kPrimLong) {
3813 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3814 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003815 // Use Location::Any() to prevent situations when running out of available fp registers.
3816 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003817 // Need some temp core regs since FP results are returned in core registers
3818 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3819 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3820 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3821 }
3822 } else {
3823 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3824 locations->SetOut(Location::RequiresFpuRegister());
3825 } else {
3826 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3827 }
3828 }
3829}
3830
3831void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3832 const FieldInfo& field_info,
3833 uint32_t dex_pc) {
3834 Primitive::Type type = field_info.GetFieldType();
3835 LocationSummary* locations = instruction->GetLocations();
3836 Register obj = locations->InAt(0).AsRegister<Register>();
3837 LoadOperandType load_type = kLoadUnsignedByte;
3838 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003839 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003840 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003841
3842 switch (type) {
3843 case Primitive::kPrimBoolean:
3844 load_type = kLoadUnsignedByte;
3845 break;
3846 case Primitive::kPrimByte:
3847 load_type = kLoadSignedByte;
3848 break;
3849 case Primitive::kPrimShort:
3850 load_type = kLoadSignedHalfword;
3851 break;
3852 case Primitive::kPrimChar:
3853 load_type = kLoadUnsignedHalfword;
3854 break;
3855 case Primitive::kPrimInt:
3856 case Primitive::kPrimFloat:
3857 case Primitive::kPrimNot:
3858 load_type = kLoadWord;
3859 break;
3860 case Primitive::kPrimLong:
3861 case Primitive::kPrimDouble:
3862 load_type = kLoadDoubleword;
3863 break;
3864 case Primitive::kPrimVoid:
3865 LOG(FATAL) << "Unreachable type " << type;
3866 UNREACHABLE();
3867 }
3868
3869 if (is_volatile && load_type == kLoadDoubleword) {
3870 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003871 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003872 // Do implicit Null check
3873 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3874 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01003875 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003876 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3877 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003878 // FP results are returned in core registers. Need to move them.
3879 Location out = locations->Out();
3880 if (out.IsFpuRegister()) {
3881 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3882 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3883 out.AsFpuRegister<FRegister>());
3884 } else {
3885 DCHECK(out.IsDoubleStackSlot());
3886 __ StoreToOffset(kStoreWord,
3887 locations->GetTemp(1).AsRegister<Register>(),
3888 SP,
3889 out.GetStackIndex());
3890 __ StoreToOffset(kStoreWord,
3891 locations->GetTemp(2).AsRegister<Register>(),
3892 SP,
3893 out.GetStackIndex() + 4);
3894 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003895 }
3896 } else {
3897 if (!Primitive::IsFloatingPointType(type)) {
3898 Register dst;
3899 if (type == Primitive::kPrimLong) {
3900 DCHECK(locations->Out().IsRegisterPair());
3901 dst = locations->Out().AsRegisterPairLow<Register>();
3902 } else {
3903 DCHECK(locations->Out().IsRegister());
3904 dst = locations->Out().AsRegister<Register>();
3905 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003906 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003907 } else {
3908 DCHECK(locations->Out().IsFpuRegister());
3909 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3910 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003911 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003912 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003913 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003914 }
3915 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003916 }
3917
3918 if (is_volatile) {
3919 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3920 }
3921}
3922
3923void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3924 Primitive::Type field_type = field_info.GetFieldType();
3925 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3926 bool generate_volatile = field_info.IsVolatile() && is_wide;
3927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003928 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003929
3930 locations->SetInAt(0, Location::RequiresRegister());
3931 if (generate_volatile) {
3932 InvokeRuntimeCallingConvention calling_convention;
3933 // need A0 to hold base + offset
3934 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3935 if (field_type == Primitive::kPrimLong) {
3936 locations->SetInAt(1, Location::RegisterPairLocation(
3937 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3938 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003939 // Use Location::Any() to prevent situations when running out of available fp registers.
3940 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003941 // Pass FP parameters in core registers.
3942 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3943 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3944 }
3945 } else {
3946 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003947 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003948 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003949 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003950 }
3951 }
3952}
3953
3954void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3955 const FieldInfo& field_info,
3956 uint32_t dex_pc) {
3957 Primitive::Type type = field_info.GetFieldType();
3958 LocationSummary* locations = instruction->GetLocations();
3959 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07003960 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003961 StoreOperandType store_type = kStoreByte;
3962 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003963 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003964 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003965
3966 switch (type) {
3967 case Primitive::kPrimBoolean:
3968 case Primitive::kPrimByte:
3969 store_type = kStoreByte;
3970 break;
3971 case Primitive::kPrimShort:
3972 case Primitive::kPrimChar:
3973 store_type = kStoreHalfword;
3974 break;
3975 case Primitive::kPrimInt:
3976 case Primitive::kPrimFloat:
3977 case Primitive::kPrimNot:
3978 store_type = kStoreWord;
3979 break;
3980 case Primitive::kPrimLong:
3981 case Primitive::kPrimDouble:
3982 store_type = kStoreDoubleword;
3983 break;
3984 case Primitive::kPrimVoid:
3985 LOG(FATAL) << "Unreachable type " << type;
3986 UNREACHABLE();
3987 }
3988
3989 if (is_volatile) {
3990 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3991 }
3992
3993 if (is_volatile && store_type == kStoreDoubleword) {
3994 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003995 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003996 // Do implicit Null check.
3997 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3998 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3999 if (type == Primitive::kPrimDouble) {
4000 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004001 if (value_location.IsFpuRegister()) {
4002 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4003 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004004 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004005 value_location.AsFpuRegister<FRegister>());
4006 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004007 __ LoadFromOffset(kLoadWord,
4008 locations->GetTemp(1).AsRegister<Register>(),
4009 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004010 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004011 __ LoadFromOffset(kLoadWord,
4012 locations->GetTemp(2).AsRegister<Register>(),
4013 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004014 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004015 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004016 DCHECK(value_location.IsConstant());
4017 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4018 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004019 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4020 locations->GetTemp(1).AsRegister<Register>(),
4021 value);
4022 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004023 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004024 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004025 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4026 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004027 if (value_location.IsConstant()) {
4028 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4029 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4030 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004031 Register src;
4032 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004033 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004034 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004035 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004036 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004037 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004038 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004039 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004040 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004041 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004042 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004043 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004044 }
4045 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004046 }
4047
4048 // TODO: memory barriers?
4049 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004050 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004051 codegen_->MarkGCCard(obj, src);
4052 }
4053
4054 if (is_volatile) {
4055 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4056 }
4057}
4058
4059void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4060 HandleFieldGet(instruction, instruction->GetFieldInfo());
4061}
4062
4063void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4064 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4065}
4066
4067void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4068 HandleFieldSet(instruction, instruction->GetFieldInfo());
4069}
4070
4071void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4072 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4073}
4074
Alexey Frunze06a46c42016-07-19 15:00:40 -07004075void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
4076 HInstruction* instruction ATTRIBUTE_UNUSED,
4077 Location root,
4078 Register obj,
4079 uint32_t offset) {
4080 Register root_reg = root.AsRegister<Register>();
4081 if (kEmitCompilerReadBarrier) {
4082 UNIMPLEMENTED(FATAL) << "for read barrier";
4083 } else {
4084 // Plain GC root load with no read barrier.
4085 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4086 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
4087 // Note that GC roots are not affected by heap poisoning, thus we
4088 // do not have to unpoison `root_reg` here.
4089 }
4090}
4091
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004092void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4093 LocationSummary::CallKind call_kind =
4094 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
4095 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4096 locations->SetInAt(0, Location::RequiresRegister());
4097 locations->SetInAt(1, Location::RequiresRegister());
4098 // The output does overlap inputs.
4099 // Note that TypeCheckSlowPathMIPS uses this register too.
4100 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4101}
4102
4103void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4104 LocationSummary* locations = instruction->GetLocations();
4105 Register obj = locations->InAt(0).AsRegister<Register>();
4106 Register cls = locations->InAt(1).AsRegister<Register>();
4107 Register out = locations->Out().AsRegister<Register>();
4108
4109 MipsLabel done;
4110
4111 // Return 0 if `obj` is null.
4112 // TODO: Avoid this check if we know `obj` is not null.
4113 __ Move(out, ZERO);
4114 __ Beqz(obj, &done);
4115
4116 // Compare the class of `obj` with `cls`.
4117 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
4118 if (instruction->IsExactCheck()) {
4119 // Classes must be equal for the instanceof to succeed.
4120 __ Xor(out, out, cls);
4121 __ Sltiu(out, out, 1);
4122 } else {
4123 // If the classes are not equal, we go into a slow path.
4124 DCHECK(locations->OnlyCallsOnSlowPath());
4125 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
4126 codegen_->AddSlowPath(slow_path);
4127 __ Bne(out, cls, slow_path->GetEntryLabel());
4128 __ LoadConst32(out, 1);
4129 __ Bind(slow_path->GetExitLabel());
4130 }
4131
4132 __ Bind(&done);
4133}
4134
4135void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
4136 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4137 locations->SetOut(Location::ConstantLocation(constant));
4138}
4139
4140void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4141 // Will be generated at use site.
4142}
4143
4144void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
4145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4146 locations->SetOut(Location::ConstantLocation(constant));
4147}
4148
4149void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4150 // Will be generated at use site.
4151}
4152
4153void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
4154 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
4155 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4156}
4157
4158void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4159 HandleInvoke(invoke);
4160 // The register T0 is required to be used for the hidden argument in
4161 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4162 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4163}
4164
4165void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4166 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4167 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004168 Location receiver = invoke->GetLocations()->InAt(0);
4169 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004170 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004171
4172 // Set the hidden argument.
4173 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
4174 invoke->GetDexMethodIndex());
4175
4176 // temp = object->GetClass();
4177 if (receiver.IsStackSlot()) {
4178 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4179 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4180 } else {
4181 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4182 }
4183 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004184 __ LoadFromOffset(kLoadWord, temp, temp,
4185 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
4186 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004187 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004188 // temp = temp->GetImtEntryAt(method_offset);
4189 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4190 // T9 = temp->GetEntryPoint();
4191 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4192 // T9();
4193 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004194 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004195 DCHECK(!codegen_->IsLeafMethod());
4196 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4197}
4198
4199void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07004200 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4201 if (intrinsic.TryDispatch(invoke)) {
4202 return;
4203 }
4204
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004205 HandleInvoke(invoke);
4206}
4207
4208void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004209 // Explicit clinit checks triggered by static invokes must have been pruned by
4210 // art::PrepareForRegisterAllocation.
4211 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004212
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004213 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4214 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4215 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4216
4217 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
4218 // R6 has PC-relative addressing.
4219 bool has_extra_input = !isR6 &&
4220 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4221 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4222
4223 if (invoke->HasPcRelativeDexCache()) {
4224 // kDexCachePcRelative is mutually exclusive with
4225 // kDirectAddressWithFixup/kCallDirectWithFixup.
4226 CHECK(!has_extra_input);
4227 has_extra_input = true;
4228 }
4229
Chris Larsen701566a2015-10-27 15:29:13 -07004230 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4231 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004232 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4233 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4234 }
Chris Larsen701566a2015-10-27 15:29:13 -07004235 return;
4236 }
4237
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004238 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004239
4240 // Add the extra input register if either the dex cache array base register
4241 // or the PC-relative base register for accessing literals is needed.
4242 if (has_extra_input) {
4243 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4244 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004245}
4246
Chris Larsen701566a2015-10-27 15:29:13 -07004247static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004248 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004249 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4250 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004251 return true;
4252 }
4253 return false;
4254}
4255
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004256HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004257 HLoadString::LoadKind desired_string_load_kind) {
4258 if (kEmitCompilerReadBarrier) {
4259 UNIMPLEMENTED(FATAL) << "for read barrier";
4260 }
4261 // We disable PC-relative load when there is an irreducible loop, as the optimization
4262 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004263 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4264 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07004265 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4266 bool fallback_load = has_irreducible_loops;
4267 switch (desired_string_load_kind) {
4268 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4269 DCHECK(!GetCompilerOptions().GetCompilePic());
4270 break;
4271 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4272 DCHECK(GetCompilerOptions().GetCompilePic());
4273 break;
4274 case HLoadString::LoadKind::kBootImageAddress:
4275 break;
4276 case HLoadString::LoadKind::kDexCacheAddress:
4277 DCHECK(Runtime::Current()->UseJitCompilation());
4278 fallback_load = false;
4279 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004280 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004281 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004282 break;
4283 case HLoadString::LoadKind::kDexCacheViaMethod:
4284 fallback_load = false;
4285 break;
4286 }
4287 if (fallback_load) {
4288 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4289 }
4290 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004291}
4292
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004293HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4294 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004295 if (kEmitCompilerReadBarrier) {
4296 UNIMPLEMENTED(FATAL) << "for read barrier";
4297 }
4298 // We disable pc-relative load when there is an irreducible loop, as the optimization
4299 // is incompatible with it.
4300 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4301 bool fallback_load = has_irreducible_loops;
4302 switch (desired_class_load_kind) {
4303 case HLoadClass::LoadKind::kReferrersClass:
4304 fallback_load = false;
4305 break;
4306 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4307 DCHECK(!GetCompilerOptions().GetCompilePic());
4308 break;
4309 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4310 DCHECK(GetCompilerOptions().GetCompilePic());
4311 break;
4312 case HLoadClass::LoadKind::kBootImageAddress:
4313 break;
4314 case HLoadClass::LoadKind::kDexCacheAddress:
4315 DCHECK(Runtime::Current()->UseJitCompilation());
4316 fallback_load = false;
4317 break;
4318 case HLoadClass::LoadKind::kDexCachePcRelative:
4319 DCHECK(!Runtime::Current()->UseJitCompilation());
4320 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4321 // with irreducible loops.
4322 break;
4323 case HLoadClass::LoadKind::kDexCacheViaMethod:
4324 fallback_load = false;
4325 break;
4326 }
4327 if (fallback_load) {
4328 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4329 }
4330 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004331}
4332
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004333Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4334 Register temp) {
4335 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4336 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4337 if (!invoke->GetLocations()->Intrinsified()) {
4338 return location.AsRegister<Register>();
4339 }
4340 // For intrinsics we allow any location, so it may be on the stack.
4341 if (!location.IsRegister()) {
4342 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4343 return temp;
4344 }
4345 // For register locations, check if the register was saved. If so, get it from the stack.
4346 // Note: There is a chance that the register was saved but not overwritten, so we could
4347 // save one load. However, since this is just an intrinsic slow path we prefer this
4348 // simple and more robust approach rather that trying to determine if that's the case.
4349 SlowPathCode* slow_path = GetCurrentSlowPath();
4350 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4351 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4352 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4353 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4354 return temp;
4355 }
4356 return location.AsRegister<Register>();
4357}
4358
Vladimir Markodc151b22015-10-15 18:02:30 +01004359HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4360 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004361 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004362 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4363 // We disable PC-relative load when there is an irreducible loop, as the optimization
4364 // is incompatible with it.
4365 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4366 bool fallback_load = true;
4367 bool fallback_call = true;
4368 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004369 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4370 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004371 fallback_load = has_irreducible_loops;
4372 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004373 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004374 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004375 break;
4376 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004377 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004378 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004379 fallback_call = has_irreducible_loops;
4380 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004381 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004382 // TODO: Implement this type.
4383 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004384 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004385 fallback_call = false;
4386 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004387 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004388 if (fallback_load) {
4389 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4390 dispatch_info.method_load_data = 0;
4391 }
4392 if (fallback_call) {
4393 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4394 dispatch_info.direct_code_ptr = 0;
4395 }
4396 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004397}
4398
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004399void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4400 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004401 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004402 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4403 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4404 bool isR6 = isa_features_.IsR6();
4405 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4406 // R6 has PC-relative addressing.
4407 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4408 (!isR6 &&
4409 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4410 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4411 Register base_reg = has_extra_input
4412 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4413 : ZERO;
4414
4415 // For better instruction scheduling we load the direct code pointer before the method pointer.
4416 switch (code_ptr_location) {
4417 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4418 // T9 = invoke->GetDirectCodePtr();
4419 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4420 break;
4421 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4422 // T9 = code address from literal pool with link-time patch.
4423 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4424 break;
4425 default:
4426 break;
4427 }
4428
4429 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004430 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004431 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004432 uint32_t offset =
4433 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004434 __ LoadFromOffset(kLoadWord,
4435 temp.AsRegister<Register>(),
4436 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004437 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004438 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004439 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004440 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004441 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004442 break;
4443 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4444 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4445 break;
4446 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004447 __ LoadLiteral(temp.AsRegister<Register>(),
4448 base_reg,
4449 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4450 break;
4451 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4452 HMipsDexCacheArraysBase* base =
4453 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4454 int32_t offset =
4455 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4456 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4457 break;
4458 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004459 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004460 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004461 Register reg = temp.AsRegister<Register>();
4462 Register method_reg;
4463 if (current_method.IsRegister()) {
4464 method_reg = current_method.AsRegister<Register>();
4465 } else {
4466 // TODO: use the appropriate DCHECK() here if possible.
4467 // DCHECK(invoke->GetLocations()->Intrinsified());
4468 DCHECK(!current_method.IsValid());
4469 method_reg = reg;
4470 __ Lw(reg, SP, kCurrentMethodStackOffset);
4471 }
4472
4473 // temp = temp->dex_cache_resolved_methods_;
4474 __ LoadFromOffset(kLoadWord,
4475 reg,
4476 method_reg,
4477 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004478 // temp = temp[index_in_cache];
4479 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4480 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004481 __ LoadFromOffset(kLoadWord,
4482 reg,
4483 reg,
4484 CodeGenerator::GetCachePointerOffset(index_in_cache));
4485 break;
4486 }
4487 }
4488
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004489 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004490 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004491 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004492 break;
4493 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004494 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4495 // T9 prepared above for better instruction scheduling.
4496 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004497 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004498 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004499 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004500 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004501 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004502 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4503 LOG(FATAL) << "Unsupported";
4504 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004505 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4506 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004507 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004508 T9,
4509 callee_method.AsRegister<Register>(),
4510 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004511 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004512 // T9()
4513 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004514 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004515 break;
4516 }
4517 DCHECK(!IsLeafMethod());
4518}
4519
4520void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004521 // Explicit clinit checks triggered by static invokes must have been pruned by
4522 // art::PrepareForRegisterAllocation.
4523 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004524
4525 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4526 return;
4527 }
4528
4529 LocationSummary* locations = invoke->GetLocations();
4530 codegen_->GenerateStaticOrDirectCall(invoke,
4531 locations->HasTemps()
4532 ? locations->GetTemp(0)
4533 : Location::NoLocation());
4534 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4535}
4536
Chris Larsen3acee732015-11-18 13:31:08 -08004537void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02004538 // Use the calling convention instead of the location of the receiver, as
4539 // intrinsics may have put the receiver in a different register. In the intrinsics
4540 // slow path, the arguments have been moved to the right place, so here we are
4541 // guaranteed that the receiver is the first register of the calling convention.
4542 InvokeDexCallingConvention calling_convention;
4543 Register receiver = calling_convention.GetRegisterAt(0);
4544
Chris Larsen3acee732015-11-18 13:31:08 -08004545 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004546 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4547 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4548 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004549 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004550
4551 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02004552 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08004553 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004554 // temp = temp->GetMethodAt(method_offset);
4555 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4556 // T9 = temp->GetEntryPoint();
4557 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4558 // T9();
4559 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004560 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004561}
4562
4563void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4564 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4565 return;
4566 }
4567
4568 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004569 DCHECK(!codegen_->IsLeafMethod());
4570 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4571}
4572
4573void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004574 if (cls->NeedsAccessCheck()) {
4575 InvokeRuntimeCallingConvention calling_convention;
4576 CodeGenerator::CreateLoadClassLocationSummary(
4577 cls,
4578 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4579 Location::RegisterLocation(V0),
4580 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4581 return;
4582 }
4583
4584 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4585 ? LocationSummary::kCallOnSlowPath
4586 : LocationSummary::kNoCall;
4587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4588 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4589 switch (load_kind) {
4590 // We need an extra register for PC-relative literals on R2.
4591 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4592 case HLoadClass::LoadKind::kBootImageAddress:
4593 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4594 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4595 break;
4596 }
4597 FALLTHROUGH_INTENDED;
4598 // We need an extra register for PC-relative dex cache accesses.
4599 case HLoadClass::LoadKind::kDexCachePcRelative:
4600 case HLoadClass::LoadKind::kReferrersClass:
4601 case HLoadClass::LoadKind::kDexCacheViaMethod:
4602 locations->SetInAt(0, Location::RequiresRegister());
4603 break;
4604 default:
4605 break;
4606 }
4607 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004608}
4609
4610void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4611 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004612 if (cls->NeedsAccessCheck()) {
4613 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004614 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004615 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004616 return;
4617 }
4618
Alexey Frunze06a46c42016-07-19 15:00:40 -07004619 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4620 Location out_loc = locations->Out();
4621 Register out = out_loc.AsRegister<Register>();
4622 Register base_or_current_method_reg;
4623 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4624 switch (load_kind) {
4625 // We need an extra register for PC-relative literals on R2.
4626 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4627 case HLoadClass::LoadKind::kBootImageAddress:
4628 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4629 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4630 break;
4631 // We need an extra register for PC-relative dex cache accesses.
4632 case HLoadClass::LoadKind::kDexCachePcRelative:
4633 case HLoadClass::LoadKind::kReferrersClass:
4634 case HLoadClass::LoadKind::kDexCacheViaMethod:
4635 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4636 break;
4637 default:
4638 base_or_current_method_reg = ZERO;
4639 break;
4640 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004641
Alexey Frunze06a46c42016-07-19 15:00:40 -07004642 bool generate_null_check = false;
4643 switch (load_kind) {
4644 case HLoadClass::LoadKind::kReferrersClass: {
4645 DCHECK(!cls->CanCallRuntime());
4646 DCHECK(!cls->MustGenerateClinitCheck());
4647 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4648 GenerateGcRootFieldLoad(cls,
4649 out_loc,
4650 base_or_current_method_reg,
4651 ArtMethod::DeclaringClassOffset().Int32Value());
4652 break;
4653 }
4654 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4655 DCHECK(!kEmitCompilerReadBarrier);
4656 __ LoadLiteral(out,
4657 base_or_current_method_reg,
4658 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4659 cls->GetTypeIndex()));
4660 break;
4661 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4662 DCHECK(!kEmitCompilerReadBarrier);
4663 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4664 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004665 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004666 break;
4667 }
4668 case HLoadClass::LoadKind::kBootImageAddress: {
4669 DCHECK(!kEmitCompilerReadBarrier);
4670 DCHECK_NE(cls->GetAddress(), 0u);
4671 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4672 __ LoadLiteral(out,
4673 base_or_current_method_reg,
4674 codegen_->DeduplicateBootImageAddressLiteral(address));
4675 break;
4676 }
4677 case HLoadClass::LoadKind::kDexCacheAddress: {
4678 DCHECK_NE(cls->GetAddress(), 0u);
4679 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4680 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4681 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4682 int16_t offset = Low16Bits(address);
4683 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4684 __ Lui(out, High16Bits(base_address));
4685 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4686 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4687 generate_null_check = !cls->IsInDexCache();
4688 break;
4689 }
4690 case HLoadClass::LoadKind::kDexCachePcRelative: {
4691 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4692 int32_t offset =
4693 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4694 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4695 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4696 generate_null_check = !cls->IsInDexCache();
4697 break;
4698 }
4699 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4700 // /* GcRoot<mirror::Class>[] */ out =
4701 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4702 __ LoadFromOffset(kLoadWord,
4703 out,
4704 base_or_current_method_reg,
4705 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4706 // /* GcRoot<mirror::Class> */ out = out[type_index]
4707 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4708 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4709 generate_null_check = !cls->IsInDexCache();
4710 }
4711 }
4712
4713 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4714 DCHECK(cls->CanCallRuntime());
4715 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4716 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4717 codegen_->AddSlowPath(slow_path);
4718 if (generate_null_check) {
4719 __ Beqz(out, slow_path->GetEntryLabel());
4720 }
4721 if (cls->MustGenerateClinitCheck()) {
4722 GenerateClassInitializationCheck(slow_path, out);
4723 } else {
4724 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004725 }
4726 }
4727}
4728
4729static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004730 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004731}
4732
4733void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4734 LocationSummary* locations =
4735 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4736 locations->SetOut(Location::RequiresRegister());
4737}
4738
4739void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4740 Register out = load->GetLocations()->Out().AsRegister<Register>();
4741 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4742}
4743
4744void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4745 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4746}
4747
4748void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4749 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4750}
4751
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004752void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004753 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00004754 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4755 ? LocationSummary::kCallOnMainOnly
4756 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004757 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004758 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004759 HLoadString::LoadKind load_kind = load->GetLoadKind();
4760 switch (load_kind) {
4761 // We need an extra register for PC-relative literals on R2.
4762 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4763 case HLoadString::LoadKind::kBootImageAddress:
4764 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004765 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004766 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4767 break;
4768 }
4769 FALLTHROUGH_INTENDED;
4770 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07004771 case HLoadString::LoadKind::kDexCacheViaMethod:
4772 locations->SetInAt(0, Location::RequiresRegister());
4773 break;
4774 default:
4775 break;
4776 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004777 locations->SetOut(Location::RequiresRegister());
4778}
4779
4780void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004781 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004782 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004783 Location out_loc = locations->Out();
4784 Register out = out_loc.AsRegister<Register>();
4785 Register base_or_current_method_reg;
4786 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4787 switch (load_kind) {
4788 // We need an extra register for PC-relative literals on R2.
4789 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4790 case HLoadString::LoadKind::kBootImageAddress:
4791 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004792 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004793 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4794 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004795 default:
4796 base_or_current_method_reg = ZERO;
4797 break;
4798 }
4799
4800 switch (load_kind) {
4801 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4802 DCHECK(!kEmitCompilerReadBarrier);
4803 __ LoadLiteral(out,
4804 base_or_current_method_reg,
4805 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4806 load->GetStringIndex()));
4807 return; // No dex cache slow path.
4808 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4809 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004810 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004811 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4812 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004813 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004814 return; // No dex cache slow path.
4815 }
4816 case HLoadString::LoadKind::kBootImageAddress: {
4817 DCHECK(!kEmitCompilerReadBarrier);
4818 DCHECK_NE(load->GetAddress(), 0u);
4819 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4820 __ LoadLiteral(out,
4821 base_or_current_method_reg,
4822 codegen_->DeduplicateBootImageAddressLiteral(address));
4823 return; // No dex cache slow path.
4824 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004825 case HLoadString::LoadKind::kBssEntry: {
4826 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4827 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4828 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
4829 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
4830 __ LoadFromOffset(kLoadWord, out, out, 0);
4831 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4832 codegen_->AddSlowPath(slow_path);
4833 __ Beqz(out, slow_path->GetEntryLabel());
4834 __ Bind(slow_path->GetExitLabel());
4835 return;
4836 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004837 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004838 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004839 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004840
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004841 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004842 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
4843 InvokeRuntimeCallingConvention calling_convention;
4844 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
4845 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4846 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004847}
4848
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004849void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4850 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4851 locations->SetOut(Location::ConstantLocation(constant));
4852}
4853
4854void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4855 // Will be generated at use site.
4856}
4857
4858void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4859 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004860 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004861 InvokeRuntimeCallingConvention calling_convention;
4862 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4863}
4864
4865void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4866 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01004867 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004868 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4869 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004870 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004871 }
4872 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4873}
4874
4875void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4876 LocationSummary* locations =
4877 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4878 switch (mul->GetResultType()) {
4879 case Primitive::kPrimInt:
4880 case Primitive::kPrimLong:
4881 locations->SetInAt(0, Location::RequiresRegister());
4882 locations->SetInAt(1, Location::RequiresRegister());
4883 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4884 break;
4885
4886 case Primitive::kPrimFloat:
4887 case Primitive::kPrimDouble:
4888 locations->SetInAt(0, Location::RequiresFpuRegister());
4889 locations->SetInAt(1, Location::RequiresFpuRegister());
4890 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4891 break;
4892
4893 default:
4894 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4895 }
4896}
4897
4898void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4899 Primitive::Type type = instruction->GetType();
4900 LocationSummary* locations = instruction->GetLocations();
4901 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4902
4903 switch (type) {
4904 case Primitive::kPrimInt: {
4905 Register dst = locations->Out().AsRegister<Register>();
4906 Register lhs = locations->InAt(0).AsRegister<Register>();
4907 Register rhs = locations->InAt(1).AsRegister<Register>();
4908
4909 if (isR6) {
4910 __ MulR6(dst, lhs, rhs);
4911 } else {
4912 __ MulR2(dst, lhs, rhs);
4913 }
4914 break;
4915 }
4916 case Primitive::kPrimLong: {
4917 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4918 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4919 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4920 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4921 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4922 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4923
4924 // Extra checks to protect caused by the existance of A1_A2.
4925 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4926 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4927 DCHECK_NE(dst_high, lhs_low);
4928 DCHECK_NE(dst_high, rhs_low);
4929
4930 // A_B * C_D
4931 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4932 // dst_lo: [ low(B*D) ]
4933 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4934
4935 if (isR6) {
4936 __ MulR6(TMP, lhs_high, rhs_low);
4937 __ MulR6(dst_high, lhs_low, rhs_high);
4938 __ Addu(dst_high, dst_high, TMP);
4939 __ MuhuR6(TMP, lhs_low, rhs_low);
4940 __ Addu(dst_high, dst_high, TMP);
4941 __ MulR6(dst_low, lhs_low, rhs_low);
4942 } else {
4943 __ MulR2(TMP, lhs_high, rhs_low);
4944 __ MulR2(dst_high, lhs_low, rhs_high);
4945 __ Addu(dst_high, dst_high, TMP);
4946 __ MultuR2(lhs_low, rhs_low);
4947 __ Mfhi(TMP);
4948 __ Addu(dst_high, dst_high, TMP);
4949 __ Mflo(dst_low);
4950 }
4951 break;
4952 }
4953 case Primitive::kPrimFloat:
4954 case Primitive::kPrimDouble: {
4955 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4956 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4957 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4958 if (type == Primitive::kPrimFloat) {
4959 __ MulS(dst, lhs, rhs);
4960 } else {
4961 __ MulD(dst, lhs, rhs);
4962 }
4963 break;
4964 }
4965 default:
4966 LOG(FATAL) << "Unexpected mul type " << type;
4967 }
4968}
4969
4970void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4971 LocationSummary* locations =
4972 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4973 switch (neg->GetResultType()) {
4974 case Primitive::kPrimInt:
4975 case Primitive::kPrimLong:
4976 locations->SetInAt(0, Location::RequiresRegister());
4977 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4978 break;
4979
4980 case Primitive::kPrimFloat:
4981 case Primitive::kPrimDouble:
4982 locations->SetInAt(0, Location::RequiresFpuRegister());
4983 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4984 break;
4985
4986 default:
4987 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4988 }
4989}
4990
4991void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4992 Primitive::Type type = instruction->GetType();
4993 LocationSummary* locations = instruction->GetLocations();
4994
4995 switch (type) {
4996 case Primitive::kPrimInt: {
4997 Register dst = locations->Out().AsRegister<Register>();
4998 Register src = locations->InAt(0).AsRegister<Register>();
4999 __ Subu(dst, ZERO, src);
5000 break;
5001 }
5002 case Primitive::kPrimLong: {
5003 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5004 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5005 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5006 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5007 __ Subu(dst_low, ZERO, src_low);
5008 __ Sltu(TMP, ZERO, dst_low);
5009 __ Subu(dst_high, ZERO, src_high);
5010 __ Subu(dst_high, dst_high, TMP);
5011 break;
5012 }
5013 case Primitive::kPrimFloat:
5014 case Primitive::kPrimDouble: {
5015 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5016 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5017 if (type == Primitive::kPrimFloat) {
5018 __ NegS(dst, src);
5019 } else {
5020 __ NegD(dst, src);
5021 }
5022 break;
5023 }
5024 default:
5025 LOG(FATAL) << "Unexpected neg type " << type;
5026 }
5027}
5028
5029void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5030 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005032 InvokeRuntimeCallingConvention calling_convention;
5033 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5034 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5035 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5036 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5037}
5038
5039void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5040 InvokeRuntimeCallingConvention calling_convention;
5041 Register current_method_register = calling_convention.GetRegisterAt(2);
5042 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5043 // Move an uint16_t value to a register.
5044 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005045 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005046 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5047 void*, uint32_t, int32_t, ArtMethod*>();
5048}
5049
5050void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5051 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005053 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005054 if (instruction->IsStringAlloc()) {
5055 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5056 } else {
5057 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5058 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5059 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005060 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5061}
5062
5063void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005064 if (instruction->IsStringAlloc()) {
5065 // String is allocated through StringFactory. Call NewEmptyString entry point.
5066 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005067 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005068 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5069 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5070 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005071 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005072 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5073 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005074 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00005075 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
5076 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005077}
5078
5079void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5081 locations->SetInAt(0, Location::RequiresRegister());
5082 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5083}
5084
5085void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5086 Primitive::Type type = instruction->GetType();
5087 LocationSummary* locations = instruction->GetLocations();
5088
5089 switch (type) {
5090 case Primitive::kPrimInt: {
5091 Register dst = locations->Out().AsRegister<Register>();
5092 Register src = locations->InAt(0).AsRegister<Register>();
5093 __ Nor(dst, src, ZERO);
5094 break;
5095 }
5096
5097 case Primitive::kPrimLong: {
5098 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5099 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5100 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5101 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5102 __ Nor(dst_high, src_high, ZERO);
5103 __ Nor(dst_low, src_low, ZERO);
5104 break;
5105 }
5106
5107 default:
5108 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5109 }
5110}
5111
5112void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5114 locations->SetInAt(0, Location::RequiresRegister());
5115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5116}
5117
5118void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5119 LocationSummary* locations = instruction->GetLocations();
5120 __ Xori(locations->Out().AsRegister<Register>(),
5121 locations->InAt(0).AsRegister<Register>(),
5122 1);
5123}
5124
5125void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005126 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5127 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005128}
5129
Calin Juravle2ae48182016-03-16 14:05:09 +00005130void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5131 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005132 return;
5133 }
5134 Location obj = instruction->GetLocations()->InAt(0);
5135
5136 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005137 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005138}
5139
Calin Juravle2ae48182016-03-16 14:05:09 +00005140void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005141 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005142 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005143
5144 Location obj = instruction->GetLocations()->InAt(0);
5145
5146 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5147}
5148
5149void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005150 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005151}
5152
5153void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5154 HandleBinaryOp(instruction);
5155}
5156
5157void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5158 HandleBinaryOp(instruction);
5159}
5160
5161void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5162 LOG(FATAL) << "Unreachable";
5163}
5164
5165void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
5166 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5167}
5168
5169void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
5170 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5171 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5172 if (location.IsStackSlot()) {
5173 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5174 } else if (location.IsDoubleStackSlot()) {
5175 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5176 }
5177 locations->SetOut(location);
5178}
5179
5180void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
5181 ATTRIBUTE_UNUSED) {
5182 // Nothing to do, the parameter is already at its location.
5183}
5184
5185void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5186 LocationSummary* locations =
5187 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5188 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5189}
5190
5191void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5192 ATTRIBUTE_UNUSED) {
5193 // Nothing to do, the method is already at its location.
5194}
5195
5196void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5197 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005198 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005199 locations->SetInAt(i, Location::Any());
5200 }
5201 locations->SetOut(Location::Any());
5202}
5203
5204void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5205 LOG(FATAL) << "Unreachable";
5206}
5207
5208void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5209 Primitive::Type type = rem->GetResultType();
5210 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005211 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5213
5214 switch (type) {
5215 case Primitive::kPrimInt:
5216 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005217 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005218 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5219 break;
5220
5221 case Primitive::kPrimLong: {
5222 InvokeRuntimeCallingConvention calling_convention;
5223 locations->SetInAt(0, Location::RegisterPairLocation(
5224 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5225 locations->SetInAt(1, Location::RegisterPairLocation(
5226 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5227 locations->SetOut(calling_convention.GetReturnLocation(type));
5228 break;
5229 }
5230
5231 case Primitive::kPrimFloat:
5232 case Primitive::kPrimDouble: {
5233 InvokeRuntimeCallingConvention calling_convention;
5234 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5235 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5236 locations->SetOut(calling_convention.GetReturnLocation(type));
5237 break;
5238 }
5239
5240 default:
5241 LOG(FATAL) << "Unexpected rem type " << type;
5242 }
5243}
5244
5245void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5246 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005247
5248 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005249 case Primitive::kPrimInt:
5250 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005251 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005252 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005253 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005254 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5255 break;
5256 }
5257 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005258 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005259 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005260 break;
5261 }
5262 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005263 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005264 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005265 break;
5266 }
5267 default:
5268 LOG(FATAL) << "Unexpected rem type " << type;
5269 }
5270}
5271
5272void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5273 memory_barrier->SetLocations(nullptr);
5274}
5275
5276void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5277 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5278}
5279
5280void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5281 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5282 Primitive::Type return_type = ret->InputAt(0)->GetType();
5283 locations->SetInAt(0, MipsReturnLocation(return_type));
5284}
5285
5286void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5287 codegen_->GenerateFrameExit();
5288}
5289
5290void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5291 ret->SetLocations(nullptr);
5292}
5293
5294void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5295 codegen_->GenerateFrameExit();
5296}
5297
Alexey Frunze92d90602015-12-18 18:16:36 -08005298void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5299 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005300}
5301
Alexey Frunze92d90602015-12-18 18:16:36 -08005302void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5303 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005304}
5305
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005306void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5307 HandleShift(shl);
5308}
5309
5310void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5311 HandleShift(shl);
5312}
5313
5314void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5315 HandleShift(shr);
5316}
5317
5318void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5319 HandleShift(shr);
5320}
5321
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005322void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5323 HandleBinaryOp(instruction);
5324}
5325
5326void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5327 HandleBinaryOp(instruction);
5328}
5329
5330void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5331 HandleFieldGet(instruction, instruction->GetFieldInfo());
5332}
5333
5334void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5335 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5336}
5337
5338void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5339 HandleFieldSet(instruction, instruction->GetFieldInfo());
5340}
5341
5342void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5343 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5344}
5345
5346void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5347 HUnresolvedInstanceFieldGet* instruction) {
5348 FieldAccessCallingConventionMIPS calling_convention;
5349 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5350 instruction->GetFieldType(),
5351 calling_convention);
5352}
5353
5354void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5355 HUnresolvedInstanceFieldGet* instruction) {
5356 FieldAccessCallingConventionMIPS calling_convention;
5357 codegen_->GenerateUnresolvedFieldAccess(instruction,
5358 instruction->GetFieldType(),
5359 instruction->GetFieldIndex(),
5360 instruction->GetDexPc(),
5361 calling_convention);
5362}
5363
5364void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5365 HUnresolvedInstanceFieldSet* instruction) {
5366 FieldAccessCallingConventionMIPS calling_convention;
5367 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5368 instruction->GetFieldType(),
5369 calling_convention);
5370}
5371
5372void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5373 HUnresolvedInstanceFieldSet* instruction) {
5374 FieldAccessCallingConventionMIPS calling_convention;
5375 codegen_->GenerateUnresolvedFieldAccess(instruction,
5376 instruction->GetFieldType(),
5377 instruction->GetFieldIndex(),
5378 instruction->GetDexPc(),
5379 calling_convention);
5380}
5381
5382void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5383 HUnresolvedStaticFieldGet* instruction) {
5384 FieldAccessCallingConventionMIPS calling_convention;
5385 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5386 instruction->GetFieldType(),
5387 calling_convention);
5388}
5389
5390void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5391 HUnresolvedStaticFieldGet* instruction) {
5392 FieldAccessCallingConventionMIPS calling_convention;
5393 codegen_->GenerateUnresolvedFieldAccess(instruction,
5394 instruction->GetFieldType(),
5395 instruction->GetFieldIndex(),
5396 instruction->GetDexPc(),
5397 calling_convention);
5398}
5399
5400void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5401 HUnresolvedStaticFieldSet* instruction) {
5402 FieldAccessCallingConventionMIPS calling_convention;
5403 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5404 instruction->GetFieldType(),
5405 calling_convention);
5406}
5407
5408void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5409 HUnresolvedStaticFieldSet* instruction) {
5410 FieldAccessCallingConventionMIPS calling_convention;
5411 codegen_->GenerateUnresolvedFieldAccess(instruction,
5412 instruction->GetFieldType(),
5413 instruction->GetFieldIndex(),
5414 instruction->GetDexPc(),
5415 calling_convention);
5416}
5417
5418void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005419 LocationSummary* locations =
5420 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005421 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005422}
5423
5424void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5425 HBasicBlock* block = instruction->GetBlock();
5426 if (block->GetLoopInformation() != nullptr) {
5427 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5428 // The back edge will generate the suspend check.
5429 return;
5430 }
5431 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5432 // The goto will generate the suspend check.
5433 return;
5434 }
5435 GenerateSuspendCheck(instruction, nullptr);
5436}
5437
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005438void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5439 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005440 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005441 InvokeRuntimeCallingConvention calling_convention;
5442 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5443}
5444
5445void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005446 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005447 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5448}
5449
5450void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5451 Primitive::Type input_type = conversion->GetInputType();
5452 Primitive::Type result_type = conversion->GetResultType();
5453 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005454 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005455
5456 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5457 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5458 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5459 }
5460
5461 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005462 if (!isR6 &&
5463 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5464 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005465 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005466 }
5467
5468 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5469
5470 if (call_kind == LocationSummary::kNoCall) {
5471 if (Primitive::IsFloatingPointType(input_type)) {
5472 locations->SetInAt(0, Location::RequiresFpuRegister());
5473 } else {
5474 locations->SetInAt(0, Location::RequiresRegister());
5475 }
5476
5477 if (Primitive::IsFloatingPointType(result_type)) {
5478 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5479 } else {
5480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5481 }
5482 } else {
5483 InvokeRuntimeCallingConvention calling_convention;
5484
5485 if (Primitive::IsFloatingPointType(input_type)) {
5486 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5487 } else {
5488 DCHECK_EQ(input_type, Primitive::kPrimLong);
5489 locations->SetInAt(0, Location::RegisterPairLocation(
5490 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5491 }
5492
5493 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5494 }
5495}
5496
5497void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5498 LocationSummary* locations = conversion->GetLocations();
5499 Primitive::Type result_type = conversion->GetResultType();
5500 Primitive::Type input_type = conversion->GetInputType();
5501 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005502 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005503
5504 DCHECK_NE(input_type, result_type);
5505
5506 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5507 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5508 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5509 Register src = locations->InAt(0).AsRegister<Register>();
5510
Alexey Frunzea871ef12016-06-27 15:20:11 -07005511 if (dst_low != src) {
5512 __ Move(dst_low, src);
5513 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005514 __ Sra(dst_high, src, 31);
5515 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5516 Register dst = locations->Out().AsRegister<Register>();
5517 Register src = (input_type == Primitive::kPrimLong)
5518 ? locations->InAt(0).AsRegisterPairLow<Register>()
5519 : locations->InAt(0).AsRegister<Register>();
5520
5521 switch (result_type) {
5522 case Primitive::kPrimChar:
5523 __ Andi(dst, src, 0xFFFF);
5524 break;
5525 case Primitive::kPrimByte:
5526 if (has_sign_extension) {
5527 __ Seb(dst, src);
5528 } else {
5529 __ Sll(dst, src, 24);
5530 __ Sra(dst, dst, 24);
5531 }
5532 break;
5533 case Primitive::kPrimShort:
5534 if (has_sign_extension) {
5535 __ Seh(dst, src);
5536 } else {
5537 __ Sll(dst, src, 16);
5538 __ Sra(dst, dst, 16);
5539 }
5540 break;
5541 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005542 if (dst != src) {
5543 __ Move(dst, src);
5544 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005545 break;
5546
5547 default:
5548 LOG(FATAL) << "Unexpected type conversion from " << input_type
5549 << " to " << result_type;
5550 }
5551 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005552 if (input_type == Primitive::kPrimLong) {
5553 if (isR6) {
5554 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5555 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5556 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5557 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5558 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5559 __ Mtc1(src_low, FTMP);
5560 __ Mthc1(src_high, FTMP);
5561 if (result_type == Primitive::kPrimFloat) {
5562 __ Cvtsl(dst, FTMP);
5563 } else {
5564 __ Cvtdl(dst, FTMP);
5565 }
5566 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005567 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
5568 : kQuickL2d;
5569 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005570 if (result_type == Primitive::kPrimFloat) {
5571 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5572 } else {
5573 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5574 }
5575 }
5576 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005577 Register src = locations->InAt(0).AsRegister<Register>();
5578 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5579 __ Mtc1(src, FTMP);
5580 if (result_type == Primitive::kPrimFloat) {
5581 __ Cvtsw(dst, FTMP);
5582 } else {
5583 __ Cvtdw(dst, FTMP);
5584 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005585 }
5586 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5587 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005588 if (result_type == Primitive::kPrimLong) {
5589 if (isR6) {
5590 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5591 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5592 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5593 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5594 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5595 MipsLabel truncate;
5596 MipsLabel done;
5597
5598 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5599 // value when the input is either a NaN or is outside of the range of the output type
5600 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5601 // the same result.
5602 //
5603 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5604 // value of the output type if the input is outside of the range after the truncation or
5605 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5606 // results. This matches the desired float/double-to-int/long conversion exactly.
5607 //
5608 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5609 //
5610 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5611 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5612 // even though it must be NAN2008=1 on R6.
5613 //
5614 // The code takes care of the different behaviors by first comparing the input to the
5615 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5616 // If the input is greater than or equal to the minimum, it procedes to the truncate
5617 // instruction, which will handle such an input the same way irrespective of NAN2008.
5618 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5619 // in order to return either zero or the minimum value.
5620 //
5621 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5622 // truncate instruction for MIPS64R6.
5623 if (input_type == Primitive::kPrimFloat) {
5624 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5625 __ LoadConst32(TMP, min_val);
5626 __ Mtc1(TMP, FTMP);
5627 __ CmpLeS(FTMP, FTMP, src);
5628 } else {
5629 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5630 __ LoadConst32(TMP, High32Bits(min_val));
5631 __ Mtc1(ZERO, FTMP);
5632 __ Mthc1(TMP, FTMP);
5633 __ CmpLeD(FTMP, FTMP, src);
5634 }
5635
5636 __ Bc1nez(FTMP, &truncate);
5637
5638 if (input_type == Primitive::kPrimFloat) {
5639 __ CmpEqS(FTMP, src, src);
5640 } else {
5641 __ CmpEqD(FTMP, src, src);
5642 }
5643 __ Move(dst_low, ZERO);
5644 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5645 __ Mfc1(TMP, FTMP);
5646 __ And(dst_high, dst_high, TMP);
5647
5648 __ B(&done);
5649
5650 __ Bind(&truncate);
5651
5652 if (input_type == Primitive::kPrimFloat) {
5653 __ TruncLS(FTMP, src);
5654 } else {
5655 __ TruncLD(FTMP, src);
5656 }
5657 __ Mfc1(dst_low, FTMP);
5658 __ Mfhc1(dst_high, FTMP);
5659
5660 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005661 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005662 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
5663 : kQuickD2l;
5664 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005665 if (input_type == Primitive::kPrimFloat) {
5666 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5667 } else {
5668 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5669 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005670 }
5671 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005672 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5673 Register dst = locations->Out().AsRegister<Register>();
5674 MipsLabel truncate;
5675 MipsLabel done;
5676
5677 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5678 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5679 // even though it must be NAN2008=1 on R6.
5680 //
5681 // For details see the large comment above for the truncation of float/double to long on R6.
5682 //
5683 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5684 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005685 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005686 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5687 __ LoadConst32(TMP, min_val);
5688 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005689 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005690 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5691 __ LoadConst32(TMP, High32Bits(min_val));
5692 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005693 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005694 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005695
5696 if (isR6) {
5697 if (input_type == Primitive::kPrimFloat) {
5698 __ CmpLeS(FTMP, FTMP, src);
5699 } else {
5700 __ CmpLeD(FTMP, FTMP, src);
5701 }
5702 __ Bc1nez(FTMP, &truncate);
5703
5704 if (input_type == Primitive::kPrimFloat) {
5705 __ CmpEqS(FTMP, src, src);
5706 } else {
5707 __ CmpEqD(FTMP, src, src);
5708 }
5709 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5710 __ Mfc1(TMP, FTMP);
5711 __ And(dst, dst, TMP);
5712 } else {
5713 if (input_type == Primitive::kPrimFloat) {
5714 __ ColeS(0, FTMP, src);
5715 } else {
5716 __ ColeD(0, FTMP, src);
5717 }
5718 __ Bc1t(0, &truncate);
5719
5720 if (input_type == Primitive::kPrimFloat) {
5721 __ CeqS(0, src, src);
5722 } else {
5723 __ CeqD(0, src, src);
5724 }
5725 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5726 __ Movf(dst, ZERO, 0);
5727 }
5728
5729 __ B(&done);
5730
5731 __ Bind(&truncate);
5732
5733 if (input_type == Primitive::kPrimFloat) {
5734 __ TruncWS(FTMP, src);
5735 } else {
5736 __ TruncWD(FTMP, src);
5737 }
5738 __ Mfc1(dst, FTMP);
5739
5740 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005741 }
5742 } else if (Primitive::IsFloatingPointType(result_type) &&
5743 Primitive::IsFloatingPointType(input_type)) {
5744 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5745 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5746 if (result_type == Primitive::kPrimFloat) {
5747 __ Cvtsd(dst, src);
5748 } else {
5749 __ Cvtds(dst, src);
5750 }
5751 } else {
5752 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5753 << " to " << result_type;
5754 }
5755}
5756
5757void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5758 HandleShift(ushr);
5759}
5760
5761void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5762 HandleShift(ushr);
5763}
5764
5765void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5766 HandleBinaryOp(instruction);
5767}
5768
5769void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5770 HandleBinaryOp(instruction);
5771}
5772
5773void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5774 // Nothing to do, this should be removed during prepare for register allocator.
5775 LOG(FATAL) << "Unreachable";
5776}
5777
5778void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5779 // Nothing to do, this should be removed during prepare for register allocator.
5780 LOG(FATAL) << "Unreachable";
5781}
5782
5783void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005784 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005785}
5786
5787void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005788 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005789}
5790
5791void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005792 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005793}
5794
5795void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005796 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005797}
5798
5799void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005800 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005801}
5802
5803void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005804 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005805}
5806
5807void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005808 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005809}
5810
5811void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005812 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005813}
5814
5815void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005816 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005817}
5818
5819void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005820 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005821}
5822
5823void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005824 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005825}
5826
5827void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005828 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005829}
5830
5831void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005832 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005833}
5834
5835void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005836 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005837}
5838
5839void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005840 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005841}
5842
5843void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005844 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005845}
5846
5847void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005848 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005849}
5850
5851void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005852 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005853}
5854
5855void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005856 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005857}
5858
5859void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005860 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005861}
5862
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005863void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5864 LocationSummary* locations =
5865 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5866 locations->SetInAt(0, Location::RequiresRegister());
5867}
5868
Alexey Frunze96b66822016-09-10 02:32:44 -07005869void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
5870 int32_t lower_bound,
5871 uint32_t num_entries,
5872 HBasicBlock* switch_block,
5873 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005874 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005875 Register temp_reg = TMP;
5876 __ Addiu32(temp_reg, value_reg, -lower_bound);
5877 // Jump to default if index is negative
5878 // Note: We don't check the case that index is positive while value < lower_bound, because in
5879 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5880 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5881
Alexey Frunze96b66822016-09-10 02:32:44 -07005882 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005883 // Jump to successors[0] if value == lower_bound.
5884 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5885 int32_t last_index = 0;
5886 for (; num_entries - last_index > 2; last_index += 2) {
5887 __ Addiu(temp_reg, temp_reg, -2);
5888 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5889 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5890 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5891 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5892 }
5893 if (num_entries - last_index == 2) {
5894 // The last missing case_value.
5895 __ Addiu(temp_reg, temp_reg, -1);
5896 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005897 }
5898
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005899 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07005900 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005901 __ B(codegen_->GetLabelOf(default_block));
5902 }
5903}
5904
Alexey Frunze96b66822016-09-10 02:32:44 -07005905void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
5906 Register constant_area,
5907 int32_t lower_bound,
5908 uint32_t num_entries,
5909 HBasicBlock* switch_block,
5910 HBasicBlock* default_block) {
5911 // Create a jump table.
5912 std::vector<MipsLabel*> labels(num_entries);
5913 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
5914 for (uint32_t i = 0; i < num_entries; i++) {
5915 labels[i] = codegen_->GetLabelOf(successors[i]);
5916 }
5917 JumpTable* table = __ CreateJumpTable(std::move(labels));
5918
5919 // Is the value in range?
5920 __ Addiu32(TMP, value_reg, -lower_bound);
5921 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
5922 __ Sltiu(AT, TMP, num_entries);
5923 __ Beqz(AT, codegen_->GetLabelOf(default_block));
5924 } else {
5925 __ LoadConst32(AT, num_entries);
5926 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
5927 }
5928
5929 // We are in the range of the table.
5930 // Load the target address from the jump table, indexing by the value.
5931 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
5932 __ Sll(TMP, TMP, 2);
5933 __ Addu(TMP, TMP, AT);
5934 __ Lw(TMP, TMP, 0);
5935 // Compute the absolute target address by adding the table start address
5936 // (the table contains offsets to targets relative to its start).
5937 __ Addu(TMP, TMP, AT);
5938 // And jump.
5939 __ Jr(TMP);
5940 __ NopIfNoReordering();
5941}
5942
5943void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5944 int32_t lower_bound = switch_instr->GetStartValue();
5945 uint32_t num_entries = switch_instr->GetNumEntries();
5946 LocationSummary* locations = switch_instr->GetLocations();
5947 Register value_reg = locations->InAt(0).AsRegister<Register>();
5948 HBasicBlock* switch_block = switch_instr->GetBlock();
5949 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5950
5951 if (codegen_->GetInstructionSetFeatures().IsR6() &&
5952 num_entries > kPackedSwitchJumpTableThreshold) {
5953 // R6 uses PC-relative addressing to access the jump table.
5954 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
5955 // the jump table and it is implemented by changing HPackedSwitch to
5956 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
5957 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
5958 GenTableBasedPackedSwitch(value_reg,
5959 ZERO,
5960 lower_bound,
5961 num_entries,
5962 switch_block,
5963 default_block);
5964 } else {
5965 GenPackedSwitchWithCompares(value_reg,
5966 lower_bound,
5967 num_entries,
5968 switch_block,
5969 default_block);
5970 }
5971}
5972
5973void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5974 LocationSummary* locations =
5975 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5976 locations->SetInAt(0, Location::RequiresRegister());
5977 // Constant area pointer (HMipsComputeBaseMethodAddress).
5978 locations->SetInAt(1, Location::RequiresRegister());
5979}
5980
5981void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5982 int32_t lower_bound = switch_instr->GetStartValue();
5983 uint32_t num_entries = switch_instr->GetNumEntries();
5984 LocationSummary* locations = switch_instr->GetLocations();
5985 Register value_reg = locations->InAt(0).AsRegister<Register>();
5986 Register constant_area = locations->InAt(1).AsRegister<Register>();
5987 HBasicBlock* switch_block = switch_instr->GetBlock();
5988 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5989
5990 // This is an R2-only path. HPackedSwitch has been changed to
5991 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
5992 // required to address the jump table relative to PC.
5993 GenTableBasedPackedSwitch(value_reg,
5994 constant_area,
5995 lower_bound,
5996 num_entries,
5997 switch_block,
5998 default_block);
5999}
6000
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006001void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6002 HMipsComputeBaseMethodAddress* insn) {
6003 LocationSummary* locations =
6004 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6005 locations->SetOut(Location::RequiresRegister());
6006}
6007
6008void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6009 HMipsComputeBaseMethodAddress* insn) {
6010 LocationSummary* locations = insn->GetLocations();
6011 Register reg = locations->Out().AsRegister<Register>();
6012
6013 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6014
6015 // Generate a dummy PC-relative call to obtain PC.
6016 __ Nal();
6017 // Grab the return address off RA.
6018 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006019 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006020
6021 // Remember this offset (the obtained PC value) for later use with constant area.
6022 __ BindPcRelBaseLabel();
6023}
6024
6025void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6026 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6027 locations->SetOut(Location::RequiresRegister());
6028}
6029
6030void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6031 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6032 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6033 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006034 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6035 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006036}
6037
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006038void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6039 // The trampoline uses the same calling convention as dex calling conventions,
6040 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6041 // the method_idx.
6042 HandleInvoke(invoke);
6043}
6044
6045void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6046 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6047}
6048
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006049void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6050 LocationSummary* locations =
6051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6052 locations->SetInAt(0, Location::RequiresRegister());
6053 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006054}
6055
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006056void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6057 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006058 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006059 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006060 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006061 __ LoadFromOffset(kLoadWord,
6062 locations->Out().AsRegister<Register>(),
6063 locations->InAt(0).AsRegister<Register>(),
6064 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006065 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006066 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006067 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006068 __ LoadFromOffset(kLoadWord,
6069 locations->Out().AsRegister<Register>(),
6070 locations->InAt(0).AsRegister<Register>(),
6071 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006072 __ LoadFromOffset(kLoadWord,
6073 locations->Out().AsRegister<Register>(),
6074 locations->Out().AsRegister<Register>(),
6075 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006076 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006077}
6078
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006079#undef __
6080#undef QUICK_ENTRY_POINT
6081
6082} // namespace mips
6083} // namespace art