blob: 12b1ab9abb9b4169e6bd74240c0e375335ba00d5 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Alexey Frunzee3fb2452016-05-10 16:08:05 -070043// We'll maximize the range of a single load instruction for dex cache array accesses
44// by aligning offset -32768 with the offset of the first used element.
45static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
46
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020047Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
102 if (calling_convention.GetRegisterAt(gp_index) == A1) {
103 gp_index_++; // Skip A1, and use A2_A3 instead.
104 gp_index++;
105 }
106 Register low_even = calling_convention.GetRegisterAt(gp_index);
107 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
108 DCHECK_EQ(low_even + 1, high_odd);
109 next_location = Location::RegisterPairLocation(low_even, high_odd);
110 } else {
111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
112 next_location = Location::DoubleStackSlot(stack_offset);
113 }
114 break;
115 }
116
117 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
118 // will take up the even/odd pair, while floats are stored in even regs only.
119 // On 64 bit FPU, both double and float are stored in even registers only.
120 case Primitive::kPrimFloat:
121 case Primitive::kPrimDouble: {
122 uint32_t float_index = float_index_++;
123 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
124 next_location = Location::FpuRegisterLocation(
125 calling_convention.GetFpuRegisterAt(float_index));
126 } else {
127 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
128 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
129 : Location::StackSlot(stack_offset);
130 }
131 break;
132 }
133
134 case Primitive::kPrimVoid:
135 LOG(FATAL) << "Unexpected parameter type " << type;
136 break;
137 }
138
139 // Space on the stack is reserved for all arguments.
140 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
141
142 return next_location;
143}
144
145Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
146 return MipsReturnLocation(type);
147}
148
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100149// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
150#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700151#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200152
153class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
154 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000155 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200156
157 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
158 LocationSummary* locations = instruction_->GetLocations();
159 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
160 __ Bind(GetEntryLabel());
161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
167 InvokeRuntimeCallingConvention calling_convention;
168 codegen->EmitParallelMoves(locations->InAt(0),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
170 Primitive::kPrimInt,
171 locations->InAt(1),
172 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
173 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100174 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
175 ? kQuickThrowStringBounds
176 : kQuickThrowArrayBounds;
177 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100178 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200179 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
180 }
181
182 bool IsFatal() const OVERRIDE { return true; }
183
184 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
185
186 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
188};
189
190class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
191 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000192 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193
194 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
196 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100197 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200198 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
199 }
200
201 bool IsFatal() const OVERRIDE { return true; }
202
203 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
204
205 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
207};
208
209class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
210 public:
211 LoadClassSlowPathMIPS(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000215 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = at_->GetLocations();
221 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
222
223 __ Bind(GetEntryLabel());
224 SaveLiveRegisters(codegen, locations);
225
226 InvokeRuntimeCallingConvention calling_convention;
227 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
228
Serban Constantinescufca16662016-07-14 09:21:59 +0100229 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
230 : kQuickInitializeType;
231 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 if (do_clinit_) {
233 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
234 } else {
235 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
236 }
237
238 // Move the class to the desired location.
239 Location out = locations->Out();
240 if (out.IsValid()) {
241 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
242 Primitive::Type type = at_->GetType();
243 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
244 }
245
246 RestoreLiveRegisters(codegen, locations);
247 __ B(GetExitLabel());
248 }
249
250 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
251
252 private:
253 // The class this slow path will load.
254 HLoadClass* const cls_;
255
256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
267};
268
269class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
270 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000271 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200272
273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
274 LocationSummary* locations = instruction_->GetLocations();
275 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
276 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
277
278 __ Bind(GetEntryLabel());
279 SaveLiveRegisters(codegen, locations);
280
281 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000282 HLoadString* load = instruction_->AsLoadString();
283 const uint32_t string_index = load->GetStringIndex();
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufca16662016-07-14 09:21:59 +0100285 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
287 Primitive::Type type = instruction_->GetType();
288 mips_codegen->MoveLocation(locations->Out(),
289 calling_convention.GetReturnLocation(type),
290 type);
291
292 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000293
294 // Store the resolved String to the BSS entry.
295 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
296 // .bss entry address in the fast path, so that we can avoid another calculation here.
297 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
298 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
299 Register out = locations->Out().AsRegister<Register>();
300 DCHECK_NE(out, AT);
301 CodeGeneratorMIPS::PcRelativePatchInfo* info =
302 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
303 mips_codegen->EmitPcRelativeAddressPlaceholder(info, TMP, base);
304 __ StoreToOffset(kStoreWord, out, TMP, 0);
305
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
310
311 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200312 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
313};
314
315class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
316 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200318
319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
320 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
321 __ Bind(GetEntryLabel());
322 if (instruction_->CanThrowIntoCatchBlock()) {
323 // Live registers will be restored in the catch block if caught.
324 SaveLiveRegisters(codegen, instruction_->GetLocations());
325 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100326 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200327 instruction_,
328 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100329 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
331 }
332
333 bool IsFatal() const OVERRIDE { return true; }
334
335 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
336
337 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200338 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
339};
340
341class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
342 public:
343 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000344 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
348 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100349 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 if (successor_ == nullptr) {
352 __ B(GetReturnLabel());
353 } else {
354 __ B(mips_codegen->GetLabelOf(successor_));
355 }
356 }
357
358 MipsLabel* GetReturnLabel() {
359 DCHECK(successor_ == nullptr);
360 return &return_label_;
361 }
362
363 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
364
365 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200366 // If not null, the block to branch to after the suspend check.
367 HBasicBlock* const successor_;
368
369 // If `successor_` is null, the label to branch to after the suspend check.
370 MipsLabel return_label_;
371
372 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
373};
374
375class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
376 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000377 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200378
379 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
380 LocationSummary* locations = instruction_->GetLocations();
381 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
382 uint32_t dex_pc = instruction_->GetDexPc();
383 DCHECK(instruction_->IsCheckCast()
384 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
385 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
386
387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, locations);
389
390 // We're moving two locations to locations that could overlap, so we need a parallel
391 // move resolver.
392 InvokeRuntimeCallingConvention calling_convention;
393 codegen->EmitParallelMoves(locations->InAt(1),
394 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
395 Primitive::kPrimNot,
396 object_class,
397 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
398 Primitive::kPrimNot);
399
400 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100401 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000402 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700403 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404 Primitive::Type ret_type = instruction_->GetType();
405 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
406 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 } else {
408 DCHECK(instruction_->IsCheckCast());
Serban Constantinescufca16662016-07-14 09:21:59 +0100409 mips_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
411 }
412
413 RestoreLiveRegisters(codegen, locations);
414 __ B(GetExitLabel());
415 }
416
417 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
418
419 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
421};
422
423class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
424 public:
Aart Bik42249c32016-01-07 15:33:50 -0800425 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000426 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200427
428 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800429 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100431 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000432 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 }
434
435 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
436
437 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
439};
440
441CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
442 const MipsInstructionSetFeatures& isa_features,
443 const CompilerOptions& compiler_options,
444 OptimizingCompilerStats* stats)
445 : CodeGenerator(graph,
446 kNumberOfCoreRegisters,
447 kNumberOfFRegisters,
448 kNumberOfRegisterPairs,
449 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
450 arraysize(kCoreCalleeSaves)),
451 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
452 arraysize(kFpuCalleeSaves)),
453 compiler_options,
454 stats),
455 block_labels_(nullptr),
456 location_builder_(graph, this),
457 instruction_visitor_(graph, this),
458 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100459 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700460 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700461 uint32_literals_(std::less<uint32_t>(),
462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700463 method_patches_(MethodReferenceComparator(),
464 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
465 call_patches_(MethodReferenceComparator(),
466 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700467 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
468 boot_image_string_patches_(StringReferenceValueComparator(),
469 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
470 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
471 boot_image_type_patches_(TypeReferenceValueComparator(),
472 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
473 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
474 boot_image_address_patches_(std::less<uint32_t>(),
475 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
476 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200477 // Save RA (containing the return address) to mimic Quick.
478 AddAllocatedRegister(Location::RegisterLocation(RA));
479}
480
481#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100482// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
483#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700484#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200485
486void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
487 // Ensure that we fix up branches.
488 __ FinalizeCode();
489
490 // Adjust native pc offsets in stack maps.
491 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
492 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
493 uint32_t new_position = __ GetAdjustedPosition(old_position);
494 DCHECK_GE(new_position, old_position);
495 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
496 }
497
498 // Adjust pc offsets for the disassembly information.
499 if (disasm_info_ != nullptr) {
500 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
501 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
502 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
503 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
504 it.second.start = __ GetAdjustedPosition(it.second.start);
505 it.second.end = __ GetAdjustedPosition(it.second.end);
506 }
507 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
508 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
509 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
510 }
511 }
512
513 CodeGenerator::Finalize(allocator);
514}
515
516MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
517 return codegen_->GetAssembler();
518}
519
520void ParallelMoveResolverMIPS::EmitMove(size_t index) {
521 DCHECK_LT(index, moves_.size());
522 MoveOperands* move = moves_[index];
523 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
524}
525
526void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
527 DCHECK_LT(index, moves_.size());
528 MoveOperands* move = moves_[index];
529 Primitive::Type type = move->GetType();
530 Location loc1 = move->GetDestination();
531 Location loc2 = move->GetSource();
532
533 DCHECK(!loc1.IsConstant());
534 DCHECK(!loc2.IsConstant());
535
536 if (loc1.Equals(loc2)) {
537 return;
538 }
539
540 if (loc1.IsRegister() && loc2.IsRegister()) {
541 // Swap 2 GPRs.
542 Register r1 = loc1.AsRegister<Register>();
543 Register r2 = loc2.AsRegister<Register>();
544 __ Move(TMP, r2);
545 __ Move(r2, r1);
546 __ Move(r1, TMP);
547 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
548 FRegister f1 = loc1.AsFpuRegister<FRegister>();
549 FRegister f2 = loc2.AsFpuRegister<FRegister>();
550 if (type == Primitive::kPrimFloat) {
551 __ MovS(FTMP, f2);
552 __ MovS(f2, f1);
553 __ MovS(f1, FTMP);
554 } else {
555 DCHECK_EQ(type, Primitive::kPrimDouble);
556 __ MovD(FTMP, f2);
557 __ MovD(f2, f1);
558 __ MovD(f1, FTMP);
559 }
560 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
561 (loc1.IsFpuRegister() && loc2.IsRegister())) {
562 // Swap FPR and GPR.
563 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
564 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
565 : loc2.AsFpuRegister<FRegister>();
566 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
567 : loc2.AsRegister<Register>();
568 __ Move(TMP, r2);
569 __ Mfc1(r2, f1);
570 __ Mtc1(TMP, f1);
571 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
572 // Swap 2 GPR register pairs.
573 Register r1 = loc1.AsRegisterPairLow<Register>();
574 Register r2 = loc2.AsRegisterPairLow<Register>();
575 __ Move(TMP, r2);
576 __ Move(r2, r1);
577 __ Move(r1, TMP);
578 r1 = loc1.AsRegisterPairHigh<Register>();
579 r2 = loc2.AsRegisterPairHigh<Register>();
580 __ Move(TMP, r2);
581 __ Move(r2, r1);
582 __ Move(r1, TMP);
583 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
584 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
585 // Swap FPR and GPR register pair.
586 DCHECK_EQ(type, Primitive::kPrimDouble);
587 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
588 : loc2.AsFpuRegister<FRegister>();
589 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
590 : loc2.AsRegisterPairLow<Register>();
591 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
592 : loc2.AsRegisterPairHigh<Register>();
593 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
594 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
595 // unpredictable and the following mfch1 will fail.
596 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800597 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200598 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800599 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200600 __ Move(r2_l, TMP);
601 __ Move(r2_h, AT);
602 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
603 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
604 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
605 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000606 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
607 (loc1.IsStackSlot() && loc2.IsRegister())) {
608 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
609 : loc2.AsRegister<Register>();
610 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
611 : loc2.GetStackIndex();
612 __ Move(TMP, reg);
613 __ LoadFromOffset(kLoadWord, reg, SP, offset);
614 __ StoreToOffset(kStoreWord, TMP, SP, offset);
615 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
616 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
617 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
618 : loc2.AsRegisterPairLow<Register>();
619 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
620 : loc2.AsRegisterPairHigh<Register>();
621 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
622 : loc2.GetStackIndex();
623 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
624 : loc2.GetHighStackIndex(kMipsWordSize);
625 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000626 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000627 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000628 __ Move(TMP, reg_h);
629 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
630 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200631 } else {
632 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
633 }
634}
635
636void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
637 __ Pop(static_cast<Register>(reg));
638}
639
640void ParallelMoveResolverMIPS::SpillScratch(int reg) {
641 __ Push(static_cast<Register>(reg));
642}
643
644void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
645 // Allocate a scratch register other than TMP, if available.
646 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
647 // automatically unspilled when the scratch scope object is destroyed).
648 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
649 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
650 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
651 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
652 __ LoadFromOffset(kLoadWord,
653 Register(ensure_scratch.GetRegister()),
654 SP,
655 index1 + stack_offset);
656 __ LoadFromOffset(kLoadWord,
657 TMP,
658 SP,
659 index2 + stack_offset);
660 __ StoreToOffset(kStoreWord,
661 Register(ensure_scratch.GetRegister()),
662 SP,
663 index2 + stack_offset);
664 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
665 }
666}
667
Alexey Frunze73296a72016-06-03 22:51:46 -0700668void CodeGeneratorMIPS::ComputeSpillMask() {
669 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
670 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
671 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
672 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
673 // registers, include the ZERO register to force alignment of FPU callee-saved registers
674 // within the stack frame.
675 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
676 core_spill_mask_ |= (1 << ZERO);
677 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700678}
679
680bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700681 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700682 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
683 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
684 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700685 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
686 // saved in an unused temporary register) and saving of RA and the current method pointer
687 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700688 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700689}
690
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200691static dwarf::Reg DWARFReg(Register reg) {
692 return dwarf::Reg::MipsCore(static_cast<int>(reg));
693}
694
695// TODO: mapping of floating-point registers to DWARF.
696
697void CodeGeneratorMIPS::GenerateFrameEntry() {
698 __ Bind(&frame_entry_label_);
699
700 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
701
702 if (do_overflow_check) {
703 __ LoadFromOffset(kLoadWord,
704 ZERO,
705 SP,
706 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
707 RecordPcInfo(nullptr, 0);
708 }
709
710 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700711 CHECK_EQ(fpu_spill_mask_, 0u);
712 CHECK_EQ(core_spill_mask_, 1u << RA);
713 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200714 return;
715 }
716
717 // Make sure the frame size isn't unreasonably large.
718 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
719 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
720 }
721
722 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200723
Alexey Frunze73296a72016-06-03 22:51:46 -0700724 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200725 __ IncreaseFrameSize(ofs);
726
Alexey Frunze73296a72016-06-03 22:51:46 -0700727 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
728 Register reg = static_cast<Register>(MostSignificantBit(mask));
729 mask ^= 1u << reg;
730 ofs -= kMipsWordSize;
731 // The ZERO register is only included for alignment.
732 if (reg != ZERO) {
733 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200734 __ cfi().RelOffset(DWARFReg(reg), ofs);
735 }
736 }
737
Alexey Frunze73296a72016-06-03 22:51:46 -0700738 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
739 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
740 mask ^= 1u << reg;
741 ofs -= kMipsDoublewordSize;
742 __ StoreDToOffset(reg, SP, ofs);
743 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200744 }
745
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100746 // Save the current method if we need it. Note that we do not
747 // do this in HCurrentMethod, as the instruction might have been removed
748 // in the SSA graph.
749 if (RequiresCurrentMethod()) {
750 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
751 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200752}
753
754void CodeGeneratorMIPS::GenerateFrameExit() {
755 __ cfi().RememberState();
756
757 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200758 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200759
Alexey Frunze73296a72016-06-03 22:51:46 -0700760 // For better instruction scheduling restore RA before other registers.
761 uint32_t ofs = GetFrameSize();
762 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
763 Register reg = static_cast<Register>(MostSignificantBit(mask));
764 mask ^= 1u << reg;
765 ofs -= kMipsWordSize;
766 // The ZERO register is only included for alignment.
767 if (reg != ZERO) {
768 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200769 __ cfi().Restore(DWARFReg(reg));
770 }
771 }
772
Alexey Frunze73296a72016-06-03 22:51:46 -0700773 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
774 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
775 mask ^= 1u << reg;
776 ofs -= kMipsDoublewordSize;
777 __ LoadDFromOffset(reg, SP, ofs);
778 // TODO: __ cfi().Restore(DWARFReg(reg));
779 }
780
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700781 size_t frame_size = GetFrameSize();
782 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
783 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
784 bool reordering = __ SetReorder(false);
785 if (exchange) {
786 __ Jr(RA);
787 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
788 } else {
789 __ DecreaseFrameSize(frame_size);
790 __ Jr(RA);
791 __ Nop(); // In delay slot.
792 }
793 __ SetReorder(reordering);
794 } else {
795 __ Jr(RA);
796 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200797 }
798
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200799 __ cfi().RestoreState();
800 __ cfi().DefCFAOffset(GetFrameSize());
801}
802
803void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
804 __ Bind(GetLabelOf(block));
805}
806
807void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
808 if (src.Equals(dst)) {
809 return;
810 }
811
812 if (src.IsConstant()) {
813 MoveConstant(dst, src.GetConstant());
814 } else {
815 if (Primitive::Is64BitType(dst_type)) {
816 Move64(dst, src);
817 } else {
818 Move32(dst, src);
819 }
820 }
821}
822
823void CodeGeneratorMIPS::Move32(Location destination, Location source) {
824 if (source.Equals(destination)) {
825 return;
826 }
827
828 if (destination.IsRegister()) {
829 if (source.IsRegister()) {
830 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
831 } else if (source.IsFpuRegister()) {
832 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
833 } else {
834 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
835 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
836 }
837 } else if (destination.IsFpuRegister()) {
838 if (source.IsRegister()) {
839 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
840 } else if (source.IsFpuRegister()) {
841 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
842 } else {
843 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
844 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
845 }
846 } else {
847 DCHECK(destination.IsStackSlot()) << destination;
848 if (source.IsRegister()) {
849 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
850 } else if (source.IsFpuRegister()) {
851 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
852 } else {
853 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
854 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
855 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
856 }
857 }
858}
859
860void CodeGeneratorMIPS::Move64(Location destination, Location source) {
861 if (source.Equals(destination)) {
862 return;
863 }
864
865 if (destination.IsRegisterPair()) {
866 if (source.IsRegisterPair()) {
867 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
868 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
869 } else if (source.IsFpuRegister()) {
870 Register dst_high = destination.AsRegisterPairHigh<Register>();
871 Register dst_low = destination.AsRegisterPairLow<Register>();
872 FRegister src = source.AsFpuRegister<FRegister>();
873 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800874 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200875 } else {
876 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
877 int32_t off = source.GetStackIndex();
878 Register r = destination.AsRegisterPairLow<Register>();
879 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
880 }
881 } else if (destination.IsFpuRegister()) {
882 if (source.IsRegisterPair()) {
883 FRegister dst = destination.AsFpuRegister<FRegister>();
884 Register src_high = source.AsRegisterPairHigh<Register>();
885 Register src_low = source.AsRegisterPairLow<Register>();
886 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800887 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200888 } else if (source.IsFpuRegister()) {
889 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
890 } else {
891 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
892 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
893 }
894 } else {
895 DCHECK(destination.IsDoubleStackSlot()) << destination;
896 int32_t off = destination.GetStackIndex();
897 if (source.IsRegisterPair()) {
898 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
899 } else if (source.IsFpuRegister()) {
900 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
901 } else {
902 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
903 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
904 __ StoreToOffset(kStoreWord, TMP, SP, off);
905 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
906 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
907 }
908 }
909}
910
911void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
912 if (c->IsIntConstant() || c->IsNullConstant()) {
913 // Move 32 bit constant.
914 int32_t value = GetInt32ValueOf(c);
915 if (destination.IsRegister()) {
916 Register dst = destination.AsRegister<Register>();
917 __ LoadConst32(dst, value);
918 } else {
919 DCHECK(destination.IsStackSlot())
920 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700921 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200922 }
923 } else if (c->IsLongConstant()) {
924 // Move 64 bit constant.
925 int64_t value = GetInt64ValueOf(c);
926 if (destination.IsRegisterPair()) {
927 Register r_h = destination.AsRegisterPairHigh<Register>();
928 Register r_l = destination.AsRegisterPairLow<Register>();
929 __ LoadConst64(r_h, r_l, value);
930 } else {
931 DCHECK(destination.IsDoubleStackSlot())
932 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700933 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200934 }
935 } else if (c->IsFloatConstant()) {
936 // Move 32 bit float constant.
937 int32_t value = GetInt32ValueOf(c);
938 if (destination.IsFpuRegister()) {
939 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
940 } else {
941 DCHECK(destination.IsStackSlot())
942 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700943 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200944 }
945 } else {
946 // Move 64 bit double constant.
947 DCHECK(c->IsDoubleConstant()) << c->DebugName();
948 int64_t value = GetInt64ValueOf(c);
949 if (destination.IsFpuRegister()) {
950 FRegister fd = destination.AsFpuRegister<FRegister>();
951 __ LoadDConst64(fd, value, TMP);
952 } else {
953 DCHECK(destination.IsDoubleStackSlot())
954 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700955 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200956 }
957 }
958}
959
960void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
961 DCHECK(destination.IsRegister());
962 Register dst = destination.AsRegister<Register>();
963 __ LoadConst32(dst, value);
964}
965
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200966void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
967 if (location.IsRegister()) {
968 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700969 } else if (location.IsRegisterPair()) {
970 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
971 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200972 } else {
973 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
974 }
975}
976
Vladimir Markoaad75c62016-10-03 08:46:48 +0000977template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
978inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
979 const ArenaDeque<PcRelativePatchInfo>& infos,
980 ArenaVector<LinkerPatch>* linker_patches) {
981 for (const PcRelativePatchInfo& info : infos) {
982 const DexFile& dex_file = info.target_dex_file;
983 size_t offset_or_index = info.offset_or_index;
984 DCHECK(info.high_label.IsBound());
985 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
986 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
987 // the assembler's base label used for PC-relative addressing.
988 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
989 ? __ GetLabelLocation(&info.pc_rel_label)
990 : __ GetPcRelBaseLabelLocation();
991 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
992 }
993}
994
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700995void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
996 DCHECK(linker_patches->empty());
997 size_t size =
998 method_patches_.size() +
999 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001000 pc_relative_dex_cache_patches_.size() +
1001 pc_relative_string_patches_.size() +
1002 pc_relative_type_patches_.size() +
1003 boot_image_string_patches_.size() +
1004 boot_image_type_patches_.size() +
1005 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001006 linker_patches->reserve(size);
1007 for (const auto& entry : method_patches_) {
1008 const MethodReference& target_method = entry.first;
1009 Literal* literal = entry.second;
1010 DCHECK(literal->GetLabel()->IsBound());
1011 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1012 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1013 target_method.dex_file,
1014 target_method.dex_method_index));
1015 }
1016 for (const auto& entry : call_patches_) {
1017 const MethodReference& target_method = entry.first;
1018 Literal* literal = entry.second;
1019 DCHECK(literal->GetLabel()->IsBound());
1020 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1021 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1022 target_method.dex_file,
1023 target_method.dex_method_index));
1024 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001025 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1026 linker_patches);
1027 if (!GetCompilerOptions().IsBootImage()) {
1028 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1029 linker_patches);
1030 } else {
1031 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1032 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001033 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001034 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1035 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001036 for (const auto& entry : boot_image_string_patches_) {
1037 const StringReference& target_string = entry.first;
1038 Literal* literal = entry.second;
1039 DCHECK(literal->GetLabel()->IsBound());
1040 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1041 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1042 target_string.dex_file,
1043 target_string.string_index));
1044 }
1045 for (const auto& entry : boot_image_type_patches_) {
1046 const TypeReference& target_type = entry.first;
1047 Literal* literal = entry.second;
1048 DCHECK(literal->GetLabel()->IsBound());
1049 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1050 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1051 target_type.dex_file,
1052 target_type.type_index));
1053 }
1054 for (const auto& entry : boot_image_address_patches_) {
1055 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1056 Literal* literal = entry.second;
1057 DCHECK(literal->GetLabel()->IsBound());
1058 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1059 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1060 }
1061}
1062
1063CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1064 const DexFile& dex_file, uint32_t string_index) {
1065 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1066}
1067
1068CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1069 const DexFile& dex_file, uint32_t type_index) {
1070 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001071}
1072
1073CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1074 const DexFile& dex_file, uint32_t element_offset) {
1075 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1076}
1077
1078CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1079 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1080 patches->emplace_back(dex_file, offset_or_index);
1081 return &patches->back();
1082}
1083
Alexey Frunze06a46c42016-07-19 15:00:40 -07001084Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1085 return map->GetOrCreate(
1086 value,
1087 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1088}
1089
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001090Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1091 MethodToLiteralMap* map) {
1092 return map->GetOrCreate(
1093 target_method,
1094 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1095}
1096
1097Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1098 return DeduplicateMethodLiteral(target_method, &method_patches_);
1099}
1100
1101Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1102 return DeduplicateMethodLiteral(target_method, &call_patches_);
1103}
1104
Alexey Frunze06a46c42016-07-19 15:00:40 -07001105Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1106 uint32_t string_index) {
1107 return boot_image_string_patches_.GetOrCreate(
1108 StringReference(&dex_file, string_index),
1109 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1110}
1111
1112Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1113 uint32_t type_index) {
1114 return boot_image_type_patches_.GetOrCreate(
1115 TypeReference(&dex_file, type_index),
1116 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1117}
1118
1119Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1120 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1121 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1122 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1123}
1124
Vladimir Markoaad75c62016-10-03 08:46:48 +00001125void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1126 PcRelativePatchInfo* info, Register out, Register base) {
1127 bool reordering = __ SetReorder(false);
1128 if (GetInstructionSetFeatures().IsR6()) {
1129 DCHECK_EQ(base, ZERO);
1130 __ Bind(&info->high_label);
1131 __ Bind(&info->pc_rel_label);
1132 // Add a 32-bit offset to PC.
1133 __ Auipc(out, /* placeholder */ 0x1234);
1134 __ Addiu(out, out, /* placeholder */ 0x5678);
1135 } else {
1136 // If base is ZERO, emit NAL to obtain the actual base.
1137 if (base == ZERO) {
1138 // Generate a dummy PC-relative call to obtain PC.
1139 __ Nal();
1140 }
1141 __ Bind(&info->high_label);
1142 __ Lui(out, /* placeholder */ 0x1234);
1143 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1144 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1145 if (base == ZERO) {
1146 __ Bind(&info->pc_rel_label);
1147 }
1148 __ Ori(out, out, /* placeholder */ 0x5678);
1149 // Add a 32-bit offset to PC.
1150 __ Addu(out, out, (base == ZERO) ? RA : base);
1151 }
1152 __ SetReorder(reordering);
1153}
1154
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001155void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1156 MipsLabel done;
1157 Register card = AT;
1158 Register temp = TMP;
1159 __ Beqz(value, &done);
1160 __ LoadFromOffset(kLoadWord,
1161 card,
1162 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001163 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001164 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1165 __ Addu(temp, card, temp);
1166 __ Sb(card, temp, 0);
1167 __ Bind(&done);
1168}
1169
David Brazdil58282f42016-01-14 12:45:10 +00001170void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001171 // 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 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001205}
1206
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001207size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1208 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1209 return kMipsWordSize;
1210}
1211
1212size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1213 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1214 return kMipsWordSize;
1215}
1216
1217size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1218 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1219 return kMipsDoublewordSize;
1220}
1221
1222size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1223 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1224 return kMipsDoublewordSize;
1225}
1226
1227void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001228 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001229}
1230
1231void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001232 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001233}
1234
Serban Constantinescufca16662016-07-14 09:21:59 +01001235constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1236
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001237void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1238 HInstruction* instruction,
1239 uint32_t dex_pc,
1240 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001241 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001242 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001243 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001244 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001245 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001246 // Reserve argument space on stack (for $a0-$a3) for
1247 // entrypoints that directly reference native implementations.
1248 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001249 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001250 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001251 } else {
1252 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001253 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001254 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001255 if (EntrypointRequiresStackMap(entrypoint)) {
1256 RecordPcInfo(instruction, dex_pc, slow_path);
1257 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001258}
1259
1260void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1261 Register class_reg) {
1262 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1263 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1264 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1265 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1266 __ Sync(0);
1267 __ Bind(slow_path->GetExitLabel());
1268}
1269
1270void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1271 __ Sync(0); // Only stype 0 is supported.
1272}
1273
1274void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1275 HBasicBlock* successor) {
1276 SuspendCheckSlowPathMIPS* slow_path =
1277 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1278 codegen_->AddSlowPath(slow_path);
1279
1280 __ LoadFromOffset(kLoadUnsignedHalfword,
1281 TMP,
1282 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001283 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001284 if (successor == nullptr) {
1285 __ Bnez(TMP, slow_path->GetEntryLabel());
1286 __ Bind(slow_path->GetReturnLabel());
1287 } else {
1288 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1289 __ B(slow_path->GetEntryLabel());
1290 // slow_path will return to GetLabelOf(successor).
1291 }
1292}
1293
1294InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1295 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001296 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 assembler_(codegen->GetAssembler()),
1298 codegen_(codegen) {}
1299
1300void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1301 DCHECK_EQ(instruction->InputCount(), 2U);
1302 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1303 Primitive::Type type = instruction->GetResultType();
1304 switch (type) {
1305 case Primitive::kPrimInt: {
1306 locations->SetInAt(0, Location::RequiresRegister());
1307 HInstruction* right = instruction->InputAt(1);
1308 bool can_use_imm = false;
1309 if (right->IsConstant()) {
1310 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1311 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1312 can_use_imm = IsUint<16>(imm);
1313 } else if (instruction->IsAdd()) {
1314 can_use_imm = IsInt<16>(imm);
1315 } else {
1316 DCHECK(instruction->IsSub());
1317 can_use_imm = IsInt<16>(-imm);
1318 }
1319 }
1320 if (can_use_imm)
1321 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1322 else
1323 locations->SetInAt(1, Location::RequiresRegister());
1324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1325 break;
1326 }
1327
1328 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001329 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001330 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001332 break;
1333 }
1334
1335 case Primitive::kPrimFloat:
1336 case Primitive::kPrimDouble:
1337 DCHECK(instruction->IsAdd() || instruction->IsSub());
1338 locations->SetInAt(0, Location::RequiresFpuRegister());
1339 locations->SetInAt(1, Location::RequiresFpuRegister());
1340 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1341 break;
1342
1343 default:
1344 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1345 }
1346}
1347
1348void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1349 Primitive::Type type = instruction->GetType();
1350 LocationSummary* locations = instruction->GetLocations();
1351
1352 switch (type) {
1353 case Primitive::kPrimInt: {
1354 Register dst = locations->Out().AsRegister<Register>();
1355 Register lhs = locations->InAt(0).AsRegister<Register>();
1356 Location rhs_location = locations->InAt(1);
1357
1358 Register rhs_reg = ZERO;
1359 int32_t rhs_imm = 0;
1360 bool use_imm = rhs_location.IsConstant();
1361 if (use_imm) {
1362 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1363 } else {
1364 rhs_reg = rhs_location.AsRegister<Register>();
1365 }
1366
1367 if (instruction->IsAnd()) {
1368 if (use_imm)
1369 __ Andi(dst, lhs, rhs_imm);
1370 else
1371 __ And(dst, lhs, rhs_reg);
1372 } else if (instruction->IsOr()) {
1373 if (use_imm)
1374 __ Ori(dst, lhs, rhs_imm);
1375 else
1376 __ Or(dst, lhs, rhs_reg);
1377 } else if (instruction->IsXor()) {
1378 if (use_imm)
1379 __ Xori(dst, lhs, rhs_imm);
1380 else
1381 __ Xor(dst, lhs, rhs_reg);
1382 } else if (instruction->IsAdd()) {
1383 if (use_imm)
1384 __ Addiu(dst, lhs, rhs_imm);
1385 else
1386 __ Addu(dst, lhs, rhs_reg);
1387 } else {
1388 DCHECK(instruction->IsSub());
1389 if (use_imm)
1390 __ Addiu(dst, lhs, -rhs_imm);
1391 else
1392 __ Subu(dst, lhs, rhs_reg);
1393 }
1394 break;
1395 }
1396
1397 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001398 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1399 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1400 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1401 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001402 Location rhs_location = locations->InAt(1);
1403 bool use_imm = rhs_location.IsConstant();
1404 if (!use_imm) {
1405 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1406 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1407 if (instruction->IsAnd()) {
1408 __ And(dst_low, lhs_low, rhs_low);
1409 __ And(dst_high, lhs_high, rhs_high);
1410 } else if (instruction->IsOr()) {
1411 __ Or(dst_low, lhs_low, rhs_low);
1412 __ Or(dst_high, lhs_high, rhs_high);
1413 } else if (instruction->IsXor()) {
1414 __ Xor(dst_low, lhs_low, rhs_low);
1415 __ Xor(dst_high, lhs_high, rhs_high);
1416 } else if (instruction->IsAdd()) {
1417 if (lhs_low == rhs_low) {
1418 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1419 __ Slt(TMP, lhs_low, ZERO);
1420 __ Addu(dst_low, lhs_low, rhs_low);
1421 } else {
1422 __ Addu(dst_low, lhs_low, rhs_low);
1423 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1424 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1425 }
1426 __ Addu(dst_high, lhs_high, rhs_high);
1427 __ Addu(dst_high, dst_high, TMP);
1428 } else {
1429 DCHECK(instruction->IsSub());
1430 __ Sltu(TMP, lhs_low, rhs_low);
1431 __ Subu(dst_low, lhs_low, rhs_low);
1432 __ Subu(dst_high, lhs_high, rhs_high);
1433 __ Subu(dst_high, dst_high, TMP);
1434 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001435 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001436 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1437 if (instruction->IsOr()) {
1438 uint32_t low = Low32Bits(value);
1439 uint32_t high = High32Bits(value);
1440 if (IsUint<16>(low)) {
1441 if (dst_low != lhs_low || low != 0) {
1442 __ Ori(dst_low, lhs_low, low);
1443 }
1444 } else {
1445 __ LoadConst32(TMP, low);
1446 __ Or(dst_low, lhs_low, TMP);
1447 }
1448 if (IsUint<16>(high)) {
1449 if (dst_high != lhs_high || high != 0) {
1450 __ Ori(dst_high, lhs_high, high);
1451 }
1452 } else {
1453 if (high != low) {
1454 __ LoadConst32(TMP, high);
1455 }
1456 __ Or(dst_high, lhs_high, TMP);
1457 }
1458 } else if (instruction->IsXor()) {
1459 uint32_t low = Low32Bits(value);
1460 uint32_t high = High32Bits(value);
1461 if (IsUint<16>(low)) {
1462 if (dst_low != lhs_low || low != 0) {
1463 __ Xori(dst_low, lhs_low, low);
1464 }
1465 } else {
1466 __ LoadConst32(TMP, low);
1467 __ Xor(dst_low, lhs_low, TMP);
1468 }
1469 if (IsUint<16>(high)) {
1470 if (dst_high != lhs_high || high != 0) {
1471 __ Xori(dst_high, lhs_high, high);
1472 }
1473 } else {
1474 if (high != low) {
1475 __ LoadConst32(TMP, high);
1476 }
1477 __ Xor(dst_high, lhs_high, TMP);
1478 }
1479 } else if (instruction->IsAnd()) {
1480 uint32_t low = Low32Bits(value);
1481 uint32_t high = High32Bits(value);
1482 if (IsUint<16>(low)) {
1483 __ Andi(dst_low, lhs_low, low);
1484 } else if (low != 0xFFFFFFFF) {
1485 __ LoadConst32(TMP, low);
1486 __ And(dst_low, lhs_low, TMP);
1487 } else if (dst_low != lhs_low) {
1488 __ Move(dst_low, lhs_low);
1489 }
1490 if (IsUint<16>(high)) {
1491 __ Andi(dst_high, lhs_high, high);
1492 } else if (high != 0xFFFFFFFF) {
1493 if (high != low) {
1494 __ LoadConst32(TMP, high);
1495 }
1496 __ And(dst_high, lhs_high, TMP);
1497 } else if (dst_high != lhs_high) {
1498 __ Move(dst_high, lhs_high);
1499 }
1500 } else {
1501 if (instruction->IsSub()) {
1502 value = -value;
1503 } else {
1504 DCHECK(instruction->IsAdd());
1505 }
1506 int32_t low = Low32Bits(value);
1507 int32_t high = High32Bits(value);
1508 if (IsInt<16>(low)) {
1509 if (dst_low != lhs_low || low != 0) {
1510 __ Addiu(dst_low, lhs_low, low);
1511 }
1512 if (low != 0) {
1513 __ Sltiu(AT, dst_low, low);
1514 }
1515 } else {
1516 __ LoadConst32(TMP, low);
1517 __ Addu(dst_low, lhs_low, TMP);
1518 __ Sltu(AT, dst_low, TMP);
1519 }
1520 if (IsInt<16>(high)) {
1521 if (dst_high != lhs_high || high != 0) {
1522 __ Addiu(dst_high, lhs_high, high);
1523 }
1524 } else {
1525 if (high != low) {
1526 __ LoadConst32(TMP, high);
1527 }
1528 __ Addu(dst_high, lhs_high, TMP);
1529 }
1530 if (low != 0) {
1531 __ Addu(dst_high, dst_high, AT);
1532 }
1533 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001534 }
1535 break;
1536 }
1537
1538 case Primitive::kPrimFloat:
1539 case Primitive::kPrimDouble: {
1540 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1541 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1542 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1543 if (instruction->IsAdd()) {
1544 if (type == Primitive::kPrimFloat) {
1545 __ AddS(dst, lhs, rhs);
1546 } else {
1547 __ AddD(dst, lhs, rhs);
1548 }
1549 } else {
1550 DCHECK(instruction->IsSub());
1551 if (type == Primitive::kPrimFloat) {
1552 __ SubS(dst, lhs, rhs);
1553 } else {
1554 __ SubD(dst, lhs, rhs);
1555 }
1556 }
1557 break;
1558 }
1559
1560 default:
1561 LOG(FATAL) << "Unexpected binary operation type " << type;
1562 }
1563}
1564
1565void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001566 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001567
1568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1569 Primitive::Type type = instr->GetResultType();
1570 switch (type) {
1571 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001572 locations->SetInAt(0, Location::RequiresRegister());
1573 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1574 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1575 break;
1576 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001577 locations->SetInAt(0, Location::RequiresRegister());
1578 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1579 locations->SetOut(Location::RequiresRegister());
1580 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001581 default:
1582 LOG(FATAL) << "Unexpected shift type " << type;
1583 }
1584}
1585
1586static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1587
1588void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001589 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001590 LocationSummary* locations = instr->GetLocations();
1591 Primitive::Type type = instr->GetType();
1592
1593 Location rhs_location = locations->InAt(1);
1594 bool use_imm = rhs_location.IsConstant();
1595 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1596 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001597 const uint32_t shift_mask =
1598 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001599 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001600 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1601 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001602
1603 switch (type) {
1604 case Primitive::kPrimInt: {
1605 Register dst = locations->Out().AsRegister<Register>();
1606 Register lhs = locations->InAt(0).AsRegister<Register>();
1607 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001608 if (shift_value == 0) {
1609 if (dst != lhs) {
1610 __ Move(dst, lhs);
1611 }
1612 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001613 __ Sll(dst, lhs, shift_value);
1614 } else if (instr->IsShr()) {
1615 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001616 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001617 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001618 } else {
1619 if (has_ins_rotr) {
1620 __ Rotr(dst, lhs, shift_value);
1621 } else {
1622 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1623 __ Srl(dst, lhs, shift_value);
1624 __ Or(dst, dst, TMP);
1625 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001626 }
1627 } else {
1628 if (instr->IsShl()) {
1629 __ Sllv(dst, lhs, rhs_reg);
1630 } else if (instr->IsShr()) {
1631 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001632 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001633 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001634 } else {
1635 if (has_ins_rotr) {
1636 __ Rotrv(dst, lhs, rhs_reg);
1637 } else {
1638 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001639 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1640 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1641 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1642 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1643 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001644 __ Sllv(TMP, lhs, TMP);
1645 __ Srlv(dst, lhs, rhs_reg);
1646 __ Or(dst, dst, TMP);
1647 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001648 }
1649 }
1650 break;
1651 }
1652
1653 case Primitive::kPrimLong: {
1654 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1655 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1656 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1657 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1658 if (use_imm) {
1659 if (shift_value == 0) {
1660 codegen_->Move64(locations->Out(), locations->InAt(0));
1661 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001662 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001663 if (instr->IsShl()) {
1664 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1665 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1666 __ Sll(dst_low, lhs_low, shift_value);
1667 } else if (instr->IsShr()) {
1668 __ Srl(dst_low, lhs_low, shift_value);
1669 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1670 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001671 } else if (instr->IsUShr()) {
1672 __ Srl(dst_low, lhs_low, shift_value);
1673 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1674 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001675 } else {
1676 __ Srl(dst_low, lhs_low, shift_value);
1677 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1678 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001679 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001680 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001681 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001682 if (instr->IsShl()) {
1683 __ Sll(dst_low, lhs_low, shift_value);
1684 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1685 __ Sll(dst_high, lhs_high, shift_value);
1686 __ Or(dst_high, dst_high, TMP);
1687 } else if (instr->IsShr()) {
1688 __ Sra(dst_high, lhs_high, shift_value);
1689 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1690 __ Srl(dst_low, lhs_low, shift_value);
1691 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001692 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001693 __ Srl(dst_high, lhs_high, shift_value);
1694 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1695 __ Srl(dst_low, lhs_low, shift_value);
1696 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001697 } else {
1698 __ Srl(TMP, lhs_low, shift_value);
1699 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1700 __ Or(dst_low, dst_low, TMP);
1701 __ Srl(TMP, lhs_high, shift_value);
1702 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1703 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001704 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001705 }
1706 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001707 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001708 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001709 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001710 __ Move(dst_low, ZERO);
1711 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001712 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001713 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001714 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001715 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001716 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001717 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001718 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001719 // 64-bit rotation by 32 is just a swap.
1720 __ Move(dst_low, lhs_high);
1721 __ Move(dst_high, lhs_low);
1722 } else {
1723 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001724 __ Srl(dst_low, lhs_high, shift_value_high);
1725 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1726 __ Srl(dst_high, lhs_low, shift_value_high);
1727 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001728 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001729 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1730 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001731 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001732 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1733 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001734 __ Or(dst_high, dst_high, TMP);
1735 }
1736 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001737 }
1738 }
1739 } else {
1740 MipsLabel done;
1741 if (instr->IsShl()) {
1742 __ Sllv(dst_low, lhs_low, rhs_reg);
1743 __ Nor(AT, ZERO, rhs_reg);
1744 __ Srl(TMP, lhs_low, 1);
1745 __ Srlv(TMP, TMP, AT);
1746 __ Sllv(dst_high, lhs_high, rhs_reg);
1747 __ Or(dst_high, dst_high, TMP);
1748 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1749 __ Beqz(TMP, &done);
1750 __ Move(dst_high, dst_low);
1751 __ Move(dst_low, ZERO);
1752 } else if (instr->IsShr()) {
1753 __ Srav(dst_high, lhs_high, rhs_reg);
1754 __ Nor(AT, ZERO, rhs_reg);
1755 __ Sll(TMP, lhs_high, 1);
1756 __ Sllv(TMP, TMP, AT);
1757 __ Srlv(dst_low, lhs_low, rhs_reg);
1758 __ Or(dst_low, dst_low, TMP);
1759 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1760 __ Beqz(TMP, &done);
1761 __ Move(dst_low, dst_high);
1762 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001763 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001764 __ Srlv(dst_high, lhs_high, rhs_reg);
1765 __ Nor(AT, ZERO, rhs_reg);
1766 __ Sll(TMP, lhs_high, 1);
1767 __ Sllv(TMP, TMP, AT);
1768 __ Srlv(dst_low, lhs_low, rhs_reg);
1769 __ Or(dst_low, dst_low, TMP);
1770 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1771 __ Beqz(TMP, &done);
1772 __ Move(dst_low, dst_high);
1773 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001774 } else {
1775 __ Nor(AT, ZERO, rhs_reg);
1776 __ Srlv(TMP, lhs_low, rhs_reg);
1777 __ Sll(dst_low, lhs_high, 1);
1778 __ Sllv(dst_low, dst_low, AT);
1779 __ Or(dst_low, dst_low, TMP);
1780 __ Srlv(TMP, lhs_high, rhs_reg);
1781 __ Sll(dst_high, lhs_low, 1);
1782 __ Sllv(dst_high, dst_high, AT);
1783 __ Or(dst_high, dst_high, TMP);
1784 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1785 __ Beqz(TMP, &done);
1786 __ Move(TMP, dst_high);
1787 __ Move(dst_high, dst_low);
1788 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001789 }
1790 __ Bind(&done);
1791 }
1792 break;
1793 }
1794
1795 default:
1796 LOG(FATAL) << "Unexpected shift operation type " << type;
1797 }
1798}
1799
1800void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1801 HandleBinaryOp(instruction);
1802}
1803
1804void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1805 HandleBinaryOp(instruction);
1806}
1807
1808void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1809 HandleBinaryOp(instruction);
1810}
1811
1812void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1813 HandleBinaryOp(instruction);
1814}
1815
1816void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1817 LocationSummary* locations =
1818 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1819 locations->SetInAt(0, Location::RequiresRegister());
1820 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1821 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1822 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1823 } else {
1824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1825 }
1826}
1827
Alexey Frunze2923db72016-08-20 01:55:47 -07001828auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1829 auto null_checker = [this, instruction]() {
1830 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1831 };
1832 return null_checker;
1833}
1834
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001835void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1836 LocationSummary* locations = instruction->GetLocations();
1837 Register obj = locations->InAt(0).AsRegister<Register>();
1838 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001839 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001840 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001841
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001842 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001843 switch (type) {
1844 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001845 Register out = locations->Out().AsRegister<Register>();
1846 if (index.IsConstant()) {
1847 size_t offset =
1848 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001849 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001850 } else {
1851 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001852 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001853 }
1854 break;
1855 }
1856
1857 case Primitive::kPrimByte: {
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(kLoadSignedByte, 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(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimShort: {
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_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001875 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 } else {
1877 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1878 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001879 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880 }
1881 break;
1882 }
1883
1884 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001885 Register out = locations->Out().AsRegister<Register>();
1886 if (index.IsConstant()) {
1887 size_t offset =
1888 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001889 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001890 } else {
1891 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1892 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001893 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001894 }
1895 break;
1896 }
1897
1898 case Primitive::kPrimInt:
1899 case Primitive::kPrimNot: {
1900 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901 Register out = locations->Out().AsRegister<Register>();
1902 if (index.IsConstant()) {
1903 size_t offset =
1904 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001905 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906 } else {
1907 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1908 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001909 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910 }
1911 break;
1912 }
1913
1914 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 Register out = locations->Out().AsRegisterPairLow<Register>();
1916 if (index.IsConstant()) {
1917 size_t offset =
1918 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001919 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001920 } else {
1921 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1922 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001923 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001924 }
1925 break;
1926 }
1927
1928 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1930 if (index.IsConstant()) {
1931 size_t offset =
1932 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001933 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 } else {
1935 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1936 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001937 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001938 }
1939 break;
1940 }
1941
1942 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001943 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1944 if (index.IsConstant()) {
1945 size_t offset =
1946 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001947 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001948 } else {
1949 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1950 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001951 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 }
1953 break;
1954 }
1955
1956 case Primitive::kPrimVoid:
1957 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1958 UNREACHABLE();
1959 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001960}
1961
1962void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1963 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1964 locations->SetInAt(0, Location::RequiresRegister());
1965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1966}
1967
1968void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1969 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001970 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001971 Register obj = locations->InAt(0).AsRegister<Register>();
1972 Register out = locations->Out().AsRegister<Register>();
1973 __ LoadFromOffset(kLoadWord, out, obj, offset);
1974 codegen_->MaybeRecordImplicitNullCheck(instruction);
1975}
1976
Alexey Frunzef58b2482016-09-02 22:14:06 -07001977Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1978 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1979 ? Location::ConstantLocation(instruction->AsConstant())
1980 : Location::RequiresRegister();
1981}
1982
1983Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1984 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1985 // We can store a non-zero float or double constant without first loading it into the FPU,
1986 // but we should only prefer this if the constant has a single use.
1987 if (instruction->IsConstant() &&
1988 (instruction->AsConstant()->IsZeroBitPattern() ||
1989 instruction->GetUses().HasExactlyOneElement())) {
1990 return Location::ConstantLocation(instruction->AsConstant());
1991 // Otherwise fall through and require an FPU register for the constant.
1992 }
1993 return Location::RequiresFpuRegister();
1994}
1995
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001996void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001997 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001998 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1999 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002000 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002001 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002002 InvokeRuntimeCallingConvention calling_convention;
2003 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2004 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2005 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2006 } else {
2007 locations->SetInAt(0, Location::RequiresRegister());
2008 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2009 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002010 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002011 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002012 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002013 }
2014 }
2015}
2016
2017void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2018 LocationSummary* locations = instruction->GetLocations();
2019 Register obj = locations->InAt(0).AsRegister<Register>();
2020 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002021 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002022 Primitive::Type value_type = instruction->GetComponentType();
2023 bool needs_runtime_call = locations->WillCall();
2024 bool needs_write_barrier =
2025 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002026 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002027 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002028
2029 switch (value_type) {
2030 case Primitive::kPrimBoolean:
2031 case Primitive::kPrimByte: {
2032 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002033 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002034 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002036 __ Addu(base_reg, obj, index.AsRegister<Register>());
2037 }
2038 if (value_location.IsConstant()) {
2039 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2040 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2041 } else {
2042 Register value = value_location.AsRegister<Register>();
2043 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044 }
2045 break;
2046 }
2047
2048 case Primitive::kPrimShort:
2049 case Primitive::kPrimChar: {
2050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002051 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002052 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002053 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002054 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2055 __ Addu(base_reg, obj, base_reg);
2056 }
2057 if (value_location.IsConstant()) {
2058 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2059 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2060 } else {
2061 Register value = value_location.AsRegister<Register>();
2062 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 }
2064 break;
2065 }
2066
2067 case Primitive::kPrimInt:
2068 case Primitive::kPrimNot: {
2069 if (!needs_runtime_call) {
2070 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002071 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002072 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002073 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002074 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2075 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002077 if (value_location.IsConstant()) {
2078 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2079 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2080 DCHECK(!needs_write_barrier);
2081 } else {
2082 Register value = value_location.AsRegister<Register>();
2083 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2084 if (needs_write_barrier) {
2085 DCHECK_EQ(value_type, Primitive::kPrimNot);
2086 codegen_->MarkGCCard(obj, value);
2087 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002088 }
2089 } else {
2090 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002091 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002092 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2093 }
2094 break;
2095 }
2096
2097 case Primitive::kPrimLong: {
2098 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002099 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002100 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002101 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002102 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2103 __ Addu(base_reg, obj, base_reg);
2104 }
2105 if (value_location.IsConstant()) {
2106 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2107 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2108 } else {
2109 Register value = value_location.AsRegisterPairLow<Register>();
2110 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002111 }
2112 break;
2113 }
2114
2115 case Primitive::kPrimFloat: {
2116 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002117 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002118 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002119 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002120 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2121 __ Addu(base_reg, obj, base_reg);
2122 }
2123 if (value_location.IsConstant()) {
2124 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2125 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2126 } else {
2127 FRegister value = value_location.AsFpuRegister<FRegister>();
2128 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002129 }
2130 break;
2131 }
2132
2133 case Primitive::kPrimDouble: {
2134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002135 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002136 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002137 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002138 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2139 __ Addu(base_reg, obj, base_reg);
2140 }
2141 if (value_location.IsConstant()) {
2142 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2143 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2144 } else {
2145 FRegister value = value_location.AsFpuRegister<FRegister>();
2146 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002147 }
2148 break;
2149 }
2150
2151 case Primitive::kPrimVoid:
2152 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2153 UNREACHABLE();
2154 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002155}
2156
2157void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002158 RegisterSet caller_saves = RegisterSet::Empty();
2159 InvokeRuntimeCallingConvention calling_convention;
2160 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2161 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2162 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002163 locations->SetInAt(0, Location::RequiresRegister());
2164 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002165}
2166
2167void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2168 LocationSummary* locations = instruction->GetLocations();
2169 BoundsCheckSlowPathMIPS* slow_path =
2170 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2171 codegen_->AddSlowPath(slow_path);
2172
2173 Register index = locations->InAt(0).AsRegister<Register>();
2174 Register length = locations->InAt(1).AsRegister<Register>();
2175
2176 // length is limited by the maximum positive signed 32-bit integer.
2177 // Unsigned comparison of length and index checks for index < 0
2178 // and for length <= index simultaneously.
2179 __ Bgeu(index, length, slow_path->GetEntryLabel());
2180}
2181
2182void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2183 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2184 instruction,
2185 LocationSummary::kCallOnSlowPath);
2186 locations->SetInAt(0, Location::RequiresRegister());
2187 locations->SetInAt(1, Location::RequiresRegister());
2188 // Note that TypeCheckSlowPathMIPS uses this register too.
2189 locations->AddTemp(Location::RequiresRegister());
2190}
2191
2192void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2193 LocationSummary* locations = instruction->GetLocations();
2194 Register obj = locations->InAt(0).AsRegister<Register>();
2195 Register cls = locations->InAt(1).AsRegister<Register>();
2196 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2197
2198 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2199 codegen_->AddSlowPath(slow_path);
2200
2201 // TODO: avoid this check if we know obj is not null.
2202 __ Beqz(obj, slow_path->GetExitLabel());
2203 // Compare the class of `obj` with `cls`.
2204 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2205 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2206 __ Bind(slow_path->GetExitLabel());
2207}
2208
2209void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2210 LocationSummary* locations =
2211 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2212 locations->SetInAt(0, Location::RequiresRegister());
2213 if (check->HasUses()) {
2214 locations->SetOut(Location::SameAsFirstInput());
2215 }
2216}
2217
2218void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2219 // We assume the class is not null.
2220 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2221 check->GetLoadClass(),
2222 check,
2223 check->GetDexPc(),
2224 true);
2225 codegen_->AddSlowPath(slow_path);
2226 GenerateClassInitializationCheck(slow_path,
2227 check->GetLocations()->InAt(0).AsRegister<Register>());
2228}
2229
2230void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2231 Primitive::Type in_type = compare->InputAt(0)->GetType();
2232
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002233 LocationSummary* locations =
2234 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002235
2236 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002237 case Primitive::kPrimBoolean:
2238 case Primitive::kPrimByte:
2239 case Primitive::kPrimShort:
2240 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002241 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002242 locations->SetInAt(0, Location::RequiresRegister());
2243 locations->SetInAt(1, Location::RequiresRegister());
2244 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2245 break;
2246
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002247 case Primitive::kPrimLong:
2248 locations->SetInAt(0, Location::RequiresRegister());
2249 locations->SetInAt(1, Location::RequiresRegister());
2250 // Output overlaps because it is written before doing the low comparison.
2251 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2252 break;
2253
2254 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002255 case Primitive::kPrimDouble:
2256 locations->SetInAt(0, Location::RequiresFpuRegister());
2257 locations->SetInAt(1, Location::RequiresFpuRegister());
2258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002259 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260
2261 default:
2262 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2263 }
2264}
2265
2266void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2267 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002268 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002269 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002270 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271
2272 // 0 if: left == right
2273 // 1 if: left > right
2274 // -1 if: left < right
2275 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002276 case Primitive::kPrimBoolean:
2277 case Primitive::kPrimByte:
2278 case Primitive::kPrimShort:
2279 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002280 case Primitive::kPrimInt: {
2281 Register lhs = locations->InAt(0).AsRegister<Register>();
2282 Register rhs = locations->InAt(1).AsRegister<Register>();
2283 __ Slt(TMP, lhs, rhs);
2284 __ Slt(res, rhs, lhs);
2285 __ Subu(res, res, TMP);
2286 break;
2287 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002288 case Primitive::kPrimLong: {
2289 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002290 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2291 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2292 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2293 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2294 // TODO: more efficient (direct) comparison with a constant.
2295 __ Slt(TMP, lhs_high, rhs_high);
2296 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2297 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2298 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2299 __ Sltu(TMP, lhs_low, rhs_low);
2300 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2301 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2302 __ Bind(&done);
2303 break;
2304 }
2305
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002306 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002307 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002308 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2309 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2310 MipsLabel done;
2311 if (isR6) {
2312 __ CmpEqS(FTMP, lhs, rhs);
2313 __ LoadConst32(res, 0);
2314 __ Bc1nez(FTMP, &done);
2315 if (gt_bias) {
2316 __ CmpLtS(FTMP, lhs, rhs);
2317 __ LoadConst32(res, -1);
2318 __ Bc1nez(FTMP, &done);
2319 __ LoadConst32(res, 1);
2320 } else {
2321 __ CmpLtS(FTMP, rhs, lhs);
2322 __ LoadConst32(res, 1);
2323 __ Bc1nez(FTMP, &done);
2324 __ LoadConst32(res, -1);
2325 }
2326 } else {
2327 if (gt_bias) {
2328 __ ColtS(0, lhs, rhs);
2329 __ LoadConst32(res, -1);
2330 __ Bc1t(0, &done);
2331 __ CeqS(0, lhs, rhs);
2332 __ LoadConst32(res, 1);
2333 __ Movt(res, ZERO, 0);
2334 } else {
2335 __ ColtS(0, rhs, lhs);
2336 __ LoadConst32(res, 1);
2337 __ Bc1t(0, &done);
2338 __ CeqS(0, lhs, rhs);
2339 __ LoadConst32(res, -1);
2340 __ Movt(res, ZERO, 0);
2341 }
2342 }
2343 __ Bind(&done);
2344 break;
2345 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002346 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002347 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002348 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2349 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2350 MipsLabel done;
2351 if (isR6) {
2352 __ CmpEqD(FTMP, lhs, rhs);
2353 __ LoadConst32(res, 0);
2354 __ Bc1nez(FTMP, &done);
2355 if (gt_bias) {
2356 __ CmpLtD(FTMP, lhs, rhs);
2357 __ LoadConst32(res, -1);
2358 __ Bc1nez(FTMP, &done);
2359 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002360 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002361 __ CmpLtD(FTMP, rhs, lhs);
2362 __ LoadConst32(res, 1);
2363 __ Bc1nez(FTMP, &done);
2364 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 }
2366 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002367 if (gt_bias) {
2368 __ ColtD(0, lhs, rhs);
2369 __ LoadConst32(res, -1);
2370 __ Bc1t(0, &done);
2371 __ CeqD(0, lhs, rhs);
2372 __ LoadConst32(res, 1);
2373 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002374 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002375 __ ColtD(0, rhs, lhs);
2376 __ LoadConst32(res, 1);
2377 __ Bc1t(0, &done);
2378 __ CeqD(0, lhs, rhs);
2379 __ LoadConst32(res, -1);
2380 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002381 }
2382 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002383 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002384 break;
2385 }
2386
2387 default:
2388 LOG(FATAL) << "Unimplemented compare type " << in_type;
2389 }
2390}
2391
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002392void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002393 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002394 switch (instruction->InputAt(0)->GetType()) {
2395 default:
2396 case Primitive::kPrimLong:
2397 locations->SetInAt(0, Location::RequiresRegister());
2398 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2399 break;
2400
2401 case Primitive::kPrimFloat:
2402 case Primitive::kPrimDouble:
2403 locations->SetInAt(0, Location::RequiresFpuRegister());
2404 locations->SetInAt(1, Location::RequiresFpuRegister());
2405 break;
2406 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002407 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2409 }
2410}
2411
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002412void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002413 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002414 return;
2415 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002417 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418 LocationSummary* locations = instruction->GetLocations();
2419 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002420 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002422 switch (type) {
2423 default:
2424 // Integer case.
2425 GenerateIntCompare(instruction->GetCondition(), locations);
2426 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002428 case Primitive::kPrimLong:
2429 // TODO: don't use branches.
2430 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002431 break;
2432
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002433 case Primitive::kPrimFloat:
2434 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002435 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2436 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002437 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002438
2439 // Convert the branches into the result.
2440 MipsLabel done;
2441
2442 // False case: result = 0.
2443 __ LoadConst32(dst, 0);
2444 __ B(&done);
2445
2446 // True case: result = 1.
2447 __ Bind(&true_label);
2448 __ LoadConst32(dst, 1);
2449 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002450}
2451
Alexey Frunze7e99e052015-11-24 19:28:01 -08002452void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2453 DCHECK(instruction->IsDiv() || instruction->IsRem());
2454 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2455
2456 LocationSummary* locations = instruction->GetLocations();
2457 Location second = locations->InAt(1);
2458 DCHECK(second.IsConstant());
2459
2460 Register out = locations->Out().AsRegister<Register>();
2461 Register dividend = locations->InAt(0).AsRegister<Register>();
2462 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2463 DCHECK(imm == 1 || imm == -1);
2464
2465 if (instruction->IsRem()) {
2466 __ Move(out, ZERO);
2467 } else {
2468 if (imm == -1) {
2469 __ Subu(out, ZERO, dividend);
2470 } else if (out != dividend) {
2471 __ Move(out, dividend);
2472 }
2473 }
2474}
2475
2476void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2477 DCHECK(instruction->IsDiv() || instruction->IsRem());
2478 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2479
2480 LocationSummary* locations = instruction->GetLocations();
2481 Location second = locations->InAt(1);
2482 DCHECK(second.IsConstant());
2483
2484 Register out = locations->Out().AsRegister<Register>();
2485 Register dividend = locations->InAt(0).AsRegister<Register>();
2486 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002487 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002488 int ctz_imm = CTZ(abs_imm);
2489
2490 if (instruction->IsDiv()) {
2491 if (ctz_imm == 1) {
2492 // Fast path for division by +/-2, which is very common.
2493 __ Srl(TMP, dividend, 31);
2494 } else {
2495 __ Sra(TMP, dividend, 31);
2496 __ Srl(TMP, TMP, 32 - ctz_imm);
2497 }
2498 __ Addu(out, dividend, TMP);
2499 __ Sra(out, out, ctz_imm);
2500 if (imm < 0) {
2501 __ Subu(out, ZERO, out);
2502 }
2503 } else {
2504 if (ctz_imm == 1) {
2505 // Fast path for modulo +/-2, which is very common.
2506 __ Sra(TMP, dividend, 31);
2507 __ Subu(out, dividend, TMP);
2508 __ Andi(out, out, 1);
2509 __ Addu(out, out, TMP);
2510 } else {
2511 __ Sra(TMP, dividend, 31);
2512 __ Srl(TMP, TMP, 32 - ctz_imm);
2513 __ Addu(out, dividend, TMP);
2514 if (IsUint<16>(abs_imm - 1)) {
2515 __ Andi(out, out, abs_imm - 1);
2516 } else {
2517 __ Sll(out, out, 32 - ctz_imm);
2518 __ Srl(out, out, 32 - ctz_imm);
2519 }
2520 __ Subu(out, out, TMP);
2521 }
2522 }
2523}
2524
2525void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2526 DCHECK(instruction->IsDiv() || instruction->IsRem());
2527 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2528
2529 LocationSummary* locations = instruction->GetLocations();
2530 Location second = locations->InAt(1);
2531 DCHECK(second.IsConstant());
2532
2533 Register out = locations->Out().AsRegister<Register>();
2534 Register dividend = locations->InAt(0).AsRegister<Register>();
2535 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2536
2537 int64_t magic;
2538 int shift;
2539 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2540
2541 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2542
2543 __ LoadConst32(TMP, magic);
2544 if (isR6) {
2545 __ MuhR6(TMP, dividend, TMP);
2546 } else {
2547 __ MultR2(dividend, TMP);
2548 __ Mfhi(TMP);
2549 }
2550 if (imm > 0 && magic < 0) {
2551 __ Addu(TMP, TMP, dividend);
2552 } else if (imm < 0 && magic > 0) {
2553 __ Subu(TMP, TMP, dividend);
2554 }
2555
2556 if (shift != 0) {
2557 __ Sra(TMP, TMP, shift);
2558 }
2559
2560 if (instruction->IsDiv()) {
2561 __ Sra(out, TMP, 31);
2562 __ Subu(out, TMP, out);
2563 } else {
2564 __ Sra(AT, TMP, 31);
2565 __ Subu(AT, TMP, AT);
2566 __ LoadConst32(TMP, imm);
2567 if (isR6) {
2568 __ MulR6(TMP, AT, TMP);
2569 } else {
2570 __ MulR2(TMP, AT, TMP);
2571 }
2572 __ Subu(out, dividend, TMP);
2573 }
2574}
2575
2576void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2577 DCHECK(instruction->IsDiv() || instruction->IsRem());
2578 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2579
2580 LocationSummary* locations = instruction->GetLocations();
2581 Register out = locations->Out().AsRegister<Register>();
2582 Location second = locations->InAt(1);
2583
2584 if (second.IsConstant()) {
2585 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2586 if (imm == 0) {
2587 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2588 } else if (imm == 1 || imm == -1) {
2589 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002590 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002591 DivRemByPowerOfTwo(instruction);
2592 } else {
2593 DCHECK(imm <= -2 || imm >= 2);
2594 GenerateDivRemWithAnyConstant(instruction);
2595 }
2596 } else {
2597 Register dividend = locations->InAt(0).AsRegister<Register>();
2598 Register divisor = second.AsRegister<Register>();
2599 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2600 if (instruction->IsDiv()) {
2601 if (isR6) {
2602 __ DivR6(out, dividend, divisor);
2603 } else {
2604 __ DivR2(out, dividend, divisor);
2605 }
2606 } else {
2607 if (isR6) {
2608 __ ModR6(out, dividend, divisor);
2609 } else {
2610 __ ModR2(out, dividend, divisor);
2611 }
2612 }
2613 }
2614}
2615
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002616void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2617 Primitive::Type type = div->GetResultType();
2618 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002619 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 : LocationSummary::kNoCall;
2621
2622 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2623
2624 switch (type) {
2625 case Primitive::kPrimInt:
2626 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002627 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2629 break;
2630
2631 case Primitive::kPrimLong: {
2632 InvokeRuntimeCallingConvention calling_convention;
2633 locations->SetInAt(0, Location::RegisterPairLocation(
2634 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2635 locations->SetInAt(1, Location::RegisterPairLocation(
2636 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2637 locations->SetOut(calling_convention.GetReturnLocation(type));
2638 break;
2639 }
2640
2641 case Primitive::kPrimFloat:
2642 case Primitive::kPrimDouble:
2643 locations->SetInAt(0, Location::RequiresFpuRegister());
2644 locations->SetInAt(1, Location::RequiresFpuRegister());
2645 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2646 break;
2647
2648 default:
2649 LOG(FATAL) << "Unexpected div type " << type;
2650 }
2651}
2652
2653void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2654 Primitive::Type type = instruction->GetType();
2655 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656
2657 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002658 case Primitive::kPrimInt:
2659 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002660 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002661 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002662 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002663 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2664 break;
2665 }
2666 case Primitive::kPrimFloat:
2667 case Primitive::kPrimDouble: {
2668 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2669 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2670 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2671 if (type == Primitive::kPrimFloat) {
2672 __ DivS(dst, lhs, rhs);
2673 } else {
2674 __ DivD(dst, lhs, rhs);
2675 }
2676 break;
2677 }
2678 default:
2679 LOG(FATAL) << "Unexpected div type " << type;
2680 }
2681}
2682
2683void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002684 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002685 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002686}
2687
2688void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2689 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2690 codegen_->AddSlowPath(slow_path);
2691 Location value = instruction->GetLocations()->InAt(0);
2692 Primitive::Type type = instruction->GetType();
2693
2694 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002695 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 case Primitive::kPrimByte:
2697 case Primitive::kPrimChar:
2698 case Primitive::kPrimShort:
2699 case Primitive::kPrimInt: {
2700 if (value.IsConstant()) {
2701 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2702 __ B(slow_path->GetEntryLabel());
2703 } else {
2704 // A division by a non-null constant is valid. We don't need to perform
2705 // any check, so simply fall through.
2706 }
2707 } else {
2708 DCHECK(value.IsRegister()) << value;
2709 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2710 }
2711 break;
2712 }
2713 case Primitive::kPrimLong: {
2714 if (value.IsConstant()) {
2715 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2716 __ B(slow_path->GetEntryLabel());
2717 } else {
2718 // A division by a non-null constant is valid. We don't need to perform
2719 // any check, so simply fall through.
2720 }
2721 } else {
2722 DCHECK(value.IsRegisterPair()) << value;
2723 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2724 __ Beqz(TMP, slow_path->GetEntryLabel());
2725 }
2726 break;
2727 }
2728 default:
2729 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2730 }
2731}
2732
2733void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2734 LocationSummary* locations =
2735 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2736 locations->SetOut(Location::ConstantLocation(constant));
2737}
2738
2739void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2740 // Will be generated at use site.
2741}
2742
2743void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2744 exit->SetLocations(nullptr);
2745}
2746
2747void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2748}
2749
2750void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2751 LocationSummary* locations =
2752 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2753 locations->SetOut(Location::ConstantLocation(constant));
2754}
2755
2756void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2757 // Will be generated at use site.
2758}
2759
2760void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2761 got->SetLocations(nullptr);
2762}
2763
2764void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2765 DCHECK(!successor->IsExitBlock());
2766 HBasicBlock* block = got->GetBlock();
2767 HInstruction* previous = got->GetPrevious();
2768 HLoopInformation* info = block->GetLoopInformation();
2769
2770 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2771 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2772 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2773 return;
2774 }
2775 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2776 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2777 }
2778 if (!codegen_->GoesToNextBlock(block, successor)) {
2779 __ B(codegen_->GetLabelOf(successor));
2780 }
2781}
2782
2783void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2784 HandleGoto(got, got->GetSuccessor());
2785}
2786
2787void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2788 try_boundary->SetLocations(nullptr);
2789}
2790
2791void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2792 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2793 if (!successor->IsExitBlock()) {
2794 HandleGoto(try_boundary, successor);
2795 }
2796}
2797
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002798void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2799 LocationSummary* locations) {
2800 Register dst = locations->Out().AsRegister<Register>();
2801 Register lhs = locations->InAt(0).AsRegister<Register>();
2802 Location rhs_location = locations->InAt(1);
2803 Register rhs_reg = ZERO;
2804 int64_t rhs_imm = 0;
2805 bool use_imm = rhs_location.IsConstant();
2806 if (use_imm) {
2807 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2808 } else {
2809 rhs_reg = rhs_location.AsRegister<Register>();
2810 }
2811
2812 switch (cond) {
2813 case kCondEQ:
2814 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002815 if (use_imm && IsInt<16>(-rhs_imm)) {
2816 if (rhs_imm == 0) {
2817 if (cond == kCondEQ) {
2818 __ Sltiu(dst, lhs, 1);
2819 } else {
2820 __ Sltu(dst, ZERO, lhs);
2821 }
2822 } else {
2823 __ Addiu(dst, lhs, -rhs_imm);
2824 if (cond == kCondEQ) {
2825 __ Sltiu(dst, dst, 1);
2826 } else {
2827 __ Sltu(dst, ZERO, dst);
2828 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002829 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002830 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002831 if (use_imm && IsUint<16>(rhs_imm)) {
2832 __ Xori(dst, lhs, rhs_imm);
2833 } else {
2834 if (use_imm) {
2835 rhs_reg = TMP;
2836 __ LoadConst32(rhs_reg, rhs_imm);
2837 }
2838 __ Xor(dst, lhs, rhs_reg);
2839 }
2840 if (cond == kCondEQ) {
2841 __ Sltiu(dst, dst, 1);
2842 } else {
2843 __ Sltu(dst, ZERO, dst);
2844 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002845 }
2846 break;
2847
2848 case kCondLT:
2849 case kCondGE:
2850 if (use_imm && IsInt<16>(rhs_imm)) {
2851 __ Slti(dst, lhs, rhs_imm);
2852 } else {
2853 if (use_imm) {
2854 rhs_reg = TMP;
2855 __ LoadConst32(rhs_reg, rhs_imm);
2856 }
2857 __ Slt(dst, lhs, rhs_reg);
2858 }
2859 if (cond == kCondGE) {
2860 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2861 // only the slt instruction but no sge.
2862 __ Xori(dst, dst, 1);
2863 }
2864 break;
2865
2866 case kCondLE:
2867 case kCondGT:
2868 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2869 // Simulate lhs <= rhs via lhs < rhs + 1.
2870 __ Slti(dst, lhs, rhs_imm + 1);
2871 if (cond == kCondGT) {
2872 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2873 // only the slti instruction but no sgti.
2874 __ Xori(dst, dst, 1);
2875 }
2876 } else {
2877 if (use_imm) {
2878 rhs_reg = TMP;
2879 __ LoadConst32(rhs_reg, rhs_imm);
2880 }
2881 __ Slt(dst, rhs_reg, lhs);
2882 if (cond == kCondLE) {
2883 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2884 // only the slt instruction but no sle.
2885 __ Xori(dst, dst, 1);
2886 }
2887 }
2888 break;
2889
2890 case kCondB:
2891 case kCondAE:
2892 if (use_imm && IsInt<16>(rhs_imm)) {
2893 // Sltiu sign-extends its 16-bit immediate operand before
2894 // the comparison and thus lets us compare directly with
2895 // unsigned values in the ranges [0, 0x7fff] and
2896 // [0xffff8000, 0xffffffff].
2897 __ Sltiu(dst, lhs, rhs_imm);
2898 } else {
2899 if (use_imm) {
2900 rhs_reg = TMP;
2901 __ LoadConst32(rhs_reg, rhs_imm);
2902 }
2903 __ Sltu(dst, lhs, rhs_reg);
2904 }
2905 if (cond == kCondAE) {
2906 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2907 // only the sltu instruction but no sgeu.
2908 __ Xori(dst, dst, 1);
2909 }
2910 break;
2911
2912 case kCondBE:
2913 case kCondA:
2914 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2915 // Simulate lhs <= rhs via lhs < rhs + 1.
2916 // Note that this only works if rhs + 1 does not overflow
2917 // to 0, hence the check above.
2918 // Sltiu sign-extends its 16-bit immediate operand before
2919 // the comparison and thus lets us compare directly with
2920 // unsigned values in the ranges [0, 0x7fff] and
2921 // [0xffff8000, 0xffffffff].
2922 __ Sltiu(dst, lhs, rhs_imm + 1);
2923 if (cond == kCondA) {
2924 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2925 // only the sltiu instruction but no sgtiu.
2926 __ Xori(dst, dst, 1);
2927 }
2928 } else {
2929 if (use_imm) {
2930 rhs_reg = TMP;
2931 __ LoadConst32(rhs_reg, rhs_imm);
2932 }
2933 __ Sltu(dst, rhs_reg, lhs);
2934 if (cond == kCondBE) {
2935 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2936 // only the sltu instruction but no sleu.
2937 __ Xori(dst, dst, 1);
2938 }
2939 }
2940 break;
2941 }
2942}
2943
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002944bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2945 LocationSummary* input_locations,
2946 Register dst) {
2947 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2948 Location rhs_location = input_locations->InAt(1);
2949 Register rhs_reg = ZERO;
2950 int64_t rhs_imm = 0;
2951 bool use_imm = rhs_location.IsConstant();
2952 if (use_imm) {
2953 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2954 } else {
2955 rhs_reg = rhs_location.AsRegister<Register>();
2956 }
2957
2958 switch (cond) {
2959 case kCondEQ:
2960 case kCondNE:
2961 if (use_imm && IsInt<16>(-rhs_imm)) {
2962 __ Addiu(dst, lhs, -rhs_imm);
2963 } else if (use_imm && IsUint<16>(rhs_imm)) {
2964 __ Xori(dst, lhs, rhs_imm);
2965 } else {
2966 if (use_imm) {
2967 rhs_reg = TMP;
2968 __ LoadConst32(rhs_reg, rhs_imm);
2969 }
2970 __ Xor(dst, lhs, rhs_reg);
2971 }
2972 return (cond == kCondEQ);
2973
2974 case kCondLT:
2975 case kCondGE:
2976 if (use_imm && IsInt<16>(rhs_imm)) {
2977 __ Slti(dst, lhs, rhs_imm);
2978 } else {
2979 if (use_imm) {
2980 rhs_reg = TMP;
2981 __ LoadConst32(rhs_reg, rhs_imm);
2982 }
2983 __ Slt(dst, lhs, rhs_reg);
2984 }
2985 return (cond == kCondGE);
2986
2987 case kCondLE:
2988 case kCondGT:
2989 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2990 // Simulate lhs <= rhs via lhs < rhs + 1.
2991 __ Slti(dst, lhs, rhs_imm + 1);
2992 return (cond == kCondGT);
2993 } else {
2994 if (use_imm) {
2995 rhs_reg = TMP;
2996 __ LoadConst32(rhs_reg, rhs_imm);
2997 }
2998 __ Slt(dst, rhs_reg, lhs);
2999 return (cond == kCondLE);
3000 }
3001
3002 case kCondB:
3003 case kCondAE:
3004 if (use_imm && IsInt<16>(rhs_imm)) {
3005 // Sltiu sign-extends its 16-bit immediate operand before
3006 // the comparison and thus lets us compare directly with
3007 // unsigned values in the ranges [0, 0x7fff] and
3008 // [0xffff8000, 0xffffffff].
3009 __ Sltiu(dst, lhs, rhs_imm);
3010 } else {
3011 if (use_imm) {
3012 rhs_reg = TMP;
3013 __ LoadConst32(rhs_reg, rhs_imm);
3014 }
3015 __ Sltu(dst, lhs, rhs_reg);
3016 }
3017 return (cond == kCondAE);
3018
3019 case kCondBE:
3020 case kCondA:
3021 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3022 // Simulate lhs <= rhs via lhs < rhs + 1.
3023 // Note that this only works if rhs + 1 does not overflow
3024 // to 0, hence the check above.
3025 // Sltiu sign-extends its 16-bit immediate operand before
3026 // the comparison and thus lets us compare directly with
3027 // unsigned values in the ranges [0, 0x7fff] and
3028 // [0xffff8000, 0xffffffff].
3029 __ Sltiu(dst, lhs, rhs_imm + 1);
3030 return (cond == kCondA);
3031 } else {
3032 if (use_imm) {
3033 rhs_reg = TMP;
3034 __ LoadConst32(rhs_reg, rhs_imm);
3035 }
3036 __ Sltu(dst, rhs_reg, lhs);
3037 return (cond == kCondBE);
3038 }
3039 }
3040}
3041
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003042void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3043 LocationSummary* locations,
3044 MipsLabel* label) {
3045 Register lhs = locations->InAt(0).AsRegister<Register>();
3046 Location rhs_location = locations->InAt(1);
3047 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003048 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003049 bool use_imm = rhs_location.IsConstant();
3050 if (use_imm) {
3051 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3052 } else {
3053 rhs_reg = rhs_location.AsRegister<Register>();
3054 }
3055
3056 if (use_imm && rhs_imm == 0) {
3057 switch (cond) {
3058 case kCondEQ:
3059 case kCondBE: // <= 0 if zero
3060 __ Beqz(lhs, label);
3061 break;
3062 case kCondNE:
3063 case kCondA: // > 0 if non-zero
3064 __ Bnez(lhs, label);
3065 break;
3066 case kCondLT:
3067 __ Bltz(lhs, label);
3068 break;
3069 case kCondGE:
3070 __ Bgez(lhs, label);
3071 break;
3072 case kCondLE:
3073 __ Blez(lhs, label);
3074 break;
3075 case kCondGT:
3076 __ Bgtz(lhs, label);
3077 break;
3078 case kCondB: // always false
3079 break;
3080 case kCondAE: // always true
3081 __ B(label);
3082 break;
3083 }
3084 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003085 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3086 if (isR6 || !use_imm) {
3087 if (use_imm) {
3088 rhs_reg = TMP;
3089 __ LoadConst32(rhs_reg, rhs_imm);
3090 }
3091 switch (cond) {
3092 case kCondEQ:
3093 __ Beq(lhs, rhs_reg, label);
3094 break;
3095 case kCondNE:
3096 __ Bne(lhs, rhs_reg, label);
3097 break;
3098 case kCondLT:
3099 __ Blt(lhs, rhs_reg, label);
3100 break;
3101 case kCondGE:
3102 __ Bge(lhs, rhs_reg, label);
3103 break;
3104 case kCondLE:
3105 __ Bge(rhs_reg, lhs, label);
3106 break;
3107 case kCondGT:
3108 __ Blt(rhs_reg, lhs, label);
3109 break;
3110 case kCondB:
3111 __ Bltu(lhs, rhs_reg, label);
3112 break;
3113 case kCondAE:
3114 __ Bgeu(lhs, rhs_reg, label);
3115 break;
3116 case kCondBE:
3117 __ Bgeu(rhs_reg, lhs, label);
3118 break;
3119 case kCondA:
3120 __ Bltu(rhs_reg, lhs, label);
3121 break;
3122 }
3123 } else {
3124 // Special cases for more efficient comparison with constants on R2.
3125 switch (cond) {
3126 case kCondEQ:
3127 __ LoadConst32(TMP, rhs_imm);
3128 __ Beq(lhs, TMP, label);
3129 break;
3130 case kCondNE:
3131 __ LoadConst32(TMP, rhs_imm);
3132 __ Bne(lhs, TMP, label);
3133 break;
3134 case kCondLT:
3135 if (IsInt<16>(rhs_imm)) {
3136 __ Slti(TMP, lhs, rhs_imm);
3137 __ Bnez(TMP, label);
3138 } else {
3139 __ LoadConst32(TMP, rhs_imm);
3140 __ Blt(lhs, TMP, label);
3141 }
3142 break;
3143 case kCondGE:
3144 if (IsInt<16>(rhs_imm)) {
3145 __ Slti(TMP, lhs, rhs_imm);
3146 __ Beqz(TMP, label);
3147 } else {
3148 __ LoadConst32(TMP, rhs_imm);
3149 __ Bge(lhs, TMP, label);
3150 }
3151 break;
3152 case kCondLE:
3153 if (IsInt<16>(rhs_imm + 1)) {
3154 // Simulate lhs <= rhs via lhs < rhs + 1.
3155 __ Slti(TMP, lhs, rhs_imm + 1);
3156 __ Bnez(TMP, label);
3157 } else {
3158 __ LoadConst32(TMP, rhs_imm);
3159 __ Bge(TMP, lhs, label);
3160 }
3161 break;
3162 case kCondGT:
3163 if (IsInt<16>(rhs_imm + 1)) {
3164 // Simulate lhs > rhs via !(lhs < rhs + 1).
3165 __ Slti(TMP, lhs, rhs_imm + 1);
3166 __ Beqz(TMP, label);
3167 } else {
3168 __ LoadConst32(TMP, rhs_imm);
3169 __ Blt(TMP, lhs, label);
3170 }
3171 break;
3172 case kCondB:
3173 if (IsInt<16>(rhs_imm)) {
3174 __ Sltiu(TMP, lhs, rhs_imm);
3175 __ Bnez(TMP, label);
3176 } else {
3177 __ LoadConst32(TMP, rhs_imm);
3178 __ Bltu(lhs, TMP, label);
3179 }
3180 break;
3181 case kCondAE:
3182 if (IsInt<16>(rhs_imm)) {
3183 __ Sltiu(TMP, lhs, rhs_imm);
3184 __ Beqz(TMP, label);
3185 } else {
3186 __ LoadConst32(TMP, rhs_imm);
3187 __ Bgeu(lhs, TMP, label);
3188 }
3189 break;
3190 case kCondBE:
3191 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3192 // Simulate lhs <= rhs via lhs < rhs + 1.
3193 // Note that this only works if rhs + 1 does not overflow
3194 // to 0, hence the check above.
3195 __ Sltiu(TMP, lhs, rhs_imm + 1);
3196 __ Bnez(TMP, label);
3197 } else {
3198 __ LoadConst32(TMP, rhs_imm);
3199 __ Bgeu(TMP, lhs, label);
3200 }
3201 break;
3202 case kCondA:
3203 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3204 // Simulate lhs > rhs via !(lhs < rhs + 1).
3205 // Note that this only works if rhs + 1 does not overflow
3206 // to 0, hence the check above.
3207 __ Sltiu(TMP, lhs, rhs_imm + 1);
3208 __ Beqz(TMP, label);
3209 } else {
3210 __ LoadConst32(TMP, rhs_imm);
3211 __ Bltu(TMP, lhs, label);
3212 }
3213 break;
3214 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003215 }
3216 }
3217}
3218
3219void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3220 LocationSummary* locations,
3221 MipsLabel* label) {
3222 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3223 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3224 Location rhs_location = locations->InAt(1);
3225 Register rhs_high = ZERO;
3226 Register rhs_low = ZERO;
3227 int64_t imm = 0;
3228 uint32_t imm_high = 0;
3229 uint32_t imm_low = 0;
3230 bool use_imm = rhs_location.IsConstant();
3231 if (use_imm) {
3232 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3233 imm_high = High32Bits(imm);
3234 imm_low = Low32Bits(imm);
3235 } else {
3236 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3237 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3238 }
3239
3240 if (use_imm && imm == 0) {
3241 switch (cond) {
3242 case kCondEQ:
3243 case kCondBE: // <= 0 if zero
3244 __ Or(TMP, lhs_high, lhs_low);
3245 __ Beqz(TMP, label);
3246 break;
3247 case kCondNE:
3248 case kCondA: // > 0 if non-zero
3249 __ Or(TMP, lhs_high, lhs_low);
3250 __ Bnez(TMP, label);
3251 break;
3252 case kCondLT:
3253 __ Bltz(lhs_high, label);
3254 break;
3255 case kCondGE:
3256 __ Bgez(lhs_high, label);
3257 break;
3258 case kCondLE:
3259 __ Or(TMP, lhs_high, lhs_low);
3260 __ Sra(AT, lhs_high, 31);
3261 __ Bgeu(AT, TMP, label);
3262 break;
3263 case kCondGT:
3264 __ Or(TMP, lhs_high, lhs_low);
3265 __ Sra(AT, lhs_high, 31);
3266 __ Bltu(AT, TMP, label);
3267 break;
3268 case kCondB: // always false
3269 break;
3270 case kCondAE: // always true
3271 __ B(label);
3272 break;
3273 }
3274 } else if (use_imm) {
3275 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3276 switch (cond) {
3277 case kCondEQ:
3278 __ LoadConst32(TMP, imm_high);
3279 __ Xor(TMP, TMP, lhs_high);
3280 __ LoadConst32(AT, imm_low);
3281 __ Xor(AT, AT, lhs_low);
3282 __ Or(TMP, TMP, AT);
3283 __ Beqz(TMP, label);
3284 break;
3285 case kCondNE:
3286 __ LoadConst32(TMP, imm_high);
3287 __ Xor(TMP, TMP, lhs_high);
3288 __ LoadConst32(AT, imm_low);
3289 __ Xor(AT, AT, lhs_low);
3290 __ Or(TMP, TMP, AT);
3291 __ Bnez(TMP, label);
3292 break;
3293 case kCondLT:
3294 __ LoadConst32(TMP, imm_high);
3295 __ Blt(lhs_high, TMP, label);
3296 __ Slt(TMP, TMP, lhs_high);
3297 __ LoadConst32(AT, imm_low);
3298 __ Sltu(AT, lhs_low, AT);
3299 __ Blt(TMP, AT, label);
3300 break;
3301 case kCondGE:
3302 __ LoadConst32(TMP, imm_high);
3303 __ Blt(TMP, lhs_high, label);
3304 __ Slt(TMP, lhs_high, TMP);
3305 __ LoadConst32(AT, imm_low);
3306 __ Sltu(AT, lhs_low, AT);
3307 __ Or(TMP, TMP, AT);
3308 __ Beqz(TMP, label);
3309 break;
3310 case kCondLE:
3311 __ LoadConst32(TMP, imm_high);
3312 __ Blt(lhs_high, TMP, label);
3313 __ Slt(TMP, TMP, lhs_high);
3314 __ LoadConst32(AT, imm_low);
3315 __ Sltu(AT, AT, lhs_low);
3316 __ Or(TMP, TMP, AT);
3317 __ Beqz(TMP, label);
3318 break;
3319 case kCondGT:
3320 __ LoadConst32(TMP, imm_high);
3321 __ Blt(TMP, lhs_high, label);
3322 __ Slt(TMP, lhs_high, TMP);
3323 __ LoadConst32(AT, imm_low);
3324 __ Sltu(AT, AT, lhs_low);
3325 __ Blt(TMP, AT, label);
3326 break;
3327 case kCondB:
3328 __ LoadConst32(TMP, imm_high);
3329 __ Bltu(lhs_high, TMP, label);
3330 __ Sltu(TMP, TMP, lhs_high);
3331 __ LoadConst32(AT, imm_low);
3332 __ Sltu(AT, lhs_low, AT);
3333 __ Blt(TMP, AT, label);
3334 break;
3335 case kCondAE:
3336 __ LoadConst32(TMP, imm_high);
3337 __ Bltu(TMP, lhs_high, label);
3338 __ Sltu(TMP, lhs_high, TMP);
3339 __ LoadConst32(AT, imm_low);
3340 __ Sltu(AT, lhs_low, AT);
3341 __ Or(TMP, TMP, AT);
3342 __ Beqz(TMP, label);
3343 break;
3344 case kCondBE:
3345 __ LoadConst32(TMP, imm_high);
3346 __ Bltu(lhs_high, TMP, label);
3347 __ Sltu(TMP, TMP, lhs_high);
3348 __ LoadConst32(AT, imm_low);
3349 __ Sltu(AT, AT, lhs_low);
3350 __ Or(TMP, TMP, AT);
3351 __ Beqz(TMP, label);
3352 break;
3353 case kCondA:
3354 __ LoadConst32(TMP, imm_high);
3355 __ Bltu(TMP, lhs_high, label);
3356 __ Sltu(TMP, lhs_high, TMP);
3357 __ LoadConst32(AT, imm_low);
3358 __ Sltu(AT, AT, lhs_low);
3359 __ Blt(TMP, AT, label);
3360 break;
3361 }
3362 } else {
3363 switch (cond) {
3364 case kCondEQ:
3365 __ Xor(TMP, lhs_high, rhs_high);
3366 __ Xor(AT, lhs_low, rhs_low);
3367 __ Or(TMP, TMP, AT);
3368 __ Beqz(TMP, label);
3369 break;
3370 case kCondNE:
3371 __ Xor(TMP, lhs_high, rhs_high);
3372 __ Xor(AT, lhs_low, rhs_low);
3373 __ Or(TMP, TMP, AT);
3374 __ Bnez(TMP, label);
3375 break;
3376 case kCondLT:
3377 __ Blt(lhs_high, rhs_high, label);
3378 __ Slt(TMP, rhs_high, lhs_high);
3379 __ Sltu(AT, lhs_low, rhs_low);
3380 __ Blt(TMP, AT, label);
3381 break;
3382 case kCondGE:
3383 __ Blt(rhs_high, lhs_high, label);
3384 __ Slt(TMP, lhs_high, rhs_high);
3385 __ Sltu(AT, lhs_low, rhs_low);
3386 __ Or(TMP, TMP, AT);
3387 __ Beqz(TMP, label);
3388 break;
3389 case kCondLE:
3390 __ Blt(lhs_high, rhs_high, label);
3391 __ Slt(TMP, rhs_high, lhs_high);
3392 __ Sltu(AT, rhs_low, lhs_low);
3393 __ Or(TMP, TMP, AT);
3394 __ Beqz(TMP, label);
3395 break;
3396 case kCondGT:
3397 __ Blt(rhs_high, lhs_high, label);
3398 __ Slt(TMP, lhs_high, rhs_high);
3399 __ Sltu(AT, rhs_low, lhs_low);
3400 __ Blt(TMP, AT, label);
3401 break;
3402 case kCondB:
3403 __ Bltu(lhs_high, rhs_high, label);
3404 __ Sltu(TMP, rhs_high, lhs_high);
3405 __ Sltu(AT, lhs_low, rhs_low);
3406 __ Blt(TMP, AT, label);
3407 break;
3408 case kCondAE:
3409 __ Bltu(rhs_high, lhs_high, label);
3410 __ Sltu(TMP, lhs_high, rhs_high);
3411 __ Sltu(AT, lhs_low, rhs_low);
3412 __ Or(TMP, TMP, AT);
3413 __ Beqz(TMP, label);
3414 break;
3415 case kCondBE:
3416 __ Bltu(lhs_high, rhs_high, label);
3417 __ Sltu(TMP, rhs_high, lhs_high);
3418 __ Sltu(AT, rhs_low, lhs_low);
3419 __ Or(TMP, TMP, AT);
3420 __ Beqz(TMP, label);
3421 break;
3422 case kCondA:
3423 __ Bltu(rhs_high, lhs_high, label);
3424 __ Sltu(TMP, lhs_high, rhs_high);
3425 __ Sltu(AT, rhs_low, lhs_low);
3426 __ Blt(TMP, AT, label);
3427 break;
3428 }
3429 }
3430}
3431
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003432void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3433 bool gt_bias,
3434 Primitive::Type type,
3435 LocationSummary* locations) {
3436 Register dst = locations->Out().AsRegister<Register>();
3437 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3438 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3439 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3440 if (type == Primitive::kPrimFloat) {
3441 if (isR6) {
3442 switch (cond) {
3443 case kCondEQ:
3444 __ CmpEqS(FTMP, lhs, rhs);
3445 __ Mfc1(dst, FTMP);
3446 __ Andi(dst, dst, 1);
3447 break;
3448 case kCondNE:
3449 __ CmpEqS(FTMP, lhs, rhs);
3450 __ Mfc1(dst, FTMP);
3451 __ Addiu(dst, dst, 1);
3452 break;
3453 case kCondLT:
3454 if (gt_bias) {
3455 __ CmpLtS(FTMP, lhs, rhs);
3456 } else {
3457 __ CmpUltS(FTMP, lhs, rhs);
3458 }
3459 __ Mfc1(dst, FTMP);
3460 __ Andi(dst, dst, 1);
3461 break;
3462 case kCondLE:
3463 if (gt_bias) {
3464 __ CmpLeS(FTMP, lhs, rhs);
3465 } else {
3466 __ CmpUleS(FTMP, lhs, rhs);
3467 }
3468 __ Mfc1(dst, FTMP);
3469 __ Andi(dst, dst, 1);
3470 break;
3471 case kCondGT:
3472 if (gt_bias) {
3473 __ CmpUltS(FTMP, rhs, lhs);
3474 } else {
3475 __ CmpLtS(FTMP, rhs, lhs);
3476 }
3477 __ Mfc1(dst, FTMP);
3478 __ Andi(dst, dst, 1);
3479 break;
3480 case kCondGE:
3481 if (gt_bias) {
3482 __ CmpUleS(FTMP, rhs, lhs);
3483 } else {
3484 __ CmpLeS(FTMP, rhs, lhs);
3485 }
3486 __ Mfc1(dst, FTMP);
3487 __ Andi(dst, dst, 1);
3488 break;
3489 default:
3490 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3491 UNREACHABLE();
3492 }
3493 } else {
3494 switch (cond) {
3495 case kCondEQ:
3496 __ CeqS(0, lhs, rhs);
3497 __ LoadConst32(dst, 1);
3498 __ Movf(dst, ZERO, 0);
3499 break;
3500 case kCondNE:
3501 __ CeqS(0, lhs, rhs);
3502 __ LoadConst32(dst, 1);
3503 __ Movt(dst, ZERO, 0);
3504 break;
3505 case kCondLT:
3506 if (gt_bias) {
3507 __ ColtS(0, lhs, rhs);
3508 } else {
3509 __ CultS(0, lhs, rhs);
3510 }
3511 __ LoadConst32(dst, 1);
3512 __ Movf(dst, ZERO, 0);
3513 break;
3514 case kCondLE:
3515 if (gt_bias) {
3516 __ ColeS(0, lhs, rhs);
3517 } else {
3518 __ CuleS(0, lhs, rhs);
3519 }
3520 __ LoadConst32(dst, 1);
3521 __ Movf(dst, ZERO, 0);
3522 break;
3523 case kCondGT:
3524 if (gt_bias) {
3525 __ CultS(0, rhs, lhs);
3526 } else {
3527 __ ColtS(0, rhs, lhs);
3528 }
3529 __ LoadConst32(dst, 1);
3530 __ Movf(dst, ZERO, 0);
3531 break;
3532 case kCondGE:
3533 if (gt_bias) {
3534 __ CuleS(0, rhs, lhs);
3535 } else {
3536 __ ColeS(0, rhs, lhs);
3537 }
3538 __ LoadConst32(dst, 1);
3539 __ Movf(dst, ZERO, 0);
3540 break;
3541 default:
3542 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3543 UNREACHABLE();
3544 }
3545 }
3546 } else {
3547 DCHECK_EQ(type, Primitive::kPrimDouble);
3548 if (isR6) {
3549 switch (cond) {
3550 case kCondEQ:
3551 __ CmpEqD(FTMP, lhs, rhs);
3552 __ Mfc1(dst, FTMP);
3553 __ Andi(dst, dst, 1);
3554 break;
3555 case kCondNE:
3556 __ CmpEqD(FTMP, lhs, rhs);
3557 __ Mfc1(dst, FTMP);
3558 __ Addiu(dst, dst, 1);
3559 break;
3560 case kCondLT:
3561 if (gt_bias) {
3562 __ CmpLtD(FTMP, lhs, rhs);
3563 } else {
3564 __ CmpUltD(FTMP, lhs, rhs);
3565 }
3566 __ Mfc1(dst, FTMP);
3567 __ Andi(dst, dst, 1);
3568 break;
3569 case kCondLE:
3570 if (gt_bias) {
3571 __ CmpLeD(FTMP, lhs, rhs);
3572 } else {
3573 __ CmpUleD(FTMP, lhs, rhs);
3574 }
3575 __ Mfc1(dst, FTMP);
3576 __ Andi(dst, dst, 1);
3577 break;
3578 case kCondGT:
3579 if (gt_bias) {
3580 __ CmpUltD(FTMP, rhs, lhs);
3581 } else {
3582 __ CmpLtD(FTMP, rhs, lhs);
3583 }
3584 __ Mfc1(dst, FTMP);
3585 __ Andi(dst, dst, 1);
3586 break;
3587 case kCondGE:
3588 if (gt_bias) {
3589 __ CmpUleD(FTMP, rhs, lhs);
3590 } else {
3591 __ CmpLeD(FTMP, rhs, lhs);
3592 }
3593 __ Mfc1(dst, FTMP);
3594 __ Andi(dst, dst, 1);
3595 break;
3596 default:
3597 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3598 UNREACHABLE();
3599 }
3600 } else {
3601 switch (cond) {
3602 case kCondEQ:
3603 __ CeqD(0, lhs, rhs);
3604 __ LoadConst32(dst, 1);
3605 __ Movf(dst, ZERO, 0);
3606 break;
3607 case kCondNE:
3608 __ CeqD(0, lhs, rhs);
3609 __ LoadConst32(dst, 1);
3610 __ Movt(dst, ZERO, 0);
3611 break;
3612 case kCondLT:
3613 if (gt_bias) {
3614 __ ColtD(0, lhs, rhs);
3615 } else {
3616 __ CultD(0, lhs, rhs);
3617 }
3618 __ LoadConst32(dst, 1);
3619 __ Movf(dst, ZERO, 0);
3620 break;
3621 case kCondLE:
3622 if (gt_bias) {
3623 __ ColeD(0, lhs, rhs);
3624 } else {
3625 __ CuleD(0, lhs, rhs);
3626 }
3627 __ LoadConst32(dst, 1);
3628 __ Movf(dst, ZERO, 0);
3629 break;
3630 case kCondGT:
3631 if (gt_bias) {
3632 __ CultD(0, rhs, lhs);
3633 } else {
3634 __ ColtD(0, rhs, lhs);
3635 }
3636 __ LoadConst32(dst, 1);
3637 __ Movf(dst, ZERO, 0);
3638 break;
3639 case kCondGE:
3640 if (gt_bias) {
3641 __ CuleD(0, rhs, lhs);
3642 } else {
3643 __ ColeD(0, rhs, lhs);
3644 }
3645 __ LoadConst32(dst, 1);
3646 __ Movf(dst, ZERO, 0);
3647 break;
3648 default:
3649 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3650 UNREACHABLE();
3651 }
3652 }
3653 }
3654}
3655
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003656bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3657 bool gt_bias,
3658 Primitive::Type type,
3659 LocationSummary* input_locations,
3660 int cc) {
3661 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3662 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3663 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3664 if (type == Primitive::kPrimFloat) {
3665 switch (cond) {
3666 case kCondEQ:
3667 __ CeqS(cc, lhs, rhs);
3668 return false;
3669 case kCondNE:
3670 __ CeqS(cc, lhs, rhs);
3671 return true;
3672 case kCondLT:
3673 if (gt_bias) {
3674 __ ColtS(cc, lhs, rhs);
3675 } else {
3676 __ CultS(cc, lhs, rhs);
3677 }
3678 return false;
3679 case kCondLE:
3680 if (gt_bias) {
3681 __ ColeS(cc, lhs, rhs);
3682 } else {
3683 __ CuleS(cc, lhs, rhs);
3684 }
3685 return false;
3686 case kCondGT:
3687 if (gt_bias) {
3688 __ CultS(cc, rhs, lhs);
3689 } else {
3690 __ ColtS(cc, rhs, lhs);
3691 }
3692 return false;
3693 case kCondGE:
3694 if (gt_bias) {
3695 __ CuleS(cc, rhs, lhs);
3696 } else {
3697 __ ColeS(cc, rhs, lhs);
3698 }
3699 return false;
3700 default:
3701 LOG(FATAL) << "Unexpected non-floating-point condition";
3702 UNREACHABLE();
3703 }
3704 } else {
3705 DCHECK_EQ(type, Primitive::kPrimDouble);
3706 switch (cond) {
3707 case kCondEQ:
3708 __ CeqD(cc, lhs, rhs);
3709 return false;
3710 case kCondNE:
3711 __ CeqD(cc, lhs, rhs);
3712 return true;
3713 case kCondLT:
3714 if (gt_bias) {
3715 __ ColtD(cc, lhs, rhs);
3716 } else {
3717 __ CultD(cc, lhs, rhs);
3718 }
3719 return false;
3720 case kCondLE:
3721 if (gt_bias) {
3722 __ ColeD(cc, lhs, rhs);
3723 } else {
3724 __ CuleD(cc, lhs, rhs);
3725 }
3726 return false;
3727 case kCondGT:
3728 if (gt_bias) {
3729 __ CultD(cc, rhs, lhs);
3730 } else {
3731 __ ColtD(cc, rhs, lhs);
3732 }
3733 return false;
3734 case kCondGE:
3735 if (gt_bias) {
3736 __ CuleD(cc, rhs, lhs);
3737 } else {
3738 __ ColeD(cc, rhs, lhs);
3739 }
3740 return false;
3741 default:
3742 LOG(FATAL) << "Unexpected non-floating-point condition";
3743 UNREACHABLE();
3744 }
3745 }
3746}
3747
3748bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3749 bool gt_bias,
3750 Primitive::Type type,
3751 LocationSummary* input_locations,
3752 FRegister dst) {
3753 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3754 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3755 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3756 if (type == Primitive::kPrimFloat) {
3757 switch (cond) {
3758 case kCondEQ:
3759 __ CmpEqS(dst, lhs, rhs);
3760 return false;
3761 case kCondNE:
3762 __ CmpEqS(dst, lhs, rhs);
3763 return true;
3764 case kCondLT:
3765 if (gt_bias) {
3766 __ CmpLtS(dst, lhs, rhs);
3767 } else {
3768 __ CmpUltS(dst, lhs, rhs);
3769 }
3770 return false;
3771 case kCondLE:
3772 if (gt_bias) {
3773 __ CmpLeS(dst, lhs, rhs);
3774 } else {
3775 __ CmpUleS(dst, lhs, rhs);
3776 }
3777 return false;
3778 case kCondGT:
3779 if (gt_bias) {
3780 __ CmpUltS(dst, rhs, lhs);
3781 } else {
3782 __ CmpLtS(dst, rhs, lhs);
3783 }
3784 return false;
3785 case kCondGE:
3786 if (gt_bias) {
3787 __ CmpUleS(dst, rhs, lhs);
3788 } else {
3789 __ CmpLeS(dst, rhs, lhs);
3790 }
3791 return false;
3792 default:
3793 LOG(FATAL) << "Unexpected non-floating-point condition";
3794 UNREACHABLE();
3795 }
3796 } else {
3797 DCHECK_EQ(type, Primitive::kPrimDouble);
3798 switch (cond) {
3799 case kCondEQ:
3800 __ CmpEqD(dst, lhs, rhs);
3801 return false;
3802 case kCondNE:
3803 __ CmpEqD(dst, lhs, rhs);
3804 return true;
3805 case kCondLT:
3806 if (gt_bias) {
3807 __ CmpLtD(dst, lhs, rhs);
3808 } else {
3809 __ CmpUltD(dst, lhs, rhs);
3810 }
3811 return false;
3812 case kCondLE:
3813 if (gt_bias) {
3814 __ CmpLeD(dst, lhs, rhs);
3815 } else {
3816 __ CmpUleD(dst, lhs, rhs);
3817 }
3818 return false;
3819 case kCondGT:
3820 if (gt_bias) {
3821 __ CmpUltD(dst, rhs, lhs);
3822 } else {
3823 __ CmpLtD(dst, rhs, lhs);
3824 }
3825 return false;
3826 case kCondGE:
3827 if (gt_bias) {
3828 __ CmpUleD(dst, rhs, lhs);
3829 } else {
3830 __ CmpLeD(dst, rhs, lhs);
3831 }
3832 return false;
3833 default:
3834 LOG(FATAL) << "Unexpected non-floating-point condition";
3835 UNREACHABLE();
3836 }
3837 }
3838}
3839
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003840void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3841 bool gt_bias,
3842 Primitive::Type type,
3843 LocationSummary* locations,
3844 MipsLabel* label) {
3845 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3846 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3847 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3848 if (type == Primitive::kPrimFloat) {
3849 if (isR6) {
3850 switch (cond) {
3851 case kCondEQ:
3852 __ CmpEqS(FTMP, lhs, rhs);
3853 __ Bc1nez(FTMP, label);
3854 break;
3855 case kCondNE:
3856 __ CmpEqS(FTMP, lhs, rhs);
3857 __ Bc1eqz(FTMP, label);
3858 break;
3859 case kCondLT:
3860 if (gt_bias) {
3861 __ CmpLtS(FTMP, lhs, rhs);
3862 } else {
3863 __ CmpUltS(FTMP, lhs, rhs);
3864 }
3865 __ Bc1nez(FTMP, label);
3866 break;
3867 case kCondLE:
3868 if (gt_bias) {
3869 __ CmpLeS(FTMP, lhs, rhs);
3870 } else {
3871 __ CmpUleS(FTMP, lhs, rhs);
3872 }
3873 __ Bc1nez(FTMP, label);
3874 break;
3875 case kCondGT:
3876 if (gt_bias) {
3877 __ CmpUltS(FTMP, rhs, lhs);
3878 } else {
3879 __ CmpLtS(FTMP, rhs, lhs);
3880 }
3881 __ Bc1nez(FTMP, label);
3882 break;
3883 case kCondGE:
3884 if (gt_bias) {
3885 __ CmpUleS(FTMP, rhs, lhs);
3886 } else {
3887 __ CmpLeS(FTMP, rhs, lhs);
3888 }
3889 __ Bc1nez(FTMP, label);
3890 break;
3891 default:
3892 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003893 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003894 }
3895 } else {
3896 switch (cond) {
3897 case kCondEQ:
3898 __ CeqS(0, lhs, rhs);
3899 __ Bc1t(0, label);
3900 break;
3901 case kCondNE:
3902 __ CeqS(0, lhs, rhs);
3903 __ Bc1f(0, label);
3904 break;
3905 case kCondLT:
3906 if (gt_bias) {
3907 __ ColtS(0, lhs, rhs);
3908 } else {
3909 __ CultS(0, lhs, rhs);
3910 }
3911 __ Bc1t(0, label);
3912 break;
3913 case kCondLE:
3914 if (gt_bias) {
3915 __ ColeS(0, lhs, rhs);
3916 } else {
3917 __ CuleS(0, lhs, rhs);
3918 }
3919 __ Bc1t(0, label);
3920 break;
3921 case kCondGT:
3922 if (gt_bias) {
3923 __ CultS(0, rhs, lhs);
3924 } else {
3925 __ ColtS(0, rhs, lhs);
3926 }
3927 __ Bc1t(0, label);
3928 break;
3929 case kCondGE:
3930 if (gt_bias) {
3931 __ CuleS(0, rhs, lhs);
3932 } else {
3933 __ ColeS(0, rhs, lhs);
3934 }
3935 __ Bc1t(0, label);
3936 break;
3937 default:
3938 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003939 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003940 }
3941 }
3942 } else {
3943 DCHECK_EQ(type, Primitive::kPrimDouble);
3944 if (isR6) {
3945 switch (cond) {
3946 case kCondEQ:
3947 __ CmpEqD(FTMP, lhs, rhs);
3948 __ Bc1nez(FTMP, label);
3949 break;
3950 case kCondNE:
3951 __ CmpEqD(FTMP, lhs, rhs);
3952 __ Bc1eqz(FTMP, label);
3953 break;
3954 case kCondLT:
3955 if (gt_bias) {
3956 __ CmpLtD(FTMP, lhs, rhs);
3957 } else {
3958 __ CmpUltD(FTMP, lhs, rhs);
3959 }
3960 __ Bc1nez(FTMP, label);
3961 break;
3962 case kCondLE:
3963 if (gt_bias) {
3964 __ CmpLeD(FTMP, lhs, rhs);
3965 } else {
3966 __ CmpUleD(FTMP, lhs, rhs);
3967 }
3968 __ Bc1nez(FTMP, label);
3969 break;
3970 case kCondGT:
3971 if (gt_bias) {
3972 __ CmpUltD(FTMP, rhs, lhs);
3973 } else {
3974 __ CmpLtD(FTMP, rhs, lhs);
3975 }
3976 __ Bc1nez(FTMP, label);
3977 break;
3978 case kCondGE:
3979 if (gt_bias) {
3980 __ CmpUleD(FTMP, rhs, lhs);
3981 } else {
3982 __ CmpLeD(FTMP, rhs, lhs);
3983 }
3984 __ Bc1nez(FTMP, label);
3985 break;
3986 default:
3987 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003988 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003989 }
3990 } else {
3991 switch (cond) {
3992 case kCondEQ:
3993 __ CeqD(0, lhs, rhs);
3994 __ Bc1t(0, label);
3995 break;
3996 case kCondNE:
3997 __ CeqD(0, lhs, rhs);
3998 __ Bc1f(0, label);
3999 break;
4000 case kCondLT:
4001 if (gt_bias) {
4002 __ ColtD(0, lhs, rhs);
4003 } else {
4004 __ CultD(0, lhs, rhs);
4005 }
4006 __ Bc1t(0, label);
4007 break;
4008 case kCondLE:
4009 if (gt_bias) {
4010 __ ColeD(0, lhs, rhs);
4011 } else {
4012 __ CuleD(0, lhs, rhs);
4013 }
4014 __ Bc1t(0, label);
4015 break;
4016 case kCondGT:
4017 if (gt_bias) {
4018 __ CultD(0, rhs, lhs);
4019 } else {
4020 __ ColtD(0, rhs, lhs);
4021 }
4022 __ Bc1t(0, label);
4023 break;
4024 case kCondGE:
4025 if (gt_bias) {
4026 __ CuleD(0, rhs, lhs);
4027 } else {
4028 __ ColeD(0, rhs, lhs);
4029 }
4030 __ Bc1t(0, label);
4031 break;
4032 default:
4033 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004034 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004035 }
4036 }
4037 }
4038}
4039
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004040void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004041 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004042 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004043 MipsLabel* false_target) {
4044 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004045
David Brazdil0debae72015-11-12 18:37:00 +00004046 if (true_target == nullptr && false_target == nullptr) {
4047 // Nothing to do. The code always falls through.
4048 return;
4049 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004050 // Constant condition, statically compared against "true" (integer value 1).
4051 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004052 if (true_target != nullptr) {
4053 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004054 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004055 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004056 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004057 if (false_target != nullptr) {
4058 __ B(false_target);
4059 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004060 }
David Brazdil0debae72015-11-12 18:37:00 +00004061 return;
4062 }
4063
4064 // The following code generates these patterns:
4065 // (1) true_target == nullptr && false_target != nullptr
4066 // - opposite condition true => branch to false_target
4067 // (2) true_target != nullptr && false_target == nullptr
4068 // - condition true => branch to true_target
4069 // (3) true_target != nullptr && false_target != nullptr
4070 // - condition true => branch to true_target
4071 // - branch to false_target
4072 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004073 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004074 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004075 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004076 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004077 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4078 } else {
4079 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4080 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004081 } else {
4082 // The condition instruction has not been materialized, use its inputs as
4083 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004084 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004085 Primitive::Type type = condition->InputAt(0)->GetType();
4086 LocationSummary* locations = cond->GetLocations();
4087 IfCondition if_cond = condition->GetCondition();
4088 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004089
David Brazdil0debae72015-11-12 18:37:00 +00004090 if (true_target == nullptr) {
4091 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004092 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004093 }
4094
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004095 switch (type) {
4096 default:
4097 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4098 break;
4099 case Primitive::kPrimLong:
4100 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4101 break;
4102 case Primitive::kPrimFloat:
4103 case Primitive::kPrimDouble:
4104 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4105 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004106 }
4107 }
David Brazdil0debae72015-11-12 18:37:00 +00004108
4109 // If neither branch falls through (case 3), the conditional branch to `true_target`
4110 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4111 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004112 __ B(false_target);
4113 }
4114}
4115
4116void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4117 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004118 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004119 locations->SetInAt(0, Location::RequiresRegister());
4120 }
4121}
4122
4123void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004124 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4125 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4126 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4127 nullptr : codegen_->GetLabelOf(true_successor);
4128 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4129 nullptr : codegen_->GetLabelOf(false_successor);
4130 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004131}
4132
4133void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4134 LocationSummary* locations = new (GetGraph()->GetArena())
4135 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004136 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004137 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004138 locations->SetInAt(0, Location::RequiresRegister());
4139 }
4140}
4141
4142void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004143 SlowPathCodeMIPS* slow_path =
4144 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004145 GenerateTestAndBranch(deoptimize,
4146 /* condition_input_index */ 0,
4147 slow_path->GetEntryLabel(),
4148 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004149}
4150
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004151// This function returns true if a conditional move can be generated for HSelect.
4152// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4153// branches and regular moves.
4154//
4155// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4156//
4157// While determining feasibility of a conditional move and setting inputs/outputs
4158// are two distinct tasks, this function does both because they share quite a bit
4159// of common logic.
4160static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4161 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4162 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4163 HCondition* condition = cond->AsCondition();
4164
4165 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4166 Primitive::Type dst_type = select->GetType();
4167
4168 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4169 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4170 bool is_true_value_zero_constant =
4171 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4172 bool is_false_value_zero_constant =
4173 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4174
4175 bool can_move_conditionally = false;
4176 bool use_const_for_false_in = false;
4177 bool use_const_for_true_in = false;
4178
4179 if (!cond->IsConstant()) {
4180 switch (cond_type) {
4181 default:
4182 switch (dst_type) {
4183 default:
4184 // Moving int on int condition.
4185 if (is_r6) {
4186 if (is_true_value_zero_constant) {
4187 // seleqz out_reg, false_reg, cond_reg
4188 can_move_conditionally = true;
4189 use_const_for_true_in = true;
4190 } else if (is_false_value_zero_constant) {
4191 // selnez out_reg, true_reg, cond_reg
4192 can_move_conditionally = true;
4193 use_const_for_false_in = true;
4194 } else if (materialized) {
4195 // Not materializing unmaterialized int conditions
4196 // to keep the instruction count low.
4197 // selnez AT, true_reg, cond_reg
4198 // seleqz TMP, false_reg, cond_reg
4199 // or out_reg, AT, TMP
4200 can_move_conditionally = true;
4201 }
4202 } else {
4203 // movn out_reg, true_reg/ZERO, cond_reg
4204 can_move_conditionally = true;
4205 use_const_for_true_in = is_true_value_zero_constant;
4206 }
4207 break;
4208 case Primitive::kPrimLong:
4209 // Moving long on int condition.
4210 if (is_r6) {
4211 if (is_true_value_zero_constant) {
4212 // seleqz out_reg_lo, false_reg_lo, cond_reg
4213 // seleqz out_reg_hi, false_reg_hi, cond_reg
4214 can_move_conditionally = true;
4215 use_const_for_true_in = true;
4216 } else if (is_false_value_zero_constant) {
4217 // selnez out_reg_lo, true_reg_lo, cond_reg
4218 // selnez out_reg_hi, true_reg_hi, cond_reg
4219 can_move_conditionally = true;
4220 use_const_for_false_in = true;
4221 }
4222 // Other long conditional moves would generate 6+ instructions,
4223 // which is too many.
4224 } else {
4225 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4226 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4227 can_move_conditionally = true;
4228 use_const_for_true_in = is_true_value_zero_constant;
4229 }
4230 break;
4231 case Primitive::kPrimFloat:
4232 case Primitive::kPrimDouble:
4233 // Moving float/double on int condition.
4234 if (is_r6) {
4235 if (materialized) {
4236 // Not materializing unmaterialized int conditions
4237 // to keep the instruction count low.
4238 can_move_conditionally = true;
4239 if (is_true_value_zero_constant) {
4240 // sltu TMP, ZERO, cond_reg
4241 // mtc1 TMP, temp_cond_reg
4242 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4243 use_const_for_true_in = true;
4244 } else if (is_false_value_zero_constant) {
4245 // sltu TMP, ZERO, cond_reg
4246 // mtc1 TMP, temp_cond_reg
4247 // selnez.fmt out_reg, true_reg, temp_cond_reg
4248 use_const_for_false_in = true;
4249 } else {
4250 // sltu TMP, ZERO, cond_reg
4251 // mtc1 TMP, temp_cond_reg
4252 // sel.fmt temp_cond_reg, false_reg, true_reg
4253 // mov.fmt out_reg, temp_cond_reg
4254 }
4255 }
4256 } else {
4257 // movn.fmt out_reg, true_reg, cond_reg
4258 can_move_conditionally = true;
4259 }
4260 break;
4261 }
4262 break;
4263 case Primitive::kPrimLong:
4264 // We don't materialize long comparison now
4265 // and use conditional branches instead.
4266 break;
4267 case Primitive::kPrimFloat:
4268 case Primitive::kPrimDouble:
4269 switch (dst_type) {
4270 default:
4271 // Moving int on float/double condition.
4272 if (is_r6) {
4273 if (is_true_value_zero_constant) {
4274 // mfc1 TMP, temp_cond_reg
4275 // seleqz out_reg, false_reg, TMP
4276 can_move_conditionally = true;
4277 use_const_for_true_in = true;
4278 } else if (is_false_value_zero_constant) {
4279 // mfc1 TMP, temp_cond_reg
4280 // selnez out_reg, true_reg, TMP
4281 can_move_conditionally = true;
4282 use_const_for_false_in = true;
4283 } else {
4284 // mfc1 TMP, temp_cond_reg
4285 // selnez AT, true_reg, TMP
4286 // seleqz TMP, false_reg, TMP
4287 // or out_reg, AT, TMP
4288 can_move_conditionally = true;
4289 }
4290 } else {
4291 // movt out_reg, true_reg/ZERO, cc
4292 can_move_conditionally = true;
4293 use_const_for_true_in = is_true_value_zero_constant;
4294 }
4295 break;
4296 case Primitive::kPrimLong:
4297 // Moving long on float/double condition.
4298 if (is_r6) {
4299 if (is_true_value_zero_constant) {
4300 // mfc1 TMP, temp_cond_reg
4301 // seleqz out_reg_lo, false_reg_lo, TMP
4302 // seleqz out_reg_hi, false_reg_hi, TMP
4303 can_move_conditionally = true;
4304 use_const_for_true_in = true;
4305 } else if (is_false_value_zero_constant) {
4306 // mfc1 TMP, temp_cond_reg
4307 // selnez out_reg_lo, true_reg_lo, TMP
4308 // selnez out_reg_hi, true_reg_hi, TMP
4309 can_move_conditionally = true;
4310 use_const_for_false_in = true;
4311 }
4312 // Other long conditional moves would generate 6+ instructions,
4313 // which is too many.
4314 } else {
4315 // movt out_reg_lo, true_reg_lo/ZERO, cc
4316 // movt out_reg_hi, true_reg_hi/ZERO, cc
4317 can_move_conditionally = true;
4318 use_const_for_true_in = is_true_value_zero_constant;
4319 }
4320 break;
4321 case Primitive::kPrimFloat:
4322 case Primitive::kPrimDouble:
4323 // Moving float/double on float/double condition.
4324 if (is_r6) {
4325 can_move_conditionally = true;
4326 if (is_true_value_zero_constant) {
4327 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4328 use_const_for_true_in = true;
4329 } else if (is_false_value_zero_constant) {
4330 // selnez.fmt out_reg, true_reg, temp_cond_reg
4331 use_const_for_false_in = true;
4332 } else {
4333 // sel.fmt temp_cond_reg, false_reg, true_reg
4334 // mov.fmt out_reg, temp_cond_reg
4335 }
4336 } else {
4337 // movt.fmt out_reg, true_reg, cc
4338 can_move_conditionally = true;
4339 }
4340 break;
4341 }
4342 break;
4343 }
4344 }
4345
4346 if (can_move_conditionally) {
4347 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4348 } else {
4349 DCHECK(!use_const_for_false_in);
4350 DCHECK(!use_const_for_true_in);
4351 }
4352
4353 if (locations_to_set != nullptr) {
4354 if (use_const_for_false_in) {
4355 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4356 } else {
4357 locations_to_set->SetInAt(0,
4358 Primitive::IsFloatingPointType(dst_type)
4359 ? Location::RequiresFpuRegister()
4360 : Location::RequiresRegister());
4361 }
4362 if (use_const_for_true_in) {
4363 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4364 } else {
4365 locations_to_set->SetInAt(1,
4366 Primitive::IsFloatingPointType(dst_type)
4367 ? Location::RequiresFpuRegister()
4368 : Location::RequiresRegister());
4369 }
4370 if (materialized) {
4371 locations_to_set->SetInAt(2, Location::RequiresRegister());
4372 }
4373 // On R6 we don't require the output to be the same as the
4374 // first input for conditional moves unlike on R2.
4375 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4376 if (is_out_same_as_first_in) {
4377 locations_to_set->SetOut(Location::SameAsFirstInput());
4378 } else {
4379 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4380 ? Location::RequiresFpuRegister()
4381 : Location::RequiresRegister());
4382 }
4383 }
4384
4385 return can_move_conditionally;
4386}
4387
4388void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4389 LocationSummary* locations = select->GetLocations();
4390 Location dst = locations->Out();
4391 Location src = locations->InAt(1);
4392 Register src_reg = ZERO;
4393 Register src_reg_high = ZERO;
4394 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4395 Register cond_reg = TMP;
4396 int cond_cc = 0;
4397 Primitive::Type cond_type = Primitive::kPrimInt;
4398 bool cond_inverted = false;
4399 Primitive::Type dst_type = select->GetType();
4400
4401 if (IsBooleanValueOrMaterializedCondition(cond)) {
4402 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4403 } else {
4404 HCondition* condition = cond->AsCondition();
4405 LocationSummary* cond_locations = cond->GetLocations();
4406 IfCondition if_cond = condition->GetCondition();
4407 cond_type = condition->InputAt(0)->GetType();
4408 switch (cond_type) {
4409 default:
4410 DCHECK_NE(cond_type, Primitive::kPrimLong);
4411 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4412 break;
4413 case Primitive::kPrimFloat:
4414 case Primitive::kPrimDouble:
4415 cond_inverted = MaterializeFpCompareR2(if_cond,
4416 condition->IsGtBias(),
4417 cond_type,
4418 cond_locations,
4419 cond_cc);
4420 break;
4421 }
4422 }
4423
4424 DCHECK(dst.Equals(locations->InAt(0)));
4425 if (src.IsRegister()) {
4426 src_reg = src.AsRegister<Register>();
4427 } else if (src.IsRegisterPair()) {
4428 src_reg = src.AsRegisterPairLow<Register>();
4429 src_reg_high = src.AsRegisterPairHigh<Register>();
4430 } else if (src.IsConstant()) {
4431 DCHECK(src.GetConstant()->IsZeroBitPattern());
4432 }
4433
4434 switch (cond_type) {
4435 default:
4436 switch (dst_type) {
4437 default:
4438 if (cond_inverted) {
4439 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4440 } else {
4441 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4442 }
4443 break;
4444 case Primitive::kPrimLong:
4445 if (cond_inverted) {
4446 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4447 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4448 } else {
4449 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4450 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4451 }
4452 break;
4453 case Primitive::kPrimFloat:
4454 if (cond_inverted) {
4455 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4456 } else {
4457 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4458 }
4459 break;
4460 case Primitive::kPrimDouble:
4461 if (cond_inverted) {
4462 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4463 } else {
4464 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4465 }
4466 break;
4467 }
4468 break;
4469 case Primitive::kPrimLong:
4470 LOG(FATAL) << "Unreachable";
4471 UNREACHABLE();
4472 case Primitive::kPrimFloat:
4473 case Primitive::kPrimDouble:
4474 switch (dst_type) {
4475 default:
4476 if (cond_inverted) {
4477 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4478 } else {
4479 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4480 }
4481 break;
4482 case Primitive::kPrimLong:
4483 if (cond_inverted) {
4484 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4485 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4486 } else {
4487 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4488 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4489 }
4490 break;
4491 case Primitive::kPrimFloat:
4492 if (cond_inverted) {
4493 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4494 } else {
4495 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4496 }
4497 break;
4498 case Primitive::kPrimDouble:
4499 if (cond_inverted) {
4500 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4501 } else {
4502 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4503 }
4504 break;
4505 }
4506 break;
4507 }
4508}
4509
4510void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4511 LocationSummary* locations = select->GetLocations();
4512 Location dst = locations->Out();
4513 Location false_src = locations->InAt(0);
4514 Location true_src = locations->InAt(1);
4515 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4516 Register cond_reg = TMP;
4517 FRegister fcond_reg = FTMP;
4518 Primitive::Type cond_type = Primitive::kPrimInt;
4519 bool cond_inverted = false;
4520 Primitive::Type dst_type = select->GetType();
4521
4522 if (IsBooleanValueOrMaterializedCondition(cond)) {
4523 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4524 } else {
4525 HCondition* condition = cond->AsCondition();
4526 LocationSummary* cond_locations = cond->GetLocations();
4527 IfCondition if_cond = condition->GetCondition();
4528 cond_type = condition->InputAt(0)->GetType();
4529 switch (cond_type) {
4530 default:
4531 DCHECK_NE(cond_type, Primitive::kPrimLong);
4532 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4533 break;
4534 case Primitive::kPrimFloat:
4535 case Primitive::kPrimDouble:
4536 cond_inverted = MaterializeFpCompareR6(if_cond,
4537 condition->IsGtBias(),
4538 cond_type,
4539 cond_locations,
4540 fcond_reg);
4541 break;
4542 }
4543 }
4544
4545 if (true_src.IsConstant()) {
4546 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4547 }
4548 if (false_src.IsConstant()) {
4549 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4550 }
4551
4552 switch (dst_type) {
4553 default:
4554 if (Primitive::IsFloatingPointType(cond_type)) {
4555 __ Mfc1(cond_reg, fcond_reg);
4556 }
4557 if (true_src.IsConstant()) {
4558 if (cond_inverted) {
4559 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4560 } else {
4561 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4562 }
4563 } else if (false_src.IsConstant()) {
4564 if (cond_inverted) {
4565 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4566 } else {
4567 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4568 }
4569 } else {
4570 DCHECK_NE(cond_reg, AT);
4571 if (cond_inverted) {
4572 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4573 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4574 } else {
4575 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4576 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4577 }
4578 __ Or(dst.AsRegister<Register>(), AT, TMP);
4579 }
4580 break;
4581 case Primitive::kPrimLong: {
4582 if (Primitive::IsFloatingPointType(cond_type)) {
4583 __ Mfc1(cond_reg, fcond_reg);
4584 }
4585 Register dst_lo = dst.AsRegisterPairLow<Register>();
4586 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4587 if (true_src.IsConstant()) {
4588 Register src_lo = false_src.AsRegisterPairLow<Register>();
4589 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4590 if (cond_inverted) {
4591 __ Selnez(dst_lo, src_lo, cond_reg);
4592 __ Selnez(dst_hi, src_hi, cond_reg);
4593 } else {
4594 __ Seleqz(dst_lo, src_lo, cond_reg);
4595 __ Seleqz(dst_hi, src_hi, cond_reg);
4596 }
4597 } else {
4598 DCHECK(false_src.IsConstant());
4599 Register src_lo = true_src.AsRegisterPairLow<Register>();
4600 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4601 if (cond_inverted) {
4602 __ Seleqz(dst_lo, src_lo, cond_reg);
4603 __ Seleqz(dst_hi, src_hi, cond_reg);
4604 } else {
4605 __ Selnez(dst_lo, src_lo, cond_reg);
4606 __ Selnez(dst_hi, src_hi, cond_reg);
4607 }
4608 }
4609 break;
4610 }
4611 case Primitive::kPrimFloat: {
4612 if (!Primitive::IsFloatingPointType(cond_type)) {
4613 // sel*.fmt tests bit 0 of the condition register, account for that.
4614 __ Sltu(TMP, ZERO, cond_reg);
4615 __ Mtc1(TMP, fcond_reg);
4616 }
4617 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4618 if (true_src.IsConstant()) {
4619 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4620 if (cond_inverted) {
4621 __ SelnezS(dst_reg, src_reg, fcond_reg);
4622 } else {
4623 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4624 }
4625 } else if (false_src.IsConstant()) {
4626 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4627 if (cond_inverted) {
4628 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4629 } else {
4630 __ SelnezS(dst_reg, src_reg, fcond_reg);
4631 }
4632 } else {
4633 if (cond_inverted) {
4634 __ SelS(fcond_reg,
4635 true_src.AsFpuRegister<FRegister>(),
4636 false_src.AsFpuRegister<FRegister>());
4637 } else {
4638 __ SelS(fcond_reg,
4639 false_src.AsFpuRegister<FRegister>(),
4640 true_src.AsFpuRegister<FRegister>());
4641 }
4642 __ MovS(dst_reg, fcond_reg);
4643 }
4644 break;
4645 }
4646 case Primitive::kPrimDouble: {
4647 if (!Primitive::IsFloatingPointType(cond_type)) {
4648 // sel*.fmt tests bit 0 of the condition register, account for that.
4649 __ Sltu(TMP, ZERO, cond_reg);
4650 __ Mtc1(TMP, fcond_reg);
4651 }
4652 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4653 if (true_src.IsConstant()) {
4654 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4655 if (cond_inverted) {
4656 __ SelnezD(dst_reg, src_reg, fcond_reg);
4657 } else {
4658 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4659 }
4660 } else if (false_src.IsConstant()) {
4661 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4662 if (cond_inverted) {
4663 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4664 } else {
4665 __ SelnezD(dst_reg, src_reg, fcond_reg);
4666 }
4667 } else {
4668 if (cond_inverted) {
4669 __ SelD(fcond_reg,
4670 true_src.AsFpuRegister<FRegister>(),
4671 false_src.AsFpuRegister<FRegister>());
4672 } else {
4673 __ SelD(fcond_reg,
4674 false_src.AsFpuRegister<FRegister>(),
4675 true_src.AsFpuRegister<FRegister>());
4676 }
4677 __ MovD(dst_reg, fcond_reg);
4678 }
4679 break;
4680 }
4681 }
4682}
4683
David Brazdil74eb1b22015-12-14 11:44:01 +00004684void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004686 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004687}
4688
4689void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004690 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4691 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4692 if (is_r6) {
4693 GenConditionalMoveR6(select);
4694 } else {
4695 GenConditionalMoveR2(select);
4696 }
4697 } else {
4698 LocationSummary* locations = select->GetLocations();
4699 MipsLabel false_target;
4700 GenerateTestAndBranch(select,
4701 /* condition_input_index */ 2,
4702 /* true_target */ nullptr,
4703 &false_target);
4704 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4705 __ Bind(&false_target);
4706 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004707}
4708
David Srbecky0cf44932015-12-09 14:09:59 +00004709void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4710 new (GetGraph()->GetArena()) LocationSummary(info);
4711}
4712
David Srbeckyd28f4a02016-03-14 17:14:24 +00004713void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4714 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004715}
4716
4717void CodeGeneratorMIPS::GenerateNop() {
4718 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004719}
4720
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004721void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4722 Primitive::Type field_type = field_info.GetFieldType();
4723 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4724 bool generate_volatile = field_info.IsVolatile() && is_wide;
4725 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004726 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004727
4728 locations->SetInAt(0, Location::RequiresRegister());
4729 if (generate_volatile) {
4730 InvokeRuntimeCallingConvention calling_convention;
4731 // need A0 to hold base + offset
4732 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4733 if (field_type == Primitive::kPrimLong) {
4734 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4735 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004736 // Use Location::Any() to prevent situations when running out of available fp registers.
4737 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004738 // Need some temp core regs since FP results are returned in core registers
4739 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4740 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4741 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4742 }
4743 } else {
4744 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4745 locations->SetOut(Location::RequiresFpuRegister());
4746 } else {
4747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4748 }
4749 }
4750}
4751
4752void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4753 const FieldInfo& field_info,
4754 uint32_t dex_pc) {
4755 Primitive::Type type = field_info.GetFieldType();
4756 LocationSummary* locations = instruction->GetLocations();
4757 Register obj = locations->InAt(0).AsRegister<Register>();
4758 LoadOperandType load_type = kLoadUnsignedByte;
4759 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004760 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004761 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004762
4763 switch (type) {
4764 case Primitive::kPrimBoolean:
4765 load_type = kLoadUnsignedByte;
4766 break;
4767 case Primitive::kPrimByte:
4768 load_type = kLoadSignedByte;
4769 break;
4770 case Primitive::kPrimShort:
4771 load_type = kLoadSignedHalfword;
4772 break;
4773 case Primitive::kPrimChar:
4774 load_type = kLoadUnsignedHalfword;
4775 break;
4776 case Primitive::kPrimInt:
4777 case Primitive::kPrimFloat:
4778 case Primitive::kPrimNot:
4779 load_type = kLoadWord;
4780 break;
4781 case Primitive::kPrimLong:
4782 case Primitive::kPrimDouble:
4783 load_type = kLoadDoubleword;
4784 break;
4785 case Primitive::kPrimVoid:
4786 LOG(FATAL) << "Unreachable type " << type;
4787 UNREACHABLE();
4788 }
4789
4790 if (is_volatile && load_type == kLoadDoubleword) {
4791 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004792 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004793 // Do implicit Null check
4794 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4795 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004796 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004797 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4798 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004799 // FP results are returned in core registers. Need to move them.
4800 Location out = locations->Out();
4801 if (out.IsFpuRegister()) {
4802 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4803 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4804 out.AsFpuRegister<FRegister>());
4805 } else {
4806 DCHECK(out.IsDoubleStackSlot());
4807 __ StoreToOffset(kStoreWord,
4808 locations->GetTemp(1).AsRegister<Register>(),
4809 SP,
4810 out.GetStackIndex());
4811 __ StoreToOffset(kStoreWord,
4812 locations->GetTemp(2).AsRegister<Register>(),
4813 SP,
4814 out.GetStackIndex() + 4);
4815 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004816 }
4817 } else {
4818 if (!Primitive::IsFloatingPointType(type)) {
4819 Register dst;
4820 if (type == Primitive::kPrimLong) {
4821 DCHECK(locations->Out().IsRegisterPair());
4822 dst = locations->Out().AsRegisterPairLow<Register>();
4823 } else {
4824 DCHECK(locations->Out().IsRegister());
4825 dst = locations->Out().AsRegister<Register>();
4826 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004827 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004828 } else {
4829 DCHECK(locations->Out().IsFpuRegister());
4830 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4831 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004832 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004833 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004834 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004835 }
4836 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004837 }
4838
4839 if (is_volatile) {
4840 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4841 }
4842}
4843
4844void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4845 Primitive::Type field_type = field_info.GetFieldType();
4846 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4847 bool generate_volatile = field_info.IsVolatile() && is_wide;
4848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004849 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004850
4851 locations->SetInAt(0, Location::RequiresRegister());
4852 if (generate_volatile) {
4853 InvokeRuntimeCallingConvention calling_convention;
4854 // need A0 to hold base + offset
4855 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4856 if (field_type == Primitive::kPrimLong) {
4857 locations->SetInAt(1, Location::RegisterPairLocation(
4858 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4859 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004860 // Use Location::Any() to prevent situations when running out of available fp registers.
4861 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004862 // Pass FP parameters in core registers.
4863 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4864 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4865 }
4866 } else {
4867 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004868 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004869 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004870 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004871 }
4872 }
4873}
4874
4875void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4876 const FieldInfo& field_info,
4877 uint32_t dex_pc) {
4878 Primitive::Type type = field_info.GetFieldType();
4879 LocationSummary* locations = instruction->GetLocations();
4880 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004881 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004882 StoreOperandType store_type = kStoreByte;
4883 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004884 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004885 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004886
4887 switch (type) {
4888 case Primitive::kPrimBoolean:
4889 case Primitive::kPrimByte:
4890 store_type = kStoreByte;
4891 break;
4892 case Primitive::kPrimShort:
4893 case Primitive::kPrimChar:
4894 store_type = kStoreHalfword;
4895 break;
4896 case Primitive::kPrimInt:
4897 case Primitive::kPrimFloat:
4898 case Primitive::kPrimNot:
4899 store_type = kStoreWord;
4900 break;
4901 case Primitive::kPrimLong:
4902 case Primitive::kPrimDouble:
4903 store_type = kStoreDoubleword;
4904 break;
4905 case Primitive::kPrimVoid:
4906 LOG(FATAL) << "Unreachable type " << type;
4907 UNREACHABLE();
4908 }
4909
4910 if (is_volatile) {
4911 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4912 }
4913
4914 if (is_volatile && store_type == kStoreDoubleword) {
4915 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004916 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004917 // Do implicit Null check.
4918 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4919 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4920 if (type == Primitive::kPrimDouble) {
4921 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004922 if (value_location.IsFpuRegister()) {
4923 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4924 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004925 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004926 value_location.AsFpuRegister<FRegister>());
4927 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004928 __ LoadFromOffset(kLoadWord,
4929 locations->GetTemp(1).AsRegister<Register>(),
4930 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004931 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004932 __ LoadFromOffset(kLoadWord,
4933 locations->GetTemp(2).AsRegister<Register>(),
4934 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004935 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004936 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004937 DCHECK(value_location.IsConstant());
4938 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4939 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004940 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4941 locations->GetTemp(1).AsRegister<Register>(),
4942 value);
4943 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004944 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004945 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004946 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4947 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004948 if (value_location.IsConstant()) {
4949 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4950 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4951 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004952 Register src;
4953 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004954 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004955 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004956 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004957 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004958 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004959 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004960 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004961 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004962 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004963 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004964 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004965 }
4966 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004967 }
4968
4969 // TODO: memory barriers?
4970 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004971 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004972 codegen_->MarkGCCard(obj, src);
4973 }
4974
4975 if (is_volatile) {
4976 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4977 }
4978}
4979
4980void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4981 HandleFieldGet(instruction, instruction->GetFieldInfo());
4982}
4983
4984void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4985 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4986}
4987
4988void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4989 HandleFieldSet(instruction, instruction->GetFieldInfo());
4990}
4991
4992void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4993 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4994}
4995
Alexey Frunze06a46c42016-07-19 15:00:40 -07004996void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
4997 HInstruction* instruction ATTRIBUTE_UNUSED,
4998 Location root,
4999 Register obj,
5000 uint32_t offset) {
5001 Register root_reg = root.AsRegister<Register>();
5002 if (kEmitCompilerReadBarrier) {
5003 UNIMPLEMENTED(FATAL) << "for read barrier";
5004 } else {
5005 // Plain GC root load with no read barrier.
5006 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5007 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5008 // Note that GC roots are not affected by heap poisoning, thus we
5009 // do not have to unpoison `root_reg` here.
5010 }
5011}
5012
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005013void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5014 LocationSummary::CallKind call_kind =
5015 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5016 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5017 locations->SetInAt(0, Location::RequiresRegister());
5018 locations->SetInAt(1, Location::RequiresRegister());
5019 // The output does overlap inputs.
5020 // Note that TypeCheckSlowPathMIPS uses this register too.
5021 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5022}
5023
5024void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5025 LocationSummary* locations = instruction->GetLocations();
5026 Register obj = locations->InAt(0).AsRegister<Register>();
5027 Register cls = locations->InAt(1).AsRegister<Register>();
5028 Register out = locations->Out().AsRegister<Register>();
5029
5030 MipsLabel done;
5031
5032 // Return 0 if `obj` is null.
5033 // TODO: Avoid this check if we know `obj` is not null.
5034 __ Move(out, ZERO);
5035 __ Beqz(obj, &done);
5036
5037 // Compare the class of `obj` with `cls`.
5038 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5039 if (instruction->IsExactCheck()) {
5040 // Classes must be equal for the instanceof to succeed.
5041 __ Xor(out, out, cls);
5042 __ Sltiu(out, out, 1);
5043 } else {
5044 // If the classes are not equal, we go into a slow path.
5045 DCHECK(locations->OnlyCallsOnSlowPath());
5046 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5047 codegen_->AddSlowPath(slow_path);
5048 __ Bne(out, cls, slow_path->GetEntryLabel());
5049 __ LoadConst32(out, 1);
5050 __ Bind(slow_path->GetExitLabel());
5051 }
5052
5053 __ Bind(&done);
5054}
5055
5056void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5058 locations->SetOut(Location::ConstantLocation(constant));
5059}
5060
5061void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5062 // Will be generated at use site.
5063}
5064
5065void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5067 locations->SetOut(Location::ConstantLocation(constant));
5068}
5069
5070void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5071 // Will be generated at use site.
5072}
5073
5074void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5075 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5076 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5077}
5078
5079void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5080 HandleInvoke(invoke);
5081 // The register T0 is required to be used for the hidden argument in
5082 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5083 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5084}
5085
5086void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5087 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5088 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005089 Location receiver = invoke->GetLocations()->InAt(0);
5090 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005091 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005092
5093 // Set the hidden argument.
5094 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5095 invoke->GetDexMethodIndex());
5096
5097 // temp = object->GetClass();
5098 if (receiver.IsStackSlot()) {
5099 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5100 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5101 } else {
5102 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5103 }
5104 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005105 __ LoadFromOffset(kLoadWord, temp, temp,
5106 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5107 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005108 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005109 // temp = temp->GetImtEntryAt(method_offset);
5110 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5111 // T9 = temp->GetEntryPoint();
5112 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5113 // T9();
5114 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005115 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005116 DCHECK(!codegen_->IsLeafMethod());
5117 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5118}
5119
5120void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005121 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5122 if (intrinsic.TryDispatch(invoke)) {
5123 return;
5124 }
5125
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005126 HandleInvoke(invoke);
5127}
5128
5129void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005130 // Explicit clinit checks triggered by static invokes must have been pruned by
5131 // art::PrepareForRegisterAllocation.
5132 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005133
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005134 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5135 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5136 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5137
5138 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
5139 // R6 has PC-relative addressing.
5140 bool has_extra_input = !isR6 &&
5141 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5142 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
5143
5144 if (invoke->HasPcRelativeDexCache()) {
5145 // kDexCachePcRelative is mutually exclusive with
5146 // kDirectAddressWithFixup/kCallDirectWithFixup.
5147 CHECK(!has_extra_input);
5148 has_extra_input = true;
5149 }
5150
Chris Larsen701566a2015-10-27 15:29:13 -07005151 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5152 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005153 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5154 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5155 }
Chris Larsen701566a2015-10-27 15:29:13 -07005156 return;
5157 }
5158
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005159 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005160
5161 // Add the extra input register if either the dex cache array base register
5162 // or the PC-relative base register for accessing literals is needed.
5163 if (has_extra_input) {
5164 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5165 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005166}
5167
Chris Larsen701566a2015-10-27 15:29:13 -07005168static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005169 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005170 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5171 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005172 return true;
5173 }
5174 return false;
5175}
5176
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005177HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005178 HLoadString::LoadKind desired_string_load_kind) {
5179 if (kEmitCompilerReadBarrier) {
5180 UNIMPLEMENTED(FATAL) << "for read barrier";
5181 }
5182 // We disable PC-relative load when there is an irreducible loop, as the optimization
5183 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005184 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5185 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005186 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5187 bool fallback_load = has_irreducible_loops;
5188 switch (desired_string_load_kind) {
5189 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5190 DCHECK(!GetCompilerOptions().GetCompilePic());
5191 break;
5192 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5193 DCHECK(GetCompilerOptions().GetCompilePic());
5194 break;
5195 case HLoadString::LoadKind::kBootImageAddress:
5196 break;
5197 case HLoadString::LoadKind::kDexCacheAddress:
5198 DCHECK(Runtime::Current()->UseJitCompilation());
5199 fallback_load = false;
5200 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005201 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005202 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005203 break;
5204 case HLoadString::LoadKind::kDexCacheViaMethod:
5205 fallback_load = false;
5206 break;
5207 }
5208 if (fallback_load) {
5209 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5210 }
5211 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005212}
5213
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005214HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5215 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005216 if (kEmitCompilerReadBarrier) {
5217 UNIMPLEMENTED(FATAL) << "for read barrier";
5218 }
5219 // We disable pc-relative load when there is an irreducible loop, as the optimization
5220 // is incompatible with it.
5221 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5222 bool fallback_load = has_irreducible_loops;
5223 switch (desired_class_load_kind) {
5224 case HLoadClass::LoadKind::kReferrersClass:
5225 fallback_load = false;
5226 break;
5227 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5228 DCHECK(!GetCompilerOptions().GetCompilePic());
5229 break;
5230 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5231 DCHECK(GetCompilerOptions().GetCompilePic());
5232 break;
5233 case HLoadClass::LoadKind::kBootImageAddress:
5234 break;
5235 case HLoadClass::LoadKind::kDexCacheAddress:
5236 DCHECK(Runtime::Current()->UseJitCompilation());
5237 fallback_load = false;
5238 break;
5239 case HLoadClass::LoadKind::kDexCachePcRelative:
5240 DCHECK(!Runtime::Current()->UseJitCompilation());
5241 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5242 // with irreducible loops.
5243 break;
5244 case HLoadClass::LoadKind::kDexCacheViaMethod:
5245 fallback_load = false;
5246 break;
5247 }
5248 if (fallback_load) {
5249 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5250 }
5251 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005252}
5253
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005254Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5255 Register temp) {
5256 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5257 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5258 if (!invoke->GetLocations()->Intrinsified()) {
5259 return location.AsRegister<Register>();
5260 }
5261 // For intrinsics we allow any location, so it may be on the stack.
5262 if (!location.IsRegister()) {
5263 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5264 return temp;
5265 }
5266 // For register locations, check if the register was saved. If so, get it from the stack.
5267 // Note: There is a chance that the register was saved but not overwritten, so we could
5268 // save one load. However, since this is just an intrinsic slow path we prefer this
5269 // simple and more robust approach rather that trying to determine if that's the case.
5270 SlowPathCode* slow_path = GetCurrentSlowPath();
5271 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5272 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5273 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5274 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5275 return temp;
5276 }
5277 return location.AsRegister<Register>();
5278}
5279
Vladimir Markodc151b22015-10-15 18:02:30 +01005280HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5281 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005282 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005283 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5284 // We disable PC-relative load when there is an irreducible loop, as the optimization
5285 // is incompatible with it.
5286 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5287 bool fallback_load = true;
5288 bool fallback_call = true;
5289 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005290 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5291 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005292 fallback_load = has_irreducible_loops;
5293 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005294 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005295 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005296 break;
5297 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005298 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005299 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005300 fallback_call = has_irreducible_loops;
5301 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005302 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005303 // TODO: Implement this type.
5304 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005305 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005306 fallback_call = false;
5307 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005308 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005309 if (fallback_load) {
5310 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5311 dispatch_info.method_load_data = 0;
5312 }
5313 if (fallback_call) {
5314 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5315 dispatch_info.direct_code_ptr = 0;
5316 }
5317 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005318}
5319
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005320void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5321 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005322 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005323 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5324 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5325 bool isR6 = isa_features_.IsR6();
5326 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
5327 // R6 has PC-relative addressing.
5328 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
5329 (!isR6 &&
5330 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5331 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
5332 Register base_reg = has_extra_input
5333 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5334 : ZERO;
5335
5336 // For better instruction scheduling we load the direct code pointer before the method pointer.
5337 switch (code_ptr_location) {
5338 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5339 // T9 = invoke->GetDirectCodePtr();
5340 __ LoadConst32(T9, invoke->GetDirectCodePtr());
5341 break;
5342 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5343 // T9 = code address from literal pool with link-time patch.
5344 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5345 break;
5346 default:
5347 break;
5348 }
5349
5350 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005351 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005352 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005353 uint32_t offset =
5354 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005355 __ LoadFromOffset(kLoadWord,
5356 temp.AsRegister<Register>(),
5357 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005358 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005359 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005360 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005361 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005362 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005363 break;
5364 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5365 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5366 break;
5367 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005368 __ LoadLiteral(temp.AsRegister<Register>(),
5369 base_reg,
5370 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5371 break;
5372 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5373 HMipsDexCacheArraysBase* base =
5374 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5375 int32_t offset =
5376 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5377 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5378 break;
5379 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005380 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005381 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005382 Register reg = temp.AsRegister<Register>();
5383 Register method_reg;
5384 if (current_method.IsRegister()) {
5385 method_reg = current_method.AsRegister<Register>();
5386 } else {
5387 // TODO: use the appropriate DCHECK() here if possible.
5388 // DCHECK(invoke->GetLocations()->Intrinsified());
5389 DCHECK(!current_method.IsValid());
5390 method_reg = reg;
5391 __ Lw(reg, SP, kCurrentMethodStackOffset);
5392 }
5393
5394 // temp = temp->dex_cache_resolved_methods_;
5395 __ LoadFromOffset(kLoadWord,
5396 reg,
5397 method_reg,
5398 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005399 // temp = temp[index_in_cache];
5400 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5401 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005402 __ LoadFromOffset(kLoadWord,
5403 reg,
5404 reg,
5405 CodeGenerator::GetCachePointerOffset(index_in_cache));
5406 break;
5407 }
5408 }
5409
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005410 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005411 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005412 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005413 break;
5414 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005415 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5416 // T9 prepared above for better instruction scheduling.
5417 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005418 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005419 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005420 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005421 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005422 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01005423 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5424 LOG(FATAL) << "Unsupported";
5425 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005426 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5427 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005428 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005429 T9,
5430 callee_method.AsRegister<Register>(),
5431 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005432 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005433 // T9()
5434 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005435 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005436 break;
5437 }
5438 DCHECK(!IsLeafMethod());
5439}
5440
5441void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005442 // Explicit clinit checks triggered by static invokes must have been pruned by
5443 // art::PrepareForRegisterAllocation.
5444 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005445
5446 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5447 return;
5448 }
5449
5450 LocationSummary* locations = invoke->GetLocations();
5451 codegen_->GenerateStaticOrDirectCall(invoke,
5452 locations->HasTemps()
5453 ? locations->GetTemp(0)
5454 : Location::NoLocation());
5455 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5456}
5457
Chris Larsen3acee732015-11-18 13:31:08 -08005458void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005459 // Use the calling convention instead of the location of the receiver, as
5460 // intrinsics may have put the receiver in a different register. In the intrinsics
5461 // slow path, the arguments have been moved to the right place, so here we are
5462 // guaranteed that the receiver is the first register of the calling convention.
5463 InvokeDexCallingConvention calling_convention;
5464 Register receiver = calling_convention.GetRegisterAt(0);
5465
Chris Larsen3acee732015-11-18 13:31:08 -08005466 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005467 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5468 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5469 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005470 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005471
5472 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005473 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005474 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005475 // temp = temp->GetMethodAt(method_offset);
5476 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5477 // T9 = temp->GetEntryPoint();
5478 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5479 // T9();
5480 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005481 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005482}
5483
5484void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5485 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5486 return;
5487 }
5488
5489 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005490 DCHECK(!codegen_->IsLeafMethod());
5491 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5492}
5493
5494void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005495 if (cls->NeedsAccessCheck()) {
5496 InvokeRuntimeCallingConvention calling_convention;
5497 CodeGenerator::CreateLoadClassLocationSummary(
5498 cls,
5499 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5500 Location::RegisterLocation(V0),
5501 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
5502 return;
5503 }
5504
5505 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5506 ? LocationSummary::kCallOnSlowPath
5507 : LocationSummary::kNoCall;
5508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5509 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5510 switch (load_kind) {
5511 // We need an extra register for PC-relative literals on R2.
5512 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5513 case HLoadClass::LoadKind::kBootImageAddress:
5514 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5515 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5516 break;
5517 }
5518 FALLTHROUGH_INTENDED;
5519 // We need an extra register for PC-relative dex cache accesses.
5520 case HLoadClass::LoadKind::kDexCachePcRelative:
5521 case HLoadClass::LoadKind::kReferrersClass:
5522 case HLoadClass::LoadKind::kDexCacheViaMethod:
5523 locations->SetInAt(0, Location::RequiresRegister());
5524 break;
5525 default:
5526 break;
5527 }
5528 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005529}
5530
5531void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
5532 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01005533 if (cls->NeedsAccessCheck()) {
5534 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005535 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005536 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01005537 return;
5538 }
5539
Alexey Frunze06a46c42016-07-19 15:00:40 -07005540 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5541 Location out_loc = locations->Out();
5542 Register out = out_loc.AsRegister<Register>();
5543 Register base_or_current_method_reg;
5544 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5545 switch (load_kind) {
5546 // We need an extra register for PC-relative literals on R2.
5547 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5548 case HLoadClass::LoadKind::kBootImageAddress:
5549 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5550 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5551 break;
5552 // We need an extra register for PC-relative dex cache accesses.
5553 case HLoadClass::LoadKind::kDexCachePcRelative:
5554 case HLoadClass::LoadKind::kReferrersClass:
5555 case HLoadClass::LoadKind::kDexCacheViaMethod:
5556 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5557 break;
5558 default:
5559 base_or_current_method_reg = ZERO;
5560 break;
5561 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005562
Alexey Frunze06a46c42016-07-19 15:00:40 -07005563 bool generate_null_check = false;
5564 switch (load_kind) {
5565 case HLoadClass::LoadKind::kReferrersClass: {
5566 DCHECK(!cls->CanCallRuntime());
5567 DCHECK(!cls->MustGenerateClinitCheck());
5568 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5569 GenerateGcRootFieldLoad(cls,
5570 out_loc,
5571 base_or_current_method_reg,
5572 ArtMethod::DeclaringClassOffset().Int32Value());
5573 break;
5574 }
5575 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5576 DCHECK(!kEmitCompilerReadBarrier);
5577 __ LoadLiteral(out,
5578 base_or_current_method_reg,
5579 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5580 cls->GetTypeIndex()));
5581 break;
5582 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5583 DCHECK(!kEmitCompilerReadBarrier);
5584 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5585 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005586 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005587 break;
5588 }
5589 case HLoadClass::LoadKind::kBootImageAddress: {
5590 DCHECK(!kEmitCompilerReadBarrier);
5591 DCHECK_NE(cls->GetAddress(), 0u);
5592 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5593 __ LoadLiteral(out,
5594 base_or_current_method_reg,
5595 codegen_->DeduplicateBootImageAddressLiteral(address));
5596 break;
5597 }
5598 case HLoadClass::LoadKind::kDexCacheAddress: {
5599 DCHECK_NE(cls->GetAddress(), 0u);
5600 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5601 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5602 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5603 int16_t offset = Low16Bits(address);
5604 uint32_t base_address = address - offset; // This accounts for offset sign extension.
5605 __ Lui(out, High16Bits(base_address));
5606 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5607 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5608 generate_null_check = !cls->IsInDexCache();
5609 break;
5610 }
5611 case HLoadClass::LoadKind::kDexCachePcRelative: {
5612 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
5613 int32_t offset =
5614 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5615 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5616 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
5617 generate_null_check = !cls->IsInDexCache();
5618 break;
5619 }
5620 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5621 // /* GcRoot<mirror::Class>[] */ out =
5622 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5623 __ LoadFromOffset(kLoadWord,
5624 out,
5625 base_or_current_method_reg,
5626 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5627 // /* GcRoot<mirror::Class> */ out = out[type_index]
5628 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5629 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5630 generate_null_check = !cls->IsInDexCache();
5631 }
5632 }
5633
5634 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5635 DCHECK(cls->CanCallRuntime());
5636 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5637 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5638 codegen_->AddSlowPath(slow_path);
5639 if (generate_null_check) {
5640 __ Beqz(out, slow_path->GetEntryLabel());
5641 }
5642 if (cls->MustGenerateClinitCheck()) {
5643 GenerateClassInitializationCheck(slow_path, out);
5644 } else {
5645 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005646 }
5647 }
5648}
5649
5650static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005651 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005652}
5653
5654void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5655 LocationSummary* locations =
5656 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5657 locations->SetOut(Location::RequiresRegister());
5658}
5659
5660void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5661 Register out = load->GetLocations()->Out().AsRegister<Register>();
5662 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5663}
5664
5665void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5666 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5667}
5668
5669void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5670 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5671}
5672
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005673void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005674 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00005675 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5676 ? LocationSummary::kCallOnMainOnly
5677 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005678 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005680 HLoadString::LoadKind load_kind = load->GetLoadKind();
5681 switch (load_kind) {
5682 // We need an extra register for PC-relative literals on R2.
5683 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5684 case HLoadString::LoadKind::kBootImageAddress:
5685 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005686 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005687 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5688 break;
5689 }
5690 FALLTHROUGH_INTENDED;
5691 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005692 case HLoadString::LoadKind::kDexCacheViaMethod:
5693 locations->SetInAt(0, Location::RequiresRegister());
5694 break;
5695 default:
5696 break;
5697 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005698 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5699 InvokeRuntimeCallingConvention calling_convention;
5700 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5701 } else {
5702 locations->SetOut(Location::RequiresRegister());
5703 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005704}
5705
5706void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005707 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005708 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005709 Location out_loc = locations->Out();
5710 Register out = out_loc.AsRegister<Register>();
5711 Register base_or_current_method_reg;
5712 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5713 switch (load_kind) {
5714 // We need an extra register for PC-relative literals on R2.
5715 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5716 case HLoadString::LoadKind::kBootImageAddress:
5717 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005718 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005719 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5720 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005721 default:
5722 base_or_current_method_reg = ZERO;
5723 break;
5724 }
5725
5726 switch (load_kind) {
5727 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5728 DCHECK(!kEmitCompilerReadBarrier);
5729 __ LoadLiteral(out,
5730 base_or_current_method_reg,
5731 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5732 load->GetStringIndex()));
5733 return; // No dex cache slow path.
5734 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5735 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005736 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005737 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5738 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005739 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005740 return; // No dex cache slow path.
5741 }
5742 case HLoadString::LoadKind::kBootImageAddress: {
5743 DCHECK(!kEmitCompilerReadBarrier);
5744 DCHECK_NE(load->GetAddress(), 0u);
5745 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5746 __ LoadLiteral(out,
5747 base_or_current_method_reg,
5748 codegen_->DeduplicateBootImageAddressLiteral(address));
5749 return; // No dex cache slow path.
5750 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005751 case HLoadString::LoadKind::kBssEntry: {
5752 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5753 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5754 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5755 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5756 __ LoadFromOffset(kLoadWord, out, out, 0);
5757 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5758 codegen_->AddSlowPath(slow_path);
5759 __ Beqz(out, slow_path->GetEntryLabel());
5760 __ Bind(slow_path->GetExitLabel());
5761 return;
5762 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005763 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005764 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005765 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005766
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005767 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005768 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5769 InvokeRuntimeCallingConvention calling_convention;
5770 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5771 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5772 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005773}
5774
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005775void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5777 locations->SetOut(Location::ConstantLocation(constant));
5778}
5779
5780void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5781 // Will be generated at use site.
5782}
5783
5784void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5785 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005786 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005787 InvokeRuntimeCallingConvention calling_convention;
5788 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5789}
5790
5791void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5792 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005793 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005794 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5795 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005796 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005797 }
5798 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5799}
5800
5801void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5802 LocationSummary* locations =
5803 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5804 switch (mul->GetResultType()) {
5805 case Primitive::kPrimInt:
5806 case Primitive::kPrimLong:
5807 locations->SetInAt(0, Location::RequiresRegister());
5808 locations->SetInAt(1, Location::RequiresRegister());
5809 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5810 break;
5811
5812 case Primitive::kPrimFloat:
5813 case Primitive::kPrimDouble:
5814 locations->SetInAt(0, Location::RequiresFpuRegister());
5815 locations->SetInAt(1, Location::RequiresFpuRegister());
5816 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5817 break;
5818
5819 default:
5820 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5821 }
5822}
5823
5824void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5825 Primitive::Type type = instruction->GetType();
5826 LocationSummary* locations = instruction->GetLocations();
5827 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5828
5829 switch (type) {
5830 case Primitive::kPrimInt: {
5831 Register dst = locations->Out().AsRegister<Register>();
5832 Register lhs = locations->InAt(0).AsRegister<Register>();
5833 Register rhs = locations->InAt(1).AsRegister<Register>();
5834
5835 if (isR6) {
5836 __ MulR6(dst, lhs, rhs);
5837 } else {
5838 __ MulR2(dst, lhs, rhs);
5839 }
5840 break;
5841 }
5842 case Primitive::kPrimLong: {
5843 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5844 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5845 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5846 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5847 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5848 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5849
5850 // Extra checks to protect caused by the existance of A1_A2.
5851 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5852 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5853 DCHECK_NE(dst_high, lhs_low);
5854 DCHECK_NE(dst_high, rhs_low);
5855
5856 // A_B * C_D
5857 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5858 // dst_lo: [ low(B*D) ]
5859 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5860
5861 if (isR6) {
5862 __ MulR6(TMP, lhs_high, rhs_low);
5863 __ MulR6(dst_high, lhs_low, rhs_high);
5864 __ Addu(dst_high, dst_high, TMP);
5865 __ MuhuR6(TMP, lhs_low, rhs_low);
5866 __ Addu(dst_high, dst_high, TMP);
5867 __ MulR6(dst_low, lhs_low, rhs_low);
5868 } else {
5869 __ MulR2(TMP, lhs_high, rhs_low);
5870 __ MulR2(dst_high, lhs_low, rhs_high);
5871 __ Addu(dst_high, dst_high, TMP);
5872 __ MultuR2(lhs_low, rhs_low);
5873 __ Mfhi(TMP);
5874 __ Addu(dst_high, dst_high, TMP);
5875 __ Mflo(dst_low);
5876 }
5877 break;
5878 }
5879 case Primitive::kPrimFloat:
5880 case Primitive::kPrimDouble: {
5881 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5882 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5883 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5884 if (type == Primitive::kPrimFloat) {
5885 __ MulS(dst, lhs, rhs);
5886 } else {
5887 __ MulD(dst, lhs, rhs);
5888 }
5889 break;
5890 }
5891 default:
5892 LOG(FATAL) << "Unexpected mul type " << type;
5893 }
5894}
5895
5896void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5897 LocationSummary* locations =
5898 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5899 switch (neg->GetResultType()) {
5900 case Primitive::kPrimInt:
5901 case Primitive::kPrimLong:
5902 locations->SetInAt(0, Location::RequiresRegister());
5903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5904 break;
5905
5906 case Primitive::kPrimFloat:
5907 case Primitive::kPrimDouble:
5908 locations->SetInAt(0, Location::RequiresFpuRegister());
5909 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5910 break;
5911
5912 default:
5913 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5914 }
5915}
5916
5917void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5918 Primitive::Type type = instruction->GetType();
5919 LocationSummary* locations = instruction->GetLocations();
5920
5921 switch (type) {
5922 case Primitive::kPrimInt: {
5923 Register dst = locations->Out().AsRegister<Register>();
5924 Register src = locations->InAt(0).AsRegister<Register>();
5925 __ Subu(dst, ZERO, src);
5926 break;
5927 }
5928 case Primitive::kPrimLong: {
5929 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5930 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5931 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5932 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5933 __ Subu(dst_low, ZERO, src_low);
5934 __ Sltu(TMP, ZERO, dst_low);
5935 __ Subu(dst_high, ZERO, src_high);
5936 __ Subu(dst_high, dst_high, TMP);
5937 break;
5938 }
5939 case Primitive::kPrimFloat:
5940 case Primitive::kPrimDouble: {
5941 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5942 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5943 if (type == Primitive::kPrimFloat) {
5944 __ NegS(dst, src);
5945 } else {
5946 __ NegD(dst, src);
5947 }
5948 break;
5949 }
5950 default:
5951 LOG(FATAL) << "Unexpected neg type " << type;
5952 }
5953}
5954
5955void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5956 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005958 InvokeRuntimeCallingConvention calling_convention;
5959 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5960 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5961 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5962 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5963}
5964
5965void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5966 InvokeRuntimeCallingConvention calling_convention;
5967 Register current_method_register = calling_convention.GetRegisterAt(2);
5968 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5969 // Move an uint16_t value to a register.
5970 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005971 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005972 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5973 void*, uint32_t, int32_t, ArtMethod*>();
5974}
5975
5976void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5977 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005979 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005980 if (instruction->IsStringAlloc()) {
5981 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5982 } else {
5983 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5984 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5985 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005986 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5987}
5988
5989void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005990 if (instruction->IsStringAlloc()) {
5991 // String is allocated through StringFactory. Call NewEmptyString entry point.
5992 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005993 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005994 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5995 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5996 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005997 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005998 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5999 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006000 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00006001 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
6002 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006003}
6004
6005void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6007 locations->SetInAt(0, Location::RequiresRegister());
6008 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6009}
6010
6011void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6012 Primitive::Type type = instruction->GetType();
6013 LocationSummary* locations = instruction->GetLocations();
6014
6015 switch (type) {
6016 case Primitive::kPrimInt: {
6017 Register dst = locations->Out().AsRegister<Register>();
6018 Register src = locations->InAt(0).AsRegister<Register>();
6019 __ Nor(dst, src, ZERO);
6020 break;
6021 }
6022
6023 case Primitive::kPrimLong: {
6024 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6025 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6026 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6027 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6028 __ Nor(dst_high, src_high, ZERO);
6029 __ Nor(dst_low, src_low, ZERO);
6030 break;
6031 }
6032
6033 default:
6034 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6035 }
6036}
6037
6038void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6039 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6040 locations->SetInAt(0, Location::RequiresRegister());
6041 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6042}
6043
6044void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6045 LocationSummary* locations = instruction->GetLocations();
6046 __ Xori(locations->Out().AsRegister<Register>(),
6047 locations->InAt(0).AsRegister<Register>(),
6048 1);
6049}
6050
6051void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006052 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6053 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006054}
6055
Calin Juravle2ae48182016-03-16 14:05:09 +00006056void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6057 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006058 return;
6059 }
6060 Location obj = instruction->GetLocations()->InAt(0);
6061
6062 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006063 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006064}
6065
Calin Juravle2ae48182016-03-16 14:05:09 +00006066void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006067 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006068 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006069
6070 Location obj = instruction->GetLocations()->InAt(0);
6071
6072 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6073}
6074
6075void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006076 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006077}
6078
6079void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6080 HandleBinaryOp(instruction);
6081}
6082
6083void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6084 HandleBinaryOp(instruction);
6085}
6086
6087void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6088 LOG(FATAL) << "Unreachable";
6089}
6090
6091void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6092 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6093}
6094
6095void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6097 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6098 if (location.IsStackSlot()) {
6099 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6100 } else if (location.IsDoubleStackSlot()) {
6101 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6102 }
6103 locations->SetOut(location);
6104}
6105
6106void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6107 ATTRIBUTE_UNUSED) {
6108 // Nothing to do, the parameter is already at its location.
6109}
6110
6111void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6112 LocationSummary* locations =
6113 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6114 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6115}
6116
6117void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6118 ATTRIBUTE_UNUSED) {
6119 // Nothing to do, the method is already at its location.
6120}
6121
6122void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006124 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006125 locations->SetInAt(i, Location::Any());
6126 }
6127 locations->SetOut(Location::Any());
6128}
6129
6130void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6131 LOG(FATAL) << "Unreachable";
6132}
6133
6134void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6135 Primitive::Type type = rem->GetResultType();
6136 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006137 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006138 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6139
6140 switch (type) {
6141 case Primitive::kPrimInt:
6142 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006143 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006144 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6145 break;
6146
6147 case Primitive::kPrimLong: {
6148 InvokeRuntimeCallingConvention calling_convention;
6149 locations->SetInAt(0, Location::RegisterPairLocation(
6150 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6151 locations->SetInAt(1, Location::RegisterPairLocation(
6152 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6153 locations->SetOut(calling_convention.GetReturnLocation(type));
6154 break;
6155 }
6156
6157 case Primitive::kPrimFloat:
6158 case Primitive::kPrimDouble: {
6159 InvokeRuntimeCallingConvention calling_convention;
6160 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6161 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6162 locations->SetOut(calling_convention.GetReturnLocation(type));
6163 break;
6164 }
6165
6166 default:
6167 LOG(FATAL) << "Unexpected rem type " << type;
6168 }
6169}
6170
6171void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6172 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006173
6174 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006175 case Primitive::kPrimInt:
6176 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006177 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006178 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006179 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006180 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6181 break;
6182 }
6183 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006184 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006185 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006186 break;
6187 }
6188 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006189 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006190 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006191 break;
6192 }
6193 default:
6194 LOG(FATAL) << "Unexpected rem type " << type;
6195 }
6196}
6197
6198void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6199 memory_barrier->SetLocations(nullptr);
6200}
6201
6202void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6203 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6204}
6205
6206void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6207 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6208 Primitive::Type return_type = ret->InputAt(0)->GetType();
6209 locations->SetInAt(0, MipsReturnLocation(return_type));
6210}
6211
6212void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6213 codegen_->GenerateFrameExit();
6214}
6215
6216void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6217 ret->SetLocations(nullptr);
6218}
6219
6220void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6221 codegen_->GenerateFrameExit();
6222}
6223
Alexey Frunze92d90602015-12-18 18:16:36 -08006224void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6225 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006226}
6227
Alexey Frunze92d90602015-12-18 18:16:36 -08006228void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6229 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006230}
6231
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006232void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6233 HandleShift(shl);
6234}
6235
6236void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6237 HandleShift(shl);
6238}
6239
6240void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6241 HandleShift(shr);
6242}
6243
6244void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6245 HandleShift(shr);
6246}
6247
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006248void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6249 HandleBinaryOp(instruction);
6250}
6251
6252void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6253 HandleBinaryOp(instruction);
6254}
6255
6256void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6257 HandleFieldGet(instruction, instruction->GetFieldInfo());
6258}
6259
6260void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6261 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6262}
6263
6264void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6265 HandleFieldSet(instruction, instruction->GetFieldInfo());
6266}
6267
6268void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6269 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6270}
6271
6272void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6273 HUnresolvedInstanceFieldGet* instruction) {
6274 FieldAccessCallingConventionMIPS calling_convention;
6275 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6276 instruction->GetFieldType(),
6277 calling_convention);
6278}
6279
6280void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6281 HUnresolvedInstanceFieldGet* instruction) {
6282 FieldAccessCallingConventionMIPS calling_convention;
6283 codegen_->GenerateUnresolvedFieldAccess(instruction,
6284 instruction->GetFieldType(),
6285 instruction->GetFieldIndex(),
6286 instruction->GetDexPc(),
6287 calling_convention);
6288}
6289
6290void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6291 HUnresolvedInstanceFieldSet* instruction) {
6292 FieldAccessCallingConventionMIPS calling_convention;
6293 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6294 instruction->GetFieldType(),
6295 calling_convention);
6296}
6297
6298void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6299 HUnresolvedInstanceFieldSet* instruction) {
6300 FieldAccessCallingConventionMIPS calling_convention;
6301 codegen_->GenerateUnresolvedFieldAccess(instruction,
6302 instruction->GetFieldType(),
6303 instruction->GetFieldIndex(),
6304 instruction->GetDexPc(),
6305 calling_convention);
6306}
6307
6308void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6309 HUnresolvedStaticFieldGet* instruction) {
6310 FieldAccessCallingConventionMIPS calling_convention;
6311 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6312 instruction->GetFieldType(),
6313 calling_convention);
6314}
6315
6316void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6317 HUnresolvedStaticFieldGet* instruction) {
6318 FieldAccessCallingConventionMIPS calling_convention;
6319 codegen_->GenerateUnresolvedFieldAccess(instruction,
6320 instruction->GetFieldType(),
6321 instruction->GetFieldIndex(),
6322 instruction->GetDexPc(),
6323 calling_convention);
6324}
6325
6326void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6327 HUnresolvedStaticFieldSet* instruction) {
6328 FieldAccessCallingConventionMIPS calling_convention;
6329 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6330 instruction->GetFieldType(),
6331 calling_convention);
6332}
6333
6334void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6335 HUnresolvedStaticFieldSet* instruction) {
6336 FieldAccessCallingConventionMIPS calling_convention;
6337 codegen_->GenerateUnresolvedFieldAccess(instruction,
6338 instruction->GetFieldType(),
6339 instruction->GetFieldIndex(),
6340 instruction->GetDexPc(),
6341 calling_convention);
6342}
6343
6344void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006345 LocationSummary* locations =
6346 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006347 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006348}
6349
6350void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6351 HBasicBlock* block = instruction->GetBlock();
6352 if (block->GetLoopInformation() != nullptr) {
6353 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6354 // The back edge will generate the suspend check.
6355 return;
6356 }
6357 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6358 // The goto will generate the suspend check.
6359 return;
6360 }
6361 GenerateSuspendCheck(instruction, nullptr);
6362}
6363
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006364void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6365 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006367 InvokeRuntimeCallingConvention calling_convention;
6368 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6369}
6370
6371void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006372 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006373 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6374}
6375
6376void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6377 Primitive::Type input_type = conversion->GetInputType();
6378 Primitive::Type result_type = conversion->GetResultType();
6379 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006380 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006381
6382 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6383 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6384 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6385 }
6386
6387 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006388 if (!isR6 &&
6389 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6390 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006391 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 }
6393
6394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6395
6396 if (call_kind == LocationSummary::kNoCall) {
6397 if (Primitive::IsFloatingPointType(input_type)) {
6398 locations->SetInAt(0, Location::RequiresFpuRegister());
6399 } else {
6400 locations->SetInAt(0, Location::RequiresRegister());
6401 }
6402
6403 if (Primitive::IsFloatingPointType(result_type)) {
6404 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6405 } else {
6406 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6407 }
6408 } else {
6409 InvokeRuntimeCallingConvention calling_convention;
6410
6411 if (Primitive::IsFloatingPointType(input_type)) {
6412 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6413 } else {
6414 DCHECK_EQ(input_type, Primitive::kPrimLong);
6415 locations->SetInAt(0, Location::RegisterPairLocation(
6416 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6417 }
6418
6419 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6420 }
6421}
6422
6423void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6424 LocationSummary* locations = conversion->GetLocations();
6425 Primitive::Type result_type = conversion->GetResultType();
6426 Primitive::Type input_type = conversion->GetInputType();
6427 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006428 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006429
6430 DCHECK_NE(input_type, result_type);
6431
6432 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6433 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6434 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6435 Register src = locations->InAt(0).AsRegister<Register>();
6436
Alexey Frunzea871ef12016-06-27 15:20:11 -07006437 if (dst_low != src) {
6438 __ Move(dst_low, src);
6439 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006440 __ Sra(dst_high, src, 31);
6441 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6442 Register dst = locations->Out().AsRegister<Register>();
6443 Register src = (input_type == Primitive::kPrimLong)
6444 ? locations->InAt(0).AsRegisterPairLow<Register>()
6445 : locations->InAt(0).AsRegister<Register>();
6446
6447 switch (result_type) {
6448 case Primitive::kPrimChar:
6449 __ Andi(dst, src, 0xFFFF);
6450 break;
6451 case Primitive::kPrimByte:
6452 if (has_sign_extension) {
6453 __ Seb(dst, src);
6454 } else {
6455 __ Sll(dst, src, 24);
6456 __ Sra(dst, dst, 24);
6457 }
6458 break;
6459 case Primitive::kPrimShort:
6460 if (has_sign_extension) {
6461 __ Seh(dst, src);
6462 } else {
6463 __ Sll(dst, src, 16);
6464 __ Sra(dst, dst, 16);
6465 }
6466 break;
6467 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006468 if (dst != src) {
6469 __ Move(dst, src);
6470 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006471 break;
6472
6473 default:
6474 LOG(FATAL) << "Unexpected type conversion from " << input_type
6475 << " to " << result_type;
6476 }
6477 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006478 if (input_type == Primitive::kPrimLong) {
6479 if (isR6) {
6480 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6481 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6482 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6483 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6484 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6485 __ Mtc1(src_low, FTMP);
6486 __ Mthc1(src_high, FTMP);
6487 if (result_type == Primitive::kPrimFloat) {
6488 __ Cvtsl(dst, FTMP);
6489 } else {
6490 __ Cvtdl(dst, FTMP);
6491 }
6492 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006493 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6494 : kQuickL2d;
6495 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006496 if (result_type == Primitive::kPrimFloat) {
6497 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6498 } else {
6499 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6500 }
6501 }
6502 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006503 Register src = locations->InAt(0).AsRegister<Register>();
6504 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6505 __ Mtc1(src, FTMP);
6506 if (result_type == Primitive::kPrimFloat) {
6507 __ Cvtsw(dst, FTMP);
6508 } else {
6509 __ Cvtdw(dst, FTMP);
6510 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006511 }
6512 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6513 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006514 if (result_type == Primitive::kPrimLong) {
6515 if (isR6) {
6516 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6517 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6518 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6519 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6520 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6521 MipsLabel truncate;
6522 MipsLabel done;
6523
6524 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6525 // value when the input is either a NaN or is outside of the range of the output type
6526 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6527 // the same result.
6528 //
6529 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6530 // value of the output type if the input is outside of the range after the truncation or
6531 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6532 // results. This matches the desired float/double-to-int/long conversion exactly.
6533 //
6534 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6535 //
6536 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6537 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6538 // even though it must be NAN2008=1 on R6.
6539 //
6540 // The code takes care of the different behaviors by first comparing the input to the
6541 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6542 // If the input is greater than or equal to the minimum, it procedes to the truncate
6543 // instruction, which will handle such an input the same way irrespective of NAN2008.
6544 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6545 // in order to return either zero or the minimum value.
6546 //
6547 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6548 // truncate instruction for MIPS64R6.
6549 if (input_type == Primitive::kPrimFloat) {
6550 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6551 __ LoadConst32(TMP, min_val);
6552 __ Mtc1(TMP, FTMP);
6553 __ CmpLeS(FTMP, FTMP, src);
6554 } else {
6555 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6556 __ LoadConst32(TMP, High32Bits(min_val));
6557 __ Mtc1(ZERO, FTMP);
6558 __ Mthc1(TMP, FTMP);
6559 __ CmpLeD(FTMP, FTMP, src);
6560 }
6561
6562 __ Bc1nez(FTMP, &truncate);
6563
6564 if (input_type == Primitive::kPrimFloat) {
6565 __ CmpEqS(FTMP, src, src);
6566 } else {
6567 __ CmpEqD(FTMP, src, src);
6568 }
6569 __ Move(dst_low, ZERO);
6570 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6571 __ Mfc1(TMP, FTMP);
6572 __ And(dst_high, dst_high, TMP);
6573
6574 __ B(&done);
6575
6576 __ Bind(&truncate);
6577
6578 if (input_type == Primitive::kPrimFloat) {
6579 __ TruncLS(FTMP, src);
6580 } else {
6581 __ TruncLD(FTMP, src);
6582 }
6583 __ Mfc1(dst_low, FTMP);
6584 __ Mfhc1(dst_high, FTMP);
6585
6586 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006587 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006588 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6589 : kQuickD2l;
6590 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006591 if (input_type == Primitive::kPrimFloat) {
6592 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6593 } else {
6594 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6595 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006596 }
6597 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006598 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6599 Register dst = locations->Out().AsRegister<Register>();
6600 MipsLabel truncate;
6601 MipsLabel done;
6602
6603 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6604 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6605 // even though it must be NAN2008=1 on R6.
6606 //
6607 // For details see the large comment above for the truncation of float/double to long on R6.
6608 //
6609 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6610 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006611 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006612 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6613 __ LoadConst32(TMP, min_val);
6614 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006615 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006616 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6617 __ LoadConst32(TMP, High32Bits(min_val));
6618 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006619 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006620 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006621
6622 if (isR6) {
6623 if (input_type == Primitive::kPrimFloat) {
6624 __ CmpLeS(FTMP, FTMP, src);
6625 } else {
6626 __ CmpLeD(FTMP, FTMP, src);
6627 }
6628 __ Bc1nez(FTMP, &truncate);
6629
6630 if (input_type == Primitive::kPrimFloat) {
6631 __ CmpEqS(FTMP, src, src);
6632 } else {
6633 __ CmpEqD(FTMP, src, src);
6634 }
6635 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6636 __ Mfc1(TMP, FTMP);
6637 __ And(dst, dst, TMP);
6638 } else {
6639 if (input_type == Primitive::kPrimFloat) {
6640 __ ColeS(0, FTMP, src);
6641 } else {
6642 __ ColeD(0, FTMP, src);
6643 }
6644 __ Bc1t(0, &truncate);
6645
6646 if (input_type == Primitive::kPrimFloat) {
6647 __ CeqS(0, src, src);
6648 } else {
6649 __ CeqD(0, src, src);
6650 }
6651 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6652 __ Movf(dst, ZERO, 0);
6653 }
6654
6655 __ B(&done);
6656
6657 __ Bind(&truncate);
6658
6659 if (input_type == Primitive::kPrimFloat) {
6660 __ TruncWS(FTMP, src);
6661 } else {
6662 __ TruncWD(FTMP, src);
6663 }
6664 __ Mfc1(dst, FTMP);
6665
6666 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006667 }
6668 } else if (Primitive::IsFloatingPointType(result_type) &&
6669 Primitive::IsFloatingPointType(input_type)) {
6670 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6671 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6672 if (result_type == Primitive::kPrimFloat) {
6673 __ Cvtsd(dst, src);
6674 } else {
6675 __ Cvtds(dst, src);
6676 }
6677 } else {
6678 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6679 << " to " << result_type;
6680 }
6681}
6682
6683void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6684 HandleShift(ushr);
6685}
6686
6687void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6688 HandleShift(ushr);
6689}
6690
6691void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6692 HandleBinaryOp(instruction);
6693}
6694
6695void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6696 HandleBinaryOp(instruction);
6697}
6698
6699void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6700 // Nothing to do, this should be removed during prepare for register allocator.
6701 LOG(FATAL) << "Unreachable";
6702}
6703
6704void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6705 // Nothing to do, this should be removed during prepare for register allocator.
6706 LOG(FATAL) << "Unreachable";
6707}
6708
6709void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006710 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006711}
6712
6713void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006714 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006715}
6716
6717void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006718 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006719}
6720
6721void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006722 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006723}
6724
6725void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006726 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006727}
6728
6729void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006730 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731}
6732
6733void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006734 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006735}
6736
6737void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006738 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006739}
6740
6741void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006742 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006743}
6744
6745void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006746 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006747}
6748
6749void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006750 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006751}
6752
6753void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006754 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006755}
6756
6757void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006758 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006759}
6760
6761void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006762 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006763}
6764
6765void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006766 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006767}
6768
6769void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006770 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006771}
6772
6773void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006774 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006775}
6776
6777void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006778 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006779}
6780
6781void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006782 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006783}
6784
6785void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006786 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006787}
6788
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006789void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6790 LocationSummary* locations =
6791 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6792 locations->SetInAt(0, Location::RequiresRegister());
6793}
6794
Alexey Frunze96b66822016-09-10 02:32:44 -07006795void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6796 int32_t lower_bound,
6797 uint32_t num_entries,
6798 HBasicBlock* switch_block,
6799 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006800 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006801 Register temp_reg = TMP;
6802 __ Addiu32(temp_reg, value_reg, -lower_bound);
6803 // Jump to default if index is negative
6804 // Note: We don't check the case that index is positive while value < lower_bound, because in
6805 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6806 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6807
Alexey Frunze96b66822016-09-10 02:32:44 -07006808 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006809 // Jump to successors[0] if value == lower_bound.
6810 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6811 int32_t last_index = 0;
6812 for (; num_entries - last_index > 2; last_index += 2) {
6813 __ Addiu(temp_reg, temp_reg, -2);
6814 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6815 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6816 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6817 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6818 }
6819 if (num_entries - last_index == 2) {
6820 // The last missing case_value.
6821 __ Addiu(temp_reg, temp_reg, -1);
6822 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006823 }
6824
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006825 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006826 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006827 __ B(codegen_->GetLabelOf(default_block));
6828 }
6829}
6830
Alexey Frunze96b66822016-09-10 02:32:44 -07006831void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6832 Register constant_area,
6833 int32_t lower_bound,
6834 uint32_t num_entries,
6835 HBasicBlock* switch_block,
6836 HBasicBlock* default_block) {
6837 // Create a jump table.
6838 std::vector<MipsLabel*> labels(num_entries);
6839 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6840 for (uint32_t i = 0; i < num_entries; i++) {
6841 labels[i] = codegen_->GetLabelOf(successors[i]);
6842 }
6843 JumpTable* table = __ CreateJumpTable(std::move(labels));
6844
6845 // Is the value in range?
6846 __ Addiu32(TMP, value_reg, -lower_bound);
6847 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6848 __ Sltiu(AT, TMP, num_entries);
6849 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6850 } else {
6851 __ LoadConst32(AT, num_entries);
6852 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6853 }
6854
6855 // We are in the range of the table.
6856 // Load the target address from the jump table, indexing by the value.
6857 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6858 __ Sll(TMP, TMP, 2);
6859 __ Addu(TMP, TMP, AT);
6860 __ Lw(TMP, TMP, 0);
6861 // Compute the absolute target address by adding the table start address
6862 // (the table contains offsets to targets relative to its start).
6863 __ Addu(TMP, TMP, AT);
6864 // And jump.
6865 __ Jr(TMP);
6866 __ NopIfNoReordering();
6867}
6868
6869void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6870 int32_t lower_bound = switch_instr->GetStartValue();
6871 uint32_t num_entries = switch_instr->GetNumEntries();
6872 LocationSummary* locations = switch_instr->GetLocations();
6873 Register value_reg = locations->InAt(0).AsRegister<Register>();
6874 HBasicBlock* switch_block = switch_instr->GetBlock();
6875 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6876
6877 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6878 num_entries > kPackedSwitchJumpTableThreshold) {
6879 // R6 uses PC-relative addressing to access the jump table.
6880 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6881 // the jump table and it is implemented by changing HPackedSwitch to
6882 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6883 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6884 GenTableBasedPackedSwitch(value_reg,
6885 ZERO,
6886 lower_bound,
6887 num_entries,
6888 switch_block,
6889 default_block);
6890 } else {
6891 GenPackedSwitchWithCompares(value_reg,
6892 lower_bound,
6893 num_entries,
6894 switch_block,
6895 default_block);
6896 }
6897}
6898
6899void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6900 LocationSummary* locations =
6901 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6902 locations->SetInAt(0, Location::RequiresRegister());
6903 // Constant area pointer (HMipsComputeBaseMethodAddress).
6904 locations->SetInAt(1, Location::RequiresRegister());
6905}
6906
6907void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6908 int32_t lower_bound = switch_instr->GetStartValue();
6909 uint32_t num_entries = switch_instr->GetNumEntries();
6910 LocationSummary* locations = switch_instr->GetLocations();
6911 Register value_reg = locations->InAt(0).AsRegister<Register>();
6912 Register constant_area = locations->InAt(1).AsRegister<Register>();
6913 HBasicBlock* switch_block = switch_instr->GetBlock();
6914 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6915
6916 // This is an R2-only path. HPackedSwitch has been changed to
6917 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6918 // required to address the jump table relative to PC.
6919 GenTableBasedPackedSwitch(value_reg,
6920 constant_area,
6921 lower_bound,
6922 num_entries,
6923 switch_block,
6924 default_block);
6925}
6926
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006927void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6928 HMipsComputeBaseMethodAddress* insn) {
6929 LocationSummary* locations =
6930 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6931 locations->SetOut(Location::RequiresRegister());
6932}
6933
6934void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6935 HMipsComputeBaseMethodAddress* insn) {
6936 LocationSummary* locations = insn->GetLocations();
6937 Register reg = locations->Out().AsRegister<Register>();
6938
6939 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6940
6941 // Generate a dummy PC-relative call to obtain PC.
6942 __ Nal();
6943 // Grab the return address off RA.
6944 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006945 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006946
6947 // Remember this offset (the obtained PC value) for later use with constant area.
6948 __ BindPcRelBaseLabel();
6949}
6950
6951void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6952 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6953 locations->SetOut(Location::RequiresRegister());
6954}
6955
6956void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6957 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6958 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6959 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006960 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6961 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006962}
6963
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006964void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6965 // The trampoline uses the same calling convention as dex calling conventions,
6966 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6967 // the method_idx.
6968 HandleInvoke(invoke);
6969}
6970
6971void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6972 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6973}
6974
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006975void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6976 LocationSummary* locations =
6977 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6978 locations->SetInAt(0, Location::RequiresRegister());
6979 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006980}
6981
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006982void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6983 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006984 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006985 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006986 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006987 __ LoadFromOffset(kLoadWord,
6988 locations->Out().AsRegister<Register>(),
6989 locations->InAt(0).AsRegister<Register>(),
6990 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006991 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006992 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006993 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006994 __ LoadFromOffset(kLoadWord,
6995 locations->Out().AsRegister<Register>(),
6996 locations->InAt(0).AsRegister<Register>(),
6997 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006998 __ LoadFromOffset(kLoadWord,
6999 locations->Out().AsRegister<Register>(),
7000 locations->Out().AsRegister<Register>(),
7001 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007002 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007003}
7004
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007005#undef __
7006#undef QUICK_ENTRY_POINT
7007
7008} // namespace mips
7009} // namespace art