blob: bc8bb480ec226cc5358b3bdef6c2bfd3626694ef [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
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100746 // Save the current method if we need it. Note that we do not
747 // do this in HCurrentMethod, as the instruction might have been removed
748 // in the SSA graph.
749 if (RequiresCurrentMethod()) {
750 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
751 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200752}
753
754void CodeGeneratorMIPS::GenerateFrameExit() {
755 __ cfi().RememberState();
756
757 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200758 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200759
Alexey Frunze73296a72016-06-03 22:51:46 -0700760 // For better instruction scheduling restore RA before other registers.
761 uint32_t ofs = GetFrameSize();
762 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
763 Register reg = static_cast<Register>(MostSignificantBit(mask));
764 mask ^= 1u << reg;
765 ofs -= kMipsWordSize;
766 // The ZERO register is only included for alignment.
767 if (reg != ZERO) {
768 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200769 __ cfi().Restore(DWARFReg(reg));
770 }
771 }
772
Alexey Frunze73296a72016-06-03 22:51:46 -0700773 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
774 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
775 mask ^= 1u << reg;
776 ofs -= kMipsDoublewordSize;
777 __ LoadDFromOffset(reg, SP, ofs);
778 // TODO: __ cfi().Restore(DWARFReg(reg));
779 }
780
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700781 size_t frame_size = GetFrameSize();
782 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
783 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
784 bool reordering = __ SetReorder(false);
785 if (exchange) {
786 __ Jr(RA);
787 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
788 } else {
789 __ DecreaseFrameSize(frame_size);
790 __ Jr(RA);
791 __ Nop(); // In delay slot.
792 }
793 __ SetReorder(reordering);
794 } else {
795 __ Jr(RA);
796 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200797 }
798
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200799 __ cfi().RestoreState();
800 __ cfi().DefCFAOffset(GetFrameSize());
801}
802
803void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
804 __ Bind(GetLabelOf(block));
805}
806
807void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
808 if (src.Equals(dst)) {
809 return;
810 }
811
812 if (src.IsConstant()) {
813 MoveConstant(dst, src.GetConstant());
814 } else {
815 if (Primitive::Is64BitType(dst_type)) {
816 Move64(dst, src);
817 } else {
818 Move32(dst, src);
819 }
820 }
821}
822
823void CodeGeneratorMIPS::Move32(Location destination, Location source) {
824 if (source.Equals(destination)) {
825 return;
826 }
827
828 if (destination.IsRegister()) {
829 if (source.IsRegister()) {
830 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
831 } else if (source.IsFpuRegister()) {
832 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
833 } else {
834 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
835 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
836 }
837 } else if (destination.IsFpuRegister()) {
838 if (source.IsRegister()) {
839 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
840 } else if (source.IsFpuRegister()) {
841 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
842 } else {
843 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
844 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
845 }
846 } else {
847 DCHECK(destination.IsStackSlot()) << destination;
848 if (source.IsRegister()) {
849 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
850 } else if (source.IsFpuRegister()) {
851 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
852 } else {
853 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
854 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
855 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
856 }
857 }
858}
859
860void CodeGeneratorMIPS::Move64(Location destination, Location source) {
861 if (source.Equals(destination)) {
862 return;
863 }
864
865 if (destination.IsRegisterPair()) {
866 if (source.IsRegisterPair()) {
867 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
868 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
869 } else if (source.IsFpuRegister()) {
870 Register dst_high = destination.AsRegisterPairHigh<Register>();
871 Register dst_low = destination.AsRegisterPairLow<Register>();
872 FRegister src = source.AsFpuRegister<FRegister>();
873 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800874 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200875 } else {
876 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
877 int32_t off = source.GetStackIndex();
878 Register r = destination.AsRegisterPairLow<Register>();
879 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
880 }
881 } else if (destination.IsFpuRegister()) {
882 if (source.IsRegisterPair()) {
883 FRegister dst = destination.AsFpuRegister<FRegister>();
884 Register src_high = source.AsRegisterPairHigh<Register>();
885 Register src_low = source.AsRegisterPairLow<Register>();
886 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800887 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200888 } else if (source.IsFpuRegister()) {
889 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
890 } else {
891 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
892 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
893 }
894 } else {
895 DCHECK(destination.IsDoubleStackSlot()) << destination;
896 int32_t off = destination.GetStackIndex();
897 if (source.IsRegisterPair()) {
898 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
899 } else if (source.IsFpuRegister()) {
900 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
901 } else {
902 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
903 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
904 __ StoreToOffset(kStoreWord, TMP, SP, off);
905 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
906 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
907 }
908 }
909}
910
911void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
912 if (c->IsIntConstant() || c->IsNullConstant()) {
913 // Move 32 bit constant.
914 int32_t value = GetInt32ValueOf(c);
915 if (destination.IsRegister()) {
916 Register dst = destination.AsRegister<Register>();
917 __ LoadConst32(dst, value);
918 } else {
919 DCHECK(destination.IsStackSlot())
920 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700921 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200922 }
923 } else if (c->IsLongConstant()) {
924 // Move 64 bit constant.
925 int64_t value = GetInt64ValueOf(c);
926 if (destination.IsRegisterPair()) {
927 Register r_h = destination.AsRegisterPairHigh<Register>();
928 Register r_l = destination.AsRegisterPairLow<Register>();
929 __ LoadConst64(r_h, r_l, value);
930 } else {
931 DCHECK(destination.IsDoubleStackSlot())
932 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700933 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200934 }
935 } else if (c->IsFloatConstant()) {
936 // Move 32 bit float constant.
937 int32_t value = GetInt32ValueOf(c);
938 if (destination.IsFpuRegister()) {
939 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
940 } else {
941 DCHECK(destination.IsStackSlot())
942 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700943 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200944 }
945 } else {
946 // Move 64 bit double constant.
947 DCHECK(c->IsDoubleConstant()) << c->DebugName();
948 int64_t value = GetInt64ValueOf(c);
949 if (destination.IsFpuRegister()) {
950 FRegister fd = destination.AsFpuRegister<FRegister>();
951 __ LoadDConst64(fd, value, TMP);
952 } else {
953 DCHECK(destination.IsDoubleStackSlot())
954 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700955 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200956 }
957 }
958}
959
960void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
961 DCHECK(destination.IsRegister());
962 Register dst = destination.AsRegister<Register>();
963 __ LoadConst32(dst, value);
964}
965
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200966void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
967 if (location.IsRegister()) {
968 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700969 } else if (location.IsRegisterPair()) {
970 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
971 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200972 } else {
973 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
974 }
975}
976
Vladimir Markoaad75c62016-10-03 08:46:48 +0000977template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
978inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
979 const ArenaDeque<PcRelativePatchInfo>& infos,
980 ArenaVector<LinkerPatch>* linker_patches) {
981 for (const PcRelativePatchInfo& info : infos) {
982 const DexFile& dex_file = info.target_dex_file;
983 size_t offset_or_index = info.offset_or_index;
984 DCHECK(info.high_label.IsBound());
985 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
986 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
987 // the assembler's base label used for PC-relative addressing.
988 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
989 ? __ GetLabelLocation(&info.pc_rel_label)
990 : __ GetPcRelBaseLabelLocation();
991 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
992 }
993}
994
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700995void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
996 DCHECK(linker_patches->empty());
997 size_t size =
998 method_patches_.size() +
999 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001000 pc_relative_dex_cache_patches_.size() +
1001 pc_relative_string_patches_.size() +
1002 pc_relative_type_patches_.size() +
1003 boot_image_string_patches_.size() +
1004 boot_image_type_patches_.size() +
1005 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001006 linker_patches->reserve(size);
1007 for (const auto& entry : method_patches_) {
1008 const MethodReference& target_method = entry.first;
1009 Literal* literal = entry.second;
1010 DCHECK(literal->GetLabel()->IsBound());
1011 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1012 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1013 target_method.dex_file,
1014 target_method.dex_method_index));
1015 }
1016 for (const auto& entry : call_patches_) {
1017 const MethodReference& target_method = entry.first;
1018 Literal* literal = entry.second;
1019 DCHECK(literal->GetLabel()->IsBound());
1020 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1021 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1022 target_method.dex_file,
1023 target_method.dex_method_index));
1024 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001025 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1026 linker_patches);
1027 if (!GetCompilerOptions().IsBootImage()) {
1028 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1029 linker_patches);
1030 } else {
1031 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1032 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001033 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001034 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1035 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001036 for (const auto& entry : boot_image_string_patches_) {
1037 const StringReference& target_string = entry.first;
1038 Literal* literal = entry.second;
1039 DCHECK(literal->GetLabel()->IsBound());
1040 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1041 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1042 target_string.dex_file,
1043 target_string.string_index));
1044 }
1045 for (const auto& entry : boot_image_type_patches_) {
1046 const TypeReference& target_type = entry.first;
1047 Literal* literal = entry.second;
1048 DCHECK(literal->GetLabel()->IsBound());
1049 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1050 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1051 target_type.dex_file,
1052 target_type.type_index));
1053 }
1054 for (const auto& entry : boot_image_address_patches_) {
1055 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1056 Literal* literal = entry.second;
1057 DCHECK(literal->GetLabel()->IsBound());
1058 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1059 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1060 }
1061}
1062
1063CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1064 const DexFile& dex_file, uint32_t string_index) {
1065 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1066}
1067
1068CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1069 const DexFile& dex_file, uint32_t type_index) {
1070 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001071}
1072
1073CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1074 const DexFile& dex_file, uint32_t element_offset) {
1075 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1076}
1077
1078CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1079 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1080 patches->emplace_back(dex_file, offset_or_index);
1081 return &patches->back();
1082}
1083
Alexey Frunze06a46c42016-07-19 15:00:40 -07001084Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1085 return map->GetOrCreate(
1086 value,
1087 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1088}
1089
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001090Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1091 MethodToLiteralMap* map) {
1092 return map->GetOrCreate(
1093 target_method,
1094 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1095}
1096
1097Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1098 return DeduplicateMethodLiteral(target_method, &method_patches_);
1099}
1100
1101Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1102 return DeduplicateMethodLiteral(target_method, &call_patches_);
1103}
1104
Alexey Frunze06a46c42016-07-19 15:00:40 -07001105Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1106 uint32_t string_index) {
1107 return boot_image_string_patches_.GetOrCreate(
1108 StringReference(&dex_file, string_index),
1109 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1110}
1111
1112Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1113 uint32_t type_index) {
1114 return boot_image_type_patches_.GetOrCreate(
1115 TypeReference(&dex_file, type_index),
1116 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1117}
1118
1119Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1120 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1121 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1122 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1123}
1124
Vladimir Markoaad75c62016-10-03 08:46:48 +00001125void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1126 PcRelativePatchInfo* info, Register out, Register base) {
1127 bool reordering = __ SetReorder(false);
1128 if (GetInstructionSetFeatures().IsR6()) {
1129 DCHECK_EQ(base, ZERO);
1130 __ Bind(&info->high_label);
1131 __ Bind(&info->pc_rel_label);
1132 // Add a 32-bit offset to PC.
1133 __ Auipc(out, /* placeholder */ 0x1234);
1134 __ Addiu(out, out, /* placeholder */ 0x5678);
1135 } else {
1136 // If base is ZERO, emit NAL to obtain the actual base.
1137 if (base == ZERO) {
1138 // Generate a dummy PC-relative call to obtain PC.
1139 __ Nal();
1140 }
1141 __ Bind(&info->high_label);
1142 __ Lui(out, /* placeholder */ 0x1234);
1143 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1144 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1145 if (base == ZERO) {
1146 __ Bind(&info->pc_rel_label);
1147 }
1148 __ Ori(out, out, /* placeholder */ 0x5678);
1149 // Add a 32-bit offset to PC.
1150 __ Addu(out, out, (base == ZERO) ? RA : base);
1151 }
1152 __ SetReorder(reordering);
1153}
1154
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001155void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1156 MipsLabel done;
1157 Register card = AT;
1158 Register temp = TMP;
1159 __ Beqz(value, &done);
1160 __ LoadFromOffset(kLoadWord,
1161 card,
1162 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001163 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001164 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1165 __ Addu(temp, card, temp);
1166 __ Sb(card, temp, 0);
1167 __ Bind(&done);
1168}
1169
David Brazdil58282f42016-01-14 12:45:10 +00001170void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001171 // Don't allocate the dalvik style register pair passing.
1172 blocked_register_pairs_[A1_A2] = true;
1173
1174 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1175 blocked_core_registers_[ZERO] = true;
1176 blocked_core_registers_[K0] = true;
1177 blocked_core_registers_[K1] = true;
1178 blocked_core_registers_[GP] = true;
1179 blocked_core_registers_[SP] = true;
1180 blocked_core_registers_[RA] = true;
1181
1182 // AT and TMP(T8) are used as temporary/scratch registers
1183 // (similar to how AT is used by MIPS assemblers).
1184 blocked_core_registers_[AT] = true;
1185 blocked_core_registers_[TMP] = true;
1186 blocked_fpu_registers_[FTMP] = true;
1187
1188 // Reserve suspend and thread registers.
1189 blocked_core_registers_[S0] = true;
1190 blocked_core_registers_[TR] = true;
1191
1192 // Reserve T9 for function calls
1193 blocked_core_registers_[T9] = true;
1194
1195 // Reserve odd-numbered FPU registers.
1196 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1197 blocked_fpu_registers_[i] = true;
1198 }
1199
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001200 if (GetGraph()->IsDebuggable()) {
1201 // Stubs do not save callee-save floating point registers. If the graph
1202 // is debuggable, we need to deal with these registers differently. For
1203 // now, just block them.
1204 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1205 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1206 }
1207 }
1208
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001209 UpdateBlockedPairRegisters();
1210}
1211
1212void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1213 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1214 MipsManagedRegister current =
1215 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1216 if (blocked_core_registers_[current.AsRegisterPairLow()]
1217 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1218 blocked_register_pairs_[i] = true;
1219 }
1220 }
1221}
1222
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001223size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1224 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1225 return kMipsWordSize;
1226}
1227
1228size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1229 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1230 return kMipsWordSize;
1231}
1232
1233size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1234 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1235 return kMipsDoublewordSize;
1236}
1237
1238size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1239 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1240 return kMipsDoublewordSize;
1241}
1242
1243void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001244 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001245}
1246
1247void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001248 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001249}
1250
Serban Constantinescufca16662016-07-14 09:21:59 +01001251constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1252
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001253void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1254 HInstruction* instruction,
1255 uint32_t dex_pc,
1256 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001257 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001258 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001259 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001260 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001261 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001262 // Reserve argument space on stack (for $a0-$a3) for
1263 // entrypoints that directly reference native implementations.
1264 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001265 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001266 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001267 } else {
1268 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001269 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001270 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001271 if (EntrypointRequiresStackMap(entrypoint)) {
1272 RecordPcInfo(instruction, dex_pc, slow_path);
1273 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274}
1275
1276void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1277 Register class_reg) {
1278 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1279 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1280 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1281 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1282 __ Sync(0);
1283 __ Bind(slow_path->GetExitLabel());
1284}
1285
1286void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1287 __ Sync(0); // Only stype 0 is supported.
1288}
1289
1290void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1291 HBasicBlock* successor) {
1292 SuspendCheckSlowPathMIPS* slow_path =
1293 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1294 codegen_->AddSlowPath(slow_path);
1295
1296 __ LoadFromOffset(kLoadUnsignedHalfword,
1297 TMP,
1298 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001299 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001300 if (successor == nullptr) {
1301 __ Bnez(TMP, slow_path->GetEntryLabel());
1302 __ Bind(slow_path->GetReturnLabel());
1303 } else {
1304 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1305 __ B(slow_path->GetEntryLabel());
1306 // slow_path will return to GetLabelOf(successor).
1307 }
1308}
1309
1310InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1311 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001312 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001313 assembler_(codegen->GetAssembler()),
1314 codegen_(codegen) {}
1315
1316void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1317 DCHECK_EQ(instruction->InputCount(), 2U);
1318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1319 Primitive::Type type = instruction->GetResultType();
1320 switch (type) {
1321 case Primitive::kPrimInt: {
1322 locations->SetInAt(0, Location::RequiresRegister());
1323 HInstruction* right = instruction->InputAt(1);
1324 bool can_use_imm = false;
1325 if (right->IsConstant()) {
1326 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1327 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1328 can_use_imm = IsUint<16>(imm);
1329 } else if (instruction->IsAdd()) {
1330 can_use_imm = IsInt<16>(imm);
1331 } else {
1332 DCHECK(instruction->IsSub());
1333 can_use_imm = IsInt<16>(-imm);
1334 }
1335 }
1336 if (can_use_imm)
1337 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1338 else
1339 locations->SetInAt(1, Location::RequiresRegister());
1340 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1341 break;
1342 }
1343
1344 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001346 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001348 break;
1349 }
1350
1351 case Primitive::kPrimFloat:
1352 case Primitive::kPrimDouble:
1353 DCHECK(instruction->IsAdd() || instruction->IsSub());
1354 locations->SetInAt(0, Location::RequiresFpuRegister());
1355 locations->SetInAt(1, Location::RequiresFpuRegister());
1356 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1357 break;
1358
1359 default:
1360 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1361 }
1362}
1363
1364void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1365 Primitive::Type type = instruction->GetType();
1366 LocationSummary* locations = instruction->GetLocations();
1367
1368 switch (type) {
1369 case Primitive::kPrimInt: {
1370 Register dst = locations->Out().AsRegister<Register>();
1371 Register lhs = locations->InAt(0).AsRegister<Register>();
1372 Location rhs_location = locations->InAt(1);
1373
1374 Register rhs_reg = ZERO;
1375 int32_t rhs_imm = 0;
1376 bool use_imm = rhs_location.IsConstant();
1377 if (use_imm) {
1378 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1379 } else {
1380 rhs_reg = rhs_location.AsRegister<Register>();
1381 }
1382
1383 if (instruction->IsAnd()) {
1384 if (use_imm)
1385 __ Andi(dst, lhs, rhs_imm);
1386 else
1387 __ And(dst, lhs, rhs_reg);
1388 } else if (instruction->IsOr()) {
1389 if (use_imm)
1390 __ Ori(dst, lhs, rhs_imm);
1391 else
1392 __ Or(dst, lhs, rhs_reg);
1393 } else if (instruction->IsXor()) {
1394 if (use_imm)
1395 __ Xori(dst, lhs, rhs_imm);
1396 else
1397 __ Xor(dst, lhs, rhs_reg);
1398 } else if (instruction->IsAdd()) {
1399 if (use_imm)
1400 __ Addiu(dst, lhs, rhs_imm);
1401 else
1402 __ Addu(dst, lhs, rhs_reg);
1403 } else {
1404 DCHECK(instruction->IsSub());
1405 if (use_imm)
1406 __ Addiu(dst, lhs, -rhs_imm);
1407 else
1408 __ Subu(dst, lhs, rhs_reg);
1409 }
1410 break;
1411 }
1412
1413 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001414 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1415 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1416 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1417 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001418 Location rhs_location = locations->InAt(1);
1419 bool use_imm = rhs_location.IsConstant();
1420 if (!use_imm) {
1421 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1422 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1423 if (instruction->IsAnd()) {
1424 __ And(dst_low, lhs_low, rhs_low);
1425 __ And(dst_high, lhs_high, rhs_high);
1426 } else if (instruction->IsOr()) {
1427 __ Or(dst_low, lhs_low, rhs_low);
1428 __ Or(dst_high, lhs_high, rhs_high);
1429 } else if (instruction->IsXor()) {
1430 __ Xor(dst_low, lhs_low, rhs_low);
1431 __ Xor(dst_high, lhs_high, rhs_high);
1432 } else if (instruction->IsAdd()) {
1433 if (lhs_low == rhs_low) {
1434 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1435 __ Slt(TMP, lhs_low, ZERO);
1436 __ Addu(dst_low, lhs_low, rhs_low);
1437 } else {
1438 __ Addu(dst_low, lhs_low, rhs_low);
1439 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1440 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1441 }
1442 __ Addu(dst_high, lhs_high, rhs_high);
1443 __ Addu(dst_high, dst_high, TMP);
1444 } else {
1445 DCHECK(instruction->IsSub());
1446 __ Sltu(TMP, lhs_low, rhs_low);
1447 __ Subu(dst_low, lhs_low, rhs_low);
1448 __ Subu(dst_high, lhs_high, rhs_high);
1449 __ Subu(dst_high, dst_high, TMP);
1450 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001451 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001452 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1453 if (instruction->IsOr()) {
1454 uint32_t low = Low32Bits(value);
1455 uint32_t high = High32Bits(value);
1456 if (IsUint<16>(low)) {
1457 if (dst_low != lhs_low || low != 0) {
1458 __ Ori(dst_low, lhs_low, low);
1459 }
1460 } else {
1461 __ LoadConst32(TMP, low);
1462 __ Or(dst_low, lhs_low, TMP);
1463 }
1464 if (IsUint<16>(high)) {
1465 if (dst_high != lhs_high || high != 0) {
1466 __ Ori(dst_high, lhs_high, high);
1467 }
1468 } else {
1469 if (high != low) {
1470 __ LoadConst32(TMP, high);
1471 }
1472 __ Or(dst_high, lhs_high, TMP);
1473 }
1474 } else if (instruction->IsXor()) {
1475 uint32_t low = Low32Bits(value);
1476 uint32_t high = High32Bits(value);
1477 if (IsUint<16>(low)) {
1478 if (dst_low != lhs_low || low != 0) {
1479 __ Xori(dst_low, lhs_low, low);
1480 }
1481 } else {
1482 __ LoadConst32(TMP, low);
1483 __ Xor(dst_low, lhs_low, TMP);
1484 }
1485 if (IsUint<16>(high)) {
1486 if (dst_high != lhs_high || high != 0) {
1487 __ Xori(dst_high, lhs_high, high);
1488 }
1489 } else {
1490 if (high != low) {
1491 __ LoadConst32(TMP, high);
1492 }
1493 __ Xor(dst_high, lhs_high, TMP);
1494 }
1495 } else if (instruction->IsAnd()) {
1496 uint32_t low = Low32Bits(value);
1497 uint32_t high = High32Bits(value);
1498 if (IsUint<16>(low)) {
1499 __ Andi(dst_low, lhs_low, low);
1500 } else if (low != 0xFFFFFFFF) {
1501 __ LoadConst32(TMP, low);
1502 __ And(dst_low, lhs_low, TMP);
1503 } else if (dst_low != lhs_low) {
1504 __ Move(dst_low, lhs_low);
1505 }
1506 if (IsUint<16>(high)) {
1507 __ Andi(dst_high, lhs_high, high);
1508 } else if (high != 0xFFFFFFFF) {
1509 if (high != low) {
1510 __ LoadConst32(TMP, high);
1511 }
1512 __ And(dst_high, lhs_high, TMP);
1513 } else if (dst_high != lhs_high) {
1514 __ Move(dst_high, lhs_high);
1515 }
1516 } else {
1517 if (instruction->IsSub()) {
1518 value = -value;
1519 } else {
1520 DCHECK(instruction->IsAdd());
1521 }
1522 int32_t low = Low32Bits(value);
1523 int32_t high = High32Bits(value);
1524 if (IsInt<16>(low)) {
1525 if (dst_low != lhs_low || low != 0) {
1526 __ Addiu(dst_low, lhs_low, low);
1527 }
1528 if (low != 0) {
1529 __ Sltiu(AT, dst_low, low);
1530 }
1531 } else {
1532 __ LoadConst32(TMP, low);
1533 __ Addu(dst_low, lhs_low, TMP);
1534 __ Sltu(AT, dst_low, TMP);
1535 }
1536 if (IsInt<16>(high)) {
1537 if (dst_high != lhs_high || high != 0) {
1538 __ Addiu(dst_high, lhs_high, high);
1539 }
1540 } else {
1541 if (high != low) {
1542 __ LoadConst32(TMP, high);
1543 }
1544 __ Addu(dst_high, lhs_high, TMP);
1545 }
1546 if (low != 0) {
1547 __ Addu(dst_high, dst_high, AT);
1548 }
1549 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001550 }
1551 break;
1552 }
1553
1554 case Primitive::kPrimFloat:
1555 case Primitive::kPrimDouble: {
1556 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1557 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1558 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1559 if (instruction->IsAdd()) {
1560 if (type == Primitive::kPrimFloat) {
1561 __ AddS(dst, lhs, rhs);
1562 } else {
1563 __ AddD(dst, lhs, rhs);
1564 }
1565 } else {
1566 DCHECK(instruction->IsSub());
1567 if (type == Primitive::kPrimFloat) {
1568 __ SubS(dst, lhs, rhs);
1569 } else {
1570 __ SubD(dst, lhs, rhs);
1571 }
1572 }
1573 break;
1574 }
1575
1576 default:
1577 LOG(FATAL) << "Unexpected binary operation type " << type;
1578 }
1579}
1580
1581void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001582 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001583
1584 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1585 Primitive::Type type = instr->GetResultType();
1586 switch (type) {
1587 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001588 locations->SetInAt(0, Location::RequiresRegister());
1589 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1590 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1591 break;
1592 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001593 locations->SetInAt(0, Location::RequiresRegister());
1594 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1595 locations->SetOut(Location::RequiresRegister());
1596 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001597 default:
1598 LOG(FATAL) << "Unexpected shift type " << type;
1599 }
1600}
1601
1602static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1603
1604void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001605 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001606 LocationSummary* locations = instr->GetLocations();
1607 Primitive::Type type = instr->GetType();
1608
1609 Location rhs_location = locations->InAt(1);
1610 bool use_imm = rhs_location.IsConstant();
1611 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1612 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001613 const uint32_t shift_mask =
1614 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001615 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001616 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1617 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001618
1619 switch (type) {
1620 case Primitive::kPrimInt: {
1621 Register dst = locations->Out().AsRegister<Register>();
1622 Register lhs = locations->InAt(0).AsRegister<Register>();
1623 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001624 if (shift_value == 0) {
1625 if (dst != lhs) {
1626 __ Move(dst, lhs);
1627 }
1628 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001629 __ Sll(dst, lhs, shift_value);
1630 } else if (instr->IsShr()) {
1631 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001632 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001633 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001634 } else {
1635 if (has_ins_rotr) {
1636 __ Rotr(dst, lhs, shift_value);
1637 } else {
1638 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1639 __ Srl(dst, lhs, shift_value);
1640 __ Or(dst, dst, TMP);
1641 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001642 }
1643 } else {
1644 if (instr->IsShl()) {
1645 __ Sllv(dst, lhs, rhs_reg);
1646 } else if (instr->IsShr()) {
1647 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001648 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001649 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001650 } else {
1651 if (has_ins_rotr) {
1652 __ Rotrv(dst, lhs, rhs_reg);
1653 } else {
1654 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001655 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1656 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1657 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1658 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1659 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001660 __ Sllv(TMP, lhs, TMP);
1661 __ Srlv(dst, lhs, rhs_reg);
1662 __ Or(dst, dst, TMP);
1663 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001664 }
1665 }
1666 break;
1667 }
1668
1669 case Primitive::kPrimLong: {
1670 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1671 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1672 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1673 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1674 if (use_imm) {
1675 if (shift_value == 0) {
1676 codegen_->Move64(locations->Out(), locations->InAt(0));
1677 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001678 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001679 if (instr->IsShl()) {
1680 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1681 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1682 __ Sll(dst_low, lhs_low, shift_value);
1683 } else if (instr->IsShr()) {
1684 __ Srl(dst_low, lhs_low, shift_value);
1685 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1686 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001687 } else if (instr->IsUShr()) {
1688 __ Srl(dst_low, lhs_low, shift_value);
1689 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1690 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001691 } else {
1692 __ Srl(dst_low, lhs_low, shift_value);
1693 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1694 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001695 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001696 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001697 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001698 if (instr->IsShl()) {
1699 __ Sll(dst_low, lhs_low, shift_value);
1700 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1701 __ Sll(dst_high, lhs_high, shift_value);
1702 __ Or(dst_high, dst_high, TMP);
1703 } else if (instr->IsShr()) {
1704 __ Sra(dst_high, lhs_high, shift_value);
1705 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1706 __ Srl(dst_low, lhs_low, shift_value);
1707 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001708 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001709 __ Srl(dst_high, lhs_high, shift_value);
1710 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1711 __ Srl(dst_low, lhs_low, shift_value);
1712 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001713 } else {
1714 __ Srl(TMP, lhs_low, shift_value);
1715 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1716 __ Or(dst_low, dst_low, TMP);
1717 __ Srl(TMP, lhs_high, shift_value);
1718 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1719 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001720 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 }
1722 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001723 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001724 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001725 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001726 __ Move(dst_low, ZERO);
1727 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001728 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001729 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001730 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001731 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001732 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001733 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001734 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001735 // 64-bit rotation by 32 is just a swap.
1736 __ Move(dst_low, lhs_high);
1737 __ Move(dst_high, lhs_low);
1738 } else {
1739 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001740 __ Srl(dst_low, lhs_high, shift_value_high);
1741 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1742 __ Srl(dst_high, lhs_low, shift_value_high);
1743 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001744 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001745 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1746 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001747 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001748 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1749 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001750 __ Or(dst_high, dst_high, TMP);
1751 }
1752 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001753 }
1754 }
1755 } else {
1756 MipsLabel done;
1757 if (instr->IsShl()) {
1758 __ Sllv(dst_low, lhs_low, rhs_reg);
1759 __ Nor(AT, ZERO, rhs_reg);
1760 __ Srl(TMP, lhs_low, 1);
1761 __ Srlv(TMP, TMP, AT);
1762 __ Sllv(dst_high, lhs_high, rhs_reg);
1763 __ Or(dst_high, dst_high, TMP);
1764 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1765 __ Beqz(TMP, &done);
1766 __ Move(dst_high, dst_low);
1767 __ Move(dst_low, ZERO);
1768 } else if (instr->IsShr()) {
1769 __ Srav(dst_high, lhs_high, rhs_reg);
1770 __ Nor(AT, ZERO, rhs_reg);
1771 __ Sll(TMP, lhs_high, 1);
1772 __ Sllv(TMP, TMP, AT);
1773 __ Srlv(dst_low, lhs_low, rhs_reg);
1774 __ Or(dst_low, dst_low, TMP);
1775 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1776 __ Beqz(TMP, &done);
1777 __ Move(dst_low, dst_high);
1778 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001779 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001780 __ Srlv(dst_high, lhs_high, rhs_reg);
1781 __ Nor(AT, ZERO, rhs_reg);
1782 __ Sll(TMP, lhs_high, 1);
1783 __ Sllv(TMP, TMP, AT);
1784 __ Srlv(dst_low, lhs_low, rhs_reg);
1785 __ Or(dst_low, dst_low, TMP);
1786 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1787 __ Beqz(TMP, &done);
1788 __ Move(dst_low, dst_high);
1789 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001790 } else {
1791 __ Nor(AT, ZERO, rhs_reg);
1792 __ Srlv(TMP, lhs_low, rhs_reg);
1793 __ Sll(dst_low, lhs_high, 1);
1794 __ Sllv(dst_low, dst_low, AT);
1795 __ Or(dst_low, dst_low, TMP);
1796 __ Srlv(TMP, lhs_high, rhs_reg);
1797 __ Sll(dst_high, lhs_low, 1);
1798 __ Sllv(dst_high, dst_high, AT);
1799 __ Or(dst_high, dst_high, TMP);
1800 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1801 __ Beqz(TMP, &done);
1802 __ Move(TMP, dst_high);
1803 __ Move(dst_high, dst_low);
1804 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001805 }
1806 __ Bind(&done);
1807 }
1808 break;
1809 }
1810
1811 default:
1812 LOG(FATAL) << "Unexpected shift operation type " << type;
1813 }
1814}
1815
1816void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1817 HandleBinaryOp(instruction);
1818}
1819
1820void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1821 HandleBinaryOp(instruction);
1822}
1823
1824void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1825 HandleBinaryOp(instruction);
1826}
1827
1828void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1829 HandleBinaryOp(instruction);
1830}
1831
1832void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1833 LocationSummary* locations =
1834 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1835 locations->SetInAt(0, Location::RequiresRegister());
1836 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1837 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1838 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1839 } else {
1840 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1841 }
1842}
1843
Alexey Frunze2923db72016-08-20 01:55:47 -07001844auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1845 auto null_checker = [this, instruction]() {
1846 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1847 };
1848 return null_checker;
1849}
1850
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001851void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1852 LocationSummary* locations = instruction->GetLocations();
1853 Register obj = locations->InAt(0).AsRegister<Register>();
1854 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001855 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001856 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001858 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001859 switch (type) {
1860 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 Register out = locations->Out().AsRegister<Register>();
1862 if (index.IsConstant()) {
1863 size_t offset =
1864 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001865 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 } else {
1867 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001868 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001869 }
1870 break;
1871 }
1872
1873 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001874 Register out = locations->Out().AsRegister<Register>();
1875 if (index.IsConstant()) {
1876 size_t offset =
1877 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001878 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879 } else {
1880 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001881 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001882 }
1883 break;
1884 }
1885
1886 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887 Register out = locations->Out().AsRegister<Register>();
1888 if (index.IsConstant()) {
1889 size_t offset =
1890 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001891 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001892 } else {
1893 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1894 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001895 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001896 }
1897 break;
1898 }
1899
1900 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901 Register out = locations->Out().AsRegister<Register>();
1902 if (index.IsConstant()) {
1903 size_t offset =
1904 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001905 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906 } else {
1907 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1908 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001909 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910 }
1911 break;
1912 }
1913
1914 case Primitive::kPrimInt:
1915 case Primitive::kPrimNot: {
1916 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001917 Register out = locations->Out().AsRegister<Register>();
1918 if (index.IsConstant()) {
1919 size_t offset =
1920 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001921 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001922 } else {
1923 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1924 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001925 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001926 }
1927 break;
1928 }
1929
1930 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001931 Register out = locations->Out().AsRegisterPairLow<Register>();
1932 if (index.IsConstant()) {
1933 size_t offset =
1934 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001935 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001936 } else {
1937 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1938 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001939 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001940 }
1941 break;
1942 }
1943
1944 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001945 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1946 if (index.IsConstant()) {
1947 size_t offset =
1948 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001949 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001950 } else {
1951 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1952 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001953 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001954 }
1955 break;
1956 }
1957
1958 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1960 if (index.IsConstant()) {
1961 size_t offset =
1962 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001963 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001964 } else {
1965 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1966 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001967 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001968 }
1969 break;
1970 }
1971
1972 case Primitive::kPrimVoid:
1973 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1974 UNREACHABLE();
1975 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001976}
1977
1978void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1979 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1980 locations->SetInAt(0, Location::RequiresRegister());
1981 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1982}
1983
1984void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1985 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001986 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001987 Register obj = locations->InAt(0).AsRegister<Register>();
1988 Register out = locations->Out().AsRegister<Register>();
1989 __ LoadFromOffset(kLoadWord, out, obj, offset);
1990 codegen_->MaybeRecordImplicitNullCheck(instruction);
1991}
1992
Alexey Frunzef58b2482016-09-02 22:14:06 -07001993Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1994 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1995 ? Location::ConstantLocation(instruction->AsConstant())
1996 : Location::RequiresRegister();
1997}
1998
1999Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2000 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2001 // We can store a non-zero float or double constant without first loading it into the FPU,
2002 // but we should only prefer this if the constant has a single use.
2003 if (instruction->IsConstant() &&
2004 (instruction->AsConstant()->IsZeroBitPattern() ||
2005 instruction->GetUses().HasExactlyOneElement())) {
2006 return Location::ConstantLocation(instruction->AsConstant());
2007 // Otherwise fall through and require an FPU register for the constant.
2008 }
2009 return Location::RequiresFpuRegister();
2010}
2011
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002012void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002013 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2015 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002016 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002017 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002018 InvokeRuntimeCallingConvention calling_convention;
2019 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2020 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2021 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2022 } else {
2023 locations->SetInAt(0, Location::RequiresRegister());
2024 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2025 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002026 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002027 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002028 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002029 }
2030 }
2031}
2032
2033void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2034 LocationSummary* locations = instruction->GetLocations();
2035 Register obj = locations->InAt(0).AsRegister<Register>();
2036 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002037 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002038 Primitive::Type value_type = instruction->GetComponentType();
2039 bool needs_runtime_call = locations->WillCall();
2040 bool needs_write_barrier =
2041 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002042 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002043 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044
2045 switch (value_type) {
2046 case Primitive::kPrimBoolean:
2047 case Primitive::kPrimByte: {
2048 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002049 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002050 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002051 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002052 __ Addu(base_reg, obj, index.AsRegister<Register>());
2053 }
2054 if (value_location.IsConstant()) {
2055 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2056 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2057 } else {
2058 Register value = value_location.AsRegister<Register>();
2059 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002060 }
2061 break;
2062 }
2063
2064 case Primitive::kPrimShort:
2065 case Primitive::kPrimChar: {
2066 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002067 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002068 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002069 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002070 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2071 __ Addu(base_reg, obj, base_reg);
2072 }
2073 if (value_location.IsConstant()) {
2074 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2075 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2076 } else {
2077 Register value = value_location.AsRegister<Register>();
2078 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002079 }
2080 break;
2081 }
2082
2083 case Primitive::kPrimInt:
2084 case Primitive::kPrimNot: {
2085 if (!needs_runtime_call) {
2086 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002088 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002090 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2091 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002092 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002093 if (value_location.IsConstant()) {
2094 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2095 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2096 DCHECK(!needs_write_barrier);
2097 } else {
2098 Register value = value_location.AsRegister<Register>();
2099 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2100 if (needs_write_barrier) {
2101 DCHECK_EQ(value_type, Primitive::kPrimNot);
2102 codegen_->MarkGCCard(obj, value);
2103 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002104 }
2105 } else {
2106 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002107 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002108 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2109 }
2110 break;
2111 }
2112
2113 case Primitive::kPrimLong: {
2114 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002115 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002116 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002117 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002118 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2119 __ Addu(base_reg, obj, base_reg);
2120 }
2121 if (value_location.IsConstant()) {
2122 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2123 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2124 } else {
2125 Register value = value_location.AsRegisterPairLow<Register>();
2126 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002127 }
2128 break;
2129 }
2130
2131 case Primitive::kPrimFloat: {
2132 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002133 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002134 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002135 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002136 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2137 __ Addu(base_reg, obj, base_reg);
2138 }
2139 if (value_location.IsConstant()) {
2140 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2141 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2142 } else {
2143 FRegister value = value_location.AsFpuRegister<FRegister>();
2144 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002145 }
2146 break;
2147 }
2148
2149 case Primitive::kPrimDouble: {
2150 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002151 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002152 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002153 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002154 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2155 __ Addu(base_reg, obj, base_reg);
2156 }
2157 if (value_location.IsConstant()) {
2158 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2159 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2160 } else {
2161 FRegister value = value_location.AsFpuRegister<FRegister>();
2162 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002163 }
2164 break;
2165 }
2166
2167 case Primitive::kPrimVoid:
2168 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2169 UNREACHABLE();
2170 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002171}
2172
2173void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002174 RegisterSet caller_saves = RegisterSet::Empty();
2175 InvokeRuntimeCallingConvention calling_convention;
2176 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2177 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2178 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002179 locations->SetInAt(0, Location::RequiresRegister());
2180 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002181}
2182
2183void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2184 LocationSummary* locations = instruction->GetLocations();
2185 BoundsCheckSlowPathMIPS* slow_path =
2186 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2187 codegen_->AddSlowPath(slow_path);
2188
2189 Register index = locations->InAt(0).AsRegister<Register>();
2190 Register length = locations->InAt(1).AsRegister<Register>();
2191
2192 // length is limited by the maximum positive signed 32-bit integer.
2193 // Unsigned comparison of length and index checks for index < 0
2194 // and for length <= index simultaneously.
2195 __ Bgeu(index, length, slow_path->GetEntryLabel());
2196}
2197
2198void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2199 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2200 instruction,
2201 LocationSummary::kCallOnSlowPath);
2202 locations->SetInAt(0, Location::RequiresRegister());
2203 locations->SetInAt(1, Location::RequiresRegister());
2204 // Note that TypeCheckSlowPathMIPS uses this register too.
2205 locations->AddTemp(Location::RequiresRegister());
2206}
2207
2208void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2209 LocationSummary* locations = instruction->GetLocations();
2210 Register obj = locations->InAt(0).AsRegister<Register>();
2211 Register cls = locations->InAt(1).AsRegister<Register>();
2212 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2213
2214 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2215 codegen_->AddSlowPath(slow_path);
2216
2217 // TODO: avoid this check if we know obj is not null.
2218 __ Beqz(obj, slow_path->GetExitLabel());
2219 // Compare the class of `obj` with `cls`.
2220 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2221 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2222 __ Bind(slow_path->GetExitLabel());
2223}
2224
2225void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2226 LocationSummary* locations =
2227 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2228 locations->SetInAt(0, Location::RequiresRegister());
2229 if (check->HasUses()) {
2230 locations->SetOut(Location::SameAsFirstInput());
2231 }
2232}
2233
2234void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2235 // We assume the class is not null.
2236 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2237 check->GetLoadClass(),
2238 check,
2239 check->GetDexPc(),
2240 true);
2241 codegen_->AddSlowPath(slow_path);
2242 GenerateClassInitializationCheck(slow_path,
2243 check->GetLocations()->InAt(0).AsRegister<Register>());
2244}
2245
2246void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2247 Primitive::Type in_type = compare->InputAt(0)->GetType();
2248
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002249 LocationSummary* locations =
2250 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002251
2252 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002253 case Primitive::kPrimBoolean:
2254 case Primitive::kPrimByte:
2255 case Primitive::kPrimShort:
2256 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002257 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002258 case Primitive::kPrimLong:
2259 locations->SetInAt(0, Location::RequiresRegister());
2260 locations->SetInAt(1, Location::RequiresRegister());
2261 // Output overlaps because it is written before doing the low comparison.
2262 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2263 break;
2264
2265 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002266 case Primitive::kPrimDouble:
2267 locations->SetInAt(0, Location::RequiresFpuRegister());
2268 locations->SetInAt(1, Location::RequiresFpuRegister());
2269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002270 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271
2272 default:
2273 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2274 }
2275}
2276
2277void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2278 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002279 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002280 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002281 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282
2283 // 0 if: left == right
2284 // 1 if: left > right
2285 // -1 if: left < right
2286 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002287 case Primitive::kPrimBoolean:
2288 case Primitive::kPrimByte:
2289 case Primitive::kPrimShort:
2290 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002291 case Primitive::kPrimInt: {
2292 Register lhs = locations->InAt(0).AsRegister<Register>();
2293 Register rhs = locations->InAt(1).AsRegister<Register>();
2294 __ Slt(TMP, lhs, rhs);
2295 __ Slt(res, rhs, lhs);
2296 __ Subu(res, res, TMP);
2297 break;
2298 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002299 case Primitive::kPrimLong: {
2300 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2302 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2303 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2304 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2305 // TODO: more efficient (direct) comparison with a constant.
2306 __ Slt(TMP, lhs_high, rhs_high);
2307 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2308 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2309 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2310 __ Sltu(TMP, lhs_low, rhs_low);
2311 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2312 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2313 __ Bind(&done);
2314 break;
2315 }
2316
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002317 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002318 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002319 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2320 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2321 MipsLabel done;
2322 if (isR6) {
2323 __ CmpEqS(FTMP, lhs, rhs);
2324 __ LoadConst32(res, 0);
2325 __ Bc1nez(FTMP, &done);
2326 if (gt_bias) {
2327 __ CmpLtS(FTMP, lhs, rhs);
2328 __ LoadConst32(res, -1);
2329 __ Bc1nez(FTMP, &done);
2330 __ LoadConst32(res, 1);
2331 } else {
2332 __ CmpLtS(FTMP, rhs, lhs);
2333 __ LoadConst32(res, 1);
2334 __ Bc1nez(FTMP, &done);
2335 __ LoadConst32(res, -1);
2336 }
2337 } else {
2338 if (gt_bias) {
2339 __ ColtS(0, lhs, rhs);
2340 __ LoadConst32(res, -1);
2341 __ Bc1t(0, &done);
2342 __ CeqS(0, lhs, rhs);
2343 __ LoadConst32(res, 1);
2344 __ Movt(res, ZERO, 0);
2345 } else {
2346 __ ColtS(0, rhs, lhs);
2347 __ LoadConst32(res, 1);
2348 __ Bc1t(0, &done);
2349 __ CeqS(0, lhs, rhs);
2350 __ LoadConst32(res, -1);
2351 __ Movt(res, ZERO, 0);
2352 }
2353 }
2354 __ Bind(&done);
2355 break;
2356 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002357 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002358 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002359 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2360 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2361 MipsLabel done;
2362 if (isR6) {
2363 __ CmpEqD(FTMP, lhs, rhs);
2364 __ LoadConst32(res, 0);
2365 __ Bc1nez(FTMP, &done);
2366 if (gt_bias) {
2367 __ CmpLtD(FTMP, lhs, rhs);
2368 __ LoadConst32(res, -1);
2369 __ Bc1nez(FTMP, &done);
2370 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002371 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002372 __ CmpLtD(FTMP, rhs, lhs);
2373 __ LoadConst32(res, 1);
2374 __ Bc1nez(FTMP, &done);
2375 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 }
2377 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002378 if (gt_bias) {
2379 __ ColtD(0, lhs, rhs);
2380 __ LoadConst32(res, -1);
2381 __ Bc1t(0, &done);
2382 __ CeqD(0, lhs, rhs);
2383 __ LoadConst32(res, 1);
2384 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002386 __ ColtD(0, rhs, lhs);
2387 __ LoadConst32(res, 1);
2388 __ Bc1t(0, &done);
2389 __ CeqD(0, lhs, rhs);
2390 __ LoadConst32(res, -1);
2391 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002392 }
2393 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002394 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002395 break;
2396 }
2397
2398 default:
2399 LOG(FATAL) << "Unimplemented compare type " << in_type;
2400 }
2401}
2402
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002403void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002404 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002405 switch (instruction->InputAt(0)->GetType()) {
2406 default:
2407 case Primitive::kPrimLong:
2408 locations->SetInAt(0, Location::RequiresRegister());
2409 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2410 break;
2411
2412 case Primitive::kPrimFloat:
2413 case Primitive::kPrimDouble:
2414 locations->SetInAt(0, Location::RequiresFpuRegister());
2415 locations->SetInAt(1, Location::RequiresFpuRegister());
2416 break;
2417 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002418 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2420 }
2421}
2422
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002423void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002424 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002425 return;
2426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002428 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429 LocationSummary* locations = instruction->GetLocations();
2430 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002431 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002433 switch (type) {
2434 default:
2435 // Integer case.
2436 GenerateIntCompare(instruction->GetCondition(), locations);
2437 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002439 case Primitive::kPrimLong:
2440 // TODO: don't use branches.
2441 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 break;
2443
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002444 case Primitive::kPrimFloat:
2445 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002446 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2447 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002448 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002449
2450 // Convert the branches into the result.
2451 MipsLabel done;
2452
2453 // False case: result = 0.
2454 __ LoadConst32(dst, 0);
2455 __ B(&done);
2456
2457 // True case: result = 1.
2458 __ Bind(&true_label);
2459 __ LoadConst32(dst, 1);
2460 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002461}
2462
Alexey Frunze7e99e052015-11-24 19:28:01 -08002463void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2464 DCHECK(instruction->IsDiv() || instruction->IsRem());
2465 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2466
2467 LocationSummary* locations = instruction->GetLocations();
2468 Location second = locations->InAt(1);
2469 DCHECK(second.IsConstant());
2470
2471 Register out = locations->Out().AsRegister<Register>();
2472 Register dividend = locations->InAt(0).AsRegister<Register>();
2473 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2474 DCHECK(imm == 1 || imm == -1);
2475
2476 if (instruction->IsRem()) {
2477 __ Move(out, ZERO);
2478 } else {
2479 if (imm == -1) {
2480 __ Subu(out, ZERO, dividend);
2481 } else if (out != dividend) {
2482 __ Move(out, dividend);
2483 }
2484 }
2485}
2486
2487void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2488 DCHECK(instruction->IsDiv() || instruction->IsRem());
2489 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2490
2491 LocationSummary* locations = instruction->GetLocations();
2492 Location second = locations->InAt(1);
2493 DCHECK(second.IsConstant());
2494
2495 Register out = locations->Out().AsRegister<Register>();
2496 Register dividend = locations->InAt(0).AsRegister<Register>();
2497 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002498 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002499 int ctz_imm = CTZ(abs_imm);
2500
2501 if (instruction->IsDiv()) {
2502 if (ctz_imm == 1) {
2503 // Fast path for division by +/-2, which is very common.
2504 __ Srl(TMP, dividend, 31);
2505 } else {
2506 __ Sra(TMP, dividend, 31);
2507 __ Srl(TMP, TMP, 32 - ctz_imm);
2508 }
2509 __ Addu(out, dividend, TMP);
2510 __ Sra(out, out, ctz_imm);
2511 if (imm < 0) {
2512 __ Subu(out, ZERO, out);
2513 }
2514 } else {
2515 if (ctz_imm == 1) {
2516 // Fast path for modulo +/-2, which is very common.
2517 __ Sra(TMP, dividend, 31);
2518 __ Subu(out, dividend, TMP);
2519 __ Andi(out, out, 1);
2520 __ Addu(out, out, TMP);
2521 } else {
2522 __ Sra(TMP, dividend, 31);
2523 __ Srl(TMP, TMP, 32 - ctz_imm);
2524 __ Addu(out, dividend, TMP);
2525 if (IsUint<16>(abs_imm - 1)) {
2526 __ Andi(out, out, abs_imm - 1);
2527 } else {
2528 __ Sll(out, out, 32 - ctz_imm);
2529 __ Srl(out, out, 32 - ctz_imm);
2530 }
2531 __ Subu(out, out, TMP);
2532 }
2533 }
2534}
2535
2536void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2537 DCHECK(instruction->IsDiv() || instruction->IsRem());
2538 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2539
2540 LocationSummary* locations = instruction->GetLocations();
2541 Location second = locations->InAt(1);
2542 DCHECK(second.IsConstant());
2543
2544 Register out = locations->Out().AsRegister<Register>();
2545 Register dividend = locations->InAt(0).AsRegister<Register>();
2546 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2547
2548 int64_t magic;
2549 int shift;
2550 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2551
2552 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2553
2554 __ LoadConst32(TMP, magic);
2555 if (isR6) {
2556 __ MuhR6(TMP, dividend, TMP);
2557 } else {
2558 __ MultR2(dividend, TMP);
2559 __ Mfhi(TMP);
2560 }
2561 if (imm > 0 && magic < 0) {
2562 __ Addu(TMP, TMP, dividend);
2563 } else if (imm < 0 && magic > 0) {
2564 __ Subu(TMP, TMP, dividend);
2565 }
2566
2567 if (shift != 0) {
2568 __ Sra(TMP, TMP, shift);
2569 }
2570
2571 if (instruction->IsDiv()) {
2572 __ Sra(out, TMP, 31);
2573 __ Subu(out, TMP, out);
2574 } else {
2575 __ Sra(AT, TMP, 31);
2576 __ Subu(AT, TMP, AT);
2577 __ LoadConst32(TMP, imm);
2578 if (isR6) {
2579 __ MulR6(TMP, AT, TMP);
2580 } else {
2581 __ MulR2(TMP, AT, TMP);
2582 }
2583 __ Subu(out, dividend, TMP);
2584 }
2585}
2586
2587void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2588 DCHECK(instruction->IsDiv() || instruction->IsRem());
2589 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2590
2591 LocationSummary* locations = instruction->GetLocations();
2592 Register out = locations->Out().AsRegister<Register>();
2593 Location second = locations->InAt(1);
2594
2595 if (second.IsConstant()) {
2596 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2597 if (imm == 0) {
2598 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2599 } else if (imm == 1 || imm == -1) {
2600 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002601 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002602 DivRemByPowerOfTwo(instruction);
2603 } else {
2604 DCHECK(imm <= -2 || imm >= 2);
2605 GenerateDivRemWithAnyConstant(instruction);
2606 }
2607 } else {
2608 Register dividend = locations->InAt(0).AsRegister<Register>();
2609 Register divisor = second.AsRegister<Register>();
2610 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2611 if (instruction->IsDiv()) {
2612 if (isR6) {
2613 __ DivR6(out, dividend, divisor);
2614 } else {
2615 __ DivR2(out, dividend, divisor);
2616 }
2617 } else {
2618 if (isR6) {
2619 __ ModR6(out, dividend, divisor);
2620 } else {
2621 __ ModR2(out, dividend, divisor);
2622 }
2623 }
2624 }
2625}
2626
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002627void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2628 Primitive::Type type = div->GetResultType();
2629 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002630 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002631 : LocationSummary::kNoCall;
2632
2633 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2634
2635 switch (type) {
2636 case Primitive::kPrimInt:
2637 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002638 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002639 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2640 break;
2641
2642 case Primitive::kPrimLong: {
2643 InvokeRuntimeCallingConvention calling_convention;
2644 locations->SetInAt(0, Location::RegisterPairLocation(
2645 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2646 locations->SetInAt(1, Location::RegisterPairLocation(
2647 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2648 locations->SetOut(calling_convention.GetReturnLocation(type));
2649 break;
2650 }
2651
2652 case Primitive::kPrimFloat:
2653 case Primitive::kPrimDouble:
2654 locations->SetInAt(0, Location::RequiresFpuRegister());
2655 locations->SetInAt(1, Location::RequiresFpuRegister());
2656 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2657 break;
2658
2659 default:
2660 LOG(FATAL) << "Unexpected div type " << type;
2661 }
2662}
2663
2664void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2665 Primitive::Type type = instruction->GetType();
2666 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002667
2668 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002669 case Primitive::kPrimInt:
2670 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002672 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002673 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002674 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2675 break;
2676 }
2677 case Primitive::kPrimFloat:
2678 case Primitive::kPrimDouble: {
2679 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2680 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2681 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2682 if (type == Primitive::kPrimFloat) {
2683 __ DivS(dst, lhs, rhs);
2684 } else {
2685 __ DivD(dst, lhs, rhs);
2686 }
2687 break;
2688 }
2689 default:
2690 LOG(FATAL) << "Unexpected div type " << type;
2691 }
2692}
2693
2694void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002695 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002697}
2698
2699void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2700 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2701 codegen_->AddSlowPath(slow_path);
2702 Location value = instruction->GetLocations()->InAt(0);
2703 Primitive::Type type = instruction->GetType();
2704
2705 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002706 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707 case Primitive::kPrimByte:
2708 case Primitive::kPrimChar:
2709 case Primitive::kPrimShort:
2710 case Primitive::kPrimInt: {
2711 if (value.IsConstant()) {
2712 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2713 __ B(slow_path->GetEntryLabel());
2714 } else {
2715 // A division by a non-null constant is valid. We don't need to perform
2716 // any check, so simply fall through.
2717 }
2718 } else {
2719 DCHECK(value.IsRegister()) << value;
2720 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2721 }
2722 break;
2723 }
2724 case Primitive::kPrimLong: {
2725 if (value.IsConstant()) {
2726 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2727 __ B(slow_path->GetEntryLabel());
2728 } else {
2729 // A division by a non-null constant is valid. We don't need to perform
2730 // any check, so simply fall through.
2731 }
2732 } else {
2733 DCHECK(value.IsRegisterPair()) << value;
2734 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2735 __ Beqz(TMP, slow_path->GetEntryLabel());
2736 }
2737 break;
2738 }
2739 default:
2740 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2741 }
2742}
2743
2744void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2745 LocationSummary* locations =
2746 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2747 locations->SetOut(Location::ConstantLocation(constant));
2748}
2749
2750void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2751 // Will be generated at use site.
2752}
2753
2754void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2755 exit->SetLocations(nullptr);
2756}
2757
2758void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2759}
2760
2761void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2762 LocationSummary* locations =
2763 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2764 locations->SetOut(Location::ConstantLocation(constant));
2765}
2766
2767void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2768 // Will be generated at use site.
2769}
2770
2771void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2772 got->SetLocations(nullptr);
2773}
2774
2775void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2776 DCHECK(!successor->IsExitBlock());
2777 HBasicBlock* block = got->GetBlock();
2778 HInstruction* previous = got->GetPrevious();
2779 HLoopInformation* info = block->GetLoopInformation();
2780
2781 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2782 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2783 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2784 return;
2785 }
2786 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2787 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2788 }
2789 if (!codegen_->GoesToNextBlock(block, successor)) {
2790 __ B(codegen_->GetLabelOf(successor));
2791 }
2792}
2793
2794void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2795 HandleGoto(got, got->GetSuccessor());
2796}
2797
2798void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2799 try_boundary->SetLocations(nullptr);
2800}
2801
2802void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2803 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2804 if (!successor->IsExitBlock()) {
2805 HandleGoto(try_boundary, successor);
2806 }
2807}
2808
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002809void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2810 LocationSummary* locations) {
2811 Register dst = locations->Out().AsRegister<Register>();
2812 Register lhs = locations->InAt(0).AsRegister<Register>();
2813 Location rhs_location = locations->InAt(1);
2814 Register rhs_reg = ZERO;
2815 int64_t rhs_imm = 0;
2816 bool use_imm = rhs_location.IsConstant();
2817 if (use_imm) {
2818 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2819 } else {
2820 rhs_reg = rhs_location.AsRegister<Register>();
2821 }
2822
2823 switch (cond) {
2824 case kCondEQ:
2825 case kCondNE:
2826 if (use_imm && IsUint<16>(rhs_imm)) {
2827 __ Xori(dst, lhs, rhs_imm);
2828 } else {
2829 if (use_imm) {
2830 rhs_reg = TMP;
2831 __ LoadConst32(rhs_reg, rhs_imm);
2832 }
2833 __ Xor(dst, lhs, rhs_reg);
2834 }
2835 if (cond == kCondEQ) {
2836 __ Sltiu(dst, dst, 1);
2837 } else {
2838 __ Sltu(dst, ZERO, dst);
2839 }
2840 break;
2841
2842 case kCondLT:
2843 case kCondGE:
2844 if (use_imm && IsInt<16>(rhs_imm)) {
2845 __ Slti(dst, lhs, rhs_imm);
2846 } else {
2847 if (use_imm) {
2848 rhs_reg = TMP;
2849 __ LoadConst32(rhs_reg, rhs_imm);
2850 }
2851 __ Slt(dst, lhs, rhs_reg);
2852 }
2853 if (cond == kCondGE) {
2854 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2855 // only the slt instruction but no sge.
2856 __ Xori(dst, dst, 1);
2857 }
2858 break;
2859
2860 case kCondLE:
2861 case kCondGT:
2862 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2863 // Simulate lhs <= rhs via lhs < rhs + 1.
2864 __ Slti(dst, lhs, rhs_imm + 1);
2865 if (cond == kCondGT) {
2866 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2867 // only the slti instruction but no sgti.
2868 __ Xori(dst, dst, 1);
2869 }
2870 } else {
2871 if (use_imm) {
2872 rhs_reg = TMP;
2873 __ LoadConst32(rhs_reg, rhs_imm);
2874 }
2875 __ Slt(dst, rhs_reg, lhs);
2876 if (cond == kCondLE) {
2877 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2878 // only the slt instruction but no sle.
2879 __ Xori(dst, dst, 1);
2880 }
2881 }
2882 break;
2883
2884 case kCondB:
2885 case kCondAE:
2886 if (use_imm && IsInt<16>(rhs_imm)) {
2887 // Sltiu sign-extends its 16-bit immediate operand before
2888 // the comparison and thus lets us compare directly with
2889 // unsigned values in the ranges [0, 0x7fff] and
2890 // [0xffff8000, 0xffffffff].
2891 __ Sltiu(dst, lhs, rhs_imm);
2892 } else {
2893 if (use_imm) {
2894 rhs_reg = TMP;
2895 __ LoadConst32(rhs_reg, rhs_imm);
2896 }
2897 __ Sltu(dst, lhs, rhs_reg);
2898 }
2899 if (cond == kCondAE) {
2900 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2901 // only the sltu instruction but no sgeu.
2902 __ Xori(dst, dst, 1);
2903 }
2904 break;
2905
2906 case kCondBE:
2907 case kCondA:
2908 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2909 // Simulate lhs <= rhs via lhs < rhs + 1.
2910 // Note that this only works if rhs + 1 does not overflow
2911 // to 0, hence the check above.
2912 // Sltiu sign-extends its 16-bit immediate operand before
2913 // the comparison and thus lets us compare directly with
2914 // unsigned values in the ranges [0, 0x7fff] and
2915 // [0xffff8000, 0xffffffff].
2916 __ Sltiu(dst, lhs, rhs_imm + 1);
2917 if (cond == kCondA) {
2918 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2919 // only the sltiu instruction but no sgtiu.
2920 __ Xori(dst, dst, 1);
2921 }
2922 } else {
2923 if (use_imm) {
2924 rhs_reg = TMP;
2925 __ LoadConst32(rhs_reg, rhs_imm);
2926 }
2927 __ Sltu(dst, rhs_reg, lhs);
2928 if (cond == kCondBE) {
2929 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2930 // only the sltu instruction but no sleu.
2931 __ Xori(dst, dst, 1);
2932 }
2933 }
2934 break;
2935 }
2936}
2937
2938void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2939 LocationSummary* locations,
2940 MipsLabel* label) {
2941 Register lhs = locations->InAt(0).AsRegister<Register>();
2942 Location rhs_location = locations->InAt(1);
2943 Register rhs_reg = ZERO;
2944 int32_t rhs_imm = 0;
2945 bool use_imm = rhs_location.IsConstant();
2946 if (use_imm) {
2947 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2948 } else {
2949 rhs_reg = rhs_location.AsRegister<Register>();
2950 }
2951
2952 if (use_imm && rhs_imm == 0) {
2953 switch (cond) {
2954 case kCondEQ:
2955 case kCondBE: // <= 0 if zero
2956 __ Beqz(lhs, label);
2957 break;
2958 case kCondNE:
2959 case kCondA: // > 0 if non-zero
2960 __ Bnez(lhs, label);
2961 break;
2962 case kCondLT:
2963 __ Bltz(lhs, label);
2964 break;
2965 case kCondGE:
2966 __ Bgez(lhs, label);
2967 break;
2968 case kCondLE:
2969 __ Blez(lhs, label);
2970 break;
2971 case kCondGT:
2972 __ Bgtz(lhs, label);
2973 break;
2974 case kCondB: // always false
2975 break;
2976 case kCondAE: // always true
2977 __ B(label);
2978 break;
2979 }
2980 } else {
2981 if (use_imm) {
2982 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2983 rhs_reg = TMP;
2984 __ LoadConst32(rhs_reg, rhs_imm);
2985 }
2986 switch (cond) {
2987 case kCondEQ:
2988 __ Beq(lhs, rhs_reg, label);
2989 break;
2990 case kCondNE:
2991 __ Bne(lhs, rhs_reg, label);
2992 break;
2993 case kCondLT:
2994 __ Blt(lhs, rhs_reg, label);
2995 break;
2996 case kCondGE:
2997 __ Bge(lhs, rhs_reg, label);
2998 break;
2999 case kCondLE:
3000 __ Bge(rhs_reg, lhs, label);
3001 break;
3002 case kCondGT:
3003 __ Blt(rhs_reg, lhs, label);
3004 break;
3005 case kCondB:
3006 __ Bltu(lhs, rhs_reg, label);
3007 break;
3008 case kCondAE:
3009 __ Bgeu(lhs, rhs_reg, label);
3010 break;
3011 case kCondBE:
3012 __ Bgeu(rhs_reg, lhs, label);
3013 break;
3014 case kCondA:
3015 __ Bltu(rhs_reg, lhs, label);
3016 break;
3017 }
3018 }
3019}
3020
3021void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3022 LocationSummary* locations,
3023 MipsLabel* label) {
3024 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3025 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3026 Location rhs_location = locations->InAt(1);
3027 Register rhs_high = ZERO;
3028 Register rhs_low = ZERO;
3029 int64_t imm = 0;
3030 uint32_t imm_high = 0;
3031 uint32_t imm_low = 0;
3032 bool use_imm = rhs_location.IsConstant();
3033 if (use_imm) {
3034 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3035 imm_high = High32Bits(imm);
3036 imm_low = Low32Bits(imm);
3037 } else {
3038 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3039 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3040 }
3041
3042 if (use_imm && imm == 0) {
3043 switch (cond) {
3044 case kCondEQ:
3045 case kCondBE: // <= 0 if zero
3046 __ Or(TMP, lhs_high, lhs_low);
3047 __ Beqz(TMP, label);
3048 break;
3049 case kCondNE:
3050 case kCondA: // > 0 if non-zero
3051 __ Or(TMP, lhs_high, lhs_low);
3052 __ Bnez(TMP, label);
3053 break;
3054 case kCondLT:
3055 __ Bltz(lhs_high, label);
3056 break;
3057 case kCondGE:
3058 __ Bgez(lhs_high, label);
3059 break;
3060 case kCondLE:
3061 __ Or(TMP, lhs_high, lhs_low);
3062 __ Sra(AT, lhs_high, 31);
3063 __ Bgeu(AT, TMP, label);
3064 break;
3065 case kCondGT:
3066 __ Or(TMP, lhs_high, lhs_low);
3067 __ Sra(AT, lhs_high, 31);
3068 __ Bltu(AT, TMP, label);
3069 break;
3070 case kCondB: // always false
3071 break;
3072 case kCondAE: // always true
3073 __ B(label);
3074 break;
3075 }
3076 } else if (use_imm) {
3077 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3078 switch (cond) {
3079 case kCondEQ:
3080 __ LoadConst32(TMP, imm_high);
3081 __ Xor(TMP, TMP, lhs_high);
3082 __ LoadConst32(AT, imm_low);
3083 __ Xor(AT, AT, lhs_low);
3084 __ Or(TMP, TMP, AT);
3085 __ Beqz(TMP, label);
3086 break;
3087 case kCondNE:
3088 __ LoadConst32(TMP, imm_high);
3089 __ Xor(TMP, TMP, lhs_high);
3090 __ LoadConst32(AT, imm_low);
3091 __ Xor(AT, AT, lhs_low);
3092 __ Or(TMP, TMP, AT);
3093 __ Bnez(TMP, label);
3094 break;
3095 case kCondLT:
3096 __ LoadConst32(TMP, imm_high);
3097 __ Blt(lhs_high, TMP, label);
3098 __ Slt(TMP, TMP, lhs_high);
3099 __ LoadConst32(AT, imm_low);
3100 __ Sltu(AT, lhs_low, AT);
3101 __ Blt(TMP, AT, label);
3102 break;
3103 case kCondGE:
3104 __ LoadConst32(TMP, imm_high);
3105 __ Blt(TMP, lhs_high, label);
3106 __ Slt(TMP, lhs_high, TMP);
3107 __ LoadConst32(AT, imm_low);
3108 __ Sltu(AT, lhs_low, AT);
3109 __ Or(TMP, TMP, AT);
3110 __ Beqz(TMP, label);
3111 break;
3112 case kCondLE:
3113 __ LoadConst32(TMP, imm_high);
3114 __ Blt(lhs_high, TMP, label);
3115 __ Slt(TMP, TMP, lhs_high);
3116 __ LoadConst32(AT, imm_low);
3117 __ Sltu(AT, AT, lhs_low);
3118 __ Or(TMP, TMP, AT);
3119 __ Beqz(TMP, label);
3120 break;
3121 case kCondGT:
3122 __ LoadConst32(TMP, imm_high);
3123 __ Blt(TMP, lhs_high, label);
3124 __ Slt(TMP, lhs_high, TMP);
3125 __ LoadConst32(AT, imm_low);
3126 __ Sltu(AT, AT, lhs_low);
3127 __ Blt(TMP, AT, label);
3128 break;
3129 case kCondB:
3130 __ LoadConst32(TMP, imm_high);
3131 __ Bltu(lhs_high, TMP, label);
3132 __ Sltu(TMP, TMP, lhs_high);
3133 __ LoadConst32(AT, imm_low);
3134 __ Sltu(AT, lhs_low, AT);
3135 __ Blt(TMP, AT, label);
3136 break;
3137 case kCondAE:
3138 __ LoadConst32(TMP, imm_high);
3139 __ Bltu(TMP, lhs_high, label);
3140 __ Sltu(TMP, lhs_high, TMP);
3141 __ LoadConst32(AT, imm_low);
3142 __ Sltu(AT, lhs_low, AT);
3143 __ Or(TMP, TMP, AT);
3144 __ Beqz(TMP, label);
3145 break;
3146 case kCondBE:
3147 __ LoadConst32(TMP, imm_high);
3148 __ Bltu(lhs_high, TMP, label);
3149 __ Sltu(TMP, TMP, lhs_high);
3150 __ LoadConst32(AT, imm_low);
3151 __ Sltu(AT, AT, lhs_low);
3152 __ Or(TMP, TMP, AT);
3153 __ Beqz(TMP, label);
3154 break;
3155 case kCondA:
3156 __ LoadConst32(TMP, imm_high);
3157 __ Bltu(TMP, lhs_high, label);
3158 __ Sltu(TMP, lhs_high, TMP);
3159 __ LoadConst32(AT, imm_low);
3160 __ Sltu(AT, AT, lhs_low);
3161 __ Blt(TMP, AT, label);
3162 break;
3163 }
3164 } else {
3165 switch (cond) {
3166 case kCondEQ:
3167 __ Xor(TMP, lhs_high, rhs_high);
3168 __ Xor(AT, lhs_low, rhs_low);
3169 __ Or(TMP, TMP, AT);
3170 __ Beqz(TMP, label);
3171 break;
3172 case kCondNE:
3173 __ Xor(TMP, lhs_high, rhs_high);
3174 __ Xor(AT, lhs_low, rhs_low);
3175 __ Or(TMP, TMP, AT);
3176 __ Bnez(TMP, label);
3177 break;
3178 case kCondLT:
3179 __ Blt(lhs_high, rhs_high, label);
3180 __ Slt(TMP, rhs_high, lhs_high);
3181 __ Sltu(AT, lhs_low, rhs_low);
3182 __ Blt(TMP, AT, label);
3183 break;
3184 case kCondGE:
3185 __ Blt(rhs_high, lhs_high, label);
3186 __ Slt(TMP, lhs_high, rhs_high);
3187 __ Sltu(AT, lhs_low, rhs_low);
3188 __ Or(TMP, TMP, AT);
3189 __ Beqz(TMP, label);
3190 break;
3191 case kCondLE:
3192 __ Blt(lhs_high, rhs_high, label);
3193 __ Slt(TMP, rhs_high, lhs_high);
3194 __ Sltu(AT, rhs_low, lhs_low);
3195 __ Or(TMP, TMP, AT);
3196 __ Beqz(TMP, label);
3197 break;
3198 case kCondGT:
3199 __ Blt(rhs_high, lhs_high, label);
3200 __ Slt(TMP, lhs_high, rhs_high);
3201 __ Sltu(AT, rhs_low, lhs_low);
3202 __ Blt(TMP, AT, label);
3203 break;
3204 case kCondB:
3205 __ Bltu(lhs_high, rhs_high, label);
3206 __ Sltu(TMP, rhs_high, lhs_high);
3207 __ Sltu(AT, lhs_low, rhs_low);
3208 __ Blt(TMP, AT, label);
3209 break;
3210 case kCondAE:
3211 __ Bltu(rhs_high, lhs_high, label);
3212 __ Sltu(TMP, lhs_high, rhs_high);
3213 __ Sltu(AT, lhs_low, rhs_low);
3214 __ Or(TMP, TMP, AT);
3215 __ Beqz(TMP, label);
3216 break;
3217 case kCondBE:
3218 __ Bltu(lhs_high, rhs_high, label);
3219 __ Sltu(TMP, rhs_high, lhs_high);
3220 __ Sltu(AT, rhs_low, lhs_low);
3221 __ Or(TMP, TMP, AT);
3222 __ Beqz(TMP, label);
3223 break;
3224 case kCondA:
3225 __ Bltu(rhs_high, lhs_high, label);
3226 __ Sltu(TMP, lhs_high, rhs_high);
3227 __ Sltu(AT, rhs_low, lhs_low);
3228 __ Blt(TMP, AT, label);
3229 break;
3230 }
3231 }
3232}
3233
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003234void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3235 bool gt_bias,
3236 Primitive::Type type,
3237 LocationSummary* locations) {
3238 Register dst = locations->Out().AsRegister<Register>();
3239 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3240 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3241 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3242 if (type == Primitive::kPrimFloat) {
3243 if (isR6) {
3244 switch (cond) {
3245 case kCondEQ:
3246 __ CmpEqS(FTMP, lhs, rhs);
3247 __ Mfc1(dst, FTMP);
3248 __ Andi(dst, dst, 1);
3249 break;
3250 case kCondNE:
3251 __ CmpEqS(FTMP, lhs, rhs);
3252 __ Mfc1(dst, FTMP);
3253 __ Addiu(dst, dst, 1);
3254 break;
3255 case kCondLT:
3256 if (gt_bias) {
3257 __ CmpLtS(FTMP, lhs, rhs);
3258 } else {
3259 __ CmpUltS(FTMP, lhs, rhs);
3260 }
3261 __ Mfc1(dst, FTMP);
3262 __ Andi(dst, dst, 1);
3263 break;
3264 case kCondLE:
3265 if (gt_bias) {
3266 __ CmpLeS(FTMP, lhs, rhs);
3267 } else {
3268 __ CmpUleS(FTMP, lhs, rhs);
3269 }
3270 __ Mfc1(dst, FTMP);
3271 __ Andi(dst, dst, 1);
3272 break;
3273 case kCondGT:
3274 if (gt_bias) {
3275 __ CmpUltS(FTMP, rhs, lhs);
3276 } else {
3277 __ CmpLtS(FTMP, rhs, lhs);
3278 }
3279 __ Mfc1(dst, FTMP);
3280 __ Andi(dst, dst, 1);
3281 break;
3282 case kCondGE:
3283 if (gt_bias) {
3284 __ CmpUleS(FTMP, rhs, lhs);
3285 } else {
3286 __ CmpLeS(FTMP, rhs, lhs);
3287 }
3288 __ Mfc1(dst, FTMP);
3289 __ Andi(dst, dst, 1);
3290 break;
3291 default:
3292 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3293 UNREACHABLE();
3294 }
3295 } else {
3296 switch (cond) {
3297 case kCondEQ:
3298 __ CeqS(0, lhs, rhs);
3299 __ LoadConst32(dst, 1);
3300 __ Movf(dst, ZERO, 0);
3301 break;
3302 case kCondNE:
3303 __ CeqS(0, lhs, rhs);
3304 __ LoadConst32(dst, 1);
3305 __ Movt(dst, ZERO, 0);
3306 break;
3307 case kCondLT:
3308 if (gt_bias) {
3309 __ ColtS(0, lhs, rhs);
3310 } else {
3311 __ CultS(0, lhs, rhs);
3312 }
3313 __ LoadConst32(dst, 1);
3314 __ Movf(dst, ZERO, 0);
3315 break;
3316 case kCondLE:
3317 if (gt_bias) {
3318 __ ColeS(0, lhs, rhs);
3319 } else {
3320 __ CuleS(0, lhs, rhs);
3321 }
3322 __ LoadConst32(dst, 1);
3323 __ Movf(dst, ZERO, 0);
3324 break;
3325 case kCondGT:
3326 if (gt_bias) {
3327 __ CultS(0, rhs, lhs);
3328 } else {
3329 __ ColtS(0, rhs, lhs);
3330 }
3331 __ LoadConst32(dst, 1);
3332 __ Movf(dst, ZERO, 0);
3333 break;
3334 case kCondGE:
3335 if (gt_bias) {
3336 __ CuleS(0, rhs, lhs);
3337 } else {
3338 __ ColeS(0, rhs, lhs);
3339 }
3340 __ LoadConst32(dst, 1);
3341 __ Movf(dst, ZERO, 0);
3342 break;
3343 default:
3344 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3345 UNREACHABLE();
3346 }
3347 }
3348 } else {
3349 DCHECK_EQ(type, Primitive::kPrimDouble);
3350 if (isR6) {
3351 switch (cond) {
3352 case kCondEQ:
3353 __ CmpEqD(FTMP, lhs, rhs);
3354 __ Mfc1(dst, FTMP);
3355 __ Andi(dst, dst, 1);
3356 break;
3357 case kCondNE:
3358 __ CmpEqD(FTMP, lhs, rhs);
3359 __ Mfc1(dst, FTMP);
3360 __ Addiu(dst, dst, 1);
3361 break;
3362 case kCondLT:
3363 if (gt_bias) {
3364 __ CmpLtD(FTMP, lhs, rhs);
3365 } else {
3366 __ CmpUltD(FTMP, lhs, rhs);
3367 }
3368 __ Mfc1(dst, FTMP);
3369 __ Andi(dst, dst, 1);
3370 break;
3371 case kCondLE:
3372 if (gt_bias) {
3373 __ CmpLeD(FTMP, lhs, rhs);
3374 } else {
3375 __ CmpUleD(FTMP, lhs, rhs);
3376 }
3377 __ Mfc1(dst, FTMP);
3378 __ Andi(dst, dst, 1);
3379 break;
3380 case kCondGT:
3381 if (gt_bias) {
3382 __ CmpUltD(FTMP, rhs, lhs);
3383 } else {
3384 __ CmpLtD(FTMP, rhs, lhs);
3385 }
3386 __ Mfc1(dst, FTMP);
3387 __ Andi(dst, dst, 1);
3388 break;
3389 case kCondGE:
3390 if (gt_bias) {
3391 __ CmpUleD(FTMP, rhs, lhs);
3392 } else {
3393 __ CmpLeD(FTMP, rhs, lhs);
3394 }
3395 __ Mfc1(dst, FTMP);
3396 __ Andi(dst, dst, 1);
3397 break;
3398 default:
3399 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3400 UNREACHABLE();
3401 }
3402 } else {
3403 switch (cond) {
3404 case kCondEQ:
3405 __ CeqD(0, lhs, rhs);
3406 __ LoadConst32(dst, 1);
3407 __ Movf(dst, ZERO, 0);
3408 break;
3409 case kCondNE:
3410 __ CeqD(0, lhs, rhs);
3411 __ LoadConst32(dst, 1);
3412 __ Movt(dst, ZERO, 0);
3413 break;
3414 case kCondLT:
3415 if (gt_bias) {
3416 __ ColtD(0, lhs, rhs);
3417 } else {
3418 __ CultD(0, lhs, rhs);
3419 }
3420 __ LoadConst32(dst, 1);
3421 __ Movf(dst, ZERO, 0);
3422 break;
3423 case kCondLE:
3424 if (gt_bias) {
3425 __ ColeD(0, lhs, rhs);
3426 } else {
3427 __ CuleD(0, lhs, rhs);
3428 }
3429 __ LoadConst32(dst, 1);
3430 __ Movf(dst, ZERO, 0);
3431 break;
3432 case kCondGT:
3433 if (gt_bias) {
3434 __ CultD(0, rhs, lhs);
3435 } else {
3436 __ ColtD(0, rhs, lhs);
3437 }
3438 __ LoadConst32(dst, 1);
3439 __ Movf(dst, ZERO, 0);
3440 break;
3441 case kCondGE:
3442 if (gt_bias) {
3443 __ CuleD(0, rhs, lhs);
3444 } else {
3445 __ ColeD(0, rhs, lhs);
3446 }
3447 __ LoadConst32(dst, 1);
3448 __ Movf(dst, ZERO, 0);
3449 break;
3450 default:
3451 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3452 UNREACHABLE();
3453 }
3454 }
3455 }
3456}
3457
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003458void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3459 bool gt_bias,
3460 Primitive::Type type,
3461 LocationSummary* locations,
3462 MipsLabel* label) {
3463 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3464 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3465 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3466 if (type == Primitive::kPrimFloat) {
3467 if (isR6) {
3468 switch (cond) {
3469 case kCondEQ:
3470 __ CmpEqS(FTMP, lhs, rhs);
3471 __ Bc1nez(FTMP, label);
3472 break;
3473 case kCondNE:
3474 __ CmpEqS(FTMP, lhs, rhs);
3475 __ Bc1eqz(FTMP, label);
3476 break;
3477 case kCondLT:
3478 if (gt_bias) {
3479 __ CmpLtS(FTMP, lhs, rhs);
3480 } else {
3481 __ CmpUltS(FTMP, lhs, rhs);
3482 }
3483 __ Bc1nez(FTMP, label);
3484 break;
3485 case kCondLE:
3486 if (gt_bias) {
3487 __ CmpLeS(FTMP, lhs, rhs);
3488 } else {
3489 __ CmpUleS(FTMP, lhs, rhs);
3490 }
3491 __ Bc1nez(FTMP, label);
3492 break;
3493 case kCondGT:
3494 if (gt_bias) {
3495 __ CmpUltS(FTMP, rhs, lhs);
3496 } else {
3497 __ CmpLtS(FTMP, rhs, lhs);
3498 }
3499 __ Bc1nez(FTMP, label);
3500 break;
3501 case kCondGE:
3502 if (gt_bias) {
3503 __ CmpUleS(FTMP, rhs, lhs);
3504 } else {
3505 __ CmpLeS(FTMP, rhs, lhs);
3506 }
3507 __ Bc1nez(FTMP, label);
3508 break;
3509 default:
3510 LOG(FATAL) << "Unexpected non-floating-point condition";
3511 }
3512 } else {
3513 switch (cond) {
3514 case kCondEQ:
3515 __ CeqS(0, lhs, rhs);
3516 __ Bc1t(0, label);
3517 break;
3518 case kCondNE:
3519 __ CeqS(0, lhs, rhs);
3520 __ Bc1f(0, label);
3521 break;
3522 case kCondLT:
3523 if (gt_bias) {
3524 __ ColtS(0, lhs, rhs);
3525 } else {
3526 __ CultS(0, lhs, rhs);
3527 }
3528 __ Bc1t(0, label);
3529 break;
3530 case kCondLE:
3531 if (gt_bias) {
3532 __ ColeS(0, lhs, rhs);
3533 } else {
3534 __ CuleS(0, lhs, rhs);
3535 }
3536 __ Bc1t(0, label);
3537 break;
3538 case kCondGT:
3539 if (gt_bias) {
3540 __ CultS(0, rhs, lhs);
3541 } else {
3542 __ ColtS(0, rhs, lhs);
3543 }
3544 __ Bc1t(0, label);
3545 break;
3546 case kCondGE:
3547 if (gt_bias) {
3548 __ CuleS(0, rhs, lhs);
3549 } else {
3550 __ ColeS(0, rhs, lhs);
3551 }
3552 __ Bc1t(0, label);
3553 break;
3554 default:
3555 LOG(FATAL) << "Unexpected non-floating-point condition";
3556 }
3557 }
3558 } else {
3559 DCHECK_EQ(type, Primitive::kPrimDouble);
3560 if (isR6) {
3561 switch (cond) {
3562 case kCondEQ:
3563 __ CmpEqD(FTMP, lhs, rhs);
3564 __ Bc1nez(FTMP, label);
3565 break;
3566 case kCondNE:
3567 __ CmpEqD(FTMP, lhs, rhs);
3568 __ Bc1eqz(FTMP, label);
3569 break;
3570 case kCondLT:
3571 if (gt_bias) {
3572 __ CmpLtD(FTMP, lhs, rhs);
3573 } else {
3574 __ CmpUltD(FTMP, lhs, rhs);
3575 }
3576 __ Bc1nez(FTMP, label);
3577 break;
3578 case kCondLE:
3579 if (gt_bias) {
3580 __ CmpLeD(FTMP, lhs, rhs);
3581 } else {
3582 __ CmpUleD(FTMP, lhs, rhs);
3583 }
3584 __ Bc1nez(FTMP, label);
3585 break;
3586 case kCondGT:
3587 if (gt_bias) {
3588 __ CmpUltD(FTMP, rhs, lhs);
3589 } else {
3590 __ CmpLtD(FTMP, rhs, lhs);
3591 }
3592 __ Bc1nez(FTMP, label);
3593 break;
3594 case kCondGE:
3595 if (gt_bias) {
3596 __ CmpUleD(FTMP, rhs, lhs);
3597 } else {
3598 __ CmpLeD(FTMP, rhs, lhs);
3599 }
3600 __ Bc1nez(FTMP, label);
3601 break;
3602 default:
3603 LOG(FATAL) << "Unexpected non-floating-point condition";
3604 }
3605 } else {
3606 switch (cond) {
3607 case kCondEQ:
3608 __ CeqD(0, lhs, rhs);
3609 __ Bc1t(0, label);
3610 break;
3611 case kCondNE:
3612 __ CeqD(0, lhs, rhs);
3613 __ Bc1f(0, label);
3614 break;
3615 case kCondLT:
3616 if (gt_bias) {
3617 __ ColtD(0, lhs, rhs);
3618 } else {
3619 __ CultD(0, lhs, rhs);
3620 }
3621 __ Bc1t(0, label);
3622 break;
3623 case kCondLE:
3624 if (gt_bias) {
3625 __ ColeD(0, lhs, rhs);
3626 } else {
3627 __ CuleD(0, lhs, rhs);
3628 }
3629 __ Bc1t(0, label);
3630 break;
3631 case kCondGT:
3632 if (gt_bias) {
3633 __ CultD(0, rhs, lhs);
3634 } else {
3635 __ ColtD(0, rhs, lhs);
3636 }
3637 __ Bc1t(0, label);
3638 break;
3639 case kCondGE:
3640 if (gt_bias) {
3641 __ CuleD(0, rhs, lhs);
3642 } else {
3643 __ ColeD(0, rhs, lhs);
3644 }
3645 __ Bc1t(0, label);
3646 break;
3647 default:
3648 LOG(FATAL) << "Unexpected non-floating-point condition";
3649 }
3650 }
3651 }
3652}
3653
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003654void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003655 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003656 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003657 MipsLabel* false_target) {
3658 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003659
David Brazdil0debae72015-11-12 18:37:00 +00003660 if (true_target == nullptr && false_target == nullptr) {
3661 // Nothing to do. The code always falls through.
3662 return;
3663 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003664 // Constant condition, statically compared against "true" (integer value 1).
3665 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003666 if (true_target != nullptr) {
3667 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003668 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003669 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003670 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003671 if (false_target != nullptr) {
3672 __ B(false_target);
3673 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003674 }
David Brazdil0debae72015-11-12 18:37:00 +00003675 return;
3676 }
3677
3678 // The following code generates these patterns:
3679 // (1) true_target == nullptr && false_target != nullptr
3680 // - opposite condition true => branch to false_target
3681 // (2) true_target != nullptr && false_target == nullptr
3682 // - condition true => branch to true_target
3683 // (3) true_target != nullptr && false_target != nullptr
3684 // - condition true => branch to true_target
3685 // - branch to false_target
3686 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003687 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003688 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003689 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003690 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003691 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3692 } else {
3693 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3694 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003695 } else {
3696 // The condition instruction has not been materialized, use its inputs as
3697 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003698 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003699 Primitive::Type type = condition->InputAt(0)->GetType();
3700 LocationSummary* locations = cond->GetLocations();
3701 IfCondition if_cond = condition->GetCondition();
3702 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003703
David Brazdil0debae72015-11-12 18:37:00 +00003704 if (true_target == nullptr) {
3705 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003706 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003707 }
3708
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003709 switch (type) {
3710 default:
3711 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3712 break;
3713 case Primitive::kPrimLong:
3714 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3715 break;
3716 case Primitive::kPrimFloat:
3717 case Primitive::kPrimDouble:
3718 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3719 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003720 }
3721 }
David Brazdil0debae72015-11-12 18:37:00 +00003722
3723 // If neither branch falls through (case 3), the conditional branch to `true_target`
3724 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3725 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003726 __ B(false_target);
3727 }
3728}
3729
3730void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003732 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003733 locations->SetInAt(0, Location::RequiresRegister());
3734 }
3735}
3736
3737void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003738 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3739 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3740 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3741 nullptr : codegen_->GetLabelOf(true_successor);
3742 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3743 nullptr : codegen_->GetLabelOf(false_successor);
3744 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003745}
3746
3747void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3748 LocationSummary* locations = new (GetGraph()->GetArena())
3749 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003750 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003751 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003752 locations->SetInAt(0, Location::RequiresRegister());
3753 }
3754}
3755
3756void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003757 SlowPathCodeMIPS* slow_path =
3758 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003759 GenerateTestAndBranch(deoptimize,
3760 /* condition_input_index */ 0,
3761 slow_path->GetEntryLabel(),
3762 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003763}
3764
David Brazdil74eb1b22015-12-14 11:44:01 +00003765void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3766 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3767 if (Primitive::IsFloatingPointType(select->GetType())) {
3768 locations->SetInAt(0, Location::RequiresFpuRegister());
3769 locations->SetInAt(1, Location::RequiresFpuRegister());
3770 } else {
3771 locations->SetInAt(0, Location::RequiresRegister());
3772 locations->SetInAt(1, Location::RequiresRegister());
3773 }
3774 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3775 locations->SetInAt(2, Location::RequiresRegister());
3776 }
3777 locations->SetOut(Location::SameAsFirstInput());
3778}
3779
3780void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3781 LocationSummary* locations = select->GetLocations();
3782 MipsLabel false_target;
3783 GenerateTestAndBranch(select,
3784 /* condition_input_index */ 2,
3785 /* true_target */ nullptr,
3786 &false_target);
3787 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3788 __ Bind(&false_target);
3789}
3790
David Srbecky0cf44932015-12-09 14:09:59 +00003791void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3792 new (GetGraph()->GetArena()) LocationSummary(info);
3793}
3794
David Srbeckyd28f4a02016-03-14 17:14:24 +00003795void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3796 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003797}
3798
3799void CodeGeneratorMIPS::GenerateNop() {
3800 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003801}
3802
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003803void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3804 Primitive::Type field_type = field_info.GetFieldType();
3805 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3806 bool generate_volatile = field_info.IsVolatile() && is_wide;
3807 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003808 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003809
3810 locations->SetInAt(0, Location::RequiresRegister());
3811 if (generate_volatile) {
3812 InvokeRuntimeCallingConvention calling_convention;
3813 // need A0 to hold base + offset
3814 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3815 if (field_type == Primitive::kPrimLong) {
3816 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3817 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003818 // Use Location::Any() to prevent situations when running out of available fp registers.
3819 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003820 // Need some temp core regs since FP results are returned in core registers
3821 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3822 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3823 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3824 }
3825 } else {
3826 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3827 locations->SetOut(Location::RequiresFpuRegister());
3828 } else {
3829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3830 }
3831 }
3832}
3833
3834void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3835 const FieldInfo& field_info,
3836 uint32_t dex_pc) {
3837 Primitive::Type type = field_info.GetFieldType();
3838 LocationSummary* locations = instruction->GetLocations();
3839 Register obj = locations->InAt(0).AsRegister<Register>();
3840 LoadOperandType load_type = kLoadUnsignedByte;
3841 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003842 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003843 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003844
3845 switch (type) {
3846 case Primitive::kPrimBoolean:
3847 load_type = kLoadUnsignedByte;
3848 break;
3849 case Primitive::kPrimByte:
3850 load_type = kLoadSignedByte;
3851 break;
3852 case Primitive::kPrimShort:
3853 load_type = kLoadSignedHalfword;
3854 break;
3855 case Primitive::kPrimChar:
3856 load_type = kLoadUnsignedHalfword;
3857 break;
3858 case Primitive::kPrimInt:
3859 case Primitive::kPrimFloat:
3860 case Primitive::kPrimNot:
3861 load_type = kLoadWord;
3862 break;
3863 case Primitive::kPrimLong:
3864 case Primitive::kPrimDouble:
3865 load_type = kLoadDoubleword;
3866 break;
3867 case Primitive::kPrimVoid:
3868 LOG(FATAL) << "Unreachable type " << type;
3869 UNREACHABLE();
3870 }
3871
3872 if (is_volatile && load_type == kLoadDoubleword) {
3873 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003874 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003875 // Do implicit Null check
3876 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3877 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01003878 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003879 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3880 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003881 // FP results are returned in core registers. Need to move them.
3882 Location out = locations->Out();
3883 if (out.IsFpuRegister()) {
3884 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3885 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3886 out.AsFpuRegister<FRegister>());
3887 } else {
3888 DCHECK(out.IsDoubleStackSlot());
3889 __ StoreToOffset(kStoreWord,
3890 locations->GetTemp(1).AsRegister<Register>(),
3891 SP,
3892 out.GetStackIndex());
3893 __ StoreToOffset(kStoreWord,
3894 locations->GetTemp(2).AsRegister<Register>(),
3895 SP,
3896 out.GetStackIndex() + 4);
3897 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003898 }
3899 } else {
3900 if (!Primitive::IsFloatingPointType(type)) {
3901 Register dst;
3902 if (type == Primitive::kPrimLong) {
3903 DCHECK(locations->Out().IsRegisterPair());
3904 dst = locations->Out().AsRegisterPairLow<Register>();
3905 } else {
3906 DCHECK(locations->Out().IsRegister());
3907 dst = locations->Out().AsRegister<Register>();
3908 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003909 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003910 } else {
3911 DCHECK(locations->Out().IsFpuRegister());
3912 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3913 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003914 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003915 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003916 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003917 }
3918 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003919 }
3920
3921 if (is_volatile) {
3922 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3923 }
3924}
3925
3926void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3927 Primitive::Type field_type = field_info.GetFieldType();
3928 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3929 bool generate_volatile = field_info.IsVolatile() && is_wide;
3930 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003931 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003932
3933 locations->SetInAt(0, Location::RequiresRegister());
3934 if (generate_volatile) {
3935 InvokeRuntimeCallingConvention calling_convention;
3936 // need A0 to hold base + offset
3937 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3938 if (field_type == Primitive::kPrimLong) {
3939 locations->SetInAt(1, Location::RegisterPairLocation(
3940 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3941 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003942 // Use Location::Any() to prevent situations when running out of available fp registers.
3943 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003944 // Pass FP parameters in core registers.
3945 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3946 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3947 }
3948 } else {
3949 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003950 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003951 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003952 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003953 }
3954 }
3955}
3956
3957void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3958 const FieldInfo& field_info,
3959 uint32_t dex_pc) {
3960 Primitive::Type type = field_info.GetFieldType();
3961 LocationSummary* locations = instruction->GetLocations();
3962 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07003963 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003964 StoreOperandType store_type = kStoreByte;
3965 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003966 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003967 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003968
3969 switch (type) {
3970 case Primitive::kPrimBoolean:
3971 case Primitive::kPrimByte:
3972 store_type = kStoreByte;
3973 break;
3974 case Primitive::kPrimShort:
3975 case Primitive::kPrimChar:
3976 store_type = kStoreHalfword;
3977 break;
3978 case Primitive::kPrimInt:
3979 case Primitive::kPrimFloat:
3980 case Primitive::kPrimNot:
3981 store_type = kStoreWord;
3982 break;
3983 case Primitive::kPrimLong:
3984 case Primitive::kPrimDouble:
3985 store_type = kStoreDoubleword;
3986 break;
3987 case Primitive::kPrimVoid:
3988 LOG(FATAL) << "Unreachable type " << type;
3989 UNREACHABLE();
3990 }
3991
3992 if (is_volatile) {
3993 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3994 }
3995
3996 if (is_volatile && store_type == kStoreDoubleword) {
3997 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003998 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003999 // Do implicit Null check.
4000 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4001 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4002 if (type == Primitive::kPrimDouble) {
4003 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004004 if (value_location.IsFpuRegister()) {
4005 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4006 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004007 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004008 value_location.AsFpuRegister<FRegister>());
4009 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004010 __ LoadFromOffset(kLoadWord,
4011 locations->GetTemp(1).AsRegister<Register>(),
4012 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004013 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004014 __ LoadFromOffset(kLoadWord,
4015 locations->GetTemp(2).AsRegister<Register>(),
4016 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004017 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004018 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004019 DCHECK(value_location.IsConstant());
4020 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4021 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004022 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4023 locations->GetTemp(1).AsRegister<Register>(),
4024 value);
4025 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004026 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004027 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004028 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4029 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004030 if (value_location.IsConstant()) {
4031 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4032 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4033 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004034 Register src;
4035 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004036 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004037 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004038 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004039 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004040 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004041 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004042 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004043 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004044 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004045 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004046 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004047 }
4048 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004049 }
4050
4051 // TODO: memory barriers?
4052 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004053 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004054 codegen_->MarkGCCard(obj, src);
4055 }
4056
4057 if (is_volatile) {
4058 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4059 }
4060}
4061
4062void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4063 HandleFieldGet(instruction, instruction->GetFieldInfo());
4064}
4065
4066void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4067 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4068}
4069
4070void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4071 HandleFieldSet(instruction, instruction->GetFieldInfo());
4072}
4073
4074void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4075 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4076}
4077
Alexey Frunze06a46c42016-07-19 15:00:40 -07004078void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
4079 HInstruction* instruction ATTRIBUTE_UNUSED,
4080 Location root,
4081 Register obj,
4082 uint32_t offset) {
4083 Register root_reg = root.AsRegister<Register>();
4084 if (kEmitCompilerReadBarrier) {
4085 UNIMPLEMENTED(FATAL) << "for read barrier";
4086 } else {
4087 // Plain GC root load with no read barrier.
4088 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4089 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
4090 // Note that GC roots are not affected by heap poisoning, thus we
4091 // do not have to unpoison `root_reg` here.
4092 }
4093}
4094
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004095void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4096 LocationSummary::CallKind call_kind =
4097 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
4098 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4099 locations->SetInAt(0, Location::RequiresRegister());
4100 locations->SetInAt(1, Location::RequiresRegister());
4101 // The output does overlap inputs.
4102 // Note that TypeCheckSlowPathMIPS uses this register too.
4103 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4104}
4105
4106void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4107 LocationSummary* locations = instruction->GetLocations();
4108 Register obj = locations->InAt(0).AsRegister<Register>();
4109 Register cls = locations->InAt(1).AsRegister<Register>();
4110 Register out = locations->Out().AsRegister<Register>();
4111
4112 MipsLabel done;
4113
4114 // Return 0 if `obj` is null.
4115 // TODO: Avoid this check if we know `obj` is not null.
4116 __ Move(out, ZERO);
4117 __ Beqz(obj, &done);
4118
4119 // Compare the class of `obj` with `cls`.
4120 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
4121 if (instruction->IsExactCheck()) {
4122 // Classes must be equal for the instanceof to succeed.
4123 __ Xor(out, out, cls);
4124 __ Sltiu(out, out, 1);
4125 } else {
4126 // If the classes are not equal, we go into a slow path.
4127 DCHECK(locations->OnlyCallsOnSlowPath());
4128 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
4129 codegen_->AddSlowPath(slow_path);
4130 __ Bne(out, cls, slow_path->GetEntryLabel());
4131 __ LoadConst32(out, 1);
4132 __ Bind(slow_path->GetExitLabel());
4133 }
4134
4135 __ Bind(&done);
4136}
4137
4138void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
4139 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4140 locations->SetOut(Location::ConstantLocation(constant));
4141}
4142
4143void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4144 // Will be generated at use site.
4145}
4146
4147void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
4148 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4149 locations->SetOut(Location::ConstantLocation(constant));
4150}
4151
4152void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4153 // Will be generated at use site.
4154}
4155
4156void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
4157 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
4158 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4159}
4160
4161void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4162 HandleInvoke(invoke);
4163 // The register T0 is required to be used for the hidden argument in
4164 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4165 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4166}
4167
4168void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4169 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4170 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004171 Location receiver = invoke->GetLocations()->InAt(0);
4172 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004173 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004174
4175 // Set the hidden argument.
4176 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
4177 invoke->GetDexMethodIndex());
4178
4179 // temp = object->GetClass();
4180 if (receiver.IsStackSlot()) {
4181 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4182 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4183 } else {
4184 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4185 }
4186 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004187 __ LoadFromOffset(kLoadWord, temp, temp,
4188 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
4189 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004190 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004191 // temp = temp->GetImtEntryAt(method_offset);
4192 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4193 // T9 = temp->GetEntryPoint();
4194 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4195 // T9();
4196 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004197 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004198 DCHECK(!codegen_->IsLeafMethod());
4199 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4200}
4201
4202void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07004203 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4204 if (intrinsic.TryDispatch(invoke)) {
4205 return;
4206 }
4207
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004208 HandleInvoke(invoke);
4209}
4210
4211void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004212 // Explicit clinit checks triggered by static invokes must have been pruned by
4213 // art::PrepareForRegisterAllocation.
4214 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004215
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004216 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4217 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4218 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4219
4220 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
4221 // R6 has PC-relative addressing.
4222 bool has_extra_input = !isR6 &&
4223 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4224 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4225
4226 if (invoke->HasPcRelativeDexCache()) {
4227 // kDexCachePcRelative is mutually exclusive with
4228 // kDirectAddressWithFixup/kCallDirectWithFixup.
4229 CHECK(!has_extra_input);
4230 has_extra_input = true;
4231 }
4232
Chris Larsen701566a2015-10-27 15:29:13 -07004233 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4234 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004235 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4236 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4237 }
Chris Larsen701566a2015-10-27 15:29:13 -07004238 return;
4239 }
4240
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004241 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004242
4243 // Add the extra input register if either the dex cache array base register
4244 // or the PC-relative base register for accessing literals is needed.
4245 if (has_extra_input) {
4246 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4247 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004248}
4249
Chris Larsen701566a2015-10-27 15:29:13 -07004250static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004251 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004252 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4253 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004254 return true;
4255 }
4256 return false;
4257}
4258
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004259HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004260 HLoadString::LoadKind desired_string_load_kind) {
4261 if (kEmitCompilerReadBarrier) {
4262 UNIMPLEMENTED(FATAL) << "for read barrier";
4263 }
4264 // We disable PC-relative load when there is an irreducible loop, as the optimization
4265 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004266 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4267 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07004268 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4269 bool fallback_load = has_irreducible_loops;
4270 switch (desired_string_load_kind) {
4271 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4272 DCHECK(!GetCompilerOptions().GetCompilePic());
4273 break;
4274 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4275 DCHECK(GetCompilerOptions().GetCompilePic());
4276 break;
4277 case HLoadString::LoadKind::kBootImageAddress:
4278 break;
4279 case HLoadString::LoadKind::kDexCacheAddress:
4280 DCHECK(Runtime::Current()->UseJitCompilation());
4281 fallback_load = false;
4282 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004283 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004284 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004285 break;
4286 case HLoadString::LoadKind::kDexCacheViaMethod:
4287 fallback_load = false;
4288 break;
4289 }
4290 if (fallback_load) {
4291 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4292 }
4293 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004294}
4295
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004296HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4297 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004298 if (kEmitCompilerReadBarrier) {
4299 UNIMPLEMENTED(FATAL) << "for read barrier";
4300 }
4301 // We disable pc-relative load when there is an irreducible loop, as the optimization
4302 // is incompatible with it.
4303 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4304 bool fallback_load = has_irreducible_loops;
4305 switch (desired_class_load_kind) {
4306 case HLoadClass::LoadKind::kReferrersClass:
4307 fallback_load = false;
4308 break;
4309 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4310 DCHECK(!GetCompilerOptions().GetCompilePic());
4311 break;
4312 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4313 DCHECK(GetCompilerOptions().GetCompilePic());
4314 break;
4315 case HLoadClass::LoadKind::kBootImageAddress:
4316 break;
4317 case HLoadClass::LoadKind::kDexCacheAddress:
4318 DCHECK(Runtime::Current()->UseJitCompilation());
4319 fallback_load = false;
4320 break;
4321 case HLoadClass::LoadKind::kDexCachePcRelative:
4322 DCHECK(!Runtime::Current()->UseJitCompilation());
4323 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4324 // with irreducible loops.
4325 break;
4326 case HLoadClass::LoadKind::kDexCacheViaMethod:
4327 fallback_load = false;
4328 break;
4329 }
4330 if (fallback_load) {
4331 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4332 }
4333 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004334}
4335
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004336Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4337 Register temp) {
4338 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4339 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4340 if (!invoke->GetLocations()->Intrinsified()) {
4341 return location.AsRegister<Register>();
4342 }
4343 // For intrinsics we allow any location, so it may be on the stack.
4344 if (!location.IsRegister()) {
4345 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4346 return temp;
4347 }
4348 // For register locations, check if the register was saved. If so, get it from the stack.
4349 // Note: There is a chance that the register was saved but not overwritten, so we could
4350 // save one load. However, since this is just an intrinsic slow path we prefer this
4351 // simple and more robust approach rather that trying to determine if that's the case.
4352 SlowPathCode* slow_path = GetCurrentSlowPath();
4353 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4354 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4355 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4356 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4357 return temp;
4358 }
4359 return location.AsRegister<Register>();
4360}
4361
Vladimir Markodc151b22015-10-15 18:02:30 +01004362HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4363 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004364 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004365 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4366 // We disable PC-relative load when there is an irreducible loop, as the optimization
4367 // is incompatible with it.
4368 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4369 bool fallback_load = true;
4370 bool fallback_call = true;
4371 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004372 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4373 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004374 fallback_load = has_irreducible_loops;
4375 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004376 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004377 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004378 break;
4379 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004380 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004381 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004382 fallback_call = has_irreducible_loops;
4383 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004384 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004385 // TODO: Implement this type.
4386 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004387 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004388 fallback_call = false;
4389 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004390 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004391 if (fallback_load) {
4392 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4393 dispatch_info.method_load_data = 0;
4394 }
4395 if (fallback_call) {
4396 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4397 dispatch_info.direct_code_ptr = 0;
4398 }
4399 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004400}
4401
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004402void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4403 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004404 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004405 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4406 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4407 bool isR6 = isa_features_.IsR6();
4408 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4409 // R6 has PC-relative addressing.
4410 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4411 (!isR6 &&
4412 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4413 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4414 Register base_reg = has_extra_input
4415 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4416 : ZERO;
4417
4418 // For better instruction scheduling we load the direct code pointer before the method pointer.
4419 switch (code_ptr_location) {
4420 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4421 // T9 = invoke->GetDirectCodePtr();
4422 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4423 break;
4424 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4425 // T9 = code address from literal pool with link-time patch.
4426 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4427 break;
4428 default:
4429 break;
4430 }
4431
4432 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004433 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004434 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004435 uint32_t offset =
4436 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004437 __ LoadFromOffset(kLoadWord,
4438 temp.AsRegister<Register>(),
4439 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004440 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004441 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004442 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004443 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004444 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004445 break;
4446 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4447 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4448 break;
4449 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004450 __ LoadLiteral(temp.AsRegister<Register>(),
4451 base_reg,
4452 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4453 break;
4454 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4455 HMipsDexCacheArraysBase* base =
4456 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4457 int32_t offset =
4458 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4459 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4460 break;
4461 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004462 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004463 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004464 Register reg = temp.AsRegister<Register>();
4465 Register method_reg;
4466 if (current_method.IsRegister()) {
4467 method_reg = current_method.AsRegister<Register>();
4468 } else {
4469 // TODO: use the appropriate DCHECK() here if possible.
4470 // DCHECK(invoke->GetLocations()->Intrinsified());
4471 DCHECK(!current_method.IsValid());
4472 method_reg = reg;
4473 __ Lw(reg, SP, kCurrentMethodStackOffset);
4474 }
4475
4476 // temp = temp->dex_cache_resolved_methods_;
4477 __ LoadFromOffset(kLoadWord,
4478 reg,
4479 method_reg,
4480 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004481 // temp = temp[index_in_cache];
4482 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4483 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004484 __ LoadFromOffset(kLoadWord,
4485 reg,
4486 reg,
4487 CodeGenerator::GetCachePointerOffset(index_in_cache));
4488 break;
4489 }
4490 }
4491
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004492 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004493 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004494 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004495 break;
4496 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004497 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4498 // T9 prepared above for better instruction scheduling.
4499 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004500 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004501 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004502 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004503 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004504 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004505 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4506 LOG(FATAL) << "Unsupported";
4507 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004508 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4509 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004510 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004511 T9,
4512 callee_method.AsRegister<Register>(),
4513 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004514 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004515 // T9()
4516 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004517 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004518 break;
4519 }
4520 DCHECK(!IsLeafMethod());
4521}
4522
4523void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004524 // Explicit clinit checks triggered by static invokes must have been pruned by
4525 // art::PrepareForRegisterAllocation.
4526 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004527
4528 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4529 return;
4530 }
4531
4532 LocationSummary* locations = invoke->GetLocations();
4533 codegen_->GenerateStaticOrDirectCall(invoke,
4534 locations->HasTemps()
4535 ? locations->GetTemp(0)
4536 : Location::NoLocation());
4537 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4538}
4539
Chris Larsen3acee732015-11-18 13:31:08 -08004540void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02004541 // Use the calling convention instead of the location of the receiver, as
4542 // intrinsics may have put the receiver in a different register. In the intrinsics
4543 // slow path, the arguments have been moved to the right place, so here we are
4544 // guaranteed that the receiver is the first register of the calling convention.
4545 InvokeDexCallingConvention calling_convention;
4546 Register receiver = calling_convention.GetRegisterAt(0);
4547
Chris Larsen3acee732015-11-18 13:31:08 -08004548 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004549 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4550 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4551 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004552 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004553
4554 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02004555 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08004556 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004557 // temp = temp->GetMethodAt(method_offset);
4558 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4559 // T9 = temp->GetEntryPoint();
4560 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4561 // T9();
4562 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004563 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004564}
4565
4566void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4567 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4568 return;
4569 }
4570
4571 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004572 DCHECK(!codegen_->IsLeafMethod());
4573 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4574}
4575
4576void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004577 if (cls->NeedsAccessCheck()) {
4578 InvokeRuntimeCallingConvention calling_convention;
4579 CodeGenerator::CreateLoadClassLocationSummary(
4580 cls,
4581 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4582 Location::RegisterLocation(V0),
4583 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4584 return;
4585 }
4586
4587 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4588 ? LocationSummary::kCallOnSlowPath
4589 : LocationSummary::kNoCall;
4590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4591 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4592 switch (load_kind) {
4593 // We need an extra register for PC-relative literals on R2.
4594 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4595 case HLoadClass::LoadKind::kBootImageAddress:
4596 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4597 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4598 break;
4599 }
4600 FALLTHROUGH_INTENDED;
4601 // We need an extra register for PC-relative dex cache accesses.
4602 case HLoadClass::LoadKind::kDexCachePcRelative:
4603 case HLoadClass::LoadKind::kReferrersClass:
4604 case HLoadClass::LoadKind::kDexCacheViaMethod:
4605 locations->SetInAt(0, Location::RequiresRegister());
4606 break;
4607 default:
4608 break;
4609 }
4610 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004611}
4612
4613void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4614 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004615 if (cls->NeedsAccessCheck()) {
4616 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004617 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004618 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004619 return;
4620 }
4621
Alexey Frunze06a46c42016-07-19 15:00:40 -07004622 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4623 Location out_loc = locations->Out();
4624 Register out = out_loc.AsRegister<Register>();
4625 Register base_or_current_method_reg;
4626 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4627 switch (load_kind) {
4628 // We need an extra register for PC-relative literals on R2.
4629 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4630 case HLoadClass::LoadKind::kBootImageAddress:
4631 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4632 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4633 break;
4634 // We need an extra register for PC-relative dex cache accesses.
4635 case HLoadClass::LoadKind::kDexCachePcRelative:
4636 case HLoadClass::LoadKind::kReferrersClass:
4637 case HLoadClass::LoadKind::kDexCacheViaMethod:
4638 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4639 break;
4640 default:
4641 base_or_current_method_reg = ZERO;
4642 break;
4643 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004644
Alexey Frunze06a46c42016-07-19 15:00:40 -07004645 bool generate_null_check = false;
4646 switch (load_kind) {
4647 case HLoadClass::LoadKind::kReferrersClass: {
4648 DCHECK(!cls->CanCallRuntime());
4649 DCHECK(!cls->MustGenerateClinitCheck());
4650 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4651 GenerateGcRootFieldLoad(cls,
4652 out_loc,
4653 base_or_current_method_reg,
4654 ArtMethod::DeclaringClassOffset().Int32Value());
4655 break;
4656 }
4657 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4658 DCHECK(!kEmitCompilerReadBarrier);
4659 __ LoadLiteral(out,
4660 base_or_current_method_reg,
4661 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4662 cls->GetTypeIndex()));
4663 break;
4664 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4665 DCHECK(!kEmitCompilerReadBarrier);
4666 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4667 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004668 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004669 break;
4670 }
4671 case HLoadClass::LoadKind::kBootImageAddress: {
4672 DCHECK(!kEmitCompilerReadBarrier);
4673 DCHECK_NE(cls->GetAddress(), 0u);
4674 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4675 __ LoadLiteral(out,
4676 base_or_current_method_reg,
4677 codegen_->DeduplicateBootImageAddressLiteral(address));
4678 break;
4679 }
4680 case HLoadClass::LoadKind::kDexCacheAddress: {
4681 DCHECK_NE(cls->GetAddress(), 0u);
4682 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4683 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4684 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4685 int16_t offset = Low16Bits(address);
4686 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4687 __ Lui(out, High16Bits(base_address));
4688 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4689 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4690 generate_null_check = !cls->IsInDexCache();
4691 break;
4692 }
4693 case HLoadClass::LoadKind::kDexCachePcRelative: {
4694 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4695 int32_t offset =
4696 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4697 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4698 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4699 generate_null_check = !cls->IsInDexCache();
4700 break;
4701 }
4702 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4703 // /* GcRoot<mirror::Class>[] */ out =
4704 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4705 __ LoadFromOffset(kLoadWord,
4706 out,
4707 base_or_current_method_reg,
4708 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4709 // /* GcRoot<mirror::Class> */ out = out[type_index]
4710 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4711 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4712 generate_null_check = !cls->IsInDexCache();
4713 }
4714 }
4715
4716 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4717 DCHECK(cls->CanCallRuntime());
4718 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4719 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4720 codegen_->AddSlowPath(slow_path);
4721 if (generate_null_check) {
4722 __ Beqz(out, slow_path->GetEntryLabel());
4723 }
4724 if (cls->MustGenerateClinitCheck()) {
4725 GenerateClassInitializationCheck(slow_path, out);
4726 } else {
4727 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004728 }
4729 }
4730}
4731
4732static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004733 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004734}
4735
4736void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4737 LocationSummary* locations =
4738 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4739 locations->SetOut(Location::RequiresRegister());
4740}
4741
4742void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4743 Register out = load->GetLocations()->Out().AsRegister<Register>();
4744 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4745}
4746
4747void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4748 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4749}
4750
4751void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4752 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4753}
4754
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004755void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004756 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00004757 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4758 ? LocationSummary::kCallOnMainOnly
4759 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004760 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004761 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004762 HLoadString::LoadKind load_kind = load->GetLoadKind();
4763 switch (load_kind) {
4764 // We need an extra register for PC-relative literals on R2.
4765 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4766 case HLoadString::LoadKind::kBootImageAddress:
4767 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004768 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004769 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4770 break;
4771 }
4772 FALLTHROUGH_INTENDED;
4773 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07004774 case HLoadString::LoadKind::kDexCacheViaMethod:
4775 locations->SetInAt(0, Location::RequiresRegister());
4776 break;
4777 default:
4778 break;
4779 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004780 locations->SetOut(Location::RequiresRegister());
4781}
4782
4783void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004784 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004785 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004786 Location out_loc = locations->Out();
4787 Register out = out_loc.AsRegister<Register>();
4788 Register base_or_current_method_reg;
4789 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4790 switch (load_kind) {
4791 // We need an extra register for PC-relative literals on R2.
4792 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4793 case HLoadString::LoadKind::kBootImageAddress:
4794 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004795 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004796 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4797 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004798 default:
4799 base_or_current_method_reg = ZERO;
4800 break;
4801 }
4802
4803 switch (load_kind) {
4804 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4805 DCHECK(!kEmitCompilerReadBarrier);
4806 __ LoadLiteral(out,
4807 base_or_current_method_reg,
4808 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4809 load->GetStringIndex()));
4810 return; // No dex cache slow path.
4811 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4812 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004813 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004814 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4815 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004816 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004817 return; // No dex cache slow path.
4818 }
4819 case HLoadString::LoadKind::kBootImageAddress: {
4820 DCHECK(!kEmitCompilerReadBarrier);
4821 DCHECK_NE(load->GetAddress(), 0u);
4822 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4823 __ LoadLiteral(out,
4824 base_or_current_method_reg,
4825 codegen_->DeduplicateBootImageAddressLiteral(address));
4826 return; // No dex cache slow path.
4827 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004828 case HLoadString::LoadKind::kBssEntry: {
4829 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4830 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4831 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
4832 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
4833 __ LoadFromOffset(kLoadWord, out, out, 0);
4834 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4835 codegen_->AddSlowPath(slow_path);
4836 __ Beqz(out, slow_path->GetEntryLabel());
4837 __ Bind(slow_path->GetExitLabel());
4838 return;
4839 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004840 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004841 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004842 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004843
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004844 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004845 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
4846 InvokeRuntimeCallingConvention calling_convention;
4847 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
4848 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4849 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004850}
4851
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004852void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4854 locations->SetOut(Location::ConstantLocation(constant));
4855}
4856
4857void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4858 // Will be generated at use site.
4859}
4860
4861void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4862 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004864 InvokeRuntimeCallingConvention calling_convention;
4865 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4866}
4867
4868void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4869 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01004870 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004871 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4872 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004873 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004874 }
4875 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4876}
4877
4878void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4879 LocationSummary* locations =
4880 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4881 switch (mul->GetResultType()) {
4882 case Primitive::kPrimInt:
4883 case Primitive::kPrimLong:
4884 locations->SetInAt(0, Location::RequiresRegister());
4885 locations->SetInAt(1, Location::RequiresRegister());
4886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4887 break;
4888
4889 case Primitive::kPrimFloat:
4890 case Primitive::kPrimDouble:
4891 locations->SetInAt(0, Location::RequiresFpuRegister());
4892 locations->SetInAt(1, Location::RequiresFpuRegister());
4893 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4894 break;
4895
4896 default:
4897 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4898 }
4899}
4900
4901void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4902 Primitive::Type type = instruction->GetType();
4903 LocationSummary* locations = instruction->GetLocations();
4904 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4905
4906 switch (type) {
4907 case Primitive::kPrimInt: {
4908 Register dst = locations->Out().AsRegister<Register>();
4909 Register lhs = locations->InAt(0).AsRegister<Register>();
4910 Register rhs = locations->InAt(1).AsRegister<Register>();
4911
4912 if (isR6) {
4913 __ MulR6(dst, lhs, rhs);
4914 } else {
4915 __ MulR2(dst, lhs, rhs);
4916 }
4917 break;
4918 }
4919 case Primitive::kPrimLong: {
4920 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4921 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4922 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4923 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4924 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4925 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4926
4927 // Extra checks to protect caused by the existance of A1_A2.
4928 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4929 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4930 DCHECK_NE(dst_high, lhs_low);
4931 DCHECK_NE(dst_high, rhs_low);
4932
4933 // A_B * C_D
4934 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4935 // dst_lo: [ low(B*D) ]
4936 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4937
4938 if (isR6) {
4939 __ MulR6(TMP, lhs_high, rhs_low);
4940 __ MulR6(dst_high, lhs_low, rhs_high);
4941 __ Addu(dst_high, dst_high, TMP);
4942 __ MuhuR6(TMP, lhs_low, rhs_low);
4943 __ Addu(dst_high, dst_high, TMP);
4944 __ MulR6(dst_low, lhs_low, rhs_low);
4945 } else {
4946 __ MulR2(TMP, lhs_high, rhs_low);
4947 __ MulR2(dst_high, lhs_low, rhs_high);
4948 __ Addu(dst_high, dst_high, TMP);
4949 __ MultuR2(lhs_low, rhs_low);
4950 __ Mfhi(TMP);
4951 __ Addu(dst_high, dst_high, TMP);
4952 __ Mflo(dst_low);
4953 }
4954 break;
4955 }
4956 case Primitive::kPrimFloat:
4957 case Primitive::kPrimDouble: {
4958 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4959 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4960 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4961 if (type == Primitive::kPrimFloat) {
4962 __ MulS(dst, lhs, rhs);
4963 } else {
4964 __ MulD(dst, lhs, rhs);
4965 }
4966 break;
4967 }
4968 default:
4969 LOG(FATAL) << "Unexpected mul type " << type;
4970 }
4971}
4972
4973void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4974 LocationSummary* locations =
4975 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4976 switch (neg->GetResultType()) {
4977 case Primitive::kPrimInt:
4978 case Primitive::kPrimLong:
4979 locations->SetInAt(0, Location::RequiresRegister());
4980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4981 break;
4982
4983 case Primitive::kPrimFloat:
4984 case Primitive::kPrimDouble:
4985 locations->SetInAt(0, Location::RequiresFpuRegister());
4986 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4987 break;
4988
4989 default:
4990 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4991 }
4992}
4993
4994void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4995 Primitive::Type type = instruction->GetType();
4996 LocationSummary* locations = instruction->GetLocations();
4997
4998 switch (type) {
4999 case Primitive::kPrimInt: {
5000 Register dst = locations->Out().AsRegister<Register>();
5001 Register src = locations->InAt(0).AsRegister<Register>();
5002 __ Subu(dst, ZERO, src);
5003 break;
5004 }
5005 case Primitive::kPrimLong: {
5006 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5007 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5008 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5009 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5010 __ Subu(dst_low, ZERO, src_low);
5011 __ Sltu(TMP, ZERO, dst_low);
5012 __ Subu(dst_high, ZERO, src_high);
5013 __ Subu(dst_high, dst_high, TMP);
5014 break;
5015 }
5016 case Primitive::kPrimFloat:
5017 case Primitive::kPrimDouble: {
5018 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5019 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5020 if (type == Primitive::kPrimFloat) {
5021 __ NegS(dst, src);
5022 } else {
5023 __ NegD(dst, src);
5024 }
5025 break;
5026 }
5027 default:
5028 LOG(FATAL) << "Unexpected neg type " << type;
5029 }
5030}
5031
5032void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5033 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005034 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005035 InvokeRuntimeCallingConvention calling_convention;
5036 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5037 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5038 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5039 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5040}
5041
5042void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5043 InvokeRuntimeCallingConvention calling_convention;
5044 Register current_method_register = calling_convention.GetRegisterAt(2);
5045 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5046 // Move an uint16_t value to a register.
5047 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005048 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005049 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5050 void*, uint32_t, int32_t, ArtMethod*>();
5051}
5052
5053void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5054 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005056 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005057 if (instruction->IsStringAlloc()) {
5058 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5059 } else {
5060 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5061 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5062 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005063 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5064}
5065
5066void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005067 if (instruction->IsStringAlloc()) {
5068 // String is allocated through StringFactory. Call NewEmptyString entry point.
5069 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005070 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005071 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5072 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5073 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005074 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005075 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5076 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005077 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00005078 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
5079 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005080}
5081
5082void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5083 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5084 locations->SetInAt(0, Location::RequiresRegister());
5085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5086}
5087
5088void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5089 Primitive::Type type = instruction->GetType();
5090 LocationSummary* locations = instruction->GetLocations();
5091
5092 switch (type) {
5093 case Primitive::kPrimInt: {
5094 Register dst = locations->Out().AsRegister<Register>();
5095 Register src = locations->InAt(0).AsRegister<Register>();
5096 __ Nor(dst, src, ZERO);
5097 break;
5098 }
5099
5100 case Primitive::kPrimLong: {
5101 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5102 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5103 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5104 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5105 __ Nor(dst_high, src_high, ZERO);
5106 __ Nor(dst_low, src_low, ZERO);
5107 break;
5108 }
5109
5110 default:
5111 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5112 }
5113}
5114
5115void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5117 locations->SetInAt(0, Location::RequiresRegister());
5118 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5119}
5120
5121void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5122 LocationSummary* locations = instruction->GetLocations();
5123 __ Xori(locations->Out().AsRegister<Register>(),
5124 locations->InAt(0).AsRegister<Register>(),
5125 1);
5126}
5127
5128void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005129 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5130 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005131}
5132
Calin Juravle2ae48182016-03-16 14:05:09 +00005133void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5134 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005135 return;
5136 }
5137 Location obj = instruction->GetLocations()->InAt(0);
5138
5139 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005140 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005141}
5142
Calin Juravle2ae48182016-03-16 14:05:09 +00005143void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005144 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005145 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005146
5147 Location obj = instruction->GetLocations()->InAt(0);
5148
5149 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5150}
5151
5152void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005153 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005154}
5155
5156void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5157 HandleBinaryOp(instruction);
5158}
5159
5160void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5161 HandleBinaryOp(instruction);
5162}
5163
5164void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5165 LOG(FATAL) << "Unreachable";
5166}
5167
5168void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
5169 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5170}
5171
5172void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
5173 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5174 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5175 if (location.IsStackSlot()) {
5176 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5177 } else if (location.IsDoubleStackSlot()) {
5178 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5179 }
5180 locations->SetOut(location);
5181}
5182
5183void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
5184 ATTRIBUTE_UNUSED) {
5185 // Nothing to do, the parameter is already at its location.
5186}
5187
5188void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5189 LocationSummary* locations =
5190 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5191 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5192}
5193
5194void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5195 ATTRIBUTE_UNUSED) {
5196 // Nothing to do, the method is already at its location.
5197}
5198
5199void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5200 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005201 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005202 locations->SetInAt(i, Location::Any());
5203 }
5204 locations->SetOut(Location::Any());
5205}
5206
5207void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5208 LOG(FATAL) << "Unreachable";
5209}
5210
5211void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5212 Primitive::Type type = rem->GetResultType();
5213 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005214 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005215 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5216
5217 switch (type) {
5218 case Primitive::kPrimInt:
5219 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005220 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5222 break;
5223
5224 case Primitive::kPrimLong: {
5225 InvokeRuntimeCallingConvention calling_convention;
5226 locations->SetInAt(0, Location::RegisterPairLocation(
5227 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5228 locations->SetInAt(1, Location::RegisterPairLocation(
5229 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5230 locations->SetOut(calling_convention.GetReturnLocation(type));
5231 break;
5232 }
5233
5234 case Primitive::kPrimFloat:
5235 case Primitive::kPrimDouble: {
5236 InvokeRuntimeCallingConvention calling_convention;
5237 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5238 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5239 locations->SetOut(calling_convention.GetReturnLocation(type));
5240 break;
5241 }
5242
5243 default:
5244 LOG(FATAL) << "Unexpected rem type " << type;
5245 }
5246}
5247
5248void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5249 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005250
5251 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005252 case Primitive::kPrimInt:
5253 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005254 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005255 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005256 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005257 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5258 break;
5259 }
5260 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005261 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005262 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005263 break;
5264 }
5265 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005266 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005267 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005268 break;
5269 }
5270 default:
5271 LOG(FATAL) << "Unexpected rem type " << type;
5272 }
5273}
5274
5275void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5276 memory_barrier->SetLocations(nullptr);
5277}
5278
5279void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5280 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5281}
5282
5283void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5285 Primitive::Type return_type = ret->InputAt(0)->GetType();
5286 locations->SetInAt(0, MipsReturnLocation(return_type));
5287}
5288
5289void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5290 codegen_->GenerateFrameExit();
5291}
5292
5293void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5294 ret->SetLocations(nullptr);
5295}
5296
5297void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5298 codegen_->GenerateFrameExit();
5299}
5300
Alexey Frunze92d90602015-12-18 18:16:36 -08005301void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5302 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005303}
5304
Alexey Frunze92d90602015-12-18 18:16:36 -08005305void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5306 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005307}
5308
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005309void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5310 HandleShift(shl);
5311}
5312
5313void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5314 HandleShift(shl);
5315}
5316
5317void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5318 HandleShift(shr);
5319}
5320
5321void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5322 HandleShift(shr);
5323}
5324
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005325void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5326 HandleBinaryOp(instruction);
5327}
5328
5329void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5330 HandleBinaryOp(instruction);
5331}
5332
5333void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5334 HandleFieldGet(instruction, instruction->GetFieldInfo());
5335}
5336
5337void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5338 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5339}
5340
5341void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5342 HandleFieldSet(instruction, instruction->GetFieldInfo());
5343}
5344
5345void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5346 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5347}
5348
5349void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5350 HUnresolvedInstanceFieldGet* instruction) {
5351 FieldAccessCallingConventionMIPS calling_convention;
5352 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5353 instruction->GetFieldType(),
5354 calling_convention);
5355}
5356
5357void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5358 HUnresolvedInstanceFieldGet* instruction) {
5359 FieldAccessCallingConventionMIPS calling_convention;
5360 codegen_->GenerateUnresolvedFieldAccess(instruction,
5361 instruction->GetFieldType(),
5362 instruction->GetFieldIndex(),
5363 instruction->GetDexPc(),
5364 calling_convention);
5365}
5366
5367void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5368 HUnresolvedInstanceFieldSet* instruction) {
5369 FieldAccessCallingConventionMIPS calling_convention;
5370 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5371 instruction->GetFieldType(),
5372 calling_convention);
5373}
5374
5375void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5376 HUnresolvedInstanceFieldSet* instruction) {
5377 FieldAccessCallingConventionMIPS calling_convention;
5378 codegen_->GenerateUnresolvedFieldAccess(instruction,
5379 instruction->GetFieldType(),
5380 instruction->GetFieldIndex(),
5381 instruction->GetDexPc(),
5382 calling_convention);
5383}
5384
5385void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5386 HUnresolvedStaticFieldGet* instruction) {
5387 FieldAccessCallingConventionMIPS calling_convention;
5388 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5389 instruction->GetFieldType(),
5390 calling_convention);
5391}
5392
5393void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5394 HUnresolvedStaticFieldGet* instruction) {
5395 FieldAccessCallingConventionMIPS calling_convention;
5396 codegen_->GenerateUnresolvedFieldAccess(instruction,
5397 instruction->GetFieldType(),
5398 instruction->GetFieldIndex(),
5399 instruction->GetDexPc(),
5400 calling_convention);
5401}
5402
5403void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5404 HUnresolvedStaticFieldSet* instruction) {
5405 FieldAccessCallingConventionMIPS calling_convention;
5406 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5407 instruction->GetFieldType(),
5408 calling_convention);
5409}
5410
5411void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5412 HUnresolvedStaticFieldSet* instruction) {
5413 FieldAccessCallingConventionMIPS calling_convention;
5414 codegen_->GenerateUnresolvedFieldAccess(instruction,
5415 instruction->GetFieldType(),
5416 instruction->GetFieldIndex(),
5417 instruction->GetDexPc(),
5418 calling_convention);
5419}
5420
5421void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005422 LocationSummary* locations =
5423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005424 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005425}
5426
5427void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5428 HBasicBlock* block = instruction->GetBlock();
5429 if (block->GetLoopInformation() != nullptr) {
5430 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5431 // The back edge will generate the suspend check.
5432 return;
5433 }
5434 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5435 // The goto will generate the suspend check.
5436 return;
5437 }
5438 GenerateSuspendCheck(instruction, nullptr);
5439}
5440
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005441void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5442 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005443 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005444 InvokeRuntimeCallingConvention calling_convention;
5445 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5446}
5447
5448void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005449 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005450 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5451}
5452
5453void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5454 Primitive::Type input_type = conversion->GetInputType();
5455 Primitive::Type result_type = conversion->GetResultType();
5456 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005457 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005458
5459 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5460 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5461 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5462 }
5463
5464 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005465 if (!isR6 &&
5466 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5467 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005468 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005469 }
5470
5471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5472
5473 if (call_kind == LocationSummary::kNoCall) {
5474 if (Primitive::IsFloatingPointType(input_type)) {
5475 locations->SetInAt(0, Location::RequiresFpuRegister());
5476 } else {
5477 locations->SetInAt(0, Location::RequiresRegister());
5478 }
5479
5480 if (Primitive::IsFloatingPointType(result_type)) {
5481 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5482 } else {
5483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5484 }
5485 } else {
5486 InvokeRuntimeCallingConvention calling_convention;
5487
5488 if (Primitive::IsFloatingPointType(input_type)) {
5489 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5490 } else {
5491 DCHECK_EQ(input_type, Primitive::kPrimLong);
5492 locations->SetInAt(0, Location::RegisterPairLocation(
5493 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5494 }
5495
5496 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5497 }
5498}
5499
5500void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5501 LocationSummary* locations = conversion->GetLocations();
5502 Primitive::Type result_type = conversion->GetResultType();
5503 Primitive::Type input_type = conversion->GetInputType();
5504 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005505 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005506
5507 DCHECK_NE(input_type, result_type);
5508
5509 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5510 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5511 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5512 Register src = locations->InAt(0).AsRegister<Register>();
5513
Alexey Frunzea871ef12016-06-27 15:20:11 -07005514 if (dst_low != src) {
5515 __ Move(dst_low, src);
5516 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005517 __ Sra(dst_high, src, 31);
5518 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5519 Register dst = locations->Out().AsRegister<Register>();
5520 Register src = (input_type == Primitive::kPrimLong)
5521 ? locations->InAt(0).AsRegisterPairLow<Register>()
5522 : locations->InAt(0).AsRegister<Register>();
5523
5524 switch (result_type) {
5525 case Primitive::kPrimChar:
5526 __ Andi(dst, src, 0xFFFF);
5527 break;
5528 case Primitive::kPrimByte:
5529 if (has_sign_extension) {
5530 __ Seb(dst, src);
5531 } else {
5532 __ Sll(dst, src, 24);
5533 __ Sra(dst, dst, 24);
5534 }
5535 break;
5536 case Primitive::kPrimShort:
5537 if (has_sign_extension) {
5538 __ Seh(dst, src);
5539 } else {
5540 __ Sll(dst, src, 16);
5541 __ Sra(dst, dst, 16);
5542 }
5543 break;
5544 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005545 if (dst != src) {
5546 __ Move(dst, src);
5547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005548 break;
5549
5550 default:
5551 LOG(FATAL) << "Unexpected type conversion from " << input_type
5552 << " to " << result_type;
5553 }
5554 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005555 if (input_type == Primitive::kPrimLong) {
5556 if (isR6) {
5557 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5558 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5559 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5560 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5561 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5562 __ Mtc1(src_low, FTMP);
5563 __ Mthc1(src_high, FTMP);
5564 if (result_type == Primitive::kPrimFloat) {
5565 __ Cvtsl(dst, FTMP);
5566 } else {
5567 __ Cvtdl(dst, FTMP);
5568 }
5569 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005570 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
5571 : kQuickL2d;
5572 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005573 if (result_type == Primitive::kPrimFloat) {
5574 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5575 } else {
5576 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5577 }
5578 }
5579 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005580 Register src = locations->InAt(0).AsRegister<Register>();
5581 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5582 __ Mtc1(src, FTMP);
5583 if (result_type == Primitive::kPrimFloat) {
5584 __ Cvtsw(dst, FTMP);
5585 } else {
5586 __ Cvtdw(dst, FTMP);
5587 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005588 }
5589 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5590 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005591 if (result_type == Primitive::kPrimLong) {
5592 if (isR6) {
5593 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5594 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5595 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5596 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5597 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5598 MipsLabel truncate;
5599 MipsLabel done;
5600
5601 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5602 // value when the input is either a NaN or is outside of the range of the output type
5603 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5604 // the same result.
5605 //
5606 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5607 // value of the output type if the input is outside of the range after the truncation or
5608 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5609 // results. This matches the desired float/double-to-int/long conversion exactly.
5610 //
5611 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5612 //
5613 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5614 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5615 // even though it must be NAN2008=1 on R6.
5616 //
5617 // The code takes care of the different behaviors by first comparing the input to the
5618 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5619 // If the input is greater than or equal to the minimum, it procedes to the truncate
5620 // instruction, which will handle such an input the same way irrespective of NAN2008.
5621 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5622 // in order to return either zero or the minimum value.
5623 //
5624 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5625 // truncate instruction for MIPS64R6.
5626 if (input_type == Primitive::kPrimFloat) {
5627 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5628 __ LoadConst32(TMP, min_val);
5629 __ Mtc1(TMP, FTMP);
5630 __ CmpLeS(FTMP, FTMP, src);
5631 } else {
5632 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5633 __ LoadConst32(TMP, High32Bits(min_val));
5634 __ Mtc1(ZERO, FTMP);
5635 __ Mthc1(TMP, FTMP);
5636 __ CmpLeD(FTMP, FTMP, src);
5637 }
5638
5639 __ Bc1nez(FTMP, &truncate);
5640
5641 if (input_type == Primitive::kPrimFloat) {
5642 __ CmpEqS(FTMP, src, src);
5643 } else {
5644 __ CmpEqD(FTMP, src, src);
5645 }
5646 __ Move(dst_low, ZERO);
5647 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5648 __ Mfc1(TMP, FTMP);
5649 __ And(dst_high, dst_high, TMP);
5650
5651 __ B(&done);
5652
5653 __ Bind(&truncate);
5654
5655 if (input_type == Primitive::kPrimFloat) {
5656 __ TruncLS(FTMP, src);
5657 } else {
5658 __ TruncLD(FTMP, src);
5659 }
5660 __ Mfc1(dst_low, FTMP);
5661 __ Mfhc1(dst_high, FTMP);
5662
5663 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005664 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005665 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
5666 : kQuickD2l;
5667 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005668 if (input_type == Primitive::kPrimFloat) {
5669 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5670 } else {
5671 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5672 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005673 }
5674 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005675 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5676 Register dst = locations->Out().AsRegister<Register>();
5677 MipsLabel truncate;
5678 MipsLabel done;
5679
5680 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5681 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5682 // even though it must be NAN2008=1 on R6.
5683 //
5684 // For details see the large comment above for the truncation of float/double to long on R6.
5685 //
5686 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5687 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005688 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005689 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5690 __ LoadConst32(TMP, min_val);
5691 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005692 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005693 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5694 __ LoadConst32(TMP, High32Bits(min_val));
5695 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005696 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005697 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005698
5699 if (isR6) {
5700 if (input_type == Primitive::kPrimFloat) {
5701 __ CmpLeS(FTMP, FTMP, src);
5702 } else {
5703 __ CmpLeD(FTMP, FTMP, src);
5704 }
5705 __ Bc1nez(FTMP, &truncate);
5706
5707 if (input_type == Primitive::kPrimFloat) {
5708 __ CmpEqS(FTMP, src, src);
5709 } else {
5710 __ CmpEqD(FTMP, src, src);
5711 }
5712 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5713 __ Mfc1(TMP, FTMP);
5714 __ And(dst, dst, TMP);
5715 } else {
5716 if (input_type == Primitive::kPrimFloat) {
5717 __ ColeS(0, FTMP, src);
5718 } else {
5719 __ ColeD(0, FTMP, src);
5720 }
5721 __ Bc1t(0, &truncate);
5722
5723 if (input_type == Primitive::kPrimFloat) {
5724 __ CeqS(0, src, src);
5725 } else {
5726 __ CeqD(0, src, src);
5727 }
5728 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5729 __ Movf(dst, ZERO, 0);
5730 }
5731
5732 __ B(&done);
5733
5734 __ Bind(&truncate);
5735
5736 if (input_type == Primitive::kPrimFloat) {
5737 __ TruncWS(FTMP, src);
5738 } else {
5739 __ TruncWD(FTMP, src);
5740 }
5741 __ Mfc1(dst, FTMP);
5742
5743 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005744 }
5745 } else if (Primitive::IsFloatingPointType(result_type) &&
5746 Primitive::IsFloatingPointType(input_type)) {
5747 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5748 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5749 if (result_type == Primitive::kPrimFloat) {
5750 __ Cvtsd(dst, src);
5751 } else {
5752 __ Cvtds(dst, src);
5753 }
5754 } else {
5755 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5756 << " to " << result_type;
5757 }
5758}
5759
5760void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5761 HandleShift(ushr);
5762}
5763
5764void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5765 HandleShift(ushr);
5766}
5767
5768void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5769 HandleBinaryOp(instruction);
5770}
5771
5772void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5773 HandleBinaryOp(instruction);
5774}
5775
5776void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5777 // Nothing to do, this should be removed during prepare for register allocator.
5778 LOG(FATAL) << "Unreachable";
5779}
5780
5781void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5782 // Nothing to do, this should be removed during prepare for register allocator.
5783 LOG(FATAL) << "Unreachable";
5784}
5785
5786void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005787 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005788}
5789
5790void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005791 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005792}
5793
5794void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005795 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005796}
5797
5798void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005799 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005800}
5801
5802void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005803 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005804}
5805
5806void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005807 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005808}
5809
5810void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005811 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005812}
5813
5814void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005815 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005816}
5817
5818void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005819 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005820}
5821
5822void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005823 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005824}
5825
5826void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005827 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005828}
5829
5830void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005831 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005832}
5833
5834void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005835 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005836}
5837
5838void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005839 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005840}
5841
5842void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005843 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005844}
5845
5846void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005847 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005848}
5849
5850void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005851 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005852}
5853
5854void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005855 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005856}
5857
5858void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005859 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005860}
5861
5862void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005863 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005864}
5865
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005866void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5867 LocationSummary* locations =
5868 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5869 locations->SetInAt(0, Location::RequiresRegister());
5870}
5871
Alexey Frunze96b66822016-09-10 02:32:44 -07005872void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
5873 int32_t lower_bound,
5874 uint32_t num_entries,
5875 HBasicBlock* switch_block,
5876 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005877 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005878 Register temp_reg = TMP;
5879 __ Addiu32(temp_reg, value_reg, -lower_bound);
5880 // Jump to default if index is negative
5881 // Note: We don't check the case that index is positive while value < lower_bound, because in
5882 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5883 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5884
Alexey Frunze96b66822016-09-10 02:32:44 -07005885 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005886 // Jump to successors[0] if value == lower_bound.
5887 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5888 int32_t last_index = 0;
5889 for (; num_entries - last_index > 2; last_index += 2) {
5890 __ Addiu(temp_reg, temp_reg, -2);
5891 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5892 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5893 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5894 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5895 }
5896 if (num_entries - last_index == 2) {
5897 // The last missing case_value.
5898 __ Addiu(temp_reg, temp_reg, -1);
5899 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005900 }
5901
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005902 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07005903 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005904 __ B(codegen_->GetLabelOf(default_block));
5905 }
5906}
5907
Alexey Frunze96b66822016-09-10 02:32:44 -07005908void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
5909 Register constant_area,
5910 int32_t lower_bound,
5911 uint32_t num_entries,
5912 HBasicBlock* switch_block,
5913 HBasicBlock* default_block) {
5914 // Create a jump table.
5915 std::vector<MipsLabel*> labels(num_entries);
5916 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
5917 for (uint32_t i = 0; i < num_entries; i++) {
5918 labels[i] = codegen_->GetLabelOf(successors[i]);
5919 }
5920 JumpTable* table = __ CreateJumpTable(std::move(labels));
5921
5922 // Is the value in range?
5923 __ Addiu32(TMP, value_reg, -lower_bound);
5924 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
5925 __ Sltiu(AT, TMP, num_entries);
5926 __ Beqz(AT, codegen_->GetLabelOf(default_block));
5927 } else {
5928 __ LoadConst32(AT, num_entries);
5929 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
5930 }
5931
5932 // We are in the range of the table.
5933 // Load the target address from the jump table, indexing by the value.
5934 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
5935 __ Sll(TMP, TMP, 2);
5936 __ Addu(TMP, TMP, AT);
5937 __ Lw(TMP, TMP, 0);
5938 // Compute the absolute target address by adding the table start address
5939 // (the table contains offsets to targets relative to its start).
5940 __ Addu(TMP, TMP, AT);
5941 // And jump.
5942 __ Jr(TMP);
5943 __ NopIfNoReordering();
5944}
5945
5946void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5947 int32_t lower_bound = switch_instr->GetStartValue();
5948 uint32_t num_entries = switch_instr->GetNumEntries();
5949 LocationSummary* locations = switch_instr->GetLocations();
5950 Register value_reg = locations->InAt(0).AsRegister<Register>();
5951 HBasicBlock* switch_block = switch_instr->GetBlock();
5952 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5953
5954 if (codegen_->GetInstructionSetFeatures().IsR6() &&
5955 num_entries > kPackedSwitchJumpTableThreshold) {
5956 // R6 uses PC-relative addressing to access the jump table.
5957 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
5958 // the jump table and it is implemented by changing HPackedSwitch to
5959 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
5960 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
5961 GenTableBasedPackedSwitch(value_reg,
5962 ZERO,
5963 lower_bound,
5964 num_entries,
5965 switch_block,
5966 default_block);
5967 } else {
5968 GenPackedSwitchWithCompares(value_reg,
5969 lower_bound,
5970 num_entries,
5971 switch_block,
5972 default_block);
5973 }
5974}
5975
5976void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5977 LocationSummary* locations =
5978 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5979 locations->SetInAt(0, Location::RequiresRegister());
5980 // Constant area pointer (HMipsComputeBaseMethodAddress).
5981 locations->SetInAt(1, Location::RequiresRegister());
5982}
5983
5984void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5985 int32_t lower_bound = switch_instr->GetStartValue();
5986 uint32_t num_entries = switch_instr->GetNumEntries();
5987 LocationSummary* locations = switch_instr->GetLocations();
5988 Register value_reg = locations->InAt(0).AsRegister<Register>();
5989 Register constant_area = locations->InAt(1).AsRegister<Register>();
5990 HBasicBlock* switch_block = switch_instr->GetBlock();
5991 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5992
5993 // This is an R2-only path. HPackedSwitch has been changed to
5994 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
5995 // required to address the jump table relative to PC.
5996 GenTableBasedPackedSwitch(value_reg,
5997 constant_area,
5998 lower_bound,
5999 num_entries,
6000 switch_block,
6001 default_block);
6002}
6003
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006004void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6005 HMipsComputeBaseMethodAddress* insn) {
6006 LocationSummary* locations =
6007 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6008 locations->SetOut(Location::RequiresRegister());
6009}
6010
6011void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6012 HMipsComputeBaseMethodAddress* insn) {
6013 LocationSummary* locations = insn->GetLocations();
6014 Register reg = locations->Out().AsRegister<Register>();
6015
6016 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6017
6018 // Generate a dummy PC-relative call to obtain PC.
6019 __ Nal();
6020 // Grab the return address off RA.
6021 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006022 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006023
6024 // Remember this offset (the obtained PC value) for later use with constant area.
6025 __ BindPcRelBaseLabel();
6026}
6027
6028void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6029 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6030 locations->SetOut(Location::RequiresRegister());
6031}
6032
6033void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6034 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6035 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6036 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006037 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6038 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006039}
6040
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006041void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6042 // The trampoline uses the same calling convention as dex calling conventions,
6043 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6044 // the method_idx.
6045 HandleInvoke(invoke);
6046}
6047
6048void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6049 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6050}
6051
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006052void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6053 LocationSummary* locations =
6054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6055 locations->SetInAt(0, Location::RequiresRegister());
6056 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006057}
6058
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006059void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6060 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006062 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006063 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006064 __ LoadFromOffset(kLoadWord,
6065 locations->Out().AsRegister<Register>(),
6066 locations->InAt(0).AsRegister<Register>(),
6067 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006068 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006069 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006070 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006071 __ LoadFromOffset(kLoadWord,
6072 locations->Out().AsRegister<Register>(),
6073 locations->InAt(0).AsRegister<Register>(),
6074 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006075 __ LoadFromOffset(kLoadWord,
6076 locations->Out().AsRegister<Register>(),
6077 locations->Out().AsRegister<Register>(),
6078 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006079 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006080}
6081
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006082#undef __
6083#undef QUICK_ENTRY_POINT
6084
6085} // namespace mips
6086} // namespace art