blob: b826a2c53770979f40c9bfabb5bd07736673f483 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Alexey Frunzee3fb2452016-05-10 16:08:05 -070043// We'll maximize the range of a single load instruction for dex cache array accesses
44// by aligning offset -32768 with the offset of the first used element.
45static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
46
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020047Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
102 if (calling_convention.GetRegisterAt(gp_index) == A1) {
103 gp_index_++; // Skip A1, and use A2_A3 instead.
104 gp_index++;
105 }
106 Register low_even = calling_convention.GetRegisterAt(gp_index);
107 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
108 DCHECK_EQ(low_even + 1, high_odd);
109 next_location = Location::RegisterPairLocation(low_even, high_odd);
110 } else {
111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
112 next_location = Location::DoubleStackSlot(stack_offset);
113 }
114 break;
115 }
116
117 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
118 // will take up the even/odd pair, while floats are stored in even regs only.
119 // On 64 bit FPU, both double and float are stored in even registers only.
120 case Primitive::kPrimFloat:
121 case Primitive::kPrimDouble: {
122 uint32_t float_index = float_index_++;
123 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
124 next_location = Location::FpuRegisterLocation(
125 calling_convention.GetFpuRegisterAt(float_index));
126 } else {
127 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
128 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
129 : Location::StackSlot(stack_offset);
130 }
131 break;
132 }
133
134 case Primitive::kPrimVoid:
135 LOG(FATAL) << "Unexpected parameter type " << type;
136 break;
137 }
138
139 // Space on the stack is reserved for all arguments.
140 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
141
142 return next_location;
143}
144
145Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
146 return MipsReturnLocation(type);
147}
148
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100149// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
150#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700151#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200152
153class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
154 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000155 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200156
157 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
158 LocationSummary* locations = instruction_->GetLocations();
159 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
160 __ Bind(GetEntryLabel());
161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
167 InvokeRuntimeCallingConvention calling_convention;
168 codegen->EmitParallelMoves(locations->InAt(0),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
170 Primitive::kPrimInt,
171 locations->InAt(1),
172 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
173 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100174 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
175 ? kQuickThrowStringBounds
176 : kQuickThrowArrayBounds;
177 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100178 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200179 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
180 }
181
182 bool IsFatal() const OVERRIDE { return true; }
183
184 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
185
186 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
188};
189
190class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
191 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000192 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193
194 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
196 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100197 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200198 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
199 }
200
201 bool IsFatal() const OVERRIDE { return true; }
202
203 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
204
205 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
207};
208
209class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
210 public:
211 LoadClassSlowPathMIPS(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000215 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = at_->GetLocations();
221 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
222
223 __ Bind(GetEntryLabel());
224 SaveLiveRegisters(codegen, locations);
225
226 InvokeRuntimeCallingConvention calling_convention;
227 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
228
Serban Constantinescufca16662016-07-14 09:21:59 +0100229 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
230 : kQuickInitializeType;
231 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 if (do_clinit_) {
233 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
234 } else {
235 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
236 }
237
238 // Move the class to the desired location.
239 Location out = locations->Out();
240 if (out.IsValid()) {
241 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
242 Primitive::Type type = at_->GetType();
243 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
244 }
245
246 RestoreLiveRegisters(codegen, locations);
247 __ B(GetExitLabel());
248 }
249
250 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
251
252 private:
253 // The class this slow path will load.
254 HLoadClass* const cls_;
255
256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
267};
268
269class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
270 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000271 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200272
273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
274 LocationSummary* locations = instruction_->GetLocations();
275 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
276 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
277
278 __ Bind(GetEntryLabel());
279 SaveLiveRegisters(codegen, locations);
280
281 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000282 HLoadString* load = instruction_->AsLoadString();
283 const uint32_t string_index = load->GetStringIndex();
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufca16662016-07-14 09:21:59 +0100285 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
287 Primitive::Type type = instruction_->GetType();
288 mips_codegen->MoveLocation(locations->Out(),
289 calling_convention.GetReturnLocation(type),
290 type);
291
292 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000293
294 // Store the resolved String to the BSS entry.
295 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
296 // .bss entry address in the fast path, so that we can avoid another calculation here.
297 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
298 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
299 Register out = locations->Out().AsRegister<Register>();
300 DCHECK_NE(out, AT);
301 CodeGeneratorMIPS::PcRelativePatchInfo* info =
302 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
303 mips_codegen->EmitPcRelativeAddressPlaceholder(info, TMP, base);
304 __ StoreToOffset(kStoreWord, out, TMP, 0);
305
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
310
311 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200312 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
313};
314
315class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
316 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200318
319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
320 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
321 __ Bind(GetEntryLabel());
322 if (instruction_->CanThrowIntoCatchBlock()) {
323 // Live registers will be restored in the catch block if caught.
324 SaveLiveRegisters(codegen, instruction_->GetLocations());
325 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100326 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200327 instruction_,
328 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100329 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
331 }
332
333 bool IsFatal() const OVERRIDE { return true; }
334
335 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
336
337 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200338 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
339};
340
341class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
342 public:
343 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000344 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
348 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100349 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 if (successor_ == nullptr) {
352 __ B(GetReturnLabel());
353 } else {
354 __ B(mips_codegen->GetLabelOf(successor_));
355 }
356 }
357
358 MipsLabel* GetReturnLabel() {
359 DCHECK(successor_ == nullptr);
360 return &return_label_;
361 }
362
363 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
364
365 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200366 // If not null, the block to branch to after the suspend check.
367 HBasicBlock* const successor_;
368
369 // If `successor_` is null, the label to branch to after the suspend check.
370 MipsLabel return_label_;
371
372 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
373};
374
375class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
376 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000377 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200378
379 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
380 LocationSummary* locations = instruction_->GetLocations();
381 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
382 uint32_t dex_pc = instruction_->GetDexPc();
383 DCHECK(instruction_->IsCheckCast()
384 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
385 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
386
387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, locations);
389
390 // We're moving two locations to locations that could overlap, so we need a parallel
391 // move resolver.
392 InvokeRuntimeCallingConvention calling_convention;
393 codegen->EmitParallelMoves(locations->InAt(1),
394 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
395 Primitive::kPrimNot,
396 object_class,
397 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
398 Primitive::kPrimNot);
399
400 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100401 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000402 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700403 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404 Primitive::Type ret_type = instruction_->GetType();
405 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
406 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 } else {
408 DCHECK(instruction_->IsCheckCast());
Serban Constantinescufca16662016-07-14 09:21:59 +0100409 mips_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
411 }
412
413 RestoreLiveRegisters(codegen, locations);
414 __ B(GetExitLabel());
415 }
416
417 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
418
419 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
421};
422
423class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
424 public:
Aart Bik42249c32016-01-07 15:33:50 -0800425 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000426 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200427
428 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800429 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100431 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000432 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 }
434
435 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
436
437 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
439};
440
441CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
442 const MipsInstructionSetFeatures& isa_features,
443 const CompilerOptions& compiler_options,
444 OptimizingCompilerStats* stats)
445 : CodeGenerator(graph,
446 kNumberOfCoreRegisters,
447 kNumberOfFRegisters,
448 kNumberOfRegisterPairs,
449 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
450 arraysize(kCoreCalleeSaves)),
451 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
452 arraysize(kFpuCalleeSaves)),
453 compiler_options,
454 stats),
455 block_labels_(nullptr),
456 location_builder_(graph, this),
457 instruction_visitor_(graph, this),
458 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100459 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700460 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700461 uint32_literals_(std::less<uint32_t>(),
462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700463 method_patches_(MethodReferenceComparator(),
464 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
465 call_patches_(MethodReferenceComparator(),
466 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700467 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
468 boot_image_string_patches_(StringReferenceValueComparator(),
469 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
470 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
471 boot_image_type_patches_(TypeReferenceValueComparator(),
472 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
473 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
474 boot_image_address_patches_(std::less<uint32_t>(),
475 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
476 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200477 // Save RA (containing the return address) to mimic Quick.
478 AddAllocatedRegister(Location::RegisterLocation(RA));
479}
480
481#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100482// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
483#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700484#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200485
486void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
487 // Ensure that we fix up branches.
488 __ FinalizeCode();
489
490 // Adjust native pc offsets in stack maps.
491 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
492 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
493 uint32_t new_position = __ GetAdjustedPosition(old_position);
494 DCHECK_GE(new_position, old_position);
495 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
496 }
497
498 // Adjust pc offsets for the disassembly information.
499 if (disasm_info_ != nullptr) {
500 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
501 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
502 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
503 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
504 it.second.start = __ GetAdjustedPosition(it.second.start);
505 it.second.end = __ GetAdjustedPosition(it.second.end);
506 }
507 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
508 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
509 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
510 }
511 }
512
513 CodeGenerator::Finalize(allocator);
514}
515
516MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
517 return codegen_->GetAssembler();
518}
519
520void ParallelMoveResolverMIPS::EmitMove(size_t index) {
521 DCHECK_LT(index, moves_.size());
522 MoveOperands* move = moves_[index];
523 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
524}
525
526void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
527 DCHECK_LT(index, moves_.size());
528 MoveOperands* move = moves_[index];
529 Primitive::Type type = move->GetType();
530 Location loc1 = move->GetDestination();
531 Location loc2 = move->GetSource();
532
533 DCHECK(!loc1.IsConstant());
534 DCHECK(!loc2.IsConstant());
535
536 if (loc1.Equals(loc2)) {
537 return;
538 }
539
540 if (loc1.IsRegister() && loc2.IsRegister()) {
541 // Swap 2 GPRs.
542 Register r1 = loc1.AsRegister<Register>();
543 Register r2 = loc2.AsRegister<Register>();
544 __ Move(TMP, r2);
545 __ Move(r2, r1);
546 __ Move(r1, TMP);
547 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
548 FRegister f1 = loc1.AsFpuRegister<FRegister>();
549 FRegister f2 = loc2.AsFpuRegister<FRegister>();
550 if (type == Primitive::kPrimFloat) {
551 __ MovS(FTMP, f2);
552 __ MovS(f2, f1);
553 __ MovS(f1, FTMP);
554 } else {
555 DCHECK_EQ(type, Primitive::kPrimDouble);
556 __ MovD(FTMP, f2);
557 __ MovD(f2, f1);
558 __ MovD(f1, FTMP);
559 }
560 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
561 (loc1.IsFpuRegister() && loc2.IsRegister())) {
562 // Swap FPR and GPR.
563 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
564 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
565 : loc2.AsFpuRegister<FRegister>();
566 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
567 : loc2.AsRegister<Register>();
568 __ Move(TMP, r2);
569 __ Mfc1(r2, f1);
570 __ Mtc1(TMP, f1);
571 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
572 // Swap 2 GPR register pairs.
573 Register r1 = loc1.AsRegisterPairLow<Register>();
574 Register r2 = loc2.AsRegisterPairLow<Register>();
575 __ Move(TMP, r2);
576 __ Move(r2, r1);
577 __ Move(r1, TMP);
578 r1 = loc1.AsRegisterPairHigh<Register>();
579 r2 = loc2.AsRegisterPairHigh<Register>();
580 __ Move(TMP, r2);
581 __ Move(r2, r1);
582 __ Move(r1, TMP);
583 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
584 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
585 // Swap FPR and GPR register pair.
586 DCHECK_EQ(type, Primitive::kPrimDouble);
587 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
588 : loc2.AsFpuRegister<FRegister>();
589 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
590 : loc2.AsRegisterPairLow<Register>();
591 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
592 : loc2.AsRegisterPairHigh<Register>();
593 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
594 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
595 // unpredictable and the following mfch1 will fail.
596 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800597 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200598 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800599 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200600 __ Move(r2_l, TMP);
601 __ Move(r2_h, AT);
602 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
603 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
604 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
605 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000606 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
607 (loc1.IsStackSlot() && loc2.IsRegister())) {
608 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
609 : loc2.AsRegister<Register>();
610 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
611 : loc2.GetStackIndex();
612 __ Move(TMP, reg);
613 __ LoadFromOffset(kLoadWord, reg, SP, offset);
614 __ StoreToOffset(kStoreWord, TMP, SP, offset);
615 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
616 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
617 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
618 : loc2.AsRegisterPairLow<Register>();
619 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
620 : loc2.AsRegisterPairHigh<Register>();
621 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
622 : loc2.GetStackIndex();
623 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
624 : loc2.GetHighStackIndex(kMipsWordSize);
625 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000626 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000627 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000628 __ Move(TMP, reg_h);
629 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
630 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200631 } else {
632 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
633 }
634}
635
636void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
637 __ Pop(static_cast<Register>(reg));
638}
639
640void ParallelMoveResolverMIPS::SpillScratch(int reg) {
641 __ Push(static_cast<Register>(reg));
642}
643
644void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
645 // Allocate a scratch register other than TMP, if available.
646 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
647 // automatically unspilled when the scratch scope object is destroyed).
648 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
649 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
650 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
651 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
652 __ LoadFromOffset(kLoadWord,
653 Register(ensure_scratch.GetRegister()),
654 SP,
655 index1 + stack_offset);
656 __ LoadFromOffset(kLoadWord,
657 TMP,
658 SP,
659 index2 + stack_offset);
660 __ StoreToOffset(kStoreWord,
661 Register(ensure_scratch.GetRegister()),
662 SP,
663 index2 + stack_offset);
664 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
665 }
666}
667
Alexey Frunze73296a72016-06-03 22:51:46 -0700668void CodeGeneratorMIPS::ComputeSpillMask() {
669 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
670 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
671 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
672 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
673 // registers, include the ZERO register to force alignment of FPU callee-saved registers
674 // within the stack frame.
675 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
676 core_spill_mask_ |= (1 << ZERO);
677 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700678}
679
680bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700681 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700682 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
683 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
684 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700685 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
686 // saved in an unused temporary register) and saving of RA and the current method pointer
687 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700688 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700689}
690
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200691static dwarf::Reg DWARFReg(Register reg) {
692 return dwarf::Reg::MipsCore(static_cast<int>(reg));
693}
694
695// TODO: mapping of floating-point registers to DWARF.
696
697void CodeGeneratorMIPS::GenerateFrameEntry() {
698 __ Bind(&frame_entry_label_);
699
700 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
701
702 if (do_overflow_check) {
703 __ LoadFromOffset(kLoadWord,
704 ZERO,
705 SP,
706 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
707 RecordPcInfo(nullptr, 0);
708 }
709
710 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700711 CHECK_EQ(fpu_spill_mask_, 0u);
712 CHECK_EQ(core_spill_mask_, 1u << RA);
713 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200714 return;
715 }
716
717 // Make sure the frame size isn't unreasonably large.
718 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
719 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
720 }
721
722 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200723
Alexey Frunze73296a72016-06-03 22:51:46 -0700724 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200725 __ IncreaseFrameSize(ofs);
726
Alexey Frunze73296a72016-06-03 22:51:46 -0700727 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
728 Register reg = static_cast<Register>(MostSignificantBit(mask));
729 mask ^= 1u << reg;
730 ofs -= kMipsWordSize;
731 // The ZERO register is only included for alignment.
732 if (reg != ZERO) {
733 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200734 __ cfi().RelOffset(DWARFReg(reg), ofs);
735 }
736 }
737
Alexey Frunze73296a72016-06-03 22:51:46 -0700738 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
739 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
740 mask ^= 1u << reg;
741 ofs -= kMipsDoublewordSize;
742 __ StoreDToOffset(reg, SP, ofs);
743 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200744 }
745
Alexey Frunze73296a72016-06-03 22:51:46 -0700746 // Store the current method pointer.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700747 // TODO: can we not do this if RequiresCurrentMethod() returns false?
Alexey Frunze73296a72016-06-03 22:51:46 -0700748 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200749}
750
751void CodeGeneratorMIPS::GenerateFrameExit() {
752 __ cfi().RememberState();
753
754 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200755 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200756
Alexey Frunze73296a72016-06-03 22:51:46 -0700757 // For better instruction scheduling restore RA before other registers.
758 uint32_t ofs = GetFrameSize();
759 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
760 Register reg = static_cast<Register>(MostSignificantBit(mask));
761 mask ^= 1u << reg;
762 ofs -= kMipsWordSize;
763 // The ZERO register is only included for alignment.
764 if (reg != ZERO) {
765 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200766 __ cfi().Restore(DWARFReg(reg));
767 }
768 }
769
Alexey Frunze73296a72016-06-03 22:51:46 -0700770 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
771 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
772 mask ^= 1u << reg;
773 ofs -= kMipsDoublewordSize;
774 __ LoadDFromOffset(reg, SP, ofs);
775 // TODO: __ cfi().Restore(DWARFReg(reg));
776 }
777
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700778 size_t frame_size = GetFrameSize();
779 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
780 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
781 bool reordering = __ SetReorder(false);
782 if (exchange) {
783 __ Jr(RA);
784 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
785 } else {
786 __ DecreaseFrameSize(frame_size);
787 __ Jr(RA);
788 __ Nop(); // In delay slot.
789 }
790 __ SetReorder(reordering);
791 } else {
792 __ Jr(RA);
793 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200794 }
795
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200796 __ cfi().RestoreState();
797 __ cfi().DefCFAOffset(GetFrameSize());
798}
799
800void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
801 __ Bind(GetLabelOf(block));
802}
803
804void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
805 if (src.Equals(dst)) {
806 return;
807 }
808
809 if (src.IsConstant()) {
810 MoveConstant(dst, src.GetConstant());
811 } else {
812 if (Primitive::Is64BitType(dst_type)) {
813 Move64(dst, src);
814 } else {
815 Move32(dst, src);
816 }
817 }
818}
819
820void CodeGeneratorMIPS::Move32(Location destination, Location source) {
821 if (source.Equals(destination)) {
822 return;
823 }
824
825 if (destination.IsRegister()) {
826 if (source.IsRegister()) {
827 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
828 } else if (source.IsFpuRegister()) {
829 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
830 } else {
831 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
832 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
833 }
834 } else if (destination.IsFpuRegister()) {
835 if (source.IsRegister()) {
836 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
837 } else if (source.IsFpuRegister()) {
838 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
839 } else {
840 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
841 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
842 }
843 } else {
844 DCHECK(destination.IsStackSlot()) << destination;
845 if (source.IsRegister()) {
846 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
847 } else if (source.IsFpuRegister()) {
848 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
849 } else {
850 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
851 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
852 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
853 }
854 }
855}
856
857void CodeGeneratorMIPS::Move64(Location destination, Location source) {
858 if (source.Equals(destination)) {
859 return;
860 }
861
862 if (destination.IsRegisterPair()) {
863 if (source.IsRegisterPair()) {
864 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
865 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
866 } else if (source.IsFpuRegister()) {
867 Register dst_high = destination.AsRegisterPairHigh<Register>();
868 Register dst_low = destination.AsRegisterPairLow<Register>();
869 FRegister src = source.AsFpuRegister<FRegister>();
870 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800871 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200872 } else {
873 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
874 int32_t off = source.GetStackIndex();
875 Register r = destination.AsRegisterPairLow<Register>();
876 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
877 }
878 } else if (destination.IsFpuRegister()) {
879 if (source.IsRegisterPair()) {
880 FRegister dst = destination.AsFpuRegister<FRegister>();
881 Register src_high = source.AsRegisterPairHigh<Register>();
882 Register src_low = source.AsRegisterPairLow<Register>();
883 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800884 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200885 } else if (source.IsFpuRegister()) {
886 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
887 } else {
888 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
889 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
890 }
891 } else {
892 DCHECK(destination.IsDoubleStackSlot()) << destination;
893 int32_t off = destination.GetStackIndex();
894 if (source.IsRegisterPair()) {
895 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
896 } else if (source.IsFpuRegister()) {
897 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
898 } else {
899 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
900 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
901 __ StoreToOffset(kStoreWord, TMP, SP, off);
902 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
903 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
904 }
905 }
906}
907
908void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
909 if (c->IsIntConstant() || c->IsNullConstant()) {
910 // Move 32 bit constant.
911 int32_t value = GetInt32ValueOf(c);
912 if (destination.IsRegister()) {
913 Register dst = destination.AsRegister<Register>();
914 __ LoadConst32(dst, value);
915 } else {
916 DCHECK(destination.IsStackSlot())
917 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700918 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200919 }
920 } else if (c->IsLongConstant()) {
921 // Move 64 bit constant.
922 int64_t value = GetInt64ValueOf(c);
923 if (destination.IsRegisterPair()) {
924 Register r_h = destination.AsRegisterPairHigh<Register>();
925 Register r_l = destination.AsRegisterPairLow<Register>();
926 __ LoadConst64(r_h, r_l, value);
927 } else {
928 DCHECK(destination.IsDoubleStackSlot())
929 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700930 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200931 }
932 } else if (c->IsFloatConstant()) {
933 // Move 32 bit float constant.
934 int32_t value = GetInt32ValueOf(c);
935 if (destination.IsFpuRegister()) {
936 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
937 } else {
938 DCHECK(destination.IsStackSlot())
939 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700940 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200941 }
942 } else {
943 // Move 64 bit double constant.
944 DCHECK(c->IsDoubleConstant()) << c->DebugName();
945 int64_t value = GetInt64ValueOf(c);
946 if (destination.IsFpuRegister()) {
947 FRegister fd = destination.AsFpuRegister<FRegister>();
948 __ LoadDConst64(fd, value, TMP);
949 } else {
950 DCHECK(destination.IsDoubleStackSlot())
951 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700952 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200953 }
954 }
955}
956
957void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
958 DCHECK(destination.IsRegister());
959 Register dst = destination.AsRegister<Register>();
960 __ LoadConst32(dst, value);
961}
962
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200963void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
964 if (location.IsRegister()) {
965 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700966 } else if (location.IsRegisterPair()) {
967 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
968 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200969 } else {
970 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
971 }
972}
973
Vladimir Markoaad75c62016-10-03 08:46:48 +0000974template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
975inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
976 const ArenaDeque<PcRelativePatchInfo>& infos,
977 ArenaVector<LinkerPatch>* linker_patches) {
978 for (const PcRelativePatchInfo& info : infos) {
979 const DexFile& dex_file = info.target_dex_file;
980 size_t offset_or_index = info.offset_or_index;
981 DCHECK(info.high_label.IsBound());
982 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
983 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
984 // the assembler's base label used for PC-relative addressing.
985 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
986 ? __ GetLabelLocation(&info.pc_rel_label)
987 : __ GetPcRelBaseLabelLocation();
988 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
989 }
990}
991
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700992void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
993 DCHECK(linker_patches->empty());
994 size_t size =
995 method_patches_.size() +
996 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700997 pc_relative_dex_cache_patches_.size() +
998 pc_relative_string_patches_.size() +
999 pc_relative_type_patches_.size() +
1000 boot_image_string_patches_.size() +
1001 boot_image_type_patches_.size() +
1002 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001003 linker_patches->reserve(size);
1004 for (const auto& entry : method_patches_) {
1005 const MethodReference& target_method = entry.first;
1006 Literal* literal = entry.second;
1007 DCHECK(literal->GetLabel()->IsBound());
1008 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1009 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1010 target_method.dex_file,
1011 target_method.dex_method_index));
1012 }
1013 for (const auto& entry : call_patches_) {
1014 const MethodReference& target_method = entry.first;
1015 Literal* literal = entry.second;
1016 DCHECK(literal->GetLabel()->IsBound());
1017 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1018 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1019 target_method.dex_file,
1020 target_method.dex_method_index));
1021 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001022 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1023 linker_patches);
1024 if (!GetCompilerOptions().IsBootImage()) {
1025 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1026 linker_patches);
1027 } else {
1028 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1029 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001030 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001031 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1032 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001033 for (const auto& entry : boot_image_string_patches_) {
1034 const StringReference& target_string = entry.first;
1035 Literal* literal = entry.second;
1036 DCHECK(literal->GetLabel()->IsBound());
1037 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1038 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1039 target_string.dex_file,
1040 target_string.string_index));
1041 }
1042 for (const auto& entry : boot_image_type_patches_) {
1043 const TypeReference& target_type = entry.first;
1044 Literal* literal = entry.second;
1045 DCHECK(literal->GetLabel()->IsBound());
1046 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1047 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1048 target_type.dex_file,
1049 target_type.type_index));
1050 }
1051 for (const auto& entry : boot_image_address_patches_) {
1052 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1053 Literal* literal = entry.second;
1054 DCHECK(literal->GetLabel()->IsBound());
1055 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1056 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1057 }
1058}
1059
1060CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1061 const DexFile& dex_file, uint32_t string_index) {
1062 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1063}
1064
1065CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1066 const DexFile& dex_file, uint32_t type_index) {
1067 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001068}
1069
1070CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1071 const DexFile& dex_file, uint32_t element_offset) {
1072 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1073}
1074
1075CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1076 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1077 patches->emplace_back(dex_file, offset_or_index);
1078 return &patches->back();
1079}
1080
Alexey Frunze06a46c42016-07-19 15:00:40 -07001081Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1082 return map->GetOrCreate(
1083 value,
1084 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1085}
1086
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001087Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1088 MethodToLiteralMap* map) {
1089 return map->GetOrCreate(
1090 target_method,
1091 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1092}
1093
1094Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1095 return DeduplicateMethodLiteral(target_method, &method_patches_);
1096}
1097
1098Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1099 return DeduplicateMethodLiteral(target_method, &call_patches_);
1100}
1101
Alexey Frunze06a46c42016-07-19 15:00:40 -07001102Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1103 uint32_t string_index) {
1104 return boot_image_string_patches_.GetOrCreate(
1105 StringReference(&dex_file, string_index),
1106 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1107}
1108
1109Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1110 uint32_t type_index) {
1111 return boot_image_type_patches_.GetOrCreate(
1112 TypeReference(&dex_file, type_index),
1113 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1114}
1115
1116Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1117 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1118 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1119 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1120}
1121
Vladimir Markoaad75c62016-10-03 08:46:48 +00001122void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1123 PcRelativePatchInfo* info, Register out, Register base) {
1124 bool reordering = __ SetReorder(false);
1125 if (GetInstructionSetFeatures().IsR6()) {
1126 DCHECK_EQ(base, ZERO);
1127 __ Bind(&info->high_label);
1128 __ Bind(&info->pc_rel_label);
1129 // Add a 32-bit offset to PC.
1130 __ Auipc(out, /* placeholder */ 0x1234);
1131 __ Addiu(out, out, /* placeholder */ 0x5678);
1132 } else {
1133 // If base is ZERO, emit NAL to obtain the actual base.
1134 if (base == ZERO) {
1135 // Generate a dummy PC-relative call to obtain PC.
1136 __ Nal();
1137 }
1138 __ Bind(&info->high_label);
1139 __ Lui(out, /* placeholder */ 0x1234);
1140 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1141 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1142 if (base == ZERO) {
1143 __ Bind(&info->pc_rel_label);
1144 }
1145 __ Ori(out, out, /* placeholder */ 0x5678);
1146 // Add a 32-bit offset to PC.
1147 __ Addu(out, out, (base == ZERO) ? RA : base);
1148 }
1149 __ SetReorder(reordering);
1150}
1151
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001152void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1153 MipsLabel done;
1154 Register card = AT;
1155 Register temp = TMP;
1156 __ Beqz(value, &done);
1157 __ LoadFromOffset(kLoadWord,
1158 card,
1159 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001160 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001161 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1162 __ Addu(temp, card, temp);
1163 __ Sb(card, temp, 0);
1164 __ Bind(&done);
1165}
1166
David Brazdil58282f42016-01-14 12:45:10 +00001167void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001168 // Don't allocate the dalvik style register pair passing.
1169 blocked_register_pairs_[A1_A2] = true;
1170
1171 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1172 blocked_core_registers_[ZERO] = true;
1173 blocked_core_registers_[K0] = true;
1174 blocked_core_registers_[K1] = true;
1175 blocked_core_registers_[GP] = true;
1176 blocked_core_registers_[SP] = true;
1177 blocked_core_registers_[RA] = true;
1178
1179 // AT and TMP(T8) are used as temporary/scratch registers
1180 // (similar to how AT is used by MIPS assemblers).
1181 blocked_core_registers_[AT] = true;
1182 blocked_core_registers_[TMP] = true;
1183 blocked_fpu_registers_[FTMP] = true;
1184
1185 // Reserve suspend and thread registers.
1186 blocked_core_registers_[S0] = true;
1187 blocked_core_registers_[TR] = true;
1188
1189 // Reserve T9 for function calls
1190 blocked_core_registers_[T9] = true;
1191
1192 // Reserve odd-numbered FPU registers.
1193 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1194 blocked_fpu_registers_[i] = true;
1195 }
1196
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001197 if (GetGraph()->IsDebuggable()) {
1198 // Stubs do not save callee-save floating point registers. If the graph
1199 // is debuggable, we need to deal with these registers differently. For
1200 // now, just block them.
1201 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1202 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1203 }
1204 }
1205
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001206 UpdateBlockedPairRegisters();
1207}
1208
1209void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1210 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1211 MipsManagedRegister current =
1212 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1213 if (blocked_core_registers_[current.AsRegisterPairLow()]
1214 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1215 blocked_register_pairs_[i] = true;
1216 }
1217 }
1218}
1219
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001220size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1221 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1222 return kMipsWordSize;
1223}
1224
1225size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1226 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1227 return kMipsWordSize;
1228}
1229
1230size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1231 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1232 return kMipsDoublewordSize;
1233}
1234
1235size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1236 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1237 return kMipsDoublewordSize;
1238}
1239
1240void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001241 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242}
1243
1244void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001245 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001246}
1247
Serban Constantinescufca16662016-07-14 09:21:59 +01001248constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1249
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001250void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1251 HInstruction* instruction,
1252 uint32_t dex_pc,
1253 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001254 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001255 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001256 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001257 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001258 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001259 // Reserve argument space on stack (for $a0-$a3) for
1260 // entrypoints that directly reference native implementations.
1261 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001262 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001263 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001264 } else {
1265 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001266 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001267 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001268 if (EntrypointRequiresStackMap(entrypoint)) {
1269 RecordPcInfo(instruction, dex_pc, slow_path);
1270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001271}
1272
1273void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1274 Register class_reg) {
1275 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1276 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1277 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1278 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1279 __ Sync(0);
1280 __ Bind(slow_path->GetExitLabel());
1281}
1282
1283void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1284 __ Sync(0); // Only stype 0 is supported.
1285}
1286
1287void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1288 HBasicBlock* successor) {
1289 SuspendCheckSlowPathMIPS* slow_path =
1290 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1291 codegen_->AddSlowPath(slow_path);
1292
1293 __ LoadFromOffset(kLoadUnsignedHalfword,
1294 TMP,
1295 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001296 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 if (successor == nullptr) {
1298 __ Bnez(TMP, slow_path->GetEntryLabel());
1299 __ Bind(slow_path->GetReturnLabel());
1300 } else {
1301 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1302 __ B(slow_path->GetEntryLabel());
1303 // slow_path will return to GetLabelOf(successor).
1304 }
1305}
1306
1307InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1308 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001309 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310 assembler_(codegen->GetAssembler()),
1311 codegen_(codegen) {}
1312
1313void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1314 DCHECK_EQ(instruction->InputCount(), 2U);
1315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1316 Primitive::Type type = instruction->GetResultType();
1317 switch (type) {
1318 case Primitive::kPrimInt: {
1319 locations->SetInAt(0, Location::RequiresRegister());
1320 HInstruction* right = instruction->InputAt(1);
1321 bool can_use_imm = false;
1322 if (right->IsConstant()) {
1323 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1324 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1325 can_use_imm = IsUint<16>(imm);
1326 } else if (instruction->IsAdd()) {
1327 can_use_imm = IsInt<16>(imm);
1328 } else {
1329 DCHECK(instruction->IsSub());
1330 can_use_imm = IsInt<16>(-imm);
1331 }
1332 }
1333 if (can_use_imm)
1334 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1335 else
1336 locations->SetInAt(1, Location::RequiresRegister());
1337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1338 break;
1339 }
1340
1341 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001342 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001343 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345 break;
1346 }
1347
1348 case Primitive::kPrimFloat:
1349 case Primitive::kPrimDouble:
1350 DCHECK(instruction->IsAdd() || instruction->IsSub());
1351 locations->SetInAt(0, Location::RequiresFpuRegister());
1352 locations->SetInAt(1, Location::RequiresFpuRegister());
1353 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1354 break;
1355
1356 default:
1357 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1358 }
1359}
1360
1361void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1362 Primitive::Type type = instruction->GetType();
1363 LocationSummary* locations = instruction->GetLocations();
1364
1365 switch (type) {
1366 case Primitive::kPrimInt: {
1367 Register dst = locations->Out().AsRegister<Register>();
1368 Register lhs = locations->InAt(0).AsRegister<Register>();
1369 Location rhs_location = locations->InAt(1);
1370
1371 Register rhs_reg = ZERO;
1372 int32_t rhs_imm = 0;
1373 bool use_imm = rhs_location.IsConstant();
1374 if (use_imm) {
1375 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1376 } else {
1377 rhs_reg = rhs_location.AsRegister<Register>();
1378 }
1379
1380 if (instruction->IsAnd()) {
1381 if (use_imm)
1382 __ Andi(dst, lhs, rhs_imm);
1383 else
1384 __ And(dst, lhs, rhs_reg);
1385 } else if (instruction->IsOr()) {
1386 if (use_imm)
1387 __ Ori(dst, lhs, rhs_imm);
1388 else
1389 __ Or(dst, lhs, rhs_reg);
1390 } else if (instruction->IsXor()) {
1391 if (use_imm)
1392 __ Xori(dst, lhs, rhs_imm);
1393 else
1394 __ Xor(dst, lhs, rhs_reg);
1395 } else if (instruction->IsAdd()) {
1396 if (use_imm)
1397 __ Addiu(dst, lhs, rhs_imm);
1398 else
1399 __ Addu(dst, lhs, rhs_reg);
1400 } else {
1401 DCHECK(instruction->IsSub());
1402 if (use_imm)
1403 __ Addiu(dst, lhs, -rhs_imm);
1404 else
1405 __ Subu(dst, lhs, rhs_reg);
1406 }
1407 break;
1408 }
1409
1410 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1412 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1413 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1414 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001415 Location rhs_location = locations->InAt(1);
1416 bool use_imm = rhs_location.IsConstant();
1417 if (!use_imm) {
1418 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1419 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1420 if (instruction->IsAnd()) {
1421 __ And(dst_low, lhs_low, rhs_low);
1422 __ And(dst_high, lhs_high, rhs_high);
1423 } else if (instruction->IsOr()) {
1424 __ Or(dst_low, lhs_low, rhs_low);
1425 __ Or(dst_high, lhs_high, rhs_high);
1426 } else if (instruction->IsXor()) {
1427 __ Xor(dst_low, lhs_low, rhs_low);
1428 __ Xor(dst_high, lhs_high, rhs_high);
1429 } else if (instruction->IsAdd()) {
1430 if (lhs_low == rhs_low) {
1431 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1432 __ Slt(TMP, lhs_low, ZERO);
1433 __ Addu(dst_low, lhs_low, rhs_low);
1434 } else {
1435 __ Addu(dst_low, lhs_low, rhs_low);
1436 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1437 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1438 }
1439 __ Addu(dst_high, lhs_high, rhs_high);
1440 __ Addu(dst_high, dst_high, TMP);
1441 } else {
1442 DCHECK(instruction->IsSub());
1443 __ Sltu(TMP, lhs_low, rhs_low);
1444 __ Subu(dst_low, lhs_low, rhs_low);
1445 __ Subu(dst_high, lhs_high, rhs_high);
1446 __ Subu(dst_high, dst_high, TMP);
1447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001448 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001449 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1450 if (instruction->IsOr()) {
1451 uint32_t low = Low32Bits(value);
1452 uint32_t high = High32Bits(value);
1453 if (IsUint<16>(low)) {
1454 if (dst_low != lhs_low || low != 0) {
1455 __ Ori(dst_low, lhs_low, low);
1456 }
1457 } else {
1458 __ LoadConst32(TMP, low);
1459 __ Or(dst_low, lhs_low, TMP);
1460 }
1461 if (IsUint<16>(high)) {
1462 if (dst_high != lhs_high || high != 0) {
1463 __ Ori(dst_high, lhs_high, high);
1464 }
1465 } else {
1466 if (high != low) {
1467 __ LoadConst32(TMP, high);
1468 }
1469 __ Or(dst_high, lhs_high, TMP);
1470 }
1471 } else if (instruction->IsXor()) {
1472 uint32_t low = Low32Bits(value);
1473 uint32_t high = High32Bits(value);
1474 if (IsUint<16>(low)) {
1475 if (dst_low != lhs_low || low != 0) {
1476 __ Xori(dst_low, lhs_low, low);
1477 }
1478 } else {
1479 __ LoadConst32(TMP, low);
1480 __ Xor(dst_low, lhs_low, TMP);
1481 }
1482 if (IsUint<16>(high)) {
1483 if (dst_high != lhs_high || high != 0) {
1484 __ Xori(dst_high, lhs_high, high);
1485 }
1486 } else {
1487 if (high != low) {
1488 __ LoadConst32(TMP, high);
1489 }
1490 __ Xor(dst_high, lhs_high, TMP);
1491 }
1492 } else if (instruction->IsAnd()) {
1493 uint32_t low = Low32Bits(value);
1494 uint32_t high = High32Bits(value);
1495 if (IsUint<16>(low)) {
1496 __ Andi(dst_low, lhs_low, low);
1497 } else if (low != 0xFFFFFFFF) {
1498 __ LoadConst32(TMP, low);
1499 __ And(dst_low, lhs_low, TMP);
1500 } else if (dst_low != lhs_low) {
1501 __ Move(dst_low, lhs_low);
1502 }
1503 if (IsUint<16>(high)) {
1504 __ Andi(dst_high, lhs_high, high);
1505 } else if (high != 0xFFFFFFFF) {
1506 if (high != low) {
1507 __ LoadConst32(TMP, high);
1508 }
1509 __ And(dst_high, lhs_high, TMP);
1510 } else if (dst_high != lhs_high) {
1511 __ Move(dst_high, lhs_high);
1512 }
1513 } else {
1514 if (instruction->IsSub()) {
1515 value = -value;
1516 } else {
1517 DCHECK(instruction->IsAdd());
1518 }
1519 int32_t low = Low32Bits(value);
1520 int32_t high = High32Bits(value);
1521 if (IsInt<16>(low)) {
1522 if (dst_low != lhs_low || low != 0) {
1523 __ Addiu(dst_low, lhs_low, low);
1524 }
1525 if (low != 0) {
1526 __ Sltiu(AT, dst_low, low);
1527 }
1528 } else {
1529 __ LoadConst32(TMP, low);
1530 __ Addu(dst_low, lhs_low, TMP);
1531 __ Sltu(AT, dst_low, TMP);
1532 }
1533 if (IsInt<16>(high)) {
1534 if (dst_high != lhs_high || high != 0) {
1535 __ Addiu(dst_high, lhs_high, high);
1536 }
1537 } else {
1538 if (high != low) {
1539 __ LoadConst32(TMP, high);
1540 }
1541 __ Addu(dst_high, lhs_high, TMP);
1542 }
1543 if (low != 0) {
1544 __ Addu(dst_high, dst_high, AT);
1545 }
1546 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001547 }
1548 break;
1549 }
1550
1551 case Primitive::kPrimFloat:
1552 case Primitive::kPrimDouble: {
1553 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1554 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1555 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1556 if (instruction->IsAdd()) {
1557 if (type == Primitive::kPrimFloat) {
1558 __ AddS(dst, lhs, rhs);
1559 } else {
1560 __ AddD(dst, lhs, rhs);
1561 }
1562 } else {
1563 DCHECK(instruction->IsSub());
1564 if (type == Primitive::kPrimFloat) {
1565 __ SubS(dst, lhs, rhs);
1566 } else {
1567 __ SubD(dst, lhs, rhs);
1568 }
1569 }
1570 break;
1571 }
1572
1573 default:
1574 LOG(FATAL) << "Unexpected binary operation type " << type;
1575 }
1576}
1577
1578void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001579 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001580
1581 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1582 Primitive::Type type = instr->GetResultType();
1583 switch (type) {
1584 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001585 locations->SetInAt(0, Location::RequiresRegister());
1586 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1588 break;
1589 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1592 locations->SetOut(Location::RequiresRegister());
1593 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001594 default:
1595 LOG(FATAL) << "Unexpected shift type " << type;
1596 }
1597}
1598
1599static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1600
1601void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001602 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 LocationSummary* locations = instr->GetLocations();
1604 Primitive::Type type = instr->GetType();
1605
1606 Location rhs_location = locations->InAt(1);
1607 bool use_imm = rhs_location.IsConstant();
1608 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1609 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001610 const uint32_t shift_mask =
1611 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001612 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001613 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1614 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001615
1616 switch (type) {
1617 case Primitive::kPrimInt: {
1618 Register dst = locations->Out().AsRegister<Register>();
1619 Register lhs = locations->InAt(0).AsRegister<Register>();
1620 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001621 if (shift_value == 0) {
1622 if (dst != lhs) {
1623 __ Move(dst, lhs);
1624 }
1625 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001626 __ Sll(dst, lhs, shift_value);
1627 } else if (instr->IsShr()) {
1628 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001629 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001630 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001631 } else {
1632 if (has_ins_rotr) {
1633 __ Rotr(dst, lhs, shift_value);
1634 } else {
1635 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1636 __ Srl(dst, lhs, shift_value);
1637 __ Or(dst, dst, TMP);
1638 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001639 }
1640 } else {
1641 if (instr->IsShl()) {
1642 __ Sllv(dst, lhs, rhs_reg);
1643 } else if (instr->IsShr()) {
1644 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001645 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001646 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001647 } else {
1648 if (has_ins_rotr) {
1649 __ Rotrv(dst, lhs, rhs_reg);
1650 } else {
1651 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001652 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1653 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1654 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1655 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1656 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001657 __ Sllv(TMP, lhs, TMP);
1658 __ Srlv(dst, lhs, rhs_reg);
1659 __ Or(dst, dst, TMP);
1660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001661 }
1662 }
1663 break;
1664 }
1665
1666 case Primitive::kPrimLong: {
1667 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1668 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1669 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1670 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1671 if (use_imm) {
1672 if (shift_value == 0) {
1673 codegen_->Move64(locations->Out(), locations->InAt(0));
1674 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001675 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001676 if (instr->IsShl()) {
1677 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1678 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1679 __ Sll(dst_low, lhs_low, shift_value);
1680 } else if (instr->IsShr()) {
1681 __ Srl(dst_low, lhs_low, shift_value);
1682 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1683 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001684 } else if (instr->IsUShr()) {
1685 __ Srl(dst_low, lhs_low, shift_value);
1686 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1687 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001688 } else {
1689 __ Srl(dst_low, lhs_low, shift_value);
1690 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1691 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001692 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001693 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001694 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001695 if (instr->IsShl()) {
1696 __ Sll(dst_low, lhs_low, shift_value);
1697 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1698 __ Sll(dst_high, lhs_high, shift_value);
1699 __ Or(dst_high, dst_high, TMP);
1700 } else if (instr->IsShr()) {
1701 __ Sra(dst_high, lhs_high, shift_value);
1702 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1703 __ Srl(dst_low, lhs_low, shift_value);
1704 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001706 __ Srl(dst_high, lhs_high, shift_value);
1707 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1708 __ Srl(dst_low, lhs_low, shift_value);
1709 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001710 } else {
1711 __ Srl(TMP, lhs_low, shift_value);
1712 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1713 __ Or(dst_low, dst_low, TMP);
1714 __ Srl(TMP, lhs_high, shift_value);
1715 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1716 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001717 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 }
1719 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001720 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001722 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001723 __ Move(dst_low, ZERO);
1724 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001725 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001726 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001727 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001728 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001729 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001730 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001731 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001732 // 64-bit rotation by 32 is just a swap.
1733 __ Move(dst_low, lhs_high);
1734 __ Move(dst_high, lhs_low);
1735 } else {
1736 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001737 __ Srl(dst_low, lhs_high, shift_value_high);
1738 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1739 __ Srl(dst_high, lhs_low, shift_value_high);
1740 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001741 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001742 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1743 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001744 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001745 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1746 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001747 __ Or(dst_high, dst_high, TMP);
1748 }
1749 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001750 }
1751 }
1752 } else {
1753 MipsLabel done;
1754 if (instr->IsShl()) {
1755 __ Sllv(dst_low, lhs_low, rhs_reg);
1756 __ Nor(AT, ZERO, rhs_reg);
1757 __ Srl(TMP, lhs_low, 1);
1758 __ Srlv(TMP, TMP, AT);
1759 __ Sllv(dst_high, lhs_high, rhs_reg);
1760 __ Or(dst_high, dst_high, TMP);
1761 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1762 __ Beqz(TMP, &done);
1763 __ Move(dst_high, dst_low);
1764 __ Move(dst_low, ZERO);
1765 } else if (instr->IsShr()) {
1766 __ Srav(dst_high, lhs_high, rhs_reg);
1767 __ Nor(AT, ZERO, rhs_reg);
1768 __ Sll(TMP, lhs_high, 1);
1769 __ Sllv(TMP, TMP, AT);
1770 __ Srlv(dst_low, lhs_low, rhs_reg);
1771 __ Or(dst_low, dst_low, TMP);
1772 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1773 __ Beqz(TMP, &done);
1774 __ Move(dst_low, dst_high);
1775 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001776 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001777 __ Srlv(dst_high, lhs_high, rhs_reg);
1778 __ Nor(AT, ZERO, rhs_reg);
1779 __ Sll(TMP, lhs_high, 1);
1780 __ Sllv(TMP, TMP, AT);
1781 __ Srlv(dst_low, lhs_low, rhs_reg);
1782 __ Or(dst_low, dst_low, TMP);
1783 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1784 __ Beqz(TMP, &done);
1785 __ Move(dst_low, dst_high);
1786 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001787 } else {
1788 __ Nor(AT, ZERO, rhs_reg);
1789 __ Srlv(TMP, lhs_low, rhs_reg);
1790 __ Sll(dst_low, lhs_high, 1);
1791 __ Sllv(dst_low, dst_low, AT);
1792 __ Or(dst_low, dst_low, TMP);
1793 __ Srlv(TMP, lhs_high, rhs_reg);
1794 __ Sll(dst_high, lhs_low, 1);
1795 __ Sllv(dst_high, dst_high, AT);
1796 __ Or(dst_high, dst_high, TMP);
1797 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1798 __ Beqz(TMP, &done);
1799 __ Move(TMP, dst_high);
1800 __ Move(dst_high, dst_low);
1801 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001802 }
1803 __ Bind(&done);
1804 }
1805 break;
1806 }
1807
1808 default:
1809 LOG(FATAL) << "Unexpected shift operation type " << type;
1810 }
1811}
1812
1813void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1814 HandleBinaryOp(instruction);
1815}
1816
1817void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1818 HandleBinaryOp(instruction);
1819}
1820
1821void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1822 HandleBinaryOp(instruction);
1823}
1824
1825void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1826 HandleBinaryOp(instruction);
1827}
1828
1829void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1830 LocationSummary* locations =
1831 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1834 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1835 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1836 } else {
1837 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1838 }
1839}
1840
Alexey Frunze2923db72016-08-20 01:55:47 -07001841auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1842 auto null_checker = [this, instruction]() {
1843 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1844 };
1845 return null_checker;
1846}
1847
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1849 LocationSummary* locations = instruction->GetLocations();
1850 Register obj = locations->InAt(0).AsRegister<Register>();
1851 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001852 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001853 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001855 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001856 switch (type) {
1857 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 Register out = locations->Out().AsRegister<Register>();
1859 if (index.IsConstant()) {
1860 size_t offset =
1861 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001862 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 } else {
1864 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001865 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 Register out = locations->Out().AsRegister<Register>();
1872 if (index.IsConstant()) {
1873 size_t offset =
1874 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001875 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 } else {
1877 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001878 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879 }
1880 break;
1881 }
1882
1883 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884 Register out = locations->Out().AsRegister<Register>();
1885 if (index.IsConstant()) {
1886 size_t offset =
1887 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001888 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001889 } else {
1890 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1891 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001892 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893 }
1894 break;
1895 }
1896
1897 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001898 Register out = locations->Out().AsRegister<Register>();
1899 if (index.IsConstant()) {
1900 size_t offset =
1901 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001902 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001903 } else {
1904 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1905 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001906 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001907 }
1908 break;
1909 }
1910
1911 case Primitive::kPrimInt:
1912 case Primitive::kPrimNot: {
1913 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914 Register out = locations->Out().AsRegister<Register>();
1915 if (index.IsConstant()) {
1916 size_t offset =
1917 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001918 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 } else {
1920 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1921 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001922 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923 }
1924 break;
1925 }
1926
1927 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001928 Register out = locations->Out().AsRegisterPairLow<Register>();
1929 if (index.IsConstant()) {
1930 size_t offset =
1931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001932 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933 } else {
1934 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1935 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001936 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937 }
1938 break;
1939 }
1940
1941 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1943 if (index.IsConstant()) {
1944 size_t offset =
1945 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001946 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947 } else {
1948 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1949 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001950 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951 }
1952 break;
1953 }
1954
1955 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1957 if (index.IsConstant()) {
1958 size_t offset =
1959 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001960 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961 } else {
1962 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1963 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001964 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001965 }
1966 break;
1967 }
1968
1969 case Primitive::kPrimVoid:
1970 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1971 UNREACHABLE();
1972 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973}
1974
1975void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1977 locations->SetInAt(0, Location::RequiresRegister());
1978 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1979}
1980
1981void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1982 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001983 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 Register obj = locations->InAt(0).AsRegister<Register>();
1985 Register out = locations->Out().AsRegister<Register>();
1986 __ LoadFromOffset(kLoadWord, out, obj, offset);
1987 codegen_->MaybeRecordImplicitNullCheck(instruction);
1988}
1989
Alexey Frunzef58b2482016-09-02 22:14:06 -07001990Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1991 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1992 ? Location::ConstantLocation(instruction->AsConstant())
1993 : Location::RequiresRegister();
1994}
1995
1996Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1997 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1998 // We can store a non-zero float or double constant without first loading it into the FPU,
1999 // but we should only prefer this if the constant has a single use.
2000 if (instruction->IsConstant() &&
2001 (instruction->AsConstant()->IsZeroBitPattern() ||
2002 instruction->GetUses().HasExactlyOneElement())) {
2003 return Location::ConstantLocation(instruction->AsConstant());
2004 // Otherwise fall through and require an FPU register for the constant.
2005 }
2006 return Location::RequiresFpuRegister();
2007}
2008
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002009void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002010 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002011 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2012 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002013 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002014 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002015 InvokeRuntimeCallingConvention calling_convention;
2016 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2017 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2018 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2019 } else {
2020 locations->SetInAt(0, Location::RequiresRegister());
2021 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2022 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002023 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002025 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 }
2027 }
2028}
2029
2030void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2031 LocationSummary* locations = instruction->GetLocations();
2032 Register obj = locations->InAt(0).AsRegister<Register>();
2033 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002034 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 Primitive::Type value_type = instruction->GetComponentType();
2036 bool needs_runtime_call = locations->WillCall();
2037 bool needs_write_barrier =
2038 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002039 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002040 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041
2042 switch (value_type) {
2043 case Primitive::kPrimBoolean:
2044 case Primitive::kPrimByte: {
2045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002046 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002047 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002048 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002049 __ Addu(base_reg, obj, index.AsRegister<Register>());
2050 }
2051 if (value_location.IsConstant()) {
2052 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2053 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2054 } else {
2055 Register value = value_location.AsRegister<Register>();
2056 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002057 }
2058 break;
2059 }
2060
2061 case Primitive::kPrimShort:
2062 case Primitive::kPrimChar: {
2063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002065 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002067 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2068 __ Addu(base_reg, obj, base_reg);
2069 }
2070 if (value_location.IsConstant()) {
2071 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2072 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2073 } else {
2074 Register value = value_location.AsRegister<Register>();
2075 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 }
2077 break;
2078 }
2079
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimNot: {
2082 if (!needs_runtime_call) {
2083 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002084 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002085 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002087 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2088 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002090 if (value_location.IsConstant()) {
2091 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2092 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2093 DCHECK(!needs_write_barrier);
2094 } else {
2095 Register value = value_location.AsRegister<Register>();
2096 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2097 if (needs_write_barrier) {
2098 DCHECK_EQ(value_type, Primitive::kPrimNot);
2099 codegen_->MarkGCCard(obj, value);
2100 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002101 }
2102 } else {
2103 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002104 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002105 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2106 }
2107 break;
2108 }
2109
2110 case Primitive::kPrimLong: {
2111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002112 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002113 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002114 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002115 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2116 __ Addu(base_reg, obj, base_reg);
2117 }
2118 if (value_location.IsConstant()) {
2119 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2120 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2121 } else {
2122 Register value = value_location.AsRegisterPairLow<Register>();
2123 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 }
2125 break;
2126 }
2127
2128 case Primitive::kPrimFloat: {
2129 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002131 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002132 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002133 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2134 __ Addu(base_reg, obj, base_reg);
2135 }
2136 if (value_location.IsConstant()) {
2137 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2138 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2139 } else {
2140 FRegister value = value_location.AsFpuRegister<FRegister>();
2141 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142 }
2143 break;
2144 }
2145
2146 case Primitive::kPrimDouble: {
2147 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002149 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002150 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002151 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2152 __ Addu(base_reg, obj, base_reg);
2153 }
2154 if (value_location.IsConstant()) {
2155 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2156 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2157 } else {
2158 FRegister value = value_location.AsFpuRegister<FRegister>();
2159 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002160 }
2161 break;
2162 }
2163
2164 case Primitive::kPrimVoid:
2165 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2166 UNREACHABLE();
2167 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002168}
2169
2170void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002171 RegisterSet caller_saves = RegisterSet::Empty();
2172 InvokeRuntimeCallingConvention calling_convention;
2173 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2174 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2175 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002176 locations->SetInAt(0, Location::RequiresRegister());
2177 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002178}
2179
2180void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2181 LocationSummary* locations = instruction->GetLocations();
2182 BoundsCheckSlowPathMIPS* slow_path =
2183 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2184 codegen_->AddSlowPath(slow_path);
2185
2186 Register index = locations->InAt(0).AsRegister<Register>();
2187 Register length = locations->InAt(1).AsRegister<Register>();
2188
2189 // length is limited by the maximum positive signed 32-bit integer.
2190 // Unsigned comparison of length and index checks for index < 0
2191 // and for length <= index simultaneously.
2192 __ Bgeu(index, length, slow_path->GetEntryLabel());
2193}
2194
2195void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2197 instruction,
2198 LocationSummary::kCallOnSlowPath);
2199 locations->SetInAt(0, Location::RequiresRegister());
2200 locations->SetInAt(1, Location::RequiresRegister());
2201 // Note that TypeCheckSlowPathMIPS uses this register too.
2202 locations->AddTemp(Location::RequiresRegister());
2203}
2204
2205void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2206 LocationSummary* locations = instruction->GetLocations();
2207 Register obj = locations->InAt(0).AsRegister<Register>();
2208 Register cls = locations->InAt(1).AsRegister<Register>();
2209 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2210
2211 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2212 codegen_->AddSlowPath(slow_path);
2213
2214 // TODO: avoid this check if we know obj is not null.
2215 __ Beqz(obj, slow_path->GetExitLabel());
2216 // Compare the class of `obj` with `cls`.
2217 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2218 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2219 __ Bind(slow_path->GetExitLabel());
2220}
2221
2222void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2223 LocationSummary* locations =
2224 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2225 locations->SetInAt(0, Location::RequiresRegister());
2226 if (check->HasUses()) {
2227 locations->SetOut(Location::SameAsFirstInput());
2228 }
2229}
2230
2231void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2232 // We assume the class is not null.
2233 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2234 check->GetLoadClass(),
2235 check,
2236 check->GetDexPc(),
2237 true);
2238 codegen_->AddSlowPath(slow_path);
2239 GenerateClassInitializationCheck(slow_path,
2240 check->GetLocations()->InAt(0).AsRegister<Register>());
2241}
2242
2243void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2244 Primitive::Type in_type = compare->InputAt(0)->GetType();
2245
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002246 LocationSummary* locations =
2247 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002248
2249 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002250 case Primitive::kPrimBoolean:
2251 case Primitive::kPrimByte:
2252 case Primitive::kPrimShort:
2253 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002254 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002255 locations->SetInAt(0, Location::RequiresRegister());
2256 locations->SetInAt(1, Location::RequiresRegister());
2257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2258 break;
2259
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 case Primitive::kPrimLong:
2261 locations->SetInAt(0, Location::RequiresRegister());
2262 locations->SetInAt(1, Location::RequiresRegister());
2263 // Output overlaps because it is written before doing the low comparison.
2264 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2265 break;
2266
2267 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002268 case Primitive::kPrimDouble:
2269 locations->SetInAt(0, Location::RequiresFpuRegister());
2270 locations->SetInAt(1, Location::RequiresFpuRegister());
2271 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002273
2274 default:
2275 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2276 }
2277}
2278
2279void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2280 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002281 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002283 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002284
2285 // 0 if: left == right
2286 // 1 if: left > right
2287 // -1 if: left < right
2288 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002289 case Primitive::kPrimBoolean:
2290 case Primitive::kPrimByte:
2291 case Primitive::kPrimShort:
2292 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002293 case Primitive::kPrimInt: {
2294 Register lhs = locations->InAt(0).AsRegister<Register>();
2295 Register rhs = locations->InAt(1).AsRegister<Register>();
2296 __ Slt(TMP, lhs, rhs);
2297 __ Slt(res, rhs, lhs);
2298 __ Subu(res, res, TMP);
2299 break;
2300 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 case Primitive::kPrimLong: {
2302 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2304 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2305 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2306 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2307 // TODO: more efficient (direct) comparison with a constant.
2308 __ Slt(TMP, lhs_high, rhs_high);
2309 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2310 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2311 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2312 __ Sltu(TMP, lhs_low, rhs_low);
2313 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2314 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2315 __ Bind(&done);
2316 break;
2317 }
2318
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002319 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002320 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002321 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2322 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2323 MipsLabel done;
2324 if (isR6) {
2325 __ CmpEqS(FTMP, lhs, rhs);
2326 __ LoadConst32(res, 0);
2327 __ Bc1nez(FTMP, &done);
2328 if (gt_bias) {
2329 __ CmpLtS(FTMP, lhs, rhs);
2330 __ LoadConst32(res, -1);
2331 __ Bc1nez(FTMP, &done);
2332 __ LoadConst32(res, 1);
2333 } else {
2334 __ CmpLtS(FTMP, rhs, lhs);
2335 __ LoadConst32(res, 1);
2336 __ Bc1nez(FTMP, &done);
2337 __ LoadConst32(res, -1);
2338 }
2339 } else {
2340 if (gt_bias) {
2341 __ ColtS(0, lhs, rhs);
2342 __ LoadConst32(res, -1);
2343 __ Bc1t(0, &done);
2344 __ CeqS(0, lhs, rhs);
2345 __ LoadConst32(res, 1);
2346 __ Movt(res, ZERO, 0);
2347 } else {
2348 __ ColtS(0, rhs, lhs);
2349 __ LoadConst32(res, 1);
2350 __ Bc1t(0, &done);
2351 __ CeqS(0, lhs, rhs);
2352 __ LoadConst32(res, -1);
2353 __ Movt(res, ZERO, 0);
2354 }
2355 }
2356 __ Bind(&done);
2357 break;
2358 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002359 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002360 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002361 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2362 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2363 MipsLabel done;
2364 if (isR6) {
2365 __ CmpEqD(FTMP, lhs, rhs);
2366 __ LoadConst32(res, 0);
2367 __ Bc1nez(FTMP, &done);
2368 if (gt_bias) {
2369 __ CmpLtD(FTMP, lhs, rhs);
2370 __ LoadConst32(res, -1);
2371 __ Bc1nez(FTMP, &done);
2372 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002373 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002374 __ CmpLtD(FTMP, rhs, lhs);
2375 __ LoadConst32(res, 1);
2376 __ Bc1nez(FTMP, &done);
2377 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 }
2379 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002380 if (gt_bias) {
2381 __ ColtD(0, lhs, rhs);
2382 __ LoadConst32(res, -1);
2383 __ Bc1t(0, &done);
2384 __ CeqD(0, lhs, rhs);
2385 __ LoadConst32(res, 1);
2386 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002387 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002388 __ ColtD(0, rhs, lhs);
2389 __ LoadConst32(res, 1);
2390 __ Bc1t(0, &done);
2391 __ CeqD(0, lhs, rhs);
2392 __ LoadConst32(res, -1);
2393 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002394 }
2395 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002396 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002397 break;
2398 }
2399
2400 default:
2401 LOG(FATAL) << "Unimplemented compare type " << in_type;
2402 }
2403}
2404
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002405void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002407 switch (instruction->InputAt(0)->GetType()) {
2408 default:
2409 case Primitive::kPrimLong:
2410 locations->SetInAt(0, Location::RequiresRegister());
2411 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2412 break;
2413
2414 case Primitive::kPrimFloat:
2415 case Primitive::kPrimDouble:
2416 locations->SetInAt(0, Location::RequiresFpuRegister());
2417 locations->SetInAt(1, Location::RequiresFpuRegister());
2418 break;
2419 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002420 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2422 }
2423}
2424
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002425void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002426 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427 return;
2428 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002430 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002431 LocationSummary* locations = instruction->GetLocations();
2432 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002433 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002435 switch (type) {
2436 default:
2437 // Integer case.
2438 GenerateIntCompare(instruction->GetCondition(), locations);
2439 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002441 case Primitive::kPrimLong:
2442 // TODO: don't use branches.
2443 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002444 break;
2445
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002446 case Primitive::kPrimFloat:
2447 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002448 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2449 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002450 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002451
2452 // Convert the branches into the result.
2453 MipsLabel done;
2454
2455 // False case: result = 0.
2456 __ LoadConst32(dst, 0);
2457 __ B(&done);
2458
2459 // True case: result = 1.
2460 __ Bind(&true_label);
2461 __ LoadConst32(dst, 1);
2462 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002463}
2464
Alexey Frunze7e99e052015-11-24 19:28:01 -08002465void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2466 DCHECK(instruction->IsDiv() || instruction->IsRem());
2467 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2468
2469 LocationSummary* locations = instruction->GetLocations();
2470 Location second = locations->InAt(1);
2471 DCHECK(second.IsConstant());
2472
2473 Register out = locations->Out().AsRegister<Register>();
2474 Register dividend = locations->InAt(0).AsRegister<Register>();
2475 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2476 DCHECK(imm == 1 || imm == -1);
2477
2478 if (instruction->IsRem()) {
2479 __ Move(out, ZERO);
2480 } else {
2481 if (imm == -1) {
2482 __ Subu(out, ZERO, dividend);
2483 } else if (out != dividend) {
2484 __ Move(out, dividend);
2485 }
2486 }
2487}
2488
2489void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2490 DCHECK(instruction->IsDiv() || instruction->IsRem());
2491 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2492
2493 LocationSummary* locations = instruction->GetLocations();
2494 Location second = locations->InAt(1);
2495 DCHECK(second.IsConstant());
2496
2497 Register out = locations->Out().AsRegister<Register>();
2498 Register dividend = locations->InAt(0).AsRegister<Register>();
2499 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002500 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002501 int ctz_imm = CTZ(abs_imm);
2502
2503 if (instruction->IsDiv()) {
2504 if (ctz_imm == 1) {
2505 // Fast path for division by +/-2, which is very common.
2506 __ Srl(TMP, dividend, 31);
2507 } else {
2508 __ Sra(TMP, dividend, 31);
2509 __ Srl(TMP, TMP, 32 - ctz_imm);
2510 }
2511 __ Addu(out, dividend, TMP);
2512 __ Sra(out, out, ctz_imm);
2513 if (imm < 0) {
2514 __ Subu(out, ZERO, out);
2515 }
2516 } else {
2517 if (ctz_imm == 1) {
2518 // Fast path for modulo +/-2, which is very common.
2519 __ Sra(TMP, dividend, 31);
2520 __ Subu(out, dividend, TMP);
2521 __ Andi(out, out, 1);
2522 __ Addu(out, out, TMP);
2523 } else {
2524 __ Sra(TMP, dividend, 31);
2525 __ Srl(TMP, TMP, 32 - ctz_imm);
2526 __ Addu(out, dividend, TMP);
2527 if (IsUint<16>(abs_imm - 1)) {
2528 __ Andi(out, out, abs_imm - 1);
2529 } else {
2530 __ Sll(out, out, 32 - ctz_imm);
2531 __ Srl(out, out, 32 - ctz_imm);
2532 }
2533 __ Subu(out, out, TMP);
2534 }
2535 }
2536}
2537
2538void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2539 DCHECK(instruction->IsDiv() || instruction->IsRem());
2540 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2541
2542 LocationSummary* locations = instruction->GetLocations();
2543 Location second = locations->InAt(1);
2544 DCHECK(second.IsConstant());
2545
2546 Register out = locations->Out().AsRegister<Register>();
2547 Register dividend = locations->InAt(0).AsRegister<Register>();
2548 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2549
2550 int64_t magic;
2551 int shift;
2552 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2553
2554 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2555
2556 __ LoadConst32(TMP, magic);
2557 if (isR6) {
2558 __ MuhR6(TMP, dividend, TMP);
2559 } else {
2560 __ MultR2(dividend, TMP);
2561 __ Mfhi(TMP);
2562 }
2563 if (imm > 0 && magic < 0) {
2564 __ Addu(TMP, TMP, dividend);
2565 } else if (imm < 0 && magic > 0) {
2566 __ Subu(TMP, TMP, dividend);
2567 }
2568
2569 if (shift != 0) {
2570 __ Sra(TMP, TMP, shift);
2571 }
2572
2573 if (instruction->IsDiv()) {
2574 __ Sra(out, TMP, 31);
2575 __ Subu(out, TMP, out);
2576 } else {
2577 __ Sra(AT, TMP, 31);
2578 __ Subu(AT, TMP, AT);
2579 __ LoadConst32(TMP, imm);
2580 if (isR6) {
2581 __ MulR6(TMP, AT, TMP);
2582 } else {
2583 __ MulR2(TMP, AT, TMP);
2584 }
2585 __ Subu(out, dividend, TMP);
2586 }
2587}
2588
2589void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2590 DCHECK(instruction->IsDiv() || instruction->IsRem());
2591 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2592
2593 LocationSummary* locations = instruction->GetLocations();
2594 Register out = locations->Out().AsRegister<Register>();
2595 Location second = locations->InAt(1);
2596
2597 if (second.IsConstant()) {
2598 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2599 if (imm == 0) {
2600 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2601 } else if (imm == 1 || imm == -1) {
2602 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002603 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002604 DivRemByPowerOfTwo(instruction);
2605 } else {
2606 DCHECK(imm <= -2 || imm >= 2);
2607 GenerateDivRemWithAnyConstant(instruction);
2608 }
2609 } else {
2610 Register dividend = locations->InAt(0).AsRegister<Register>();
2611 Register divisor = second.AsRegister<Register>();
2612 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2613 if (instruction->IsDiv()) {
2614 if (isR6) {
2615 __ DivR6(out, dividend, divisor);
2616 } else {
2617 __ DivR2(out, dividend, divisor);
2618 }
2619 } else {
2620 if (isR6) {
2621 __ ModR6(out, dividend, divisor);
2622 } else {
2623 __ ModR2(out, dividend, divisor);
2624 }
2625 }
2626 }
2627}
2628
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002629void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2630 Primitive::Type type = div->GetResultType();
2631 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002632 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633 : LocationSummary::kNoCall;
2634
2635 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2636
2637 switch (type) {
2638 case Primitive::kPrimInt:
2639 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002640 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2642 break;
2643
2644 case Primitive::kPrimLong: {
2645 InvokeRuntimeCallingConvention calling_convention;
2646 locations->SetInAt(0, Location::RegisterPairLocation(
2647 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2648 locations->SetInAt(1, Location::RegisterPairLocation(
2649 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2650 locations->SetOut(calling_convention.GetReturnLocation(type));
2651 break;
2652 }
2653
2654 case Primitive::kPrimFloat:
2655 case Primitive::kPrimDouble:
2656 locations->SetInAt(0, Location::RequiresFpuRegister());
2657 locations->SetInAt(1, Location::RequiresFpuRegister());
2658 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2659 break;
2660
2661 default:
2662 LOG(FATAL) << "Unexpected div type " << type;
2663 }
2664}
2665
2666void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2667 Primitive::Type type = instruction->GetType();
2668 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669
2670 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002671 case Primitive::kPrimInt:
2672 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002673 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002674 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002675 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002676 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2677 break;
2678 }
2679 case Primitive::kPrimFloat:
2680 case Primitive::kPrimDouble: {
2681 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2682 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2683 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2684 if (type == Primitive::kPrimFloat) {
2685 __ DivS(dst, lhs, rhs);
2686 } else {
2687 __ DivD(dst, lhs, rhs);
2688 }
2689 break;
2690 }
2691 default:
2692 LOG(FATAL) << "Unexpected div type " << type;
2693 }
2694}
2695
2696void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002697 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002698 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699}
2700
2701void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2702 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2703 codegen_->AddSlowPath(slow_path);
2704 Location value = instruction->GetLocations()->InAt(0);
2705 Primitive::Type type = instruction->GetType();
2706
2707 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002708 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002709 case Primitive::kPrimByte:
2710 case Primitive::kPrimChar:
2711 case Primitive::kPrimShort:
2712 case Primitive::kPrimInt: {
2713 if (value.IsConstant()) {
2714 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2715 __ B(slow_path->GetEntryLabel());
2716 } else {
2717 // A division by a non-null constant is valid. We don't need to perform
2718 // any check, so simply fall through.
2719 }
2720 } else {
2721 DCHECK(value.IsRegister()) << value;
2722 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2723 }
2724 break;
2725 }
2726 case Primitive::kPrimLong: {
2727 if (value.IsConstant()) {
2728 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2729 __ B(slow_path->GetEntryLabel());
2730 } else {
2731 // A division by a non-null constant is valid. We don't need to perform
2732 // any check, so simply fall through.
2733 }
2734 } else {
2735 DCHECK(value.IsRegisterPair()) << value;
2736 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2737 __ Beqz(TMP, slow_path->GetEntryLabel());
2738 }
2739 break;
2740 }
2741 default:
2742 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2743 }
2744}
2745
2746void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2747 LocationSummary* locations =
2748 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2749 locations->SetOut(Location::ConstantLocation(constant));
2750}
2751
2752void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2753 // Will be generated at use site.
2754}
2755
2756void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2757 exit->SetLocations(nullptr);
2758}
2759
2760void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2761}
2762
2763void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2764 LocationSummary* locations =
2765 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2766 locations->SetOut(Location::ConstantLocation(constant));
2767}
2768
2769void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2770 // Will be generated at use site.
2771}
2772
2773void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2774 got->SetLocations(nullptr);
2775}
2776
2777void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2778 DCHECK(!successor->IsExitBlock());
2779 HBasicBlock* block = got->GetBlock();
2780 HInstruction* previous = got->GetPrevious();
2781 HLoopInformation* info = block->GetLoopInformation();
2782
2783 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2784 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2785 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2786 return;
2787 }
2788 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2789 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2790 }
2791 if (!codegen_->GoesToNextBlock(block, successor)) {
2792 __ B(codegen_->GetLabelOf(successor));
2793 }
2794}
2795
2796void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2797 HandleGoto(got, got->GetSuccessor());
2798}
2799
2800void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2801 try_boundary->SetLocations(nullptr);
2802}
2803
2804void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2805 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2806 if (!successor->IsExitBlock()) {
2807 HandleGoto(try_boundary, successor);
2808 }
2809}
2810
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002811void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2812 LocationSummary* locations) {
2813 Register dst = locations->Out().AsRegister<Register>();
2814 Register lhs = locations->InAt(0).AsRegister<Register>();
2815 Location rhs_location = locations->InAt(1);
2816 Register rhs_reg = ZERO;
2817 int64_t rhs_imm = 0;
2818 bool use_imm = rhs_location.IsConstant();
2819 if (use_imm) {
2820 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2821 } else {
2822 rhs_reg = rhs_location.AsRegister<Register>();
2823 }
2824
2825 switch (cond) {
2826 case kCondEQ:
2827 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002828 if (use_imm && IsInt<16>(-rhs_imm)) {
2829 if (rhs_imm == 0) {
2830 if (cond == kCondEQ) {
2831 __ Sltiu(dst, lhs, 1);
2832 } else {
2833 __ Sltu(dst, ZERO, lhs);
2834 }
2835 } else {
2836 __ Addiu(dst, lhs, -rhs_imm);
2837 if (cond == kCondEQ) {
2838 __ Sltiu(dst, dst, 1);
2839 } else {
2840 __ Sltu(dst, ZERO, dst);
2841 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002842 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002843 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002844 if (use_imm && IsUint<16>(rhs_imm)) {
2845 __ Xori(dst, lhs, rhs_imm);
2846 } else {
2847 if (use_imm) {
2848 rhs_reg = TMP;
2849 __ LoadConst32(rhs_reg, rhs_imm);
2850 }
2851 __ Xor(dst, lhs, rhs_reg);
2852 }
2853 if (cond == kCondEQ) {
2854 __ Sltiu(dst, dst, 1);
2855 } else {
2856 __ Sltu(dst, ZERO, dst);
2857 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002858 }
2859 break;
2860
2861 case kCondLT:
2862 case kCondGE:
2863 if (use_imm && IsInt<16>(rhs_imm)) {
2864 __ Slti(dst, lhs, rhs_imm);
2865 } else {
2866 if (use_imm) {
2867 rhs_reg = TMP;
2868 __ LoadConst32(rhs_reg, rhs_imm);
2869 }
2870 __ Slt(dst, lhs, rhs_reg);
2871 }
2872 if (cond == kCondGE) {
2873 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2874 // only the slt instruction but no sge.
2875 __ Xori(dst, dst, 1);
2876 }
2877 break;
2878
2879 case kCondLE:
2880 case kCondGT:
2881 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2882 // Simulate lhs <= rhs via lhs < rhs + 1.
2883 __ Slti(dst, lhs, rhs_imm + 1);
2884 if (cond == kCondGT) {
2885 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2886 // only the slti instruction but no sgti.
2887 __ Xori(dst, dst, 1);
2888 }
2889 } else {
2890 if (use_imm) {
2891 rhs_reg = TMP;
2892 __ LoadConst32(rhs_reg, rhs_imm);
2893 }
2894 __ Slt(dst, rhs_reg, lhs);
2895 if (cond == kCondLE) {
2896 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2897 // only the slt instruction but no sle.
2898 __ Xori(dst, dst, 1);
2899 }
2900 }
2901 break;
2902
2903 case kCondB:
2904 case kCondAE:
2905 if (use_imm && IsInt<16>(rhs_imm)) {
2906 // Sltiu sign-extends its 16-bit immediate operand before
2907 // the comparison and thus lets us compare directly with
2908 // unsigned values in the ranges [0, 0x7fff] and
2909 // [0xffff8000, 0xffffffff].
2910 __ Sltiu(dst, lhs, rhs_imm);
2911 } else {
2912 if (use_imm) {
2913 rhs_reg = TMP;
2914 __ LoadConst32(rhs_reg, rhs_imm);
2915 }
2916 __ Sltu(dst, lhs, rhs_reg);
2917 }
2918 if (cond == kCondAE) {
2919 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2920 // only the sltu instruction but no sgeu.
2921 __ Xori(dst, dst, 1);
2922 }
2923 break;
2924
2925 case kCondBE:
2926 case kCondA:
2927 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2928 // Simulate lhs <= rhs via lhs < rhs + 1.
2929 // Note that this only works if rhs + 1 does not overflow
2930 // to 0, hence the check above.
2931 // Sltiu sign-extends its 16-bit immediate operand before
2932 // the comparison and thus lets us compare directly with
2933 // unsigned values in the ranges [0, 0x7fff] and
2934 // [0xffff8000, 0xffffffff].
2935 __ Sltiu(dst, lhs, rhs_imm + 1);
2936 if (cond == kCondA) {
2937 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2938 // only the sltiu instruction but no sgtiu.
2939 __ Xori(dst, dst, 1);
2940 }
2941 } else {
2942 if (use_imm) {
2943 rhs_reg = TMP;
2944 __ LoadConst32(rhs_reg, rhs_imm);
2945 }
2946 __ Sltu(dst, rhs_reg, lhs);
2947 if (cond == kCondBE) {
2948 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2949 // only the sltu instruction but no sleu.
2950 __ Xori(dst, dst, 1);
2951 }
2952 }
2953 break;
2954 }
2955}
2956
2957void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2958 LocationSummary* locations,
2959 MipsLabel* label) {
2960 Register lhs = locations->InAt(0).AsRegister<Register>();
2961 Location rhs_location = locations->InAt(1);
2962 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07002963 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002964 bool use_imm = rhs_location.IsConstant();
2965 if (use_imm) {
2966 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2967 } else {
2968 rhs_reg = rhs_location.AsRegister<Register>();
2969 }
2970
2971 if (use_imm && rhs_imm == 0) {
2972 switch (cond) {
2973 case kCondEQ:
2974 case kCondBE: // <= 0 if zero
2975 __ Beqz(lhs, label);
2976 break;
2977 case kCondNE:
2978 case kCondA: // > 0 if non-zero
2979 __ Bnez(lhs, label);
2980 break;
2981 case kCondLT:
2982 __ Bltz(lhs, label);
2983 break;
2984 case kCondGE:
2985 __ Bgez(lhs, label);
2986 break;
2987 case kCondLE:
2988 __ Blez(lhs, label);
2989 break;
2990 case kCondGT:
2991 __ Bgtz(lhs, label);
2992 break;
2993 case kCondB: // always false
2994 break;
2995 case kCondAE: // always true
2996 __ B(label);
2997 break;
2998 }
2999 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003000 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3001 if (isR6 || !use_imm) {
3002 if (use_imm) {
3003 rhs_reg = TMP;
3004 __ LoadConst32(rhs_reg, rhs_imm);
3005 }
3006 switch (cond) {
3007 case kCondEQ:
3008 __ Beq(lhs, rhs_reg, label);
3009 break;
3010 case kCondNE:
3011 __ Bne(lhs, rhs_reg, label);
3012 break;
3013 case kCondLT:
3014 __ Blt(lhs, rhs_reg, label);
3015 break;
3016 case kCondGE:
3017 __ Bge(lhs, rhs_reg, label);
3018 break;
3019 case kCondLE:
3020 __ Bge(rhs_reg, lhs, label);
3021 break;
3022 case kCondGT:
3023 __ Blt(rhs_reg, lhs, label);
3024 break;
3025 case kCondB:
3026 __ Bltu(lhs, rhs_reg, label);
3027 break;
3028 case kCondAE:
3029 __ Bgeu(lhs, rhs_reg, label);
3030 break;
3031 case kCondBE:
3032 __ Bgeu(rhs_reg, lhs, label);
3033 break;
3034 case kCondA:
3035 __ Bltu(rhs_reg, lhs, label);
3036 break;
3037 }
3038 } else {
3039 // Special cases for more efficient comparison with constants on R2.
3040 switch (cond) {
3041 case kCondEQ:
3042 __ LoadConst32(TMP, rhs_imm);
3043 __ Beq(lhs, TMP, label);
3044 break;
3045 case kCondNE:
3046 __ LoadConst32(TMP, rhs_imm);
3047 __ Bne(lhs, TMP, label);
3048 break;
3049 case kCondLT:
3050 if (IsInt<16>(rhs_imm)) {
3051 __ Slti(TMP, lhs, rhs_imm);
3052 __ Bnez(TMP, label);
3053 } else {
3054 __ LoadConst32(TMP, rhs_imm);
3055 __ Blt(lhs, TMP, label);
3056 }
3057 break;
3058 case kCondGE:
3059 if (IsInt<16>(rhs_imm)) {
3060 __ Slti(TMP, lhs, rhs_imm);
3061 __ Beqz(TMP, label);
3062 } else {
3063 __ LoadConst32(TMP, rhs_imm);
3064 __ Bge(lhs, TMP, label);
3065 }
3066 break;
3067 case kCondLE:
3068 if (IsInt<16>(rhs_imm + 1)) {
3069 // Simulate lhs <= rhs via lhs < rhs + 1.
3070 __ Slti(TMP, lhs, rhs_imm + 1);
3071 __ Bnez(TMP, label);
3072 } else {
3073 __ LoadConst32(TMP, rhs_imm);
3074 __ Bge(TMP, lhs, label);
3075 }
3076 break;
3077 case kCondGT:
3078 if (IsInt<16>(rhs_imm + 1)) {
3079 // Simulate lhs > rhs via !(lhs < rhs + 1).
3080 __ Slti(TMP, lhs, rhs_imm + 1);
3081 __ Beqz(TMP, label);
3082 } else {
3083 __ LoadConst32(TMP, rhs_imm);
3084 __ Blt(TMP, lhs, label);
3085 }
3086 break;
3087 case kCondB:
3088 if (IsInt<16>(rhs_imm)) {
3089 __ Sltiu(TMP, lhs, rhs_imm);
3090 __ Bnez(TMP, label);
3091 } else {
3092 __ LoadConst32(TMP, rhs_imm);
3093 __ Bltu(lhs, TMP, label);
3094 }
3095 break;
3096 case kCondAE:
3097 if (IsInt<16>(rhs_imm)) {
3098 __ Sltiu(TMP, lhs, rhs_imm);
3099 __ Beqz(TMP, label);
3100 } else {
3101 __ LoadConst32(TMP, rhs_imm);
3102 __ Bgeu(lhs, TMP, label);
3103 }
3104 break;
3105 case kCondBE:
3106 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3107 // Simulate lhs <= rhs via lhs < rhs + 1.
3108 // Note that this only works if rhs + 1 does not overflow
3109 // to 0, hence the check above.
3110 __ Sltiu(TMP, lhs, rhs_imm + 1);
3111 __ Bnez(TMP, label);
3112 } else {
3113 __ LoadConst32(TMP, rhs_imm);
3114 __ Bgeu(TMP, lhs, label);
3115 }
3116 break;
3117 case kCondA:
3118 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3119 // Simulate lhs > rhs via !(lhs < rhs + 1).
3120 // Note that this only works if rhs + 1 does not overflow
3121 // to 0, hence the check above.
3122 __ Sltiu(TMP, lhs, rhs_imm + 1);
3123 __ Beqz(TMP, label);
3124 } else {
3125 __ LoadConst32(TMP, rhs_imm);
3126 __ Bltu(TMP, lhs, label);
3127 }
3128 break;
3129 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003130 }
3131 }
3132}
3133
3134void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3135 LocationSummary* locations,
3136 MipsLabel* label) {
3137 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3138 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3139 Location rhs_location = locations->InAt(1);
3140 Register rhs_high = ZERO;
3141 Register rhs_low = ZERO;
3142 int64_t imm = 0;
3143 uint32_t imm_high = 0;
3144 uint32_t imm_low = 0;
3145 bool use_imm = rhs_location.IsConstant();
3146 if (use_imm) {
3147 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3148 imm_high = High32Bits(imm);
3149 imm_low = Low32Bits(imm);
3150 } else {
3151 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3152 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3153 }
3154
3155 if (use_imm && imm == 0) {
3156 switch (cond) {
3157 case kCondEQ:
3158 case kCondBE: // <= 0 if zero
3159 __ Or(TMP, lhs_high, lhs_low);
3160 __ Beqz(TMP, label);
3161 break;
3162 case kCondNE:
3163 case kCondA: // > 0 if non-zero
3164 __ Or(TMP, lhs_high, lhs_low);
3165 __ Bnez(TMP, label);
3166 break;
3167 case kCondLT:
3168 __ Bltz(lhs_high, label);
3169 break;
3170 case kCondGE:
3171 __ Bgez(lhs_high, label);
3172 break;
3173 case kCondLE:
3174 __ Or(TMP, lhs_high, lhs_low);
3175 __ Sra(AT, lhs_high, 31);
3176 __ Bgeu(AT, TMP, label);
3177 break;
3178 case kCondGT:
3179 __ Or(TMP, lhs_high, lhs_low);
3180 __ Sra(AT, lhs_high, 31);
3181 __ Bltu(AT, TMP, label);
3182 break;
3183 case kCondB: // always false
3184 break;
3185 case kCondAE: // always true
3186 __ B(label);
3187 break;
3188 }
3189 } else if (use_imm) {
3190 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3191 switch (cond) {
3192 case kCondEQ:
3193 __ LoadConst32(TMP, imm_high);
3194 __ Xor(TMP, TMP, lhs_high);
3195 __ LoadConst32(AT, imm_low);
3196 __ Xor(AT, AT, lhs_low);
3197 __ Or(TMP, TMP, AT);
3198 __ Beqz(TMP, label);
3199 break;
3200 case kCondNE:
3201 __ LoadConst32(TMP, imm_high);
3202 __ Xor(TMP, TMP, lhs_high);
3203 __ LoadConst32(AT, imm_low);
3204 __ Xor(AT, AT, lhs_low);
3205 __ Or(TMP, TMP, AT);
3206 __ Bnez(TMP, label);
3207 break;
3208 case kCondLT:
3209 __ LoadConst32(TMP, imm_high);
3210 __ Blt(lhs_high, TMP, label);
3211 __ Slt(TMP, TMP, lhs_high);
3212 __ LoadConst32(AT, imm_low);
3213 __ Sltu(AT, lhs_low, AT);
3214 __ Blt(TMP, AT, label);
3215 break;
3216 case kCondGE:
3217 __ LoadConst32(TMP, imm_high);
3218 __ Blt(TMP, lhs_high, label);
3219 __ Slt(TMP, lhs_high, TMP);
3220 __ LoadConst32(AT, imm_low);
3221 __ Sltu(AT, lhs_low, AT);
3222 __ Or(TMP, TMP, AT);
3223 __ Beqz(TMP, label);
3224 break;
3225 case kCondLE:
3226 __ LoadConst32(TMP, imm_high);
3227 __ Blt(lhs_high, TMP, label);
3228 __ Slt(TMP, TMP, lhs_high);
3229 __ LoadConst32(AT, imm_low);
3230 __ Sltu(AT, AT, lhs_low);
3231 __ Or(TMP, TMP, AT);
3232 __ Beqz(TMP, label);
3233 break;
3234 case kCondGT:
3235 __ LoadConst32(TMP, imm_high);
3236 __ Blt(TMP, lhs_high, label);
3237 __ Slt(TMP, lhs_high, TMP);
3238 __ LoadConst32(AT, imm_low);
3239 __ Sltu(AT, AT, lhs_low);
3240 __ Blt(TMP, AT, label);
3241 break;
3242 case kCondB:
3243 __ LoadConst32(TMP, imm_high);
3244 __ Bltu(lhs_high, TMP, label);
3245 __ Sltu(TMP, TMP, lhs_high);
3246 __ LoadConst32(AT, imm_low);
3247 __ Sltu(AT, lhs_low, AT);
3248 __ Blt(TMP, AT, label);
3249 break;
3250 case kCondAE:
3251 __ LoadConst32(TMP, imm_high);
3252 __ Bltu(TMP, lhs_high, label);
3253 __ Sltu(TMP, lhs_high, TMP);
3254 __ LoadConst32(AT, imm_low);
3255 __ Sltu(AT, lhs_low, AT);
3256 __ Or(TMP, TMP, AT);
3257 __ Beqz(TMP, label);
3258 break;
3259 case kCondBE:
3260 __ LoadConst32(TMP, imm_high);
3261 __ Bltu(lhs_high, TMP, label);
3262 __ Sltu(TMP, TMP, lhs_high);
3263 __ LoadConst32(AT, imm_low);
3264 __ Sltu(AT, AT, lhs_low);
3265 __ Or(TMP, TMP, AT);
3266 __ Beqz(TMP, label);
3267 break;
3268 case kCondA:
3269 __ LoadConst32(TMP, imm_high);
3270 __ Bltu(TMP, lhs_high, label);
3271 __ Sltu(TMP, lhs_high, TMP);
3272 __ LoadConst32(AT, imm_low);
3273 __ Sltu(AT, AT, lhs_low);
3274 __ Blt(TMP, AT, label);
3275 break;
3276 }
3277 } else {
3278 switch (cond) {
3279 case kCondEQ:
3280 __ Xor(TMP, lhs_high, rhs_high);
3281 __ Xor(AT, lhs_low, rhs_low);
3282 __ Or(TMP, TMP, AT);
3283 __ Beqz(TMP, label);
3284 break;
3285 case kCondNE:
3286 __ Xor(TMP, lhs_high, rhs_high);
3287 __ Xor(AT, lhs_low, rhs_low);
3288 __ Or(TMP, TMP, AT);
3289 __ Bnez(TMP, label);
3290 break;
3291 case kCondLT:
3292 __ Blt(lhs_high, rhs_high, label);
3293 __ Slt(TMP, rhs_high, lhs_high);
3294 __ Sltu(AT, lhs_low, rhs_low);
3295 __ Blt(TMP, AT, label);
3296 break;
3297 case kCondGE:
3298 __ Blt(rhs_high, lhs_high, label);
3299 __ Slt(TMP, lhs_high, rhs_high);
3300 __ Sltu(AT, lhs_low, rhs_low);
3301 __ Or(TMP, TMP, AT);
3302 __ Beqz(TMP, label);
3303 break;
3304 case kCondLE:
3305 __ Blt(lhs_high, rhs_high, label);
3306 __ Slt(TMP, rhs_high, lhs_high);
3307 __ Sltu(AT, rhs_low, lhs_low);
3308 __ Or(TMP, TMP, AT);
3309 __ Beqz(TMP, label);
3310 break;
3311 case kCondGT:
3312 __ Blt(rhs_high, lhs_high, label);
3313 __ Slt(TMP, lhs_high, rhs_high);
3314 __ Sltu(AT, rhs_low, lhs_low);
3315 __ Blt(TMP, AT, label);
3316 break;
3317 case kCondB:
3318 __ Bltu(lhs_high, rhs_high, label);
3319 __ Sltu(TMP, rhs_high, lhs_high);
3320 __ Sltu(AT, lhs_low, rhs_low);
3321 __ Blt(TMP, AT, label);
3322 break;
3323 case kCondAE:
3324 __ Bltu(rhs_high, lhs_high, label);
3325 __ Sltu(TMP, lhs_high, rhs_high);
3326 __ Sltu(AT, lhs_low, rhs_low);
3327 __ Or(TMP, TMP, AT);
3328 __ Beqz(TMP, label);
3329 break;
3330 case kCondBE:
3331 __ Bltu(lhs_high, rhs_high, label);
3332 __ Sltu(TMP, rhs_high, lhs_high);
3333 __ Sltu(AT, rhs_low, lhs_low);
3334 __ Or(TMP, TMP, AT);
3335 __ Beqz(TMP, label);
3336 break;
3337 case kCondA:
3338 __ Bltu(rhs_high, lhs_high, label);
3339 __ Sltu(TMP, lhs_high, rhs_high);
3340 __ Sltu(AT, rhs_low, lhs_low);
3341 __ Blt(TMP, AT, label);
3342 break;
3343 }
3344 }
3345}
3346
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003347void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3348 bool gt_bias,
3349 Primitive::Type type,
3350 LocationSummary* locations) {
3351 Register dst = locations->Out().AsRegister<Register>();
3352 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3353 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3354 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3355 if (type == Primitive::kPrimFloat) {
3356 if (isR6) {
3357 switch (cond) {
3358 case kCondEQ:
3359 __ CmpEqS(FTMP, lhs, rhs);
3360 __ Mfc1(dst, FTMP);
3361 __ Andi(dst, dst, 1);
3362 break;
3363 case kCondNE:
3364 __ CmpEqS(FTMP, lhs, rhs);
3365 __ Mfc1(dst, FTMP);
3366 __ Addiu(dst, dst, 1);
3367 break;
3368 case kCondLT:
3369 if (gt_bias) {
3370 __ CmpLtS(FTMP, lhs, rhs);
3371 } else {
3372 __ CmpUltS(FTMP, lhs, rhs);
3373 }
3374 __ Mfc1(dst, FTMP);
3375 __ Andi(dst, dst, 1);
3376 break;
3377 case kCondLE:
3378 if (gt_bias) {
3379 __ CmpLeS(FTMP, lhs, rhs);
3380 } else {
3381 __ CmpUleS(FTMP, lhs, rhs);
3382 }
3383 __ Mfc1(dst, FTMP);
3384 __ Andi(dst, dst, 1);
3385 break;
3386 case kCondGT:
3387 if (gt_bias) {
3388 __ CmpUltS(FTMP, rhs, lhs);
3389 } else {
3390 __ CmpLtS(FTMP, rhs, lhs);
3391 }
3392 __ Mfc1(dst, FTMP);
3393 __ Andi(dst, dst, 1);
3394 break;
3395 case kCondGE:
3396 if (gt_bias) {
3397 __ CmpUleS(FTMP, rhs, lhs);
3398 } else {
3399 __ CmpLeS(FTMP, rhs, lhs);
3400 }
3401 __ Mfc1(dst, FTMP);
3402 __ Andi(dst, dst, 1);
3403 break;
3404 default:
3405 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3406 UNREACHABLE();
3407 }
3408 } else {
3409 switch (cond) {
3410 case kCondEQ:
3411 __ CeqS(0, lhs, rhs);
3412 __ LoadConst32(dst, 1);
3413 __ Movf(dst, ZERO, 0);
3414 break;
3415 case kCondNE:
3416 __ CeqS(0, lhs, rhs);
3417 __ LoadConst32(dst, 1);
3418 __ Movt(dst, ZERO, 0);
3419 break;
3420 case kCondLT:
3421 if (gt_bias) {
3422 __ ColtS(0, lhs, rhs);
3423 } else {
3424 __ CultS(0, lhs, rhs);
3425 }
3426 __ LoadConst32(dst, 1);
3427 __ Movf(dst, ZERO, 0);
3428 break;
3429 case kCondLE:
3430 if (gt_bias) {
3431 __ ColeS(0, lhs, rhs);
3432 } else {
3433 __ CuleS(0, lhs, rhs);
3434 }
3435 __ LoadConst32(dst, 1);
3436 __ Movf(dst, ZERO, 0);
3437 break;
3438 case kCondGT:
3439 if (gt_bias) {
3440 __ CultS(0, rhs, lhs);
3441 } else {
3442 __ ColtS(0, rhs, lhs);
3443 }
3444 __ LoadConst32(dst, 1);
3445 __ Movf(dst, ZERO, 0);
3446 break;
3447 case kCondGE:
3448 if (gt_bias) {
3449 __ CuleS(0, rhs, lhs);
3450 } else {
3451 __ ColeS(0, rhs, lhs);
3452 }
3453 __ LoadConst32(dst, 1);
3454 __ Movf(dst, ZERO, 0);
3455 break;
3456 default:
3457 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3458 UNREACHABLE();
3459 }
3460 }
3461 } else {
3462 DCHECK_EQ(type, Primitive::kPrimDouble);
3463 if (isR6) {
3464 switch (cond) {
3465 case kCondEQ:
3466 __ CmpEqD(FTMP, lhs, rhs);
3467 __ Mfc1(dst, FTMP);
3468 __ Andi(dst, dst, 1);
3469 break;
3470 case kCondNE:
3471 __ CmpEqD(FTMP, lhs, rhs);
3472 __ Mfc1(dst, FTMP);
3473 __ Addiu(dst, dst, 1);
3474 break;
3475 case kCondLT:
3476 if (gt_bias) {
3477 __ CmpLtD(FTMP, lhs, rhs);
3478 } else {
3479 __ CmpUltD(FTMP, lhs, rhs);
3480 }
3481 __ Mfc1(dst, FTMP);
3482 __ Andi(dst, dst, 1);
3483 break;
3484 case kCondLE:
3485 if (gt_bias) {
3486 __ CmpLeD(FTMP, lhs, rhs);
3487 } else {
3488 __ CmpUleD(FTMP, lhs, rhs);
3489 }
3490 __ Mfc1(dst, FTMP);
3491 __ Andi(dst, dst, 1);
3492 break;
3493 case kCondGT:
3494 if (gt_bias) {
3495 __ CmpUltD(FTMP, rhs, lhs);
3496 } else {
3497 __ CmpLtD(FTMP, rhs, lhs);
3498 }
3499 __ Mfc1(dst, FTMP);
3500 __ Andi(dst, dst, 1);
3501 break;
3502 case kCondGE:
3503 if (gt_bias) {
3504 __ CmpUleD(FTMP, rhs, lhs);
3505 } else {
3506 __ CmpLeD(FTMP, rhs, lhs);
3507 }
3508 __ Mfc1(dst, FTMP);
3509 __ Andi(dst, dst, 1);
3510 break;
3511 default:
3512 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3513 UNREACHABLE();
3514 }
3515 } else {
3516 switch (cond) {
3517 case kCondEQ:
3518 __ CeqD(0, lhs, rhs);
3519 __ LoadConst32(dst, 1);
3520 __ Movf(dst, ZERO, 0);
3521 break;
3522 case kCondNE:
3523 __ CeqD(0, lhs, rhs);
3524 __ LoadConst32(dst, 1);
3525 __ Movt(dst, ZERO, 0);
3526 break;
3527 case kCondLT:
3528 if (gt_bias) {
3529 __ ColtD(0, lhs, rhs);
3530 } else {
3531 __ CultD(0, lhs, rhs);
3532 }
3533 __ LoadConst32(dst, 1);
3534 __ Movf(dst, ZERO, 0);
3535 break;
3536 case kCondLE:
3537 if (gt_bias) {
3538 __ ColeD(0, lhs, rhs);
3539 } else {
3540 __ CuleD(0, lhs, rhs);
3541 }
3542 __ LoadConst32(dst, 1);
3543 __ Movf(dst, ZERO, 0);
3544 break;
3545 case kCondGT:
3546 if (gt_bias) {
3547 __ CultD(0, rhs, lhs);
3548 } else {
3549 __ ColtD(0, rhs, lhs);
3550 }
3551 __ LoadConst32(dst, 1);
3552 __ Movf(dst, ZERO, 0);
3553 break;
3554 case kCondGE:
3555 if (gt_bias) {
3556 __ CuleD(0, rhs, lhs);
3557 } else {
3558 __ ColeD(0, rhs, lhs);
3559 }
3560 __ LoadConst32(dst, 1);
3561 __ Movf(dst, ZERO, 0);
3562 break;
3563 default:
3564 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3565 UNREACHABLE();
3566 }
3567 }
3568 }
3569}
3570
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003571void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3572 bool gt_bias,
3573 Primitive::Type type,
3574 LocationSummary* locations,
3575 MipsLabel* label) {
3576 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3577 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3578 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3579 if (type == Primitive::kPrimFloat) {
3580 if (isR6) {
3581 switch (cond) {
3582 case kCondEQ:
3583 __ CmpEqS(FTMP, lhs, rhs);
3584 __ Bc1nez(FTMP, label);
3585 break;
3586 case kCondNE:
3587 __ CmpEqS(FTMP, lhs, rhs);
3588 __ Bc1eqz(FTMP, label);
3589 break;
3590 case kCondLT:
3591 if (gt_bias) {
3592 __ CmpLtS(FTMP, lhs, rhs);
3593 } else {
3594 __ CmpUltS(FTMP, lhs, rhs);
3595 }
3596 __ Bc1nez(FTMP, label);
3597 break;
3598 case kCondLE:
3599 if (gt_bias) {
3600 __ CmpLeS(FTMP, lhs, rhs);
3601 } else {
3602 __ CmpUleS(FTMP, lhs, rhs);
3603 }
3604 __ Bc1nez(FTMP, label);
3605 break;
3606 case kCondGT:
3607 if (gt_bias) {
3608 __ CmpUltS(FTMP, rhs, lhs);
3609 } else {
3610 __ CmpLtS(FTMP, rhs, lhs);
3611 }
3612 __ Bc1nez(FTMP, label);
3613 break;
3614 case kCondGE:
3615 if (gt_bias) {
3616 __ CmpUleS(FTMP, rhs, lhs);
3617 } else {
3618 __ CmpLeS(FTMP, rhs, lhs);
3619 }
3620 __ Bc1nez(FTMP, label);
3621 break;
3622 default:
3623 LOG(FATAL) << "Unexpected non-floating-point condition";
3624 }
3625 } else {
3626 switch (cond) {
3627 case kCondEQ:
3628 __ CeqS(0, lhs, rhs);
3629 __ Bc1t(0, label);
3630 break;
3631 case kCondNE:
3632 __ CeqS(0, lhs, rhs);
3633 __ Bc1f(0, label);
3634 break;
3635 case kCondLT:
3636 if (gt_bias) {
3637 __ ColtS(0, lhs, rhs);
3638 } else {
3639 __ CultS(0, lhs, rhs);
3640 }
3641 __ Bc1t(0, label);
3642 break;
3643 case kCondLE:
3644 if (gt_bias) {
3645 __ ColeS(0, lhs, rhs);
3646 } else {
3647 __ CuleS(0, lhs, rhs);
3648 }
3649 __ Bc1t(0, label);
3650 break;
3651 case kCondGT:
3652 if (gt_bias) {
3653 __ CultS(0, rhs, lhs);
3654 } else {
3655 __ ColtS(0, rhs, lhs);
3656 }
3657 __ Bc1t(0, label);
3658 break;
3659 case kCondGE:
3660 if (gt_bias) {
3661 __ CuleS(0, rhs, lhs);
3662 } else {
3663 __ ColeS(0, rhs, lhs);
3664 }
3665 __ Bc1t(0, label);
3666 break;
3667 default:
3668 LOG(FATAL) << "Unexpected non-floating-point condition";
3669 }
3670 }
3671 } else {
3672 DCHECK_EQ(type, Primitive::kPrimDouble);
3673 if (isR6) {
3674 switch (cond) {
3675 case kCondEQ:
3676 __ CmpEqD(FTMP, lhs, rhs);
3677 __ Bc1nez(FTMP, label);
3678 break;
3679 case kCondNE:
3680 __ CmpEqD(FTMP, lhs, rhs);
3681 __ Bc1eqz(FTMP, label);
3682 break;
3683 case kCondLT:
3684 if (gt_bias) {
3685 __ CmpLtD(FTMP, lhs, rhs);
3686 } else {
3687 __ CmpUltD(FTMP, lhs, rhs);
3688 }
3689 __ Bc1nez(FTMP, label);
3690 break;
3691 case kCondLE:
3692 if (gt_bias) {
3693 __ CmpLeD(FTMP, lhs, rhs);
3694 } else {
3695 __ CmpUleD(FTMP, lhs, rhs);
3696 }
3697 __ Bc1nez(FTMP, label);
3698 break;
3699 case kCondGT:
3700 if (gt_bias) {
3701 __ CmpUltD(FTMP, rhs, lhs);
3702 } else {
3703 __ CmpLtD(FTMP, rhs, lhs);
3704 }
3705 __ Bc1nez(FTMP, label);
3706 break;
3707 case kCondGE:
3708 if (gt_bias) {
3709 __ CmpUleD(FTMP, rhs, lhs);
3710 } else {
3711 __ CmpLeD(FTMP, rhs, lhs);
3712 }
3713 __ Bc1nez(FTMP, label);
3714 break;
3715 default:
3716 LOG(FATAL) << "Unexpected non-floating-point condition";
3717 }
3718 } else {
3719 switch (cond) {
3720 case kCondEQ:
3721 __ CeqD(0, lhs, rhs);
3722 __ Bc1t(0, label);
3723 break;
3724 case kCondNE:
3725 __ CeqD(0, lhs, rhs);
3726 __ Bc1f(0, label);
3727 break;
3728 case kCondLT:
3729 if (gt_bias) {
3730 __ ColtD(0, lhs, rhs);
3731 } else {
3732 __ CultD(0, lhs, rhs);
3733 }
3734 __ Bc1t(0, label);
3735 break;
3736 case kCondLE:
3737 if (gt_bias) {
3738 __ ColeD(0, lhs, rhs);
3739 } else {
3740 __ CuleD(0, lhs, rhs);
3741 }
3742 __ Bc1t(0, label);
3743 break;
3744 case kCondGT:
3745 if (gt_bias) {
3746 __ CultD(0, rhs, lhs);
3747 } else {
3748 __ ColtD(0, rhs, lhs);
3749 }
3750 __ Bc1t(0, label);
3751 break;
3752 case kCondGE:
3753 if (gt_bias) {
3754 __ CuleD(0, rhs, lhs);
3755 } else {
3756 __ ColeD(0, rhs, lhs);
3757 }
3758 __ Bc1t(0, label);
3759 break;
3760 default:
3761 LOG(FATAL) << "Unexpected non-floating-point condition";
3762 }
3763 }
3764 }
3765}
3766
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003767void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003768 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003769 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003770 MipsLabel* false_target) {
3771 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003772
David Brazdil0debae72015-11-12 18:37:00 +00003773 if (true_target == nullptr && false_target == nullptr) {
3774 // Nothing to do. The code always falls through.
3775 return;
3776 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003777 // Constant condition, statically compared against "true" (integer value 1).
3778 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003779 if (true_target != nullptr) {
3780 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003781 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003782 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003783 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003784 if (false_target != nullptr) {
3785 __ B(false_target);
3786 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003787 }
David Brazdil0debae72015-11-12 18:37:00 +00003788 return;
3789 }
3790
3791 // The following code generates these patterns:
3792 // (1) true_target == nullptr && false_target != nullptr
3793 // - opposite condition true => branch to false_target
3794 // (2) true_target != nullptr && false_target == nullptr
3795 // - condition true => branch to true_target
3796 // (3) true_target != nullptr && false_target != nullptr
3797 // - condition true => branch to true_target
3798 // - branch to false_target
3799 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003800 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003801 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003802 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003803 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003804 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3805 } else {
3806 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3807 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003808 } else {
3809 // The condition instruction has not been materialized, use its inputs as
3810 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003811 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003812 Primitive::Type type = condition->InputAt(0)->GetType();
3813 LocationSummary* locations = cond->GetLocations();
3814 IfCondition if_cond = condition->GetCondition();
3815 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003816
David Brazdil0debae72015-11-12 18:37:00 +00003817 if (true_target == nullptr) {
3818 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003819 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003820 }
3821
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003822 switch (type) {
3823 default:
3824 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3825 break;
3826 case Primitive::kPrimLong:
3827 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3828 break;
3829 case Primitive::kPrimFloat:
3830 case Primitive::kPrimDouble:
3831 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3832 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003833 }
3834 }
David Brazdil0debae72015-11-12 18:37:00 +00003835
3836 // If neither branch falls through (case 3), the conditional branch to `true_target`
3837 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3838 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003839 __ B(false_target);
3840 }
3841}
3842
3843void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3844 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003845 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003846 locations->SetInAt(0, Location::RequiresRegister());
3847 }
3848}
3849
3850void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003851 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3852 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3853 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3854 nullptr : codegen_->GetLabelOf(true_successor);
3855 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3856 nullptr : codegen_->GetLabelOf(false_successor);
3857 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003858}
3859
3860void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3861 LocationSummary* locations = new (GetGraph()->GetArena())
3862 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003863 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003864 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003865 locations->SetInAt(0, Location::RequiresRegister());
3866 }
3867}
3868
3869void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003870 SlowPathCodeMIPS* slow_path =
3871 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003872 GenerateTestAndBranch(deoptimize,
3873 /* condition_input_index */ 0,
3874 slow_path->GetEntryLabel(),
3875 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003876}
3877
David Brazdil74eb1b22015-12-14 11:44:01 +00003878void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3879 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3880 if (Primitive::IsFloatingPointType(select->GetType())) {
3881 locations->SetInAt(0, Location::RequiresFpuRegister());
3882 locations->SetInAt(1, Location::RequiresFpuRegister());
3883 } else {
3884 locations->SetInAt(0, Location::RequiresRegister());
3885 locations->SetInAt(1, Location::RequiresRegister());
3886 }
3887 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3888 locations->SetInAt(2, Location::RequiresRegister());
3889 }
3890 locations->SetOut(Location::SameAsFirstInput());
3891}
3892
3893void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3894 LocationSummary* locations = select->GetLocations();
3895 MipsLabel false_target;
3896 GenerateTestAndBranch(select,
3897 /* condition_input_index */ 2,
3898 /* true_target */ nullptr,
3899 &false_target);
3900 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3901 __ Bind(&false_target);
3902}
3903
David Srbecky0cf44932015-12-09 14:09:59 +00003904void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3905 new (GetGraph()->GetArena()) LocationSummary(info);
3906}
3907
David Srbeckyd28f4a02016-03-14 17:14:24 +00003908void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3909 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003910}
3911
3912void CodeGeneratorMIPS::GenerateNop() {
3913 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003914}
3915
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003916void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3917 Primitive::Type field_type = field_info.GetFieldType();
3918 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3919 bool generate_volatile = field_info.IsVolatile() && is_wide;
3920 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003921 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003922
3923 locations->SetInAt(0, Location::RequiresRegister());
3924 if (generate_volatile) {
3925 InvokeRuntimeCallingConvention calling_convention;
3926 // need A0 to hold base + offset
3927 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3928 if (field_type == Primitive::kPrimLong) {
3929 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3930 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003931 // Use Location::Any() to prevent situations when running out of available fp registers.
3932 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003933 // Need some temp core regs since FP results are returned in core registers
3934 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3935 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3936 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3937 }
3938 } else {
3939 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3940 locations->SetOut(Location::RequiresFpuRegister());
3941 } else {
3942 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3943 }
3944 }
3945}
3946
3947void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3948 const FieldInfo& field_info,
3949 uint32_t dex_pc) {
3950 Primitive::Type type = field_info.GetFieldType();
3951 LocationSummary* locations = instruction->GetLocations();
3952 Register obj = locations->InAt(0).AsRegister<Register>();
3953 LoadOperandType load_type = kLoadUnsignedByte;
3954 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003955 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003956 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003957
3958 switch (type) {
3959 case Primitive::kPrimBoolean:
3960 load_type = kLoadUnsignedByte;
3961 break;
3962 case Primitive::kPrimByte:
3963 load_type = kLoadSignedByte;
3964 break;
3965 case Primitive::kPrimShort:
3966 load_type = kLoadSignedHalfword;
3967 break;
3968 case Primitive::kPrimChar:
3969 load_type = kLoadUnsignedHalfword;
3970 break;
3971 case Primitive::kPrimInt:
3972 case Primitive::kPrimFloat:
3973 case Primitive::kPrimNot:
3974 load_type = kLoadWord;
3975 break;
3976 case Primitive::kPrimLong:
3977 case Primitive::kPrimDouble:
3978 load_type = kLoadDoubleword;
3979 break;
3980 case Primitive::kPrimVoid:
3981 LOG(FATAL) << "Unreachable type " << type;
3982 UNREACHABLE();
3983 }
3984
3985 if (is_volatile && load_type == kLoadDoubleword) {
3986 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003987 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003988 // Do implicit Null check
3989 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3990 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01003991 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003992 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3993 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003994 // FP results are returned in core registers. Need to move them.
3995 Location out = locations->Out();
3996 if (out.IsFpuRegister()) {
3997 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3998 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3999 out.AsFpuRegister<FRegister>());
4000 } else {
4001 DCHECK(out.IsDoubleStackSlot());
4002 __ StoreToOffset(kStoreWord,
4003 locations->GetTemp(1).AsRegister<Register>(),
4004 SP,
4005 out.GetStackIndex());
4006 __ StoreToOffset(kStoreWord,
4007 locations->GetTemp(2).AsRegister<Register>(),
4008 SP,
4009 out.GetStackIndex() + 4);
4010 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004011 }
4012 } else {
4013 if (!Primitive::IsFloatingPointType(type)) {
4014 Register dst;
4015 if (type == Primitive::kPrimLong) {
4016 DCHECK(locations->Out().IsRegisterPair());
4017 dst = locations->Out().AsRegisterPairLow<Register>();
4018 } else {
4019 DCHECK(locations->Out().IsRegister());
4020 dst = locations->Out().AsRegister<Register>();
4021 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004022 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004023 } else {
4024 DCHECK(locations->Out().IsFpuRegister());
4025 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4026 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004027 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004028 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004029 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004030 }
4031 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004032 }
4033
4034 if (is_volatile) {
4035 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4036 }
4037}
4038
4039void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4040 Primitive::Type field_type = field_info.GetFieldType();
4041 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4042 bool generate_volatile = field_info.IsVolatile() && is_wide;
4043 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004044 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004045
4046 locations->SetInAt(0, Location::RequiresRegister());
4047 if (generate_volatile) {
4048 InvokeRuntimeCallingConvention calling_convention;
4049 // need A0 to hold base + offset
4050 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4051 if (field_type == Primitive::kPrimLong) {
4052 locations->SetInAt(1, Location::RegisterPairLocation(
4053 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4054 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004055 // Use Location::Any() to prevent situations when running out of available fp registers.
4056 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004057 // Pass FP parameters in core registers.
4058 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4059 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4060 }
4061 } else {
4062 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004063 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004064 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004065 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004066 }
4067 }
4068}
4069
4070void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4071 const FieldInfo& field_info,
4072 uint32_t dex_pc) {
4073 Primitive::Type type = field_info.GetFieldType();
4074 LocationSummary* locations = instruction->GetLocations();
4075 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004076 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004077 StoreOperandType store_type = kStoreByte;
4078 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004079 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004080 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004081
4082 switch (type) {
4083 case Primitive::kPrimBoolean:
4084 case Primitive::kPrimByte:
4085 store_type = kStoreByte;
4086 break;
4087 case Primitive::kPrimShort:
4088 case Primitive::kPrimChar:
4089 store_type = kStoreHalfword;
4090 break;
4091 case Primitive::kPrimInt:
4092 case Primitive::kPrimFloat:
4093 case Primitive::kPrimNot:
4094 store_type = kStoreWord;
4095 break;
4096 case Primitive::kPrimLong:
4097 case Primitive::kPrimDouble:
4098 store_type = kStoreDoubleword;
4099 break;
4100 case Primitive::kPrimVoid:
4101 LOG(FATAL) << "Unreachable type " << type;
4102 UNREACHABLE();
4103 }
4104
4105 if (is_volatile) {
4106 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4107 }
4108
4109 if (is_volatile && store_type == kStoreDoubleword) {
4110 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004111 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004112 // Do implicit Null check.
4113 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4114 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4115 if (type == Primitive::kPrimDouble) {
4116 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004117 if (value_location.IsFpuRegister()) {
4118 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4119 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004120 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004121 value_location.AsFpuRegister<FRegister>());
4122 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004123 __ LoadFromOffset(kLoadWord,
4124 locations->GetTemp(1).AsRegister<Register>(),
4125 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004126 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004127 __ LoadFromOffset(kLoadWord,
4128 locations->GetTemp(2).AsRegister<Register>(),
4129 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004130 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004131 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004132 DCHECK(value_location.IsConstant());
4133 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4134 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004135 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4136 locations->GetTemp(1).AsRegister<Register>(),
4137 value);
4138 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004139 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004140 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004141 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4142 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004143 if (value_location.IsConstant()) {
4144 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4145 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4146 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004147 Register src;
4148 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004149 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004150 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004151 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004152 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004153 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004154 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004155 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004156 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004157 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004158 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004159 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004160 }
4161 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004162 }
4163
4164 // TODO: memory barriers?
4165 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004166 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004167 codegen_->MarkGCCard(obj, src);
4168 }
4169
4170 if (is_volatile) {
4171 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4172 }
4173}
4174
4175void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4176 HandleFieldGet(instruction, instruction->GetFieldInfo());
4177}
4178
4179void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4180 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4181}
4182
4183void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4184 HandleFieldSet(instruction, instruction->GetFieldInfo());
4185}
4186
4187void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4188 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4189}
4190
Alexey Frunze06a46c42016-07-19 15:00:40 -07004191void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
4192 HInstruction* instruction ATTRIBUTE_UNUSED,
4193 Location root,
4194 Register obj,
4195 uint32_t offset) {
4196 Register root_reg = root.AsRegister<Register>();
4197 if (kEmitCompilerReadBarrier) {
4198 UNIMPLEMENTED(FATAL) << "for read barrier";
4199 } else {
4200 // Plain GC root load with no read barrier.
4201 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4202 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
4203 // Note that GC roots are not affected by heap poisoning, thus we
4204 // do not have to unpoison `root_reg` here.
4205 }
4206}
4207
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004208void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4209 LocationSummary::CallKind call_kind =
4210 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
4211 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4212 locations->SetInAt(0, Location::RequiresRegister());
4213 locations->SetInAt(1, Location::RequiresRegister());
4214 // The output does overlap inputs.
4215 // Note that TypeCheckSlowPathMIPS uses this register too.
4216 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4217}
4218
4219void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4220 LocationSummary* locations = instruction->GetLocations();
4221 Register obj = locations->InAt(0).AsRegister<Register>();
4222 Register cls = locations->InAt(1).AsRegister<Register>();
4223 Register out = locations->Out().AsRegister<Register>();
4224
4225 MipsLabel done;
4226
4227 // Return 0 if `obj` is null.
4228 // TODO: Avoid this check if we know `obj` is not null.
4229 __ Move(out, ZERO);
4230 __ Beqz(obj, &done);
4231
4232 // Compare the class of `obj` with `cls`.
4233 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
4234 if (instruction->IsExactCheck()) {
4235 // Classes must be equal for the instanceof to succeed.
4236 __ Xor(out, out, cls);
4237 __ Sltiu(out, out, 1);
4238 } else {
4239 // If the classes are not equal, we go into a slow path.
4240 DCHECK(locations->OnlyCallsOnSlowPath());
4241 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
4242 codegen_->AddSlowPath(slow_path);
4243 __ Bne(out, cls, slow_path->GetEntryLabel());
4244 __ LoadConst32(out, 1);
4245 __ Bind(slow_path->GetExitLabel());
4246 }
4247
4248 __ Bind(&done);
4249}
4250
4251void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
4252 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4253 locations->SetOut(Location::ConstantLocation(constant));
4254}
4255
4256void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4257 // Will be generated at use site.
4258}
4259
4260void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
4261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4262 locations->SetOut(Location::ConstantLocation(constant));
4263}
4264
4265void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4266 // Will be generated at use site.
4267}
4268
4269void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
4270 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
4271 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4272}
4273
4274void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4275 HandleInvoke(invoke);
4276 // The register T0 is required to be used for the hidden argument in
4277 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4278 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4279}
4280
4281void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4282 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4283 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004284 Location receiver = invoke->GetLocations()->InAt(0);
4285 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004286 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004287
4288 // Set the hidden argument.
4289 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
4290 invoke->GetDexMethodIndex());
4291
4292 // temp = object->GetClass();
4293 if (receiver.IsStackSlot()) {
4294 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4295 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4296 } else {
4297 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4298 }
4299 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004300 __ LoadFromOffset(kLoadWord, temp, temp,
4301 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
4302 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004303 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004304 // temp = temp->GetImtEntryAt(method_offset);
4305 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4306 // T9 = temp->GetEntryPoint();
4307 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4308 // T9();
4309 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004310 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004311 DCHECK(!codegen_->IsLeafMethod());
4312 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4313}
4314
4315void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07004316 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4317 if (intrinsic.TryDispatch(invoke)) {
4318 return;
4319 }
4320
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004321 HandleInvoke(invoke);
4322}
4323
4324void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004325 // Explicit clinit checks triggered by static invokes must have been pruned by
4326 // art::PrepareForRegisterAllocation.
4327 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004328
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004329 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4330 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4331 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4332
4333 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
4334 // R6 has PC-relative addressing.
4335 bool has_extra_input = !isR6 &&
4336 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4337 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4338
4339 if (invoke->HasPcRelativeDexCache()) {
4340 // kDexCachePcRelative is mutually exclusive with
4341 // kDirectAddressWithFixup/kCallDirectWithFixup.
4342 CHECK(!has_extra_input);
4343 has_extra_input = true;
4344 }
4345
Chris Larsen701566a2015-10-27 15:29:13 -07004346 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4347 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004348 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4349 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4350 }
Chris Larsen701566a2015-10-27 15:29:13 -07004351 return;
4352 }
4353
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004354 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004355
4356 // Add the extra input register if either the dex cache array base register
4357 // or the PC-relative base register for accessing literals is needed.
4358 if (has_extra_input) {
4359 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4360 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004361}
4362
Chris Larsen701566a2015-10-27 15:29:13 -07004363static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004364 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004365 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4366 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004367 return true;
4368 }
4369 return false;
4370}
4371
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004372HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004373 HLoadString::LoadKind desired_string_load_kind) {
4374 if (kEmitCompilerReadBarrier) {
4375 UNIMPLEMENTED(FATAL) << "for read barrier";
4376 }
4377 // We disable PC-relative load when there is an irreducible loop, as the optimization
4378 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004379 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4380 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07004381 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4382 bool fallback_load = has_irreducible_loops;
4383 switch (desired_string_load_kind) {
4384 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4385 DCHECK(!GetCompilerOptions().GetCompilePic());
4386 break;
4387 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4388 DCHECK(GetCompilerOptions().GetCompilePic());
4389 break;
4390 case HLoadString::LoadKind::kBootImageAddress:
4391 break;
4392 case HLoadString::LoadKind::kDexCacheAddress:
4393 DCHECK(Runtime::Current()->UseJitCompilation());
4394 fallback_load = false;
4395 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004396 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004397 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004398 break;
4399 case HLoadString::LoadKind::kDexCacheViaMethod:
4400 fallback_load = false;
4401 break;
4402 }
4403 if (fallback_load) {
4404 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4405 }
4406 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004407}
4408
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004409HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4410 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004411 if (kEmitCompilerReadBarrier) {
4412 UNIMPLEMENTED(FATAL) << "for read barrier";
4413 }
4414 // We disable pc-relative load when there is an irreducible loop, as the optimization
4415 // is incompatible with it.
4416 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4417 bool fallback_load = has_irreducible_loops;
4418 switch (desired_class_load_kind) {
4419 case HLoadClass::LoadKind::kReferrersClass:
4420 fallback_load = false;
4421 break;
4422 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4423 DCHECK(!GetCompilerOptions().GetCompilePic());
4424 break;
4425 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4426 DCHECK(GetCompilerOptions().GetCompilePic());
4427 break;
4428 case HLoadClass::LoadKind::kBootImageAddress:
4429 break;
4430 case HLoadClass::LoadKind::kDexCacheAddress:
4431 DCHECK(Runtime::Current()->UseJitCompilation());
4432 fallback_load = false;
4433 break;
4434 case HLoadClass::LoadKind::kDexCachePcRelative:
4435 DCHECK(!Runtime::Current()->UseJitCompilation());
4436 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4437 // with irreducible loops.
4438 break;
4439 case HLoadClass::LoadKind::kDexCacheViaMethod:
4440 fallback_load = false;
4441 break;
4442 }
4443 if (fallback_load) {
4444 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4445 }
4446 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004447}
4448
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004449Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4450 Register temp) {
4451 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4452 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4453 if (!invoke->GetLocations()->Intrinsified()) {
4454 return location.AsRegister<Register>();
4455 }
4456 // For intrinsics we allow any location, so it may be on the stack.
4457 if (!location.IsRegister()) {
4458 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4459 return temp;
4460 }
4461 // For register locations, check if the register was saved. If so, get it from the stack.
4462 // Note: There is a chance that the register was saved but not overwritten, so we could
4463 // save one load. However, since this is just an intrinsic slow path we prefer this
4464 // simple and more robust approach rather that trying to determine if that's the case.
4465 SlowPathCode* slow_path = GetCurrentSlowPath();
4466 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4467 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4468 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4469 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4470 return temp;
4471 }
4472 return location.AsRegister<Register>();
4473}
4474
Vladimir Markodc151b22015-10-15 18:02:30 +01004475HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4476 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004477 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004478 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4479 // We disable PC-relative load when there is an irreducible loop, as the optimization
4480 // is incompatible with it.
4481 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4482 bool fallback_load = true;
4483 bool fallback_call = true;
4484 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004485 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4486 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004487 fallback_load = has_irreducible_loops;
4488 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004489 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004490 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004491 break;
4492 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004493 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004494 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004495 fallback_call = has_irreducible_loops;
4496 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004497 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004498 // TODO: Implement this type.
4499 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004500 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004501 fallback_call = false;
4502 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004503 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004504 if (fallback_load) {
4505 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4506 dispatch_info.method_load_data = 0;
4507 }
4508 if (fallback_call) {
4509 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4510 dispatch_info.direct_code_ptr = 0;
4511 }
4512 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004513}
4514
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004515void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4516 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004517 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004518 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4519 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4520 bool isR6 = isa_features_.IsR6();
4521 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4522 // R6 has PC-relative addressing.
4523 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4524 (!isR6 &&
4525 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4526 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4527 Register base_reg = has_extra_input
4528 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4529 : ZERO;
4530
4531 // For better instruction scheduling we load the direct code pointer before the method pointer.
4532 switch (code_ptr_location) {
4533 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4534 // T9 = invoke->GetDirectCodePtr();
4535 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4536 break;
4537 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4538 // T9 = code address from literal pool with link-time patch.
4539 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4540 break;
4541 default:
4542 break;
4543 }
4544
4545 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004546 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004547 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004548 uint32_t offset =
4549 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004550 __ LoadFromOffset(kLoadWord,
4551 temp.AsRegister<Register>(),
4552 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004553 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004554 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004555 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004556 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004557 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004558 break;
4559 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4560 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4561 break;
4562 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004563 __ LoadLiteral(temp.AsRegister<Register>(),
4564 base_reg,
4565 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4566 break;
4567 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4568 HMipsDexCacheArraysBase* base =
4569 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4570 int32_t offset =
4571 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4572 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4573 break;
4574 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004575 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004576 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004577 Register reg = temp.AsRegister<Register>();
4578 Register method_reg;
4579 if (current_method.IsRegister()) {
4580 method_reg = current_method.AsRegister<Register>();
4581 } else {
4582 // TODO: use the appropriate DCHECK() here if possible.
4583 // DCHECK(invoke->GetLocations()->Intrinsified());
4584 DCHECK(!current_method.IsValid());
4585 method_reg = reg;
4586 __ Lw(reg, SP, kCurrentMethodStackOffset);
4587 }
4588
4589 // temp = temp->dex_cache_resolved_methods_;
4590 __ LoadFromOffset(kLoadWord,
4591 reg,
4592 method_reg,
4593 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004594 // temp = temp[index_in_cache];
4595 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4596 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004597 __ LoadFromOffset(kLoadWord,
4598 reg,
4599 reg,
4600 CodeGenerator::GetCachePointerOffset(index_in_cache));
4601 break;
4602 }
4603 }
4604
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004605 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004606 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004607 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004608 break;
4609 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004610 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4611 // T9 prepared above for better instruction scheduling.
4612 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004613 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004614 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004615 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004616 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004617 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004618 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4619 LOG(FATAL) << "Unsupported";
4620 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004621 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4622 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004623 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004624 T9,
4625 callee_method.AsRegister<Register>(),
4626 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004627 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004628 // T9()
4629 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004630 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004631 break;
4632 }
4633 DCHECK(!IsLeafMethod());
4634}
4635
4636void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004637 // Explicit clinit checks triggered by static invokes must have been pruned by
4638 // art::PrepareForRegisterAllocation.
4639 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004640
4641 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4642 return;
4643 }
4644
4645 LocationSummary* locations = invoke->GetLocations();
4646 codegen_->GenerateStaticOrDirectCall(invoke,
4647 locations->HasTemps()
4648 ? locations->GetTemp(0)
4649 : Location::NoLocation());
4650 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4651}
4652
Chris Larsen3acee732015-11-18 13:31:08 -08004653void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02004654 // Use the calling convention instead of the location of the receiver, as
4655 // intrinsics may have put the receiver in a different register. In the intrinsics
4656 // slow path, the arguments have been moved to the right place, so here we are
4657 // guaranteed that the receiver is the first register of the calling convention.
4658 InvokeDexCallingConvention calling_convention;
4659 Register receiver = calling_convention.GetRegisterAt(0);
4660
Chris Larsen3acee732015-11-18 13:31:08 -08004661 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004662 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4663 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4664 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004665 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004666
4667 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02004668 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08004669 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004670 // temp = temp->GetMethodAt(method_offset);
4671 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4672 // T9 = temp->GetEntryPoint();
4673 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4674 // T9();
4675 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004676 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004677}
4678
4679void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4680 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4681 return;
4682 }
4683
4684 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004685 DCHECK(!codegen_->IsLeafMethod());
4686 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4687}
4688
4689void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004690 if (cls->NeedsAccessCheck()) {
4691 InvokeRuntimeCallingConvention calling_convention;
4692 CodeGenerator::CreateLoadClassLocationSummary(
4693 cls,
4694 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4695 Location::RegisterLocation(V0),
4696 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4697 return;
4698 }
4699
4700 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4701 ? LocationSummary::kCallOnSlowPath
4702 : LocationSummary::kNoCall;
4703 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4704 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4705 switch (load_kind) {
4706 // We need an extra register for PC-relative literals on R2.
4707 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4708 case HLoadClass::LoadKind::kBootImageAddress:
4709 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4710 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4711 break;
4712 }
4713 FALLTHROUGH_INTENDED;
4714 // We need an extra register for PC-relative dex cache accesses.
4715 case HLoadClass::LoadKind::kDexCachePcRelative:
4716 case HLoadClass::LoadKind::kReferrersClass:
4717 case HLoadClass::LoadKind::kDexCacheViaMethod:
4718 locations->SetInAt(0, Location::RequiresRegister());
4719 break;
4720 default:
4721 break;
4722 }
4723 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004724}
4725
4726void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4727 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004728 if (cls->NeedsAccessCheck()) {
4729 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004730 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004731 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004732 return;
4733 }
4734
Alexey Frunze06a46c42016-07-19 15:00:40 -07004735 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4736 Location out_loc = locations->Out();
4737 Register out = out_loc.AsRegister<Register>();
4738 Register base_or_current_method_reg;
4739 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4740 switch (load_kind) {
4741 // We need an extra register for PC-relative literals on R2.
4742 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4743 case HLoadClass::LoadKind::kBootImageAddress:
4744 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4745 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4746 break;
4747 // We need an extra register for PC-relative dex cache accesses.
4748 case HLoadClass::LoadKind::kDexCachePcRelative:
4749 case HLoadClass::LoadKind::kReferrersClass:
4750 case HLoadClass::LoadKind::kDexCacheViaMethod:
4751 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4752 break;
4753 default:
4754 base_or_current_method_reg = ZERO;
4755 break;
4756 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004757
Alexey Frunze06a46c42016-07-19 15:00:40 -07004758 bool generate_null_check = false;
4759 switch (load_kind) {
4760 case HLoadClass::LoadKind::kReferrersClass: {
4761 DCHECK(!cls->CanCallRuntime());
4762 DCHECK(!cls->MustGenerateClinitCheck());
4763 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4764 GenerateGcRootFieldLoad(cls,
4765 out_loc,
4766 base_or_current_method_reg,
4767 ArtMethod::DeclaringClassOffset().Int32Value());
4768 break;
4769 }
4770 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4771 DCHECK(!kEmitCompilerReadBarrier);
4772 __ LoadLiteral(out,
4773 base_or_current_method_reg,
4774 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4775 cls->GetTypeIndex()));
4776 break;
4777 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4778 DCHECK(!kEmitCompilerReadBarrier);
4779 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4780 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004781 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004782 break;
4783 }
4784 case HLoadClass::LoadKind::kBootImageAddress: {
4785 DCHECK(!kEmitCompilerReadBarrier);
4786 DCHECK_NE(cls->GetAddress(), 0u);
4787 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4788 __ LoadLiteral(out,
4789 base_or_current_method_reg,
4790 codegen_->DeduplicateBootImageAddressLiteral(address));
4791 break;
4792 }
4793 case HLoadClass::LoadKind::kDexCacheAddress: {
4794 DCHECK_NE(cls->GetAddress(), 0u);
4795 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4796 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4797 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4798 int16_t offset = Low16Bits(address);
4799 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4800 __ Lui(out, High16Bits(base_address));
4801 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4802 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4803 generate_null_check = !cls->IsInDexCache();
4804 break;
4805 }
4806 case HLoadClass::LoadKind::kDexCachePcRelative: {
4807 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4808 int32_t offset =
4809 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4810 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4811 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4812 generate_null_check = !cls->IsInDexCache();
4813 break;
4814 }
4815 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4816 // /* GcRoot<mirror::Class>[] */ out =
4817 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4818 __ LoadFromOffset(kLoadWord,
4819 out,
4820 base_or_current_method_reg,
4821 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4822 // /* GcRoot<mirror::Class> */ out = out[type_index]
4823 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4824 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4825 generate_null_check = !cls->IsInDexCache();
4826 }
4827 }
4828
4829 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4830 DCHECK(cls->CanCallRuntime());
4831 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4832 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4833 codegen_->AddSlowPath(slow_path);
4834 if (generate_null_check) {
4835 __ Beqz(out, slow_path->GetEntryLabel());
4836 }
4837 if (cls->MustGenerateClinitCheck()) {
4838 GenerateClassInitializationCheck(slow_path, out);
4839 } else {
4840 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004841 }
4842 }
4843}
4844
4845static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004846 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004847}
4848
4849void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4850 LocationSummary* locations =
4851 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4852 locations->SetOut(Location::RequiresRegister());
4853}
4854
4855void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4856 Register out = load->GetLocations()->Out().AsRegister<Register>();
4857 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4858}
4859
4860void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4861 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4862}
4863
4864void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4865 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4866}
4867
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004868void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004869 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00004870 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4871 ? LocationSummary::kCallOnMainOnly
4872 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004873 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004874 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004875 HLoadString::LoadKind load_kind = load->GetLoadKind();
4876 switch (load_kind) {
4877 // We need an extra register for PC-relative literals on R2.
4878 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4879 case HLoadString::LoadKind::kBootImageAddress:
4880 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004881 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004882 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4883 break;
4884 }
4885 FALLTHROUGH_INTENDED;
4886 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07004887 case HLoadString::LoadKind::kDexCacheViaMethod:
4888 locations->SetInAt(0, Location::RequiresRegister());
4889 break;
4890 default:
4891 break;
4892 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004893 locations->SetOut(Location::RequiresRegister());
4894}
4895
4896void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004897 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004898 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004899 Location out_loc = locations->Out();
4900 Register out = out_loc.AsRegister<Register>();
4901 Register base_or_current_method_reg;
4902 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4903 switch (load_kind) {
4904 // We need an extra register for PC-relative literals on R2.
4905 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4906 case HLoadString::LoadKind::kBootImageAddress:
4907 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004908 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004909 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4910 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004911 default:
4912 base_or_current_method_reg = ZERO;
4913 break;
4914 }
4915
4916 switch (load_kind) {
4917 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4918 DCHECK(!kEmitCompilerReadBarrier);
4919 __ LoadLiteral(out,
4920 base_or_current_method_reg,
4921 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4922 load->GetStringIndex()));
4923 return; // No dex cache slow path.
4924 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4925 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004926 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004927 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4928 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004929 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004930 return; // No dex cache slow path.
4931 }
4932 case HLoadString::LoadKind::kBootImageAddress: {
4933 DCHECK(!kEmitCompilerReadBarrier);
4934 DCHECK_NE(load->GetAddress(), 0u);
4935 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4936 __ LoadLiteral(out,
4937 base_or_current_method_reg,
4938 codegen_->DeduplicateBootImageAddressLiteral(address));
4939 return; // No dex cache slow path.
4940 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004941 case HLoadString::LoadKind::kBssEntry: {
4942 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4943 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4944 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
4945 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
4946 __ LoadFromOffset(kLoadWord, out, out, 0);
4947 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4948 codegen_->AddSlowPath(slow_path);
4949 __ Beqz(out, slow_path->GetEntryLabel());
4950 __ Bind(slow_path->GetExitLabel());
4951 return;
4952 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004953 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004954 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004955 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004956
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004957 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004958 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
4959 InvokeRuntimeCallingConvention calling_convention;
4960 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
4961 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4962 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004963}
4964
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004965void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4966 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4967 locations->SetOut(Location::ConstantLocation(constant));
4968}
4969
4970void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4971 // Will be generated at use site.
4972}
4973
4974void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4975 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004976 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004977 InvokeRuntimeCallingConvention calling_convention;
4978 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4979}
4980
4981void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4982 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01004983 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004984 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4985 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004986 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004987 }
4988 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4989}
4990
4991void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4992 LocationSummary* locations =
4993 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4994 switch (mul->GetResultType()) {
4995 case Primitive::kPrimInt:
4996 case Primitive::kPrimLong:
4997 locations->SetInAt(0, Location::RequiresRegister());
4998 locations->SetInAt(1, Location::RequiresRegister());
4999 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5000 break;
5001
5002 case Primitive::kPrimFloat:
5003 case Primitive::kPrimDouble:
5004 locations->SetInAt(0, Location::RequiresFpuRegister());
5005 locations->SetInAt(1, Location::RequiresFpuRegister());
5006 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5007 break;
5008
5009 default:
5010 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5011 }
5012}
5013
5014void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5015 Primitive::Type type = instruction->GetType();
5016 LocationSummary* locations = instruction->GetLocations();
5017 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5018
5019 switch (type) {
5020 case Primitive::kPrimInt: {
5021 Register dst = locations->Out().AsRegister<Register>();
5022 Register lhs = locations->InAt(0).AsRegister<Register>();
5023 Register rhs = locations->InAt(1).AsRegister<Register>();
5024
5025 if (isR6) {
5026 __ MulR6(dst, lhs, rhs);
5027 } else {
5028 __ MulR2(dst, lhs, rhs);
5029 }
5030 break;
5031 }
5032 case Primitive::kPrimLong: {
5033 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5034 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5035 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5036 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5037 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5038 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5039
5040 // Extra checks to protect caused by the existance of A1_A2.
5041 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5042 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5043 DCHECK_NE(dst_high, lhs_low);
5044 DCHECK_NE(dst_high, rhs_low);
5045
5046 // A_B * C_D
5047 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5048 // dst_lo: [ low(B*D) ]
5049 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5050
5051 if (isR6) {
5052 __ MulR6(TMP, lhs_high, rhs_low);
5053 __ MulR6(dst_high, lhs_low, rhs_high);
5054 __ Addu(dst_high, dst_high, TMP);
5055 __ MuhuR6(TMP, lhs_low, rhs_low);
5056 __ Addu(dst_high, dst_high, TMP);
5057 __ MulR6(dst_low, lhs_low, rhs_low);
5058 } else {
5059 __ MulR2(TMP, lhs_high, rhs_low);
5060 __ MulR2(dst_high, lhs_low, rhs_high);
5061 __ Addu(dst_high, dst_high, TMP);
5062 __ MultuR2(lhs_low, rhs_low);
5063 __ Mfhi(TMP);
5064 __ Addu(dst_high, dst_high, TMP);
5065 __ Mflo(dst_low);
5066 }
5067 break;
5068 }
5069 case Primitive::kPrimFloat:
5070 case Primitive::kPrimDouble: {
5071 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5072 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5073 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5074 if (type == Primitive::kPrimFloat) {
5075 __ MulS(dst, lhs, rhs);
5076 } else {
5077 __ MulD(dst, lhs, rhs);
5078 }
5079 break;
5080 }
5081 default:
5082 LOG(FATAL) << "Unexpected mul type " << type;
5083 }
5084}
5085
5086void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5087 LocationSummary* locations =
5088 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5089 switch (neg->GetResultType()) {
5090 case Primitive::kPrimInt:
5091 case Primitive::kPrimLong:
5092 locations->SetInAt(0, Location::RequiresRegister());
5093 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5094 break;
5095
5096 case Primitive::kPrimFloat:
5097 case Primitive::kPrimDouble:
5098 locations->SetInAt(0, Location::RequiresFpuRegister());
5099 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5100 break;
5101
5102 default:
5103 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5104 }
5105}
5106
5107void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5108 Primitive::Type type = instruction->GetType();
5109 LocationSummary* locations = instruction->GetLocations();
5110
5111 switch (type) {
5112 case Primitive::kPrimInt: {
5113 Register dst = locations->Out().AsRegister<Register>();
5114 Register src = locations->InAt(0).AsRegister<Register>();
5115 __ Subu(dst, ZERO, src);
5116 break;
5117 }
5118 case Primitive::kPrimLong: {
5119 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5120 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5121 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5122 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5123 __ Subu(dst_low, ZERO, src_low);
5124 __ Sltu(TMP, ZERO, dst_low);
5125 __ Subu(dst_high, ZERO, src_high);
5126 __ Subu(dst_high, dst_high, TMP);
5127 break;
5128 }
5129 case Primitive::kPrimFloat:
5130 case Primitive::kPrimDouble: {
5131 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5132 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5133 if (type == Primitive::kPrimFloat) {
5134 __ NegS(dst, src);
5135 } else {
5136 __ NegD(dst, src);
5137 }
5138 break;
5139 }
5140 default:
5141 LOG(FATAL) << "Unexpected neg type " << type;
5142 }
5143}
5144
5145void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5146 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005147 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005148 InvokeRuntimeCallingConvention calling_convention;
5149 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5150 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5151 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5152 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5153}
5154
5155void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5156 InvokeRuntimeCallingConvention calling_convention;
5157 Register current_method_register = calling_convention.GetRegisterAt(2);
5158 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5159 // Move an uint16_t value to a register.
5160 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005161 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005162 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5163 void*, uint32_t, int32_t, ArtMethod*>();
5164}
5165
5166void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5167 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005169 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005170 if (instruction->IsStringAlloc()) {
5171 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5172 } else {
5173 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5174 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5175 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005176 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5177}
5178
5179void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005180 if (instruction->IsStringAlloc()) {
5181 // String is allocated through StringFactory. Call NewEmptyString entry point.
5182 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005183 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005184 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5185 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5186 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005187 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005188 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5189 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005190 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00005191 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
5192 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005193}
5194
5195void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5197 locations->SetInAt(0, Location::RequiresRegister());
5198 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5199}
5200
5201void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5202 Primitive::Type type = instruction->GetType();
5203 LocationSummary* locations = instruction->GetLocations();
5204
5205 switch (type) {
5206 case Primitive::kPrimInt: {
5207 Register dst = locations->Out().AsRegister<Register>();
5208 Register src = locations->InAt(0).AsRegister<Register>();
5209 __ Nor(dst, src, ZERO);
5210 break;
5211 }
5212
5213 case Primitive::kPrimLong: {
5214 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5215 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5216 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5217 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5218 __ Nor(dst_high, src_high, ZERO);
5219 __ Nor(dst_low, src_low, ZERO);
5220 break;
5221 }
5222
5223 default:
5224 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5225 }
5226}
5227
5228void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5229 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5230 locations->SetInAt(0, Location::RequiresRegister());
5231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5232}
5233
5234void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5235 LocationSummary* locations = instruction->GetLocations();
5236 __ Xori(locations->Out().AsRegister<Register>(),
5237 locations->InAt(0).AsRegister<Register>(),
5238 1);
5239}
5240
5241void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005242 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5243 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005244}
5245
Calin Juravle2ae48182016-03-16 14:05:09 +00005246void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5247 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005248 return;
5249 }
5250 Location obj = instruction->GetLocations()->InAt(0);
5251
5252 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005253 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005254}
5255
Calin Juravle2ae48182016-03-16 14:05:09 +00005256void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005257 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005258 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005259
5260 Location obj = instruction->GetLocations()->InAt(0);
5261
5262 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5263}
5264
5265void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005266 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005267}
5268
5269void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5270 HandleBinaryOp(instruction);
5271}
5272
5273void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5274 HandleBinaryOp(instruction);
5275}
5276
5277void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5278 LOG(FATAL) << "Unreachable";
5279}
5280
5281void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
5282 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5283}
5284
5285void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
5286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5287 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5288 if (location.IsStackSlot()) {
5289 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5290 } else if (location.IsDoubleStackSlot()) {
5291 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5292 }
5293 locations->SetOut(location);
5294}
5295
5296void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
5297 ATTRIBUTE_UNUSED) {
5298 // Nothing to do, the parameter is already at its location.
5299}
5300
5301void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5302 LocationSummary* locations =
5303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5304 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5305}
5306
5307void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5308 ATTRIBUTE_UNUSED) {
5309 // Nothing to do, the method is already at its location.
5310}
5311
5312void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005314 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005315 locations->SetInAt(i, Location::Any());
5316 }
5317 locations->SetOut(Location::Any());
5318}
5319
5320void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5321 LOG(FATAL) << "Unreachable";
5322}
5323
5324void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5325 Primitive::Type type = rem->GetResultType();
5326 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005327 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005328 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5329
5330 switch (type) {
5331 case Primitive::kPrimInt:
5332 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005333 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5335 break;
5336
5337 case Primitive::kPrimLong: {
5338 InvokeRuntimeCallingConvention calling_convention;
5339 locations->SetInAt(0, Location::RegisterPairLocation(
5340 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5341 locations->SetInAt(1, Location::RegisterPairLocation(
5342 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5343 locations->SetOut(calling_convention.GetReturnLocation(type));
5344 break;
5345 }
5346
5347 case Primitive::kPrimFloat:
5348 case Primitive::kPrimDouble: {
5349 InvokeRuntimeCallingConvention calling_convention;
5350 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5351 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5352 locations->SetOut(calling_convention.GetReturnLocation(type));
5353 break;
5354 }
5355
5356 default:
5357 LOG(FATAL) << "Unexpected rem type " << type;
5358 }
5359}
5360
5361void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5362 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005363
5364 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005365 case Primitive::kPrimInt:
5366 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005367 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005368 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005369 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005370 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5371 break;
5372 }
5373 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005374 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005375 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005376 break;
5377 }
5378 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005379 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005380 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005381 break;
5382 }
5383 default:
5384 LOG(FATAL) << "Unexpected rem type " << type;
5385 }
5386}
5387
5388void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5389 memory_barrier->SetLocations(nullptr);
5390}
5391
5392void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5393 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5394}
5395
5396void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5398 Primitive::Type return_type = ret->InputAt(0)->GetType();
5399 locations->SetInAt(0, MipsReturnLocation(return_type));
5400}
5401
5402void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5403 codegen_->GenerateFrameExit();
5404}
5405
5406void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5407 ret->SetLocations(nullptr);
5408}
5409
5410void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5411 codegen_->GenerateFrameExit();
5412}
5413
Alexey Frunze92d90602015-12-18 18:16:36 -08005414void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5415 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005416}
5417
Alexey Frunze92d90602015-12-18 18:16:36 -08005418void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5419 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005420}
5421
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005422void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5423 HandleShift(shl);
5424}
5425
5426void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5427 HandleShift(shl);
5428}
5429
5430void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5431 HandleShift(shr);
5432}
5433
5434void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5435 HandleShift(shr);
5436}
5437
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005438void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5439 HandleBinaryOp(instruction);
5440}
5441
5442void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5443 HandleBinaryOp(instruction);
5444}
5445
5446void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5447 HandleFieldGet(instruction, instruction->GetFieldInfo());
5448}
5449
5450void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5451 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5452}
5453
5454void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5455 HandleFieldSet(instruction, instruction->GetFieldInfo());
5456}
5457
5458void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5459 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5460}
5461
5462void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5463 HUnresolvedInstanceFieldGet* instruction) {
5464 FieldAccessCallingConventionMIPS calling_convention;
5465 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5466 instruction->GetFieldType(),
5467 calling_convention);
5468}
5469
5470void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5471 HUnresolvedInstanceFieldGet* instruction) {
5472 FieldAccessCallingConventionMIPS calling_convention;
5473 codegen_->GenerateUnresolvedFieldAccess(instruction,
5474 instruction->GetFieldType(),
5475 instruction->GetFieldIndex(),
5476 instruction->GetDexPc(),
5477 calling_convention);
5478}
5479
5480void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5481 HUnresolvedInstanceFieldSet* instruction) {
5482 FieldAccessCallingConventionMIPS calling_convention;
5483 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5484 instruction->GetFieldType(),
5485 calling_convention);
5486}
5487
5488void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5489 HUnresolvedInstanceFieldSet* instruction) {
5490 FieldAccessCallingConventionMIPS calling_convention;
5491 codegen_->GenerateUnresolvedFieldAccess(instruction,
5492 instruction->GetFieldType(),
5493 instruction->GetFieldIndex(),
5494 instruction->GetDexPc(),
5495 calling_convention);
5496}
5497
5498void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5499 HUnresolvedStaticFieldGet* instruction) {
5500 FieldAccessCallingConventionMIPS calling_convention;
5501 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5502 instruction->GetFieldType(),
5503 calling_convention);
5504}
5505
5506void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5507 HUnresolvedStaticFieldGet* instruction) {
5508 FieldAccessCallingConventionMIPS calling_convention;
5509 codegen_->GenerateUnresolvedFieldAccess(instruction,
5510 instruction->GetFieldType(),
5511 instruction->GetFieldIndex(),
5512 instruction->GetDexPc(),
5513 calling_convention);
5514}
5515
5516void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5517 HUnresolvedStaticFieldSet* instruction) {
5518 FieldAccessCallingConventionMIPS calling_convention;
5519 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5520 instruction->GetFieldType(),
5521 calling_convention);
5522}
5523
5524void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5525 HUnresolvedStaticFieldSet* instruction) {
5526 FieldAccessCallingConventionMIPS calling_convention;
5527 codegen_->GenerateUnresolvedFieldAccess(instruction,
5528 instruction->GetFieldType(),
5529 instruction->GetFieldIndex(),
5530 instruction->GetDexPc(),
5531 calling_convention);
5532}
5533
5534void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005535 LocationSummary* locations =
5536 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005537 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005538}
5539
5540void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5541 HBasicBlock* block = instruction->GetBlock();
5542 if (block->GetLoopInformation() != nullptr) {
5543 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5544 // The back edge will generate the suspend check.
5545 return;
5546 }
5547 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5548 // The goto will generate the suspend check.
5549 return;
5550 }
5551 GenerateSuspendCheck(instruction, nullptr);
5552}
5553
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005554void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5555 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005557 InvokeRuntimeCallingConvention calling_convention;
5558 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5559}
5560
5561void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005562 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005563 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5564}
5565
5566void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5567 Primitive::Type input_type = conversion->GetInputType();
5568 Primitive::Type result_type = conversion->GetResultType();
5569 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005570 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005571
5572 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5573 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5574 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5575 }
5576
5577 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005578 if (!isR6 &&
5579 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5580 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005581 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005582 }
5583
5584 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5585
5586 if (call_kind == LocationSummary::kNoCall) {
5587 if (Primitive::IsFloatingPointType(input_type)) {
5588 locations->SetInAt(0, Location::RequiresFpuRegister());
5589 } else {
5590 locations->SetInAt(0, Location::RequiresRegister());
5591 }
5592
5593 if (Primitive::IsFloatingPointType(result_type)) {
5594 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5595 } else {
5596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5597 }
5598 } else {
5599 InvokeRuntimeCallingConvention calling_convention;
5600
5601 if (Primitive::IsFloatingPointType(input_type)) {
5602 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5603 } else {
5604 DCHECK_EQ(input_type, Primitive::kPrimLong);
5605 locations->SetInAt(0, Location::RegisterPairLocation(
5606 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5607 }
5608
5609 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5610 }
5611}
5612
5613void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5614 LocationSummary* locations = conversion->GetLocations();
5615 Primitive::Type result_type = conversion->GetResultType();
5616 Primitive::Type input_type = conversion->GetInputType();
5617 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005618 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005619
5620 DCHECK_NE(input_type, result_type);
5621
5622 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5623 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5624 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5625 Register src = locations->InAt(0).AsRegister<Register>();
5626
Alexey Frunzea871ef12016-06-27 15:20:11 -07005627 if (dst_low != src) {
5628 __ Move(dst_low, src);
5629 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005630 __ Sra(dst_high, src, 31);
5631 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5632 Register dst = locations->Out().AsRegister<Register>();
5633 Register src = (input_type == Primitive::kPrimLong)
5634 ? locations->InAt(0).AsRegisterPairLow<Register>()
5635 : locations->InAt(0).AsRegister<Register>();
5636
5637 switch (result_type) {
5638 case Primitive::kPrimChar:
5639 __ Andi(dst, src, 0xFFFF);
5640 break;
5641 case Primitive::kPrimByte:
5642 if (has_sign_extension) {
5643 __ Seb(dst, src);
5644 } else {
5645 __ Sll(dst, src, 24);
5646 __ Sra(dst, dst, 24);
5647 }
5648 break;
5649 case Primitive::kPrimShort:
5650 if (has_sign_extension) {
5651 __ Seh(dst, src);
5652 } else {
5653 __ Sll(dst, src, 16);
5654 __ Sra(dst, dst, 16);
5655 }
5656 break;
5657 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005658 if (dst != src) {
5659 __ Move(dst, src);
5660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005661 break;
5662
5663 default:
5664 LOG(FATAL) << "Unexpected type conversion from " << input_type
5665 << " to " << result_type;
5666 }
5667 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005668 if (input_type == Primitive::kPrimLong) {
5669 if (isR6) {
5670 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5671 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5672 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5673 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5674 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5675 __ Mtc1(src_low, FTMP);
5676 __ Mthc1(src_high, FTMP);
5677 if (result_type == Primitive::kPrimFloat) {
5678 __ Cvtsl(dst, FTMP);
5679 } else {
5680 __ Cvtdl(dst, FTMP);
5681 }
5682 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005683 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
5684 : kQuickL2d;
5685 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005686 if (result_type == Primitive::kPrimFloat) {
5687 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5688 } else {
5689 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5690 }
5691 }
5692 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005693 Register src = locations->InAt(0).AsRegister<Register>();
5694 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5695 __ Mtc1(src, FTMP);
5696 if (result_type == Primitive::kPrimFloat) {
5697 __ Cvtsw(dst, FTMP);
5698 } else {
5699 __ Cvtdw(dst, FTMP);
5700 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005701 }
5702 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5703 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005704 if (result_type == Primitive::kPrimLong) {
5705 if (isR6) {
5706 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5707 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5708 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5709 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5710 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5711 MipsLabel truncate;
5712 MipsLabel done;
5713
5714 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5715 // value when the input is either a NaN or is outside of the range of the output type
5716 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5717 // the same result.
5718 //
5719 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5720 // value of the output type if the input is outside of the range after the truncation or
5721 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5722 // results. This matches the desired float/double-to-int/long conversion exactly.
5723 //
5724 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5725 //
5726 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5727 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5728 // even though it must be NAN2008=1 on R6.
5729 //
5730 // The code takes care of the different behaviors by first comparing the input to the
5731 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5732 // If the input is greater than or equal to the minimum, it procedes to the truncate
5733 // instruction, which will handle such an input the same way irrespective of NAN2008.
5734 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5735 // in order to return either zero or the minimum value.
5736 //
5737 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5738 // truncate instruction for MIPS64R6.
5739 if (input_type == Primitive::kPrimFloat) {
5740 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5741 __ LoadConst32(TMP, min_val);
5742 __ Mtc1(TMP, FTMP);
5743 __ CmpLeS(FTMP, FTMP, src);
5744 } else {
5745 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5746 __ LoadConst32(TMP, High32Bits(min_val));
5747 __ Mtc1(ZERO, FTMP);
5748 __ Mthc1(TMP, FTMP);
5749 __ CmpLeD(FTMP, FTMP, src);
5750 }
5751
5752 __ Bc1nez(FTMP, &truncate);
5753
5754 if (input_type == Primitive::kPrimFloat) {
5755 __ CmpEqS(FTMP, src, src);
5756 } else {
5757 __ CmpEqD(FTMP, src, src);
5758 }
5759 __ Move(dst_low, ZERO);
5760 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5761 __ Mfc1(TMP, FTMP);
5762 __ And(dst_high, dst_high, TMP);
5763
5764 __ B(&done);
5765
5766 __ Bind(&truncate);
5767
5768 if (input_type == Primitive::kPrimFloat) {
5769 __ TruncLS(FTMP, src);
5770 } else {
5771 __ TruncLD(FTMP, src);
5772 }
5773 __ Mfc1(dst_low, FTMP);
5774 __ Mfhc1(dst_high, FTMP);
5775
5776 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005777 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005778 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
5779 : kQuickD2l;
5780 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005781 if (input_type == Primitive::kPrimFloat) {
5782 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5783 } else {
5784 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5785 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005786 }
5787 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005788 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5789 Register dst = locations->Out().AsRegister<Register>();
5790 MipsLabel truncate;
5791 MipsLabel done;
5792
5793 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5794 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5795 // even though it must be NAN2008=1 on R6.
5796 //
5797 // For details see the large comment above for the truncation of float/double to long on R6.
5798 //
5799 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5800 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005801 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005802 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5803 __ LoadConst32(TMP, min_val);
5804 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005805 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005806 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5807 __ LoadConst32(TMP, High32Bits(min_val));
5808 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005809 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005810 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005811
5812 if (isR6) {
5813 if (input_type == Primitive::kPrimFloat) {
5814 __ CmpLeS(FTMP, FTMP, src);
5815 } else {
5816 __ CmpLeD(FTMP, FTMP, src);
5817 }
5818 __ Bc1nez(FTMP, &truncate);
5819
5820 if (input_type == Primitive::kPrimFloat) {
5821 __ CmpEqS(FTMP, src, src);
5822 } else {
5823 __ CmpEqD(FTMP, src, src);
5824 }
5825 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5826 __ Mfc1(TMP, FTMP);
5827 __ And(dst, dst, TMP);
5828 } else {
5829 if (input_type == Primitive::kPrimFloat) {
5830 __ ColeS(0, FTMP, src);
5831 } else {
5832 __ ColeD(0, FTMP, src);
5833 }
5834 __ Bc1t(0, &truncate);
5835
5836 if (input_type == Primitive::kPrimFloat) {
5837 __ CeqS(0, src, src);
5838 } else {
5839 __ CeqD(0, src, src);
5840 }
5841 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5842 __ Movf(dst, ZERO, 0);
5843 }
5844
5845 __ B(&done);
5846
5847 __ Bind(&truncate);
5848
5849 if (input_type == Primitive::kPrimFloat) {
5850 __ TruncWS(FTMP, src);
5851 } else {
5852 __ TruncWD(FTMP, src);
5853 }
5854 __ Mfc1(dst, FTMP);
5855
5856 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005857 }
5858 } else if (Primitive::IsFloatingPointType(result_type) &&
5859 Primitive::IsFloatingPointType(input_type)) {
5860 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5861 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5862 if (result_type == Primitive::kPrimFloat) {
5863 __ Cvtsd(dst, src);
5864 } else {
5865 __ Cvtds(dst, src);
5866 }
5867 } else {
5868 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5869 << " to " << result_type;
5870 }
5871}
5872
5873void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5874 HandleShift(ushr);
5875}
5876
5877void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5878 HandleShift(ushr);
5879}
5880
5881void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5882 HandleBinaryOp(instruction);
5883}
5884
5885void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5886 HandleBinaryOp(instruction);
5887}
5888
5889void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5890 // Nothing to do, this should be removed during prepare for register allocator.
5891 LOG(FATAL) << "Unreachable";
5892}
5893
5894void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5895 // Nothing to do, this should be removed during prepare for register allocator.
5896 LOG(FATAL) << "Unreachable";
5897}
5898
5899void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005900 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005901}
5902
5903void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005904 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005905}
5906
5907void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005908 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005909}
5910
5911void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005912 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005913}
5914
5915void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005916 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005917}
5918
5919void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005920 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005921}
5922
5923void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005924 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005925}
5926
5927void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005928 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005929}
5930
5931void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005932 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005933}
5934
5935void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005936 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005937}
5938
5939void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005940 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005941}
5942
5943void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005944 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005945}
5946
5947void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005948 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005949}
5950
5951void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005952 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005953}
5954
5955void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005956 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005957}
5958
5959void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005960 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005961}
5962
5963void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005964 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005965}
5966
5967void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005968 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005969}
5970
5971void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005972 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005973}
5974
5975void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005976 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005977}
5978
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005979void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5980 LocationSummary* locations =
5981 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5982 locations->SetInAt(0, Location::RequiresRegister());
5983}
5984
Alexey Frunze96b66822016-09-10 02:32:44 -07005985void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
5986 int32_t lower_bound,
5987 uint32_t num_entries,
5988 HBasicBlock* switch_block,
5989 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005990 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005991 Register temp_reg = TMP;
5992 __ Addiu32(temp_reg, value_reg, -lower_bound);
5993 // Jump to default if index is negative
5994 // Note: We don't check the case that index is positive while value < lower_bound, because in
5995 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5996 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5997
Alexey Frunze96b66822016-09-10 02:32:44 -07005998 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005999 // Jump to successors[0] if value == lower_bound.
6000 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6001 int32_t last_index = 0;
6002 for (; num_entries - last_index > 2; last_index += 2) {
6003 __ Addiu(temp_reg, temp_reg, -2);
6004 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6005 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6006 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6007 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6008 }
6009 if (num_entries - last_index == 2) {
6010 // The last missing case_value.
6011 __ Addiu(temp_reg, temp_reg, -1);
6012 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006013 }
6014
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006015 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006016 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006017 __ B(codegen_->GetLabelOf(default_block));
6018 }
6019}
6020
Alexey Frunze96b66822016-09-10 02:32:44 -07006021void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6022 Register constant_area,
6023 int32_t lower_bound,
6024 uint32_t num_entries,
6025 HBasicBlock* switch_block,
6026 HBasicBlock* default_block) {
6027 // Create a jump table.
6028 std::vector<MipsLabel*> labels(num_entries);
6029 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6030 for (uint32_t i = 0; i < num_entries; i++) {
6031 labels[i] = codegen_->GetLabelOf(successors[i]);
6032 }
6033 JumpTable* table = __ CreateJumpTable(std::move(labels));
6034
6035 // Is the value in range?
6036 __ Addiu32(TMP, value_reg, -lower_bound);
6037 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6038 __ Sltiu(AT, TMP, num_entries);
6039 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6040 } else {
6041 __ LoadConst32(AT, num_entries);
6042 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6043 }
6044
6045 // We are in the range of the table.
6046 // Load the target address from the jump table, indexing by the value.
6047 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6048 __ Sll(TMP, TMP, 2);
6049 __ Addu(TMP, TMP, AT);
6050 __ Lw(TMP, TMP, 0);
6051 // Compute the absolute target address by adding the table start address
6052 // (the table contains offsets to targets relative to its start).
6053 __ Addu(TMP, TMP, AT);
6054 // And jump.
6055 __ Jr(TMP);
6056 __ NopIfNoReordering();
6057}
6058
6059void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6060 int32_t lower_bound = switch_instr->GetStartValue();
6061 uint32_t num_entries = switch_instr->GetNumEntries();
6062 LocationSummary* locations = switch_instr->GetLocations();
6063 Register value_reg = locations->InAt(0).AsRegister<Register>();
6064 HBasicBlock* switch_block = switch_instr->GetBlock();
6065 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6066
6067 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6068 num_entries > kPackedSwitchJumpTableThreshold) {
6069 // R6 uses PC-relative addressing to access the jump table.
6070 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6071 // the jump table and it is implemented by changing HPackedSwitch to
6072 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6073 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6074 GenTableBasedPackedSwitch(value_reg,
6075 ZERO,
6076 lower_bound,
6077 num_entries,
6078 switch_block,
6079 default_block);
6080 } else {
6081 GenPackedSwitchWithCompares(value_reg,
6082 lower_bound,
6083 num_entries,
6084 switch_block,
6085 default_block);
6086 }
6087}
6088
6089void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6090 LocationSummary* locations =
6091 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6092 locations->SetInAt(0, Location::RequiresRegister());
6093 // Constant area pointer (HMipsComputeBaseMethodAddress).
6094 locations->SetInAt(1, Location::RequiresRegister());
6095}
6096
6097void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6098 int32_t lower_bound = switch_instr->GetStartValue();
6099 uint32_t num_entries = switch_instr->GetNumEntries();
6100 LocationSummary* locations = switch_instr->GetLocations();
6101 Register value_reg = locations->InAt(0).AsRegister<Register>();
6102 Register constant_area = locations->InAt(1).AsRegister<Register>();
6103 HBasicBlock* switch_block = switch_instr->GetBlock();
6104 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6105
6106 // This is an R2-only path. HPackedSwitch has been changed to
6107 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6108 // required to address the jump table relative to PC.
6109 GenTableBasedPackedSwitch(value_reg,
6110 constant_area,
6111 lower_bound,
6112 num_entries,
6113 switch_block,
6114 default_block);
6115}
6116
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006117void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6118 HMipsComputeBaseMethodAddress* insn) {
6119 LocationSummary* locations =
6120 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6121 locations->SetOut(Location::RequiresRegister());
6122}
6123
6124void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6125 HMipsComputeBaseMethodAddress* insn) {
6126 LocationSummary* locations = insn->GetLocations();
6127 Register reg = locations->Out().AsRegister<Register>();
6128
6129 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6130
6131 // Generate a dummy PC-relative call to obtain PC.
6132 __ Nal();
6133 // Grab the return address off RA.
6134 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006135 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006136
6137 // Remember this offset (the obtained PC value) for later use with constant area.
6138 __ BindPcRelBaseLabel();
6139}
6140
6141void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6142 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6143 locations->SetOut(Location::RequiresRegister());
6144}
6145
6146void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6147 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6148 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6149 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006150 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6151 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006152}
6153
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006154void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6155 // The trampoline uses the same calling convention as dex calling conventions,
6156 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6157 // the method_idx.
6158 HandleInvoke(invoke);
6159}
6160
6161void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6162 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6163}
6164
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006165void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6166 LocationSummary* locations =
6167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6168 locations->SetInAt(0, Location::RequiresRegister());
6169 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006170}
6171
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006172void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6173 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006174 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006175 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006176 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006177 __ LoadFromOffset(kLoadWord,
6178 locations->Out().AsRegister<Register>(),
6179 locations->InAt(0).AsRegister<Register>(),
6180 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006181 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006182 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006183 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006184 __ LoadFromOffset(kLoadWord,
6185 locations->Out().AsRegister<Register>(),
6186 locations->InAt(0).AsRegister<Register>(),
6187 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006188 __ LoadFromOffset(kLoadWord,
6189 locations->Out().AsRegister<Register>(),
6190 locations->Out().AsRegister<Register>(),
6191 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006192 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006193}
6194
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006195#undef __
6196#undef QUICK_ENTRY_POINT
6197
6198} // namespace mips
6199} // namespace art