blob: e336df8c6c70ba83ece3104ff45c9f7263a21154 [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:
Alexey Frunzee7697712016-09-15 21:37:49 -07002258 locations->SetInAt(0, Location::RequiresRegister());
2259 locations->SetInAt(1, Location::RequiresRegister());
2260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2261 break;
2262
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002263 case Primitive::kPrimLong:
2264 locations->SetInAt(0, Location::RequiresRegister());
2265 locations->SetInAt(1, Location::RequiresRegister());
2266 // Output overlaps because it is written before doing the low comparison.
2267 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2268 break;
2269
2270 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002271 case Primitive::kPrimDouble:
2272 locations->SetInAt(0, Location::RequiresFpuRegister());
2273 locations->SetInAt(1, Location::RequiresFpuRegister());
2274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002275 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276
2277 default:
2278 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2279 }
2280}
2281
2282void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2283 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002284 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002285 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002286 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002287
2288 // 0 if: left == right
2289 // 1 if: left > right
2290 // -1 if: left < right
2291 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002292 case Primitive::kPrimBoolean:
2293 case Primitive::kPrimByte:
2294 case Primitive::kPrimShort:
2295 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002296 case Primitive::kPrimInt: {
2297 Register lhs = locations->InAt(0).AsRegister<Register>();
2298 Register rhs = locations->InAt(1).AsRegister<Register>();
2299 __ Slt(TMP, lhs, rhs);
2300 __ Slt(res, rhs, lhs);
2301 __ Subu(res, res, TMP);
2302 break;
2303 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002304 case Primitive::kPrimLong: {
2305 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002306 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2307 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2308 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2309 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2310 // TODO: more efficient (direct) comparison with a constant.
2311 __ Slt(TMP, lhs_high, rhs_high);
2312 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2313 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2314 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2315 __ Sltu(TMP, lhs_low, rhs_low);
2316 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2317 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2318 __ Bind(&done);
2319 break;
2320 }
2321
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002322 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002323 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002324 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2325 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2326 MipsLabel done;
2327 if (isR6) {
2328 __ CmpEqS(FTMP, lhs, rhs);
2329 __ LoadConst32(res, 0);
2330 __ Bc1nez(FTMP, &done);
2331 if (gt_bias) {
2332 __ CmpLtS(FTMP, lhs, rhs);
2333 __ LoadConst32(res, -1);
2334 __ Bc1nez(FTMP, &done);
2335 __ LoadConst32(res, 1);
2336 } else {
2337 __ CmpLtS(FTMP, rhs, lhs);
2338 __ LoadConst32(res, 1);
2339 __ Bc1nez(FTMP, &done);
2340 __ LoadConst32(res, -1);
2341 }
2342 } else {
2343 if (gt_bias) {
2344 __ ColtS(0, lhs, rhs);
2345 __ LoadConst32(res, -1);
2346 __ Bc1t(0, &done);
2347 __ CeqS(0, lhs, rhs);
2348 __ LoadConst32(res, 1);
2349 __ Movt(res, ZERO, 0);
2350 } else {
2351 __ ColtS(0, rhs, lhs);
2352 __ LoadConst32(res, 1);
2353 __ Bc1t(0, &done);
2354 __ CeqS(0, lhs, rhs);
2355 __ LoadConst32(res, -1);
2356 __ Movt(res, ZERO, 0);
2357 }
2358 }
2359 __ Bind(&done);
2360 break;
2361 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002362 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002363 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002364 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2365 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2366 MipsLabel done;
2367 if (isR6) {
2368 __ CmpEqD(FTMP, lhs, rhs);
2369 __ LoadConst32(res, 0);
2370 __ Bc1nez(FTMP, &done);
2371 if (gt_bias) {
2372 __ CmpLtD(FTMP, lhs, rhs);
2373 __ LoadConst32(res, -1);
2374 __ Bc1nez(FTMP, &done);
2375 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002377 __ CmpLtD(FTMP, rhs, lhs);
2378 __ LoadConst32(res, 1);
2379 __ Bc1nez(FTMP, &done);
2380 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002381 }
2382 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002383 if (gt_bias) {
2384 __ ColtD(0, lhs, rhs);
2385 __ LoadConst32(res, -1);
2386 __ Bc1t(0, &done);
2387 __ CeqD(0, lhs, rhs);
2388 __ LoadConst32(res, 1);
2389 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002390 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002391 __ ColtD(0, rhs, lhs);
2392 __ LoadConst32(res, 1);
2393 __ Bc1t(0, &done);
2394 __ CeqD(0, lhs, rhs);
2395 __ LoadConst32(res, -1);
2396 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002397 }
2398 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002399 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002400 break;
2401 }
2402
2403 default:
2404 LOG(FATAL) << "Unimplemented compare type " << in_type;
2405 }
2406}
2407
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002408void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002409 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002410 switch (instruction->InputAt(0)->GetType()) {
2411 default:
2412 case Primitive::kPrimLong:
2413 locations->SetInAt(0, Location::RequiresRegister());
2414 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2415 break;
2416
2417 case Primitive::kPrimFloat:
2418 case Primitive::kPrimDouble:
2419 locations->SetInAt(0, Location::RequiresFpuRegister());
2420 locations->SetInAt(1, Location::RequiresFpuRegister());
2421 break;
2422 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002423 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2425 }
2426}
2427
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002428void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002429 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002430 return;
2431 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002433 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434 LocationSummary* locations = instruction->GetLocations();
2435 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002436 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002437
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002438 switch (type) {
2439 default:
2440 // Integer case.
2441 GenerateIntCompare(instruction->GetCondition(), locations);
2442 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002443
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002444 case Primitive::kPrimLong:
2445 // TODO: don't use branches.
2446 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002447 break;
2448
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002449 case Primitive::kPrimFloat:
2450 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002451 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2452 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002453 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002454
2455 // Convert the branches into the result.
2456 MipsLabel done;
2457
2458 // False case: result = 0.
2459 __ LoadConst32(dst, 0);
2460 __ B(&done);
2461
2462 // True case: result = 1.
2463 __ Bind(&true_label);
2464 __ LoadConst32(dst, 1);
2465 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002466}
2467
Alexey Frunze7e99e052015-11-24 19:28:01 -08002468void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2469 DCHECK(instruction->IsDiv() || instruction->IsRem());
2470 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2471
2472 LocationSummary* locations = instruction->GetLocations();
2473 Location second = locations->InAt(1);
2474 DCHECK(second.IsConstant());
2475
2476 Register out = locations->Out().AsRegister<Register>();
2477 Register dividend = locations->InAt(0).AsRegister<Register>();
2478 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2479 DCHECK(imm == 1 || imm == -1);
2480
2481 if (instruction->IsRem()) {
2482 __ Move(out, ZERO);
2483 } else {
2484 if (imm == -1) {
2485 __ Subu(out, ZERO, dividend);
2486 } else if (out != dividend) {
2487 __ Move(out, dividend);
2488 }
2489 }
2490}
2491
2492void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2493 DCHECK(instruction->IsDiv() || instruction->IsRem());
2494 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2495
2496 LocationSummary* locations = instruction->GetLocations();
2497 Location second = locations->InAt(1);
2498 DCHECK(second.IsConstant());
2499
2500 Register out = locations->Out().AsRegister<Register>();
2501 Register dividend = locations->InAt(0).AsRegister<Register>();
2502 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002503 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002504 int ctz_imm = CTZ(abs_imm);
2505
2506 if (instruction->IsDiv()) {
2507 if (ctz_imm == 1) {
2508 // Fast path for division by +/-2, which is very common.
2509 __ Srl(TMP, dividend, 31);
2510 } else {
2511 __ Sra(TMP, dividend, 31);
2512 __ Srl(TMP, TMP, 32 - ctz_imm);
2513 }
2514 __ Addu(out, dividend, TMP);
2515 __ Sra(out, out, ctz_imm);
2516 if (imm < 0) {
2517 __ Subu(out, ZERO, out);
2518 }
2519 } else {
2520 if (ctz_imm == 1) {
2521 // Fast path for modulo +/-2, which is very common.
2522 __ Sra(TMP, dividend, 31);
2523 __ Subu(out, dividend, TMP);
2524 __ Andi(out, out, 1);
2525 __ Addu(out, out, TMP);
2526 } else {
2527 __ Sra(TMP, dividend, 31);
2528 __ Srl(TMP, TMP, 32 - ctz_imm);
2529 __ Addu(out, dividend, TMP);
2530 if (IsUint<16>(abs_imm - 1)) {
2531 __ Andi(out, out, abs_imm - 1);
2532 } else {
2533 __ Sll(out, out, 32 - ctz_imm);
2534 __ Srl(out, out, 32 - ctz_imm);
2535 }
2536 __ Subu(out, out, TMP);
2537 }
2538 }
2539}
2540
2541void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2542 DCHECK(instruction->IsDiv() || instruction->IsRem());
2543 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2544
2545 LocationSummary* locations = instruction->GetLocations();
2546 Location second = locations->InAt(1);
2547 DCHECK(second.IsConstant());
2548
2549 Register out = locations->Out().AsRegister<Register>();
2550 Register dividend = locations->InAt(0).AsRegister<Register>();
2551 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2552
2553 int64_t magic;
2554 int shift;
2555 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2556
2557 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2558
2559 __ LoadConst32(TMP, magic);
2560 if (isR6) {
2561 __ MuhR6(TMP, dividend, TMP);
2562 } else {
2563 __ MultR2(dividend, TMP);
2564 __ Mfhi(TMP);
2565 }
2566 if (imm > 0 && magic < 0) {
2567 __ Addu(TMP, TMP, dividend);
2568 } else if (imm < 0 && magic > 0) {
2569 __ Subu(TMP, TMP, dividend);
2570 }
2571
2572 if (shift != 0) {
2573 __ Sra(TMP, TMP, shift);
2574 }
2575
2576 if (instruction->IsDiv()) {
2577 __ Sra(out, TMP, 31);
2578 __ Subu(out, TMP, out);
2579 } else {
2580 __ Sra(AT, TMP, 31);
2581 __ Subu(AT, TMP, AT);
2582 __ LoadConst32(TMP, imm);
2583 if (isR6) {
2584 __ MulR6(TMP, AT, TMP);
2585 } else {
2586 __ MulR2(TMP, AT, TMP);
2587 }
2588 __ Subu(out, dividend, TMP);
2589 }
2590}
2591
2592void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2593 DCHECK(instruction->IsDiv() || instruction->IsRem());
2594 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2595
2596 LocationSummary* locations = instruction->GetLocations();
2597 Register out = locations->Out().AsRegister<Register>();
2598 Location second = locations->InAt(1);
2599
2600 if (second.IsConstant()) {
2601 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2602 if (imm == 0) {
2603 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2604 } else if (imm == 1 || imm == -1) {
2605 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002606 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002607 DivRemByPowerOfTwo(instruction);
2608 } else {
2609 DCHECK(imm <= -2 || imm >= 2);
2610 GenerateDivRemWithAnyConstant(instruction);
2611 }
2612 } else {
2613 Register dividend = locations->InAt(0).AsRegister<Register>();
2614 Register divisor = second.AsRegister<Register>();
2615 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2616 if (instruction->IsDiv()) {
2617 if (isR6) {
2618 __ DivR6(out, dividend, divisor);
2619 } else {
2620 __ DivR2(out, dividend, divisor);
2621 }
2622 } else {
2623 if (isR6) {
2624 __ ModR6(out, dividend, divisor);
2625 } else {
2626 __ ModR2(out, dividend, divisor);
2627 }
2628 }
2629 }
2630}
2631
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002632void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2633 Primitive::Type type = div->GetResultType();
2634 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002635 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002636 : LocationSummary::kNoCall;
2637
2638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2639
2640 switch (type) {
2641 case Primitive::kPrimInt:
2642 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002643 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002644 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2645 break;
2646
2647 case Primitive::kPrimLong: {
2648 InvokeRuntimeCallingConvention calling_convention;
2649 locations->SetInAt(0, Location::RegisterPairLocation(
2650 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2651 locations->SetInAt(1, Location::RegisterPairLocation(
2652 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2653 locations->SetOut(calling_convention.GetReturnLocation(type));
2654 break;
2655 }
2656
2657 case Primitive::kPrimFloat:
2658 case Primitive::kPrimDouble:
2659 locations->SetInAt(0, Location::RequiresFpuRegister());
2660 locations->SetInAt(1, Location::RequiresFpuRegister());
2661 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2662 break;
2663
2664 default:
2665 LOG(FATAL) << "Unexpected div type " << type;
2666 }
2667}
2668
2669void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2670 Primitive::Type type = instruction->GetType();
2671 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002672
2673 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002674 case Primitive::kPrimInt:
2675 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002676 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002677 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002678 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002679 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2680 break;
2681 }
2682 case Primitive::kPrimFloat:
2683 case Primitive::kPrimDouble: {
2684 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2685 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2686 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2687 if (type == Primitive::kPrimFloat) {
2688 __ DivS(dst, lhs, rhs);
2689 } else {
2690 __ DivD(dst, lhs, rhs);
2691 }
2692 break;
2693 }
2694 default:
2695 LOG(FATAL) << "Unexpected div type " << type;
2696 }
2697}
2698
2699void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002700 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002701 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002702}
2703
2704void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2705 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2706 codegen_->AddSlowPath(slow_path);
2707 Location value = instruction->GetLocations()->InAt(0);
2708 Primitive::Type type = instruction->GetType();
2709
2710 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002711 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002712 case Primitive::kPrimByte:
2713 case Primitive::kPrimChar:
2714 case Primitive::kPrimShort:
2715 case Primitive::kPrimInt: {
2716 if (value.IsConstant()) {
2717 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2718 __ B(slow_path->GetEntryLabel());
2719 } else {
2720 // A division by a non-null constant is valid. We don't need to perform
2721 // any check, so simply fall through.
2722 }
2723 } else {
2724 DCHECK(value.IsRegister()) << value;
2725 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2726 }
2727 break;
2728 }
2729 case Primitive::kPrimLong: {
2730 if (value.IsConstant()) {
2731 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2732 __ B(slow_path->GetEntryLabel());
2733 } else {
2734 // A division by a non-null constant is valid. We don't need to perform
2735 // any check, so simply fall through.
2736 }
2737 } else {
2738 DCHECK(value.IsRegisterPair()) << value;
2739 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2740 __ Beqz(TMP, slow_path->GetEntryLabel());
2741 }
2742 break;
2743 }
2744 default:
2745 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2746 }
2747}
2748
2749void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2750 LocationSummary* locations =
2751 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2752 locations->SetOut(Location::ConstantLocation(constant));
2753}
2754
2755void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2756 // Will be generated at use site.
2757}
2758
2759void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2760 exit->SetLocations(nullptr);
2761}
2762
2763void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2764}
2765
2766void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2767 LocationSummary* locations =
2768 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2769 locations->SetOut(Location::ConstantLocation(constant));
2770}
2771
2772void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2773 // Will be generated at use site.
2774}
2775
2776void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2777 got->SetLocations(nullptr);
2778}
2779
2780void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2781 DCHECK(!successor->IsExitBlock());
2782 HBasicBlock* block = got->GetBlock();
2783 HInstruction* previous = got->GetPrevious();
2784 HLoopInformation* info = block->GetLoopInformation();
2785
2786 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2787 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2788 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2789 return;
2790 }
2791 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2792 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2793 }
2794 if (!codegen_->GoesToNextBlock(block, successor)) {
2795 __ B(codegen_->GetLabelOf(successor));
2796 }
2797}
2798
2799void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2800 HandleGoto(got, got->GetSuccessor());
2801}
2802
2803void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2804 try_boundary->SetLocations(nullptr);
2805}
2806
2807void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2808 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2809 if (!successor->IsExitBlock()) {
2810 HandleGoto(try_boundary, successor);
2811 }
2812}
2813
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002814void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2815 LocationSummary* locations) {
2816 Register dst = locations->Out().AsRegister<Register>();
2817 Register lhs = locations->InAt(0).AsRegister<Register>();
2818 Location rhs_location = locations->InAt(1);
2819 Register rhs_reg = ZERO;
2820 int64_t rhs_imm = 0;
2821 bool use_imm = rhs_location.IsConstant();
2822 if (use_imm) {
2823 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2824 } else {
2825 rhs_reg = rhs_location.AsRegister<Register>();
2826 }
2827
2828 switch (cond) {
2829 case kCondEQ:
2830 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002831 if (use_imm && IsInt<16>(-rhs_imm)) {
2832 if (rhs_imm == 0) {
2833 if (cond == kCondEQ) {
2834 __ Sltiu(dst, lhs, 1);
2835 } else {
2836 __ Sltu(dst, ZERO, lhs);
2837 }
2838 } else {
2839 __ Addiu(dst, lhs, -rhs_imm);
2840 if (cond == kCondEQ) {
2841 __ Sltiu(dst, dst, 1);
2842 } else {
2843 __ Sltu(dst, ZERO, dst);
2844 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002845 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002846 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002847 if (use_imm && IsUint<16>(rhs_imm)) {
2848 __ Xori(dst, lhs, rhs_imm);
2849 } else {
2850 if (use_imm) {
2851 rhs_reg = TMP;
2852 __ LoadConst32(rhs_reg, rhs_imm);
2853 }
2854 __ Xor(dst, lhs, rhs_reg);
2855 }
2856 if (cond == kCondEQ) {
2857 __ Sltiu(dst, dst, 1);
2858 } else {
2859 __ Sltu(dst, ZERO, dst);
2860 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002861 }
2862 break;
2863
2864 case kCondLT:
2865 case kCondGE:
2866 if (use_imm && IsInt<16>(rhs_imm)) {
2867 __ Slti(dst, lhs, rhs_imm);
2868 } else {
2869 if (use_imm) {
2870 rhs_reg = TMP;
2871 __ LoadConst32(rhs_reg, rhs_imm);
2872 }
2873 __ Slt(dst, lhs, rhs_reg);
2874 }
2875 if (cond == kCondGE) {
2876 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2877 // only the slt instruction but no sge.
2878 __ Xori(dst, dst, 1);
2879 }
2880 break;
2881
2882 case kCondLE:
2883 case kCondGT:
2884 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2885 // Simulate lhs <= rhs via lhs < rhs + 1.
2886 __ Slti(dst, lhs, rhs_imm + 1);
2887 if (cond == kCondGT) {
2888 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2889 // only the slti instruction but no sgti.
2890 __ Xori(dst, dst, 1);
2891 }
2892 } else {
2893 if (use_imm) {
2894 rhs_reg = TMP;
2895 __ LoadConst32(rhs_reg, rhs_imm);
2896 }
2897 __ Slt(dst, rhs_reg, lhs);
2898 if (cond == kCondLE) {
2899 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2900 // only the slt instruction but no sle.
2901 __ Xori(dst, dst, 1);
2902 }
2903 }
2904 break;
2905
2906 case kCondB:
2907 case kCondAE:
2908 if (use_imm && IsInt<16>(rhs_imm)) {
2909 // Sltiu sign-extends its 16-bit immediate operand before
2910 // the comparison and thus lets us compare directly with
2911 // unsigned values in the ranges [0, 0x7fff] and
2912 // [0xffff8000, 0xffffffff].
2913 __ Sltiu(dst, lhs, rhs_imm);
2914 } else {
2915 if (use_imm) {
2916 rhs_reg = TMP;
2917 __ LoadConst32(rhs_reg, rhs_imm);
2918 }
2919 __ Sltu(dst, lhs, rhs_reg);
2920 }
2921 if (cond == kCondAE) {
2922 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2923 // only the sltu instruction but no sgeu.
2924 __ Xori(dst, dst, 1);
2925 }
2926 break;
2927
2928 case kCondBE:
2929 case kCondA:
2930 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2931 // Simulate lhs <= rhs via lhs < rhs + 1.
2932 // Note that this only works if rhs + 1 does not overflow
2933 // to 0, hence the check above.
2934 // Sltiu sign-extends its 16-bit immediate operand before
2935 // the comparison and thus lets us compare directly with
2936 // unsigned values in the ranges [0, 0x7fff] and
2937 // [0xffff8000, 0xffffffff].
2938 __ Sltiu(dst, lhs, rhs_imm + 1);
2939 if (cond == kCondA) {
2940 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2941 // only the sltiu instruction but no sgtiu.
2942 __ Xori(dst, dst, 1);
2943 }
2944 } else {
2945 if (use_imm) {
2946 rhs_reg = TMP;
2947 __ LoadConst32(rhs_reg, rhs_imm);
2948 }
2949 __ Sltu(dst, rhs_reg, lhs);
2950 if (cond == kCondBE) {
2951 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2952 // only the sltu instruction but no sleu.
2953 __ Xori(dst, dst, 1);
2954 }
2955 }
2956 break;
2957 }
2958}
2959
2960void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2961 LocationSummary* locations,
2962 MipsLabel* label) {
2963 Register lhs = locations->InAt(0).AsRegister<Register>();
2964 Location rhs_location = locations->InAt(1);
2965 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07002966 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002967 bool use_imm = rhs_location.IsConstant();
2968 if (use_imm) {
2969 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2970 } else {
2971 rhs_reg = rhs_location.AsRegister<Register>();
2972 }
2973
2974 if (use_imm && rhs_imm == 0) {
2975 switch (cond) {
2976 case kCondEQ:
2977 case kCondBE: // <= 0 if zero
2978 __ Beqz(lhs, label);
2979 break;
2980 case kCondNE:
2981 case kCondA: // > 0 if non-zero
2982 __ Bnez(lhs, label);
2983 break;
2984 case kCondLT:
2985 __ Bltz(lhs, label);
2986 break;
2987 case kCondGE:
2988 __ Bgez(lhs, label);
2989 break;
2990 case kCondLE:
2991 __ Blez(lhs, label);
2992 break;
2993 case kCondGT:
2994 __ Bgtz(lhs, label);
2995 break;
2996 case kCondB: // always false
2997 break;
2998 case kCondAE: // always true
2999 __ B(label);
3000 break;
3001 }
3002 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003003 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3004 if (isR6 || !use_imm) {
3005 if (use_imm) {
3006 rhs_reg = TMP;
3007 __ LoadConst32(rhs_reg, rhs_imm);
3008 }
3009 switch (cond) {
3010 case kCondEQ:
3011 __ Beq(lhs, rhs_reg, label);
3012 break;
3013 case kCondNE:
3014 __ Bne(lhs, rhs_reg, label);
3015 break;
3016 case kCondLT:
3017 __ Blt(lhs, rhs_reg, label);
3018 break;
3019 case kCondGE:
3020 __ Bge(lhs, rhs_reg, label);
3021 break;
3022 case kCondLE:
3023 __ Bge(rhs_reg, lhs, label);
3024 break;
3025 case kCondGT:
3026 __ Blt(rhs_reg, lhs, label);
3027 break;
3028 case kCondB:
3029 __ Bltu(lhs, rhs_reg, label);
3030 break;
3031 case kCondAE:
3032 __ Bgeu(lhs, rhs_reg, label);
3033 break;
3034 case kCondBE:
3035 __ Bgeu(rhs_reg, lhs, label);
3036 break;
3037 case kCondA:
3038 __ Bltu(rhs_reg, lhs, label);
3039 break;
3040 }
3041 } else {
3042 // Special cases for more efficient comparison with constants on R2.
3043 switch (cond) {
3044 case kCondEQ:
3045 __ LoadConst32(TMP, rhs_imm);
3046 __ Beq(lhs, TMP, label);
3047 break;
3048 case kCondNE:
3049 __ LoadConst32(TMP, rhs_imm);
3050 __ Bne(lhs, TMP, label);
3051 break;
3052 case kCondLT:
3053 if (IsInt<16>(rhs_imm)) {
3054 __ Slti(TMP, lhs, rhs_imm);
3055 __ Bnez(TMP, label);
3056 } else {
3057 __ LoadConst32(TMP, rhs_imm);
3058 __ Blt(lhs, TMP, label);
3059 }
3060 break;
3061 case kCondGE:
3062 if (IsInt<16>(rhs_imm)) {
3063 __ Slti(TMP, lhs, rhs_imm);
3064 __ Beqz(TMP, label);
3065 } else {
3066 __ LoadConst32(TMP, rhs_imm);
3067 __ Bge(lhs, TMP, label);
3068 }
3069 break;
3070 case kCondLE:
3071 if (IsInt<16>(rhs_imm + 1)) {
3072 // Simulate lhs <= rhs via lhs < rhs + 1.
3073 __ Slti(TMP, lhs, rhs_imm + 1);
3074 __ Bnez(TMP, label);
3075 } else {
3076 __ LoadConst32(TMP, rhs_imm);
3077 __ Bge(TMP, lhs, label);
3078 }
3079 break;
3080 case kCondGT:
3081 if (IsInt<16>(rhs_imm + 1)) {
3082 // Simulate lhs > rhs via !(lhs < rhs + 1).
3083 __ Slti(TMP, lhs, rhs_imm + 1);
3084 __ Beqz(TMP, label);
3085 } else {
3086 __ LoadConst32(TMP, rhs_imm);
3087 __ Blt(TMP, lhs, label);
3088 }
3089 break;
3090 case kCondB:
3091 if (IsInt<16>(rhs_imm)) {
3092 __ Sltiu(TMP, lhs, rhs_imm);
3093 __ Bnez(TMP, label);
3094 } else {
3095 __ LoadConst32(TMP, rhs_imm);
3096 __ Bltu(lhs, TMP, label);
3097 }
3098 break;
3099 case kCondAE:
3100 if (IsInt<16>(rhs_imm)) {
3101 __ Sltiu(TMP, lhs, rhs_imm);
3102 __ Beqz(TMP, label);
3103 } else {
3104 __ LoadConst32(TMP, rhs_imm);
3105 __ Bgeu(lhs, TMP, label);
3106 }
3107 break;
3108 case kCondBE:
3109 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3110 // Simulate lhs <= rhs via lhs < rhs + 1.
3111 // Note that this only works if rhs + 1 does not overflow
3112 // to 0, hence the check above.
3113 __ Sltiu(TMP, lhs, rhs_imm + 1);
3114 __ Bnez(TMP, label);
3115 } else {
3116 __ LoadConst32(TMP, rhs_imm);
3117 __ Bgeu(TMP, lhs, label);
3118 }
3119 break;
3120 case kCondA:
3121 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3122 // Simulate lhs > rhs via !(lhs < rhs + 1).
3123 // Note that this only works if rhs + 1 does not overflow
3124 // to 0, hence the check above.
3125 __ Sltiu(TMP, lhs, rhs_imm + 1);
3126 __ Beqz(TMP, label);
3127 } else {
3128 __ LoadConst32(TMP, rhs_imm);
3129 __ Bltu(TMP, lhs, label);
3130 }
3131 break;
3132 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003133 }
3134 }
3135}
3136
3137void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3138 LocationSummary* locations,
3139 MipsLabel* label) {
3140 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3141 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3142 Location rhs_location = locations->InAt(1);
3143 Register rhs_high = ZERO;
3144 Register rhs_low = ZERO;
3145 int64_t imm = 0;
3146 uint32_t imm_high = 0;
3147 uint32_t imm_low = 0;
3148 bool use_imm = rhs_location.IsConstant();
3149 if (use_imm) {
3150 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3151 imm_high = High32Bits(imm);
3152 imm_low = Low32Bits(imm);
3153 } else {
3154 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3155 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3156 }
3157
3158 if (use_imm && imm == 0) {
3159 switch (cond) {
3160 case kCondEQ:
3161 case kCondBE: // <= 0 if zero
3162 __ Or(TMP, lhs_high, lhs_low);
3163 __ Beqz(TMP, label);
3164 break;
3165 case kCondNE:
3166 case kCondA: // > 0 if non-zero
3167 __ Or(TMP, lhs_high, lhs_low);
3168 __ Bnez(TMP, label);
3169 break;
3170 case kCondLT:
3171 __ Bltz(lhs_high, label);
3172 break;
3173 case kCondGE:
3174 __ Bgez(lhs_high, label);
3175 break;
3176 case kCondLE:
3177 __ Or(TMP, lhs_high, lhs_low);
3178 __ Sra(AT, lhs_high, 31);
3179 __ Bgeu(AT, TMP, label);
3180 break;
3181 case kCondGT:
3182 __ Or(TMP, lhs_high, lhs_low);
3183 __ Sra(AT, lhs_high, 31);
3184 __ Bltu(AT, TMP, label);
3185 break;
3186 case kCondB: // always false
3187 break;
3188 case kCondAE: // always true
3189 __ B(label);
3190 break;
3191 }
3192 } else if (use_imm) {
3193 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3194 switch (cond) {
3195 case kCondEQ:
3196 __ LoadConst32(TMP, imm_high);
3197 __ Xor(TMP, TMP, lhs_high);
3198 __ LoadConst32(AT, imm_low);
3199 __ Xor(AT, AT, lhs_low);
3200 __ Or(TMP, TMP, AT);
3201 __ Beqz(TMP, label);
3202 break;
3203 case kCondNE:
3204 __ LoadConst32(TMP, imm_high);
3205 __ Xor(TMP, TMP, lhs_high);
3206 __ LoadConst32(AT, imm_low);
3207 __ Xor(AT, AT, lhs_low);
3208 __ Or(TMP, TMP, AT);
3209 __ Bnez(TMP, label);
3210 break;
3211 case kCondLT:
3212 __ LoadConst32(TMP, imm_high);
3213 __ Blt(lhs_high, TMP, label);
3214 __ Slt(TMP, TMP, lhs_high);
3215 __ LoadConst32(AT, imm_low);
3216 __ Sltu(AT, lhs_low, AT);
3217 __ Blt(TMP, AT, label);
3218 break;
3219 case kCondGE:
3220 __ LoadConst32(TMP, imm_high);
3221 __ Blt(TMP, lhs_high, label);
3222 __ Slt(TMP, lhs_high, TMP);
3223 __ LoadConst32(AT, imm_low);
3224 __ Sltu(AT, lhs_low, AT);
3225 __ Or(TMP, TMP, AT);
3226 __ Beqz(TMP, label);
3227 break;
3228 case kCondLE:
3229 __ LoadConst32(TMP, imm_high);
3230 __ Blt(lhs_high, TMP, label);
3231 __ Slt(TMP, TMP, lhs_high);
3232 __ LoadConst32(AT, imm_low);
3233 __ Sltu(AT, AT, lhs_low);
3234 __ Or(TMP, TMP, AT);
3235 __ Beqz(TMP, label);
3236 break;
3237 case kCondGT:
3238 __ LoadConst32(TMP, imm_high);
3239 __ Blt(TMP, lhs_high, label);
3240 __ Slt(TMP, lhs_high, TMP);
3241 __ LoadConst32(AT, imm_low);
3242 __ Sltu(AT, AT, lhs_low);
3243 __ Blt(TMP, AT, label);
3244 break;
3245 case kCondB:
3246 __ LoadConst32(TMP, imm_high);
3247 __ Bltu(lhs_high, TMP, label);
3248 __ Sltu(TMP, TMP, lhs_high);
3249 __ LoadConst32(AT, imm_low);
3250 __ Sltu(AT, lhs_low, AT);
3251 __ Blt(TMP, AT, label);
3252 break;
3253 case kCondAE:
3254 __ LoadConst32(TMP, imm_high);
3255 __ Bltu(TMP, lhs_high, label);
3256 __ Sltu(TMP, lhs_high, TMP);
3257 __ LoadConst32(AT, imm_low);
3258 __ Sltu(AT, lhs_low, AT);
3259 __ Or(TMP, TMP, AT);
3260 __ Beqz(TMP, label);
3261 break;
3262 case kCondBE:
3263 __ LoadConst32(TMP, imm_high);
3264 __ Bltu(lhs_high, TMP, label);
3265 __ Sltu(TMP, TMP, lhs_high);
3266 __ LoadConst32(AT, imm_low);
3267 __ Sltu(AT, AT, lhs_low);
3268 __ Or(TMP, TMP, AT);
3269 __ Beqz(TMP, label);
3270 break;
3271 case kCondA:
3272 __ LoadConst32(TMP, imm_high);
3273 __ Bltu(TMP, lhs_high, label);
3274 __ Sltu(TMP, lhs_high, TMP);
3275 __ LoadConst32(AT, imm_low);
3276 __ Sltu(AT, AT, lhs_low);
3277 __ Blt(TMP, AT, label);
3278 break;
3279 }
3280 } else {
3281 switch (cond) {
3282 case kCondEQ:
3283 __ Xor(TMP, lhs_high, rhs_high);
3284 __ Xor(AT, lhs_low, rhs_low);
3285 __ Or(TMP, TMP, AT);
3286 __ Beqz(TMP, label);
3287 break;
3288 case kCondNE:
3289 __ Xor(TMP, lhs_high, rhs_high);
3290 __ Xor(AT, lhs_low, rhs_low);
3291 __ Or(TMP, TMP, AT);
3292 __ Bnez(TMP, label);
3293 break;
3294 case kCondLT:
3295 __ Blt(lhs_high, rhs_high, label);
3296 __ Slt(TMP, rhs_high, lhs_high);
3297 __ Sltu(AT, lhs_low, rhs_low);
3298 __ Blt(TMP, AT, label);
3299 break;
3300 case kCondGE:
3301 __ Blt(rhs_high, lhs_high, label);
3302 __ Slt(TMP, lhs_high, rhs_high);
3303 __ Sltu(AT, lhs_low, rhs_low);
3304 __ Or(TMP, TMP, AT);
3305 __ Beqz(TMP, label);
3306 break;
3307 case kCondLE:
3308 __ Blt(lhs_high, rhs_high, label);
3309 __ Slt(TMP, rhs_high, lhs_high);
3310 __ Sltu(AT, rhs_low, lhs_low);
3311 __ Or(TMP, TMP, AT);
3312 __ Beqz(TMP, label);
3313 break;
3314 case kCondGT:
3315 __ Blt(rhs_high, lhs_high, label);
3316 __ Slt(TMP, lhs_high, rhs_high);
3317 __ Sltu(AT, rhs_low, lhs_low);
3318 __ Blt(TMP, AT, label);
3319 break;
3320 case kCondB:
3321 __ Bltu(lhs_high, rhs_high, label);
3322 __ Sltu(TMP, rhs_high, lhs_high);
3323 __ Sltu(AT, lhs_low, rhs_low);
3324 __ Blt(TMP, AT, label);
3325 break;
3326 case kCondAE:
3327 __ Bltu(rhs_high, lhs_high, label);
3328 __ Sltu(TMP, lhs_high, rhs_high);
3329 __ Sltu(AT, lhs_low, rhs_low);
3330 __ Or(TMP, TMP, AT);
3331 __ Beqz(TMP, label);
3332 break;
3333 case kCondBE:
3334 __ Bltu(lhs_high, rhs_high, label);
3335 __ Sltu(TMP, rhs_high, lhs_high);
3336 __ Sltu(AT, rhs_low, lhs_low);
3337 __ Or(TMP, TMP, AT);
3338 __ Beqz(TMP, label);
3339 break;
3340 case kCondA:
3341 __ Bltu(rhs_high, lhs_high, label);
3342 __ Sltu(TMP, lhs_high, rhs_high);
3343 __ Sltu(AT, rhs_low, lhs_low);
3344 __ Blt(TMP, AT, label);
3345 break;
3346 }
3347 }
3348}
3349
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003350void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3351 bool gt_bias,
3352 Primitive::Type type,
3353 LocationSummary* locations) {
3354 Register dst = locations->Out().AsRegister<Register>();
3355 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3356 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3357 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3358 if (type == Primitive::kPrimFloat) {
3359 if (isR6) {
3360 switch (cond) {
3361 case kCondEQ:
3362 __ CmpEqS(FTMP, lhs, rhs);
3363 __ Mfc1(dst, FTMP);
3364 __ Andi(dst, dst, 1);
3365 break;
3366 case kCondNE:
3367 __ CmpEqS(FTMP, lhs, rhs);
3368 __ Mfc1(dst, FTMP);
3369 __ Addiu(dst, dst, 1);
3370 break;
3371 case kCondLT:
3372 if (gt_bias) {
3373 __ CmpLtS(FTMP, lhs, rhs);
3374 } else {
3375 __ CmpUltS(FTMP, lhs, rhs);
3376 }
3377 __ Mfc1(dst, FTMP);
3378 __ Andi(dst, dst, 1);
3379 break;
3380 case kCondLE:
3381 if (gt_bias) {
3382 __ CmpLeS(FTMP, lhs, rhs);
3383 } else {
3384 __ CmpUleS(FTMP, lhs, rhs);
3385 }
3386 __ Mfc1(dst, FTMP);
3387 __ Andi(dst, dst, 1);
3388 break;
3389 case kCondGT:
3390 if (gt_bias) {
3391 __ CmpUltS(FTMP, rhs, lhs);
3392 } else {
3393 __ CmpLtS(FTMP, rhs, lhs);
3394 }
3395 __ Mfc1(dst, FTMP);
3396 __ Andi(dst, dst, 1);
3397 break;
3398 case kCondGE:
3399 if (gt_bias) {
3400 __ CmpUleS(FTMP, rhs, lhs);
3401 } else {
3402 __ CmpLeS(FTMP, rhs, lhs);
3403 }
3404 __ Mfc1(dst, FTMP);
3405 __ Andi(dst, dst, 1);
3406 break;
3407 default:
3408 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3409 UNREACHABLE();
3410 }
3411 } else {
3412 switch (cond) {
3413 case kCondEQ:
3414 __ CeqS(0, lhs, rhs);
3415 __ LoadConst32(dst, 1);
3416 __ Movf(dst, ZERO, 0);
3417 break;
3418 case kCondNE:
3419 __ CeqS(0, lhs, rhs);
3420 __ LoadConst32(dst, 1);
3421 __ Movt(dst, ZERO, 0);
3422 break;
3423 case kCondLT:
3424 if (gt_bias) {
3425 __ ColtS(0, lhs, rhs);
3426 } else {
3427 __ CultS(0, lhs, rhs);
3428 }
3429 __ LoadConst32(dst, 1);
3430 __ Movf(dst, ZERO, 0);
3431 break;
3432 case kCondLE:
3433 if (gt_bias) {
3434 __ ColeS(0, lhs, rhs);
3435 } else {
3436 __ CuleS(0, lhs, rhs);
3437 }
3438 __ LoadConst32(dst, 1);
3439 __ Movf(dst, ZERO, 0);
3440 break;
3441 case kCondGT:
3442 if (gt_bias) {
3443 __ CultS(0, rhs, lhs);
3444 } else {
3445 __ ColtS(0, rhs, lhs);
3446 }
3447 __ LoadConst32(dst, 1);
3448 __ Movf(dst, ZERO, 0);
3449 break;
3450 case kCondGE:
3451 if (gt_bias) {
3452 __ CuleS(0, rhs, lhs);
3453 } else {
3454 __ ColeS(0, rhs, lhs);
3455 }
3456 __ LoadConst32(dst, 1);
3457 __ Movf(dst, ZERO, 0);
3458 break;
3459 default:
3460 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3461 UNREACHABLE();
3462 }
3463 }
3464 } else {
3465 DCHECK_EQ(type, Primitive::kPrimDouble);
3466 if (isR6) {
3467 switch (cond) {
3468 case kCondEQ:
3469 __ CmpEqD(FTMP, lhs, rhs);
3470 __ Mfc1(dst, FTMP);
3471 __ Andi(dst, dst, 1);
3472 break;
3473 case kCondNE:
3474 __ CmpEqD(FTMP, lhs, rhs);
3475 __ Mfc1(dst, FTMP);
3476 __ Addiu(dst, dst, 1);
3477 break;
3478 case kCondLT:
3479 if (gt_bias) {
3480 __ CmpLtD(FTMP, lhs, rhs);
3481 } else {
3482 __ CmpUltD(FTMP, lhs, rhs);
3483 }
3484 __ Mfc1(dst, FTMP);
3485 __ Andi(dst, dst, 1);
3486 break;
3487 case kCondLE:
3488 if (gt_bias) {
3489 __ CmpLeD(FTMP, lhs, rhs);
3490 } else {
3491 __ CmpUleD(FTMP, lhs, rhs);
3492 }
3493 __ Mfc1(dst, FTMP);
3494 __ Andi(dst, dst, 1);
3495 break;
3496 case kCondGT:
3497 if (gt_bias) {
3498 __ CmpUltD(FTMP, rhs, lhs);
3499 } else {
3500 __ CmpLtD(FTMP, rhs, lhs);
3501 }
3502 __ Mfc1(dst, FTMP);
3503 __ Andi(dst, dst, 1);
3504 break;
3505 case kCondGE:
3506 if (gt_bias) {
3507 __ CmpUleD(FTMP, rhs, lhs);
3508 } else {
3509 __ CmpLeD(FTMP, rhs, lhs);
3510 }
3511 __ Mfc1(dst, FTMP);
3512 __ Andi(dst, dst, 1);
3513 break;
3514 default:
3515 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3516 UNREACHABLE();
3517 }
3518 } else {
3519 switch (cond) {
3520 case kCondEQ:
3521 __ CeqD(0, lhs, rhs);
3522 __ LoadConst32(dst, 1);
3523 __ Movf(dst, ZERO, 0);
3524 break;
3525 case kCondNE:
3526 __ CeqD(0, lhs, rhs);
3527 __ LoadConst32(dst, 1);
3528 __ Movt(dst, ZERO, 0);
3529 break;
3530 case kCondLT:
3531 if (gt_bias) {
3532 __ ColtD(0, lhs, rhs);
3533 } else {
3534 __ CultD(0, lhs, rhs);
3535 }
3536 __ LoadConst32(dst, 1);
3537 __ Movf(dst, ZERO, 0);
3538 break;
3539 case kCondLE:
3540 if (gt_bias) {
3541 __ ColeD(0, lhs, rhs);
3542 } else {
3543 __ CuleD(0, lhs, rhs);
3544 }
3545 __ LoadConst32(dst, 1);
3546 __ Movf(dst, ZERO, 0);
3547 break;
3548 case kCondGT:
3549 if (gt_bias) {
3550 __ CultD(0, rhs, lhs);
3551 } else {
3552 __ ColtD(0, rhs, lhs);
3553 }
3554 __ LoadConst32(dst, 1);
3555 __ Movf(dst, ZERO, 0);
3556 break;
3557 case kCondGE:
3558 if (gt_bias) {
3559 __ CuleD(0, rhs, lhs);
3560 } else {
3561 __ ColeD(0, rhs, lhs);
3562 }
3563 __ LoadConst32(dst, 1);
3564 __ Movf(dst, ZERO, 0);
3565 break;
3566 default:
3567 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3568 UNREACHABLE();
3569 }
3570 }
3571 }
3572}
3573
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003574void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3575 bool gt_bias,
3576 Primitive::Type type,
3577 LocationSummary* locations,
3578 MipsLabel* label) {
3579 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3580 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3581 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3582 if (type == Primitive::kPrimFloat) {
3583 if (isR6) {
3584 switch (cond) {
3585 case kCondEQ:
3586 __ CmpEqS(FTMP, lhs, rhs);
3587 __ Bc1nez(FTMP, label);
3588 break;
3589 case kCondNE:
3590 __ CmpEqS(FTMP, lhs, rhs);
3591 __ Bc1eqz(FTMP, label);
3592 break;
3593 case kCondLT:
3594 if (gt_bias) {
3595 __ CmpLtS(FTMP, lhs, rhs);
3596 } else {
3597 __ CmpUltS(FTMP, lhs, rhs);
3598 }
3599 __ Bc1nez(FTMP, label);
3600 break;
3601 case kCondLE:
3602 if (gt_bias) {
3603 __ CmpLeS(FTMP, lhs, rhs);
3604 } else {
3605 __ CmpUleS(FTMP, lhs, rhs);
3606 }
3607 __ Bc1nez(FTMP, label);
3608 break;
3609 case kCondGT:
3610 if (gt_bias) {
3611 __ CmpUltS(FTMP, rhs, lhs);
3612 } else {
3613 __ CmpLtS(FTMP, rhs, lhs);
3614 }
3615 __ Bc1nez(FTMP, label);
3616 break;
3617 case kCondGE:
3618 if (gt_bias) {
3619 __ CmpUleS(FTMP, rhs, lhs);
3620 } else {
3621 __ CmpLeS(FTMP, rhs, lhs);
3622 }
3623 __ Bc1nez(FTMP, label);
3624 break;
3625 default:
3626 LOG(FATAL) << "Unexpected non-floating-point condition";
3627 }
3628 } else {
3629 switch (cond) {
3630 case kCondEQ:
3631 __ CeqS(0, lhs, rhs);
3632 __ Bc1t(0, label);
3633 break;
3634 case kCondNE:
3635 __ CeqS(0, lhs, rhs);
3636 __ Bc1f(0, label);
3637 break;
3638 case kCondLT:
3639 if (gt_bias) {
3640 __ ColtS(0, lhs, rhs);
3641 } else {
3642 __ CultS(0, lhs, rhs);
3643 }
3644 __ Bc1t(0, label);
3645 break;
3646 case kCondLE:
3647 if (gt_bias) {
3648 __ ColeS(0, lhs, rhs);
3649 } else {
3650 __ CuleS(0, lhs, rhs);
3651 }
3652 __ Bc1t(0, label);
3653 break;
3654 case kCondGT:
3655 if (gt_bias) {
3656 __ CultS(0, rhs, lhs);
3657 } else {
3658 __ ColtS(0, rhs, lhs);
3659 }
3660 __ Bc1t(0, label);
3661 break;
3662 case kCondGE:
3663 if (gt_bias) {
3664 __ CuleS(0, rhs, lhs);
3665 } else {
3666 __ ColeS(0, rhs, lhs);
3667 }
3668 __ Bc1t(0, label);
3669 break;
3670 default:
3671 LOG(FATAL) << "Unexpected non-floating-point condition";
3672 }
3673 }
3674 } else {
3675 DCHECK_EQ(type, Primitive::kPrimDouble);
3676 if (isR6) {
3677 switch (cond) {
3678 case kCondEQ:
3679 __ CmpEqD(FTMP, lhs, rhs);
3680 __ Bc1nez(FTMP, label);
3681 break;
3682 case kCondNE:
3683 __ CmpEqD(FTMP, lhs, rhs);
3684 __ Bc1eqz(FTMP, label);
3685 break;
3686 case kCondLT:
3687 if (gt_bias) {
3688 __ CmpLtD(FTMP, lhs, rhs);
3689 } else {
3690 __ CmpUltD(FTMP, lhs, rhs);
3691 }
3692 __ Bc1nez(FTMP, label);
3693 break;
3694 case kCondLE:
3695 if (gt_bias) {
3696 __ CmpLeD(FTMP, lhs, rhs);
3697 } else {
3698 __ CmpUleD(FTMP, lhs, rhs);
3699 }
3700 __ Bc1nez(FTMP, label);
3701 break;
3702 case kCondGT:
3703 if (gt_bias) {
3704 __ CmpUltD(FTMP, rhs, lhs);
3705 } else {
3706 __ CmpLtD(FTMP, rhs, lhs);
3707 }
3708 __ Bc1nez(FTMP, label);
3709 break;
3710 case kCondGE:
3711 if (gt_bias) {
3712 __ CmpUleD(FTMP, rhs, lhs);
3713 } else {
3714 __ CmpLeD(FTMP, rhs, lhs);
3715 }
3716 __ Bc1nez(FTMP, label);
3717 break;
3718 default:
3719 LOG(FATAL) << "Unexpected non-floating-point condition";
3720 }
3721 } else {
3722 switch (cond) {
3723 case kCondEQ:
3724 __ CeqD(0, lhs, rhs);
3725 __ Bc1t(0, label);
3726 break;
3727 case kCondNE:
3728 __ CeqD(0, lhs, rhs);
3729 __ Bc1f(0, label);
3730 break;
3731 case kCondLT:
3732 if (gt_bias) {
3733 __ ColtD(0, lhs, rhs);
3734 } else {
3735 __ CultD(0, lhs, rhs);
3736 }
3737 __ Bc1t(0, label);
3738 break;
3739 case kCondLE:
3740 if (gt_bias) {
3741 __ ColeD(0, lhs, rhs);
3742 } else {
3743 __ CuleD(0, lhs, rhs);
3744 }
3745 __ Bc1t(0, label);
3746 break;
3747 case kCondGT:
3748 if (gt_bias) {
3749 __ CultD(0, rhs, lhs);
3750 } else {
3751 __ ColtD(0, rhs, lhs);
3752 }
3753 __ Bc1t(0, label);
3754 break;
3755 case kCondGE:
3756 if (gt_bias) {
3757 __ CuleD(0, rhs, lhs);
3758 } else {
3759 __ ColeD(0, rhs, lhs);
3760 }
3761 __ Bc1t(0, label);
3762 break;
3763 default:
3764 LOG(FATAL) << "Unexpected non-floating-point condition";
3765 }
3766 }
3767 }
3768}
3769
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003770void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003771 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003772 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003773 MipsLabel* false_target) {
3774 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003775
David Brazdil0debae72015-11-12 18:37:00 +00003776 if (true_target == nullptr && false_target == nullptr) {
3777 // Nothing to do. The code always falls through.
3778 return;
3779 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003780 // Constant condition, statically compared against "true" (integer value 1).
3781 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003782 if (true_target != nullptr) {
3783 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003784 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003785 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003786 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003787 if (false_target != nullptr) {
3788 __ B(false_target);
3789 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003790 }
David Brazdil0debae72015-11-12 18:37:00 +00003791 return;
3792 }
3793
3794 // The following code generates these patterns:
3795 // (1) true_target == nullptr && false_target != nullptr
3796 // - opposite condition true => branch to false_target
3797 // (2) true_target != nullptr && false_target == nullptr
3798 // - condition true => branch to true_target
3799 // (3) true_target != nullptr && false_target != nullptr
3800 // - condition true => branch to true_target
3801 // - branch to false_target
3802 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003803 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003804 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003805 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003806 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003807 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3808 } else {
3809 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3810 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003811 } else {
3812 // The condition instruction has not been materialized, use its inputs as
3813 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003814 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003815 Primitive::Type type = condition->InputAt(0)->GetType();
3816 LocationSummary* locations = cond->GetLocations();
3817 IfCondition if_cond = condition->GetCondition();
3818 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003819
David Brazdil0debae72015-11-12 18:37:00 +00003820 if (true_target == nullptr) {
3821 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003822 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003823 }
3824
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003825 switch (type) {
3826 default:
3827 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3828 break;
3829 case Primitive::kPrimLong:
3830 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3831 break;
3832 case Primitive::kPrimFloat:
3833 case Primitive::kPrimDouble:
3834 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3835 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003836 }
3837 }
David Brazdil0debae72015-11-12 18:37:00 +00003838
3839 // If neither branch falls through (case 3), the conditional branch to `true_target`
3840 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3841 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003842 __ B(false_target);
3843 }
3844}
3845
3846void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3847 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003848 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003849 locations->SetInAt(0, Location::RequiresRegister());
3850 }
3851}
3852
3853void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003854 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3855 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3856 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3857 nullptr : codegen_->GetLabelOf(true_successor);
3858 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3859 nullptr : codegen_->GetLabelOf(false_successor);
3860 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003861}
3862
3863void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3864 LocationSummary* locations = new (GetGraph()->GetArena())
3865 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003866 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003867 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003868 locations->SetInAt(0, Location::RequiresRegister());
3869 }
3870}
3871
3872void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003873 SlowPathCodeMIPS* slow_path =
3874 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003875 GenerateTestAndBranch(deoptimize,
3876 /* condition_input_index */ 0,
3877 slow_path->GetEntryLabel(),
3878 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003879}
3880
David Brazdil74eb1b22015-12-14 11:44:01 +00003881void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3883 if (Primitive::IsFloatingPointType(select->GetType())) {
3884 locations->SetInAt(0, Location::RequiresFpuRegister());
3885 locations->SetInAt(1, Location::RequiresFpuRegister());
3886 } else {
3887 locations->SetInAt(0, Location::RequiresRegister());
3888 locations->SetInAt(1, Location::RequiresRegister());
3889 }
3890 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3891 locations->SetInAt(2, Location::RequiresRegister());
3892 }
3893 locations->SetOut(Location::SameAsFirstInput());
3894}
3895
3896void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3897 LocationSummary* locations = select->GetLocations();
3898 MipsLabel false_target;
3899 GenerateTestAndBranch(select,
3900 /* condition_input_index */ 2,
3901 /* true_target */ nullptr,
3902 &false_target);
3903 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3904 __ Bind(&false_target);
3905}
3906
David Srbecky0cf44932015-12-09 14:09:59 +00003907void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3908 new (GetGraph()->GetArena()) LocationSummary(info);
3909}
3910
David Srbeckyd28f4a02016-03-14 17:14:24 +00003911void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3912 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003913}
3914
3915void CodeGeneratorMIPS::GenerateNop() {
3916 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003917}
3918
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003919void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3920 Primitive::Type field_type = field_info.GetFieldType();
3921 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3922 bool generate_volatile = field_info.IsVolatile() && is_wide;
3923 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003924 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003925
3926 locations->SetInAt(0, Location::RequiresRegister());
3927 if (generate_volatile) {
3928 InvokeRuntimeCallingConvention calling_convention;
3929 // need A0 to hold base + offset
3930 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3931 if (field_type == Primitive::kPrimLong) {
3932 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3933 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003934 // Use Location::Any() to prevent situations when running out of available fp registers.
3935 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003936 // Need some temp core regs since FP results are returned in core registers
3937 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3938 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3939 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3940 }
3941 } else {
3942 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3943 locations->SetOut(Location::RequiresFpuRegister());
3944 } else {
3945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3946 }
3947 }
3948}
3949
3950void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3951 const FieldInfo& field_info,
3952 uint32_t dex_pc) {
3953 Primitive::Type type = field_info.GetFieldType();
3954 LocationSummary* locations = instruction->GetLocations();
3955 Register obj = locations->InAt(0).AsRegister<Register>();
3956 LoadOperandType load_type = kLoadUnsignedByte;
3957 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003958 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003959 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003960
3961 switch (type) {
3962 case Primitive::kPrimBoolean:
3963 load_type = kLoadUnsignedByte;
3964 break;
3965 case Primitive::kPrimByte:
3966 load_type = kLoadSignedByte;
3967 break;
3968 case Primitive::kPrimShort:
3969 load_type = kLoadSignedHalfword;
3970 break;
3971 case Primitive::kPrimChar:
3972 load_type = kLoadUnsignedHalfword;
3973 break;
3974 case Primitive::kPrimInt:
3975 case Primitive::kPrimFloat:
3976 case Primitive::kPrimNot:
3977 load_type = kLoadWord;
3978 break;
3979 case Primitive::kPrimLong:
3980 case Primitive::kPrimDouble:
3981 load_type = kLoadDoubleword;
3982 break;
3983 case Primitive::kPrimVoid:
3984 LOG(FATAL) << "Unreachable type " << type;
3985 UNREACHABLE();
3986 }
3987
3988 if (is_volatile && load_type == kLoadDoubleword) {
3989 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003990 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003991 // Do implicit Null check
3992 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3993 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01003994 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003995 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3996 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003997 // FP results are returned in core registers. Need to move them.
3998 Location out = locations->Out();
3999 if (out.IsFpuRegister()) {
4000 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4001 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4002 out.AsFpuRegister<FRegister>());
4003 } else {
4004 DCHECK(out.IsDoubleStackSlot());
4005 __ StoreToOffset(kStoreWord,
4006 locations->GetTemp(1).AsRegister<Register>(),
4007 SP,
4008 out.GetStackIndex());
4009 __ StoreToOffset(kStoreWord,
4010 locations->GetTemp(2).AsRegister<Register>(),
4011 SP,
4012 out.GetStackIndex() + 4);
4013 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004014 }
4015 } else {
4016 if (!Primitive::IsFloatingPointType(type)) {
4017 Register dst;
4018 if (type == Primitive::kPrimLong) {
4019 DCHECK(locations->Out().IsRegisterPair());
4020 dst = locations->Out().AsRegisterPairLow<Register>();
4021 } else {
4022 DCHECK(locations->Out().IsRegister());
4023 dst = locations->Out().AsRegister<Register>();
4024 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004025 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004026 } else {
4027 DCHECK(locations->Out().IsFpuRegister());
4028 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4029 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004030 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004031 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004032 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004033 }
4034 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004035 }
4036
4037 if (is_volatile) {
4038 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4039 }
4040}
4041
4042void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4043 Primitive::Type field_type = field_info.GetFieldType();
4044 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4045 bool generate_volatile = field_info.IsVolatile() && is_wide;
4046 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004047 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004048
4049 locations->SetInAt(0, Location::RequiresRegister());
4050 if (generate_volatile) {
4051 InvokeRuntimeCallingConvention calling_convention;
4052 // need A0 to hold base + offset
4053 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4054 if (field_type == Primitive::kPrimLong) {
4055 locations->SetInAt(1, Location::RegisterPairLocation(
4056 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4057 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004058 // Use Location::Any() to prevent situations when running out of available fp registers.
4059 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004060 // Pass FP parameters in core registers.
4061 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4062 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4063 }
4064 } else {
4065 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004066 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004067 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004068 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004069 }
4070 }
4071}
4072
4073void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4074 const FieldInfo& field_info,
4075 uint32_t dex_pc) {
4076 Primitive::Type type = field_info.GetFieldType();
4077 LocationSummary* locations = instruction->GetLocations();
4078 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004079 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004080 StoreOperandType store_type = kStoreByte;
4081 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004082 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004083 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004084
4085 switch (type) {
4086 case Primitive::kPrimBoolean:
4087 case Primitive::kPrimByte:
4088 store_type = kStoreByte;
4089 break;
4090 case Primitive::kPrimShort:
4091 case Primitive::kPrimChar:
4092 store_type = kStoreHalfword;
4093 break;
4094 case Primitive::kPrimInt:
4095 case Primitive::kPrimFloat:
4096 case Primitive::kPrimNot:
4097 store_type = kStoreWord;
4098 break;
4099 case Primitive::kPrimLong:
4100 case Primitive::kPrimDouble:
4101 store_type = kStoreDoubleword;
4102 break;
4103 case Primitive::kPrimVoid:
4104 LOG(FATAL) << "Unreachable type " << type;
4105 UNREACHABLE();
4106 }
4107
4108 if (is_volatile) {
4109 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4110 }
4111
4112 if (is_volatile && store_type == kStoreDoubleword) {
4113 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004114 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004115 // Do implicit Null check.
4116 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4117 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4118 if (type == Primitive::kPrimDouble) {
4119 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004120 if (value_location.IsFpuRegister()) {
4121 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4122 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004123 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004124 value_location.AsFpuRegister<FRegister>());
4125 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004126 __ LoadFromOffset(kLoadWord,
4127 locations->GetTemp(1).AsRegister<Register>(),
4128 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004129 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004130 __ LoadFromOffset(kLoadWord,
4131 locations->GetTemp(2).AsRegister<Register>(),
4132 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004133 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004134 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004135 DCHECK(value_location.IsConstant());
4136 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4137 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004138 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4139 locations->GetTemp(1).AsRegister<Register>(),
4140 value);
4141 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004142 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004143 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004144 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4145 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004146 if (value_location.IsConstant()) {
4147 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4148 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4149 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004150 Register src;
4151 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004152 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004153 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004154 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004155 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004156 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004157 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004158 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004159 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004160 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004161 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004162 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004163 }
4164 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004165 }
4166
4167 // TODO: memory barriers?
4168 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004169 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004170 codegen_->MarkGCCard(obj, src);
4171 }
4172
4173 if (is_volatile) {
4174 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4175 }
4176}
4177
4178void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4179 HandleFieldGet(instruction, instruction->GetFieldInfo());
4180}
4181
4182void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4183 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4184}
4185
4186void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4187 HandleFieldSet(instruction, instruction->GetFieldInfo());
4188}
4189
4190void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4191 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4192}
4193
Alexey Frunze06a46c42016-07-19 15:00:40 -07004194void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
4195 HInstruction* instruction ATTRIBUTE_UNUSED,
4196 Location root,
4197 Register obj,
4198 uint32_t offset) {
4199 Register root_reg = root.AsRegister<Register>();
4200 if (kEmitCompilerReadBarrier) {
4201 UNIMPLEMENTED(FATAL) << "for read barrier";
4202 } else {
4203 // Plain GC root load with no read barrier.
4204 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4205 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
4206 // Note that GC roots are not affected by heap poisoning, thus we
4207 // do not have to unpoison `root_reg` here.
4208 }
4209}
4210
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004211void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4212 LocationSummary::CallKind call_kind =
4213 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
4214 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4215 locations->SetInAt(0, Location::RequiresRegister());
4216 locations->SetInAt(1, Location::RequiresRegister());
4217 // The output does overlap inputs.
4218 // Note that TypeCheckSlowPathMIPS uses this register too.
4219 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4220}
4221
4222void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4223 LocationSummary* locations = instruction->GetLocations();
4224 Register obj = locations->InAt(0).AsRegister<Register>();
4225 Register cls = locations->InAt(1).AsRegister<Register>();
4226 Register out = locations->Out().AsRegister<Register>();
4227
4228 MipsLabel done;
4229
4230 // Return 0 if `obj` is null.
4231 // TODO: Avoid this check if we know `obj` is not null.
4232 __ Move(out, ZERO);
4233 __ Beqz(obj, &done);
4234
4235 // Compare the class of `obj` with `cls`.
4236 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
4237 if (instruction->IsExactCheck()) {
4238 // Classes must be equal for the instanceof to succeed.
4239 __ Xor(out, out, cls);
4240 __ Sltiu(out, out, 1);
4241 } else {
4242 // If the classes are not equal, we go into a slow path.
4243 DCHECK(locations->OnlyCallsOnSlowPath());
4244 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
4245 codegen_->AddSlowPath(slow_path);
4246 __ Bne(out, cls, slow_path->GetEntryLabel());
4247 __ LoadConst32(out, 1);
4248 __ Bind(slow_path->GetExitLabel());
4249 }
4250
4251 __ Bind(&done);
4252}
4253
4254void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
4255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4256 locations->SetOut(Location::ConstantLocation(constant));
4257}
4258
4259void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4260 // Will be generated at use site.
4261}
4262
4263void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
4264 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4265 locations->SetOut(Location::ConstantLocation(constant));
4266}
4267
4268void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4269 // Will be generated at use site.
4270}
4271
4272void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
4273 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
4274 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4275}
4276
4277void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4278 HandleInvoke(invoke);
4279 // The register T0 is required to be used for the hidden argument in
4280 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4281 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4282}
4283
4284void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4285 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4286 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004287 Location receiver = invoke->GetLocations()->InAt(0);
4288 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004289 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004290
4291 // Set the hidden argument.
4292 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
4293 invoke->GetDexMethodIndex());
4294
4295 // temp = object->GetClass();
4296 if (receiver.IsStackSlot()) {
4297 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4298 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4299 } else {
4300 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4301 }
4302 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004303 __ LoadFromOffset(kLoadWord, temp, temp,
4304 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
4305 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004306 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004307 // temp = temp->GetImtEntryAt(method_offset);
4308 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4309 // T9 = temp->GetEntryPoint();
4310 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4311 // T9();
4312 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004313 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004314 DCHECK(!codegen_->IsLeafMethod());
4315 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4316}
4317
4318void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07004319 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4320 if (intrinsic.TryDispatch(invoke)) {
4321 return;
4322 }
4323
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004324 HandleInvoke(invoke);
4325}
4326
4327void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004328 // Explicit clinit checks triggered by static invokes must have been pruned by
4329 // art::PrepareForRegisterAllocation.
4330 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004331
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004332 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4333 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4334 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4335
4336 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
4337 // R6 has PC-relative addressing.
4338 bool has_extra_input = !isR6 &&
4339 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4340 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4341
4342 if (invoke->HasPcRelativeDexCache()) {
4343 // kDexCachePcRelative is mutually exclusive with
4344 // kDirectAddressWithFixup/kCallDirectWithFixup.
4345 CHECK(!has_extra_input);
4346 has_extra_input = true;
4347 }
4348
Chris Larsen701566a2015-10-27 15:29:13 -07004349 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4350 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004351 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4352 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4353 }
Chris Larsen701566a2015-10-27 15:29:13 -07004354 return;
4355 }
4356
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004357 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004358
4359 // Add the extra input register if either the dex cache array base register
4360 // or the PC-relative base register for accessing literals is needed.
4361 if (has_extra_input) {
4362 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4363 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004364}
4365
Chris Larsen701566a2015-10-27 15:29:13 -07004366static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004367 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004368 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4369 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004370 return true;
4371 }
4372 return false;
4373}
4374
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004375HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004376 HLoadString::LoadKind desired_string_load_kind) {
4377 if (kEmitCompilerReadBarrier) {
4378 UNIMPLEMENTED(FATAL) << "for read barrier";
4379 }
4380 // We disable PC-relative load when there is an irreducible loop, as the optimization
4381 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004382 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4383 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07004384 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4385 bool fallback_load = has_irreducible_loops;
4386 switch (desired_string_load_kind) {
4387 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4388 DCHECK(!GetCompilerOptions().GetCompilePic());
4389 break;
4390 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4391 DCHECK(GetCompilerOptions().GetCompilePic());
4392 break;
4393 case HLoadString::LoadKind::kBootImageAddress:
4394 break;
4395 case HLoadString::LoadKind::kDexCacheAddress:
4396 DCHECK(Runtime::Current()->UseJitCompilation());
4397 fallback_load = false;
4398 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004399 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004400 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004401 break;
4402 case HLoadString::LoadKind::kDexCacheViaMethod:
4403 fallback_load = false;
4404 break;
4405 }
4406 if (fallback_load) {
4407 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4408 }
4409 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004410}
4411
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004412HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4413 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004414 if (kEmitCompilerReadBarrier) {
4415 UNIMPLEMENTED(FATAL) << "for read barrier";
4416 }
4417 // We disable pc-relative load when there is an irreducible loop, as the optimization
4418 // is incompatible with it.
4419 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4420 bool fallback_load = has_irreducible_loops;
4421 switch (desired_class_load_kind) {
4422 case HLoadClass::LoadKind::kReferrersClass:
4423 fallback_load = false;
4424 break;
4425 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4426 DCHECK(!GetCompilerOptions().GetCompilePic());
4427 break;
4428 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4429 DCHECK(GetCompilerOptions().GetCompilePic());
4430 break;
4431 case HLoadClass::LoadKind::kBootImageAddress:
4432 break;
4433 case HLoadClass::LoadKind::kDexCacheAddress:
4434 DCHECK(Runtime::Current()->UseJitCompilation());
4435 fallback_load = false;
4436 break;
4437 case HLoadClass::LoadKind::kDexCachePcRelative:
4438 DCHECK(!Runtime::Current()->UseJitCompilation());
4439 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4440 // with irreducible loops.
4441 break;
4442 case HLoadClass::LoadKind::kDexCacheViaMethod:
4443 fallback_load = false;
4444 break;
4445 }
4446 if (fallback_load) {
4447 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4448 }
4449 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004450}
4451
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004452Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4453 Register temp) {
4454 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4455 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4456 if (!invoke->GetLocations()->Intrinsified()) {
4457 return location.AsRegister<Register>();
4458 }
4459 // For intrinsics we allow any location, so it may be on the stack.
4460 if (!location.IsRegister()) {
4461 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4462 return temp;
4463 }
4464 // For register locations, check if the register was saved. If so, get it from the stack.
4465 // Note: There is a chance that the register was saved but not overwritten, so we could
4466 // save one load. However, since this is just an intrinsic slow path we prefer this
4467 // simple and more robust approach rather that trying to determine if that's the case.
4468 SlowPathCode* slow_path = GetCurrentSlowPath();
4469 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4470 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4471 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4472 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4473 return temp;
4474 }
4475 return location.AsRegister<Register>();
4476}
4477
Vladimir Markodc151b22015-10-15 18:02:30 +01004478HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4479 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004480 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004481 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4482 // We disable PC-relative load when there is an irreducible loop, as the optimization
4483 // is incompatible with it.
4484 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4485 bool fallback_load = true;
4486 bool fallback_call = true;
4487 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004488 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4489 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004490 fallback_load = has_irreducible_loops;
4491 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004492 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004493 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004494 break;
4495 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004496 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004497 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004498 fallback_call = has_irreducible_loops;
4499 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004500 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004501 // TODO: Implement this type.
4502 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004503 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004504 fallback_call = false;
4505 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004506 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004507 if (fallback_load) {
4508 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4509 dispatch_info.method_load_data = 0;
4510 }
4511 if (fallback_call) {
4512 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4513 dispatch_info.direct_code_ptr = 0;
4514 }
4515 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004516}
4517
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004518void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4519 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004520 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004521 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4522 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4523 bool isR6 = isa_features_.IsR6();
4524 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4525 // R6 has PC-relative addressing.
4526 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4527 (!isR6 &&
4528 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4529 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4530 Register base_reg = has_extra_input
4531 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4532 : ZERO;
4533
4534 // For better instruction scheduling we load the direct code pointer before the method pointer.
4535 switch (code_ptr_location) {
4536 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4537 // T9 = invoke->GetDirectCodePtr();
4538 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4539 break;
4540 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4541 // T9 = code address from literal pool with link-time patch.
4542 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4543 break;
4544 default:
4545 break;
4546 }
4547
4548 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004549 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004550 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004551 uint32_t offset =
4552 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004553 __ LoadFromOffset(kLoadWord,
4554 temp.AsRegister<Register>(),
4555 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004556 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004557 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004558 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004559 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004560 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004561 break;
4562 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4563 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4564 break;
4565 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004566 __ LoadLiteral(temp.AsRegister<Register>(),
4567 base_reg,
4568 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4569 break;
4570 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4571 HMipsDexCacheArraysBase* base =
4572 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4573 int32_t offset =
4574 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4575 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4576 break;
4577 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004578 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004579 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004580 Register reg = temp.AsRegister<Register>();
4581 Register method_reg;
4582 if (current_method.IsRegister()) {
4583 method_reg = current_method.AsRegister<Register>();
4584 } else {
4585 // TODO: use the appropriate DCHECK() here if possible.
4586 // DCHECK(invoke->GetLocations()->Intrinsified());
4587 DCHECK(!current_method.IsValid());
4588 method_reg = reg;
4589 __ Lw(reg, SP, kCurrentMethodStackOffset);
4590 }
4591
4592 // temp = temp->dex_cache_resolved_methods_;
4593 __ LoadFromOffset(kLoadWord,
4594 reg,
4595 method_reg,
4596 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004597 // temp = temp[index_in_cache];
4598 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4599 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004600 __ LoadFromOffset(kLoadWord,
4601 reg,
4602 reg,
4603 CodeGenerator::GetCachePointerOffset(index_in_cache));
4604 break;
4605 }
4606 }
4607
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004608 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004609 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004610 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004611 break;
4612 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004613 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4614 // T9 prepared above for better instruction scheduling.
4615 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004616 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004617 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004618 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004619 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004620 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004621 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4622 LOG(FATAL) << "Unsupported";
4623 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004624 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4625 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004626 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004627 T9,
4628 callee_method.AsRegister<Register>(),
4629 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004630 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004631 // T9()
4632 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004633 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004634 break;
4635 }
4636 DCHECK(!IsLeafMethod());
4637}
4638
4639void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004640 // Explicit clinit checks triggered by static invokes must have been pruned by
4641 // art::PrepareForRegisterAllocation.
4642 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004643
4644 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4645 return;
4646 }
4647
4648 LocationSummary* locations = invoke->GetLocations();
4649 codegen_->GenerateStaticOrDirectCall(invoke,
4650 locations->HasTemps()
4651 ? locations->GetTemp(0)
4652 : Location::NoLocation());
4653 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4654}
4655
Chris Larsen3acee732015-11-18 13:31:08 -08004656void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02004657 // Use the calling convention instead of the location of the receiver, as
4658 // intrinsics may have put the receiver in a different register. In the intrinsics
4659 // slow path, the arguments have been moved to the right place, so here we are
4660 // guaranteed that the receiver is the first register of the calling convention.
4661 InvokeDexCallingConvention calling_convention;
4662 Register receiver = calling_convention.GetRegisterAt(0);
4663
Chris Larsen3acee732015-11-18 13:31:08 -08004664 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004665 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4666 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4667 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004668 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004669
4670 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02004671 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08004672 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004673 // temp = temp->GetMethodAt(method_offset);
4674 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4675 // T9 = temp->GetEntryPoint();
4676 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4677 // T9();
4678 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004679 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004680}
4681
4682void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4683 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4684 return;
4685 }
4686
4687 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004688 DCHECK(!codegen_->IsLeafMethod());
4689 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4690}
4691
4692void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004693 if (cls->NeedsAccessCheck()) {
4694 InvokeRuntimeCallingConvention calling_convention;
4695 CodeGenerator::CreateLoadClassLocationSummary(
4696 cls,
4697 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4698 Location::RegisterLocation(V0),
4699 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4700 return;
4701 }
4702
4703 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4704 ? LocationSummary::kCallOnSlowPath
4705 : LocationSummary::kNoCall;
4706 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4707 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4708 switch (load_kind) {
4709 // We need an extra register for PC-relative literals on R2.
4710 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4711 case HLoadClass::LoadKind::kBootImageAddress:
4712 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4713 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4714 break;
4715 }
4716 FALLTHROUGH_INTENDED;
4717 // We need an extra register for PC-relative dex cache accesses.
4718 case HLoadClass::LoadKind::kDexCachePcRelative:
4719 case HLoadClass::LoadKind::kReferrersClass:
4720 case HLoadClass::LoadKind::kDexCacheViaMethod:
4721 locations->SetInAt(0, Location::RequiresRegister());
4722 break;
4723 default:
4724 break;
4725 }
4726 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004727}
4728
4729void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4730 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004731 if (cls->NeedsAccessCheck()) {
4732 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004733 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004734 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004735 return;
4736 }
4737
Alexey Frunze06a46c42016-07-19 15:00:40 -07004738 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4739 Location out_loc = locations->Out();
4740 Register out = out_loc.AsRegister<Register>();
4741 Register base_or_current_method_reg;
4742 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4743 switch (load_kind) {
4744 // We need an extra register for PC-relative literals on R2.
4745 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4746 case HLoadClass::LoadKind::kBootImageAddress:
4747 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4748 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4749 break;
4750 // We need an extra register for PC-relative dex cache accesses.
4751 case HLoadClass::LoadKind::kDexCachePcRelative:
4752 case HLoadClass::LoadKind::kReferrersClass:
4753 case HLoadClass::LoadKind::kDexCacheViaMethod:
4754 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4755 break;
4756 default:
4757 base_or_current_method_reg = ZERO;
4758 break;
4759 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004760
Alexey Frunze06a46c42016-07-19 15:00:40 -07004761 bool generate_null_check = false;
4762 switch (load_kind) {
4763 case HLoadClass::LoadKind::kReferrersClass: {
4764 DCHECK(!cls->CanCallRuntime());
4765 DCHECK(!cls->MustGenerateClinitCheck());
4766 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4767 GenerateGcRootFieldLoad(cls,
4768 out_loc,
4769 base_or_current_method_reg,
4770 ArtMethod::DeclaringClassOffset().Int32Value());
4771 break;
4772 }
4773 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4774 DCHECK(!kEmitCompilerReadBarrier);
4775 __ LoadLiteral(out,
4776 base_or_current_method_reg,
4777 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4778 cls->GetTypeIndex()));
4779 break;
4780 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4781 DCHECK(!kEmitCompilerReadBarrier);
4782 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4783 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004784 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004785 break;
4786 }
4787 case HLoadClass::LoadKind::kBootImageAddress: {
4788 DCHECK(!kEmitCompilerReadBarrier);
4789 DCHECK_NE(cls->GetAddress(), 0u);
4790 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4791 __ LoadLiteral(out,
4792 base_or_current_method_reg,
4793 codegen_->DeduplicateBootImageAddressLiteral(address));
4794 break;
4795 }
4796 case HLoadClass::LoadKind::kDexCacheAddress: {
4797 DCHECK_NE(cls->GetAddress(), 0u);
4798 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4799 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4800 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4801 int16_t offset = Low16Bits(address);
4802 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4803 __ Lui(out, High16Bits(base_address));
4804 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4805 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4806 generate_null_check = !cls->IsInDexCache();
4807 break;
4808 }
4809 case HLoadClass::LoadKind::kDexCachePcRelative: {
4810 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4811 int32_t offset =
4812 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4813 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4814 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4815 generate_null_check = !cls->IsInDexCache();
4816 break;
4817 }
4818 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4819 // /* GcRoot<mirror::Class>[] */ out =
4820 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4821 __ LoadFromOffset(kLoadWord,
4822 out,
4823 base_or_current_method_reg,
4824 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4825 // /* GcRoot<mirror::Class> */ out = out[type_index]
4826 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4827 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4828 generate_null_check = !cls->IsInDexCache();
4829 }
4830 }
4831
4832 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4833 DCHECK(cls->CanCallRuntime());
4834 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4835 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4836 codegen_->AddSlowPath(slow_path);
4837 if (generate_null_check) {
4838 __ Beqz(out, slow_path->GetEntryLabel());
4839 }
4840 if (cls->MustGenerateClinitCheck()) {
4841 GenerateClassInitializationCheck(slow_path, out);
4842 } else {
4843 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004844 }
4845 }
4846}
4847
4848static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004849 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004850}
4851
4852void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4853 LocationSummary* locations =
4854 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4855 locations->SetOut(Location::RequiresRegister());
4856}
4857
4858void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4859 Register out = load->GetLocations()->Out().AsRegister<Register>();
4860 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4861}
4862
4863void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4864 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4865}
4866
4867void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4868 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4869}
4870
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004871void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004872 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00004873 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4874 ? LocationSummary::kCallOnMainOnly
4875 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004876 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004877 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004878 HLoadString::LoadKind load_kind = load->GetLoadKind();
4879 switch (load_kind) {
4880 // We need an extra register for PC-relative literals on R2.
4881 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4882 case HLoadString::LoadKind::kBootImageAddress:
4883 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004884 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004885 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4886 break;
4887 }
4888 FALLTHROUGH_INTENDED;
4889 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07004890 case HLoadString::LoadKind::kDexCacheViaMethod:
4891 locations->SetInAt(0, Location::RequiresRegister());
4892 break;
4893 default:
4894 break;
4895 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004896 locations->SetOut(Location::RequiresRegister());
4897}
4898
4899void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004900 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004901 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004902 Location out_loc = locations->Out();
4903 Register out = out_loc.AsRegister<Register>();
4904 Register base_or_current_method_reg;
4905 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4906 switch (load_kind) {
4907 // We need an extra register for PC-relative literals on R2.
4908 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4909 case HLoadString::LoadKind::kBootImageAddress:
4910 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004911 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004912 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4913 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004914 default:
4915 base_or_current_method_reg = ZERO;
4916 break;
4917 }
4918
4919 switch (load_kind) {
4920 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4921 DCHECK(!kEmitCompilerReadBarrier);
4922 __ LoadLiteral(out,
4923 base_or_current_method_reg,
4924 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4925 load->GetStringIndex()));
4926 return; // No dex cache slow path.
4927 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4928 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004929 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004930 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4931 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004932 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004933 return; // No dex cache slow path.
4934 }
4935 case HLoadString::LoadKind::kBootImageAddress: {
4936 DCHECK(!kEmitCompilerReadBarrier);
4937 DCHECK_NE(load->GetAddress(), 0u);
4938 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4939 __ LoadLiteral(out,
4940 base_or_current_method_reg,
4941 codegen_->DeduplicateBootImageAddressLiteral(address));
4942 return; // No dex cache slow path.
4943 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004944 case HLoadString::LoadKind::kBssEntry: {
4945 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4946 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4947 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
4948 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
4949 __ LoadFromOffset(kLoadWord, out, out, 0);
4950 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4951 codegen_->AddSlowPath(slow_path);
4952 __ Beqz(out, slow_path->GetEntryLabel());
4953 __ Bind(slow_path->GetExitLabel());
4954 return;
4955 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004956 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004957 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004958 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004959
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004960 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004961 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
4962 InvokeRuntimeCallingConvention calling_convention;
4963 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
4964 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4965 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004966}
4967
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004968void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4970 locations->SetOut(Location::ConstantLocation(constant));
4971}
4972
4973void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4974 // Will be generated at use site.
4975}
4976
4977void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4978 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004979 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004980 InvokeRuntimeCallingConvention calling_convention;
4981 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4982}
4983
4984void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4985 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01004986 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004987 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4988 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004989 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004990 }
4991 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4992}
4993
4994void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4995 LocationSummary* locations =
4996 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4997 switch (mul->GetResultType()) {
4998 case Primitive::kPrimInt:
4999 case Primitive::kPrimLong:
5000 locations->SetInAt(0, Location::RequiresRegister());
5001 locations->SetInAt(1, Location::RequiresRegister());
5002 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5003 break;
5004
5005 case Primitive::kPrimFloat:
5006 case Primitive::kPrimDouble:
5007 locations->SetInAt(0, Location::RequiresFpuRegister());
5008 locations->SetInAt(1, Location::RequiresFpuRegister());
5009 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5010 break;
5011
5012 default:
5013 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5014 }
5015}
5016
5017void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5018 Primitive::Type type = instruction->GetType();
5019 LocationSummary* locations = instruction->GetLocations();
5020 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5021
5022 switch (type) {
5023 case Primitive::kPrimInt: {
5024 Register dst = locations->Out().AsRegister<Register>();
5025 Register lhs = locations->InAt(0).AsRegister<Register>();
5026 Register rhs = locations->InAt(1).AsRegister<Register>();
5027
5028 if (isR6) {
5029 __ MulR6(dst, lhs, rhs);
5030 } else {
5031 __ MulR2(dst, lhs, rhs);
5032 }
5033 break;
5034 }
5035 case Primitive::kPrimLong: {
5036 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5037 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5038 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5039 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5040 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5041 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5042
5043 // Extra checks to protect caused by the existance of A1_A2.
5044 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5045 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5046 DCHECK_NE(dst_high, lhs_low);
5047 DCHECK_NE(dst_high, rhs_low);
5048
5049 // A_B * C_D
5050 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5051 // dst_lo: [ low(B*D) ]
5052 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5053
5054 if (isR6) {
5055 __ MulR6(TMP, lhs_high, rhs_low);
5056 __ MulR6(dst_high, lhs_low, rhs_high);
5057 __ Addu(dst_high, dst_high, TMP);
5058 __ MuhuR6(TMP, lhs_low, rhs_low);
5059 __ Addu(dst_high, dst_high, TMP);
5060 __ MulR6(dst_low, lhs_low, rhs_low);
5061 } else {
5062 __ MulR2(TMP, lhs_high, rhs_low);
5063 __ MulR2(dst_high, lhs_low, rhs_high);
5064 __ Addu(dst_high, dst_high, TMP);
5065 __ MultuR2(lhs_low, rhs_low);
5066 __ Mfhi(TMP);
5067 __ Addu(dst_high, dst_high, TMP);
5068 __ Mflo(dst_low);
5069 }
5070 break;
5071 }
5072 case Primitive::kPrimFloat:
5073 case Primitive::kPrimDouble: {
5074 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5075 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5076 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5077 if (type == Primitive::kPrimFloat) {
5078 __ MulS(dst, lhs, rhs);
5079 } else {
5080 __ MulD(dst, lhs, rhs);
5081 }
5082 break;
5083 }
5084 default:
5085 LOG(FATAL) << "Unexpected mul type " << type;
5086 }
5087}
5088
5089void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5090 LocationSummary* locations =
5091 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5092 switch (neg->GetResultType()) {
5093 case Primitive::kPrimInt:
5094 case Primitive::kPrimLong:
5095 locations->SetInAt(0, Location::RequiresRegister());
5096 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5097 break;
5098
5099 case Primitive::kPrimFloat:
5100 case Primitive::kPrimDouble:
5101 locations->SetInAt(0, Location::RequiresFpuRegister());
5102 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5103 break;
5104
5105 default:
5106 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5107 }
5108}
5109
5110void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5111 Primitive::Type type = instruction->GetType();
5112 LocationSummary* locations = instruction->GetLocations();
5113
5114 switch (type) {
5115 case Primitive::kPrimInt: {
5116 Register dst = locations->Out().AsRegister<Register>();
5117 Register src = locations->InAt(0).AsRegister<Register>();
5118 __ Subu(dst, ZERO, src);
5119 break;
5120 }
5121 case Primitive::kPrimLong: {
5122 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5123 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5124 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5125 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5126 __ Subu(dst_low, ZERO, src_low);
5127 __ Sltu(TMP, ZERO, dst_low);
5128 __ Subu(dst_high, ZERO, src_high);
5129 __ Subu(dst_high, dst_high, TMP);
5130 break;
5131 }
5132 case Primitive::kPrimFloat:
5133 case Primitive::kPrimDouble: {
5134 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5135 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5136 if (type == Primitive::kPrimFloat) {
5137 __ NegS(dst, src);
5138 } else {
5139 __ NegD(dst, src);
5140 }
5141 break;
5142 }
5143 default:
5144 LOG(FATAL) << "Unexpected neg type " << type;
5145 }
5146}
5147
5148void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5149 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005150 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005151 InvokeRuntimeCallingConvention calling_convention;
5152 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5153 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5154 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5155 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5156}
5157
5158void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5159 InvokeRuntimeCallingConvention calling_convention;
5160 Register current_method_register = calling_convention.GetRegisterAt(2);
5161 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5162 // Move an uint16_t value to a register.
5163 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005164 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005165 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5166 void*, uint32_t, int32_t, ArtMethod*>();
5167}
5168
5169void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5170 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005171 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005172 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005173 if (instruction->IsStringAlloc()) {
5174 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5175 } else {
5176 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5177 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5178 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005179 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5180}
5181
5182void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005183 if (instruction->IsStringAlloc()) {
5184 // String is allocated through StringFactory. Call NewEmptyString entry point.
5185 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005186 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005187 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5188 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5189 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005190 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005191 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5192 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005193 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00005194 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
5195 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005196}
5197
5198void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5199 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5200 locations->SetInAt(0, Location::RequiresRegister());
5201 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5202}
5203
5204void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5205 Primitive::Type type = instruction->GetType();
5206 LocationSummary* locations = instruction->GetLocations();
5207
5208 switch (type) {
5209 case Primitive::kPrimInt: {
5210 Register dst = locations->Out().AsRegister<Register>();
5211 Register src = locations->InAt(0).AsRegister<Register>();
5212 __ Nor(dst, src, ZERO);
5213 break;
5214 }
5215
5216 case Primitive::kPrimLong: {
5217 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5218 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5219 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5220 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5221 __ Nor(dst_high, src_high, ZERO);
5222 __ Nor(dst_low, src_low, ZERO);
5223 break;
5224 }
5225
5226 default:
5227 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5228 }
5229}
5230
5231void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5232 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5233 locations->SetInAt(0, Location::RequiresRegister());
5234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5235}
5236
5237void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5238 LocationSummary* locations = instruction->GetLocations();
5239 __ Xori(locations->Out().AsRegister<Register>(),
5240 locations->InAt(0).AsRegister<Register>(),
5241 1);
5242}
5243
5244void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005245 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5246 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005247}
5248
Calin Juravle2ae48182016-03-16 14:05:09 +00005249void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5250 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005251 return;
5252 }
5253 Location obj = instruction->GetLocations()->InAt(0);
5254
5255 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005256 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005257}
5258
Calin Juravle2ae48182016-03-16 14:05:09 +00005259void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005260 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005261 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005262
5263 Location obj = instruction->GetLocations()->InAt(0);
5264
5265 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5266}
5267
5268void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005269 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005270}
5271
5272void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5273 HandleBinaryOp(instruction);
5274}
5275
5276void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5277 HandleBinaryOp(instruction);
5278}
5279
5280void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5281 LOG(FATAL) << "Unreachable";
5282}
5283
5284void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
5285 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5286}
5287
5288void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
5289 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5290 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5291 if (location.IsStackSlot()) {
5292 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5293 } else if (location.IsDoubleStackSlot()) {
5294 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5295 }
5296 locations->SetOut(location);
5297}
5298
5299void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
5300 ATTRIBUTE_UNUSED) {
5301 // Nothing to do, the parameter is already at its location.
5302}
5303
5304void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5305 LocationSummary* locations =
5306 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5307 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5308}
5309
5310void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5311 ATTRIBUTE_UNUSED) {
5312 // Nothing to do, the method is already at its location.
5313}
5314
5315void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005317 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005318 locations->SetInAt(i, Location::Any());
5319 }
5320 locations->SetOut(Location::Any());
5321}
5322
5323void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5324 LOG(FATAL) << "Unreachable";
5325}
5326
5327void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5328 Primitive::Type type = rem->GetResultType();
5329 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005330 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005331 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5332
5333 switch (type) {
5334 case Primitive::kPrimInt:
5335 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005336 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5338 break;
5339
5340 case Primitive::kPrimLong: {
5341 InvokeRuntimeCallingConvention calling_convention;
5342 locations->SetInAt(0, Location::RegisterPairLocation(
5343 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5344 locations->SetInAt(1, Location::RegisterPairLocation(
5345 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5346 locations->SetOut(calling_convention.GetReturnLocation(type));
5347 break;
5348 }
5349
5350 case Primitive::kPrimFloat:
5351 case Primitive::kPrimDouble: {
5352 InvokeRuntimeCallingConvention calling_convention;
5353 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5354 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5355 locations->SetOut(calling_convention.GetReturnLocation(type));
5356 break;
5357 }
5358
5359 default:
5360 LOG(FATAL) << "Unexpected rem type " << type;
5361 }
5362}
5363
5364void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5365 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005366
5367 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005368 case Primitive::kPrimInt:
5369 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005370 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005371 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005372 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005373 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5374 break;
5375 }
5376 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005377 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005378 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005379 break;
5380 }
5381 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005382 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005383 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005384 break;
5385 }
5386 default:
5387 LOG(FATAL) << "Unexpected rem type " << type;
5388 }
5389}
5390
5391void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5392 memory_barrier->SetLocations(nullptr);
5393}
5394
5395void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5396 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5397}
5398
5399void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5401 Primitive::Type return_type = ret->InputAt(0)->GetType();
5402 locations->SetInAt(0, MipsReturnLocation(return_type));
5403}
5404
5405void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5406 codegen_->GenerateFrameExit();
5407}
5408
5409void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5410 ret->SetLocations(nullptr);
5411}
5412
5413void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5414 codegen_->GenerateFrameExit();
5415}
5416
Alexey Frunze92d90602015-12-18 18:16:36 -08005417void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5418 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005419}
5420
Alexey Frunze92d90602015-12-18 18:16:36 -08005421void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5422 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005423}
5424
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005425void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5426 HandleShift(shl);
5427}
5428
5429void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5430 HandleShift(shl);
5431}
5432
5433void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5434 HandleShift(shr);
5435}
5436
5437void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5438 HandleShift(shr);
5439}
5440
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005441void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5442 HandleBinaryOp(instruction);
5443}
5444
5445void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5446 HandleBinaryOp(instruction);
5447}
5448
5449void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5450 HandleFieldGet(instruction, instruction->GetFieldInfo());
5451}
5452
5453void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5454 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5455}
5456
5457void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5458 HandleFieldSet(instruction, instruction->GetFieldInfo());
5459}
5460
5461void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5462 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5463}
5464
5465void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5466 HUnresolvedInstanceFieldGet* instruction) {
5467 FieldAccessCallingConventionMIPS calling_convention;
5468 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5469 instruction->GetFieldType(),
5470 calling_convention);
5471}
5472
5473void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5474 HUnresolvedInstanceFieldGet* instruction) {
5475 FieldAccessCallingConventionMIPS calling_convention;
5476 codegen_->GenerateUnresolvedFieldAccess(instruction,
5477 instruction->GetFieldType(),
5478 instruction->GetFieldIndex(),
5479 instruction->GetDexPc(),
5480 calling_convention);
5481}
5482
5483void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5484 HUnresolvedInstanceFieldSet* instruction) {
5485 FieldAccessCallingConventionMIPS calling_convention;
5486 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5487 instruction->GetFieldType(),
5488 calling_convention);
5489}
5490
5491void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5492 HUnresolvedInstanceFieldSet* instruction) {
5493 FieldAccessCallingConventionMIPS calling_convention;
5494 codegen_->GenerateUnresolvedFieldAccess(instruction,
5495 instruction->GetFieldType(),
5496 instruction->GetFieldIndex(),
5497 instruction->GetDexPc(),
5498 calling_convention);
5499}
5500
5501void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5502 HUnresolvedStaticFieldGet* instruction) {
5503 FieldAccessCallingConventionMIPS calling_convention;
5504 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5505 instruction->GetFieldType(),
5506 calling_convention);
5507}
5508
5509void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5510 HUnresolvedStaticFieldGet* instruction) {
5511 FieldAccessCallingConventionMIPS calling_convention;
5512 codegen_->GenerateUnresolvedFieldAccess(instruction,
5513 instruction->GetFieldType(),
5514 instruction->GetFieldIndex(),
5515 instruction->GetDexPc(),
5516 calling_convention);
5517}
5518
5519void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5520 HUnresolvedStaticFieldSet* instruction) {
5521 FieldAccessCallingConventionMIPS calling_convention;
5522 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5523 instruction->GetFieldType(),
5524 calling_convention);
5525}
5526
5527void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5528 HUnresolvedStaticFieldSet* instruction) {
5529 FieldAccessCallingConventionMIPS calling_convention;
5530 codegen_->GenerateUnresolvedFieldAccess(instruction,
5531 instruction->GetFieldType(),
5532 instruction->GetFieldIndex(),
5533 instruction->GetDexPc(),
5534 calling_convention);
5535}
5536
5537void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005538 LocationSummary* locations =
5539 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005540 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005541}
5542
5543void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5544 HBasicBlock* block = instruction->GetBlock();
5545 if (block->GetLoopInformation() != nullptr) {
5546 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5547 // The back edge will generate the suspend check.
5548 return;
5549 }
5550 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5551 // The goto will generate the suspend check.
5552 return;
5553 }
5554 GenerateSuspendCheck(instruction, nullptr);
5555}
5556
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005557void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5558 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005560 InvokeRuntimeCallingConvention calling_convention;
5561 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5562}
5563
5564void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005565 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005566 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5567}
5568
5569void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5570 Primitive::Type input_type = conversion->GetInputType();
5571 Primitive::Type result_type = conversion->GetResultType();
5572 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005573 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005574
5575 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5576 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5577 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5578 }
5579
5580 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005581 if (!isR6 &&
5582 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5583 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005584 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005585 }
5586
5587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5588
5589 if (call_kind == LocationSummary::kNoCall) {
5590 if (Primitive::IsFloatingPointType(input_type)) {
5591 locations->SetInAt(0, Location::RequiresFpuRegister());
5592 } else {
5593 locations->SetInAt(0, Location::RequiresRegister());
5594 }
5595
5596 if (Primitive::IsFloatingPointType(result_type)) {
5597 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5598 } else {
5599 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5600 }
5601 } else {
5602 InvokeRuntimeCallingConvention calling_convention;
5603
5604 if (Primitive::IsFloatingPointType(input_type)) {
5605 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5606 } else {
5607 DCHECK_EQ(input_type, Primitive::kPrimLong);
5608 locations->SetInAt(0, Location::RegisterPairLocation(
5609 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5610 }
5611
5612 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5613 }
5614}
5615
5616void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5617 LocationSummary* locations = conversion->GetLocations();
5618 Primitive::Type result_type = conversion->GetResultType();
5619 Primitive::Type input_type = conversion->GetInputType();
5620 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005621 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005622
5623 DCHECK_NE(input_type, result_type);
5624
5625 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5626 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5627 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5628 Register src = locations->InAt(0).AsRegister<Register>();
5629
Alexey Frunzea871ef12016-06-27 15:20:11 -07005630 if (dst_low != src) {
5631 __ Move(dst_low, src);
5632 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005633 __ Sra(dst_high, src, 31);
5634 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5635 Register dst = locations->Out().AsRegister<Register>();
5636 Register src = (input_type == Primitive::kPrimLong)
5637 ? locations->InAt(0).AsRegisterPairLow<Register>()
5638 : locations->InAt(0).AsRegister<Register>();
5639
5640 switch (result_type) {
5641 case Primitive::kPrimChar:
5642 __ Andi(dst, src, 0xFFFF);
5643 break;
5644 case Primitive::kPrimByte:
5645 if (has_sign_extension) {
5646 __ Seb(dst, src);
5647 } else {
5648 __ Sll(dst, src, 24);
5649 __ Sra(dst, dst, 24);
5650 }
5651 break;
5652 case Primitive::kPrimShort:
5653 if (has_sign_extension) {
5654 __ Seh(dst, src);
5655 } else {
5656 __ Sll(dst, src, 16);
5657 __ Sra(dst, dst, 16);
5658 }
5659 break;
5660 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005661 if (dst != src) {
5662 __ Move(dst, src);
5663 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005664 break;
5665
5666 default:
5667 LOG(FATAL) << "Unexpected type conversion from " << input_type
5668 << " to " << result_type;
5669 }
5670 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005671 if (input_type == Primitive::kPrimLong) {
5672 if (isR6) {
5673 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5674 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5675 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5676 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5677 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5678 __ Mtc1(src_low, FTMP);
5679 __ Mthc1(src_high, FTMP);
5680 if (result_type == Primitive::kPrimFloat) {
5681 __ Cvtsl(dst, FTMP);
5682 } else {
5683 __ Cvtdl(dst, FTMP);
5684 }
5685 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005686 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
5687 : kQuickL2d;
5688 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005689 if (result_type == Primitive::kPrimFloat) {
5690 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5691 } else {
5692 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5693 }
5694 }
5695 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005696 Register src = locations->InAt(0).AsRegister<Register>();
5697 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5698 __ Mtc1(src, FTMP);
5699 if (result_type == Primitive::kPrimFloat) {
5700 __ Cvtsw(dst, FTMP);
5701 } else {
5702 __ Cvtdw(dst, FTMP);
5703 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005704 }
5705 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5706 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005707 if (result_type == Primitive::kPrimLong) {
5708 if (isR6) {
5709 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5710 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5711 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5712 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5713 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5714 MipsLabel truncate;
5715 MipsLabel done;
5716
5717 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5718 // value when the input is either a NaN or is outside of the range of the output type
5719 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5720 // the same result.
5721 //
5722 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5723 // value of the output type if the input is outside of the range after the truncation or
5724 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5725 // results. This matches the desired float/double-to-int/long conversion exactly.
5726 //
5727 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5728 //
5729 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5730 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5731 // even though it must be NAN2008=1 on R6.
5732 //
5733 // The code takes care of the different behaviors by first comparing the input to the
5734 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5735 // If the input is greater than or equal to the minimum, it procedes to the truncate
5736 // instruction, which will handle such an input the same way irrespective of NAN2008.
5737 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5738 // in order to return either zero or the minimum value.
5739 //
5740 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5741 // truncate instruction for MIPS64R6.
5742 if (input_type == Primitive::kPrimFloat) {
5743 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5744 __ LoadConst32(TMP, min_val);
5745 __ Mtc1(TMP, FTMP);
5746 __ CmpLeS(FTMP, FTMP, src);
5747 } else {
5748 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5749 __ LoadConst32(TMP, High32Bits(min_val));
5750 __ Mtc1(ZERO, FTMP);
5751 __ Mthc1(TMP, FTMP);
5752 __ CmpLeD(FTMP, FTMP, src);
5753 }
5754
5755 __ Bc1nez(FTMP, &truncate);
5756
5757 if (input_type == Primitive::kPrimFloat) {
5758 __ CmpEqS(FTMP, src, src);
5759 } else {
5760 __ CmpEqD(FTMP, src, src);
5761 }
5762 __ Move(dst_low, ZERO);
5763 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5764 __ Mfc1(TMP, FTMP);
5765 __ And(dst_high, dst_high, TMP);
5766
5767 __ B(&done);
5768
5769 __ Bind(&truncate);
5770
5771 if (input_type == Primitive::kPrimFloat) {
5772 __ TruncLS(FTMP, src);
5773 } else {
5774 __ TruncLD(FTMP, src);
5775 }
5776 __ Mfc1(dst_low, FTMP);
5777 __ Mfhc1(dst_high, FTMP);
5778
5779 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005780 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005781 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
5782 : kQuickD2l;
5783 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005784 if (input_type == Primitive::kPrimFloat) {
5785 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5786 } else {
5787 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5788 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005789 }
5790 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005791 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5792 Register dst = locations->Out().AsRegister<Register>();
5793 MipsLabel truncate;
5794 MipsLabel done;
5795
5796 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5797 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5798 // even though it must be NAN2008=1 on R6.
5799 //
5800 // For details see the large comment above for the truncation of float/double to long on R6.
5801 //
5802 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5803 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005804 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005805 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5806 __ LoadConst32(TMP, min_val);
5807 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005808 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005809 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5810 __ LoadConst32(TMP, High32Bits(min_val));
5811 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005812 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005813 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005814
5815 if (isR6) {
5816 if (input_type == Primitive::kPrimFloat) {
5817 __ CmpLeS(FTMP, FTMP, src);
5818 } else {
5819 __ CmpLeD(FTMP, FTMP, src);
5820 }
5821 __ Bc1nez(FTMP, &truncate);
5822
5823 if (input_type == Primitive::kPrimFloat) {
5824 __ CmpEqS(FTMP, src, src);
5825 } else {
5826 __ CmpEqD(FTMP, src, src);
5827 }
5828 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5829 __ Mfc1(TMP, FTMP);
5830 __ And(dst, dst, TMP);
5831 } else {
5832 if (input_type == Primitive::kPrimFloat) {
5833 __ ColeS(0, FTMP, src);
5834 } else {
5835 __ ColeD(0, FTMP, src);
5836 }
5837 __ Bc1t(0, &truncate);
5838
5839 if (input_type == Primitive::kPrimFloat) {
5840 __ CeqS(0, src, src);
5841 } else {
5842 __ CeqD(0, src, src);
5843 }
5844 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5845 __ Movf(dst, ZERO, 0);
5846 }
5847
5848 __ B(&done);
5849
5850 __ Bind(&truncate);
5851
5852 if (input_type == Primitive::kPrimFloat) {
5853 __ TruncWS(FTMP, src);
5854 } else {
5855 __ TruncWD(FTMP, src);
5856 }
5857 __ Mfc1(dst, FTMP);
5858
5859 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005860 }
5861 } else if (Primitive::IsFloatingPointType(result_type) &&
5862 Primitive::IsFloatingPointType(input_type)) {
5863 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5864 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5865 if (result_type == Primitive::kPrimFloat) {
5866 __ Cvtsd(dst, src);
5867 } else {
5868 __ Cvtds(dst, src);
5869 }
5870 } else {
5871 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5872 << " to " << result_type;
5873 }
5874}
5875
5876void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5877 HandleShift(ushr);
5878}
5879
5880void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5881 HandleShift(ushr);
5882}
5883
5884void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5885 HandleBinaryOp(instruction);
5886}
5887
5888void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5889 HandleBinaryOp(instruction);
5890}
5891
5892void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5893 // Nothing to do, this should be removed during prepare for register allocator.
5894 LOG(FATAL) << "Unreachable";
5895}
5896
5897void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5898 // Nothing to do, this should be removed during prepare for register allocator.
5899 LOG(FATAL) << "Unreachable";
5900}
5901
5902void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005903 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005904}
5905
5906void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005907 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005908}
5909
5910void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005911 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005912}
5913
5914void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005915 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005916}
5917
5918void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005919 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005920}
5921
5922void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005923 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005924}
5925
5926void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005927 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005928}
5929
5930void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005931 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005932}
5933
5934void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005935 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005936}
5937
5938void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005939 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005940}
5941
5942void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005943 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005944}
5945
5946void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005947 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005948}
5949
5950void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005951 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005952}
5953
5954void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005955 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005956}
5957
5958void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005959 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005960}
5961
5962void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005963 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005964}
5965
5966void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005967 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005968}
5969
5970void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005971 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005972}
5973
5974void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005975 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005976}
5977
5978void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005979 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005980}
5981
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005982void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5983 LocationSummary* locations =
5984 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5985 locations->SetInAt(0, Location::RequiresRegister());
5986}
5987
Alexey Frunze96b66822016-09-10 02:32:44 -07005988void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
5989 int32_t lower_bound,
5990 uint32_t num_entries,
5991 HBasicBlock* switch_block,
5992 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005993 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005994 Register temp_reg = TMP;
5995 __ Addiu32(temp_reg, value_reg, -lower_bound);
5996 // Jump to default if index is negative
5997 // Note: We don't check the case that index is positive while value < lower_bound, because in
5998 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5999 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6000
Alexey Frunze96b66822016-09-10 02:32:44 -07006001 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006002 // Jump to successors[0] if value == lower_bound.
6003 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6004 int32_t last_index = 0;
6005 for (; num_entries - last_index > 2; last_index += 2) {
6006 __ Addiu(temp_reg, temp_reg, -2);
6007 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6008 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6009 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6010 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6011 }
6012 if (num_entries - last_index == 2) {
6013 // The last missing case_value.
6014 __ Addiu(temp_reg, temp_reg, -1);
6015 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006016 }
6017
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006018 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006019 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006020 __ B(codegen_->GetLabelOf(default_block));
6021 }
6022}
6023
Alexey Frunze96b66822016-09-10 02:32:44 -07006024void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6025 Register constant_area,
6026 int32_t lower_bound,
6027 uint32_t num_entries,
6028 HBasicBlock* switch_block,
6029 HBasicBlock* default_block) {
6030 // Create a jump table.
6031 std::vector<MipsLabel*> labels(num_entries);
6032 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6033 for (uint32_t i = 0; i < num_entries; i++) {
6034 labels[i] = codegen_->GetLabelOf(successors[i]);
6035 }
6036 JumpTable* table = __ CreateJumpTable(std::move(labels));
6037
6038 // Is the value in range?
6039 __ Addiu32(TMP, value_reg, -lower_bound);
6040 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6041 __ Sltiu(AT, TMP, num_entries);
6042 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6043 } else {
6044 __ LoadConst32(AT, num_entries);
6045 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6046 }
6047
6048 // We are in the range of the table.
6049 // Load the target address from the jump table, indexing by the value.
6050 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6051 __ Sll(TMP, TMP, 2);
6052 __ Addu(TMP, TMP, AT);
6053 __ Lw(TMP, TMP, 0);
6054 // Compute the absolute target address by adding the table start address
6055 // (the table contains offsets to targets relative to its start).
6056 __ Addu(TMP, TMP, AT);
6057 // And jump.
6058 __ Jr(TMP);
6059 __ NopIfNoReordering();
6060}
6061
6062void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6063 int32_t lower_bound = switch_instr->GetStartValue();
6064 uint32_t num_entries = switch_instr->GetNumEntries();
6065 LocationSummary* locations = switch_instr->GetLocations();
6066 Register value_reg = locations->InAt(0).AsRegister<Register>();
6067 HBasicBlock* switch_block = switch_instr->GetBlock();
6068 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6069
6070 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6071 num_entries > kPackedSwitchJumpTableThreshold) {
6072 // R6 uses PC-relative addressing to access the jump table.
6073 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6074 // the jump table and it is implemented by changing HPackedSwitch to
6075 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6076 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6077 GenTableBasedPackedSwitch(value_reg,
6078 ZERO,
6079 lower_bound,
6080 num_entries,
6081 switch_block,
6082 default_block);
6083 } else {
6084 GenPackedSwitchWithCompares(value_reg,
6085 lower_bound,
6086 num_entries,
6087 switch_block,
6088 default_block);
6089 }
6090}
6091
6092void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6093 LocationSummary* locations =
6094 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6095 locations->SetInAt(0, Location::RequiresRegister());
6096 // Constant area pointer (HMipsComputeBaseMethodAddress).
6097 locations->SetInAt(1, Location::RequiresRegister());
6098}
6099
6100void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6101 int32_t lower_bound = switch_instr->GetStartValue();
6102 uint32_t num_entries = switch_instr->GetNumEntries();
6103 LocationSummary* locations = switch_instr->GetLocations();
6104 Register value_reg = locations->InAt(0).AsRegister<Register>();
6105 Register constant_area = locations->InAt(1).AsRegister<Register>();
6106 HBasicBlock* switch_block = switch_instr->GetBlock();
6107 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6108
6109 // This is an R2-only path. HPackedSwitch has been changed to
6110 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6111 // required to address the jump table relative to PC.
6112 GenTableBasedPackedSwitch(value_reg,
6113 constant_area,
6114 lower_bound,
6115 num_entries,
6116 switch_block,
6117 default_block);
6118}
6119
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006120void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6121 HMipsComputeBaseMethodAddress* insn) {
6122 LocationSummary* locations =
6123 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6124 locations->SetOut(Location::RequiresRegister());
6125}
6126
6127void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6128 HMipsComputeBaseMethodAddress* insn) {
6129 LocationSummary* locations = insn->GetLocations();
6130 Register reg = locations->Out().AsRegister<Register>();
6131
6132 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6133
6134 // Generate a dummy PC-relative call to obtain PC.
6135 __ Nal();
6136 // Grab the return address off RA.
6137 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006138 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006139
6140 // Remember this offset (the obtained PC value) for later use with constant area.
6141 __ BindPcRelBaseLabel();
6142}
6143
6144void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6146 locations->SetOut(Location::RequiresRegister());
6147}
6148
6149void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6150 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6151 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6152 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006153 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6154 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006155}
6156
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006157void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6158 // The trampoline uses the same calling convention as dex calling conventions,
6159 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6160 // the method_idx.
6161 HandleInvoke(invoke);
6162}
6163
6164void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6165 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6166}
6167
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006168void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6169 LocationSummary* locations =
6170 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6171 locations->SetInAt(0, Location::RequiresRegister());
6172 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006173}
6174
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006175void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6176 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006177 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006178 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006179 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006180 __ LoadFromOffset(kLoadWord,
6181 locations->Out().AsRegister<Register>(),
6182 locations->InAt(0).AsRegister<Register>(),
6183 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006184 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006185 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006186 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006187 __ LoadFromOffset(kLoadWord,
6188 locations->Out().AsRegister<Register>(),
6189 locations->InAt(0).AsRegister<Register>(),
6190 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006191 __ LoadFromOffset(kLoadWord,
6192 locations->Out().AsRegister<Register>(),
6193 locations->Out().AsRegister<Register>(),
6194 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006195 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006196}
6197
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006198#undef __
6199#undef QUICK_ENTRY_POINT
6200
6201} // namespace mips
6202} // namespace art