blob: b0737751a76639d376527fee0c2d18c26b046966 [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());
197 if (instruction_->CanThrowIntoCatchBlock()) {
198 // Live registers will be restored in the catch block if caught.
199 SaveLiveRegisters(codegen, instruction_->GetLocations());
200 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100201 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200202 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
203 }
204
205 bool IsFatal() const OVERRIDE { return true; }
206
207 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
208
209 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200210 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
211};
212
213class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
214 public:
215 LoadClassSlowPathMIPS(HLoadClass* cls,
216 HInstruction* at,
217 uint32_t dex_pc,
218 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000219 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200220 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
221 }
222
223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
224 LocationSummary* locations = at_->GetLocations();
225 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
226
227 __ Bind(GetEntryLabel());
228 SaveLiveRegisters(codegen, locations);
229
230 InvokeRuntimeCallingConvention calling_convention;
231 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
232
Serban Constantinescufca16662016-07-14 09:21:59 +0100233 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
234 : kQuickInitializeType;
235 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200236 if (do_clinit_) {
237 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
238 } else {
239 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
240 }
241
242 // Move the class to the desired location.
243 Location out = locations->Out();
244 if (out.IsValid()) {
245 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
246 Primitive::Type type = at_->GetType();
247 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
248 }
249
250 RestoreLiveRegisters(codegen, locations);
251 __ B(GetExitLabel());
252 }
253
254 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
255
256 private:
257 // The class this slow path will load.
258 HLoadClass* const cls_;
259
260 // The instruction where this slow path is happening.
261 // (Might be the load class or an initialization check).
262 HInstruction* const at_;
263
264 // The dex PC of `at_`.
265 const uint32_t dex_pc_;
266
267 // Whether to initialize the class.
268 const bool do_clinit_;
269
270 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
271};
272
273class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
274 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000275 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200276
277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
278 LocationSummary* locations = instruction_->GetLocations();
279 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
280 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
281
282 __ Bind(GetEntryLabel());
283 SaveLiveRegisters(codegen, locations);
284
285 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
287 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufca16662016-07-14 09:21:59 +0100288 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
290 Primitive::Type type = instruction_->GetType();
291 mips_codegen->MoveLocation(locations->Out(),
292 calling_convention.GetReturnLocation(type),
293 type);
294
295 RestoreLiveRegisters(codegen, locations);
296 __ B(GetExitLabel());
297 }
298
299 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
300
301 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200302 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
303};
304
305class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
306 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000307 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200308
309 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
310 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
311 __ Bind(GetEntryLabel());
312 if (instruction_->CanThrowIntoCatchBlock()) {
313 // Live registers will be restored in the catch block if caught.
314 SaveLiveRegisters(codegen, instruction_->GetLocations());
315 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100316 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200317 instruction_,
318 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100319 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200320 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
321 }
322
323 bool IsFatal() const OVERRIDE { return true; }
324
325 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
326
327 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200328 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
329};
330
331class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
332 public:
333 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000334 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335
336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
337 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
338 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100339 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200340 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 if (successor_ == nullptr) {
342 __ B(GetReturnLabel());
343 } else {
344 __ B(mips_codegen->GetLabelOf(successor_));
345 }
346 }
347
348 MipsLabel* GetReturnLabel() {
349 DCHECK(successor_ == nullptr);
350 return &return_label_;
351 }
352
353 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
354
355 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200356 // If not null, the block to branch to after the suspend check.
357 HBasicBlock* const successor_;
358
359 // If `successor_` is null, the label to branch to after the suspend check.
360 MipsLabel return_label_;
361
362 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
363};
364
365class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
366 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000367 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200368
369 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
370 LocationSummary* locations = instruction_->GetLocations();
371 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
372 uint32_t dex_pc = instruction_->GetDexPc();
373 DCHECK(instruction_->IsCheckCast()
374 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
375 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
376
377 __ Bind(GetEntryLabel());
378 SaveLiveRegisters(codegen, locations);
379
380 // We're moving two locations to locations that could overlap, so we need a parallel
381 // move resolver.
382 InvokeRuntimeCallingConvention calling_convention;
383 codegen->EmitParallelMoves(locations->InAt(1),
384 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
385 Primitive::kPrimNot,
386 object_class,
387 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
388 Primitive::kPrimNot);
389
390 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100391 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000392 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700393 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200394 Primitive::Type ret_type = instruction_->GetType();
395 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
396 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200397 } else {
398 DCHECK(instruction_->IsCheckCast());
Serban Constantinescufca16662016-07-14 09:21:59 +0100399 mips_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200400 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
401 }
402
403 RestoreLiveRegisters(codegen, locations);
404 __ B(GetExitLabel());
405 }
406
407 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
408
409 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
411};
412
413class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
414 public:
Aart Bik42249c32016-01-07 15:33:50 -0800415 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000416 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417
418 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800419 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, instruction_->GetLocations());
Serban Constantinescufca16662016-07-14 09:21:59 +0100422 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000423 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200424 }
425
426 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
427
428 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200429 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
430};
431
432CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
433 const MipsInstructionSetFeatures& isa_features,
434 const CompilerOptions& compiler_options,
435 OptimizingCompilerStats* stats)
436 : CodeGenerator(graph,
437 kNumberOfCoreRegisters,
438 kNumberOfFRegisters,
439 kNumberOfRegisterPairs,
440 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
441 arraysize(kCoreCalleeSaves)),
442 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
443 arraysize(kFpuCalleeSaves)),
444 compiler_options,
445 stats),
446 block_labels_(nullptr),
447 location_builder_(graph, this),
448 instruction_visitor_(graph, this),
449 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100450 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700451 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700452 uint32_literals_(std::less<uint32_t>(),
453 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700454 method_patches_(MethodReferenceComparator(),
455 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
456 call_patches_(MethodReferenceComparator(),
457 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700458 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
459 boot_image_string_patches_(StringReferenceValueComparator(),
460 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
461 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
462 boot_image_type_patches_(TypeReferenceValueComparator(),
463 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
464 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
465 boot_image_address_patches_(std::less<uint32_t>(),
466 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
467 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200468 // Save RA (containing the return address) to mimic Quick.
469 AddAllocatedRegister(Location::RegisterLocation(RA));
470}
471
472#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100473// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
474#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700475#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200476
477void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
478 // Ensure that we fix up branches.
479 __ FinalizeCode();
480
481 // Adjust native pc offsets in stack maps.
482 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
483 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
484 uint32_t new_position = __ GetAdjustedPosition(old_position);
485 DCHECK_GE(new_position, old_position);
486 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
487 }
488
489 // Adjust pc offsets for the disassembly information.
490 if (disasm_info_ != nullptr) {
491 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
492 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
493 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
494 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
495 it.second.start = __ GetAdjustedPosition(it.second.start);
496 it.second.end = __ GetAdjustedPosition(it.second.end);
497 }
498 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
499 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
500 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
501 }
502 }
503
504 CodeGenerator::Finalize(allocator);
505}
506
507MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
508 return codegen_->GetAssembler();
509}
510
511void ParallelMoveResolverMIPS::EmitMove(size_t index) {
512 DCHECK_LT(index, moves_.size());
513 MoveOperands* move = moves_[index];
514 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
515}
516
517void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
518 DCHECK_LT(index, moves_.size());
519 MoveOperands* move = moves_[index];
520 Primitive::Type type = move->GetType();
521 Location loc1 = move->GetDestination();
522 Location loc2 = move->GetSource();
523
524 DCHECK(!loc1.IsConstant());
525 DCHECK(!loc2.IsConstant());
526
527 if (loc1.Equals(loc2)) {
528 return;
529 }
530
531 if (loc1.IsRegister() && loc2.IsRegister()) {
532 // Swap 2 GPRs.
533 Register r1 = loc1.AsRegister<Register>();
534 Register r2 = loc2.AsRegister<Register>();
535 __ Move(TMP, r2);
536 __ Move(r2, r1);
537 __ Move(r1, TMP);
538 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
539 FRegister f1 = loc1.AsFpuRegister<FRegister>();
540 FRegister f2 = loc2.AsFpuRegister<FRegister>();
541 if (type == Primitive::kPrimFloat) {
542 __ MovS(FTMP, f2);
543 __ MovS(f2, f1);
544 __ MovS(f1, FTMP);
545 } else {
546 DCHECK_EQ(type, Primitive::kPrimDouble);
547 __ MovD(FTMP, f2);
548 __ MovD(f2, f1);
549 __ MovD(f1, FTMP);
550 }
551 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
552 (loc1.IsFpuRegister() && loc2.IsRegister())) {
553 // Swap FPR and GPR.
554 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
555 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
556 : loc2.AsFpuRegister<FRegister>();
557 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
558 : loc2.AsRegister<Register>();
559 __ Move(TMP, r2);
560 __ Mfc1(r2, f1);
561 __ Mtc1(TMP, f1);
562 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
563 // Swap 2 GPR register pairs.
564 Register r1 = loc1.AsRegisterPairLow<Register>();
565 Register r2 = loc2.AsRegisterPairLow<Register>();
566 __ Move(TMP, r2);
567 __ Move(r2, r1);
568 __ Move(r1, TMP);
569 r1 = loc1.AsRegisterPairHigh<Register>();
570 r2 = loc2.AsRegisterPairHigh<Register>();
571 __ Move(TMP, r2);
572 __ Move(r2, r1);
573 __ Move(r1, TMP);
574 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
575 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
576 // Swap FPR and GPR register pair.
577 DCHECK_EQ(type, Primitive::kPrimDouble);
578 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
579 : loc2.AsFpuRegister<FRegister>();
580 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
581 : loc2.AsRegisterPairLow<Register>();
582 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
583 : loc2.AsRegisterPairHigh<Register>();
584 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
585 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
586 // unpredictable and the following mfch1 will fail.
587 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800588 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200589 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800590 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200591 __ Move(r2_l, TMP);
592 __ Move(r2_h, AT);
593 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
594 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
595 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
596 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000597 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
598 (loc1.IsStackSlot() && loc2.IsRegister())) {
599 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
600 : loc2.AsRegister<Register>();
601 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
602 : loc2.GetStackIndex();
603 __ Move(TMP, reg);
604 __ LoadFromOffset(kLoadWord, reg, SP, offset);
605 __ StoreToOffset(kStoreWord, TMP, SP, offset);
606 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
607 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
608 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
609 : loc2.AsRegisterPairLow<Register>();
610 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
611 : loc2.AsRegisterPairHigh<Register>();
612 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
613 : loc2.GetStackIndex();
614 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
615 : loc2.GetHighStackIndex(kMipsWordSize);
616 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000617 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000618 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000619 __ Move(TMP, reg_h);
620 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
621 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200622 } else {
623 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
624 }
625}
626
627void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
628 __ Pop(static_cast<Register>(reg));
629}
630
631void ParallelMoveResolverMIPS::SpillScratch(int reg) {
632 __ Push(static_cast<Register>(reg));
633}
634
635void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
636 // Allocate a scratch register other than TMP, if available.
637 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
638 // automatically unspilled when the scratch scope object is destroyed).
639 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
640 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
641 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
642 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
643 __ LoadFromOffset(kLoadWord,
644 Register(ensure_scratch.GetRegister()),
645 SP,
646 index1 + stack_offset);
647 __ LoadFromOffset(kLoadWord,
648 TMP,
649 SP,
650 index2 + stack_offset);
651 __ StoreToOffset(kStoreWord,
652 Register(ensure_scratch.GetRegister()),
653 SP,
654 index2 + stack_offset);
655 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
656 }
657}
658
Alexey Frunze73296a72016-06-03 22:51:46 -0700659void CodeGeneratorMIPS::ComputeSpillMask() {
660 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
661 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
662 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
663 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
664 // registers, include the ZERO register to force alignment of FPU callee-saved registers
665 // within the stack frame.
666 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
667 core_spill_mask_ |= (1 << ZERO);
668 }
Alexey Frunze06a46c42016-07-19 15:00:40 -0700669 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
670 // (this can happen in leaf methods), artificially spill the ZERO register in order to
671 // force explicit saving and restoring of RA. RA isn't saved/restored when it's the only
672 // spilled register.
673 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
674 // saved in an unused temporary register) and saving of RA and the current method pointer
675 // in the frame.
676 if (clobbered_ra_ && core_spill_mask_ == (1u << RA) && fpu_spill_mask_ == 0) {
677 core_spill_mask_ |= (1 << ZERO);
678 }
Alexey Frunze73296a72016-06-03 22:51:46 -0700679}
680
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200681static dwarf::Reg DWARFReg(Register reg) {
682 return dwarf::Reg::MipsCore(static_cast<int>(reg));
683}
684
685// TODO: mapping of floating-point registers to DWARF.
686
687void CodeGeneratorMIPS::GenerateFrameEntry() {
688 __ Bind(&frame_entry_label_);
689
690 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
691
692 if (do_overflow_check) {
693 __ LoadFromOffset(kLoadWord,
694 ZERO,
695 SP,
696 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
697 RecordPcInfo(nullptr, 0);
698 }
699
700 if (HasEmptyFrame()) {
701 return;
702 }
703
704 // Make sure the frame size isn't unreasonably large.
705 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
706 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
707 }
708
709 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200710
Alexey Frunze73296a72016-06-03 22:51:46 -0700711 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200712 __ IncreaseFrameSize(ofs);
713
Alexey Frunze73296a72016-06-03 22:51:46 -0700714 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
715 Register reg = static_cast<Register>(MostSignificantBit(mask));
716 mask ^= 1u << reg;
717 ofs -= kMipsWordSize;
718 // The ZERO register is only included for alignment.
719 if (reg != ZERO) {
720 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200721 __ cfi().RelOffset(DWARFReg(reg), ofs);
722 }
723 }
724
Alexey Frunze73296a72016-06-03 22:51:46 -0700725 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
726 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
727 mask ^= 1u << reg;
728 ofs -= kMipsDoublewordSize;
729 __ StoreDToOffset(reg, SP, ofs);
730 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200731 }
732
Alexey Frunze73296a72016-06-03 22:51:46 -0700733 // Store the current method pointer.
734 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200735}
736
737void CodeGeneratorMIPS::GenerateFrameExit() {
738 __ cfi().RememberState();
739
740 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200741 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200742
Alexey Frunze73296a72016-06-03 22:51:46 -0700743 // For better instruction scheduling restore RA before other registers.
744 uint32_t ofs = GetFrameSize();
745 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
746 Register reg = static_cast<Register>(MostSignificantBit(mask));
747 mask ^= 1u << reg;
748 ofs -= kMipsWordSize;
749 // The ZERO register is only included for alignment.
750 if (reg != ZERO) {
751 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200752 __ cfi().Restore(DWARFReg(reg));
753 }
754 }
755
Alexey Frunze73296a72016-06-03 22:51:46 -0700756 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
757 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
758 mask ^= 1u << reg;
759 ofs -= kMipsDoublewordSize;
760 __ LoadDFromOffset(reg, SP, ofs);
761 // TODO: __ cfi().Restore(DWARFReg(reg));
762 }
763
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700764 size_t frame_size = GetFrameSize();
765 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
766 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
767 bool reordering = __ SetReorder(false);
768 if (exchange) {
769 __ Jr(RA);
770 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
771 } else {
772 __ DecreaseFrameSize(frame_size);
773 __ Jr(RA);
774 __ Nop(); // In delay slot.
775 }
776 __ SetReorder(reordering);
777 } else {
778 __ Jr(RA);
779 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200780 }
781
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200782 __ cfi().RestoreState();
783 __ cfi().DefCFAOffset(GetFrameSize());
784}
785
786void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
787 __ Bind(GetLabelOf(block));
788}
789
790void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
791 if (src.Equals(dst)) {
792 return;
793 }
794
795 if (src.IsConstant()) {
796 MoveConstant(dst, src.GetConstant());
797 } else {
798 if (Primitive::Is64BitType(dst_type)) {
799 Move64(dst, src);
800 } else {
801 Move32(dst, src);
802 }
803 }
804}
805
806void CodeGeneratorMIPS::Move32(Location destination, Location source) {
807 if (source.Equals(destination)) {
808 return;
809 }
810
811 if (destination.IsRegister()) {
812 if (source.IsRegister()) {
813 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
814 } else if (source.IsFpuRegister()) {
815 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
816 } else {
817 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
818 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
819 }
820 } else if (destination.IsFpuRegister()) {
821 if (source.IsRegister()) {
822 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
823 } else if (source.IsFpuRegister()) {
824 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
825 } else {
826 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
827 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
828 }
829 } else {
830 DCHECK(destination.IsStackSlot()) << destination;
831 if (source.IsRegister()) {
832 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
833 } else if (source.IsFpuRegister()) {
834 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
835 } else {
836 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
837 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
838 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
839 }
840 }
841}
842
843void CodeGeneratorMIPS::Move64(Location destination, Location source) {
844 if (source.Equals(destination)) {
845 return;
846 }
847
848 if (destination.IsRegisterPair()) {
849 if (source.IsRegisterPair()) {
850 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
851 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
852 } else if (source.IsFpuRegister()) {
853 Register dst_high = destination.AsRegisterPairHigh<Register>();
854 Register dst_low = destination.AsRegisterPairLow<Register>();
855 FRegister src = source.AsFpuRegister<FRegister>();
856 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800857 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200858 } else {
859 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
860 int32_t off = source.GetStackIndex();
861 Register r = destination.AsRegisterPairLow<Register>();
862 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
863 }
864 } else if (destination.IsFpuRegister()) {
865 if (source.IsRegisterPair()) {
866 FRegister dst = destination.AsFpuRegister<FRegister>();
867 Register src_high = source.AsRegisterPairHigh<Register>();
868 Register src_low = source.AsRegisterPairLow<Register>();
869 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800870 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200871 } else if (source.IsFpuRegister()) {
872 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
873 } else {
874 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
875 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
876 }
877 } else {
878 DCHECK(destination.IsDoubleStackSlot()) << destination;
879 int32_t off = destination.GetStackIndex();
880 if (source.IsRegisterPair()) {
881 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
882 } else if (source.IsFpuRegister()) {
883 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
884 } else {
885 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
886 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
887 __ StoreToOffset(kStoreWord, TMP, SP, off);
888 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
889 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
890 }
891 }
892}
893
894void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
895 if (c->IsIntConstant() || c->IsNullConstant()) {
896 // Move 32 bit constant.
897 int32_t value = GetInt32ValueOf(c);
898 if (destination.IsRegister()) {
899 Register dst = destination.AsRegister<Register>();
900 __ LoadConst32(dst, value);
901 } else {
902 DCHECK(destination.IsStackSlot())
903 << "Cannot move " << c->DebugName() << " to " << destination;
904 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
905 }
906 } else if (c->IsLongConstant()) {
907 // Move 64 bit constant.
908 int64_t value = GetInt64ValueOf(c);
909 if (destination.IsRegisterPair()) {
910 Register r_h = destination.AsRegisterPairHigh<Register>();
911 Register r_l = destination.AsRegisterPairLow<Register>();
912 __ LoadConst64(r_h, r_l, value);
913 } else {
914 DCHECK(destination.IsDoubleStackSlot())
915 << "Cannot move " << c->DebugName() << " to " << destination;
916 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
917 }
918 } else if (c->IsFloatConstant()) {
919 // Move 32 bit float constant.
920 int32_t value = GetInt32ValueOf(c);
921 if (destination.IsFpuRegister()) {
922 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
923 } else {
924 DCHECK(destination.IsStackSlot())
925 << "Cannot move " << c->DebugName() << " to " << destination;
926 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
927 }
928 } else {
929 // Move 64 bit double constant.
930 DCHECK(c->IsDoubleConstant()) << c->DebugName();
931 int64_t value = GetInt64ValueOf(c);
932 if (destination.IsFpuRegister()) {
933 FRegister fd = destination.AsFpuRegister<FRegister>();
934 __ LoadDConst64(fd, value, TMP);
935 } else {
936 DCHECK(destination.IsDoubleStackSlot())
937 << "Cannot move " << c->DebugName() << " to " << destination;
938 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
939 }
940 }
941}
942
943void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
944 DCHECK(destination.IsRegister());
945 Register dst = destination.AsRegister<Register>();
946 __ LoadConst32(dst, value);
947}
948
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200949void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
950 if (location.IsRegister()) {
951 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700952 } else if (location.IsRegisterPair()) {
953 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
954 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200955 } else {
956 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
957 }
958}
959
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700960void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
961 DCHECK(linker_patches->empty());
962 size_t size =
963 method_patches_.size() +
964 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700965 pc_relative_dex_cache_patches_.size() +
966 pc_relative_string_patches_.size() +
967 pc_relative_type_patches_.size() +
968 boot_image_string_patches_.size() +
969 boot_image_type_patches_.size() +
970 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700971 linker_patches->reserve(size);
972 for (const auto& entry : method_patches_) {
973 const MethodReference& target_method = entry.first;
974 Literal* literal = entry.second;
975 DCHECK(literal->GetLabel()->IsBound());
976 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
977 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
978 target_method.dex_file,
979 target_method.dex_method_index));
980 }
981 for (const auto& entry : call_patches_) {
982 const MethodReference& target_method = entry.first;
983 Literal* literal = entry.second;
984 DCHECK(literal->GetLabel()->IsBound());
985 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
986 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
987 target_method.dex_file,
988 target_method.dex_method_index));
989 }
990 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
991 const DexFile& dex_file = info.target_dex_file;
992 size_t base_element_offset = info.offset_or_index;
993 DCHECK(info.high_label.IsBound());
994 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
995 DCHECK(info.pc_rel_label.IsBound());
996 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
997 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
998 &dex_file,
999 pc_rel_offset,
1000 base_element_offset));
1001 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001002 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
1003 const DexFile& dex_file = info.target_dex_file;
1004 size_t string_index = info.offset_or_index;
1005 DCHECK(info.high_label.IsBound());
1006 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1007 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1008 // the assembler's base label used for PC-relative literals.
1009 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1010 ? __ GetLabelLocation(&info.pc_rel_label)
1011 : __ GetPcRelBaseLabelLocation();
1012 linker_patches->push_back(LinkerPatch::RelativeStringPatch(high_offset,
1013 &dex_file,
1014 pc_rel_offset,
1015 string_index));
1016 }
1017 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
1018 const DexFile& dex_file = info.target_dex_file;
1019 size_t type_index = info.offset_or_index;
1020 DCHECK(info.high_label.IsBound());
1021 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1022 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1023 // the assembler's base label used for PC-relative literals.
1024 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1025 ? __ GetLabelLocation(&info.pc_rel_label)
1026 : __ GetPcRelBaseLabelLocation();
1027 linker_patches->push_back(LinkerPatch::RelativeTypePatch(high_offset,
1028 &dex_file,
1029 pc_rel_offset,
1030 type_index));
1031 }
1032 for (const auto& entry : boot_image_string_patches_) {
1033 const StringReference& target_string = entry.first;
1034 Literal* literal = entry.second;
1035 DCHECK(literal->GetLabel()->IsBound());
1036 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1037 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1038 target_string.dex_file,
1039 target_string.string_index));
1040 }
1041 for (const auto& entry : boot_image_type_patches_) {
1042 const TypeReference& target_type = entry.first;
1043 Literal* literal = entry.second;
1044 DCHECK(literal->GetLabel()->IsBound());
1045 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1046 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1047 target_type.dex_file,
1048 target_type.type_index));
1049 }
1050 for (const auto& entry : boot_image_address_patches_) {
1051 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1052 Literal* literal = entry.second;
1053 DCHECK(literal->GetLabel()->IsBound());
1054 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1055 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1056 }
1057}
1058
1059CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1060 const DexFile& dex_file, uint32_t string_index) {
1061 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1062}
1063
1064CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1065 const DexFile& dex_file, uint32_t type_index) {
1066 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001067}
1068
1069CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1070 const DexFile& dex_file, uint32_t element_offset) {
1071 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1072}
1073
1074CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1075 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1076 patches->emplace_back(dex_file, offset_or_index);
1077 return &patches->back();
1078}
1079
Alexey Frunze06a46c42016-07-19 15:00:40 -07001080Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1081 return map->GetOrCreate(
1082 value,
1083 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1084}
1085
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001086Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1087 MethodToLiteralMap* map) {
1088 return map->GetOrCreate(
1089 target_method,
1090 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1091}
1092
1093Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1094 return DeduplicateMethodLiteral(target_method, &method_patches_);
1095}
1096
1097Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1098 return DeduplicateMethodLiteral(target_method, &call_patches_);
1099}
1100
Alexey Frunze06a46c42016-07-19 15:00:40 -07001101Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1102 uint32_t string_index) {
1103 return boot_image_string_patches_.GetOrCreate(
1104 StringReference(&dex_file, string_index),
1105 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1106}
1107
1108Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1109 uint32_t type_index) {
1110 return boot_image_type_patches_.GetOrCreate(
1111 TypeReference(&dex_file, type_index),
1112 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1113}
1114
1115Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1116 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1117 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1118 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1119}
1120
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001121void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1122 MipsLabel done;
1123 Register card = AT;
1124 Register temp = TMP;
1125 __ Beqz(value, &done);
1126 __ LoadFromOffset(kLoadWord,
1127 card,
1128 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001129 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001130 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1131 __ Addu(temp, card, temp);
1132 __ Sb(card, temp, 0);
1133 __ Bind(&done);
1134}
1135
David Brazdil58282f42016-01-14 12:45:10 +00001136void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001137 // Don't allocate the dalvik style register pair passing.
1138 blocked_register_pairs_[A1_A2] = true;
1139
1140 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1141 blocked_core_registers_[ZERO] = true;
1142 blocked_core_registers_[K0] = true;
1143 blocked_core_registers_[K1] = true;
1144 blocked_core_registers_[GP] = true;
1145 blocked_core_registers_[SP] = true;
1146 blocked_core_registers_[RA] = true;
1147
1148 // AT and TMP(T8) are used as temporary/scratch registers
1149 // (similar to how AT is used by MIPS assemblers).
1150 blocked_core_registers_[AT] = true;
1151 blocked_core_registers_[TMP] = true;
1152 blocked_fpu_registers_[FTMP] = true;
1153
1154 // Reserve suspend and thread registers.
1155 blocked_core_registers_[S0] = true;
1156 blocked_core_registers_[TR] = true;
1157
1158 // Reserve T9 for function calls
1159 blocked_core_registers_[T9] = true;
1160
1161 // Reserve odd-numbered FPU registers.
1162 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1163 blocked_fpu_registers_[i] = true;
1164 }
1165
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001166 if (GetGraph()->IsDebuggable()) {
1167 // Stubs do not save callee-save floating point registers. If the graph
1168 // is debuggable, we need to deal with these registers differently. For
1169 // now, just block them.
1170 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1171 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1172 }
1173 }
1174
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001175 UpdateBlockedPairRegisters();
1176}
1177
1178void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1179 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1180 MipsManagedRegister current =
1181 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1182 if (blocked_core_registers_[current.AsRegisterPairLow()]
1183 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1184 blocked_register_pairs_[i] = true;
1185 }
1186 }
1187}
1188
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001189size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1190 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1191 return kMipsWordSize;
1192}
1193
1194size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1195 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1196 return kMipsWordSize;
1197}
1198
1199size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1200 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1201 return kMipsDoublewordSize;
1202}
1203
1204size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1205 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1206 return kMipsDoublewordSize;
1207}
1208
1209void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001210 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001211}
1212
1213void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001214 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001215}
1216
Serban Constantinescufca16662016-07-14 09:21:59 +01001217constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1218
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001219void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1220 HInstruction* instruction,
1221 uint32_t dex_pc,
1222 SlowPathCode* slow_path) {
Serban Constantinescufca16662016-07-14 09:21:59 +01001223 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001224 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001225 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001226 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001227 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001228 // Reserve argument space on stack (for $a0-$a3) for
1229 // entrypoints that directly reference native implementations.
1230 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001231 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001232 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001233 } else {
1234 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001235 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001236 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001237 if (EntrypointRequiresStackMap(entrypoint)) {
1238 RecordPcInfo(instruction, dex_pc, slow_path);
1239 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001240}
1241
1242void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1243 Register class_reg) {
1244 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1245 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1246 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1247 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1248 __ Sync(0);
1249 __ Bind(slow_path->GetExitLabel());
1250}
1251
1252void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1253 __ Sync(0); // Only stype 0 is supported.
1254}
1255
1256void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1257 HBasicBlock* successor) {
1258 SuspendCheckSlowPathMIPS* slow_path =
1259 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1260 codegen_->AddSlowPath(slow_path);
1261
1262 __ LoadFromOffset(kLoadUnsignedHalfword,
1263 TMP,
1264 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001265 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001266 if (successor == nullptr) {
1267 __ Bnez(TMP, slow_path->GetEntryLabel());
1268 __ Bind(slow_path->GetReturnLabel());
1269 } else {
1270 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1271 __ B(slow_path->GetEntryLabel());
1272 // slow_path will return to GetLabelOf(successor).
1273 }
1274}
1275
1276InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1277 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001278 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001279 assembler_(codegen->GetAssembler()),
1280 codegen_(codegen) {}
1281
1282void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1283 DCHECK_EQ(instruction->InputCount(), 2U);
1284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1285 Primitive::Type type = instruction->GetResultType();
1286 switch (type) {
1287 case Primitive::kPrimInt: {
1288 locations->SetInAt(0, Location::RequiresRegister());
1289 HInstruction* right = instruction->InputAt(1);
1290 bool can_use_imm = false;
1291 if (right->IsConstant()) {
1292 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1293 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1294 can_use_imm = IsUint<16>(imm);
1295 } else if (instruction->IsAdd()) {
1296 can_use_imm = IsInt<16>(imm);
1297 } else {
1298 DCHECK(instruction->IsSub());
1299 can_use_imm = IsInt<16>(-imm);
1300 }
1301 }
1302 if (can_use_imm)
1303 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1304 else
1305 locations->SetInAt(1, Location::RequiresRegister());
1306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1307 break;
1308 }
1309
1310 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001312 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1313 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001314 break;
1315 }
1316
1317 case Primitive::kPrimFloat:
1318 case Primitive::kPrimDouble:
1319 DCHECK(instruction->IsAdd() || instruction->IsSub());
1320 locations->SetInAt(0, Location::RequiresFpuRegister());
1321 locations->SetInAt(1, Location::RequiresFpuRegister());
1322 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1323 break;
1324
1325 default:
1326 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1327 }
1328}
1329
1330void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1331 Primitive::Type type = instruction->GetType();
1332 LocationSummary* locations = instruction->GetLocations();
1333
1334 switch (type) {
1335 case Primitive::kPrimInt: {
1336 Register dst = locations->Out().AsRegister<Register>();
1337 Register lhs = locations->InAt(0).AsRegister<Register>();
1338 Location rhs_location = locations->InAt(1);
1339
1340 Register rhs_reg = ZERO;
1341 int32_t rhs_imm = 0;
1342 bool use_imm = rhs_location.IsConstant();
1343 if (use_imm) {
1344 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1345 } else {
1346 rhs_reg = rhs_location.AsRegister<Register>();
1347 }
1348
1349 if (instruction->IsAnd()) {
1350 if (use_imm)
1351 __ Andi(dst, lhs, rhs_imm);
1352 else
1353 __ And(dst, lhs, rhs_reg);
1354 } else if (instruction->IsOr()) {
1355 if (use_imm)
1356 __ Ori(dst, lhs, rhs_imm);
1357 else
1358 __ Or(dst, lhs, rhs_reg);
1359 } else if (instruction->IsXor()) {
1360 if (use_imm)
1361 __ Xori(dst, lhs, rhs_imm);
1362 else
1363 __ Xor(dst, lhs, rhs_reg);
1364 } else if (instruction->IsAdd()) {
1365 if (use_imm)
1366 __ Addiu(dst, lhs, rhs_imm);
1367 else
1368 __ Addu(dst, lhs, rhs_reg);
1369 } else {
1370 DCHECK(instruction->IsSub());
1371 if (use_imm)
1372 __ Addiu(dst, lhs, -rhs_imm);
1373 else
1374 __ Subu(dst, lhs, rhs_reg);
1375 }
1376 break;
1377 }
1378
1379 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001380 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1381 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1382 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1383 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001384 Location rhs_location = locations->InAt(1);
1385 bool use_imm = rhs_location.IsConstant();
1386 if (!use_imm) {
1387 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1388 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1389 if (instruction->IsAnd()) {
1390 __ And(dst_low, lhs_low, rhs_low);
1391 __ And(dst_high, lhs_high, rhs_high);
1392 } else if (instruction->IsOr()) {
1393 __ Or(dst_low, lhs_low, rhs_low);
1394 __ Or(dst_high, lhs_high, rhs_high);
1395 } else if (instruction->IsXor()) {
1396 __ Xor(dst_low, lhs_low, rhs_low);
1397 __ Xor(dst_high, lhs_high, rhs_high);
1398 } else if (instruction->IsAdd()) {
1399 if (lhs_low == rhs_low) {
1400 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1401 __ Slt(TMP, lhs_low, ZERO);
1402 __ Addu(dst_low, lhs_low, rhs_low);
1403 } else {
1404 __ Addu(dst_low, lhs_low, rhs_low);
1405 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1406 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1407 }
1408 __ Addu(dst_high, lhs_high, rhs_high);
1409 __ Addu(dst_high, dst_high, TMP);
1410 } else {
1411 DCHECK(instruction->IsSub());
1412 __ Sltu(TMP, lhs_low, rhs_low);
1413 __ Subu(dst_low, lhs_low, rhs_low);
1414 __ Subu(dst_high, lhs_high, rhs_high);
1415 __ Subu(dst_high, dst_high, TMP);
1416 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001417 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001418 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1419 if (instruction->IsOr()) {
1420 uint32_t low = Low32Bits(value);
1421 uint32_t high = High32Bits(value);
1422 if (IsUint<16>(low)) {
1423 if (dst_low != lhs_low || low != 0) {
1424 __ Ori(dst_low, lhs_low, low);
1425 }
1426 } else {
1427 __ LoadConst32(TMP, low);
1428 __ Or(dst_low, lhs_low, TMP);
1429 }
1430 if (IsUint<16>(high)) {
1431 if (dst_high != lhs_high || high != 0) {
1432 __ Ori(dst_high, lhs_high, high);
1433 }
1434 } else {
1435 if (high != low) {
1436 __ LoadConst32(TMP, high);
1437 }
1438 __ Or(dst_high, lhs_high, TMP);
1439 }
1440 } else if (instruction->IsXor()) {
1441 uint32_t low = Low32Bits(value);
1442 uint32_t high = High32Bits(value);
1443 if (IsUint<16>(low)) {
1444 if (dst_low != lhs_low || low != 0) {
1445 __ Xori(dst_low, lhs_low, low);
1446 }
1447 } else {
1448 __ LoadConst32(TMP, low);
1449 __ Xor(dst_low, lhs_low, TMP);
1450 }
1451 if (IsUint<16>(high)) {
1452 if (dst_high != lhs_high || high != 0) {
1453 __ Xori(dst_high, lhs_high, high);
1454 }
1455 } else {
1456 if (high != low) {
1457 __ LoadConst32(TMP, high);
1458 }
1459 __ Xor(dst_high, lhs_high, TMP);
1460 }
1461 } else if (instruction->IsAnd()) {
1462 uint32_t low = Low32Bits(value);
1463 uint32_t high = High32Bits(value);
1464 if (IsUint<16>(low)) {
1465 __ Andi(dst_low, lhs_low, low);
1466 } else if (low != 0xFFFFFFFF) {
1467 __ LoadConst32(TMP, low);
1468 __ And(dst_low, lhs_low, TMP);
1469 } else if (dst_low != lhs_low) {
1470 __ Move(dst_low, lhs_low);
1471 }
1472 if (IsUint<16>(high)) {
1473 __ Andi(dst_high, lhs_high, high);
1474 } else if (high != 0xFFFFFFFF) {
1475 if (high != low) {
1476 __ LoadConst32(TMP, high);
1477 }
1478 __ And(dst_high, lhs_high, TMP);
1479 } else if (dst_high != lhs_high) {
1480 __ Move(dst_high, lhs_high);
1481 }
1482 } else {
1483 if (instruction->IsSub()) {
1484 value = -value;
1485 } else {
1486 DCHECK(instruction->IsAdd());
1487 }
1488 int32_t low = Low32Bits(value);
1489 int32_t high = High32Bits(value);
1490 if (IsInt<16>(low)) {
1491 if (dst_low != lhs_low || low != 0) {
1492 __ Addiu(dst_low, lhs_low, low);
1493 }
1494 if (low != 0) {
1495 __ Sltiu(AT, dst_low, low);
1496 }
1497 } else {
1498 __ LoadConst32(TMP, low);
1499 __ Addu(dst_low, lhs_low, TMP);
1500 __ Sltu(AT, dst_low, TMP);
1501 }
1502 if (IsInt<16>(high)) {
1503 if (dst_high != lhs_high || high != 0) {
1504 __ Addiu(dst_high, lhs_high, high);
1505 }
1506 } else {
1507 if (high != low) {
1508 __ LoadConst32(TMP, high);
1509 }
1510 __ Addu(dst_high, lhs_high, TMP);
1511 }
1512 if (low != 0) {
1513 __ Addu(dst_high, dst_high, AT);
1514 }
1515 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001516 }
1517 break;
1518 }
1519
1520 case Primitive::kPrimFloat:
1521 case Primitive::kPrimDouble: {
1522 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1523 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1524 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1525 if (instruction->IsAdd()) {
1526 if (type == Primitive::kPrimFloat) {
1527 __ AddS(dst, lhs, rhs);
1528 } else {
1529 __ AddD(dst, lhs, rhs);
1530 }
1531 } else {
1532 DCHECK(instruction->IsSub());
1533 if (type == Primitive::kPrimFloat) {
1534 __ SubS(dst, lhs, rhs);
1535 } else {
1536 __ SubD(dst, lhs, rhs);
1537 }
1538 }
1539 break;
1540 }
1541
1542 default:
1543 LOG(FATAL) << "Unexpected binary operation type " << type;
1544 }
1545}
1546
1547void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001548 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001549
1550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1551 Primitive::Type type = instr->GetResultType();
1552 switch (type) {
1553 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001554 locations->SetInAt(0, Location::RequiresRegister());
1555 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1556 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1557 break;
1558 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001559 locations->SetInAt(0, Location::RequiresRegister());
1560 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1561 locations->SetOut(Location::RequiresRegister());
1562 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001563 default:
1564 LOG(FATAL) << "Unexpected shift type " << type;
1565 }
1566}
1567
1568static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1569
1570void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001571 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001572 LocationSummary* locations = instr->GetLocations();
1573 Primitive::Type type = instr->GetType();
1574
1575 Location rhs_location = locations->InAt(1);
1576 bool use_imm = rhs_location.IsConstant();
1577 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1578 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001579 const uint32_t shift_mask =
1580 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001581 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001582 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1583 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001584
1585 switch (type) {
1586 case Primitive::kPrimInt: {
1587 Register dst = locations->Out().AsRegister<Register>();
1588 Register lhs = locations->InAt(0).AsRegister<Register>();
1589 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001590 if (shift_value == 0) {
1591 if (dst != lhs) {
1592 __ Move(dst, lhs);
1593 }
1594 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001595 __ Sll(dst, lhs, shift_value);
1596 } else if (instr->IsShr()) {
1597 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001598 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001599 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001600 } else {
1601 if (has_ins_rotr) {
1602 __ Rotr(dst, lhs, shift_value);
1603 } else {
1604 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1605 __ Srl(dst, lhs, shift_value);
1606 __ Or(dst, dst, TMP);
1607 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001608 }
1609 } else {
1610 if (instr->IsShl()) {
1611 __ Sllv(dst, lhs, rhs_reg);
1612 } else if (instr->IsShr()) {
1613 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001614 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001615 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001616 } else {
1617 if (has_ins_rotr) {
1618 __ Rotrv(dst, lhs, rhs_reg);
1619 } else {
1620 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001621 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1622 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1623 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1624 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1625 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001626 __ Sllv(TMP, lhs, TMP);
1627 __ Srlv(dst, lhs, rhs_reg);
1628 __ Or(dst, dst, TMP);
1629 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001630 }
1631 }
1632 break;
1633 }
1634
1635 case Primitive::kPrimLong: {
1636 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1637 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1638 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1639 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1640 if (use_imm) {
1641 if (shift_value == 0) {
1642 codegen_->Move64(locations->Out(), locations->InAt(0));
1643 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001644 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001645 if (instr->IsShl()) {
1646 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1647 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1648 __ Sll(dst_low, lhs_low, shift_value);
1649 } else if (instr->IsShr()) {
1650 __ Srl(dst_low, lhs_low, shift_value);
1651 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1652 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001653 } else if (instr->IsUShr()) {
1654 __ Srl(dst_low, lhs_low, shift_value);
1655 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1656 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001657 } else {
1658 __ Srl(dst_low, lhs_low, shift_value);
1659 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1660 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001661 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001662 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001663 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001664 if (instr->IsShl()) {
1665 __ Sll(dst_low, lhs_low, shift_value);
1666 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1667 __ Sll(dst_high, lhs_high, shift_value);
1668 __ Or(dst_high, dst_high, TMP);
1669 } else if (instr->IsShr()) {
1670 __ Sra(dst_high, lhs_high, shift_value);
1671 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1672 __ Srl(dst_low, lhs_low, shift_value);
1673 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001674 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001675 __ Srl(dst_high, lhs_high, shift_value);
1676 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1677 __ Srl(dst_low, lhs_low, shift_value);
1678 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001679 } else {
1680 __ Srl(TMP, lhs_low, shift_value);
1681 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1682 __ Or(dst_low, dst_low, TMP);
1683 __ Srl(TMP, lhs_high, shift_value);
1684 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1685 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001686 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001687 }
1688 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001689 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001690 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001691 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001692 __ Move(dst_low, ZERO);
1693 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001694 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001695 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001696 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001697 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001698 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001699 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001700 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001701 // 64-bit rotation by 32 is just a swap.
1702 __ Move(dst_low, lhs_high);
1703 __ Move(dst_high, lhs_low);
1704 } else {
1705 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001706 __ Srl(dst_low, lhs_high, shift_value_high);
1707 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1708 __ Srl(dst_high, lhs_low, shift_value_high);
1709 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001710 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001711 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1712 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001713 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001714 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1715 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001716 __ Or(dst_high, dst_high, TMP);
1717 }
1718 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001719 }
1720 }
1721 } else {
1722 MipsLabel done;
1723 if (instr->IsShl()) {
1724 __ Sllv(dst_low, lhs_low, rhs_reg);
1725 __ Nor(AT, ZERO, rhs_reg);
1726 __ Srl(TMP, lhs_low, 1);
1727 __ Srlv(TMP, TMP, AT);
1728 __ Sllv(dst_high, lhs_high, rhs_reg);
1729 __ Or(dst_high, dst_high, TMP);
1730 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1731 __ Beqz(TMP, &done);
1732 __ Move(dst_high, dst_low);
1733 __ Move(dst_low, ZERO);
1734 } else if (instr->IsShr()) {
1735 __ Srav(dst_high, lhs_high, rhs_reg);
1736 __ Nor(AT, ZERO, rhs_reg);
1737 __ Sll(TMP, lhs_high, 1);
1738 __ Sllv(TMP, TMP, AT);
1739 __ Srlv(dst_low, lhs_low, rhs_reg);
1740 __ Or(dst_low, dst_low, TMP);
1741 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1742 __ Beqz(TMP, &done);
1743 __ Move(dst_low, dst_high);
1744 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001745 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001746 __ Srlv(dst_high, lhs_high, rhs_reg);
1747 __ Nor(AT, ZERO, rhs_reg);
1748 __ Sll(TMP, lhs_high, 1);
1749 __ Sllv(TMP, TMP, AT);
1750 __ Srlv(dst_low, lhs_low, rhs_reg);
1751 __ Or(dst_low, dst_low, TMP);
1752 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1753 __ Beqz(TMP, &done);
1754 __ Move(dst_low, dst_high);
1755 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001756 } else {
1757 __ Nor(AT, ZERO, rhs_reg);
1758 __ Srlv(TMP, lhs_low, rhs_reg);
1759 __ Sll(dst_low, lhs_high, 1);
1760 __ Sllv(dst_low, dst_low, AT);
1761 __ Or(dst_low, dst_low, TMP);
1762 __ Srlv(TMP, lhs_high, rhs_reg);
1763 __ Sll(dst_high, lhs_low, 1);
1764 __ Sllv(dst_high, dst_high, AT);
1765 __ Or(dst_high, dst_high, TMP);
1766 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1767 __ Beqz(TMP, &done);
1768 __ Move(TMP, dst_high);
1769 __ Move(dst_high, dst_low);
1770 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001771 }
1772 __ Bind(&done);
1773 }
1774 break;
1775 }
1776
1777 default:
1778 LOG(FATAL) << "Unexpected shift operation type " << type;
1779 }
1780}
1781
1782void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1783 HandleBinaryOp(instruction);
1784}
1785
1786void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1787 HandleBinaryOp(instruction);
1788}
1789
1790void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1791 HandleBinaryOp(instruction);
1792}
1793
1794void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1795 HandleBinaryOp(instruction);
1796}
1797
1798void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1799 LocationSummary* locations =
1800 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1801 locations->SetInAt(0, Location::RequiresRegister());
1802 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1803 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1804 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1805 } else {
1806 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1807 }
1808}
1809
Alexey Frunze2923db72016-08-20 01:55:47 -07001810auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1811 auto null_checker = [this, instruction]() {
1812 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1813 };
1814 return null_checker;
1815}
1816
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001817void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1818 LocationSummary* locations = instruction->GetLocations();
1819 Register obj = locations->InAt(0).AsRegister<Register>();
1820 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001821 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001822 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001823
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001824 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001825 switch (type) {
1826 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001827 Register out = locations->Out().AsRegister<Register>();
1828 if (index.IsConstant()) {
1829 size_t offset =
1830 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001831 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001832 } else {
1833 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001834 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001835 }
1836 break;
1837 }
1838
1839 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001840 Register out = locations->Out().AsRegister<Register>();
1841 if (index.IsConstant()) {
1842 size_t offset =
1843 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001844 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001845 } else {
1846 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001847 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848 }
1849 break;
1850 }
1851
1852 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001853 Register out = locations->Out().AsRegister<Register>();
1854 if (index.IsConstant()) {
1855 size_t offset =
1856 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001857 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 } else {
1859 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1860 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001861 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001862 }
1863 break;
1864 }
1865
1866 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 Register out = locations->Out().AsRegister<Register>();
1868 if (index.IsConstant()) {
1869 size_t offset =
1870 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001871 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872 } else {
1873 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1874 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001875 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 }
1877 break;
1878 }
1879
1880 case Primitive::kPrimInt:
1881 case Primitive::kPrimNot: {
1882 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001883 Register out = locations->Out().AsRegister<Register>();
1884 if (index.IsConstant()) {
1885 size_t offset =
1886 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001887 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001888 } else {
1889 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1890 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001891 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001892 }
1893 break;
1894 }
1895
1896 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001897 Register out = locations->Out().AsRegisterPairLow<Register>();
1898 if (index.IsConstant()) {
1899 size_t offset =
1900 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001901 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001902 } else {
1903 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1904 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001905 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906 }
1907 break;
1908 }
1909
1910 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001911 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1912 if (index.IsConstant()) {
1913 size_t offset =
1914 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001915 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001916 } else {
1917 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1918 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001919 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001920 }
1921 break;
1922 }
1923
1924 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001925 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1926 if (index.IsConstant()) {
1927 size_t offset =
1928 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001929 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001930 } else {
1931 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1932 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001933 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 }
1935 break;
1936 }
1937
1938 case Primitive::kPrimVoid:
1939 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1940 UNREACHABLE();
1941 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942}
1943
1944void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1945 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1946 locations->SetInAt(0, Location::RequiresRegister());
1947 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1948}
1949
1950void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1951 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001952 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001953 Register obj = locations->InAt(0).AsRegister<Register>();
1954 Register out = locations->Out().AsRegister<Register>();
1955 __ LoadFromOffset(kLoadWord, out, obj, offset);
1956 codegen_->MaybeRecordImplicitNullCheck(instruction);
1957}
1958
1959void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001960 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1962 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001963 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001964 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001965 InvokeRuntimeCallingConvention calling_convention;
1966 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1967 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1968 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1969 } else {
1970 locations->SetInAt(0, Location::RequiresRegister());
1971 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1972 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1973 locations->SetInAt(2, Location::RequiresFpuRegister());
1974 } else {
1975 locations->SetInAt(2, Location::RequiresRegister());
1976 }
1977 }
1978}
1979
1980void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1981 LocationSummary* locations = instruction->GetLocations();
1982 Register obj = locations->InAt(0).AsRegister<Register>();
1983 Location index = locations->InAt(1);
1984 Primitive::Type value_type = instruction->GetComponentType();
1985 bool needs_runtime_call = locations->WillCall();
1986 bool needs_write_barrier =
1987 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07001988 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001989
1990 switch (value_type) {
1991 case Primitive::kPrimBoolean:
1992 case Primitive::kPrimByte: {
1993 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1994 Register value = locations->InAt(2).AsRegister<Register>();
1995 if (index.IsConstant()) {
1996 size_t offset =
1997 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001998 __ StoreToOffset(kStoreByte, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001999 } else {
2000 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002001 __ StoreToOffset(kStoreByte, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002002 }
2003 break;
2004 }
2005
2006 case Primitive::kPrimShort:
2007 case Primitive::kPrimChar: {
2008 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
2009 Register value = locations->InAt(2).AsRegister<Register>();
2010 if (index.IsConstant()) {
2011 size_t offset =
2012 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002013 __ StoreToOffset(kStoreHalfword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014 } else {
2015 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
2016 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002017 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002018 }
2019 break;
2020 }
2021
2022 case Primitive::kPrimInt:
2023 case Primitive::kPrimNot: {
2024 if (!needs_runtime_call) {
2025 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2026 Register value = locations->InAt(2).AsRegister<Register>();
2027 if (index.IsConstant()) {
2028 size_t offset =
2029 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002030 __ StoreToOffset(kStoreWord, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002031 } else {
2032 DCHECK(index.IsRegister()) << index;
2033 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2034 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002035 __ StoreToOffset(kStoreWord, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002036 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002037 if (needs_write_barrier) {
2038 DCHECK_EQ(value_type, Primitive::kPrimNot);
2039 codegen_->MarkGCCard(obj, value);
2040 }
2041 } else {
2042 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002043 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2045 }
2046 break;
2047 }
2048
2049 case Primitive::kPrimLong: {
2050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2051 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2052 if (index.IsConstant()) {
2053 size_t offset =
2054 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002055 __ StoreToOffset(kStoreDoubleword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002056 } else {
2057 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2058 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002059 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002060 }
2061 break;
2062 }
2063
2064 case Primitive::kPrimFloat: {
2065 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2066 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2067 DCHECK(locations->InAt(2).IsFpuRegister());
2068 if (index.IsConstant()) {
2069 size_t offset =
2070 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002071 __ StoreSToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002072 } else {
2073 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2074 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002075 __ StoreSToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 }
2077 break;
2078 }
2079
2080 case Primitive::kPrimDouble: {
2081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2082 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2083 DCHECK(locations->InAt(2).IsFpuRegister());
2084 if (index.IsConstant()) {
2085 size_t offset =
2086 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002087 __ StoreDToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002088 } else {
2089 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2090 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002091 __ StoreDToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002092 }
2093 break;
2094 }
2095
2096 case Primitive::kPrimVoid:
2097 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2098 UNREACHABLE();
2099 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002100}
2101
2102void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2103 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2104 ? LocationSummary::kCallOnSlowPath
2105 : LocationSummary::kNoCall;
2106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2107 locations->SetInAt(0, Location::RequiresRegister());
2108 locations->SetInAt(1, Location::RequiresRegister());
2109 if (instruction->HasUses()) {
2110 locations->SetOut(Location::SameAsFirstInput());
2111 }
2112}
2113
2114void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2115 LocationSummary* locations = instruction->GetLocations();
2116 BoundsCheckSlowPathMIPS* slow_path =
2117 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2118 codegen_->AddSlowPath(slow_path);
2119
2120 Register index = locations->InAt(0).AsRegister<Register>();
2121 Register length = locations->InAt(1).AsRegister<Register>();
2122
2123 // length is limited by the maximum positive signed 32-bit integer.
2124 // Unsigned comparison of length and index checks for index < 0
2125 // and for length <= index simultaneously.
2126 __ Bgeu(index, length, slow_path->GetEntryLabel());
2127}
2128
2129void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2130 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2131 instruction,
2132 LocationSummary::kCallOnSlowPath);
2133 locations->SetInAt(0, Location::RequiresRegister());
2134 locations->SetInAt(1, Location::RequiresRegister());
2135 // Note that TypeCheckSlowPathMIPS uses this register too.
2136 locations->AddTemp(Location::RequiresRegister());
2137}
2138
2139void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2140 LocationSummary* locations = instruction->GetLocations();
2141 Register obj = locations->InAt(0).AsRegister<Register>();
2142 Register cls = locations->InAt(1).AsRegister<Register>();
2143 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2144
2145 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2146 codegen_->AddSlowPath(slow_path);
2147
2148 // TODO: avoid this check if we know obj is not null.
2149 __ Beqz(obj, slow_path->GetExitLabel());
2150 // Compare the class of `obj` with `cls`.
2151 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2152 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2153 __ Bind(slow_path->GetExitLabel());
2154}
2155
2156void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2157 LocationSummary* locations =
2158 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2159 locations->SetInAt(0, Location::RequiresRegister());
2160 if (check->HasUses()) {
2161 locations->SetOut(Location::SameAsFirstInput());
2162 }
2163}
2164
2165void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2166 // We assume the class is not null.
2167 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2168 check->GetLoadClass(),
2169 check,
2170 check->GetDexPc(),
2171 true);
2172 codegen_->AddSlowPath(slow_path);
2173 GenerateClassInitializationCheck(slow_path,
2174 check->GetLocations()->InAt(0).AsRegister<Register>());
2175}
2176
2177void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2178 Primitive::Type in_type = compare->InputAt(0)->GetType();
2179
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002180 LocationSummary* locations =
2181 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002182
2183 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002184 case Primitive::kPrimBoolean:
2185 case Primitive::kPrimByte:
2186 case Primitive::kPrimShort:
2187 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002188 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002189 case Primitive::kPrimLong:
2190 locations->SetInAt(0, Location::RequiresRegister());
2191 locations->SetInAt(1, Location::RequiresRegister());
2192 // Output overlaps because it is written before doing the low comparison.
2193 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2194 break;
2195
2196 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002197 case Primitive::kPrimDouble:
2198 locations->SetInAt(0, Location::RequiresFpuRegister());
2199 locations->SetInAt(1, Location::RequiresFpuRegister());
2200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002201 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002202
2203 default:
2204 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2205 }
2206}
2207
2208void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2209 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002210 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002211 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002212 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002213
2214 // 0 if: left == right
2215 // 1 if: left > right
2216 // -1 if: left < right
2217 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002218 case Primitive::kPrimBoolean:
2219 case Primitive::kPrimByte:
2220 case Primitive::kPrimShort:
2221 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002222 case Primitive::kPrimInt: {
2223 Register lhs = locations->InAt(0).AsRegister<Register>();
2224 Register rhs = locations->InAt(1).AsRegister<Register>();
2225 __ Slt(TMP, lhs, rhs);
2226 __ Slt(res, rhs, lhs);
2227 __ Subu(res, res, TMP);
2228 break;
2229 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002230 case Primitive::kPrimLong: {
2231 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002232 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2233 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2234 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2235 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2236 // TODO: more efficient (direct) comparison with a constant.
2237 __ Slt(TMP, lhs_high, rhs_high);
2238 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2239 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2240 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2241 __ Sltu(TMP, lhs_low, rhs_low);
2242 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2243 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2244 __ Bind(&done);
2245 break;
2246 }
2247
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002248 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002249 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002250 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2251 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2252 MipsLabel done;
2253 if (isR6) {
2254 __ CmpEqS(FTMP, lhs, rhs);
2255 __ LoadConst32(res, 0);
2256 __ Bc1nez(FTMP, &done);
2257 if (gt_bias) {
2258 __ CmpLtS(FTMP, lhs, rhs);
2259 __ LoadConst32(res, -1);
2260 __ Bc1nez(FTMP, &done);
2261 __ LoadConst32(res, 1);
2262 } else {
2263 __ CmpLtS(FTMP, rhs, lhs);
2264 __ LoadConst32(res, 1);
2265 __ Bc1nez(FTMP, &done);
2266 __ LoadConst32(res, -1);
2267 }
2268 } else {
2269 if (gt_bias) {
2270 __ ColtS(0, lhs, rhs);
2271 __ LoadConst32(res, -1);
2272 __ Bc1t(0, &done);
2273 __ CeqS(0, lhs, rhs);
2274 __ LoadConst32(res, 1);
2275 __ Movt(res, ZERO, 0);
2276 } else {
2277 __ ColtS(0, rhs, lhs);
2278 __ LoadConst32(res, 1);
2279 __ Bc1t(0, &done);
2280 __ CeqS(0, lhs, rhs);
2281 __ LoadConst32(res, -1);
2282 __ Movt(res, ZERO, 0);
2283 }
2284 }
2285 __ Bind(&done);
2286 break;
2287 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002288 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002289 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002290 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2291 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2292 MipsLabel done;
2293 if (isR6) {
2294 __ CmpEqD(FTMP, lhs, rhs);
2295 __ LoadConst32(res, 0);
2296 __ Bc1nez(FTMP, &done);
2297 if (gt_bias) {
2298 __ CmpLtD(FTMP, lhs, rhs);
2299 __ LoadConst32(res, -1);
2300 __ Bc1nez(FTMP, &done);
2301 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002302 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002303 __ CmpLtD(FTMP, rhs, lhs);
2304 __ LoadConst32(res, 1);
2305 __ Bc1nez(FTMP, &done);
2306 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002307 }
2308 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002309 if (gt_bias) {
2310 __ ColtD(0, lhs, rhs);
2311 __ LoadConst32(res, -1);
2312 __ Bc1t(0, &done);
2313 __ CeqD(0, lhs, rhs);
2314 __ LoadConst32(res, 1);
2315 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002317 __ ColtD(0, rhs, lhs);
2318 __ LoadConst32(res, 1);
2319 __ Bc1t(0, &done);
2320 __ CeqD(0, lhs, rhs);
2321 __ LoadConst32(res, -1);
2322 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002323 }
2324 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002325 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002326 break;
2327 }
2328
2329 default:
2330 LOG(FATAL) << "Unimplemented compare type " << in_type;
2331 }
2332}
2333
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002334void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002335 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002336 switch (instruction->InputAt(0)->GetType()) {
2337 default:
2338 case Primitive::kPrimLong:
2339 locations->SetInAt(0, Location::RequiresRegister());
2340 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2341 break;
2342
2343 case Primitive::kPrimFloat:
2344 case Primitive::kPrimDouble:
2345 locations->SetInAt(0, Location::RequiresFpuRegister());
2346 locations->SetInAt(1, Location::RequiresFpuRegister());
2347 break;
2348 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002349 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2351 }
2352}
2353
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002354void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002355 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 return;
2357 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002358
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002359 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002360 LocationSummary* locations = instruction->GetLocations();
2361 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002362 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002363
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002364 switch (type) {
2365 default:
2366 // Integer case.
2367 GenerateIntCompare(instruction->GetCondition(), locations);
2368 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002369
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002370 case Primitive::kPrimLong:
2371 // TODO: don't use branches.
2372 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002373 break;
2374
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002375 case Primitive::kPrimFloat:
2376 case Primitive::kPrimDouble:
2377 // TODO: don't use branches.
2378 GenerateFpCompareAndBranch(instruction->GetCondition(),
2379 instruction->IsGtBias(),
2380 type,
2381 locations,
2382 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002383 break;
2384 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002385
2386 // Convert the branches into the result.
2387 MipsLabel done;
2388
2389 // False case: result = 0.
2390 __ LoadConst32(dst, 0);
2391 __ B(&done);
2392
2393 // True case: result = 1.
2394 __ Bind(&true_label);
2395 __ LoadConst32(dst, 1);
2396 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002397}
2398
Alexey Frunze7e99e052015-11-24 19:28:01 -08002399void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2400 DCHECK(instruction->IsDiv() || instruction->IsRem());
2401 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2402
2403 LocationSummary* locations = instruction->GetLocations();
2404 Location second = locations->InAt(1);
2405 DCHECK(second.IsConstant());
2406
2407 Register out = locations->Out().AsRegister<Register>();
2408 Register dividend = locations->InAt(0).AsRegister<Register>();
2409 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2410 DCHECK(imm == 1 || imm == -1);
2411
2412 if (instruction->IsRem()) {
2413 __ Move(out, ZERO);
2414 } else {
2415 if (imm == -1) {
2416 __ Subu(out, ZERO, dividend);
2417 } else if (out != dividend) {
2418 __ Move(out, dividend);
2419 }
2420 }
2421}
2422
2423void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2424 DCHECK(instruction->IsDiv() || instruction->IsRem());
2425 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2426
2427 LocationSummary* locations = instruction->GetLocations();
2428 Location second = locations->InAt(1);
2429 DCHECK(second.IsConstant());
2430
2431 Register out = locations->Out().AsRegister<Register>();
2432 Register dividend = locations->InAt(0).AsRegister<Register>();
2433 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002434 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002435 int ctz_imm = CTZ(abs_imm);
2436
2437 if (instruction->IsDiv()) {
2438 if (ctz_imm == 1) {
2439 // Fast path for division by +/-2, which is very common.
2440 __ Srl(TMP, dividend, 31);
2441 } else {
2442 __ Sra(TMP, dividend, 31);
2443 __ Srl(TMP, TMP, 32 - ctz_imm);
2444 }
2445 __ Addu(out, dividend, TMP);
2446 __ Sra(out, out, ctz_imm);
2447 if (imm < 0) {
2448 __ Subu(out, ZERO, out);
2449 }
2450 } else {
2451 if (ctz_imm == 1) {
2452 // Fast path for modulo +/-2, which is very common.
2453 __ Sra(TMP, dividend, 31);
2454 __ Subu(out, dividend, TMP);
2455 __ Andi(out, out, 1);
2456 __ Addu(out, out, TMP);
2457 } else {
2458 __ Sra(TMP, dividend, 31);
2459 __ Srl(TMP, TMP, 32 - ctz_imm);
2460 __ Addu(out, dividend, TMP);
2461 if (IsUint<16>(abs_imm - 1)) {
2462 __ Andi(out, out, abs_imm - 1);
2463 } else {
2464 __ Sll(out, out, 32 - ctz_imm);
2465 __ Srl(out, out, 32 - ctz_imm);
2466 }
2467 __ Subu(out, out, TMP);
2468 }
2469 }
2470}
2471
2472void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2473 DCHECK(instruction->IsDiv() || instruction->IsRem());
2474 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2475
2476 LocationSummary* locations = instruction->GetLocations();
2477 Location second = locations->InAt(1);
2478 DCHECK(second.IsConstant());
2479
2480 Register out = locations->Out().AsRegister<Register>();
2481 Register dividend = locations->InAt(0).AsRegister<Register>();
2482 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2483
2484 int64_t magic;
2485 int shift;
2486 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2487
2488 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2489
2490 __ LoadConst32(TMP, magic);
2491 if (isR6) {
2492 __ MuhR6(TMP, dividend, TMP);
2493 } else {
2494 __ MultR2(dividend, TMP);
2495 __ Mfhi(TMP);
2496 }
2497 if (imm > 0 && magic < 0) {
2498 __ Addu(TMP, TMP, dividend);
2499 } else if (imm < 0 && magic > 0) {
2500 __ Subu(TMP, TMP, dividend);
2501 }
2502
2503 if (shift != 0) {
2504 __ Sra(TMP, TMP, shift);
2505 }
2506
2507 if (instruction->IsDiv()) {
2508 __ Sra(out, TMP, 31);
2509 __ Subu(out, TMP, out);
2510 } else {
2511 __ Sra(AT, TMP, 31);
2512 __ Subu(AT, TMP, AT);
2513 __ LoadConst32(TMP, imm);
2514 if (isR6) {
2515 __ MulR6(TMP, AT, TMP);
2516 } else {
2517 __ MulR2(TMP, AT, TMP);
2518 }
2519 __ Subu(out, dividend, TMP);
2520 }
2521}
2522
2523void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2524 DCHECK(instruction->IsDiv() || instruction->IsRem());
2525 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2526
2527 LocationSummary* locations = instruction->GetLocations();
2528 Register out = locations->Out().AsRegister<Register>();
2529 Location second = locations->InAt(1);
2530
2531 if (second.IsConstant()) {
2532 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2533 if (imm == 0) {
2534 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2535 } else if (imm == 1 || imm == -1) {
2536 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002537 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002538 DivRemByPowerOfTwo(instruction);
2539 } else {
2540 DCHECK(imm <= -2 || imm >= 2);
2541 GenerateDivRemWithAnyConstant(instruction);
2542 }
2543 } else {
2544 Register dividend = locations->InAt(0).AsRegister<Register>();
2545 Register divisor = second.AsRegister<Register>();
2546 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2547 if (instruction->IsDiv()) {
2548 if (isR6) {
2549 __ DivR6(out, dividend, divisor);
2550 } else {
2551 __ DivR2(out, dividend, divisor);
2552 }
2553 } else {
2554 if (isR6) {
2555 __ ModR6(out, dividend, divisor);
2556 } else {
2557 __ ModR2(out, dividend, divisor);
2558 }
2559 }
2560 }
2561}
2562
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002563void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2564 Primitive::Type type = div->GetResultType();
2565 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002566 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002567 : LocationSummary::kNoCall;
2568
2569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2570
2571 switch (type) {
2572 case Primitive::kPrimInt:
2573 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002574 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002575 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2576 break;
2577
2578 case Primitive::kPrimLong: {
2579 InvokeRuntimeCallingConvention calling_convention;
2580 locations->SetInAt(0, Location::RegisterPairLocation(
2581 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2582 locations->SetInAt(1, Location::RegisterPairLocation(
2583 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2584 locations->SetOut(calling_convention.GetReturnLocation(type));
2585 break;
2586 }
2587
2588 case Primitive::kPrimFloat:
2589 case Primitive::kPrimDouble:
2590 locations->SetInAt(0, Location::RequiresFpuRegister());
2591 locations->SetInAt(1, Location::RequiresFpuRegister());
2592 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2593 break;
2594
2595 default:
2596 LOG(FATAL) << "Unexpected div type " << type;
2597 }
2598}
2599
2600void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2601 Primitive::Type type = instruction->GetType();
2602 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603
2604 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002605 case Primitive::kPrimInt:
2606 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002608 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002609 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002610 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2611 break;
2612 }
2613 case Primitive::kPrimFloat:
2614 case Primitive::kPrimDouble: {
2615 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2616 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2617 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2618 if (type == Primitive::kPrimFloat) {
2619 __ DivS(dst, lhs, rhs);
2620 } else {
2621 __ DivD(dst, lhs, rhs);
2622 }
2623 break;
2624 }
2625 default:
2626 LOG(FATAL) << "Unexpected div type " << type;
2627 }
2628}
2629
2630void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2631 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2632 ? LocationSummary::kCallOnSlowPath
2633 : LocationSummary::kNoCall;
2634 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2635 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2636 if (instruction->HasUses()) {
2637 locations->SetOut(Location::SameAsFirstInput());
2638 }
2639}
2640
2641void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2642 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2643 codegen_->AddSlowPath(slow_path);
2644 Location value = instruction->GetLocations()->InAt(0);
2645 Primitive::Type type = instruction->GetType();
2646
2647 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002648 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002649 case Primitive::kPrimByte:
2650 case Primitive::kPrimChar:
2651 case Primitive::kPrimShort:
2652 case Primitive::kPrimInt: {
2653 if (value.IsConstant()) {
2654 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2655 __ B(slow_path->GetEntryLabel());
2656 } else {
2657 // A division by a non-null constant is valid. We don't need to perform
2658 // any check, so simply fall through.
2659 }
2660 } else {
2661 DCHECK(value.IsRegister()) << value;
2662 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2663 }
2664 break;
2665 }
2666 case Primitive::kPrimLong: {
2667 if (value.IsConstant()) {
2668 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2669 __ B(slow_path->GetEntryLabel());
2670 } else {
2671 // A division by a non-null constant is valid. We don't need to perform
2672 // any check, so simply fall through.
2673 }
2674 } else {
2675 DCHECK(value.IsRegisterPair()) << value;
2676 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2677 __ Beqz(TMP, slow_path->GetEntryLabel());
2678 }
2679 break;
2680 }
2681 default:
2682 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2683 }
2684}
2685
2686void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2687 LocationSummary* locations =
2688 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2689 locations->SetOut(Location::ConstantLocation(constant));
2690}
2691
2692void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2693 // Will be generated at use site.
2694}
2695
2696void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2697 exit->SetLocations(nullptr);
2698}
2699
2700void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2701}
2702
2703void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2704 LocationSummary* locations =
2705 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2706 locations->SetOut(Location::ConstantLocation(constant));
2707}
2708
2709void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2710 // Will be generated at use site.
2711}
2712
2713void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2714 got->SetLocations(nullptr);
2715}
2716
2717void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2718 DCHECK(!successor->IsExitBlock());
2719 HBasicBlock* block = got->GetBlock();
2720 HInstruction* previous = got->GetPrevious();
2721 HLoopInformation* info = block->GetLoopInformation();
2722
2723 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2724 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2725 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2726 return;
2727 }
2728 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2729 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2730 }
2731 if (!codegen_->GoesToNextBlock(block, successor)) {
2732 __ B(codegen_->GetLabelOf(successor));
2733 }
2734}
2735
2736void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2737 HandleGoto(got, got->GetSuccessor());
2738}
2739
2740void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2741 try_boundary->SetLocations(nullptr);
2742}
2743
2744void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2745 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2746 if (!successor->IsExitBlock()) {
2747 HandleGoto(try_boundary, successor);
2748 }
2749}
2750
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002751void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2752 LocationSummary* locations) {
2753 Register dst = locations->Out().AsRegister<Register>();
2754 Register lhs = locations->InAt(0).AsRegister<Register>();
2755 Location rhs_location = locations->InAt(1);
2756 Register rhs_reg = ZERO;
2757 int64_t rhs_imm = 0;
2758 bool use_imm = rhs_location.IsConstant();
2759 if (use_imm) {
2760 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2761 } else {
2762 rhs_reg = rhs_location.AsRegister<Register>();
2763 }
2764
2765 switch (cond) {
2766 case kCondEQ:
2767 case kCondNE:
2768 if (use_imm && IsUint<16>(rhs_imm)) {
2769 __ Xori(dst, lhs, rhs_imm);
2770 } else {
2771 if (use_imm) {
2772 rhs_reg = TMP;
2773 __ LoadConst32(rhs_reg, rhs_imm);
2774 }
2775 __ Xor(dst, lhs, rhs_reg);
2776 }
2777 if (cond == kCondEQ) {
2778 __ Sltiu(dst, dst, 1);
2779 } else {
2780 __ Sltu(dst, ZERO, dst);
2781 }
2782 break;
2783
2784 case kCondLT:
2785 case kCondGE:
2786 if (use_imm && IsInt<16>(rhs_imm)) {
2787 __ Slti(dst, lhs, rhs_imm);
2788 } else {
2789 if (use_imm) {
2790 rhs_reg = TMP;
2791 __ LoadConst32(rhs_reg, rhs_imm);
2792 }
2793 __ Slt(dst, lhs, rhs_reg);
2794 }
2795 if (cond == kCondGE) {
2796 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2797 // only the slt instruction but no sge.
2798 __ Xori(dst, dst, 1);
2799 }
2800 break;
2801
2802 case kCondLE:
2803 case kCondGT:
2804 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2805 // Simulate lhs <= rhs via lhs < rhs + 1.
2806 __ Slti(dst, lhs, rhs_imm + 1);
2807 if (cond == kCondGT) {
2808 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2809 // only the slti instruction but no sgti.
2810 __ Xori(dst, dst, 1);
2811 }
2812 } else {
2813 if (use_imm) {
2814 rhs_reg = TMP;
2815 __ LoadConst32(rhs_reg, rhs_imm);
2816 }
2817 __ Slt(dst, rhs_reg, lhs);
2818 if (cond == kCondLE) {
2819 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2820 // only the slt instruction but no sle.
2821 __ Xori(dst, dst, 1);
2822 }
2823 }
2824 break;
2825
2826 case kCondB:
2827 case kCondAE:
2828 if (use_imm && IsInt<16>(rhs_imm)) {
2829 // Sltiu sign-extends its 16-bit immediate operand before
2830 // the comparison and thus lets us compare directly with
2831 // unsigned values in the ranges [0, 0x7fff] and
2832 // [0xffff8000, 0xffffffff].
2833 __ Sltiu(dst, lhs, rhs_imm);
2834 } else {
2835 if (use_imm) {
2836 rhs_reg = TMP;
2837 __ LoadConst32(rhs_reg, rhs_imm);
2838 }
2839 __ Sltu(dst, lhs, rhs_reg);
2840 }
2841 if (cond == kCondAE) {
2842 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2843 // only the sltu instruction but no sgeu.
2844 __ Xori(dst, dst, 1);
2845 }
2846 break;
2847
2848 case kCondBE:
2849 case kCondA:
2850 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2851 // Simulate lhs <= rhs via lhs < rhs + 1.
2852 // Note that this only works if rhs + 1 does not overflow
2853 // to 0, hence the check above.
2854 // Sltiu sign-extends its 16-bit immediate operand before
2855 // the comparison and thus lets us compare directly with
2856 // unsigned values in the ranges [0, 0x7fff] and
2857 // [0xffff8000, 0xffffffff].
2858 __ Sltiu(dst, lhs, rhs_imm + 1);
2859 if (cond == kCondA) {
2860 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2861 // only the sltiu instruction but no sgtiu.
2862 __ Xori(dst, dst, 1);
2863 }
2864 } else {
2865 if (use_imm) {
2866 rhs_reg = TMP;
2867 __ LoadConst32(rhs_reg, rhs_imm);
2868 }
2869 __ Sltu(dst, rhs_reg, lhs);
2870 if (cond == kCondBE) {
2871 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2872 // only the sltu instruction but no sleu.
2873 __ Xori(dst, dst, 1);
2874 }
2875 }
2876 break;
2877 }
2878}
2879
2880void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2881 LocationSummary* locations,
2882 MipsLabel* label) {
2883 Register lhs = locations->InAt(0).AsRegister<Register>();
2884 Location rhs_location = locations->InAt(1);
2885 Register rhs_reg = ZERO;
2886 int32_t rhs_imm = 0;
2887 bool use_imm = rhs_location.IsConstant();
2888 if (use_imm) {
2889 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2890 } else {
2891 rhs_reg = rhs_location.AsRegister<Register>();
2892 }
2893
2894 if (use_imm && rhs_imm == 0) {
2895 switch (cond) {
2896 case kCondEQ:
2897 case kCondBE: // <= 0 if zero
2898 __ Beqz(lhs, label);
2899 break;
2900 case kCondNE:
2901 case kCondA: // > 0 if non-zero
2902 __ Bnez(lhs, label);
2903 break;
2904 case kCondLT:
2905 __ Bltz(lhs, label);
2906 break;
2907 case kCondGE:
2908 __ Bgez(lhs, label);
2909 break;
2910 case kCondLE:
2911 __ Blez(lhs, label);
2912 break;
2913 case kCondGT:
2914 __ Bgtz(lhs, label);
2915 break;
2916 case kCondB: // always false
2917 break;
2918 case kCondAE: // always true
2919 __ B(label);
2920 break;
2921 }
2922 } else {
2923 if (use_imm) {
2924 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2925 rhs_reg = TMP;
2926 __ LoadConst32(rhs_reg, rhs_imm);
2927 }
2928 switch (cond) {
2929 case kCondEQ:
2930 __ Beq(lhs, rhs_reg, label);
2931 break;
2932 case kCondNE:
2933 __ Bne(lhs, rhs_reg, label);
2934 break;
2935 case kCondLT:
2936 __ Blt(lhs, rhs_reg, label);
2937 break;
2938 case kCondGE:
2939 __ Bge(lhs, rhs_reg, label);
2940 break;
2941 case kCondLE:
2942 __ Bge(rhs_reg, lhs, label);
2943 break;
2944 case kCondGT:
2945 __ Blt(rhs_reg, lhs, label);
2946 break;
2947 case kCondB:
2948 __ Bltu(lhs, rhs_reg, label);
2949 break;
2950 case kCondAE:
2951 __ Bgeu(lhs, rhs_reg, label);
2952 break;
2953 case kCondBE:
2954 __ Bgeu(rhs_reg, lhs, label);
2955 break;
2956 case kCondA:
2957 __ Bltu(rhs_reg, lhs, label);
2958 break;
2959 }
2960 }
2961}
2962
2963void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2964 LocationSummary* locations,
2965 MipsLabel* label) {
2966 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2967 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2968 Location rhs_location = locations->InAt(1);
2969 Register rhs_high = ZERO;
2970 Register rhs_low = ZERO;
2971 int64_t imm = 0;
2972 uint32_t imm_high = 0;
2973 uint32_t imm_low = 0;
2974 bool use_imm = rhs_location.IsConstant();
2975 if (use_imm) {
2976 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2977 imm_high = High32Bits(imm);
2978 imm_low = Low32Bits(imm);
2979 } else {
2980 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2981 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2982 }
2983
2984 if (use_imm && imm == 0) {
2985 switch (cond) {
2986 case kCondEQ:
2987 case kCondBE: // <= 0 if zero
2988 __ Or(TMP, lhs_high, lhs_low);
2989 __ Beqz(TMP, label);
2990 break;
2991 case kCondNE:
2992 case kCondA: // > 0 if non-zero
2993 __ Or(TMP, lhs_high, lhs_low);
2994 __ Bnez(TMP, label);
2995 break;
2996 case kCondLT:
2997 __ Bltz(lhs_high, label);
2998 break;
2999 case kCondGE:
3000 __ Bgez(lhs_high, label);
3001 break;
3002 case kCondLE:
3003 __ Or(TMP, lhs_high, lhs_low);
3004 __ Sra(AT, lhs_high, 31);
3005 __ Bgeu(AT, TMP, label);
3006 break;
3007 case kCondGT:
3008 __ Or(TMP, lhs_high, lhs_low);
3009 __ Sra(AT, lhs_high, 31);
3010 __ Bltu(AT, TMP, label);
3011 break;
3012 case kCondB: // always false
3013 break;
3014 case kCondAE: // always true
3015 __ B(label);
3016 break;
3017 }
3018 } else if (use_imm) {
3019 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3020 switch (cond) {
3021 case kCondEQ:
3022 __ LoadConst32(TMP, imm_high);
3023 __ Xor(TMP, TMP, lhs_high);
3024 __ LoadConst32(AT, imm_low);
3025 __ Xor(AT, AT, lhs_low);
3026 __ Or(TMP, TMP, AT);
3027 __ Beqz(TMP, label);
3028 break;
3029 case kCondNE:
3030 __ LoadConst32(TMP, imm_high);
3031 __ Xor(TMP, TMP, lhs_high);
3032 __ LoadConst32(AT, imm_low);
3033 __ Xor(AT, AT, lhs_low);
3034 __ Or(TMP, TMP, AT);
3035 __ Bnez(TMP, label);
3036 break;
3037 case kCondLT:
3038 __ LoadConst32(TMP, imm_high);
3039 __ Blt(lhs_high, TMP, label);
3040 __ Slt(TMP, TMP, lhs_high);
3041 __ LoadConst32(AT, imm_low);
3042 __ Sltu(AT, lhs_low, AT);
3043 __ Blt(TMP, AT, label);
3044 break;
3045 case kCondGE:
3046 __ LoadConst32(TMP, imm_high);
3047 __ Blt(TMP, lhs_high, label);
3048 __ Slt(TMP, lhs_high, TMP);
3049 __ LoadConst32(AT, imm_low);
3050 __ Sltu(AT, lhs_low, AT);
3051 __ Or(TMP, TMP, AT);
3052 __ Beqz(TMP, label);
3053 break;
3054 case kCondLE:
3055 __ LoadConst32(TMP, imm_high);
3056 __ Blt(lhs_high, TMP, label);
3057 __ Slt(TMP, TMP, lhs_high);
3058 __ LoadConst32(AT, imm_low);
3059 __ Sltu(AT, AT, lhs_low);
3060 __ Or(TMP, TMP, AT);
3061 __ Beqz(TMP, label);
3062 break;
3063 case kCondGT:
3064 __ LoadConst32(TMP, imm_high);
3065 __ Blt(TMP, lhs_high, label);
3066 __ Slt(TMP, lhs_high, TMP);
3067 __ LoadConst32(AT, imm_low);
3068 __ Sltu(AT, AT, lhs_low);
3069 __ Blt(TMP, AT, label);
3070 break;
3071 case kCondB:
3072 __ LoadConst32(TMP, imm_high);
3073 __ Bltu(lhs_high, TMP, label);
3074 __ Sltu(TMP, TMP, lhs_high);
3075 __ LoadConst32(AT, imm_low);
3076 __ Sltu(AT, lhs_low, AT);
3077 __ Blt(TMP, AT, label);
3078 break;
3079 case kCondAE:
3080 __ LoadConst32(TMP, imm_high);
3081 __ Bltu(TMP, lhs_high, label);
3082 __ Sltu(TMP, lhs_high, TMP);
3083 __ LoadConst32(AT, imm_low);
3084 __ Sltu(AT, lhs_low, AT);
3085 __ Or(TMP, TMP, AT);
3086 __ Beqz(TMP, label);
3087 break;
3088 case kCondBE:
3089 __ LoadConst32(TMP, imm_high);
3090 __ Bltu(lhs_high, TMP, label);
3091 __ Sltu(TMP, TMP, lhs_high);
3092 __ LoadConst32(AT, imm_low);
3093 __ Sltu(AT, AT, lhs_low);
3094 __ Or(TMP, TMP, AT);
3095 __ Beqz(TMP, label);
3096 break;
3097 case kCondA:
3098 __ LoadConst32(TMP, imm_high);
3099 __ Bltu(TMP, lhs_high, label);
3100 __ Sltu(TMP, lhs_high, TMP);
3101 __ LoadConst32(AT, imm_low);
3102 __ Sltu(AT, AT, lhs_low);
3103 __ Blt(TMP, AT, label);
3104 break;
3105 }
3106 } else {
3107 switch (cond) {
3108 case kCondEQ:
3109 __ Xor(TMP, lhs_high, rhs_high);
3110 __ Xor(AT, lhs_low, rhs_low);
3111 __ Or(TMP, TMP, AT);
3112 __ Beqz(TMP, label);
3113 break;
3114 case kCondNE:
3115 __ Xor(TMP, lhs_high, rhs_high);
3116 __ Xor(AT, lhs_low, rhs_low);
3117 __ Or(TMP, TMP, AT);
3118 __ Bnez(TMP, label);
3119 break;
3120 case kCondLT:
3121 __ Blt(lhs_high, rhs_high, label);
3122 __ Slt(TMP, rhs_high, lhs_high);
3123 __ Sltu(AT, lhs_low, rhs_low);
3124 __ Blt(TMP, AT, label);
3125 break;
3126 case kCondGE:
3127 __ Blt(rhs_high, lhs_high, label);
3128 __ Slt(TMP, lhs_high, rhs_high);
3129 __ Sltu(AT, lhs_low, rhs_low);
3130 __ Or(TMP, TMP, AT);
3131 __ Beqz(TMP, label);
3132 break;
3133 case kCondLE:
3134 __ Blt(lhs_high, rhs_high, label);
3135 __ Slt(TMP, rhs_high, lhs_high);
3136 __ Sltu(AT, rhs_low, lhs_low);
3137 __ Or(TMP, TMP, AT);
3138 __ Beqz(TMP, label);
3139 break;
3140 case kCondGT:
3141 __ Blt(rhs_high, lhs_high, label);
3142 __ Slt(TMP, lhs_high, rhs_high);
3143 __ Sltu(AT, rhs_low, lhs_low);
3144 __ Blt(TMP, AT, label);
3145 break;
3146 case kCondB:
3147 __ Bltu(lhs_high, rhs_high, label);
3148 __ Sltu(TMP, rhs_high, lhs_high);
3149 __ Sltu(AT, lhs_low, rhs_low);
3150 __ Blt(TMP, AT, label);
3151 break;
3152 case kCondAE:
3153 __ Bltu(rhs_high, lhs_high, label);
3154 __ Sltu(TMP, lhs_high, rhs_high);
3155 __ Sltu(AT, lhs_low, rhs_low);
3156 __ Or(TMP, TMP, AT);
3157 __ Beqz(TMP, label);
3158 break;
3159 case kCondBE:
3160 __ Bltu(lhs_high, rhs_high, label);
3161 __ Sltu(TMP, rhs_high, lhs_high);
3162 __ Sltu(AT, rhs_low, lhs_low);
3163 __ Or(TMP, TMP, AT);
3164 __ Beqz(TMP, label);
3165 break;
3166 case kCondA:
3167 __ Bltu(rhs_high, lhs_high, label);
3168 __ Sltu(TMP, lhs_high, rhs_high);
3169 __ Sltu(AT, rhs_low, lhs_low);
3170 __ Blt(TMP, AT, label);
3171 break;
3172 }
3173 }
3174}
3175
3176void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3177 bool gt_bias,
3178 Primitive::Type type,
3179 LocationSummary* locations,
3180 MipsLabel* label) {
3181 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3182 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3183 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3184 if (type == Primitive::kPrimFloat) {
3185 if (isR6) {
3186 switch (cond) {
3187 case kCondEQ:
3188 __ CmpEqS(FTMP, lhs, rhs);
3189 __ Bc1nez(FTMP, label);
3190 break;
3191 case kCondNE:
3192 __ CmpEqS(FTMP, lhs, rhs);
3193 __ Bc1eqz(FTMP, label);
3194 break;
3195 case kCondLT:
3196 if (gt_bias) {
3197 __ CmpLtS(FTMP, lhs, rhs);
3198 } else {
3199 __ CmpUltS(FTMP, lhs, rhs);
3200 }
3201 __ Bc1nez(FTMP, label);
3202 break;
3203 case kCondLE:
3204 if (gt_bias) {
3205 __ CmpLeS(FTMP, lhs, rhs);
3206 } else {
3207 __ CmpUleS(FTMP, lhs, rhs);
3208 }
3209 __ Bc1nez(FTMP, label);
3210 break;
3211 case kCondGT:
3212 if (gt_bias) {
3213 __ CmpUltS(FTMP, rhs, lhs);
3214 } else {
3215 __ CmpLtS(FTMP, rhs, lhs);
3216 }
3217 __ Bc1nez(FTMP, label);
3218 break;
3219 case kCondGE:
3220 if (gt_bias) {
3221 __ CmpUleS(FTMP, rhs, lhs);
3222 } else {
3223 __ CmpLeS(FTMP, rhs, lhs);
3224 }
3225 __ Bc1nez(FTMP, label);
3226 break;
3227 default:
3228 LOG(FATAL) << "Unexpected non-floating-point condition";
3229 }
3230 } else {
3231 switch (cond) {
3232 case kCondEQ:
3233 __ CeqS(0, lhs, rhs);
3234 __ Bc1t(0, label);
3235 break;
3236 case kCondNE:
3237 __ CeqS(0, lhs, rhs);
3238 __ Bc1f(0, label);
3239 break;
3240 case kCondLT:
3241 if (gt_bias) {
3242 __ ColtS(0, lhs, rhs);
3243 } else {
3244 __ CultS(0, lhs, rhs);
3245 }
3246 __ Bc1t(0, label);
3247 break;
3248 case kCondLE:
3249 if (gt_bias) {
3250 __ ColeS(0, lhs, rhs);
3251 } else {
3252 __ CuleS(0, lhs, rhs);
3253 }
3254 __ Bc1t(0, label);
3255 break;
3256 case kCondGT:
3257 if (gt_bias) {
3258 __ CultS(0, rhs, lhs);
3259 } else {
3260 __ ColtS(0, rhs, lhs);
3261 }
3262 __ Bc1t(0, label);
3263 break;
3264 case kCondGE:
3265 if (gt_bias) {
3266 __ CuleS(0, rhs, lhs);
3267 } else {
3268 __ ColeS(0, rhs, lhs);
3269 }
3270 __ Bc1t(0, label);
3271 break;
3272 default:
3273 LOG(FATAL) << "Unexpected non-floating-point condition";
3274 }
3275 }
3276 } else {
3277 DCHECK_EQ(type, Primitive::kPrimDouble);
3278 if (isR6) {
3279 switch (cond) {
3280 case kCondEQ:
3281 __ CmpEqD(FTMP, lhs, rhs);
3282 __ Bc1nez(FTMP, label);
3283 break;
3284 case kCondNE:
3285 __ CmpEqD(FTMP, lhs, rhs);
3286 __ Bc1eqz(FTMP, label);
3287 break;
3288 case kCondLT:
3289 if (gt_bias) {
3290 __ CmpLtD(FTMP, lhs, rhs);
3291 } else {
3292 __ CmpUltD(FTMP, lhs, rhs);
3293 }
3294 __ Bc1nez(FTMP, label);
3295 break;
3296 case kCondLE:
3297 if (gt_bias) {
3298 __ CmpLeD(FTMP, lhs, rhs);
3299 } else {
3300 __ CmpUleD(FTMP, lhs, rhs);
3301 }
3302 __ Bc1nez(FTMP, label);
3303 break;
3304 case kCondGT:
3305 if (gt_bias) {
3306 __ CmpUltD(FTMP, rhs, lhs);
3307 } else {
3308 __ CmpLtD(FTMP, rhs, lhs);
3309 }
3310 __ Bc1nez(FTMP, label);
3311 break;
3312 case kCondGE:
3313 if (gt_bias) {
3314 __ CmpUleD(FTMP, rhs, lhs);
3315 } else {
3316 __ CmpLeD(FTMP, rhs, lhs);
3317 }
3318 __ Bc1nez(FTMP, label);
3319 break;
3320 default:
3321 LOG(FATAL) << "Unexpected non-floating-point condition";
3322 }
3323 } else {
3324 switch (cond) {
3325 case kCondEQ:
3326 __ CeqD(0, lhs, rhs);
3327 __ Bc1t(0, label);
3328 break;
3329 case kCondNE:
3330 __ CeqD(0, lhs, rhs);
3331 __ Bc1f(0, label);
3332 break;
3333 case kCondLT:
3334 if (gt_bias) {
3335 __ ColtD(0, lhs, rhs);
3336 } else {
3337 __ CultD(0, lhs, rhs);
3338 }
3339 __ Bc1t(0, label);
3340 break;
3341 case kCondLE:
3342 if (gt_bias) {
3343 __ ColeD(0, lhs, rhs);
3344 } else {
3345 __ CuleD(0, lhs, rhs);
3346 }
3347 __ Bc1t(0, label);
3348 break;
3349 case kCondGT:
3350 if (gt_bias) {
3351 __ CultD(0, rhs, lhs);
3352 } else {
3353 __ ColtD(0, rhs, lhs);
3354 }
3355 __ Bc1t(0, label);
3356 break;
3357 case kCondGE:
3358 if (gt_bias) {
3359 __ CuleD(0, rhs, lhs);
3360 } else {
3361 __ ColeD(0, rhs, lhs);
3362 }
3363 __ Bc1t(0, label);
3364 break;
3365 default:
3366 LOG(FATAL) << "Unexpected non-floating-point condition";
3367 }
3368 }
3369 }
3370}
3371
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003372void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003373 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003374 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003375 MipsLabel* false_target) {
3376 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003377
David Brazdil0debae72015-11-12 18:37:00 +00003378 if (true_target == nullptr && false_target == nullptr) {
3379 // Nothing to do. The code always falls through.
3380 return;
3381 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003382 // Constant condition, statically compared against "true" (integer value 1).
3383 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003384 if (true_target != nullptr) {
3385 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003386 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003387 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003388 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003389 if (false_target != nullptr) {
3390 __ B(false_target);
3391 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003392 }
David Brazdil0debae72015-11-12 18:37:00 +00003393 return;
3394 }
3395
3396 // The following code generates these patterns:
3397 // (1) true_target == nullptr && false_target != nullptr
3398 // - opposite condition true => branch to false_target
3399 // (2) true_target != nullptr && false_target == nullptr
3400 // - condition true => branch to true_target
3401 // (3) true_target != nullptr && false_target != nullptr
3402 // - condition true => branch to true_target
3403 // - branch to false_target
3404 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003405 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003406 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003407 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003408 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003409 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3410 } else {
3411 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3412 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003413 } else {
3414 // The condition instruction has not been materialized, use its inputs as
3415 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003416 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003417 Primitive::Type type = condition->InputAt(0)->GetType();
3418 LocationSummary* locations = cond->GetLocations();
3419 IfCondition if_cond = condition->GetCondition();
3420 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003421
David Brazdil0debae72015-11-12 18:37:00 +00003422 if (true_target == nullptr) {
3423 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003424 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003425 }
3426
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003427 switch (type) {
3428 default:
3429 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3430 break;
3431 case Primitive::kPrimLong:
3432 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3433 break;
3434 case Primitive::kPrimFloat:
3435 case Primitive::kPrimDouble:
3436 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3437 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003438 }
3439 }
David Brazdil0debae72015-11-12 18:37:00 +00003440
3441 // If neither branch falls through (case 3), the conditional branch to `true_target`
3442 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3443 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003444 __ B(false_target);
3445 }
3446}
3447
3448void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3449 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003450 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003451 locations->SetInAt(0, Location::RequiresRegister());
3452 }
3453}
3454
3455void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003456 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3457 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3458 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3459 nullptr : codegen_->GetLabelOf(true_successor);
3460 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3461 nullptr : codegen_->GetLabelOf(false_successor);
3462 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003463}
3464
3465void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3466 LocationSummary* locations = new (GetGraph()->GetArena())
3467 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003468 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003469 locations->SetInAt(0, Location::RequiresRegister());
3470 }
3471}
3472
3473void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003474 SlowPathCodeMIPS* slow_path =
3475 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003476 GenerateTestAndBranch(deoptimize,
3477 /* condition_input_index */ 0,
3478 slow_path->GetEntryLabel(),
3479 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003480}
3481
David Brazdil74eb1b22015-12-14 11:44:01 +00003482void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3484 if (Primitive::IsFloatingPointType(select->GetType())) {
3485 locations->SetInAt(0, Location::RequiresFpuRegister());
3486 locations->SetInAt(1, Location::RequiresFpuRegister());
3487 } else {
3488 locations->SetInAt(0, Location::RequiresRegister());
3489 locations->SetInAt(1, Location::RequiresRegister());
3490 }
3491 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3492 locations->SetInAt(2, Location::RequiresRegister());
3493 }
3494 locations->SetOut(Location::SameAsFirstInput());
3495}
3496
3497void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3498 LocationSummary* locations = select->GetLocations();
3499 MipsLabel false_target;
3500 GenerateTestAndBranch(select,
3501 /* condition_input_index */ 2,
3502 /* true_target */ nullptr,
3503 &false_target);
3504 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3505 __ Bind(&false_target);
3506}
3507
David Srbecky0cf44932015-12-09 14:09:59 +00003508void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3509 new (GetGraph()->GetArena()) LocationSummary(info);
3510}
3511
David Srbeckyd28f4a02016-03-14 17:14:24 +00003512void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3513 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003514}
3515
3516void CodeGeneratorMIPS::GenerateNop() {
3517 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003518}
3519
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003520void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3521 Primitive::Type field_type = field_info.GetFieldType();
3522 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3523 bool generate_volatile = field_info.IsVolatile() && is_wide;
3524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003525 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003526
3527 locations->SetInAt(0, Location::RequiresRegister());
3528 if (generate_volatile) {
3529 InvokeRuntimeCallingConvention calling_convention;
3530 // need A0 to hold base + offset
3531 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3532 if (field_type == Primitive::kPrimLong) {
3533 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3534 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003535 // Use Location::Any() to prevent situations when running out of available fp registers.
3536 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003537 // Need some temp core regs since FP results are returned in core registers
3538 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3539 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3540 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3541 }
3542 } else {
3543 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3544 locations->SetOut(Location::RequiresFpuRegister());
3545 } else {
3546 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3547 }
3548 }
3549}
3550
3551void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3552 const FieldInfo& field_info,
3553 uint32_t dex_pc) {
3554 Primitive::Type type = field_info.GetFieldType();
3555 LocationSummary* locations = instruction->GetLocations();
3556 Register obj = locations->InAt(0).AsRegister<Register>();
3557 LoadOperandType load_type = kLoadUnsignedByte;
3558 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003559 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003560 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003561
3562 switch (type) {
3563 case Primitive::kPrimBoolean:
3564 load_type = kLoadUnsignedByte;
3565 break;
3566 case Primitive::kPrimByte:
3567 load_type = kLoadSignedByte;
3568 break;
3569 case Primitive::kPrimShort:
3570 load_type = kLoadSignedHalfword;
3571 break;
3572 case Primitive::kPrimChar:
3573 load_type = kLoadUnsignedHalfword;
3574 break;
3575 case Primitive::kPrimInt:
3576 case Primitive::kPrimFloat:
3577 case Primitive::kPrimNot:
3578 load_type = kLoadWord;
3579 break;
3580 case Primitive::kPrimLong:
3581 case Primitive::kPrimDouble:
3582 load_type = kLoadDoubleword;
3583 break;
3584 case Primitive::kPrimVoid:
3585 LOG(FATAL) << "Unreachable type " << type;
3586 UNREACHABLE();
3587 }
3588
3589 if (is_volatile && load_type == kLoadDoubleword) {
3590 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003591 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003592 // Do implicit Null check
3593 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3594 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01003595 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003596 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3597 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003598 // FP results are returned in core registers. Need to move them.
3599 Location out = locations->Out();
3600 if (out.IsFpuRegister()) {
3601 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3602 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3603 out.AsFpuRegister<FRegister>());
3604 } else {
3605 DCHECK(out.IsDoubleStackSlot());
3606 __ StoreToOffset(kStoreWord,
3607 locations->GetTemp(1).AsRegister<Register>(),
3608 SP,
3609 out.GetStackIndex());
3610 __ StoreToOffset(kStoreWord,
3611 locations->GetTemp(2).AsRegister<Register>(),
3612 SP,
3613 out.GetStackIndex() + 4);
3614 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003615 }
3616 } else {
3617 if (!Primitive::IsFloatingPointType(type)) {
3618 Register dst;
3619 if (type == Primitive::kPrimLong) {
3620 DCHECK(locations->Out().IsRegisterPair());
3621 dst = locations->Out().AsRegisterPairLow<Register>();
3622 } else {
3623 DCHECK(locations->Out().IsRegister());
3624 dst = locations->Out().AsRegister<Register>();
3625 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003626 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003627 } else {
3628 DCHECK(locations->Out().IsFpuRegister());
3629 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3630 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003631 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003632 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003633 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003634 }
3635 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003636 }
3637
3638 if (is_volatile) {
3639 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3640 }
3641}
3642
3643void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3644 Primitive::Type field_type = field_info.GetFieldType();
3645 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3646 bool generate_volatile = field_info.IsVolatile() && is_wide;
3647 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003648 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003649
3650 locations->SetInAt(0, Location::RequiresRegister());
3651 if (generate_volatile) {
3652 InvokeRuntimeCallingConvention calling_convention;
3653 // need A0 to hold base + offset
3654 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3655 if (field_type == Primitive::kPrimLong) {
3656 locations->SetInAt(1, Location::RegisterPairLocation(
3657 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3658 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003659 // Use Location::Any() to prevent situations when running out of available fp registers.
3660 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003661 // Pass FP parameters in core registers.
3662 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3663 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3664 }
3665 } else {
3666 if (Primitive::IsFloatingPointType(field_type)) {
3667 locations->SetInAt(1, Location::RequiresFpuRegister());
3668 } else {
3669 locations->SetInAt(1, Location::RequiresRegister());
3670 }
3671 }
3672}
3673
3674void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3675 const FieldInfo& field_info,
3676 uint32_t dex_pc) {
3677 Primitive::Type type = field_info.GetFieldType();
3678 LocationSummary* locations = instruction->GetLocations();
3679 Register obj = locations->InAt(0).AsRegister<Register>();
3680 StoreOperandType store_type = kStoreByte;
3681 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003682 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003683 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003684
3685 switch (type) {
3686 case Primitive::kPrimBoolean:
3687 case Primitive::kPrimByte:
3688 store_type = kStoreByte;
3689 break;
3690 case Primitive::kPrimShort:
3691 case Primitive::kPrimChar:
3692 store_type = kStoreHalfword;
3693 break;
3694 case Primitive::kPrimInt:
3695 case Primitive::kPrimFloat:
3696 case Primitive::kPrimNot:
3697 store_type = kStoreWord;
3698 break;
3699 case Primitive::kPrimLong:
3700 case Primitive::kPrimDouble:
3701 store_type = kStoreDoubleword;
3702 break;
3703 case Primitive::kPrimVoid:
3704 LOG(FATAL) << "Unreachable type " << type;
3705 UNREACHABLE();
3706 }
3707
3708 if (is_volatile) {
3709 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3710 }
3711
3712 if (is_volatile && store_type == kStoreDoubleword) {
3713 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003714 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003715 // Do implicit Null check.
3716 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3717 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3718 if (type == Primitive::kPrimDouble) {
3719 // Pass FP parameters in core registers.
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003720 Location in = locations->InAt(1);
3721 if (in.IsFpuRegister()) {
3722 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(), in.AsFpuRegister<FRegister>());
3723 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3724 in.AsFpuRegister<FRegister>());
3725 } else if (in.IsDoubleStackSlot()) {
3726 __ LoadFromOffset(kLoadWord,
3727 locations->GetTemp(1).AsRegister<Register>(),
3728 SP,
3729 in.GetStackIndex());
3730 __ LoadFromOffset(kLoadWord,
3731 locations->GetTemp(2).AsRegister<Register>(),
3732 SP,
3733 in.GetStackIndex() + 4);
3734 } else {
3735 DCHECK(in.IsConstant());
3736 DCHECK(in.GetConstant()->IsDoubleConstant());
3737 int64_t value = bit_cast<int64_t, double>(in.GetConstant()->AsDoubleConstant()->GetValue());
3738 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
3739 locations->GetTemp(1).AsRegister<Register>(),
3740 value);
3741 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003742 }
Serban Constantinescufca16662016-07-14 09:21:59 +01003743 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003744 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3745 } else {
3746 if (!Primitive::IsFloatingPointType(type)) {
3747 Register src;
3748 if (type == Primitive::kPrimLong) {
3749 DCHECK(locations->InAt(1).IsRegisterPair());
3750 src = locations->InAt(1).AsRegisterPairLow<Register>();
3751 } else {
3752 DCHECK(locations->InAt(1).IsRegister());
3753 src = locations->InAt(1).AsRegister<Register>();
3754 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003755 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003756 } else {
3757 DCHECK(locations->InAt(1).IsFpuRegister());
3758 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3759 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003760 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003761 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003762 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003763 }
3764 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003765 }
3766
3767 // TODO: memory barriers?
3768 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3769 DCHECK(locations->InAt(1).IsRegister());
3770 Register src = locations->InAt(1).AsRegister<Register>();
3771 codegen_->MarkGCCard(obj, src);
3772 }
3773
3774 if (is_volatile) {
3775 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3776 }
3777}
3778
3779void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3780 HandleFieldGet(instruction, instruction->GetFieldInfo());
3781}
3782
3783void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3784 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3785}
3786
3787void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3788 HandleFieldSet(instruction, instruction->GetFieldInfo());
3789}
3790
3791void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3792 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3793}
3794
Alexey Frunze06a46c42016-07-19 15:00:40 -07003795void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
3796 HInstruction* instruction ATTRIBUTE_UNUSED,
3797 Location root,
3798 Register obj,
3799 uint32_t offset) {
3800 Register root_reg = root.AsRegister<Register>();
3801 if (kEmitCompilerReadBarrier) {
3802 UNIMPLEMENTED(FATAL) << "for read barrier";
3803 } else {
3804 // Plain GC root load with no read barrier.
3805 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3806 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
3807 // Note that GC roots are not affected by heap poisoning, thus we
3808 // do not have to unpoison `root_reg` here.
3809 }
3810}
3811
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003812void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3813 LocationSummary::CallKind call_kind =
3814 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3816 locations->SetInAt(0, Location::RequiresRegister());
3817 locations->SetInAt(1, Location::RequiresRegister());
3818 // The output does overlap inputs.
3819 // Note that TypeCheckSlowPathMIPS uses this register too.
3820 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3821}
3822
3823void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3824 LocationSummary* locations = instruction->GetLocations();
3825 Register obj = locations->InAt(0).AsRegister<Register>();
3826 Register cls = locations->InAt(1).AsRegister<Register>();
3827 Register out = locations->Out().AsRegister<Register>();
3828
3829 MipsLabel done;
3830
3831 // Return 0 if `obj` is null.
3832 // TODO: Avoid this check if we know `obj` is not null.
3833 __ Move(out, ZERO);
3834 __ Beqz(obj, &done);
3835
3836 // Compare the class of `obj` with `cls`.
3837 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3838 if (instruction->IsExactCheck()) {
3839 // Classes must be equal for the instanceof to succeed.
3840 __ Xor(out, out, cls);
3841 __ Sltiu(out, out, 1);
3842 } else {
3843 // If the classes are not equal, we go into a slow path.
3844 DCHECK(locations->OnlyCallsOnSlowPath());
3845 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3846 codegen_->AddSlowPath(slow_path);
3847 __ Bne(out, cls, slow_path->GetEntryLabel());
3848 __ LoadConst32(out, 1);
3849 __ Bind(slow_path->GetExitLabel());
3850 }
3851
3852 __ Bind(&done);
3853}
3854
3855void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3857 locations->SetOut(Location::ConstantLocation(constant));
3858}
3859
3860void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3861 // Will be generated at use site.
3862}
3863
3864void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3866 locations->SetOut(Location::ConstantLocation(constant));
3867}
3868
3869void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3870 // Will be generated at use site.
3871}
3872
3873void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3874 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3875 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3876}
3877
3878void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3879 HandleInvoke(invoke);
3880 // The register T0 is required to be used for the hidden argument in
3881 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3882 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3883}
3884
3885void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3886 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3887 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003888 Location receiver = invoke->GetLocations()->InAt(0);
3889 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003890 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003891
3892 // Set the hidden argument.
3893 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3894 invoke->GetDexMethodIndex());
3895
3896 // temp = object->GetClass();
3897 if (receiver.IsStackSlot()) {
3898 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3899 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3900 } else {
3901 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3902 }
3903 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003904 __ LoadFromOffset(kLoadWord, temp, temp,
3905 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3906 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003907 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003908 // temp = temp->GetImtEntryAt(method_offset);
3909 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3910 // T9 = temp->GetEntryPoint();
3911 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3912 // T9();
3913 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003914 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003915 DCHECK(!codegen_->IsLeafMethod());
3916 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3917}
3918
3919void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003920 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3921 if (intrinsic.TryDispatch(invoke)) {
3922 return;
3923 }
3924
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003925 HandleInvoke(invoke);
3926}
3927
3928void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003929 // Explicit clinit checks triggered by static invokes must have been pruned by
3930 // art::PrepareForRegisterAllocation.
3931 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003932
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003933 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3934 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3935 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3936
3937 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
3938 // R6 has PC-relative addressing.
3939 bool has_extra_input = !isR6 &&
3940 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3941 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
3942
3943 if (invoke->HasPcRelativeDexCache()) {
3944 // kDexCachePcRelative is mutually exclusive with
3945 // kDirectAddressWithFixup/kCallDirectWithFixup.
3946 CHECK(!has_extra_input);
3947 has_extra_input = true;
3948 }
3949
Chris Larsen701566a2015-10-27 15:29:13 -07003950 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3951 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003952 if (invoke->GetLocations()->CanCall() && has_extra_input) {
3953 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3954 }
Chris Larsen701566a2015-10-27 15:29:13 -07003955 return;
3956 }
3957
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003958 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003959
3960 // Add the extra input register if either the dex cache array base register
3961 // or the PC-relative base register for accessing literals is needed.
3962 if (has_extra_input) {
3963 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3964 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003965}
3966
Chris Larsen701566a2015-10-27 15:29:13 -07003967static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003968 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003969 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3970 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003971 return true;
3972 }
3973 return false;
3974}
3975
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003976HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07003977 HLoadString::LoadKind desired_string_load_kind) {
3978 if (kEmitCompilerReadBarrier) {
3979 UNIMPLEMENTED(FATAL) << "for read barrier";
3980 }
3981 // We disable PC-relative load when there is an irreducible loop, as the optimization
3982 // is incompatible with it.
3983 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
3984 bool fallback_load = has_irreducible_loops;
3985 switch (desired_string_load_kind) {
3986 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3987 DCHECK(!GetCompilerOptions().GetCompilePic());
3988 break;
3989 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3990 DCHECK(GetCompilerOptions().GetCompilePic());
3991 break;
3992 case HLoadString::LoadKind::kBootImageAddress:
3993 break;
3994 case HLoadString::LoadKind::kDexCacheAddress:
3995 DCHECK(Runtime::Current()->UseJitCompilation());
3996 fallback_load = false;
3997 break;
3998 case HLoadString::LoadKind::kDexCachePcRelative:
3999 DCHECK(!Runtime::Current()->UseJitCompilation());
4000 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4001 // with irreducible loops.
4002 break;
4003 case HLoadString::LoadKind::kDexCacheViaMethod:
4004 fallback_load = false;
4005 break;
4006 }
4007 if (fallback_load) {
4008 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4009 }
4010 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004011}
4012
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004013HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4014 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004015 if (kEmitCompilerReadBarrier) {
4016 UNIMPLEMENTED(FATAL) << "for read barrier";
4017 }
4018 // We disable pc-relative load when there is an irreducible loop, as the optimization
4019 // is incompatible with it.
4020 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4021 bool fallback_load = has_irreducible_loops;
4022 switch (desired_class_load_kind) {
4023 case HLoadClass::LoadKind::kReferrersClass:
4024 fallback_load = false;
4025 break;
4026 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4027 DCHECK(!GetCompilerOptions().GetCompilePic());
4028 break;
4029 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4030 DCHECK(GetCompilerOptions().GetCompilePic());
4031 break;
4032 case HLoadClass::LoadKind::kBootImageAddress:
4033 break;
4034 case HLoadClass::LoadKind::kDexCacheAddress:
4035 DCHECK(Runtime::Current()->UseJitCompilation());
4036 fallback_load = false;
4037 break;
4038 case HLoadClass::LoadKind::kDexCachePcRelative:
4039 DCHECK(!Runtime::Current()->UseJitCompilation());
4040 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4041 // with irreducible loops.
4042 break;
4043 case HLoadClass::LoadKind::kDexCacheViaMethod:
4044 fallback_load = false;
4045 break;
4046 }
4047 if (fallback_load) {
4048 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4049 }
4050 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004051}
4052
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004053Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4054 Register temp) {
4055 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4056 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4057 if (!invoke->GetLocations()->Intrinsified()) {
4058 return location.AsRegister<Register>();
4059 }
4060 // For intrinsics we allow any location, so it may be on the stack.
4061 if (!location.IsRegister()) {
4062 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4063 return temp;
4064 }
4065 // For register locations, check if the register was saved. If so, get it from the stack.
4066 // Note: There is a chance that the register was saved but not overwritten, so we could
4067 // save one load. However, since this is just an intrinsic slow path we prefer this
4068 // simple and more robust approach rather that trying to determine if that's the case.
4069 SlowPathCode* slow_path = GetCurrentSlowPath();
4070 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4071 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4072 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4073 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4074 return temp;
4075 }
4076 return location.AsRegister<Register>();
4077}
4078
Vladimir Markodc151b22015-10-15 18:02:30 +01004079HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4080 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4081 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004082 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4083 // We disable PC-relative load when there is an irreducible loop, as the optimization
4084 // is incompatible with it.
4085 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4086 bool fallback_load = true;
4087 bool fallback_call = true;
4088 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004089 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4090 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004091 fallback_load = has_irreducible_loops;
4092 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004093 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004094 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004095 break;
4096 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004097 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004098 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004099 fallback_call = has_irreducible_loops;
4100 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004101 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004102 // TODO: Implement this type.
4103 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004104 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004105 fallback_call = false;
4106 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004107 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004108 if (fallback_load) {
4109 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4110 dispatch_info.method_load_data = 0;
4111 }
4112 if (fallback_call) {
4113 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4114 dispatch_info.direct_code_ptr = 0;
4115 }
4116 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004117}
4118
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004119void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4120 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004121 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004122 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4123 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4124 bool isR6 = isa_features_.IsR6();
4125 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4126 // R6 has PC-relative addressing.
4127 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4128 (!isR6 &&
4129 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4130 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4131 Register base_reg = has_extra_input
4132 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4133 : ZERO;
4134
4135 // For better instruction scheduling we load the direct code pointer before the method pointer.
4136 switch (code_ptr_location) {
4137 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4138 // T9 = invoke->GetDirectCodePtr();
4139 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4140 break;
4141 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4142 // T9 = code address from literal pool with link-time patch.
4143 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4144 break;
4145 default:
4146 break;
4147 }
4148
4149 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004150 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4151 // temp = thread->string_init_entrypoint
4152 __ LoadFromOffset(kLoadWord,
4153 temp.AsRegister<Register>(),
4154 TR,
4155 invoke->GetStringInitOffset());
4156 break;
4157 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004158 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004159 break;
4160 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4161 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4162 break;
4163 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004164 __ LoadLiteral(temp.AsRegister<Register>(),
4165 base_reg,
4166 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4167 break;
4168 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4169 HMipsDexCacheArraysBase* base =
4170 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4171 int32_t offset =
4172 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4173 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4174 break;
4175 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004176 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004177 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004178 Register reg = temp.AsRegister<Register>();
4179 Register method_reg;
4180 if (current_method.IsRegister()) {
4181 method_reg = current_method.AsRegister<Register>();
4182 } else {
4183 // TODO: use the appropriate DCHECK() here if possible.
4184 // DCHECK(invoke->GetLocations()->Intrinsified());
4185 DCHECK(!current_method.IsValid());
4186 method_reg = reg;
4187 __ Lw(reg, SP, kCurrentMethodStackOffset);
4188 }
4189
4190 // temp = temp->dex_cache_resolved_methods_;
4191 __ LoadFromOffset(kLoadWord,
4192 reg,
4193 method_reg,
4194 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004195 // temp = temp[index_in_cache];
4196 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4197 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004198 __ LoadFromOffset(kLoadWord,
4199 reg,
4200 reg,
4201 CodeGenerator::GetCachePointerOffset(index_in_cache));
4202 break;
4203 }
4204 }
4205
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004206 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004207 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004208 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004209 break;
4210 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004211 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4212 // T9 prepared above for better instruction scheduling.
4213 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004214 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004215 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004216 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004217 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004218 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004219 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4220 LOG(FATAL) << "Unsupported";
4221 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004222 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4223 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004224 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004225 T9,
4226 callee_method.AsRegister<Register>(),
4227 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004228 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004229 // T9()
4230 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004231 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004232 break;
4233 }
4234 DCHECK(!IsLeafMethod());
4235}
4236
4237void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004238 // Explicit clinit checks triggered by static invokes must have been pruned by
4239 // art::PrepareForRegisterAllocation.
4240 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004241
4242 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4243 return;
4244 }
4245
4246 LocationSummary* locations = invoke->GetLocations();
4247 codegen_->GenerateStaticOrDirectCall(invoke,
4248 locations->HasTemps()
4249 ? locations->GetTemp(0)
4250 : Location::NoLocation());
4251 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4252}
4253
Chris Larsen3acee732015-11-18 13:31:08 -08004254void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004255 LocationSummary* locations = invoke->GetLocations();
4256 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004257 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004258 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4259 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4260 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004261 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004262
4263 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004264 DCHECK(receiver.IsRegister());
4265 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4266 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004267 // temp = temp->GetMethodAt(method_offset);
4268 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4269 // T9 = temp->GetEntryPoint();
4270 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4271 // T9();
4272 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004273 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004274}
4275
4276void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4277 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4278 return;
4279 }
4280
4281 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004282 DCHECK(!codegen_->IsLeafMethod());
4283 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4284}
4285
4286void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004287 if (cls->NeedsAccessCheck()) {
4288 InvokeRuntimeCallingConvention calling_convention;
4289 CodeGenerator::CreateLoadClassLocationSummary(
4290 cls,
4291 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4292 Location::RegisterLocation(V0),
4293 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4294 return;
4295 }
4296
4297 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4298 ? LocationSummary::kCallOnSlowPath
4299 : LocationSummary::kNoCall;
4300 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4301 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4302 switch (load_kind) {
4303 // We need an extra register for PC-relative literals on R2.
4304 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4305 case HLoadClass::LoadKind::kBootImageAddress:
4306 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4307 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4308 break;
4309 }
4310 FALLTHROUGH_INTENDED;
4311 // We need an extra register for PC-relative dex cache accesses.
4312 case HLoadClass::LoadKind::kDexCachePcRelative:
4313 case HLoadClass::LoadKind::kReferrersClass:
4314 case HLoadClass::LoadKind::kDexCacheViaMethod:
4315 locations->SetInAt(0, Location::RequiresRegister());
4316 break;
4317 default:
4318 break;
4319 }
4320 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004321}
4322
4323void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4324 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004325 if (cls->NeedsAccessCheck()) {
4326 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004327 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004328 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004329 return;
4330 }
4331
Alexey Frunze06a46c42016-07-19 15:00:40 -07004332 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4333 Location out_loc = locations->Out();
4334 Register out = out_loc.AsRegister<Register>();
4335 Register base_or_current_method_reg;
4336 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4337 switch (load_kind) {
4338 // We need an extra register for PC-relative literals on R2.
4339 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4340 case HLoadClass::LoadKind::kBootImageAddress:
4341 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4342 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4343 break;
4344 // We need an extra register for PC-relative dex cache accesses.
4345 case HLoadClass::LoadKind::kDexCachePcRelative:
4346 case HLoadClass::LoadKind::kReferrersClass:
4347 case HLoadClass::LoadKind::kDexCacheViaMethod:
4348 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4349 break;
4350 default:
4351 base_or_current_method_reg = ZERO;
4352 break;
4353 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004354
Alexey Frunze06a46c42016-07-19 15:00:40 -07004355 bool generate_null_check = false;
4356 switch (load_kind) {
4357 case HLoadClass::LoadKind::kReferrersClass: {
4358 DCHECK(!cls->CanCallRuntime());
4359 DCHECK(!cls->MustGenerateClinitCheck());
4360 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4361 GenerateGcRootFieldLoad(cls,
4362 out_loc,
4363 base_or_current_method_reg,
4364 ArtMethod::DeclaringClassOffset().Int32Value());
4365 break;
4366 }
4367 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4368 DCHECK(!kEmitCompilerReadBarrier);
4369 __ LoadLiteral(out,
4370 base_or_current_method_reg,
4371 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4372 cls->GetTypeIndex()));
4373 break;
4374 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4375 DCHECK(!kEmitCompilerReadBarrier);
4376 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4377 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004378 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004379 if (isR6) {
4380 __ Bind(&info->high_label);
4381 __ Bind(&info->pc_rel_label);
4382 // Add a 32-bit offset to PC.
4383 __ Auipc(out, /* placeholder */ 0x1234);
4384 __ Addiu(out, out, /* placeholder */ 0x5678);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004385 } else {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004386 __ Bind(&info->high_label);
4387 __ Lui(out, /* placeholder */ 0x1234);
4388 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4389 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4390 __ Ori(out, out, /* placeholder */ 0x5678);
4391 // Add a 32-bit offset to PC.
4392 __ Addu(out, out, base_or_current_method_reg);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004393 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004394 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004395 break;
4396 }
4397 case HLoadClass::LoadKind::kBootImageAddress: {
4398 DCHECK(!kEmitCompilerReadBarrier);
4399 DCHECK_NE(cls->GetAddress(), 0u);
4400 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4401 __ LoadLiteral(out,
4402 base_or_current_method_reg,
4403 codegen_->DeduplicateBootImageAddressLiteral(address));
4404 break;
4405 }
4406 case HLoadClass::LoadKind::kDexCacheAddress: {
4407 DCHECK_NE(cls->GetAddress(), 0u);
4408 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4409 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4410 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4411 int16_t offset = Low16Bits(address);
4412 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4413 __ Lui(out, High16Bits(base_address));
4414 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4415 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4416 generate_null_check = !cls->IsInDexCache();
4417 break;
4418 }
4419 case HLoadClass::LoadKind::kDexCachePcRelative: {
4420 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4421 int32_t offset =
4422 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4423 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4424 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4425 generate_null_check = !cls->IsInDexCache();
4426 break;
4427 }
4428 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4429 // /* GcRoot<mirror::Class>[] */ out =
4430 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4431 __ LoadFromOffset(kLoadWord,
4432 out,
4433 base_or_current_method_reg,
4434 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4435 // /* GcRoot<mirror::Class> */ out = out[type_index]
4436 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4437 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4438 generate_null_check = !cls->IsInDexCache();
4439 }
4440 }
4441
4442 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4443 DCHECK(cls->CanCallRuntime());
4444 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4445 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4446 codegen_->AddSlowPath(slow_path);
4447 if (generate_null_check) {
4448 __ Beqz(out, slow_path->GetEntryLabel());
4449 }
4450 if (cls->MustGenerateClinitCheck()) {
4451 GenerateClassInitializationCheck(slow_path, out);
4452 } else {
4453 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004454 }
4455 }
4456}
4457
4458static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004459 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004460}
4461
4462void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4463 LocationSummary* locations =
4464 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4465 locations->SetOut(Location::RequiresRegister());
4466}
4467
4468void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4469 Register out = load->GetLocations()->Out().AsRegister<Register>();
4470 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4471}
4472
4473void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4474 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4475}
4476
4477void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4478 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4479}
4480
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004481void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004482 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004483 ? LocationSummary::kCallOnSlowPath
4484 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004485 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004486 HLoadString::LoadKind load_kind = load->GetLoadKind();
4487 switch (load_kind) {
4488 // We need an extra register for PC-relative literals on R2.
4489 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4490 case HLoadString::LoadKind::kBootImageAddress:
4491 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4492 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4493 break;
4494 }
4495 FALLTHROUGH_INTENDED;
4496 // We need an extra register for PC-relative dex cache accesses.
4497 case HLoadString::LoadKind::kDexCachePcRelative:
4498 case HLoadString::LoadKind::kDexCacheViaMethod:
4499 locations->SetInAt(0, Location::RequiresRegister());
4500 break;
4501 default:
4502 break;
4503 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004504 locations->SetOut(Location::RequiresRegister());
4505}
4506
4507void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004508 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004509 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004510 Location out_loc = locations->Out();
4511 Register out = out_loc.AsRegister<Register>();
4512 Register base_or_current_method_reg;
4513 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4514 switch (load_kind) {
4515 // We need an extra register for PC-relative literals on R2.
4516 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4517 case HLoadString::LoadKind::kBootImageAddress:
4518 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4519 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4520 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004521 default:
4522 base_or_current_method_reg = ZERO;
4523 break;
4524 }
4525
4526 switch (load_kind) {
4527 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4528 DCHECK(!kEmitCompilerReadBarrier);
4529 __ LoadLiteral(out,
4530 base_or_current_method_reg,
4531 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4532 load->GetStringIndex()));
4533 return; // No dex cache slow path.
4534 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4535 DCHECK(!kEmitCompilerReadBarrier);
4536 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4537 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004538 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004539 if (isR6) {
4540 __ Bind(&info->high_label);
4541 __ Bind(&info->pc_rel_label);
4542 // Add a 32-bit offset to PC.
4543 __ Auipc(out, /* placeholder */ 0x1234);
4544 __ Addiu(out, out, /* placeholder */ 0x5678);
4545 } else {
4546 __ Bind(&info->high_label);
4547 __ Lui(out, /* placeholder */ 0x1234);
4548 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4549 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4550 __ Ori(out, out, /* placeholder */ 0x5678);
4551 // Add a 32-bit offset to PC.
4552 __ Addu(out, out, base_or_current_method_reg);
4553 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004554 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004555 return; // No dex cache slow path.
4556 }
4557 case HLoadString::LoadKind::kBootImageAddress: {
4558 DCHECK(!kEmitCompilerReadBarrier);
4559 DCHECK_NE(load->GetAddress(), 0u);
4560 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4561 __ LoadLiteral(out,
4562 base_or_current_method_reg,
4563 codegen_->DeduplicateBootImageAddressLiteral(address));
4564 return; // No dex cache slow path.
4565 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004566 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004567 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004568 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004569
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004570 // TODO: Re-add the compiler code to do string dex cache lookup again.
4571 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4572 codegen_->AddSlowPath(slow_path);
4573 __ B(slow_path->GetEntryLabel());
4574 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004575}
4576
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004577void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4578 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4579 locations->SetOut(Location::ConstantLocation(constant));
4580}
4581
4582void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4583 // Will be generated at use site.
4584}
4585
4586void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4587 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004589 InvokeRuntimeCallingConvention calling_convention;
4590 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4591}
4592
4593void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4594 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01004595 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004596 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4597 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004598 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004599 }
4600 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4601}
4602
4603void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4604 LocationSummary* locations =
4605 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4606 switch (mul->GetResultType()) {
4607 case Primitive::kPrimInt:
4608 case Primitive::kPrimLong:
4609 locations->SetInAt(0, Location::RequiresRegister());
4610 locations->SetInAt(1, Location::RequiresRegister());
4611 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4612 break;
4613
4614 case Primitive::kPrimFloat:
4615 case Primitive::kPrimDouble:
4616 locations->SetInAt(0, Location::RequiresFpuRegister());
4617 locations->SetInAt(1, Location::RequiresFpuRegister());
4618 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4619 break;
4620
4621 default:
4622 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4623 }
4624}
4625
4626void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4627 Primitive::Type type = instruction->GetType();
4628 LocationSummary* locations = instruction->GetLocations();
4629 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4630
4631 switch (type) {
4632 case Primitive::kPrimInt: {
4633 Register dst = locations->Out().AsRegister<Register>();
4634 Register lhs = locations->InAt(0).AsRegister<Register>();
4635 Register rhs = locations->InAt(1).AsRegister<Register>();
4636
4637 if (isR6) {
4638 __ MulR6(dst, lhs, rhs);
4639 } else {
4640 __ MulR2(dst, lhs, rhs);
4641 }
4642 break;
4643 }
4644 case Primitive::kPrimLong: {
4645 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4646 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4647 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4648 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4649 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4650 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4651
4652 // Extra checks to protect caused by the existance of A1_A2.
4653 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4654 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4655 DCHECK_NE(dst_high, lhs_low);
4656 DCHECK_NE(dst_high, rhs_low);
4657
4658 // A_B * C_D
4659 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4660 // dst_lo: [ low(B*D) ]
4661 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4662
4663 if (isR6) {
4664 __ MulR6(TMP, lhs_high, rhs_low);
4665 __ MulR6(dst_high, lhs_low, rhs_high);
4666 __ Addu(dst_high, dst_high, TMP);
4667 __ MuhuR6(TMP, lhs_low, rhs_low);
4668 __ Addu(dst_high, dst_high, TMP);
4669 __ MulR6(dst_low, lhs_low, rhs_low);
4670 } else {
4671 __ MulR2(TMP, lhs_high, rhs_low);
4672 __ MulR2(dst_high, lhs_low, rhs_high);
4673 __ Addu(dst_high, dst_high, TMP);
4674 __ MultuR2(lhs_low, rhs_low);
4675 __ Mfhi(TMP);
4676 __ Addu(dst_high, dst_high, TMP);
4677 __ Mflo(dst_low);
4678 }
4679 break;
4680 }
4681 case Primitive::kPrimFloat:
4682 case Primitive::kPrimDouble: {
4683 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4684 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4685 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4686 if (type == Primitive::kPrimFloat) {
4687 __ MulS(dst, lhs, rhs);
4688 } else {
4689 __ MulD(dst, lhs, rhs);
4690 }
4691 break;
4692 }
4693 default:
4694 LOG(FATAL) << "Unexpected mul type " << type;
4695 }
4696}
4697
4698void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4699 LocationSummary* locations =
4700 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4701 switch (neg->GetResultType()) {
4702 case Primitive::kPrimInt:
4703 case Primitive::kPrimLong:
4704 locations->SetInAt(0, Location::RequiresRegister());
4705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4706 break;
4707
4708 case Primitive::kPrimFloat:
4709 case Primitive::kPrimDouble:
4710 locations->SetInAt(0, Location::RequiresFpuRegister());
4711 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4712 break;
4713
4714 default:
4715 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4716 }
4717}
4718
4719void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4720 Primitive::Type type = instruction->GetType();
4721 LocationSummary* locations = instruction->GetLocations();
4722
4723 switch (type) {
4724 case Primitive::kPrimInt: {
4725 Register dst = locations->Out().AsRegister<Register>();
4726 Register src = locations->InAt(0).AsRegister<Register>();
4727 __ Subu(dst, ZERO, src);
4728 break;
4729 }
4730 case Primitive::kPrimLong: {
4731 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4732 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4733 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4734 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4735 __ Subu(dst_low, ZERO, src_low);
4736 __ Sltu(TMP, ZERO, dst_low);
4737 __ Subu(dst_high, ZERO, src_high);
4738 __ Subu(dst_high, dst_high, TMP);
4739 break;
4740 }
4741 case Primitive::kPrimFloat:
4742 case Primitive::kPrimDouble: {
4743 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4744 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4745 if (type == Primitive::kPrimFloat) {
4746 __ NegS(dst, src);
4747 } else {
4748 __ NegD(dst, src);
4749 }
4750 break;
4751 }
4752 default:
4753 LOG(FATAL) << "Unexpected neg type " << type;
4754 }
4755}
4756
4757void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4758 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004760 InvokeRuntimeCallingConvention calling_convention;
4761 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4762 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4763 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4764 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4765}
4766
4767void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4768 InvokeRuntimeCallingConvention calling_convention;
4769 Register current_method_register = calling_convention.GetRegisterAt(2);
4770 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4771 // Move an uint16_t value to a register.
4772 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004773 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004774 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4775 void*, uint32_t, int32_t, ArtMethod*>();
4776}
4777
4778void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4779 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004780 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004781 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004782 if (instruction->IsStringAlloc()) {
4783 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4784 } else {
4785 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4786 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4787 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004788 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4789}
4790
4791void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004792 if (instruction->IsStringAlloc()) {
4793 // String is allocated through StringFactory. Call NewEmptyString entry point.
4794 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004795 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004796 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4797 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4798 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004799 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00004800 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4801 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004802 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004803 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4804 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004805}
4806
4807void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4808 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4809 locations->SetInAt(0, Location::RequiresRegister());
4810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4811}
4812
4813void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4814 Primitive::Type type = instruction->GetType();
4815 LocationSummary* locations = instruction->GetLocations();
4816
4817 switch (type) {
4818 case Primitive::kPrimInt: {
4819 Register dst = locations->Out().AsRegister<Register>();
4820 Register src = locations->InAt(0).AsRegister<Register>();
4821 __ Nor(dst, src, ZERO);
4822 break;
4823 }
4824
4825 case Primitive::kPrimLong: {
4826 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4827 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4828 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4829 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4830 __ Nor(dst_high, src_high, ZERO);
4831 __ Nor(dst_low, src_low, ZERO);
4832 break;
4833 }
4834
4835 default:
4836 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4837 }
4838}
4839
4840void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4842 locations->SetInAt(0, Location::RequiresRegister());
4843 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4844}
4845
4846void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4847 LocationSummary* locations = instruction->GetLocations();
4848 __ Xori(locations->Out().AsRegister<Register>(),
4849 locations->InAt(0).AsRegister<Register>(),
4850 1);
4851}
4852
4853void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4854 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4855 ? LocationSummary::kCallOnSlowPath
4856 : LocationSummary::kNoCall;
4857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4858 locations->SetInAt(0, Location::RequiresRegister());
4859 if (instruction->HasUses()) {
4860 locations->SetOut(Location::SameAsFirstInput());
4861 }
4862}
4863
Calin Juravle2ae48182016-03-16 14:05:09 +00004864void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4865 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004866 return;
4867 }
4868 Location obj = instruction->GetLocations()->InAt(0);
4869
4870 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004871 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004872}
4873
Calin Juravle2ae48182016-03-16 14:05:09 +00004874void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004875 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004876 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004877
4878 Location obj = instruction->GetLocations()->InAt(0);
4879
4880 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4881}
4882
4883void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004884 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004885}
4886
4887void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4888 HandleBinaryOp(instruction);
4889}
4890
4891void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4892 HandleBinaryOp(instruction);
4893}
4894
4895void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4896 LOG(FATAL) << "Unreachable";
4897}
4898
4899void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4900 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4901}
4902
4903void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4904 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4905 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4906 if (location.IsStackSlot()) {
4907 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4908 } else if (location.IsDoubleStackSlot()) {
4909 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4910 }
4911 locations->SetOut(location);
4912}
4913
4914void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4915 ATTRIBUTE_UNUSED) {
4916 // Nothing to do, the parameter is already at its location.
4917}
4918
4919void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4920 LocationSummary* locations =
4921 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4922 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4923}
4924
4925void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4926 ATTRIBUTE_UNUSED) {
4927 // Nothing to do, the method is already at its location.
4928}
4929
4930void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004932 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004933 locations->SetInAt(i, Location::Any());
4934 }
4935 locations->SetOut(Location::Any());
4936}
4937
4938void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4939 LOG(FATAL) << "Unreachable";
4940}
4941
4942void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4943 Primitive::Type type = rem->GetResultType();
4944 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004945 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4947
4948 switch (type) {
4949 case Primitive::kPrimInt:
4950 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004951 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4953 break;
4954
4955 case Primitive::kPrimLong: {
4956 InvokeRuntimeCallingConvention calling_convention;
4957 locations->SetInAt(0, Location::RegisterPairLocation(
4958 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4959 locations->SetInAt(1, Location::RegisterPairLocation(
4960 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4961 locations->SetOut(calling_convention.GetReturnLocation(type));
4962 break;
4963 }
4964
4965 case Primitive::kPrimFloat:
4966 case Primitive::kPrimDouble: {
4967 InvokeRuntimeCallingConvention calling_convention;
4968 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4969 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4970 locations->SetOut(calling_convention.GetReturnLocation(type));
4971 break;
4972 }
4973
4974 default:
4975 LOG(FATAL) << "Unexpected rem type " << type;
4976 }
4977}
4978
4979void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4980 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004981
4982 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004983 case Primitive::kPrimInt:
4984 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004985 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004986 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01004987 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004988 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4989 break;
4990 }
4991 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01004992 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004993 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004994 break;
4995 }
4996 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01004997 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004998 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004999 break;
5000 }
5001 default:
5002 LOG(FATAL) << "Unexpected rem type " << type;
5003 }
5004}
5005
5006void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5007 memory_barrier->SetLocations(nullptr);
5008}
5009
5010void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5011 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5012}
5013
5014void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5016 Primitive::Type return_type = ret->InputAt(0)->GetType();
5017 locations->SetInAt(0, MipsReturnLocation(return_type));
5018}
5019
5020void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5021 codegen_->GenerateFrameExit();
5022}
5023
5024void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5025 ret->SetLocations(nullptr);
5026}
5027
5028void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5029 codegen_->GenerateFrameExit();
5030}
5031
Alexey Frunze92d90602015-12-18 18:16:36 -08005032void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5033 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005034}
5035
Alexey Frunze92d90602015-12-18 18:16:36 -08005036void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5037 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005038}
5039
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005040void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5041 HandleShift(shl);
5042}
5043
5044void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5045 HandleShift(shl);
5046}
5047
5048void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5049 HandleShift(shr);
5050}
5051
5052void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5053 HandleShift(shr);
5054}
5055
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005056void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5057 HandleBinaryOp(instruction);
5058}
5059
5060void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5061 HandleBinaryOp(instruction);
5062}
5063
5064void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5065 HandleFieldGet(instruction, instruction->GetFieldInfo());
5066}
5067
5068void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5069 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5070}
5071
5072void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5073 HandleFieldSet(instruction, instruction->GetFieldInfo());
5074}
5075
5076void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5077 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5078}
5079
5080void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5081 HUnresolvedInstanceFieldGet* instruction) {
5082 FieldAccessCallingConventionMIPS calling_convention;
5083 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5084 instruction->GetFieldType(),
5085 calling_convention);
5086}
5087
5088void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5089 HUnresolvedInstanceFieldGet* instruction) {
5090 FieldAccessCallingConventionMIPS calling_convention;
5091 codegen_->GenerateUnresolvedFieldAccess(instruction,
5092 instruction->GetFieldType(),
5093 instruction->GetFieldIndex(),
5094 instruction->GetDexPc(),
5095 calling_convention);
5096}
5097
5098void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5099 HUnresolvedInstanceFieldSet* instruction) {
5100 FieldAccessCallingConventionMIPS calling_convention;
5101 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5102 instruction->GetFieldType(),
5103 calling_convention);
5104}
5105
5106void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5107 HUnresolvedInstanceFieldSet* instruction) {
5108 FieldAccessCallingConventionMIPS calling_convention;
5109 codegen_->GenerateUnresolvedFieldAccess(instruction,
5110 instruction->GetFieldType(),
5111 instruction->GetFieldIndex(),
5112 instruction->GetDexPc(),
5113 calling_convention);
5114}
5115
5116void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5117 HUnresolvedStaticFieldGet* instruction) {
5118 FieldAccessCallingConventionMIPS calling_convention;
5119 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5120 instruction->GetFieldType(),
5121 calling_convention);
5122}
5123
5124void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5125 HUnresolvedStaticFieldGet* instruction) {
5126 FieldAccessCallingConventionMIPS calling_convention;
5127 codegen_->GenerateUnresolvedFieldAccess(instruction,
5128 instruction->GetFieldType(),
5129 instruction->GetFieldIndex(),
5130 instruction->GetDexPc(),
5131 calling_convention);
5132}
5133
5134void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5135 HUnresolvedStaticFieldSet* instruction) {
5136 FieldAccessCallingConventionMIPS calling_convention;
5137 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5138 instruction->GetFieldType(),
5139 calling_convention);
5140}
5141
5142void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5143 HUnresolvedStaticFieldSet* instruction) {
5144 FieldAccessCallingConventionMIPS calling_convention;
5145 codegen_->GenerateUnresolvedFieldAccess(instruction,
5146 instruction->GetFieldType(),
5147 instruction->GetFieldIndex(),
5148 instruction->GetDexPc(),
5149 calling_convention);
5150}
5151
5152void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005153 LocationSummary* locations =
5154 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5155 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005156}
5157
5158void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5159 HBasicBlock* block = instruction->GetBlock();
5160 if (block->GetLoopInformation() != nullptr) {
5161 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5162 // The back edge will generate the suspend check.
5163 return;
5164 }
5165 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5166 // The goto will generate the suspend check.
5167 return;
5168 }
5169 GenerateSuspendCheck(instruction, nullptr);
5170}
5171
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005172void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5173 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005175 InvokeRuntimeCallingConvention calling_convention;
5176 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5177}
5178
5179void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005180 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005181 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5182}
5183
5184void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5185 Primitive::Type input_type = conversion->GetInputType();
5186 Primitive::Type result_type = conversion->GetResultType();
5187 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005188 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005189
5190 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5191 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5192 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5193 }
5194
5195 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005196 if (!isR6 &&
5197 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5198 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005199 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005200 }
5201
5202 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5203
5204 if (call_kind == LocationSummary::kNoCall) {
5205 if (Primitive::IsFloatingPointType(input_type)) {
5206 locations->SetInAt(0, Location::RequiresFpuRegister());
5207 } else {
5208 locations->SetInAt(0, Location::RequiresRegister());
5209 }
5210
5211 if (Primitive::IsFloatingPointType(result_type)) {
5212 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5213 } else {
5214 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5215 }
5216 } else {
5217 InvokeRuntimeCallingConvention calling_convention;
5218
5219 if (Primitive::IsFloatingPointType(input_type)) {
5220 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5221 } else {
5222 DCHECK_EQ(input_type, Primitive::kPrimLong);
5223 locations->SetInAt(0, Location::RegisterPairLocation(
5224 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5225 }
5226
5227 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5228 }
5229}
5230
5231void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5232 LocationSummary* locations = conversion->GetLocations();
5233 Primitive::Type result_type = conversion->GetResultType();
5234 Primitive::Type input_type = conversion->GetInputType();
5235 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005236 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005237
5238 DCHECK_NE(input_type, result_type);
5239
5240 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5241 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5242 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5243 Register src = locations->InAt(0).AsRegister<Register>();
5244
Alexey Frunzea871ef12016-06-27 15:20:11 -07005245 if (dst_low != src) {
5246 __ Move(dst_low, src);
5247 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005248 __ Sra(dst_high, src, 31);
5249 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5250 Register dst = locations->Out().AsRegister<Register>();
5251 Register src = (input_type == Primitive::kPrimLong)
5252 ? locations->InAt(0).AsRegisterPairLow<Register>()
5253 : locations->InAt(0).AsRegister<Register>();
5254
5255 switch (result_type) {
5256 case Primitive::kPrimChar:
5257 __ Andi(dst, src, 0xFFFF);
5258 break;
5259 case Primitive::kPrimByte:
5260 if (has_sign_extension) {
5261 __ Seb(dst, src);
5262 } else {
5263 __ Sll(dst, src, 24);
5264 __ Sra(dst, dst, 24);
5265 }
5266 break;
5267 case Primitive::kPrimShort:
5268 if (has_sign_extension) {
5269 __ Seh(dst, src);
5270 } else {
5271 __ Sll(dst, src, 16);
5272 __ Sra(dst, dst, 16);
5273 }
5274 break;
5275 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005276 if (dst != src) {
5277 __ Move(dst, src);
5278 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005279 break;
5280
5281 default:
5282 LOG(FATAL) << "Unexpected type conversion from " << input_type
5283 << " to " << result_type;
5284 }
5285 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005286 if (input_type == Primitive::kPrimLong) {
5287 if (isR6) {
5288 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5289 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5290 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5291 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5292 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5293 __ Mtc1(src_low, FTMP);
5294 __ Mthc1(src_high, FTMP);
5295 if (result_type == Primitive::kPrimFloat) {
5296 __ Cvtsl(dst, FTMP);
5297 } else {
5298 __ Cvtdl(dst, FTMP);
5299 }
5300 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005301 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
5302 : kQuickL2d;
5303 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005304 if (result_type == Primitive::kPrimFloat) {
5305 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5306 } else {
5307 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5308 }
5309 }
5310 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005311 Register src = locations->InAt(0).AsRegister<Register>();
5312 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5313 __ Mtc1(src, FTMP);
5314 if (result_type == Primitive::kPrimFloat) {
5315 __ Cvtsw(dst, FTMP);
5316 } else {
5317 __ Cvtdw(dst, FTMP);
5318 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005319 }
5320 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5321 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005322 if (result_type == Primitive::kPrimLong) {
5323 if (isR6) {
5324 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5325 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5326 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5327 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5328 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5329 MipsLabel truncate;
5330 MipsLabel done;
5331
5332 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5333 // value when the input is either a NaN or is outside of the range of the output type
5334 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5335 // the same result.
5336 //
5337 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5338 // value of the output type if the input is outside of the range after the truncation or
5339 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5340 // results. This matches the desired float/double-to-int/long conversion exactly.
5341 //
5342 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5343 //
5344 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5345 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5346 // even though it must be NAN2008=1 on R6.
5347 //
5348 // The code takes care of the different behaviors by first comparing the input to the
5349 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5350 // If the input is greater than or equal to the minimum, it procedes to the truncate
5351 // instruction, which will handle such an input the same way irrespective of NAN2008.
5352 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5353 // in order to return either zero or the minimum value.
5354 //
5355 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5356 // truncate instruction for MIPS64R6.
5357 if (input_type == Primitive::kPrimFloat) {
5358 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5359 __ LoadConst32(TMP, min_val);
5360 __ Mtc1(TMP, FTMP);
5361 __ CmpLeS(FTMP, FTMP, src);
5362 } else {
5363 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5364 __ LoadConst32(TMP, High32Bits(min_val));
5365 __ Mtc1(ZERO, FTMP);
5366 __ Mthc1(TMP, FTMP);
5367 __ CmpLeD(FTMP, FTMP, src);
5368 }
5369
5370 __ Bc1nez(FTMP, &truncate);
5371
5372 if (input_type == Primitive::kPrimFloat) {
5373 __ CmpEqS(FTMP, src, src);
5374 } else {
5375 __ CmpEqD(FTMP, src, src);
5376 }
5377 __ Move(dst_low, ZERO);
5378 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5379 __ Mfc1(TMP, FTMP);
5380 __ And(dst_high, dst_high, TMP);
5381
5382 __ B(&done);
5383
5384 __ Bind(&truncate);
5385
5386 if (input_type == Primitive::kPrimFloat) {
5387 __ TruncLS(FTMP, src);
5388 } else {
5389 __ TruncLD(FTMP, src);
5390 }
5391 __ Mfc1(dst_low, FTMP);
5392 __ Mfhc1(dst_high, FTMP);
5393
5394 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005395 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005396 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
5397 : kQuickD2l;
5398 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005399 if (input_type == Primitive::kPrimFloat) {
5400 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5401 } else {
5402 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5403 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005404 }
5405 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005406 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5407 Register dst = locations->Out().AsRegister<Register>();
5408 MipsLabel truncate;
5409 MipsLabel done;
5410
5411 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5412 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5413 // even though it must be NAN2008=1 on R6.
5414 //
5415 // For details see the large comment above for the truncation of float/double to long on R6.
5416 //
5417 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5418 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005419 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005420 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5421 __ LoadConst32(TMP, min_val);
5422 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005423 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005424 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5425 __ LoadConst32(TMP, High32Bits(min_val));
5426 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005427 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005428 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005429
5430 if (isR6) {
5431 if (input_type == Primitive::kPrimFloat) {
5432 __ CmpLeS(FTMP, FTMP, src);
5433 } else {
5434 __ CmpLeD(FTMP, FTMP, src);
5435 }
5436 __ Bc1nez(FTMP, &truncate);
5437
5438 if (input_type == Primitive::kPrimFloat) {
5439 __ CmpEqS(FTMP, src, src);
5440 } else {
5441 __ CmpEqD(FTMP, src, src);
5442 }
5443 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5444 __ Mfc1(TMP, FTMP);
5445 __ And(dst, dst, TMP);
5446 } else {
5447 if (input_type == Primitive::kPrimFloat) {
5448 __ ColeS(0, FTMP, src);
5449 } else {
5450 __ ColeD(0, FTMP, src);
5451 }
5452 __ Bc1t(0, &truncate);
5453
5454 if (input_type == Primitive::kPrimFloat) {
5455 __ CeqS(0, src, src);
5456 } else {
5457 __ CeqD(0, src, src);
5458 }
5459 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5460 __ Movf(dst, ZERO, 0);
5461 }
5462
5463 __ B(&done);
5464
5465 __ Bind(&truncate);
5466
5467 if (input_type == Primitive::kPrimFloat) {
5468 __ TruncWS(FTMP, src);
5469 } else {
5470 __ TruncWD(FTMP, src);
5471 }
5472 __ Mfc1(dst, FTMP);
5473
5474 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005475 }
5476 } else if (Primitive::IsFloatingPointType(result_type) &&
5477 Primitive::IsFloatingPointType(input_type)) {
5478 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5479 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5480 if (result_type == Primitive::kPrimFloat) {
5481 __ Cvtsd(dst, src);
5482 } else {
5483 __ Cvtds(dst, src);
5484 }
5485 } else {
5486 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5487 << " to " << result_type;
5488 }
5489}
5490
5491void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5492 HandleShift(ushr);
5493}
5494
5495void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5496 HandleShift(ushr);
5497}
5498
5499void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5500 HandleBinaryOp(instruction);
5501}
5502
5503void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5504 HandleBinaryOp(instruction);
5505}
5506
5507void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5508 // Nothing to do, this should be removed during prepare for register allocator.
5509 LOG(FATAL) << "Unreachable";
5510}
5511
5512void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5513 // Nothing to do, this should be removed during prepare for register allocator.
5514 LOG(FATAL) << "Unreachable";
5515}
5516
5517void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005518 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005519}
5520
5521void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005522 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005523}
5524
5525void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005526 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005527}
5528
5529void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005530 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005531}
5532
5533void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005534 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005535}
5536
5537void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005538 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005539}
5540
5541void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005542 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005543}
5544
5545void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005546 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005547}
5548
5549void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005550 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005551}
5552
5553void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005554 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005555}
5556
5557void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005558 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005559}
5560
5561void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005562 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005563}
5564
5565void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005566 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005567}
5568
5569void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005570 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005571}
5572
5573void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005574 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005575}
5576
5577void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005578 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005579}
5580
5581void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005582 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005583}
5584
5585void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005586 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005587}
5588
5589void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005590 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005591}
5592
5593void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005594 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005595}
5596
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005597void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5598 LocationSummary* locations =
5599 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5600 locations->SetInAt(0, Location::RequiresRegister());
5601}
5602
5603void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5604 int32_t lower_bound = switch_instr->GetStartValue();
5605 int32_t num_entries = switch_instr->GetNumEntries();
5606 LocationSummary* locations = switch_instr->GetLocations();
5607 Register value_reg = locations->InAt(0).AsRegister<Register>();
5608 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5609
5610 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005611 Register temp_reg = TMP;
5612 __ Addiu32(temp_reg, value_reg, -lower_bound);
5613 // Jump to default if index is negative
5614 // Note: We don't check the case that index is positive while value < lower_bound, because in
5615 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5616 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5617
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005618 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005619 // Jump to successors[0] if value == lower_bound.
5620 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5621 int32_t last_index = 0;
5622 for (; num_entries - last_index > 2; last_index += 2) {
5623 __ Addiu(temp_reg, temp_reg, -2);
5624 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5625 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5626 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5627 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5628 }
5629 if (num_entries - last_index == 2) {
5630 // The last missing case_value.
5631 __ Addiu(temp_reg, temp_reg, -1);
5632 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005633 }
5634
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005635 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005636 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5637 __ B(codegen_->GetLabelOf(default_block));
5638 }
5639}
5640
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005641void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5642 HMipsComputeBaseMethodAddress* insn) {
5643 LocationSummary* locations =
5644 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5645 locations->SetOut(Location::RequiresRegister());
5646}
5647
5648void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5649 HMipsComputeBaseMethodAddress* insn) {
5650 LocationSummary* locations = insn->GetLocations();
5651 Register reg = locations->Out().AsRegister<Register>();
5652
5653 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5654
5655 // Generate a dummy PC-relative call to obtain PC.
5656 __ Nal();
5657 // Grab the return address off RA.
5658 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005659 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005660
5661 // Remember this offset (the obtained PC value) for later use with constant area.
5662 __ BindPcRelBaseLabel();
5663}
5664
5665void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5667 locations->SetOut(Location::RequiresRegister());
5668}
5669
5670void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5671 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5672 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5673 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005674 bool reordering = __ SetReorder(false);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005675 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5676 __ Bind(&info->high_label);
5677 __ Bind(&info->pc_rel_label);
5678 // Add a 32-bit offset to PC.
5679 __ Auipc(reg, /* placeholder */ 0x1234);
5680 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5681 } else {
5682 // Generate a dummy PC-relative call to obtain PC.
5683 __ Nal();
5684 __ Bind(&info->high_label);
5685 __ Lui(reg, /* placeholder */ 0x1234);
5686 __ Bind(&info->pc_rel_label);
5687 __ Ori(reg, reg, /* placeholder */ 0x5678);
5688 // Add a 32-bit offset to PC.
5689 __ Addu(reg, reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005690 // TODO: Can we share this code with that of VisitMipsComputeBaseMethodAddress()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005691 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005692 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005693}
5694
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005695void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5696 // The trampoline uses the same calling convention as dex calling conventions,
5697 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5698 // the method_idx.
5699 HandleInvoke(invoke);
5700}
5701
5702void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5703 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5704}
5705
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005706void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5707 LocationSummary* locations =
5708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5709 locations->SetInAt(0, Location::RequiresRegister());
5710 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005711}
5712
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005713void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5714 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005715 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005716 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005717 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005718 __ LoadFromOffset(kLoadWord,
5719 locations->Out().AsRegister<Register>(),
5720 locations->InAt(0).AsRegister<Register>(),
5721 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005722 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005723 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005724 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005725 __ LoadFromOffset(kLoadWord,
5726 locations->Out().AsRegister<Register>(),
5727 locations->InAt(0).AsRegister<Register>(),
5728 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005729 __ LoadFromOffset(kLoadWord,
5730 locations->Out().AsRegister<Register>(),
5731 locations->Out().AsRegister<Register>(),
5732 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005733 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005734}
5735
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005736#undef __
5737#undef QUICK_ENTRY_POINT
5738
5739} // namespace mips
5740} // namespace art