blob: 018cc457cc040fcfc17514f8b5436fb0ae26f5ed [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());
Serban Constantinescufca16662016-07-14 09:21:59 +0100421 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000422 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 }
424
425 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
426
427 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200428 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
429};
430
431CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
432 const MipsInstructionSetFeatures& isa_features,
433 const CompilerOptions& compiler_options,
434 OptimizingCompilerStats* stats)
435 : CodeGenerator(graph,
436 kNumberOfCoreRegisters,
437 kNumberOfFRegisters,
438 kNumberOfRegisterPairs,
439 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
440 arraysize(kCoreCalleeSaves)),
441 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
442 arraysize(kFpuCalleeSaves)),
443 compiler_options,
444 stats),
445 block_labels_(nullptr),
446 location_builder_(graph, this),
447 instruction_visitor_(graph, this),
448 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100449 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700450 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700451 uint32_literals_(std::less<uint32_t>(),
452 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700453 method_patches_(MethodReferenceComparator(),
454 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
455 call_patches_(MethodReferenceComparator(),
456 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700457 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
458 boot_image_string_patches_(StringReferenceValueComparator(),
459 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
460 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
461 boot_image_type_patches_(TypeReferenceValueComparator(),
462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
463 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
464 boot_image_address_patches_(std::less<uint32_t>(),
465 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
466 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200467 // Save RA (containing the return address) to mimic Quick.
468 AddAllocatedRegister(Location::RegisterLocation(RA));
469}
470
471#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100472// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
473#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700474#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200475
476void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
477 // Ensure that we fix up branches.
478 __ FinalizeCode();
479
480 // Adjust native pc offsets in stack maps.
481 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
482 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
483 uint32_t new_position = __ GetAdjustedPosition(old_position);
484 DCHECK_GE(new_position, old_position);
485 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
486 }
487
488 // Adjust pc offsets for the disassembly information.
489 if (disasm_info_ != nullptr) {
490 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
491 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
492 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
493 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
494 it.second.start = __ GetAdjustedPosition(it.second.start);
495 it.second.end = __ GetAdjustedPosition(it.second.end);
496 }
497 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
498 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
499 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
500 }
501 }
502
503 CodeGenerator::Finalize(allocator);
504}
505
506MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
507 return codegen_->GetAssembler();
508}
509
510void ParallelMoveResolverMIPS::EmitMove(size_t index) {
511 DCHECK_LT(index, moves_.size());
512 MoveOperands* move = moves_[index];
513 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
514}
515
516void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
517 DCHECK_LT(index, moves_.size());
518 MoveOperands* move = moves_[index];
519 Primitive::Type type = move->GetType();
520 Location loc1 = move->GetDestination();
521 Location loc2 = move->GetSource();
522
523 DCHECK(!loc1.IsConstant());
524 DCHECK(!loc2.IsConstant());
525
526 if (loc1.Equals(loc2)) {
527 return;
528 }
529
530 if (loc1.IsRegister() && loc2.IsRegister()) {
531 // Swap 2 GPRs.
532 Register r1 = loc1.AsRegister<Register>();
533 Register r2 = loc2.AsRegister<Register>();
534 __ Move(TMP, r2);
535 __ Move(r2, r1);
536 __ Move(r1, TMP);
537 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
538 FRegister f1 = loc1.AsFpuRegister<FRegister>();
539 FRegister f2 = loc2.AsFpuRegister<FRegister>();
540 if (type == Primitive::kPrimFloat) {
541 __ MovS(FTMP, f2);
542 __ MovS(f2, f1);
543 __ MovS(f1, FTMP);
544 } else {
545 DCHECK_EQ(type, Primitive::kPrimDouble);
546 __ MovD(FTMP, f2);
547 __ MovD(f2, f1);
548 __ MovD(f1, FTMP);
549 }
550 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
551 (loc1.IsFpuRegister() && loc2.IsRegister())) {
552 // Swap FPR and GPR.
553 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
554 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
555 : loc2.AsFpuRegister<FRegister>();
556 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
557 : loc2.AsRegister<Register>();
558 __ Move(TMP, r2);
559 __ Mfc1(r2, f1);
560 __ Mtc1(TMP, f1);
561 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
562 // Swap 2 GPR register pairs.
563 Register r1 = loc1.AsRegisterPairLow<Register>();
564 Register r2 = loc2.AsRegisterPairLow<Register>();
565 __ Move(TMP, r2);
566 __ Move(r2, r1);
567 __ Move(r1, TMP);
568 r1 = loc1.AsRegisterPairHigh<Register>();
569 r2 = loc2.AsRegisterPairHigh<Register>();
570 __ Move(TMP, r2);
571 __ Move(r2, r1);
572 __ Move(r1, TMP);
573 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
574 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
575 // Swap FPR and GPR register pair.
576 DCHECK_EQ(type, Primitive::kPrimDouble);
577 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
578 : loc2.AsFpuRegister<FRegister>();
579 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
580 : loc2.AsRegisterPairLow<Register>();
581 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
582 : loc2.AsRegisterPairHigh<Register>();
583 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
584 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
585 // unpredictable and the following mfch1 will fail.
586 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800587 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200588 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800589 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200590 __ Move(r2_l, TMP);
591 __ Move(r2_h, AT);
592 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
593 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
594 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
595 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000596 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
597 (loc1.IsStackSlot() && loc2.IsRegister())) {
598 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
599 : loc2.AsRegister<Register>();
600 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
601 : loc2.GetStackIndex();
602 __ Move(TMP, reg);
603 __ LoadFromOffset(kLoadWord, reg, SP, offset);
604 __ StoreToOffset(kStoreWord, TMP, SP, offset);
605 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
606 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
607 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
608 : loc2.AsRegisterPairLow<Register>();
609 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
610 : loc2.AsRegisterPairHigh<Register>();
611 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
612 : loc2.GetStackIndex();
613 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
614 : loc2.GetHighStackIndex(kMipsWordSize);
615 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000616 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000617 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000618 __ Move(TMP, reg_h);
619 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
620 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200621 } else {
622 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
623 }
624}
625
626void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
627 __ Pop(static_cast<Register>(reg));
628}
629
630void ParallelMoveResolverMIPS::SpillScratch(int reg) {
631 __ Push(static_cast<Register>(reg));
632}
633
634void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
635 // Allocate a scratch register other than TMP, if available.
636 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
637 // automatically unspilled when the scratch scope object is destroyed).
638 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
639 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
640 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
641 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
642 __ LoadFromOffset(kLoadWord,
643 Register(ensure_scratch.GetRegister()),
644 SP,
645 index1 + stack_offset);
646 __ LoadFromOffset(kLoadWord,
647 TMP,
648 SP,
649 index2 + stack_offset);
650 __ StoreToOffset(kStoreWord,
651 Register(ensure_scratch.GetRegister()),
652 SP,
653 index2 + stack_offset);
654 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
655 }
656}
657
Alexey Frunze73296a72016-06-03 22:51:46 -0700658void CodeGeneratorMIPS::ComputeSpillMask() {
659 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
660 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
661 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
662 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
663 // registers, include the ZERO register to force alignment of FPU callee-saved registers
664 // within the stack frame.
665 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
666 core_spill_mask_ |= (1 << ZERO);
667 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700668}
669
670bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700671 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700672 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
673 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
674 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700675 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
676 // saved in an unused temporary register) and saving of RA and the current method pointer
677 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700678 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
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()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700701 CHECK_EQ(fpu_spill_mask_, 0u);
702 CHECK_EQ(core_spill_mask_, 1u << RA);
703 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200704 return;
705 }
706
707 // Make sure the frame size isn't unreasonably large.
708 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
709 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
710 }
711
712 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200713
Alexey Frunze73296a72016-06-03 22:51:46 -0700714 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200715 __ IncreaseFrameSize(ofs);
716
Alexey Frunze73296a72016-06-03 22:51:46 -0700717 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
718 Register reg = static_cast<Register>(MostSignificantBit(mask));
719 mask ^= 1u << reg;
720 ofs -= kMipsWordSize;
721 // The ZERO register is only included for alignment.
722 if (reg != ZERO) {
723 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200724 __ cfi().RelOffset(DWARFReg(reg), ofs);
725 }
726 }
727
Alexey Frunze73296a72016-06-03 22:51:46 -0700728 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
729 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
730 mask ^= 1u << reg;
731 ofs -= kMipsDoublewordSize;
732 __ StoreDToOffset(reg, SP, ofs);
733 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200734 }
735
Alexey Frunze73296a72016-06-03 22:51:46 -0700736 // Store the current method pointer.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700737 // TODO: can we not do this if RequiresCurrentMethod() returns false?
Alexey Frunze73296a72016-06-03 22:51:46 -0700738 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200739}
740
741void CodeGeneratorMIPS::GenerateFrameExit() {
742 __ cfi().RememberState();
743
744 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200745 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200746
Alexey Frunze73296a72016-06-03 22:51:46 -0700747 // For better instruction scheduling restore RA before other registers.
748 uint32_t ofs = GetFrameSize();
749 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
750 Register reg = static_cast<Register>(MostSignificantBit(mask));
751 mask ^= 1u << reg;
752 ofs -= kMipsWordSize;
753 // The ZERO register is only included for alignment.
754 if (reg != ZERO) {
755 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200756 __ cfi().Restore(DWARFReg(reg));
757 }
758 }
759
Alexey Frunze73296a72016-06-03 22:51:46 -0700760 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
761 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
762 mask ^= 1u << reg;
763 ofs -= kMipsDoublewordSize;
764 __ LoadDFromOffset(reg, SP, ofs);
765 // TODO: __ cfi().Restore(DWARFReg(reg));
766 }
767
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700768 size_t frame_size = GetFrameSize();
769 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
770 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
771 bool reordering = __ SetReorder(false);
772 if (exchange) {
773 __ Jr(RA);
774 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
775 } else {
776 __ DecreaseFrameSize(frame_size);
777 __ Jr(RA);
778 __ Nop(); // In delay slot.
779 }
780 __ SetReorder(reordering);
781 } else {
782 __ Jr(RA);
783 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200784 }
785
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200786 __ cfi().RestoreState();
787 __ cfi().DefCFAOffset(GetFrameSize());
788}
789
790void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
791 __ Bind(GetLabelOf(block));
792}
793
794void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
795 if (src.Equals(dst)) {
796 return;
797 }
798
799 if (src.IsConstant()) {
800 MoveConstant(dst, src.GetConstant());
801 } else {
802 if (Primitive::Is64BitType(dst_type)) {
803 Move64(dst, src);
804 } else {
805 Move32(dst, src);
806 }
807 }
808}
809
810void CodeGeneratorMIPS::Move32(Location destination, Location source) {
811 if (source.Equals(destination)) {
812 return;
813 }
814
815 if (destination.IsRegister()) {
816 if (source.IsRegister()) {
817 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
818 } else if (source.IsFpuRegister()) {
819 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
820 } else {
821 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
822 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
823 }
824 } else if (destination.IsFpuRegister()) {
825 if (source.IsRegister()) {
826 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
827 } else if (source.IsFpuRegister()) {
828 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
829 } else {
830 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
831 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
832 }
833 } else {
834 DCHECK(destination.IsStackSlot()) << destination;
835 if (source.IsRegister()) {
836 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
837 } else if (source.IsFpuRegister()) {
838 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
839 } else {
840 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
841 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
842 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
843 }
844 }
845}
846
847void CodeGeneratorMIPS::Move64(Location destination, Location source) {
848 if (source.Equals(destination)) {
849 return;
850 }
851
852 if (destination.IsRegisterPair()) {
853 if (source.IsRegisterPair()) {
854 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
855 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
856 } else if (source.IsFpuRegister()) {
857 Register dst_high = destination.AsRegisterPairHigh<Register>();
858 Register dst_low = destination.AsRegisterPairLow<Register>();
859 FRegister src = source.AsFpuRegister<FRegister>();
860 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800861 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200862 } else {
863 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
864 int32_t off = source.GetStackIndex();
865 Register r = destination.AsRegisterPairLow<Register>();
866 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
867 }
868 } else if (destination.IsFpuRegister()) {
869 if (source.IsRegisterPair()) {
870 FRegister dst = destination.AsFpuRegister<FRegister>();
871 Register src_high = source.AsRegisterPairHigh<Register>();
872 Register src_low = source.AsRegisterPairLow<Register>();
873 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800874 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200875 } else if (source.IsFpuRegister()) {
876 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
877 } else {
878 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
879 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
880 }
881 } else {
882 DCHECK(destination.IsDoubleStackSlot()) << destination;
883 int32_t off = destination.GetStackIndex();
884 if (source.IsRegisterPair()) {
885 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
886 } else if (source.IsFpuRegister()) {
887 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
888 } else {
889 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
890 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
891 __ StoreToOffset(kStoreWord, TMP, SP, off);
892 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
893 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
894 }
895 }
896}
897
898void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
899 if (c->IsIntConstant() || c->IsNullConstant()) {
900 // Move 32 bit constant.
901 int32_t value = GetInt32ValueOf(c);
902 if (destination.IsRegister()) {
903 Register dst = destination.AsRegister<Register>();
904 __ LoadConst32(dst, value);
905 } else {
906 DCHECK(destination.IsStackSlot())
907 << "Cannot move " << c->DebugName() << " to " << destination;
908 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
909 }
910 } else if (c->IsLongConstant()) {
911 // Move 64 bit constant.
912 int64_t value = GetInt64ValueOf(c);
913 if (destination.IsRegisterPair()) {
914 Register r_h = destination.AsRegisterPairHigh<Register>();
915 Register r_l = destination.AsRegisterPairLow<Register>();
916 __ LoadConst64(r_h, r_l, value);
917 } else {
918 DCHECK(destination.IsDoubleStackSlot())
919 << "Cannot move " << c->DebugName() << " to " << destination;
920 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
921 }
922 } else if (c->IsFloatConstant()) {
923 // Move 32 bit float constant.
924 int32_t value = GetInt32ValueOf(c);
925 if (destination.IsFpuRegister()) {
926 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
927 } else {
928 DCHECK(destination.IsStackSlot())
929 << "Cannot move " << c->DebugName() << " to " << destination;
930 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
931 }
932 } else {
933 // Move 64 bit double constant.
934 DCHECK(c->IsDoubleConstant()) << c->DebugName();
935 int64_t value = GetInt64ValueOf(c);
936 if (destination.IsFpuRegister()) {
937 FRegister fd = destination.AsFpuRegister<FRegister>();
938 __ LoadDConst64(fd, value, TMP);
939 } else {
940 DCHECK(destination.IsDoubleStackSlot())
941 << "Cannot move " << c->DebugName() << " to " << destination;
942 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
943 }
944 }
945}
946
947void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
948 DCHECK(destination.IsRegister());
949 Register dst = destination.AsRegister<Register>();
950 __ LoadConst32(dst, value);
951}
952
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200953void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
954 if (location.IsRegister()) {
955 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700956 } else if (location.IsRegisterPair()) {
957 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
958 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200959 } else {
960 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
961 }
962}
963
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700964void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
965 DCHECK(linker_patches->empty());
966 size_t size =
967 method_patches_.size() +
968 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700969 pc_relative_dex_cache_patches_.size() +
970 pc_relative_string_patches_.size() +
971 pc_relative_type_patches_.size() +
972 boot_image_string_patches_.size() +
973 boot_image_type_patches_.size() +
974 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700975 linker_patches->reserve(size);
976 for (const auto& entry : method_patches_) {
977 const MethodReference& target_method = entry.first;
978 Literal* literal = entry.second;
979 DCHECK(literal->GetLabel()->IsBound());
980 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
981 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
982 target_method.dex_file,
983 target_method.dex_method_index));
984 }
985 for (const auto& entry : call_patches_) {
986 const MethodReference& target_method = entry.first;
987 Literal* literal = entry.second;
988 DCHECK(literal->GetLabel()->IsBound());
989 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
990 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
991 target_method.dex_file,
992 target_method.dex_method_index));
993 }
994 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
995 const DexFile& dex_file = info.target_dex_file;
996 size_t base_element_offset = info.offset_or_index;
997 DCHECK(info.high_label.IsBound());
998 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
999 DCHECK(info.pc_rel_label.IsBound());
1000 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1001 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
1002 &dex_file,
1003 pc_rel_offset,
1004 base_element_offset));
1005 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001006 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
1007 const DexFile& dex_file = info.target_dex_file;
1008 size_t string_index = info.offset_or_index;
1009 DCHECK(info.high_label.IsBound());
1010 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1011 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1012 // the assembler's base label used for PC-relative literals.
1013 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1014 ? __ GetLabelLocation(&info.pc_rel_label)
1015 : __ GetPcRelBaseLabelLocation();
1016 linker_patches->push_back(LinkerPatch::RelativeStringPatch(high_offset,
1017 &dex_file,
1018 pc_rel_offset,
1019 string_index));
1020 }
1021 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
1022 const DexFile& dex_file = info.target_dex_file;
1023 size_t type_index = info.offset_or_index;
1024 DCHECK(info.high_label.IsBound());
1025 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1026 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1027 // the assembler's base label used for PC-relative literals.
1028 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1029 ? __ GetLabelLocation(&info.pc_rel_label)
1030 : __ GetPcRelBaseLabelLocation();
1031 linker_patches->push_back(LinkerPatch::RelativeTypePatch(high_offset,
1032 &dex_file,
1033 pc_rel_offset,
1034 type_index));
1035 }
1036 for (const auto& entry : boot_image_string_patches_) {
1037 const StringReference& target_string = entry.first;
1038 Literal* literal = entry.second;
1039 DCHECK(literal->GetLabel()->IsBound());
1040 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1041 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1042 target_string.dex_file,
1043 target_string.string_index));
1044 }
1045 for (const auto& entry : boot_image_type_patches_) {
1046 const TypeReference& target_type = entry.first;
1047 Literal* literal = entry.second;
1048 DCHECK(literal->GetLabel()->IsBound());
1049 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1050 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1051 target_type.dex_file,
1052 target_type.type_index));
1053 }
1054 for (const auto& entry : boot_image_address_patches_) {
1055 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1056 Literal* literal = entry.second;
1057 DCHECK(literal->GetLabel()->IsBound());
1058 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1059 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1060 }
1061}
1062
1063CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1064 const DexFile& dex_file, uint32_t string_index) {
1065 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1066}
1067
1068CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1069 const DexFile& dex_file, uint32_t type_index) {
1070 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001071}
1072
1073CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1074 const DexFile& dex_file, uint32_t element_offset) {
1075 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1076}
1077
1078CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1079 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1080 patches->emplace_back(dex_file, offset_or_index);
1081 return &patches->back();
1082}
1083
Alexey Frunze06a46c42016-07-19 15:00:40 -07001084Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1085 return map->GetOrCreate(
1086 value,
1087 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1088}
1089
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001090Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1091 MethodToLiteralMap* map) {
1092 return map->GetOrCreate(
1093 target_method,
1094 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1095}
1096
1097Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1098 return DeduplicateMethodLiteral(target_method, &method_patches_);
1099}
1100
1101Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1102 return DeduplicateMethodLiteral(target_method, &call_patches_);
1103}
1104
Alexey Frunze06a46c42016-07-19 15:00:40 -07001105Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1106 uint32_t string_index) {
1107 return boot_image_string_patches_.GetOrCreate(
1108 StringReference(&dex_file, string_index),
1109 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1110}
1111
1112Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1113 uint32_t type_index) {
1114 return boot_image_type_patches_.GetOrCreate(
1115 TypeReference(&dex_file, type_index),
1116 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1117}
1118
1119Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1120 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1121 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1122 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1123}
1124
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001125void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1126 MipsLabel done;
1127 Register card = AT;
1128 Register temp = TMP;
1129 __ Beqz(value, &done);
1130 __ LoadFromOffset(kLoadWord,
1131 card,
1132 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001133 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001134 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1135 __ Addu(temp, card, temp);
1136 __ Sb(card, temp, 0);
1137 __ Bind(&done);
1138}
1139
David Brazdil58282f42016-01-14 12:45:10 +00001140void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001141 // Don't allocate the dalvik style register pair passing.
1142 blocked_register_pairs_[A1_A2] = true;
1143
1144 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1145 blocked_core_registers_[ZERO] = true;
1146 blocked_core_registers_[K0] = true;
1147 blocked_core_registers_[K1] = true;
1148 blocked_core_registers_[GP] = true;
1149 blocked_core_registers_[SP] = true;
1150 blocked_core_registers_[RA] = true;
1151
1152 // AT and TMP(T8) are used as temporary/scratch registers
1153 // (similar to how AT is used by MIPS assemblers).
1154 blocked_core_registers_[AT] = true;
1155 blocked_core_registers_[TMP] = true;
1156 blocked_fpu_registers_[FTMP] = true;
1157
1158 // Reserve suspend and thread registers.
1159 blocked_core_registers_[S0] = true;
1160 blocked_core_registers_[TR] = true;
1161
1162 // Reserve T9 for function calls
1163 blocked_core_registers_[T9] = true;
1164
1165 // Reserve odd-numbered FPU registers.
1166 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1167 blocked_fpu_registers_[i] = true;
1168 }
1169
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001170 if (GetGraph()->IsDebuggable()) {
1171 // Stubs do not save callee-save floating point registers. If the graph
1172 // is debuggable, we need to deal with these registers differently. For
1173 // now, just block them.
1174 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1175 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1176 }
1177 }
1178
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001179 UpdateBlockedPairRegisters();
1180}
1181
1182void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1183 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1184 MipsManagedRegister current =
1185 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1186 if (blocked_core_registers_[current.AsRegisterPairLow()]
1187 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1188 blocked_register_pairs_[i] = true;
1189 }
1190 }
1191}
1192
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001193size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1194 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1195 return kMipsWordSize;
1196}
1197
1198size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1199 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1200 return kMipsWordSize;
1201}
1202
1203size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1204 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1205 return kMipsDoublewordSize;
1206}
1207
1208size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1209 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1210 return kMipsDoublewordSize;
1211}
1212
1213void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001214 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001215}
1216
1217void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001218 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001219}
1220
Serban Constantinescufca16662016-07-14 09:21:59 +01001221constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1222
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001223void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1224 HInstruction* instruction,
1225 uint32_t dex_pc,
1226 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001227 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001228 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001229 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001230 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001231 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001232 // Reserve argument space on stack (for $a0-$a3) for
1233 // entrypoints that directly reference native implementations.
1234 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001235 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001236 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001237 } else {
1238 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001239 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001240 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001241 if (EntrypointRequiresStackMap(entrypoint)) {
1242 RecordPcInfo(instruction, dex_pc, slow_path);
1243 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001244}
1245
1246void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1247 Register class_reg) {
1248 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1249 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1250 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1251 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1252 __ Sync(0);
1253 __ Bind(slow_path->GetExitLabel());
1254}
1255
1256void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1257 __ Sync(0); // Only stype 0 is supported.
1258}
1259
1260void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1261 HBasicBlock* successor) {
1262 SuspendCheckSlowPathMIPS* slow_path =
1263 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1264 codegen_->AddSlowPath(slow_path);
1265
1266 __ LoadFromOffset(kLoadUnsignedHalfword,
1267 TMP,
1268 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001269 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001270 if (successor == nullptr) {
1271 __ Bnez(TMP, slow_path->GetEntryLabel());
1272 __ Bind(slow_path->GetReturnLabel());
1273 } else {
1274 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1275 __ B(slow_path->GetEntryLabel());
1276 // slow_path will return to GetLabelOf(successor).
1277 }
1278}
1279
1280InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1281 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001282 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001283 assembler_(codegen->GetAssembler()),
1284 codegen_(codegen) {}
1285
1286void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1287 DCHECK_EQ(instruction->InputCount(), 2U);
1288 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1289 Primitive::Type type = instruction->GetResultType();
1290 switch (type) {
1291 case Primitive::kPrimInt: {
1292 locations->SetInAt(0, Location::RequiresRegister());
1293 HInstruction* right = instruction->InputAt(1);
1294 bool can_use_imm = false;
1295 if (right->IsConstant()) {
1296 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1297 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1298 can_use_imm = IsUint<16>(imm);
1299 } else if (instruction->IsAdd()) {
1300 can_use_imm = IsInt<16>(imm);
1301 } else {
1302 DCHECK(instruction->IsSub());
1303 can_use_imm = IsInt<16>(-imm);
1304 }
1305 }
1306 if (can_use_imm)
1307 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1308 else
1309 locations->SetInAt(1, Location::RequiresRegister());
1310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1311 break;
1312 }
1313
1314 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001316 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001318 break;
1319 }
1320
1321 case Primitive::kPrimFloat:
1322 case Primitive::kPrimDouble:
1323 DCHECK(instruction->IsAdd() || instruction->IsSub());
1324 locations->SetInAt(0, Location::RequiresFpuRegister());
1325 locations->SetInAt(1, Location::RequiresFpuRegister());
1326 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1327 break;
1328
1329 default:
1330 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1331 }
1332}
1333
1334void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1335 Primitive::Type type = instruction->GetType();
1336 LocationSummary* locations = instruction->GetLocations();
1337
1338 switch (type) {
1339 case Primitive::kPrimInt: {
1340 Register dst = locations->Out().AsRegister<Register>();
1341 Register lhs = locations->InAt(0).AsRegister<Register>();
1342 Location rhs_location = locations->InAt(1);
1343
1344 Register rhs_reg = ZERO;
1345 int32_t rhs_imm = 0;
1346 bool use_imm = rhs_location.IsConstant();
1347 if (use_imm) {
1348 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1349 } else {
1350 rhs_reg = rhs_location.AsRegister<Register>();
1351 }
1352
1353 if (instruction->IsAnd()) {
1354 if (use_imm)
1355 __ Andi(dst, lhs, rhs_imm);
1356 else
1357 __ And(dst, lhs, rhs_reg);
1358 } else if (instruction->IsOr()) {
1359 if (use_imm)
1360 __ Ori(dst, lhs, rhs_imm);
1361 else
1362 __ Or(dst, lhs, rhs_reg);
1363 } else if (instruction->IsXor()) {
1364 if (use_imm)
1365 __ Xori(dst, lhs, rhs_imm);
1366 else
1367 __ Xor(dst, lhs, rhs_reg);
1368 } else if (instruction->IsAdd()) {
1369 if (use_imm)
1370 __ Addiu(dst, lhs, rhs_imm);
1371 else
1372 __ Addu(dst, lhs, rhs_reg);
1373 } else {
1374 DCHECK(instruction->IsSub());
1375 if (use_imm)
1376 __ Addiu(dst, lhs, -rhs_imm);
1377 else
1378 __ Subu(dst, lhs, rhs_reg);
1379 }
1380 break;
1381 }
1382
1383 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001384 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1385 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1386 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1387 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001388 Location rhs_location = locations->InAt(1);
1389 bool use_imm = rhs_location.IsConstant();
1390 if (!use_imm) {
1391 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1392 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1393 if (instruction->IsAnd()) {
1394 __ And(dst_low, lhs_low, rhs_low);
1395 __ And(dst_high, lhs_high, rhs_high);
1396 } else if (instruction->IsOr()) {
1397 __ Or(dst_low, lhs_low, rhs_low);
1398 __ Or(dst_high, lhs_high, rhs_high);
1399 } else if (instruction->IsXor()) {
1400 __ Xor(dst_low, lhs_low, rhs_low);
1401 __ Xor(dst_high, lhs_high, rhs_high);
1402 } else if (instruction->IsAdd()) {
1403 if (lhs_low == rhs_low) {
1404 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1405 __ Slt(TMP, lhs_low, ZERO);
1406 __ Addu(dst_low, lhs_low, rhs_low);
1407 } else {
1408 __ Addu(dst_low, lhs_low, rhs_low);
1409 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1410 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1411 }
1412 __ Addu(dst_high, lhs_high, rhs_high);
1413 __ Addu(dst_high, dst_high, TMP);
1414 } else {
1415 DCHECK(instruction->IsSub());
1416 __ Sltu(TMP, lhs_low, rhs_low);
1417 __ Subu(dst_low, lhs_low, rhs_low);
1418 __ Subu(dst_high, lhs_high, rhs_high);
1419 __ Subu(dst_high, dst_high, TMP);
1420 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001421 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001422 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1423 if (instruction->IsOr()) {
1424 uint32_t low = Low32Bits(value);
1425 uint32_t high = High32Bits(value);
1426 if (IsUint<16>(low)) {
1427 if (dst_low != lhs_low || low != 0) {
1428 __ Ori(dst_low, lhs_low, low);
1429 }
1430 } else {
1431 __ LoadConst32(TMP, low);
1432 __ Or(dst_low, lhs_low, TMP);
1433 }
1434 if (IsUint<16>(high)) {
1435 if (dst_high != lhs_high || high != 0) {
1436 __ Ori(dst_high, lhs_high, high);
1437 }
1438 } else {
1439 if (high != low) {
1440 __ LoadConst32(TMP, high);
1441 }
1442 __ Or(dst_high, lhs_high, TMP);
1443 }
1444 } else if (instruction->IsXor()) {
1445 uint32_t low = Low32Bits(value);
1446 uint32_t high = High32Bits(value);
1447 if (IsUint<16>(low)) {
1448 if (dst_low != lhs_low || low != 0) {
1449 __ Xori(dst_low, lhs_low, low);
1450 }
1451 } else {
1452 __ LoadConst32(TMP, low);
1453 __ Xor(dst_low, lhs_low, TMP);
1454 }
1455 if (IsUint<16>(high)) {
1456 if (dst_high != lhs_high || high != 0) {
1457 __ Xori(dst_high, lhs_high, high);
1458 }
1459 } else {
1460 if (high != low) {
1461 __ LoadConst32(TMP, high);
1462 }
1463 __ Xor(dst_high, lhs_high, TMP);
1464 }
1465 } else if (instruction->IsAnd()) {
1466 uint32_t low = Low32Bits(value);
1467 uint32_t high = High32Bits(value);
1468 if (IsUint<16>(low)) {
1469 __ Andi(dst_low, lhs_low, low);
1470 } else if (low != 0xFFFFFFFF) {
1471 __ LoadConst32(TMP, low);
1472 __ And(dst_low, lhs_low, TMP);
1473 } else if (dst_low != lhs_low) {
1474 __ Move(dst_low, lhs_low);
1475 }
1476 if (IsUint<16>(high)) {
1477 __ Andi(dst_high, lhs_high, high);
1478 } else if (high != 0xFFFFFFFF) {
1479 if (high != low) {
1480 __ LoadConst32(TMP, high);
1481 }
1482 __ And(dst_high, lhs_high, TMP);
1483 } else if (dst_high != lhs_high) {
1484 __ Move(dst_high, lhs_high);
1485 }
1486 } else {
1487 if (instruction->IsSub()) {
1488 value = -value;
1489 } else {
1490 DCHECK(instruction->IsAdd());
1491 }
1492 int32_t low = Low32Bits(value);
1493 int32_t high = High32Bits(value);
1494 if (IsInt<16>(low)) {
1495 if (dst_low != lhs_low || low != 0) {
1496 __ Addiu(dst_low, lhs_low, low);
1497 }
1498 if (low != 0) {
1499 __ Sltiu(AT, dst_low, low);
1500 }
1501 } else {
1502 __ LoadConst32(TMP, low);
1503 __ Addu(dst_low, lhs_low, TMP);
1504 __ Sltu(AT, dst_low, TMP);
1505 }
1506 if (IsInt<16>(high)) {
1507 if (dst_high != lhs_high || high != 0) {
1508 __ Addiu(dst_high, lhs_high, high);
1509 }
1510 } else {
1511 if (high != low) {
1512 __ LoadConst32(TMP, high);
1513 }
1514 __ Addu(dst_high, lhs_high, TMP);
1515 }
1516 if (low != 0) {
1517 __ Addu(dst_high, dst_high, AT);
1518 }
1519 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001520 }
1521 break;
1522 }
1523
1524 case Primitive::kPrimFloat:
1525 case Primitive::kPrimDouble: {
1526 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1527 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1528 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1529 if (instruction->IsAdd()) {
1530 if (type == Primitive::kPrimFloat) {
1531 __ AddS(dst, lhs, rhs);
1532 } else {
1533 __ AddD(dst, lhs, rhs);
1534 }
1535 } else {
1536 DCHECK(instruction->IsSub());
1537 if (type == Primitive::kPrimFloat) {
1538 __ SubS(dst, lhs, rhs);
1539 } else {
1540 __ SubD(dst, lhs, rhs);
1541 }
1542 }
1543 break;
1544 }
1545
1546 default:
1547 LOG(FATAL) << "Unexpected binary operation type " << type;
1548 }
1549}
1550
1551void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001552 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001553
1554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1555 Primitive::Type type = instr->GetResultType();
1556 switch (type) {
1557 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1560 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1561 break;
1562 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001563 locations->SetInAt(0, Location::RequiresRegister());
1564 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1565 locations->SetOut(Location::RequiresRegister());
1566 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001567 default:
1568 LOG(FATAL) << "Unexpected shift type " << type;
1569 }
1570}
1571
1572static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1573
1574void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001575 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 LocationSummary* locations = instr->GetLocations();
1577 Primitive::Type type = instr->GetType();
1578
1579 Location rhs_location = locations->InAt(1);
1580 bool use_imm = rhs_location.IsConstant();
1581 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1582 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001583 const uint32_t shift_mask =
1584 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001585 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001586 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1587 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001588
1589 switch (type) {
1590 case Primitive::kPrimInt: {
1591 Register dst = locations->Out().AsRegister<Register>();
1592 Register lhs = locations->InAt(0).AsRegister<Register>();
1593 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001594 if (shift_value == 0) {
1595 if (dst != lhs) {
1596 __ Move(dst, lhs);
1597 }
1598 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001599 __ Sll(dst, lhs, shift_value);
1600 } else if (instr->IsShr()) {
1601 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001602 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001604 } else {
1605 if (has_ins_rotr) {
1606 __ Rotr(dst, lhs, shift_value);
1607 } else {
1608 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1609 __ Srl(dst, lhs, shift_value);
1610 __ Or(dst, dst, TMP);
1611 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001612 }
1613 } else {
1614 if (instr->IsShl()) {
1615 __ Sllv(dst, lhs, rhs_reg);
1616 } else if (instr->IsShr()) {
1617 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001618 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001619 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001620 } else {
1621 if (has_ins_rotr) {
1622 __ Rotrv(dst, lhs, rhs_reg);
1623 } else {
1624 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001625 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1626 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1627 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1628 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1629 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001630 __ Sllv(TMP, lhs, TMP);
1631 __ Srlv(dst, lhs, rhs_reg);
1632 __ Or(dst, dst, TMP);
1633 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001634 }
1635 }
1636 break;
1637 }
1638
1639 case Primitive::kPrimLong: {
1640 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1641 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1642 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1643 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1644 if (use_imm) {
1645 if (shift_value == 0) {
1646 codegen_->Move64(locations->Out(), locations->InAt(0));
1647 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001648 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001649 if (instr->IsShl()) {
1650 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1651 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1652 __ Sll(dst_low, lhs_low, shift_value);
1653 } else if (instr->IsShr()) {
1654 __ Srl(dst_low, lhs_low, shift_value);
1655 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1656 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001657 } else if (instr->IsUShr()) {
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 Frunze5c7aed32015-11-25 19:41:54 -08001661 } else {
1662 __ Srl(dst_low, lhs_low, shift_value);
1663 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1664 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001665 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001666 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001667 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001668 if (instr->IsShl()) {
1669 __ Sll(dst_low, lhs_low, shift_value);
1670 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1671 __ Sll(dst_high, lhs_high, shift_value);
1672 __ Or(dst_high, dst_high, TMP);
1673 } else if (instr->IsShr()) {
1674 __ Sra(dst_high, lhs_high, shift_value);
1675 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1676 __ Srl(dst_low, lhs_low, shift_value);
1677 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001678 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001679 __ Srl(dst_high, lhs_high, shift_value);
1680 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1681 __ Srl(dst_low, lhs_low, shift_value);
1682 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001683 } else {
1684 __ Srl(TMP, lhs_low, shift_value);
1685 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1686 __ Or(dst_low, dst_low, TMP);
1687 __ Srl(TMP, lhs_high, shift_value);
1688 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1689 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001690 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001691 }
1692 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001693 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001694 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001695 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001696 __ Move(dst_low, ZERO);
1697 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001698 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001699 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001700 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001701 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001702 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001703 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001704 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 // 64-bit rotation by 32 is just a swap.
1706 __ Move(dst_low, lhs_high);
1707 __ Move(dst_high, lhs_low);
1708 } else {
1709 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001710 __ Srl(dst_low, lhs_high, shift_value_high);
1711 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1712 __ Srl(dst_high, lhs_low, shift_value_high);
1713 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001714 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001715 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1716 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001717 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001718 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1719 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001720 __ Or(dst_high, dst_high, TMP);
1721 }
1722 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001723 }
1724 }
1725 } else {
1726 MipsLabel done;
1727 if (instr->IsShl()) {
1728 __ Sllv(dst_low, lhs_low, rhs_reg);
1729 __ Nor(AT, ZERO, rhs_reg);
1730 __ Srl(TMP, lhs_low, 1);
1731 __ Srlv(TMP, TMP, AT);
1732 __ Sllv(dst_high, lhs_high, rhs_reg);
1733 __ Or(dst_high, dst_high, TMP);
1734 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1735 __ Beqz(TMP, &done);
1736 __ Move(dst_high, dst_low);
1737 __ Move(dst_low, ZERO);
1738 } else if (instr->IsShr()) {
1739 __ Srav(dst_high, lhs_high, rhs_reg);
1740 __ Nor(AT, ZERO, rhs_reg);
1741 __ Sll(TMP, lhs_high, 1);
1742 __ Sllv(TMP, TMP, AT);
1743 __ Srlv(dst_low, lhs_low, rhs_reg);
1744 __ Or(dst_low, dst_low, TMP);
1745 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1746 __ Beqz(TMP, &done);
1747 __ Move(dst_low, dst_high);
1748 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001749 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001750 __ Srlv(dst_high, lhs_high, rhs_reg);
1751 __ Nor(AT, ZERO, rhs_reg);
1752 __ Sll(TMP, lhs_high, 1);
1753 __ Sllv(TMP, TMP, AT);
1754 __ Srlv(dst_low, lhs_low, rhs_reg);
1755 __ Or(dst_low, dst_low, TMP);
1756 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1757 __ Beqz(TMP, &done);
1758 __ Move(dst_low, dst_high);
1759 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001760 } else {
1761 __ Nor(AT, ZERO, rhs_reg);
1762 __ Srlv(TMP, lhs_low, rhs_reg);
1763 __ Sll(dst_low, lhs_high, 1);
1764 __ Sllv(dst_low, dst_low, AT);
1765 __ Or(dst_low, dst_low, TMP);
1766 __ Srlv(TMP, lhs_high, rhs_reg);
1767 __ Sll(dst_high, lhs_low, 1);
1768 __ Sllv(dst_high, dst_high, AT);
1769 __ Or(dst_high, dst_high, TMP);
1770 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1771 __ Beqz(TMP, &done);
1772 __ Move(TMP, dst_high);
1773 __ Move(dst_high, dst_low);
1774 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001775 }
1776 __ Bind(&done);
1777 }
1778 break;
1779 }
1780
1781 default:
1782 LOG(FATAL) << "Unexpected shift operation type " << type;
1783 }
1784}
1785
1786void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1787 HandleBinaryOp(instruction);
1788}
1789
1790void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1791 HandleBinaryOp(instruction);
1792}
1793
1794void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1795 HandleBinaryOp(instruction);
1796}
1797
1798void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1799 HandleBinaryOp(instruction);
1800}
1801
1802void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1803 LocationSummary* locations =
1804 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1805 locations->SetInAt(0, Location::RequiresRegister());
1806 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1807 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1808 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1809 } else {
1810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1811 }
1812}
1813
Alexey Frunze2923db72016-08-20 01:55:47 -07001814auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1815 auto null_checker = [this, instruction]() {
1816 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1817 };
1818 return null_checker;
1819}
1820
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001821void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1822 LocationSummary* locations = instruction->GetLocations();
1823 Register obj = locations->InAt(0).AsRegister<Register>();
1824 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001825 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001826 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001827
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001828 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001829 switch (type) {
1830 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001831 Register out = locations->Out().AsRegister<Register>();
1832 if (index.IsConstant()) {
1833 size_t offset =
1834 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001835 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001836 } else {
1837 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001838 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001839 }
1840 break;
1841 }
1842
1843 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001844 Register out = locations->Out().AsRegister<Register>();
1845 if (index.IsConstant()) {
1846 size_t offset =
1847 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001848 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001849 } else {
1850 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001851 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852 }
1853 break;
1854 }
1855
1856 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 Register out = locations->Out().AsRegister<Register>();
1858 if (index.IsConstant()) {
1859 size_t offset =
1860 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001861 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001862 } else {
1863 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1864 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001865 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 Register out = locations->Out().AsRegister<Register>();
1872 if (index.IsConstant()) {
1873 size_t offset =
1874 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001875 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 } else {
1877 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1878 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001879 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880 }
1881 break;
1882 }
1883
1884 case Primitive::kPrimInt:
1885 case Primitive::kPrimNot: {
1886 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887 Register out = locations->Out().AsRegister<Register>();
1888 if (index.IsConstant()) {
1889 size_t offset =
1890 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001891 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001892 } else {
1893 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1894 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001895 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001896 }
1897 break;
1898 }
1899
1900 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901 Register out = locations->Out().AsRegisterPairLow<Register>();
1902 if (index.IsConstant()) {
1903 size_t offset =
1904 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001905 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906 } else {
1907 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1908 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001909 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910 }
1911 break;
1912 }
1913
1914 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1916 if (index.IsConstant()) {
1917 size_t offset =
1918 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001919 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001920 } else {
1921 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1922 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001923 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001924 }
1925 break;
1926 }
1927
1928 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1930 if (index.IsConstant()) {
1931 size_t offset =
1932 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001933 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 } else {
1935 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1936 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001937 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001938 }
1939 break;
1940 }
1941
1942 case Primitive::kPrimVoid:
1943 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1944 UNREACHABLE();
1945 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946}
1947
1948void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1949 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1950 locations->SetInAt(0, Location::RequiresRegister());
1951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1952}
1953
1954void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1955 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001956 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001957 Register obj = locations->InAt(0).AsRegister<Register>();
1958 Register out = locations->Out().AsRegister<Register>();
1959 __ LoadFromOffset(kLoadWord, out, obj, offset);
1960 codegen_->MaybeRecordImplicitNullCheck(instruction);
1961}
1962
1963void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001964 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001965 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1966 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001967 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001968 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001969 InvokeRuntimeCallingConvention calling_convention;
1970 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1971 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1972 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1973 } else {
1974 locations->SetInAt(0, Location::RequiresRegister());
1975 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1976 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1977 locations->SetInAt(2, Location::RequiresFpuRegister());
1978 } else {
1979 locations->SetInAt(2, Location::RequiresRegister());
1980 }
1981 }
1982}
1983
1984void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1985 LocationSummary* locations = instruction->GetLocations();
1986 Register obj = locations->InAt(0).AsRegister<Register>();
1987 Location index = locations->InAt(1);
1988 Primitive::Type value_type = instruction->GetComponentType();
1989 bool needs_runtime_call = locations->WillCall();
1990 bool needs_write_barrier =
1991 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07001992 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001993
1994 switch (value_type) {
1995 case Primitive::kPrimBoolean:
1996 case Primitive::kPrimByte: {
1997 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1998 Register value = locations->InAt(2).AsRegister<Register>();
1999 if (index.IsConstant()) {
2000 size_t offset =
2001 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002002 __ StoreToOffset(kStoreByte, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002003 } else {
2004 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002005 __ StoreToOffset(kStoreByte, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002006 }
2007 break;
2008 }
2009
2010 case Primitive::kPrimShort:
2011 case Primitive::kPrimChar: {
2012 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
2013 Register value = locations->InAt(2).AsRegister<Register>();
2014 if (index.IsConstant()) {
2015 size_t offset =
2016 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002017 __ StoreToOffset(kStoreHalfword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002018 } else {
2019 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
2020 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002021 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002022 }
2023 break;
2024 }
2025
2026 case Primitive::kPrimInt:
2027 case Primitive::kPrimNot: {
2028 if (!needs_runtime_call) {
2029 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2030 Register value = locations->InAt(2).AsRegister<Register>();
2031 if (index.IsConstant()) {
2032 size_t offset =
2033 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002034 __ StoreToOffset(kStoreWord, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 } else {
2036 DCHECK(index.IsRegister()) << index;
2037 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2038 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002039 __ StoreToOffset(kStoreWord, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002040 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041 if (needs_write_barrier) {
2042 DCHECK_EQ(value_type, Primitive::kPrimNot);
2043 codegen_->MarkGCCard(obj, value);
2044 }
2045 } else {
2046 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002047 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002048 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2049 }
2050 break;
2051 }
2052
2053 case Primitive::kPrimLong: {
2054 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2055 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2056 if (index.IsConstant()) {
2057 size_t offset =
2058 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002059 __ StoreToOffset(kStoreDoubleword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002060 } else {
2061 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2062 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002063 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 }
2065 break;
2066 }
2067
2068 case Primitive::kPrimFloat: {
2069 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2070 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2071 DCHECK(locations->InAt(2).IsFpuRegister());
2072 if (index.IsConstant()) {
2073 size_t offset =
2074 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002075 __ StoreSToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 } else {
2077 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2078 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002079 __ StoreSToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002080 }
2081 break;
2082 }
2083
2084 case Primitive::kPrimDouble: {
2085 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2086 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2087 DCHECK(locations->InAt(2).IsFpuRegister());
2088 if (index.IsConstant()) {
2089 size_t offset =
2090 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002091 __ StoreDToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002092 } else {
2093 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2094 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002095 __ StoreDToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002096 }
2097 break;
2098 }
2099
2100 case Primitive::kPrimVoid:
2101 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2102 UNREACHABLE();
2103 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002104}
2105
2106void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2107 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2108 ? LocationSummary::kCallOnSlowPath
2109 : LocationSummary::kNoCall;
2110 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2111 locations->SetInAt(0, Location::RequiresRegister());
2112 locations->SetInAt(1, Location::RequiresRegister());
2113 if (instruction->HasUses()) {
2114 locations->SetOut(Location::SameAsFirstInput());
2115 }
2116}
2117
2118void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2119 LocationSummary* locations = instruction->GetLocations();
2120 BoundsCheckSlowPathMIPS* slow_path =
2121 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2122 codegen_->AddSlowPath(slow_path);
2123
2124 Register index = locations->InAt(0).AsRegister<Register>();
2125 Register length = locations->InAt(1).AsRegister<Register>();
2126
2127 // length is limited by the maximum positive signed 32-bit integer.
2128 // Unsigned comparison of length and index checks for index < 0
2129 // and for length <= index simultaneously.
2130 __ Bgeu(index, length, slow_path->GetEntryLabel());
2131}
2132
2133void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2135 instruction,
2136 LocationSummary::kCallOnSlowPath);
2137 locations->SetInAt(0, Location::RequiresRegister());
2138 locations->SetInAt(1, Location::RequiresRegister());
2139 // Note that TypeCheckSlowPathMIPS uses this register too.
2140 locations->AddTemp(Location::RequiresRegister());
2141}
2142
2143void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2144 LocationSummary* locations = instruction->GetLocations();
2145 Register obj = locations->InAt(0).AsRegister<Register>();
2146 Register cls = locations->InAt(1).AsRegister<Register>();
2147 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2148
2149 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2150 codegen_->AddSlowPath(slow_path);
2151
2152 // TODO: avoid this check if we know obj is not null.
2153 __ Beqz(obj, slow_path->GetExitLabel());
2154 // Compare the class of `obj` with `cls`.
2155 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2156 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2157 __ Bind(slow_path->GetExitLabel());
2158}
2159
2160void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2161 LocationSummary* locations =
2162 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2163 locations->SetInAt(0, Location::RequiresRegister());
2164 if (check->HasUses()) {
2165 locations->SetOut(Location::SameAsFirstInput());
2166 }
2167}
2168
2169void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2170 // We assume the class is not null.
2171 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2172 check->GetLoadClass(),
2173 check,
2174 check->GetDexPc(),
2175 true);
2176 codegen_->AddSlowPath(slow_path);
2177 GenerateClassInitializationCheck(slow_path,
2178 check->GetLocations()->InAt(0).AsRegister<Register>());
2179}
2180
2181void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2182 Primitive::Type in_type = compare->InputAt(0)->GetType();
2183
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002184 LocationSummary* locations =
2185 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002186
2187 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002188 case Primitive::kPrimBoolean:
2189 case Primitive::kPrimByte:
2190 case Primitive::kPrimShort:
2191 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002192 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002193 case Primitive::kPrimLong:
2194 locations->SetInAt(0, Location::RequiresRegister());
2195 locations->SetInAt(1, Location::RequiresRegister());
2196 // Output overlaps because it is written before doing the low comparison.
2197 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2198 break;
2199
2200 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002201 case Primitive::kPrimDouble:
2202 locations->SetInAt(0, Location::RequiresFpuRegister());
2203 locations->SetInAt(1, Location::RequiresFpuRegister());
2204 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002205 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002206
2207 default:
2208 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2209 }
2210}
2211
2212void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2213 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002214 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002215 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002216 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002217
2218 // 0 if: left == right
2219 // 1 if: left > right
2220 // -1 if: left < right
2221 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002222 case Primitive::kPrimBoolean:
2223 case Primitive::kPrimByte:
2224 case Primitive::kPrimShort:
2225 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002226 case Primitive::kPrimInt: {
2227 Register lhs = locations->InAt(0).AsRegister<Register>();
2228 Register rhs = locations->InAt(1).AsRegister<Register>();
2229 __ Slt(TMP, lhs, rhs);
2230 __ Slt(res, rhs, lhs);
2231 __ Subu(res, res, TMP);
2232 break;
2233 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002234 case Primitive::kPrimLong: {
2235 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002236 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2237 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2238 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2239 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2240 // TODO: more efficient (direct) comparison with a constant.
2241 __ Slt(TMP, lhs_high, rhs_high);
2242 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2243 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2244 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2245 __ Sltu(TMP, lhs_low, rhs_low);
2246 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2247 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2248 __ Bind(&done);
2249 break;
2250 }
2251
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002252 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002253 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002254 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2255 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2256 MipsLabel done;
2257 if (isR6) {
2258 __ CmpEqS(FTMP, lhs, rhs);
2259 __ LoadConst32(res, 0);
2260 __ Bc1nez(FTMP, &done);
2261 if (gt_bias) {
2262 __ CmpLtS(FTMP, lhs, rhs);
2263 __ LoadConst32(res, -1);
2264 __ Bc1nez(FTMP, &done);
2265 __ LoadConst32(res, 1);
2266 } else {
2267 __ CmpLtS(FTMP, rhs, lhs);
2268 __ LoadConst32(res, 1);
2269 __ Bc1nez(FTMP, &done);
2270 __ LoadConst32(res, -1);
2271 }
2272 } else {
2273 if (gt_bias) {
2274 __ ColtS(0, lhs, rhs);
2275 __ LoadConst32(res, -1);
2276 __ Bc1t(0, &done);
2277 __ CeqS(0, lhs, rhs);
2278 __ LoadConst32(res, 1);
2279 __ Movt(res, ZERO, 0);
2280 } else {
2281 __ ColtS(0, rhs, lhs);
2282 __ LoadConst32(res, 1);
2283 __ Bc1t(0, &done);
2284 __ CeqS(0, lhs, rhs);
2285 __ LoadConst32(res, -1);
2286 __ Movt(res, ZERO, 0);
2287 }
2288 }
2289 __ Bind(&done);
2290 break;
2291 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002292 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002293 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002294 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2295 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2296 MipsLabel done;
2297 if (isR6) {
2298 __ CmpEqD(FTMP, lhs, rhs);
2299 __ LoadConst32(res, 0);
2300 __ Bc1nez(FTMP, &done);
2301 if (gt_bias) {
2302 __ CmpLtD(FTMP, lhs, rhs);
2303 __ LoadConst32(res, -1);
2304 __ Bc1nez(FTMP, &done);
2305 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002306 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002307 __ CmpLtD(FTMP, rhs, lhs);
2308 __ LoadConst32(res, 1);
2309 __ Bc1nez(FTMP, &done);
2310 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002311 }
2312 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002313 if (gt_bias) {
2314 __ ColtD(0, lhs, rhs);
2315 __ LoadConst32(res, -1);
2316 __ Bc1t(0, &done);
2317 __ CeqD(0, lhs, rhs);
2318 __ LoadConst32(res, 1);
2319 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002320 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002321 __ ColtD(0, rhs, lhs);
2322 __ LoadConst32(res, 1);
2323 __ Bc1t(0, &done);
2324 __ CeqD(0, lhs, rhs);
2325 __ LoadConst32(res, -1);
2326 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327 }
2328 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002329 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002330 break;
2331 }
2332
2333 default:
2334 LOG(FATAL) << "Unimplemented compare type " << in_type;
2335 }
2336}
2337
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002338void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002339 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002340 switch (instruction->InputAt(0)->GetType()) {
2341 default:
2342 case Primitive::kPrimLong:
2343 locations->SetInAt(0, Location::RequiresRegister());
2344 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2345 break;
2346
2347 case Primitive::kPrimFloat:
2348 case Primitive::kPrimDouble:
2349 locations->SetInAt(0, Location::RequiresFpuRegister());
2350 locations->SetInAt(1, Location::RequiresFpuRegister());
2351 break;
2352 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002353 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2355 }
2356}
2357
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002358void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002359 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002360 return;
2361 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002362
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002363 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002364 LocationSummary* locations = instruction->GetLocations();
2365 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002366 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002367
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002368 switch (type) {
2369 default:
2370 // Integer case.
2371 GenerateIntCompare(instruction->GetCondition(), locations);
2372 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002373
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002374 case Primitive::kPrimLong:
2375 // TODO: don't use branches.
2376 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002377 break;
2378
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002379 case Primitive::kPrimFloat:
2380 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002381 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2382 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002383 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002384
2385 // Convert the branches into the result.
2386 MipsLabel done;
2387
2388 // False case: result = 0.
2389 __ LoadConst32(dst, 0);
2390 __ B(&done);
2391
2392 // True case: result = 1.
2393 __ Bind(&true_label);
2394 __ LoadConst32(dst, 1);
2395 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002396}
2397
Alexey Frunze7e99e052015-11-24 19:28:01 -08002398void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2399 DCHECK(instruction->IsDiv() || instruction->IsRem());
2400 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2401
2402 LocationSummary* locations = instruction->GetLocations();
2403 Location second = locations->InAt(1);
2404 DCHECK(second.IsConstant());
2405
2406 Register out = locations->Out().AsRegister<Register>();
2407 Register dividend = locations->InAt(0).AsRegister<Register>();
2408 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2409 DCHECK(imm == 1 || imm == -1);
2410
2411 if (instruction->IsRem()) {
2412 __ Move(out, ZERO);
2413 } else {
2414 if (imm == -1) {
2415 __ Subu(out, ZERO, dividend);
2416 } else if (out != dividend) {
2417 __ Move(out, dividend);
2418 }
2419 }
2420}
2421
2422void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2423 DCHECK(instruction->IsDiv() || instruction->IsRem());
2424 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2425
2426 LocationSummary* locations = instruction->GetLocations();
2427 Location second = locations->InAt(1);
2428 DCHECK(second.IsConstant());
2429
2430 Register out = locations->Out().AsRegister<Register>();
2431 Register dividend = locations->InAt(0).AsRegister<Register>();
2432 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002433 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002434 int ctz_imm = CTZ(abs_imm);
2435
2436 if (instruction->IsDiv()) {
2437 if (ctz_imm == 1) {
2438 // Fast path for division by +/-2, which is very common.
2439 __ Srl(TMP, dividend, 31);
2440 } else {
2441 __ Sra(TMP, dividend, 31);
2442 __ Srl(TMP, TMP, 32 - ctz_imm);
2443 }
2444 __ Addu(out, dividend, TMP);
2445 __ Sra(out, out, ctz_imm);
2446 if (imm < 0) {
2447 __ Subu(out, ZERO, out);
2448 }
2449 } else {
2450 if (ctz_imm == 1) {
2451 // Fast path for modulo +/-2, which is very common.
2452 __ Sra(TMP, dividend, 31);
2453 __ Subu(out, dividend, TMP);
2454 __ Andi(out, out, 1);
2455 __ Addu(out, out, TMP);
2456 } else {
2457 __ Sra(TMP, dividend, 31);
2458 __ Srl(TMP, TMP, 32 - ctz_imm);
2459 __ Addu(out, dividend, TMP);
2460 if (IsUint<16>(abs_imm - 1)) {
2461 __ Andi(out, out, abs_imm - 1);
2462 } else {
2463 __ Sll(out, out, 32 - ctz_imm);
2464 __ Srl(out, out, 32 - ctz_imm);
2465 }
2466 __ Subu(out, out, TMP);
2467 }
2468 }
2469}
2470
2471void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2472 DCHECK(instruction->IsDiv() || instruction->IsRem());
2473 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2474
2475 LocationSummary* locations = instruction->GetLocations();
2476 Location second = locations->InAt(1);
2477 DCHECK(second.IsConstant());
2478
2479 Register out = locations->Out().AsRegister<Register>();
2480 Register dividend = locations->InAt(0).AsRegister<Register>();
2481 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2482
2483 int64_t magic;
2484 int shift;
2485 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2486
2487 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2488
2489 __ LoadConst32(TMP, magic);
2490 if (isR6) {
2491 __ MuhR6(TMP, dividend, TMP);
2492 } else {
2493 __ MultR2(dividend, TMP);
2494 __ Mfhi(TMP);
2495 }
2496 if (imm > 0 && magic < 0) {
2497 __ Addu(TMP, TMP, dividend);
2498 } else if (imm < 0 && magic > 0) {
2499 __ Subu(TMP, TMP, dividend);
2500 }
2501
2502 if (shift != 0) {
2503 __ Sra(TMP, TMP, shift);
2504 }
2505
2506 if (instruction->IsDiv()) {
2507 __ Sra(out, TMP, 31);
2508 __ Subu(out, TMP, out);
2509 } else {
2510 __ Sra(AT, TMP, 31);
2511 __ Subu(AT, TMP, AT);
2512 __ LoadConst32(TMP, imm);
2513 if (isR6) {
2514 __ MulR6(TMP, AT, TMP);
2515 } else {
2516 __ MulR2(TMP, AT, TMP);
2517 }
2518 __ Subu(out, dividend, TMP);
2519 }
2520}
2521
2522void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2523 DCHECK(instruction->IsDiv() || instruction->IsRem());
2524 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2525
2526 LocationSummary* locations = instruction->GetLocations();
2527 Register out = locations->Out().AsRegister<Register>();
2528 Location second = locations->InAt(1);
2529
2530 if (second.IsConstant()) {
2531 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2532 if (imm == 0) {
2533 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2534 } else if (imm == 1 || imm == -1) {
2535 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002536 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002537 DivRemByPowerOfTwo(instruction);
2538 } else {
2539 DCHECK(imm <= -2 || imm >= 2);
2540 GenerateDivRemWithAnyConstant(instruction);
2541 }
2542 } else {
2543 Register dividend = locations->InAt(0).AsRegister<Register>();
2544 Register divisor = second.AsRegister<Register>();
2545 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2546 if (instruction->IsDiv()) {
2547 if (isR6) {
2548 __ DivR6(out, dividend, divisor);
2549 } else {
2550 __ DivR2(out, dividend, divisor);
2551 }
2552 } else {
2553 if (isR6) {
2554 __ ModR6(out, dividend, divisor);
2555 } else {
2556 __ ModR2(out, dividend, divisor);
2557 }
2558 }
2559 }
2560}
2561
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002562void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2563 Primitive::Type type = div->GetResultType();
2564 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002565 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002566 : LocationSummary::kNoCall;
2567
2568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2569
2570 switch (type) {
2571 case Primitive::kPrimInt:
2572 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002573 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002574 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2575 break;
2576
2577 case Primitive::kPrimLong: {
2578 InvokeRuntimeCallingConvention calling_convention;
2579 locations->SetInAt(0, Location::RegisterPairLocation(
2580 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2581 locations->SetInAt(1, Location::RegisterPairLocation(
2582 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2583 locations->SetOut(calling_convention.GetReturnLocation(type));
2584 break;
2585 }
2586
2587 case Primitive::kPrimFloat:
2588 case Primitive::kPrimDouble:
2589 locations->SetInAt(0, Location::RequiresFpuRegister());
2590 locations->SetInAt(1, Location::RequiresFpuRegister());
2591 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2592 break;
2593
2594 default:
2595 LOG(FATAL) << "Unexpected div type " << type;
2596 }
2597}
2598
2599void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2600 Primitive::Type type = instruction->GetType();
2601 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002602
2603 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002604 case Primitive::kPrimInt:
2605 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002606 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002608 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002609 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2610 break;
2611 }
2612 case Primitive::kPrimFloat:
2613 case Primitive::kPrimDouble: {
2614 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2615 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2616 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2617 if (type == Primitive::kPrimFloat) {
2618 __ DivS(dst, lhs, rhs);
2619 } else {
2620 __ DivD(dst, lhs, rhs);
2621 }
2622 break;
2623 }
2624 default:
2625 LOG(FATAL) << "Unexpected div type " << type;
2626 }
2627}
2628
2629void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2630 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2631 ? LocationSummary::kCallOnSlowPath
2632 : LocationSummary::kNoCall;
2633 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2634 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2635 if (instruction->HasUses()) {
2636 locations->SetOut(Location::SameAsFirstInput());
2637 }
2638}
2639
2640void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2641 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2642 codegen_->AddSlowPath(slow_path);
2643 Location value = instruction->GetLocations()->InAt(0);
2644 Primitive::Type type = instruction->GetType();
2645
2646 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002647 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002648 case Primitive::kPrimByte:
2649 case Primitive::kPrimChar:
2650 case Primitive::kPrimShort:
2651 case Primitive::kPrimInt: {
2652 if (value.IsConstant()) {
2653 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2654 __ B(slow_path->GetEntryLabel());
2655 } else {
2656 // A division by a non-null constant is valid. We don't need to perform
2657 // any check, so simply fall through.
2658 }
2659 } else {
2660 DCHECK(value.IsRegister()) << value;
2661 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2662 }
2663 break;
2664 }
2665 case Primitive::kPrimLong: {
2666 if (value.IsConstant()) {
2667 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2668 __ B(slow_path->GetEntryLabel());
2669 } else {
2670 // A division by a non-null constant is valid. We don't need to perform
2671 // any check, so simply fall through.
2672 }
2673 } else {
2674 DCHECK(value.IsRegisterPair()) << value;
2675 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2676 __ Beqz(TMP, slow_path->GetEntryLabel());
2677 }
2678 break;
2679 }
2680 default:
2681 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2682 }
2683}
2684
2685void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2686 LocationSummary* locations =
2687 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2688 locations->SetOut(Location::ConstantLocation(constant));
2689}
2690
2691void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2692 // Will be generated at use site.
2693}
2694
2695void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2696 exit->SetLocations(nullptr);
2697}
2698
2699void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2700}
2701
2702void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2703 LocationSummary* locations =
2704 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2705 locations->SetOut(Location::ConstantLocation(constant));
2706}
2707
2708void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2709 // Will be generated at use site.
2710}
2711
2712void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2713 got->SetLocations(nullptr);
2714}
2715
2716void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2717 DCHECK(!successor->IsExitBlock());
2718 HBasicBlock* block = got->GetBlock();
2719 HInstruction* previous = got->GetPrevious();
2720 HLoopInformation* info = block->GetLoopInformation();
2721
2722 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2723 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2724 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2725 return;
2726 }
2727 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2728 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2729 }
2730 if (!codegen_->GoesToNextBlock(block, successor)) {
2731 __ B(codegen_->GetLabelOf(successor));
2732 }
2733}
2734
2735void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2736 HandleGoto(got, got->GetSuccessor());
2737}
2738
2739void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2740 try_boundary->SetLocations(nullptr);
2741}
2742
2743void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2744 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2745 if (!successor->IsExitBlock()) {
2746 HandleGoto(try_boundary, successor);
2747 }
2748}
2749
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002750void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2751 LocationSummary* locations) {
2752 Register dst = locations->Out().AsRegister<Register>();
2753 Register lhs = locations->InAt(0).AsRegister<Register>();
2754 Location rhs_location = locations->InAt(1);
2755 Register rhs_reg = ZERO;
2756 int64_t rhs_imm = 0;
2757 bool use_imm = rhs_location.IsConstant();
2758 if (use_imm) {
2759 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2760 } else {
2761 rhs_reg = rhs_location.AsRegister<Register>();
2762 }
2763
2764 switch (cond) {
2765 case kCondEQ:
2766 case kCondNE:
2767 if (use_imm && IsUint<16>(rhs_imm)) {
2768 __ Xori(dst, lhs, rhs_imm);
2769 } else {
2770 if (use_imm) {
2771 rhs_reg = TMP;
2772 __ LoadConst32(rhs_reg, rhs_imm);
2773 }
2774 __ Xor(dst, lhs, rhs_reg);
2775 }
2776 if (cond == kCondEQ) {
2777 __ Sltiu(dst, dst, 1);
2778 } else {
2779 __ Sltu(dst, ZERO, dst);
2780 }
2781 break;
2782
2783 case kCondLT:
2784 case kCondGE:
2785 if (use_imm && IsInt<16>(rhs_imm)) {
2786 __ Slti(dst, lhs, rhs_imm);
2787 } else {
2788 if (use_imm) {
2789 rhs_reg = TMP;
2790 __ LoadConst32(rhs_reg, rhs_imm);
2791 }
2792 __ Slt(dst, lhs, rhs_reg);
2793 }
2794 if (cond == kCondGE) {
2795 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2796 // only the slt instruction but no sge.
2797 __ Xori(dst, dst, 1);
2798 }
2799 break;
2800
2801 case kCondLE:
2802 case kCondGT:
2803 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2804 // Simulate lhs <= rhs via lhs < rhs + 1.
2805 __ Slti(dst, lhs, rhs_imm + 1);
2806 if (cond == kCondGT) {
2807 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2808 // only the slti instruction but no sgti.
2809 __ Xori(dst, dst, 1);
2810 }
2811 } else {
2812 if (use_imm) {
2813 rhs_reg = TMP;
2814 __ LoadConst32(rhs_reg, rhs_imm);
2815 }
2816 __ Slt(dst, rhs_reg, lhs);
2817 if (cond == kCondLE) {
2818 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2819 // only the slt instruction but no sle.
2820 __ Xori(dst, dst, 1);
2821 }
2822 }
2823 break;
2824
2825 case kCondB:
2826 case kCondAE:
2827 if (use_imm && IsInt<16>(rhs_imm)) {
2828 // Sltiu sign-extends its 16-bit immediate operand before
2829 // the comparison and thus lets us compare directly with
2830 // unsigned values in the ranges [0, 0x7fff] and
2831 // [0xffff8000, 0xffffffff].
2832 __ Sltiu(dst, lhs, rhs_imm);
2833 } else {
2834 if (use_imm) {
2835 rhs_reg = TMP;
2836 __ LoadConst32(rhs_reg, rhs_imm);
2837 }
2838 __ Sltu(dst, lhs, rhs_reg);
2839 }
2840 if (cond == kCondAE) {
2841 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2842 // only the sltu instruction but no sgeu.
2843 __ Xori(dst, dst, 1);
2844 }
2845 break;
2846
2847 case kCondBE:
2848 case kCondA:
2849 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2850 // Simulate lhs <= rhs via lhs < rhs + 1.
2851 // Note that this only works if rhs + 1 does not overflow
2852 // to 0, hence the check above.
2853 // Sltiu sign-extends its 16-bit immediate operand before
2854 // the comparison and thus lets us compare directly with
2855 // unsigned values in the ranges [0, 0x7fff] and
2856 // [0xffff8000, 0xffffffff].
2857 __ Sltiu(dst, lhs, rhs_imm + 1);
2858 if (cond == kCondA) {
2859 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2860 // only the sltiu instruction but no sgtiu.
2861 __ Xori(dst, dst, 1);
2862 }
2863 } else {
2864 if (use_imm) {
2865 rhs_reg = TMP;
2866 __ LoadConst32(rhs_reg, rhs_imm);
2867 }
2868 __ Sltu(dst, rhs_reg, lhs);
2869 if (cond == kCondBE) {
2870 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2871 // only the sltu instruction but no sleu.
2872 __ Xori(dst, dst, 1);
2873 }
2874 }
2875 break;
2876 }
2877}
2878
2879void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2880 LocationSummary* locations,
2881 MipsLabel* label) {
2882 Register lhs = locations->InAt(0).AsRegister<Register>();
2883 Location rhs_location = locations->InAt(1);
2884 Register rhs_reg = ZERO;
2885 int32_t rhs_imm = 0;
2886 bool use_imm = rhs_location.IsConstant();
2887 if (use_imm) {
2888 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2889 } else {
2890 rhs_reg = rhs_location.AsRegister<Register>();
2891 }
2892
2893 if (use_imm && rhs_imm == 0) {
2894 switch (cond) {
2895 case kCondEQ:
2896 case kCondBE: // <= 0 if zero
2897 __ Beqz(lhs, label);
2898 break;
2899 case kCondNE:
2900 case kCondA: // > 0 if non-zero
2901 __ Bnez(lhs, label);
2902 break;
2903 case kCondLT:
2904 __ Bltz(lhs, label);
2905 break;
2906 case kCondGE:
2907 __ Bgez(lhs, label);
2908 break;
2909 case kCondLE:
2910 __ Blez(lhs, label);
2911 break;
2912 case kCondGT:
2913 __ Bgtz(lhs, label);
2914 break;
2915 case kCondB: // always false
2916 break;
2917 case kCondAE: // always true
2918 __ B(label);
2919 break;
2920 }
2921 } else {
2922 if (use_imm) {
2923 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2924 rhs_reg = TMP;
2925 __ LoadConst32(rhs_reg, rhs_imm);
2926 }
2927 switch (cond) {
2928 case kCondEQ:
2929 __ Beq(lhs, rhs_reg, label);
2930 break;
2931 case kCondNE:
2932 __ Bne(lhs, rhs_reg, label);
2933 break;
2934 case kCondLT:
2935 __ Blt(lhs, rhs_reg, label);
2936 break;
2937 case kCondGE:
2938 __ Bge(lhs, rhs_reg, label);
2939 break;
2940 case kCondLE:
2941 __ Bge(rhs_reg, lhs, label);
2942 break;
2943 case kCondGT:
2944 __ Blt(rhs_reg, lhs, label);
2945 break;
2946 case kCondB:
2947 __ Bltu(lhs, rhs_reg, label);
2948 break;
2949 case kCondAE:
2950 __ Bgeu(lhs, rhs_reg, label);
2951 break;
2952 case kCondBE:
2953 __ Bgeu(rhs_reg, lhs, label);
2954 break;
2955 case kCondA:
2956 __ Bltu(rhs_reg, lhs, label);
2957 break;
2958 }
2959 }
2960}
2961
2962void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2963 LocationSummary* locations,
2964 MipsLabel* label) {
2965 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2966 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2967 Location rhs_location = locations->InAt(1);
2968 Register rhs_high = ZERO;
2969 Register rhs_low = ZERO;
2970 int64_t imm = 0;
2971 uint32_t imm_high = 0;
2972 uint32_t imm_low = 0;
2973 bool use_imm = rhs_location.IsConstant();
2974 if (use_imm) {
2975 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2976 imm_high = High32Bits(imm);
2977 imm_low = Low32Bits(imm);
2978 } else {
2979 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2980 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2981 }
2982
2983 if (use_imm && imm == 0) {
2984 switch (cond) {
2985 case kCondEQ:
2986 case kCondBE: // <= 0 if zero
2987 __ Or(TMP, lhs_high, lhs_low);
2988 __ Beqz(TMP, label);
2989 break;
2990 case kCondNE:
2991 case kCondA: // > 0 if non-zero
2992 __ Or(TMP, lhs_high, lhs_low);
2993 __ Bnez(TMP, label);
2994 break;
2995 case kCondLT:
2996 __ Bltz(lhs_high, label);
2997 break;
2998 case kCondGE:
2999 __ Bgez(lhs_high, label);
3000 break;
3001 case kCondLE:
3002 __ Or(TMP, lhs_high, lhs_low);
3003 __ Sra(AT, lhs_high, 31);
3004 __ Bgeu(AT, TMP, label);
3005 break;
3006 case kCondGT:
3007 __ Or(TMP, lhs_high, lhs_low);
3008 __ Sra(AT, lhs_high, 31);
3009 __ Bltu(AT, TMP, label);
3010 break;
3011 case kCondB: // always false
3012 break;
3013 case kCondAE: // always true
3014 __ B(label);
3015 break;
3016 }
3017 } else if (use_imm) {
3018 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3019 switch (cond) {
3020 case kCondEQ:
3021 __ LoadConst32(TMP, imm_high);
3022 __ Xor(TMP, TMP, lhs_high);
3023 __ LoadConst32(AT, imm_low);
3024 __ Xor(AT, AT, lhs_low);
3025 __ Or(TMP, TMP, AT);
3026 __ Beqz(TMP, label);
3027 break;
3028 case kCondNE:
3029 __ LoadConst32(TMP, imm_high);
3030 __ Xor(TMP, TMP, lhs_high);
3031 __ LoadConst32(AT, imm_low);
3032 __ Xor(AT, AT, lhs_low);
3033 __ Or(TMP, TMP, AT);
3034 __ Bnez(TMP, label);
3035 break;
3036 case kCondLT:
3037 __ LoadConst32(TMP, imm_high);
3038 __ Blt(lhs_high, TMP, label);
3039 __ Slt(TMP, TMP, lhs_high);
3040 __ LoadConst32(AT, imm_low);
3041 __ Sltu(AT, lhs_low, AT);
3042 __ Blt(TMP, AT, label);
3043 break;
3044 case kCondGE:
3045 __ LoadConst32(TMP, imm_high);
3046 __ Blt(TMP, lhs_high, label);
3047 __ Slt(TMP, lhs_high, TMP);
3048 __ LoadConst32(AT, imm_low);
3049 __ Sltu(AT, lhs_low, AT);
3050 __ Or(TMP, TMP, AT);
3051 __ Beqz(TMP, label);
3052 break;
3053 case kCondLE:
3054 __ LoadConst32(TMP, imm_high);
3055 __ Blt(lhs_high, TMP, label);
3056 __ Slt(TMP, TMP, lhs_high);
3057 __ LoadConst32(AT, imm_low);
3058 __ Sltu(AT, AT, lhs_low);
3059 __ Or(TMP, TMP, AT);
3060 __ Beqz(TMP, label);
3061 break;
3062 case kCondGT:
3063 __ LoadConst32(TMP, imm_high);
3064 __ Blt(TMP, lhs_high, label);
3065 __ Slt(TMP, lhs_high, TMP);
3066 __ LoadConst32(AT, imm_low);
3067 __ Sltu(AT, AT, lhs_low);
3068 __ Blt(TMP, AT, label);
3069 break;
3070 case kCondB:
3071 __ LoadConst32(TMP, imm_high);
3072 __ Bltu(lhs_high, TMP, label);
3073 __ Sltu(TMP, TMP, lhs_high);
3074 __ LoadConst32(AT, imm_low);
3075 __ Sltu(AT, lhs_low, AT);
3076 __ Blt(TMP, AT, label);
3077 break;
3078 case kCondAE:
3079 __ LoadConst32(TMP, imm_high);
3080 __ Bltu(TMP, lhs_high, label);
3081 __ Sltu(TMP, lhs_high, TMP);
3082 __ LoadConst32(AT, imm_low);
3083 __ Sltu(AT, lhs_low, AT);
3084 __ Or(TMP, TMP, AT);
3085 __ Beqz(TMP, label);
3086 break;
3087 case kCondBE:
3088 __ LoadConst32(TMP, imm_high);
3089 __ Bltu(lhs_high, TMP, label);
3090 __ Sltu(TMP, TMP, lhs_high);
3091 __ LoadConst32(AT, imm_low);
3092 __ Sltu(AT, AT, lhs_low);
3093 __ Or(TMP, TMP, AT);
3094 __ Beqz(TMP, label);
3095 break;
3096 case kCondA:
3097 __ LoadConst32(TMP, imm_high);
3098 __ Bltu(TMP, lhs_high, label);
3099 __ Sltu(TMP, lhs_high, TMP);
3100 __ LoadConst32(AT, imm_low);
3101 __ Sltu(AT, AT, lhs_low);
3102 __ Blt(TMP, AT, label);
3103 break;
3104 }
3105 } else {
3106 switch (cond) {
3107 case kCondEQ:
3108 __ Xor(TMP, lhs_high, rhs_high);
3109 __ Xor(AT, lhs_low, rhs_low);
3110 __ Or(TMP, TMP, AT);
3111 __ Beqz(TMP, label);
3112 break;
3113 case kCondNE:
3114 __ Xor(TMP, lhs_high, rhs_high);
3115 __ Xor(AT, lhs_low, rhs_low);
3116 __ Or(TMP, TMP, AT);
3117 __ Bnez(TMP, label);
3118 break;
3119 case kCondLT:
3120 __ Blt(lhs_high, rhs_high, label);
3121 __ Slt(TMP, rhs_high, lhs_high);
3122 __ Sltu(AT, lhs_low, rhs_low);
3123 __ Blt(TMP, AT, label);
3124 break;
3125 case kCondGE:
3126 __ Blt(rhs_high, lhs_high, label);
3127 __ Slt(TMP, lhs_high, rhs_high);
3128 __ Sltu(AT, lhs_low, rhs_low);
3129 __ Or(TMP, TMP, AT);
3130 __ Beqz(TMP, label);
3131 break;
3132 case kCondLE:
3133 __ Blt(lhs_high, rhs_high, label);
3134 __ Slt(TMP, rhs_high, lhs_high);
3135 __ Sltu(AT, rhs_low, lhs_low);
3136 __ Or(TMP, TMP, AT);
3137 __ Beqz(TMP, label);
3138 break;
3139 case kCondGT:
3140 __ Blt(rhs_high, lhs_high, label);
3141 __ Slt(TMP, lhs_high, rhs_high);
3142 __ Sltu(AT, rhs_low, lhs_low);
3143 __ Blt(TMP, AT, label);
3144 break;
3145 case kCondB:
3146 __ Bltu(lhs_high, rhs_high, label);
3147 __ Sltu(TMP, rhs_high, lhs_high);
3148 __ Sltu(AT, lhs_low, rhs_low);
3149 __ Blt(TMP, AT, label);
3150 break;
3151 case kCondAE:
3152 __ Bltu(rhs_high, lhs_high, label);
3153 __ Sltu(TMP, lhs_high, rhs_high);
3154 __ Sltu(AT, lhs_low, rhs_low);
3155 __ Or(TMP, TMP, AT);
3156 __ Beqz(TMP, label);
3157 break;
3158 case kCondBE:
3159 __ Bltu(lhs_high, rhs_high, label);
3160 __ Sltu(TMP, rhs_high, lhs_high);
3161 __ Sltu(AT, rhs_low, lhs_low);
3162 __ Or(TMP, TMP, AT);
3163 __ Beqz(TMP, label);
3164 break;
3165 case kCondA:
3166 __ Bltu(rhs_high, lhs_high, label);
3167 __ Sltu(TMP, lhs_high, rhs_high);
3168 __ Sltu(AT, rhs_low, lhs_low);
3169 __ Blt(TMP, AT, label);
3170 break;
3171 }
3172 }
3173}
3174
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003175void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3176 bool gt_bias,
3177 Primitive::Type type,
3178 LocationSummary* locations) {
3179 Register dst = locations->Out().AsRegister<Register>();
3180 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3181 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3182 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3183 if (type == Primitive::kPrimFloat) {
3184 if (isR6) {
3185 switch (cond) {
3186 case kCondEQ:
3187 __ CmpEqS(FTMP, lhs, rhs);
3188 __ Mfc1(dst, FTMP);
3189 __ Andi(dst, dst, 1);
3190 break;
3191 case kCondNE:
3192 __ CmpEqS(FTMP, lhs, rhs);
3193 __ Mfc1(dst, FTMP);
3194 __ Addiu(dst, dst, 1);
3195 break;
3196 case kCondLT:
3197 if (gt_bias) {
3198 __ CmpLtS(FTMP, lhs, rhs);
3199 } else {
3200 __ CmpUltS(FTMP, lhs, rhs);
3201 }
3202 __ Mfc1(dst, FTMP);
3203 __ Andi(dst, dst, 1);
3204 break;
3205 case kCondLE:
3206 if (gt_bias) {
3207 __ CmpLeS(FTMP, lhs, rhs);
3208 } else {
3209 __ CmpUleS(FTMP, lhs, rhs);
3210 }
3211 __ Mfc1(dst, FTMP);
3212 __ Andi(dst, dst, 1);
3213 break;
3214 case kCondGT:
3215 if (gt_bias) {
3216 __ CmpUltS(FTMP, rhs, lhs);
3217 } else {
3218 __ CmpLtS(FTMP, rhs, lhs);
3219 }
3220 __ Mfc1(dst, FTMP);
3221 __ Andi(dst, dst, 1);
3222 break;
3223 case kCondGE:
3224 if (gt_bias) {
3225 __ CmpUleS(FTMP, rhs, lhs);
3226 } else {
3227 __ CmpLeS(FTMP, rhs, lhs);
3228 }
3229 __ Mfc1(dst, FTMP);
3230 __ Andi(dst, dst, 1);
3231 break;
3232 default:
3233 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3234 UNREACHABLE();
3235 }
3236 } else {
3237 switch (cond) {
3238 case kCondEQ:
3239 __ CeqS(0, lhs, rhs);
3240 __ LoadConst32(dst, 1);
3241 __ Movf(dst, ZERO, 0);
3242 break;
3243 case kCondNE:
3244 __ CeqS(0, lhs, rhs);
3245 __ LoadConst32(dst, 1);
3246 __ Movt(dst, ZERO, 0);
3247 break;
3248 case kCondLT:
3249 if (gt_bias) {
3250 __ ColtS(0, lhs, rhs);
3251 } else {
3252 __ CultS(0, lhs, rhs);
3253 }
3254 __ LoadConst32(dst, 1);
3255 __ Movf(dst, ZERO, 0);
3256 break;
3257 case kCondLE:
3258 if (gt_bias) {
3259 __ ColeS(0, lhs, rhs);
3260 } else {
3261 __ CuleS(0, lhs, rhs);
3262 }
3263 __ LoadConst32(dst, 1);
3264 __ Movf(dst, ZERO, 0);
3265 break;
3266 case kCondGT:
3267 if (gt_bias) {
3268 __ CultS(0, rhs, lhs);
3269 } else {
3270 __ ColtS(0, rhs, lhs);
3271 }
3272 __ LoadConst32(dst, 1);
3273 __ Movf(dst, ZERO, 0);
3274 break;
3275 case kCondGE:
3276 if (gt_bias) {
3277 __ CuleS(0, rhs, lhs);
3278 } else {
3279 __ ColeS(0, rhs, lhs);
3280 }
3281 __ LoadConst32(dst, 1);
3282 __ Movf(dst, ZERO, 0);
3283 break;
3284 default:
3285 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3286 UNREACHABLE();
3287 }
3288 }
3289 } else {
3290 DCHECK_EQ(type, Primitive::kPrimDouble);
3291 if (isR6) {
3292 switch (cond) {
3293 case kCondEQ:
3294 __ CmpEqD(FTMP, lhs, rhs);
3295 __ Mfc1(dst, FTMP);
3296 __ Andi(dst, dst, 1);
3297 break;
3298 case kCondNE:
3299 __ CmpEqD(FTMP, lhs, rhs);
3300 __ Mfc1(dst, FTMP);
3301 __ Addiu(dst, dst, 1);
3302 break;
3303 case kCondLT:
3304 if (gt_bias) {
3305 __ CmpLtD(FTMP, lhs, rhs);
3306 } else {
3307 __ CmpUltD(FTMP, lhs, rhs);
3308 }
3309 __ Mfc1(dst, FTMP);
3310 __ Andi(dst, dst, 1);
3311 break;
3312 case kCondLE:
3313 if (gt_bias) {
3314 __ CmpLeD(FTMP, lhs, rhs);
3315 } else {
3316 __ CmpUleD(FTMP, lhs, rhs);
3317 }
3318 __ Mfc1(dst, FTMP);
3319 __ Andi(dst, dst, 1);
3320 break;
3321 case kCondGT:
3322 if (gt_bias) {
3323 __ CmpUltD(FTMP, rhs, lhs);
3324 } else {
3325 __ CmpLtD(FTMP, rhs, lhs);
3326 }
3327 __ Mfc1(dst, FTMP);
3328 __ Andi(dst, dst, 1);
3329 break;
3330 case kCondGE:
3331 if (gt_bias) {
3332 __ CmpUleD(FTMP, rhs, lhs);
3333 } else {
3334 __ CmpLeD(FTMP, rhs, lhs);
3335 }
3336 __ Mfc1(dst, FTMP);
3337 __ Andi(dst, dst, 1);
3338 break;
3339 default:
3340 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3341 UNREACHABLE();
3342 }
3343 } else {
3344 switch (cond) {
3345 case kCondEQ:
3346 __ CeqD(0, lhs, rhs);
3347 __ LoadConst32(dst, 1);
3348 __ Movf(dst, ZERO, 0);
3349 break;
3350 case kCondNE:
3351 __ CeqD(0, lhs, rhs);
3352 __ LoadConst32(dst, 1);
3353 __ Movt(dst, ZERO, 0);
3354 break;
3355 case kCondLT:
3356 if (gt_bias) {
3357 __ ColtD(0, lhs, rhs);
3358 } else {
3359 __ CultD(0, lhs, rhs);
3360 }
3361 __ LoadConst32(dst, 1);
3362 __ Movf(dst, ZERO, 0);
3363 break;
3364 case kCondLE:
3365 if (gt_bias) {
3366 __ ColeD(0, lhs, rhs);
3367 } else {
3368 __ CuleD(0, lhs, rhs);
3369 }
3370 __ LoadConst32(dst, 1);
3371 __ Movf(dst, ZERO, 0);
3372 break;
3373 case kCondGT:
3374 if (gt_bias) {
3375 __ CultD(0, rhs, lhs);
3376 } else {
3377 __ ColtD(0, rhs, lhs);
3378 }
3379 __ LoadConst32(dst, 1);
3380 __ Movf(dst, ZERO, 0);
3381 break;
3382 case kCondGE:
3383 if (gt_bias) {
3384 __ CuleD(0, rhs, lhs);
3385 } else {
3386 __ ColeD(0, rhs, lhs);
3387 }
3388 __ LoadConst32(dst, 1);
3389 __ Movf(dst, ZERO, 0);
3390 break;
3391 default:
3392 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3393 UNREACHABLE();
3394 }
3395 }
3396 }
3397}
3398
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003399void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3400 bool gt_bias,
3401 Primitive::Type type,
3402 LocationSummary* locations,
3403 MipsLabel* label) {
3404 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3405 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3406 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3407 if (type == Primitive::kPrimFloat) {
3408 if (isR6) {
3409 switch (cond) {
3410 case kCondEQ:
3411 __ CmpEqS(FTMP, lhs, rhs);
3412 __ Bc1nez(FTMP, label);
3413 break;
3414 case kCondNE:
3415 __ CmpEqS(FTMP, lhs, rhs);
3416 __ Bc1eqz(FTMP, label);
3417 break;
3418 case kCondLT:
3419 if (gt_bias) {
3420 __ CmpLtS(FTMP, lhs, rhs);
3421 } else {
3422 __ CmpUltS(FTMP, lhs, rhs);
3423 }
3424 __ Bc1nez(FTMP, label);
3425 break;
3426 case kCondLE:
3427 if (gt_bias) {
3428 __ CmpLeS(FTMP, lhs, rhs);
3429 } else {
3430 __ CmpUleS(FTMP, lhs, rhs);
3431 }
3432 __ Bc1nez(FTMP, label);
3433 break;
3434 case kCondGT:
3435 if (gt_bias) {
3436 __ CmpUltS(FTMP, rhs, lhs);
3437 } else {
3438 __ CmpLtS(FTMP, rhs, lhs);
3439 }
3440 __ Bc1nez(FTMP, label);
3441 break;
3442 case kCondGE:
3443 if (gt_bias) {
3444 __ CmpUleS(FTMP, rhs, lhs);
3445 } else {
3446 __ CmpLeS(FTMP, rhs, lhs);
3447 }
3448 __ Bc1nez(FTMP, label);
3449 break;
3450 default:
3451 LOG(FATAL) << "Unexpected non-floating-point condition";
3452 }
3453 } else {
3454 switch (cond) {
3455 case kCondEQ:
3456 __ CeqS(0, lhs, rhs);
3457 __ Bc1t(0, label);
3458 break;
3459 case kCondNE:
3460 __ CeqS(0, lhs, rhs);
3461 __ Bc1f(0, label);
3462 break;
3463 case kCondLT:
3464 if (gt_bias) {
3465 __ ColtS(0, lhs, rhs);
3466 } else {
3467 __ CultS(0, lhs, rhs);
3468 }
3469 __ Bc1t(0, label);
3470 break;
3471 case kCondLE:
3472 if (gt_bias) {
3473 __ ColeS(0, lhs, rhs);
3474 } else {
3475 __ CuleS(0, lhs, rhs);
3476 }
3477 __ Bc1t(0, label);
3478 break;
3479 case kCondGT:
3480 if (gt_bias) {
3481 __ CultS(0, rhs, lhs);
3482 } else {
3483 __ ColtS(0, rhs, lhs);
3484 }
3485 __ Bc1t(0, label);
3486 break;
3487 case kCondGE:
3488 if (gt_bias) {
3489 __ CuleS(0, rhs, lhs);
3490 } else {
3491 __ ColeS(0, rhs, lhs);
3492 }
3493 __ Bc1t(0, label);
3494 break;
3495 default:
3496 LOG(FATAL) << "Unexpected non-floating-point condition";
3497 }
3498 }
3499 } else {
3500 DCHECK_EQ(type, Primitive::kPrimDouble);
3501 if (isR6) {
3502 switch (cond) {
3503 case kCondEQ:
3504 __ CmpEqD(FTMP, lhs, rhs);
3505 __ Bc1nez(FTMP, label);
3506 break;
3507 case kCondNE:
3508 __ CmpEqD(FTMP, lhs, rhs);
3509 __ Bc1eqz(FTMP, label);
3510 break;
3511 case kCondLT:
3512 if (gt_bias) {
3513 __ CmpLtD(FTMP, lhs, rhs);
3514 } else {
3515 __ CmpUltD(FTMP, lhs, rhs);
3516 }
3517 __ Bc1nez(FTMP, label);
3518 break;
3519 case kCondLE:
3520 if (gt_bias) {
3521 __ CmpLeD(FTMP, lhs, rhs);
3522 } else {
3523 __ CmpUleD(FTMP, lhs, rhs);
3524 }
3525 __ Bc1nez(FTMP, label);
3526 break;
3527 case kCondGT:
3528 if (gt_bias) {
3529 __ CmpUltD(FTMP, rhs, lhs);
3530 } else {
3531 __ CmpLtD(FTMP, rhs, lhs);
3532 }
3533 __ Bc1nez(FTMP, label);
3534 break;
3535 case kCondGE:
3536 if (gt_bias) {
3537 __ CmpUleD(FTMP, rhs, lhs);
3538 } else {
3539 __ CmpLeD(FTMP, rhs, lhs);
3540 }
3541 __ Bc1nez(FTMP, label);
3542 break;
3543 default:
3544 LOG(FATAL) << "Unexpected non-floating-point condition";
3545 }
3546 } else {
3547 switch (cond) {
3548 case kCondEQ:
3549 __ CeqD(0, lhs, rhs);
3550 __ Bc1t(0, label);
3551 break;
3552 case kCondNE:
3553 __ CeqD(0, lhs, rhs);
3554 __ Bc1f(0, label);
3555 break;
3556 case kCondLT:
3557 if (gt_bias) {
3558 __ ColtD(0, lhs, rhs);
3559 } else {
3560 __ CultD(0, lhs, rhs);
3561 }
3562 __ Bc1t(0, label);
3563 break;
3564 case kCondLE:
3565 if (gt_bias) {
3566 __ ColeD(0, lhs, rhs);
3567 } else {
3568 __ CuleD(0, lhs, rhs);
3569 }
3570 __ Bc1t(0, label);
3571 break;
3572 case kCondGT:
3573 if (gt_bias) {
3574 __ CultD(0, rhs, lhs);
3575 } else {
3576 __ ColtD(0, rhs, lhs);
3577 }
3578 __ Bc1t(0, label);
3579 break;
3580 case kCondGE:
3581 if (gt_bias) {
3582 __ CuleD(0, rhs, lhs);
3583 } else {
3584 __ ColeD(0, rhs, lhs);
3585 }
3586 __ Bc1t(0, label);
3587 break;
3588 default:
3589 LOG(FATAL) << "Unexpected non-floating-point condition";
3590 }
3591 }
3592 }
3593}
3594
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003596 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003597 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003598 MipsLabel* false_target) {
3599 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003600
David Brazdil0debae72015-11-12 18:37:00 +00003601 if (true_target == nullptr && false_target == nullptr) {
3602 // Nothing to do. The code always falls through.
3603 return;
3604 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003605 // Constant condition, statically compared against "true" (integer value 1).
3606 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003607 if (true_target != nullptr) {
3608 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003609 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003610 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003611 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003612 if (false_target != nullptr) {
3613 __ B(false_target);
3614 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003615 }
David Brazdil0debae72015-11-12 18:37:00 +00003616 return;
3617 }
3618
3619 // The following code generates these patterns:
3620 // (1) true_target == nullptr && false_target != nullptr
3621 // - opposite condition true => branch to false_target
3622 // (2) true_target != nullptr && false_target == nullptr
3623 // - condition true => branch to true_target
3624 // (3) true_target != nullptr && false_target != nullptr
3625 // - condition true => branch to true_target
3626 // - branch to false_target
3627 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003628 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003629 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003630 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003631 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003632 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3633 } else {
3634 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3635 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003636 } else {
3637 // The condition instruction has not been materialized, use its inputs as
3638 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003639 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003640 Primitive::Type type = condition->InputAt(0)->GetType();
3641 LocationSummary* locations = cond->GetLocations();
3642 IfCondition if_cond = condition->GetCondition();
3643 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003644
David Brazdil0debae72015-11-12 18:37:00 +00003645 if (true_target == nullptr) {
3646 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003647 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003648 }
3649
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003650 switch (type) {
3651 default:
3652 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3653 break;
3654 case Primitive::kPrimLong:
3655 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3656 break;
3657 case Primitive::kPrimFloat:
3658 case Primitive::kPrimDouble:
3659 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3660 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003661 }
3662 }
David Brazdil0debae72015-11-12 18:37:00 +00003663
3664 // If neither branch falls through (case 3), the conditional branch to `true_target`
3665 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3666 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003667 __ B(false_target);
3668 }
3669}
3670
3671void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003673 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003674 locations->SetInAt(0, Location::RequiresRegister());
3675 }
3676}
3677
3678void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003679 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3680 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3681 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3682 nullptr : codegen_->GetLabelOf(true_successor);
3683 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3684 nullptr : codegen_->GetLabelOf(false_successor);
3685 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003686}
3687
3688void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3689 LocationSummary* locations = new (GetGraph()->GetArena())
3690 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01003691 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003692 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003693 locations->SetInAt(0, Location::RequiresRegister());
3694 }
3695}
3696
3697void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003698 SlowPathCodeMIPS* slow_path =
3699 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003700 GenerateTestAndBranch(deoptimize,
3701 /* condition_input_index */ 0,
3702 slow_path->GetEntryLabel(),
3703 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704}
3705
David Brazdil74eb1b22015-12-14 11:44:01 +00003706void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3707 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3708 if (Primitive::IsFloatingPointType(select->GetType())) {
3709 locations->SetInAt(0, Location::RequiresFpuRegister());
3710 locations->SetInAt(1, Location::RequiresFpuRegister());
3711 } else {
3712 locations->SetInAt(0, Location::RequiresRegister());
3713 locations->SetInAt(1, Location::RequiresRegister());
3714 }
3715 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3716 locations->SetInAt(2, Location::RequiresRegister());
3717 }
3718 locations->SetOut(Location::SameAsFirstInput());
3719}
3720
3721void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3722 LocationSummary* locations = select->GetLocations();
3723 MipsLabel false_target;
3724 GenerateTestAndBranch(select,
3725 /* condition_input_index */ 2,
3726 /* true_target */ nullptr,
3727 &false_target);
3728 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3729 __ Bind(&false_target);
3730}
3731
David Srbecky0cf44932015-12-09 14:09:59 +00003732void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3733 new (GetGraph()->GetArena()) LocationSummary(info);
3734}
3735
David Srbeckyd28f4a02016-03-14 17:14:24 +00003736void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3737 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003738}
3739
3740void CodeGeneratorMIPS::GenerateNop() {
3741 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003742}
3743
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003744void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3745 Primitive::Type field_type = field_info.GetFieldType();
3746 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3747 bool generate_volatile = field_info.IsVolatile() && is_wide;
3748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003749 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003750
3751 locations->SetInAt(0, Location::RequiresRegister());
3752 if (generate_volatile) {
3753 InvokeRuntimeCallingConvention calling_convention;
3754 // need A0 to hold base + offset
3755 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3756 if (field_type == Primitive::kPrimLong) {
3757 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3758 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003759 // Use Location::Any() to prevent situations when running out of available fp registers.
3760 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003761 // Need some temp core regs since FP results are returned in core registers
3762 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3763 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3764 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3765 }
3766 } else {
3767 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3768 locations->SetOut(Location::RequiresFpuRegister());
3769 } else {
3770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3771 }
3772 }
3773}
3774
3775void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3776 const FieldInfo& field_info,
3777 uint32_t dex_pc) {
3778 Primitive::Type type = field_info.GetFieldType();
3779 LocationSummary* locations = instruction->GetLocations();
3780 Register obj = locations->InAt(0).AsRegister<Register>();
3781 LoadOperandType load_type = kLoadUnsignedByte;
3782 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003783 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003784 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003785
3786 switch (type) {
3787 case Primitive::kPrimBoolean:
3788 load_type = kLoadUnsignedByte;
3789 break;
3790 case Primitive::kPrimByte:
3791 load_type = kLoadSignedByte;
3792 break;
3793 case Primitive::kPrimShort:
3794 load_type = kLoadSignedHalfword;
3795 break;
3796 case Primitive::kPrimChar:
3797 load_type = kLoadUnsignedHalfword;
3798 break;
3799 case Primitive::kPrimInt:
3800 case Primitive::kPrimFloat:
3801 case Primitive::kPrimNot:
3802 load_type = kLoadWord;
3803 break;
3804 case Primitive::kPrimLong:
3805 case Primitive::kPrimDouble:
3806 load_type = kLoadDoubleword;
3807 break;
3808 case Primitive::kPrimVoid:
3809 LOG(FATAL) << "Unreachable type " << type;
3810 UNREACHABLE();
3811 }
3812
3813 if (is_volatile && load_type == kLoadDoubleword) {
3814 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003815 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003816 // Do implicit Null check
3817 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3818 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01003819 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003820 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3821 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003822 // FP results are returned in core registers. Need to move them.
3823 Location out = locations->Out();
3824 if (out.IsFpuRegister()) {
3825 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3826 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3827 out.AsFpuRegister<FRegister>());
3828 } else {
3829 DCHECK(out.IsDoubleStackSlot());
3830 __ StoreToOffset(kStoreWord,
3831 locations->GetTemp(1).AsRegister<Register>(),
3832 SP,
3833 out.GetStackIndex());
3834 __ StoreToOffset(kStoreWord,
3835 locations->GetTemp(2).AsRegister<Register>(),
3836 SP,
3837 out.GetStackIndex() + 4);
3838 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003839 }
3840 } else {
3841 if (!Primitive::IsFloatingPointType(type)) {
3842 Register dst;
3843 if (type == Primitive::kPrimLong) {
3844 DCHECK(locations->Out().IsRegisterPair());
3845 dst = locations->Out().AsRegisterPairLow<Register>();
3846 } else {
3847 DCHECK(locations->Out().IsRegister());
3848 dst = locations->Out().AsRegister<Register>();
3849 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003850 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003851 } else {
3852 DCHECK(locations->Out().IsFpuRegister());
3853 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3854 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003855 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003856 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003857 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003858 }
3859 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003860 }
3861
3862 if (is_volatile) {
3863 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3864 }
3865}
3866
3867void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3868 Primitive::Type field_type = field_info.GetFieldType();
3869 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3870 bool generate_volatile = field_info.IsVolatile() && is_wide;
3871 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003872 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003873
3874 locations->SetInAt(0, Location::RequiresRegister());
3875 if (generate_volatile) {
3876 InvokeRuntimeCallingConvention calling_convention;
3877 // need A0 to hold base + offset
3878 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3879 if (field_type == Primitive::kPrimLong) {
3880 locations->SetInAt(1, Location::RegisterPairLocation(
3881 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3882 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003883 // Use Location::Any() to prevent situations when running out of available fp registers.
3884 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003885 // Pass FP parameters in core registers.
3886 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3887 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3888 }
3889 } else {
3890 if (Primitive::IsFloatingPointType(field_type)) {
3891 locations->SetInAt(1, Location::RequiresFpuRegister());
3892 } else {
3893 locations->SetInAt(1, Location::RequiresRegister());
3894 }
3895 }
3896}
3897
3898void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3899 const FieldInfo& field_info,
3900 uint32_t dex_pc) {
3901 Primitive::Type type = field_info.GetFieldType();
3902 LocationSummary* locations = instruction->GetLocations();
3903 Register obj = locations->InAt(0).AsRegister<Register>();
3904 StoreOperandType store_type = kStoreByte;
3905 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003906 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003907 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003908
3909 switch (type) {
3910 case Primitive::kPrimBoolean:
3911 case Primitive::kPrimByte:
3912 store_type = kStoreByte;
3913 break;
3914 case Primitive::kPrimShort:
3915 case Primitive::kPrimChar:
3916 store_type = kStoreHalfword;
3917 break;
3918 case Primitive::kPrimInt:
3919 case Primitive::kPrimFloat:
3920 case Primitive::kPrimNot:
3921 store_type = kStoreWord;
3922 break;
3923 case Primitive::kPrimLong:
3924 case Primitive::kPrimDouble:
3925 store_type = kStoreDoubleword;
3926 break;
3927 case Primitive::kPrimVoid:
3928 LOG(FATAL) << "Unreachable type " << type;
3929 UNREACHABLE();
3930 }
3931
3932 if (is_volatile) {
3933 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3934 }
3935
3936 if (is_volatile && store_type == kStoreDoubleword) {
3937 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003938 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003939 // Do implicit Null check.
3940 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3941 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3942 if (type == Primitive::kPrimDouble) {
3943 // Pass FP parameters in core registers.
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003944 Location in = locations->InAt(1);
3945 if (in.IsFpuRegister()) {
3946 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(), in.AsFpuRegister<FRegister>());
3947 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3948 in.AsFpuRegister<FRegister>());
3949 } else if (in.IsDoubleStackSlot()) {
3950 __ LoadFromOffset(kLoadWord,
3951 locations->GetTemp(1).AsRegister<Register>(),
3952 SP,
3953 in.GetStackIndex());
3954 __ LoadFromOffset(kLoadWord,
3955 locations->GetTemp(2).AsRegister<Register>(),
3956 SP,
3957 in.GetStackIndex() + 4);
3958 } else {
3959 DCHECK(in.IsConstant());
3960 DCHECK(in.GetConstant()->IsDoubleConstant());
3961 int64_t value = bit_cast<int64_t, double>(in.GetConstant()->AsDoubleConstant()->GetValue());
3962 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
3963 locations->GetTemp(1).AsRegister<Register>(),
3964 value);
3965 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003966 }
Serban Constantinescufca16662016-07-14 09:21:59 +01003967 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003968 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3969 } else {
3970 if (!Primitive::IsFloatingPointType(type)) {
3971 Register src;
3972 if (type == Primitive::kPrimLong) {
3973 DCHECK(locations->InAt(1).IsRegisterPair());
3974 src = locations->InAt(1).AsRegisterPairLow<Register>();
3975 } else {
3976 DCHECK(locations->InAt(1).IsRegister());
3977 src = locations->InAt(1).AsRegister<Register>();
3978 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003979 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003980 } else {
3981 DCHECK(locations->InAt(1).IsFpuRegister());
3982 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3983 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003984 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003985 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003986 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003987 }
3988 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003989 }
3990
3991 // TODO: memory barriers?
3992 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3993 DCHECK(locations->InAt(1).IsRegister());
3994 Register src = locations->InAt(1).AsRegister<Register>();
3995 codegen_->MarkGCCard(obj, src);
3996 }
3997
3998 if (is_volatile) {
3999 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4000 }
4001}
4002
4003void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4004 HandleFieldGet(instruction, instruction->GetFieldInfo());
4005}
4006
4007void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4008 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4009}
4010
4011void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4012 HandleFieldSet(instruction, instruction->GetFieldInfo());
4013}
4014
4015void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4016 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4017}
4018
Alexey Frunze06a46c42016-07-19 15:00:40 -07004019void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
4020 HInstruction* instruction ATTRIBUTE_UNUSED,
4021 Location root,
4022 Register obj,
4023 uint32_t offset) {
4024 Register root_reg = root.AsRegister<Register>();
4025 if (kEmitCompilerReadBarrier) {
4026 UNIMPLEMENTED(FATAL) << "for read barrier";
4027 } else {
4028 // Plain GC root load with no read barrier.
4029 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4030 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
4031 // Note that GC roots are not affected by heap poisoning, thus we
4032 // do not have to unpoison `root_reg` here.
4033 }
4034}
4035
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004036void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4037 LocationSummary::CallKind call_kind =
4038 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
4039 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4040 locations->SetInAt(0, Location::RequiresRegister());
4041 locations->SetInAt(1, Location::RequiresRegister());
4042 // The output does overlap inputs.
4043 // Note that TypeCheckSlowPathMIPS uses this register too.
4044 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4045}
4046
4047void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4048 LocationSummary* locations = instruction->GetLocations();
4049 Register obj = locations->InAt(0).AsRegister<Register>();
4050 Register cls = locations->InAt(1).AsRegister<Register>();
4051 Register out = locations->Out().AsRegister<Register>();
4052
4053 MipsLabel done;
4054
4055 // Return 0 if `obj` is null.
4056 // TODO: Avoid this check if we know `obj` is not null.
4057 __ Move(out, ZERO);
4058 __ Beqz(obj, &done);
4059
4060 // Compare the class of `obj` with `cls`.
4061 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
4062 if (instruction->IsExactCheck()) {
4063 // Classes must be equal for the instanceof to succeed.
4064 __ Xor(out, out, cls);
4065 __ Sltiu(out, out, 1);
4066 } else {
4067 // If the classes are not equal, we go into a slow path.
4068 DCHECK(locations->OnlyCallsOnSlowPath());
4069 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
4070 codegen_->AddSlowPath(slow_path);
4071 __ Bne(out, cls, slow_path->GetEntryLabel());
4072 __ LoadConst32(out, 1);
4073 __ Bind(slow_path->GetExitLabel());
4074 }
4075
4076 __ Bind(&done);
4077}
4078
4079void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
4080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4081 locations->SetOut(Location::ConstantLocation(constant));
4082}
4083
4084void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4085 // Will be generated at use site.
4086}
4087
4088void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
4089 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4090 locations->SetOut(Location::ConstantLocation(constant));
4091}
4092
4093void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4094 // Will be generated at use site.
4095}
4096
4097void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
4098 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
4099 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4100}
4101
4102void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4103 HandleInvoke(invoke);
4104 // The register T0 is required to be used for the hidden argument in
4105 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4106 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4107}
4108
4109void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4110 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4111 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004112 Location receiver = invoke->GetLocations()->InAt(0);
4113 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004114 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004115
4116 // Set the hidden argument.
4117 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
4118 invoke->GetDexMethodIndex());
4119
4120 // temp = object->GetClass();
4121 if (receiver.IsStackSlot()) {
4122 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4123 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4124 } else {
4125 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4126 }
4127 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004128 __ LoadFromOffset(kLoadWord, temp, temp,
4129 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
4130 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004131 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004132 // temp = temp->GetImtEntryAt(method_offset);
4133 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4134 // T9 = temp->GetEntryPoint();
4135 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4136 // T9();
4137 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004138 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004139 DCHECK(!codegen_->IsLeafMethod());
4140 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4141}
4142
4143void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07004144 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4145 if (intrinsic.TryDispatch(invoke)) {
4146 return;
4147 }
4148
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004149 HandleInvoke(invoke);
4150}
4151
4152void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004153 // Explicit clinit checks triggered by static invokes must have been pruned by
4154 // art::PrepareForRegisterAllocation.
4155 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004156
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004157 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4158 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4159 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4160
4161 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
4162 // R6 has PC-relative addressing.
4163 bool has_extra_input = !isR6 &&
4164 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4165 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4166
4167 if (invoke->HasPcRelativeDexCache()) {
4168 // kDexCachePcRelative is mutually exclusive with
4169 // kDirectAddressWithFixup/kCallDirectWithFixup.
4170 CHECK(!has_extra_input);
4171 has_extra_input = true;
4172 }
4173
Chris Larsen701566a2015-10-27 15:29:13 -07004174 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4175 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004176 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4177 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4178 }
Chris Larsen701566a2015-10-27 15:29:13 -07004179 return;
4180 }
4181
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004182 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004183
4184 // Add the extra input register if either the dex cache array base register
4185 // or the PC-relative base register for accessing literals is needed.
4186 if (has_extra_input) {
4187 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4188 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004189}
4190
Chris Larsen701566a2015-10-27 15:29:13 -07004191static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004192 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004193 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4194 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004195 return true;
4196 }
4197 return false;
4198}
4199
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004200HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004201 HLoadString::LoadKind desired_string_load_kind) {
4202 if (kEmitCompilerReadBarrier) {
4203 UNIMPLEMENTED(FATAL) << "for read barrier";
4204 }
4205 // We disable PC-relative load when there is an irreducible loop, as the optimization
4206 // is incompatible with it.
4207 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4208 bool fallback_load = has_irreducible_loops;
4209 switch (desired_string_load_kind) {
4210 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4211 DCHECK(!GetCompilerOptions().GetCompilePic());
4212 break;
4213 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4214 DCHECK(GetCompilerOptions().GetCompilePic());
4215 break;
4216 case HLoadString::LoadKind::kBootImageAddress:
4217 break;
4218 case HLoadString::LoadKind::kDexCacheAddress:
4219 DCHECK(Runtime::Current()->UseJitCompilation());
4220 fallback_load = false;
4221 break;
4222 case HLoadString::LoadKind::kDexCachePcRelative:
4223 DCHECK(!Runtime::Current()->UseJitCompilation());
4224 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4225 // with irreducible loops.
4226 break;
4227 case HLoadString::LoadKind::kDexCacheViaMethod:
4228 fallback_load = false;
4229 break;
4230 }
4231 if (fallback_load) {
4232 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4233 }
4234 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004235}
4236
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004237HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4238 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004239 if (kEmitCompilerReadBarrier) {
4240 UNIMPLEMENTED(FATAL) << "for read barrier";
4241 }
4242 // We disable pc-relative load when there is an irreducible loop, as the optimization
4243 // is incompatible with it.
4244 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4245 bool fallback_load = has_irreducible_loops;
4246 switch (desired_class_load_kind) {
4247 case HLoadClass::LoadKind::kReferrersClass:
4248 fallback_load = false;
4249 break;
4250 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4251 DCHECK(!GetCompilerOptions().GetCompilePic());
4252 break;
4253 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4254 DCHECK(GetCompilerOptions().GetCompilePic());
4255 break;
4256 case HLoadClass::LoadKind::kBootImageAddress:
4257 break;
4258 case HLoadClass::LoadKind::kDexCacheAddress:
4259 DCHECK(Runtime::Current()->UseJitCompilation());
4260 fallback_load = false;
4261 break;
4262 case HLoadClass::LoadKind::kDexCachePcRelative:
4263 DCHECK(!Runtime::Current()->UseJitCompilation());
4264 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4265 // with irreducible loops.
4266 break;
4267 case HLoadClass::LoadKind::kDexCacheViaMethod:
4268 fallback_load = false;
4269 break;
4270 }
4271 if (fallback_load) {
4272 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4273 }
4274 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004275}
4276
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004277Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4278 Register temp) {
4279 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4280 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4281 if (!invoke->GetLocations()->Intrinsified()) {
4282 return location.AsRegister<Register>();
4283 }
4284 // For intrinsics we allow any location, so it may be on the stack.
4285 if (!location.IsRegister()) {
4286 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4287 return temp;
4288 }
4289 // For register locations, check if the register was saved. If so, get it from the stack.
4290 // Note: There is a chance that the register was saved but not overwritten, so we could
4291 // save one load. However, since this is just an intrinsic slow path we prefer this
4292 // simple and more robust approach rather that trying to determine if that's the case.
4293 SlowPathCode* slow_path = GetCurrentSlowPath();
4294 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4295 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4296 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4297 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4298 return temp;
4299 }
4300 return location.AsRegister<Register>();
4301}
4302
Vladimir Markodc151b22015-10-15 18:02:30 +01004303HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4304 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4305 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004306 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4307 // We disable PC-relative load when there is an irreducible loop, as the optimization
4308 // is incompatible with it.
4309 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4310 bool fallback_load = true;
4311 bool fallback_call = true;
4312 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004313 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4314 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004315 fallback_load = has_irreducible_loops;
4316 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004317 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004318 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004319 break;
4320 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004321 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004322 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004323 fallback_call = has_irreducible_loops;
4324 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004325 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004326 // TODO: Implement this type.
4327 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004328 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004329 fallback_call = false;
4330 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004331 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004332 if (fallback_load) {
4333 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4334 dispatch_info.method_load_data = 0;
4335 }
4336 if (fallback_call) {
4337 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4338 dispatch_info.direct_code_ptr = 0;
4339 }
4340 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004341}
4342
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004343void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4344 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004345 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004346 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4347 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4348 bool isR6 = isa_features_.IsR6();
4349 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4350 // R6 has PC-relative addressing.
4351 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4352 (!isR6 &&
4353 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4354 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4355 Register base_reg = has_extra_input
4356 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4357 : ZERO;
4358
4359 // For better instruction scheduling we load the direct code pointer before the method pointer.
4360 switch (code_ptr_location) {
4361 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4362 // T9 = invoke->GetDirectCodePtr();
4363 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4364 break;
4365 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4366 // T9 = code address from literal pool with link-time patch.
4367 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4368 break;
4369 default:
4370 break;
4371 }
4372
4373 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004374 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4375 // temp = thread->string_init_entrypoint
4376 __ LoadFromOffset(kLoadWord,
4377 temp.AsRegister<Register>(),
4378 TR,
4379 invoke->GetStringInitOffset());
4380 break;
4381 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004382 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004383 break;
4384 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4385 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4386 break;
4387 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004388 __ LoadLiteral(temp.AsRegister<Register>(),
4389 base_reg,
4390 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4391 break;
4392 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4393 HMipsDexCacheArraysBase* base =
4394 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4395 int32_t offset =
4396 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4397 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4398 break;
4399 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004400 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004401 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004402 Register reg = temp.AsRegister<Register>();
4403 Register method_reg;
4404 if (current_method.IsRegister()) {
4405 method_reg = current_method.AsRegister<Register>();
4406 } else {
4407 // TODO: use the appropriate DCHECK() here if possible.
4408 // DCHECK(invoke->GetLocations()->Intrinsified());
4409 DCHECK(!current_method.IsValid());
4410 method_reg = reg;
4411 __ Lw(reg, SP, kCurrentMethodStackOffset);
4412 }
4413
4414 // temp = temp->dex_cache_resolved_methods_;
4415 __ LoadFromOffset(kLoadWord,
4416 reg,
4417 method_reg,
4418 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004419 // temp = temp[index_in_cache];
4420 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4421 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004422 __ LoadFromOffset(kLoadWord,
4423 reg,
4424 reg,
4425 CodeGenerator::GetCachePointerOffset(index_in_cache));
4426 break;
4427 }
4428 }
4429
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004430 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004431 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004432 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004433 break;
4434 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004435 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4436 // T9 prepared above for better instruction scheduling.
4437 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004438 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004439 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004440 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004441 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004442 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004443 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4444 LOG(FATAL) << "Unsupported";
4445 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004446 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4447 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004448 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004449 T9,
4450 callee_method.AsRegister<Register>(),
4451 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004452 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004453 // T9()
4454 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004455 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004456 break;
4457 }
4458 DCHECK(!IsLeafMethod());
4459}
4460
4461void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004462 // Explicit clinit checks triggered by static invokes must have been pruned by
4463 // art::PrepareForRegisterAllocation.
4464 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004465
4466 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4467 return;
4468 }
4469
4470 LocationSummary* locations = invoke->GetLocations();
4471 codegen_->GenerateStaticOrDirectCall(invoke,
4472 locations->HasTemps()
4473 ? locations->GetTemp(0)
4474 : Location::NoLocation());
4475 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4476}
4477
Chris Larsen3acee732015-11-18 13:31:08 -08004478void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004479 LocationSummary* locations = invoke->GetLocations();
4480 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004481 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004482 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4483 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4484 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004485 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004486
4487 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004488 DCHECK(receiver.IsRegister());
4489 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4490 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004491 // temp = temp->GetMethodAt(method_offset);
4492 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4493 // T9 = temp->GetEntryPoint();
4494 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4495 // T9();
4496 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004497 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004498}
4499
4500void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4501 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4502 return;
4503 }
4504
4505 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004506 DCHECK(!codegen_->IsLeafMethod());
4507 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4508}
4509
4510void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004511 if (cls->NeedsAccessCheck()) {
4512 InvokeRuntimeCallingConvention calling_convention;
4513 CodeGenerator::CreateLoadClassLocationSummary(
4514 cls,
4515 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4516 Location::RegisterLocation(V0),
4517 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4518 return;
4519 }
4520
4521 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4522 ? LocationSummary::kCallOnSlowPath
4523 : LocationSummary::kNoCall;
4524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4525 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4526 switch (load_kind) {
4527 // We need an extra register for PC-relative literals on R2.
4528 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4529 case HLoadClass::LoadKind::kBootImageAddress:
4530 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4531 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4532 break;
4533 }
4534 FALLTHROUGH_INTENDED;
4535 // We need an extra register for PC-relative dex cache accesses.
4536 case HLoadClass::LoadKind::kDexCachePcRelative:
4537 case HLoadClass::LoadKind::kReferrersClass:
4538 case HLoadClass::LoadKind::kDexCacheViaMethod:
4539 locations->SetInAt(0, Location::RequiresRegister());
4540 break;
4541 default:
4542 break;
4543 }
4544 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004545}
4546
4547void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4548 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004549 if (cls->NeedsAccessCheck()) {
4550 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004551 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004552 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004553 return;
4554 }
4555
Alexey Frunze06a46c42016-07-19 15:00:40 -07004556 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4557 Location out_loc = locations->Out();
4558 Register out = out_loc.AsRegister<Register>();
4559 Register base_or_current_method_reg;
4560 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4561 switch (load_kind) {
4562 // We need an extra register for PC-relative literals on R2.
4563 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4564 case HLoadClass::LoadKind::kBootImageAddress:
4565 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4566 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4567 break;
4568 // We need an extra register for PC-relative dex cache accesses.
4569 case HLoadClass::LoadKind::kDexCachePcRelative:
4570 case HLoadClass::LoadKind::kReferrersClass:
4571 case HLoadClass::LoadKind::kDexCacheViaMethod:
4572 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4573 break;
4574 default:
4575 base_or_current_method_reg = ZERO;
4576 break;
4577 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004578
Alexey Frunze06a46c42016-07-19 15:00:40 -07004579 bool generate_null_check = false;
4580 switch (load_kind) {
4581 case HLoadClass::LoadKind::kReferrersClass: {
4582 DCHECK(!cls->CanCallRuntime());
4583 DCHECK(!cls->MustGenerateClinitCheck());
4584 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4585 GenerateGcRootFieldLoad(cls,
4586 out_loc,
4587 base_or_current_method_reg,
4588 ArtMethod::DeclaringClassOffset().Int32Value());
4589 break;
4590 }
4591 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4592 DCHECK(!kEmitCompilerReadBarrier);
4593 __ LoadLiteral(out,
4594 base_or_current_method_reg,
4595 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4596 cls->GetTypeIndex()));
4597 break;
4598 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4599 DCHECK(!kEmitCompilerReadBarrier);
4600 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4601 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004602 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004603 if (isR6) {
4604 __ Bind(&info->high_label);
4605 __ Bind(&info->pc_rel_label);
4606 // Add a 32-bit offset to PC.
4607 __ Auipc(out, /* placeholder */ 0x1234);
4608 __ Addiu(out, out, /* placeholder */ 0x5678);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004609 } else {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004610 __ Bind(&info->high_label);
4611 __ Lui(out, /* placeholder */ 0x1234);
4612 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4613 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4614 __ Ori(out, out, /* placeholder */ 0x5678);
4615 // Add a 32-bit offset to PC.
4616 __ Addu(out, out, base_or_current_method_reg);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004617 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004618 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004619 break;
4620 }
4621 case HLoadClass::LoadKind::kBootImageAddress: {
4622 DCHECK(!kEmitCompilerReadBarrier);
4623 DCHECK_NE(cls->GetAddress(), 0u);
4624 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4625 __ LoadLiteral(out,
4626 base_or_current_method_reg,
4627 codegen_->DeduplicateBootImageAddressLiteral(address));
4628 break;
4629 }
4630 case HLoadClass::LoadKind::kDexCacheAddress: {
4631 DCHECK_NE(cls->GetAddress(), 0u);
4632 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4633 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4634 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4635 int16_t offset = Low16Bits(address);
4636 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4637 __ Lui(out, High16Bits(base_address));
4638 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4639 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4640 generate_null_check = !cls->IsInDexCache();
4641 break;
4642 }
4643 case HLoadClass::LoadKind::kDexCachePcRelative: {
4644 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4645 int32_t offset =
4646 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4647 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4648 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4649 generate_null_check = !cls->IsInDexCache();
4650 break;
4651 }
4652 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4653 // /* GcRoot<mirror::Class>[] */ out =
4654 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4655 __ LoadFromOffset(kLoadWord,
4656 out,
4657 base_or_current_method_reg,
4658 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4659 // /* GcRoot<mirror::Class> */ out = out[type_index]
4660 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4661 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4662 generate_null_check = !cls->IsInDexCache();
4663 }
4664 }
4665
4666 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4667 DCHECK(cls->CanCallRuntime());
4668 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4669 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4670 codegen_->AddSlowPath(slow_path);
4671 if (generate_null_check) {
4672 __ Beqz(out, slow_path->GetEntryLabel());
4673 }
4674 if (cls->MustGenerateClinitCheck()) {
4675 GenerateClassInitializationCheck(slow_path, out);
4676 } else {
4677 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004678 }
4679 }
4680}
4681
4682static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004683 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004684}
4685
4686void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4687 LocationSummary* locations =
4688 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4689 locations->SetOut(Location::RequiresRegister());
4690}
4691
4692void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4693 Register out = load->GetLocations()->Out().AsRegister<Register>();
4694 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4695}
4696
4697void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4698 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4699}
4700
4701void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4702 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4703}
4704
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004705void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004706 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004707 ? LocationSummary::kCallOnSlowPath
4708 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004710 HLoadString::LoadKind load_kind = load->GetLoadKind();
4711 switch (load_kind) {
4712 // We need an extra register for PC-relative literals on R2.
4713 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4714 case HLoadString::LoadKind::kBootImageAddress:
4715 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4716 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4717 break;
4718 }
4719 FALLTHROUGH_INTENDED;
4720 // We need an extra register for PC-relative dex cache accesses.
4721 case HLoadString::LoadKind::kDexCachePcRelative:
4722 case HLoadString::LoadKind::kDexCacheViaMethod:
4723 locations->SetInAt(0, Location::RequiresRegister());
4724 break;
4725 default:
4726 break;
4727 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004728 locations->SetOut(Location::RequiresRegister());
4729}
4730
4731void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004732 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004733 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004734 Location out_loc = locations->Out();
4735 Register out = out_loc.AsRegister<Register>();
4736 Register base_or_current_method_reg;
4737 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4738 switch (load_kind) {
4739 // We need an extra register for PC-relative literals on R2.
4740 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4741 case HLoadString::LoadKind::kBootImageAddress:
4742 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4743 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4744 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004745 default:
4746 base_or_current_method_reg = ZERO;
4747 break;
4748 }
4749
4750 switch (load_kind) {
4751 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4752 DCHECK(!kEmitCompilerReadBarrier);
4753 __ LoadLiteral(out,
4754 base_or_current_method_reg,
4755 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4756 load->GetStringIndex()));
4757 return; // No dex cache slow path.
4758 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4759 DCHECK(!kEmitCompilerReadBarrier);
4760 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4761 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004762 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004763 if (isR6) {
4764 __ Bind(&info->high_label);
4765 __ Bind(&info->pc_rel_label);
4766 // Add a 32-bit offset to PC.
4767 __ Auipc(out, /* placeholder */ 0x1234);
4768 __ Addiu(out, out, /* placeholder */ 0x5678);
4769 } else {
4770 __ Bind(&info->high_label);
4771 __ Lui(out, /* placeholder */ 0x1234);
4772 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4773 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4774 __ Ori(out, out, /* placeholder */ 0x5678);
4775 // Add a 32-bit offset to PC.
4776 __ Addu(out, out, base_or_current_method_reg);
4777 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004778 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004779 return; // No dex cache slow path.
4780 }
4781 case HLoadString::LoadKind::kBootImageAddress: {
4782 DCHECK(!kEmitCompilerReadBarrier);
4783 DCHECK_NE(load->GetAddress(), 0u);
4784 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4785 __ LoadLiteral(out,
4786 base_or_current_method_reg,
4787 codegen_->DeduplicateBootImageAddressLiteral(address));
4788 return; // No dex cache slow path.
4789 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004790 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004791 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004792 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004793
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004794 // TODO: Re-add the compiler code to do string dex cache lookup again.
4795 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4796 codegen_->AddSlowPath(slow_path);
4797 __ B(slow_path->GetEntryLabel());
4798 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004799}
4800
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004801void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4803 locations->SetOut(Location::ConstantLocation(constant));
4804}
4805
4806void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4807 // Will be generated at use site.
4808}
4809
4810void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4811 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004812 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004813 InvokeRuntimeCallingConvention calling_convention;
4814 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4815}
4816
4817void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4818 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01004819 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004820 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4821 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004822 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004823 }
4824 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4825}
4826
4827void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4828 LocationSummary* locations =
4829 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4830 switch (mul->GetResultType()) {
4831 case Primitive::kPrimInt:
4832 case Primitive::kPrimLong:
4833 locations->SetInAt(0, Location::RequiresRegister());
4834 locations->SetInAt(1, Location::RequiresRegister());
4835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4836 break;
4837
4838 case Primitive::kPrimFloat:
4839 case Primitive::kPrimDouble:
4840 locations->SetInAt(0, Location::RequiresFpuRegister());
4841 locations->SetInAt(1, Location::RequiresFpuRegister());
4842 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4843 break;
4844
4845 default:
4846 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4847 }
4848}
4849
4850void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4851 Primitive::Type type = instruction->GetType();
4852 LocationSummary* locations = instruction->GetLocations();
4853 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4854
4855 switch (type) {
4856 case Primitive::kPrimInt: {
4857 Register dst = locations->Out().AsRegister<Register>();
4858 Register lhs = locations->InAt(0).AsRegister<Register>();
4859 Register rhs = locations->InAt(1).AsRegister<Register>();
4860
4861 if (isR6) {
4862 __ MulR6(dst, lhs, rhs);
4863 } else {
4864 __ MulR2(dst, lhs, rhs);
4865 }
4866 break;
4867 }
4868 case Primitive::kPrimLong: {
4869 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4870 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4871 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4872 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4873 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4874 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4875
4876 // Extra checks to protect caused by the existance of A1_A2.
4877 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4878 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4879 DCHECK_NE(dst_high, lhs_low);
4880 DCHECK_NE(dst_high, rhs_low);
4881
4882 // A_B * C_D
4883 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4884 // dst_lo: [ low(B*D) ]
4885 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4886
4887 if (isR6) {
4888 __ MulR6(TMP, lhs_high, rhs_low);
4889 __ MulR6(dst_high, lhs_low, rhs_high);
4890 __ Addu(dst_high, dst_high, TMP);
4891 __ MuhuR6(TMP, lhs_low, rhs_low);
4892 __ Addu(dst_high, dst_high, TMP);
4893 __ MulR6(dst_low, lhs_low, rhs_low);
4894 } else {
4895 __ MulR2(TMP, lhs_high, rhs_low);
4896 __ MulR2(dst_high, lhs_low, rhs_high);
4897 __ Addu(dst_high, dst_high, TMP);
4898 __ MultuR2(lhs_low, rhs_low);
4899 __ Mfhi(TMP);
4900 __ Addu(dst_high, dst_high, TMP);
4901 __ Mflo(dst_low);
4902 }
4903 break;
4904 }
4905 case Primitive::kPrimFloat:
4906 case Primitive::kPrimDouble: {
4907 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4908 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4909 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4910 if (type == Primitive::kPrimFloat) {
4911 __ MulS(dst, lhs, rhs);
4912 } else {
4913 __ MulD(dst, lhs, rhs);
4914 }
4915 break;
4916 }
4917 default:
4918 LOG(FATAL) << "Unexpected mul type " << type;
4919 }
4920}
4921
4922void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4923 LocationSummary* locations =
4924 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4925 switch (neg->GetResultType()) {
4926 case Primitive::kPrimInt:
4927 case Primitive::kPrimLong:
4928 locations->SetInAt(0, Location::RequiresRegister());
4929 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4930 break;
4931
4932 case Primitive::kPrimFloat:
4933 case Primitive::kPrimDouble:
4934 locations->SetInAt(0, Location::RequiresFpuRegister());
4935 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4936 break;
4937
4938 default:
4939 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4940 }
4941}
4942
4943void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4944 Primitive::Type type = instruction->GetType();
4945 LocationSummary* locations = instruction->GetLocations();
4946
4947 switch (type) {
4948 case Primitive::kPrimInt: {
4949 Register dst = locations->Out().AsRegister<Register>();
4950 Register src = locations->InAt(0).AsRegister<Register>();
4951 __ Subu(dst, ZERO, src);
4952 break;
4953 }
4954 case Primitive::kPrimLong: {
4955 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4956 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4957 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4958 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4959 __ Subu(dst_low, ZERO, src_low);
4960 __ Sltu(TMP, ZERO, dst_low);
4961 __ Subu(dst_high, ZERO, src_high);
4962 __ Subu(dst_high, dst_high, TMP);
4963 break;
4964 }
4965 case Primitive::kPrimFloat:
4966 case Primitive::kPrimDouble: {
4967 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4968 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4969 if (type == Primitive::kPrimFloat) {
4970 __ NegS(dst, src);
4971 } else {
4972 __ NegD(dst, src);
4973 }
4974 break;
4975 }
4976 default:
4977 LOG(FATAL) << "Unexpected neg type " << type;
4978 }
4979}
4980
4981void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4982 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004984 InvokeRuntimeCallingConvention calling_convention;
4985 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4986 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4987 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4988 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4989}
4990
4991void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4992 InvokeRuntimeCallingConvention calling_convention;
4993 Register current_method_register = calling_convention.GetRegisterAt(2);
4994 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4995 // Move an uint16_t value to a register.
4996 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004997 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004998 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4999 void*, uint32_t, int32_t, ArtMethod*>();
5000}
5001
5002void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5003 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005005 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005006 if (instruction->IsStringAlloc()) {
5007 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5008 } else {
5009 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5010 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5011 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005012 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5013}
5014
5015void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005016 if (instruction->IsStringAlloc()) {
5017 // String is allocated through StringFactory. Call NewEmptyString entry point.
5018 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005019 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005020 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5021 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5022 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005023 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005024 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5025 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005026 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00005027 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
5028 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005029}
5030
5031void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5032 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5033 locations->SetInAt(0, Location::RequiresRegister());
5034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5035}
5036
5037void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5038 Primitive::Type type = instruction->GetType();
5039 LocationSummary* locations = instruction->GetLocations();
5040
5041 switch (type) {
5042 case Primitive::kPrimInt: {
5043 Register dst = locations->Out().AsRegister<Register>();
5044 Register src = locations->InAt(0).AsRegister<Register>();
5045 __ Nor(dst, src, ZERO);
5046 break;
5047 }
5048
5049 case Primitive::kPrimLong: {
5050 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5051 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5052 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5053 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5054 __ Nor(dst_high, src_high, ZERO);
5055 __ Nor(dst_low, src_low, ZERO);
5056 break;
5057 }
5058
5059 default:
5060 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5061 }
5062}
5063
5064void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5066 locations->SetInAt(0, Location::RequiresRegister());
5067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5068}
5069
5070void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5071 LocationSummary* locations = instruction->GetLocations();
5072 __ Xori(locations->Out().AsRegister<Register>(),
5073 locations->InAt(0).AsRegister<Register>(),
5074 1);
5075}
5076
5077void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko3b7537b2016-09-13 11:56:01 +00005078 codegen_->CreateNullCheckLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005079}
5080
Calin Juravle2ae48182016-03-16 14:05:09 +00005081void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5082 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005083 return;
5084 }
5085 Location obj = instruction->GetLocations()->InAt(0);
5086
5087 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005088 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005089}
5090
Calin Juravle2ae48182016-03-16 14:05:09 +00005091void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005092 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005093 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005094
5095 Location obj = instruction->GetLocations()->InAt(0);
5096
5097 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5098}
5099
5100void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005101 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005102}
5103
5104void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5105 HandleBinaryOp(instruction);
5106}
5107
5108void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5109 HandleBinaryOp(instruction);
5110}
5111
5112void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5113 LOG(FATAL) << "Unreachable";
5114}
5115
5116void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
5117 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5118}
5119
5120void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
5121 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5122 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5123 if (location.IsStackSlot()) {
5124 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5125 } else if (location.IsDoubleStackSlot()) {
5126 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5127 }
5128 locations->SetOut(location);
5129}
5130
5131void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
5132 ATTRIBUTE_UNUSED) {
5133 // Nothing to do, the parameter is already at its location.
5134}
5135
5136void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5137 LocationSummary* locations =
5138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5139 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5140}
5141
5142void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5143 ATTRIBUTE_UNUSED) {
5144 // Nothing to do, the method is already at its location.
5145}
5146
5147void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5148 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005149 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005150 locations->SetInAt(i, Location::Any());
5151 }
5152 locations->SetOut(Location::Any());
5153}
5154
5155void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5156 LOG(FATAL) << "Unreachable";
5157}
5158
5159void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5160 Primitive::Type type = rem->GetResultType();
5161 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005162 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5164
5165 switch (type) {
5166 case Primitive::kPrimInt:
5167 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005168 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5170 break;
5171
5172 case Primitive::kPrimLong: {
5173 InvokeRuntimeCallingConvention calling_convention;
5174 locations->SetInAt(0, Location::RegisterPairLocation(
5175 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5176 locations->SetInAt(1, Location::RegisterPairLocation(
5177 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5178 locations->SetOut(calling_convention.GetReturnLocation(type));
5179 break;
5180 }
5181
5182 case Primitive::kPrimFloat:
5183 case Primitive::kPrimDouble: {
5184 InvokeRuntimeCallingConvention calling_convention;
5185 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5186 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5187 locations->SetOut(calling_convention.GetReturnLocation(type));
5188 break;
5189 }
5190
5191 default:
5192 LOG(FATAL) << "Unexpected rem type " << type;
5193 }
5194}
5195
5196void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5197 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005198
5199 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005200 case Primitive::kPrimInt:
5201 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005202 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005203 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005204 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005205 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5206 break;
5207 }
5208 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005209 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005210 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005211 break;
5212 }
5213 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005214 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005215 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005216 break;
5217 }
5218 default:
5219 LOG(FATAL) << "Unexpected rem type " << type;
5220 }
5221}
5222
5223void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5224 memory_barrier->SetLocations(nullptr);
5225}
5226
5227void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5228 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5229}
5230
5231void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5232 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5233 Primitive::Type return_type = ret->InputAt(0)->GetType();
5234 locations->SetInAt(0, MipsReturnLocation(return_type));
5235}
5236
5237void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5238 codegen_->GenerateFrameExit();
5239}
5240
5241void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5242 ret->SetLocations(nullptr);
5243}
5244
5245void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5246 codegen_->GenerateFrameExit();
5247}
5248
Alexey Frunze92d90602015-12-18 18:16:36 -08005249void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5250 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005251}
5252
Alexey Frunze92d90602015-12-18 18:16:36 -08005253void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5254 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005255}
5256
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005257void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5258 HandleShift(shl);
5259}
5260
5261void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5262 HandleShift(shl);
5263}
5264
5265void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5266 HandleShift(shr);
5267}
5268
5269void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5270 HandleShift(shr);
5271}
5272
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005273void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5274 HandleBinaryOp(instruction);
5275}
5276
5277void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5278 HandleBinaryOp(instruction);
5279}
5280
5281void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5282 HandleFieldGet(instruction, instruction->GetFieldInfo());
5283}
5284
5285void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5286 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5287}
5288
5289void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5290 HandleFieldSet(instruction, instruction->GetFieldInfo());
5291}
5292
5293void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5294 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5295}
5296
5297void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5298 HUnresolvedInstanceFieldGet* instruction) {
5299 FieldAccessCallingConventionMIPS calling_convention;
5300 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5301 instruction->GetFieldType(),
5302 calling_convention);
5303}
5304
5305void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5306 HUnresolvedInstanceFieldGet* instruction) {
5307 FieldAccessCallingConventionMIPS calling_convention;
5308 codegen_->GenerateUnresolvedFieldAccess(instruction,
5309 instruction->GetFieldType(),
5310 instruction->GetFieldIndex(),
5311 instruction->GetDexPc(),
5312 calling_convention);
5313}
5314
5315void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5316 HUnresolvedInstanceFieldSet* instruction) {
5317 FieldAccessCallingConventionMIPS calling_convention;
5318 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5319 instruction->GetFieldType(),
5320 calling_convention);
5321}
5322
5323void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5324 HUnresolvedInstanceFieldSet* instruction) {
5325 FieldAccessCallingConventionMIPS calling_convention;
5326 codegen_->GenerateUnresolvedFieldAccess(instruction,
5327 instruction->GetFieldType(),
5328 instruction->GetFieldIndex(),
5329 instruction->GetDexPc(),
5330 calling_convention);
5331}
5332
5333void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5334 HUnresolvedStaticFieldGet* instruction) {
5335 FieldAccessCallingConventionMIPS calling_convention;
5336 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5337 instruction->GetFieldType(),
5338 calling_convention);
5339}
5340
5341void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5342 HUnresolvedStaticFieldGet* instruction) {
5343 FieldAccessCallingConventionMIPS calling_convention;
5344 codegen_->GenerateUnresolvedFieldAccess(instruction,
5345 instruction->GetFieldType(),
5346 instruction->GetFieldIndex(),
5347 instruction->GetDexPc(),
5348 calling_convention);
5349}
5350
5351void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5352 HUnresolvedStaticFieldSet* instruction) {
5353 FieldAccessCallingConventionMIPS calling_convention;
5354 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5355 instruction->GetFieldType(),
5356 calling_convention);
5357}
5358
5359void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5360 HUnresolvedStaticFieldSet* instruction) {
5361 FieldAccessCallingConventionMIPS calling_convention;
5362 codegen_->GenerateUnresolvedFieldAccess(instruction,
5363 instruction->GetFieldType(),
5364 instruction->GetFieldIndex(),
5365 instruction->GetDexPc(),
5366 calling_convention);
5367}
5368
5369void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005370 LocationSummary* locations =
5371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5372 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005373}
5374
5375void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5376 HBasicBlock* block = instruction->GetBlock();
5377 if (block->GetLoopInformation() != nullptr) {
5378 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5379 // The back edge will generate the suspend check.
5380 return;
5381 }
5382 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5383 // The goto will generate the suspend check.
5384 return;
5385 }
5386 GenerateSuspendCheck(instruction, nullptr);
5387}
5388
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005389void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5390 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005391 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005392 InvokeRuntimeCallingConvention calling_convention;
5393 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5394}
5395
5396void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005397 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005398 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5399}
5400
5401void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5402 Primitive::Type input_type = conversion->GetInputType();
5403 Primitive::Type result_type = conversion->GetResultType();
5404 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005405 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005406
5407 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5408 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5409 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5410 }
5411
5412 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005413 if (!isR6 &&
5414 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5415 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005416 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005417 }
5418
5419 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5420
5421 if (call_kind == LocationSummary::kNoCall) {
5422 if (Primitive::IsFloatingPointType(input_type)) {
5423 locations->SetInAt(0, Location::RequiresFpuRegister());
5424 } else {
5425 locations->SetInAt(0, Location::RequiresRegister());
5426 }
5427
5428 if (Primitive::IsFloatingPointType(result_type)) {
5429 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5430 } else {
5431 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5432 }
5433 } else {
5434 InvokeRuntimeCallingConvention calling_convention;
5435
5436 if (Primitive::IsFloatingPointType(input_type)) {
5437 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5438 } else {
5439 DCHECK_EQ(input_type, Primitive::kPrimLong);
5440 locations->SetInAt(0, Location::RegisterPairLocation(
5441 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5442 }
5443
5444 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5445 }
5446}
5447
5448void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5449 LocationSummary* locations = conversion->GetLocations();
5450 Primitive::Type result_type = conversion->GetResultType();
5451 Primitive::Type input_type = conversion->GetInputType();
5452 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005453 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005454
5455 DCHECK_NE(input_type, result_type);
5456
5457 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5458 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5459 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5460 Register src = locations->InAt(0).AsRegister<Register>();
5461
Alexey Frunzea871ef12016-06-27 15:20:11 -07005462 if (dst_low != src) {
5463 __ Move(dst_low, src);
5464 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005465 __ Sra(dst_high, src, 31);
5466 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5467 Register dst = locations->Out().AsRegister<Register>();
5468 Register src = (input_type == Primitive::kPrimLong)
5469 ? locations->InAt(0).AsRegisterPairLow<Register>()
5470 : locations->InAt(0).AsRegister<Register>();
5471
5472 switch (result_type) {
5473 case Primitive::kPrimChar:
5474 __ Andi(dst, src, 0xFFFF);
5475 break;
5476 case Primitive::kPrimByte:
5477 if (has_sign_extension) {
5478 __ Seb(dst, src);
5479 } else {
5480 __ Sll(dst, src, 24);
5481 __ Sra(dst, dst, 24);
5482 }
5483 break;
5484 case Primitive::kPrimShort:
5485 if (has_sign_extension) {
5486 __ Seh(dst, src);
5487 } else {
5488 __ Sll(dst, src, 16);
5489 __ Sra(dst, dst, 16);
5490 }
5491 break;
5492 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005493 if (dst != src) {
5494 __ Move(dst, src);
5495 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005496 break;
5497
5498 default:
5499 LOG(FATAL) << "Unexpected type conversion from " << input_type
5500 << " to " << result_type;
5501 }
5502 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005503 if (input_type == Primitive::kPrimLong) {
5504 if (isR6) {
5505 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5506 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5507 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5508 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5509 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5510 __ Mtc1(src_low, FTMP);
5511 __ Mthc1(src_high, FTMP);
5512 if (result_type == Primitive::kPrimFloat) {
5513 __ Cvtsl(dst, FTMP);
5514 } else {
5515 __ Cvtdl(dst, FTMP);
5516 }
5517 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005518 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
5519 : kQuickL2d;
5520 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005521 if (result_type == Primitive::kPrimFloat) {
5522 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5523 } else {
5524 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5525 }
5526 }
5527 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005528 Register src = locations->InAt(0).AsRegister<Register>();
5529 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5530 __ Mtc1(src, FTMP);
5531 if (result_type == Primitive::kPrimFloat) {
5532 __ Cvtsw(dst, FTMP);
5533 } else {
5534 __ Cvtdw(dst, FTMP);
5535 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005536 }
5537 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5538 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005539 if (result_type == Primitive::kPrimLong) {
5540 if (isR6) {
5541 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5542 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5543 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5544 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5545 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5546 MipsLabel truncate;
5547 MipsLabel done;
5548
5549 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5550 // value when the input is either a NaN or is outside of the range of the output type
5551 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5552 // the same result.
5553 //
5554 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5555 // value of the output type if the input is outside of the range after the truncation or
5556 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5557 // results. This matches the desired float/double-to-int/long conversion exactly.
5558 //
5559 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5560 //
5561 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5562 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5563 // even though it must be NAN2008=1 on R6.
5564 //
5565 // The code takes care of the different behaviors by first comparing the input to the
5566 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5567 // If the input is greater than or equal to the minimum, it procedes to the truncate
5568 // instruction, which will handle such an input the same way irrespective of NAN2008.
5569 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5570 // in order to return either zero or the minimum value.
5571 //
5572 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5573 // truncate instruction for MIPS64R6.
5574 if (input_type == Primitive::kPrimFloat) {
5575 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5576 __ LoadConst32(TMP, min_val);
5577 __ Mtc1(TMP, FTMP);
5578 __ CmpLeS(FTMP, FTMP, src);
5579 } else {
5580 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5581 __ LoadConst32(TMP, High32Bits(min_val));
5582 __ Mtc1(ZERO, FTMP);
5583 __ Mthc1(TMP, FTMP);
5584 __ CmpLeD(FTMP, FTMP, src);
5585 }
5586
5587 __ Bc1nez(FTMP, &truncate);
5588
5589 if (input_type == Primitive::kPrimFloat) {
5590 __ CmpEqS(FTMP, src, src);
5591 } else {
5592 __ CmpEqD(FTMP, src, src);
5593 }
5594 __ Move(dst_low, ZERO);
5595 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5596 __ Mfc1(TMP, FTMP);
5597 __ And(dst_high, dst_high, TMP);
5598
5599 __ B(&done);
5600
5601 __ Bind(&truncate);
5602
5603 if (input_type == Primitive::kPrimFloat) {
5604 __ TruncLS(FTMP, src);
5605 } else {
5606 __ TruncLD(FTMP, src);
5607 }
5608 __ Mfc1(dst_low, FTMP);
5609 __ Mfhc1(dst_high, FTMP);
5610
5611 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005612 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005613 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
5614 : kQuickD2l;
5615 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005616 if (input_type == Primitive::kPrimFloat) {
5617 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5618 } else {
5619 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5620 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005621 }
5622 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005623 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5624 Register dst = locations->Out().AsRegister<Register>();
5625 MipsLabel truncate;
5626 MipsLabel done;
5627
5628 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5629 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5630 // even though it must be NAN2008=1 on R6.
5631 //
5632 // For details see the large comment above for the truncation of float/double to long on R6.
5633 //
5634 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5635 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005636 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005637 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5638 __ LoadConst32(TMP, min_val);
5639 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005640 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005641 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5642 __ LoadConst32(TMP, High32Bits(min_val));
5643 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005644 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005645 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005646
5647 if (isR6) {
5648 if (input_type == Primitive::kPrimFloat) {
5649 __ CmpLeS(FTMP, FTMP, src);
5650 } else {
5651 __ CmpLeD(FTMP, FTMP, src);
5652 }
5653 __ Bc1nez(FTMP, &truncate);
5654
5655 if (input_type == Primitive::kPrimFloat) {
5656 __ CmpEqS(FTMP, src, src);
5657 } else {
5658 __ CmpEqD(FTMP, src, src);
5659 }
5660 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5661 __ Mfc1(TMP, FTMP);
5662 __ And(dst, dst, TMP);
5663 } else {
5664 if (input_type == Primitive::kPrimFloat) {
5665 __ ColeS(0, FTMP, src);
5666 } else {
5667 __ ColeD(0, FTMP, src);
5668 }
5669 __ Bc1t(0, &truncate);
5670
5671 if (input_type == Primitive::kPrimFloat) {
5672 __ CeqS(0, src, src);
5673 } else {
5674 __ CeqD(0, src, src);
5675 }
5676 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5677 __ Movf(dst, ZERO, 0);
5678 }
5679
5680 __ B(&done);
5681
5682 __ Bind(&truncate);
5683
5684 if (input_type == Primitive::kPrimFloat) {
5685 __ TruncWS(FTMP, src);
5686 } else {
5687 __ TruncWD(FTMP, src);
5688 }
5689 __ Mfc1(dst, FTMP);
5690
5691 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005692 }
5693 } else if (Primitive::IsFloatingPointType(result_type) &&
5694 Primitive::IsFloatingPointType(input_type)) {
5695 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5696 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5697 if (result_type == Primitive::kPrimFloat) {
5698 __ Cvtsd(dst, src);
5699 } else {
5700 __ Cvtds(dst, src);
5701 }
5702 } else {
5703 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5704 << " to " << result_type;
5705 }
5706}
5707
5708void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5709 HandleShift(ushr);
5710}
5711
5712void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5713 HandleShift(ushr);
5714}
5715
5716void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5717 HandleBinaryOp(instruction);
5718}
5719
5720void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5721 HandleBinaryOp(instruction);
5722}
5723
5724void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5725 // Nothing to do, this should be removed during prepare for register allocator.
5726 LOG(FATAL) << "Unreachable";
5727}
5728
5729void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5730 // Nothing to do, this should be removed during prepare for register allocator.
5731 LOG(FATAL) << "Unreachable";
5732}
5733
5734void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005735 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005736}
5737
5738void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005739 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005740}
5741
5742void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005743 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005744}
5745
5746void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005747 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005748}
5749
5750void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005751 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005752}
5753
5754void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005755 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005756}
5757
5758void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005759 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005760}
5761
5762void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005763 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005764}
5765
5766void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005767 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005768}
5769
5770void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005771 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005772}
5773
5774void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005775 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005776}
5777
5778void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005779 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005780}
5781
5782void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005783 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005784}
5785
5786void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005787 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005788}
5789
5790void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005791 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005792}
5793
5794void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005795 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005796}
5797
5798void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005799 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005800}
5801
5802void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005803 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005804}
5805
5806void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005807 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005808}
5809
5810void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005811 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005812}
5813
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005814void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5815 LocationSummary* locations =
5816 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5817 locations->SetInAt(0, Location::RequiresRegister());
5818}
5819
Alexey Frunze96b66822016-09-10 02:32:44 -07005820void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
5821 int32_t lower_bound,
5822 uint32_t num_entries,
5823 HBasicBlock* switch_block,
5824 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005825 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005826 Register temp_reg = TMP;
5827 __ Addiu32(temp_reg, value_reg, -lower_bound);
5828 // Jump to default if index is negative
5829 // Note: We don't check the case that index is positive while value < lower_bound, because in
5830 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5831 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5832
Alexey Frunze96b66822016-09-10 02:32:44 -07005833 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005834 // Jump to successors[0] if value == lower_bound.
5835 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5836 int32_t last_index = 0;
5837 for (; num_entries - last_index > 2; last_index += 2) {
5838 __ Addiu(temp_reg, temp_reg, -2);
5839 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5840 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5841 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5842 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5843 }
5844 if (num_entries - last_index == 2) {
5845 // The last missing case_value.
5846 __ Addiu(temp_reg, temp_reg, -1);
5847 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005848 }
5849
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005850 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07005851 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005852 __ B(codegen_->GetLabelOf(default_block));
5853 }
5854}
5855
Alexey Frunze96b66822016-09-10 02:32:44 -07005856void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
5857 Register constant_area,
5858 int32_t lower_bound,
5859 uint32_t num_entries,
5860 HBasicBlock* switch_block,
5861 HBasicBlock* default_block) {
5862 // Create a jump table.
5863 std::vector<MipsLabel*> labels(num_entries);
5864 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
5865 for (uint32_t i = 0; i < num_entries; i++) {
5866 labels[i] = codegen_->GetLabelOf(successors[i]);
5867 }
5868 JumpTable* table = __ CreateJumpTable(std::move(labels));
5869
5870 // Is the value in range?
5871 __ Addiu32(TMP, value_reg, -lower_bound);
5872 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
5873 __ Sltiu(AT, TMP, num_entries);
5874 __ Beqz(AT, codegen_->GetLabelOf(default_block));
5875 } else {
5876 __ LoadConst32(AT, num_entries);
5877 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
5878 }
5879
5880 // We are in the range of the table.
5881 // Load the target address from the jump table, indexing by the value.
5882 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
5883 __ Sll(TMP, TMP, 2);
5884 __ Addu(TMP, TMP, AT);
5885 __ Lw(TMP, TMP, 0);
5886 // Compute the absolute target address by adding the table start address
5887 // (the table contains offsets to targets relative to its start).
5888 __ Addu(TMP, TMP, AT);
5889 // And jump.
5890 __ Jr(TMP);
5891 __ NopIfNoReordering();
5892}
5893
5894void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5895 int32_t lower_bound = switch_instr->GetStartValue();
5896 uint32_t num_entries = switch_instr->GetNumEntries();
5897 LocationSummary* locations = switch_instr->GetLocations();
5898 Register value_reg = locations->InAt(0).AsRegister<Register>();
5899 HBasicBlock* switch_block = switch_instr->GetBlock();
5900 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5901
5902 if (codegen_->GetInstructionSetFeatures().IsR6() &&
5903 num_entries > kPackedSwitchJumpTableThreshold) {
5904 // R6 uses PC-relative addressing to access the jump table.
5905 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
5906 // the jump table and it is implemented by changing HPackedSwitch to
5907 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
5908 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
5909 GenTableBasedPackedSwitch(value_reg,
5910 ZERO,
5911 lower_bound,
5912 num_entries,
5913 switch_block,
5914 default_block);
5915 } else {
5916 GenPackedSwitchWithCompares(value_reg,
5917 lower_bound,
5918 num_entries,
5919 switch_block,
5920 default_block);
5921 }
5922}
5923
5924void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5925 LocationSummary* locations =
5926 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5927 locations->SetInAt(0, Location::RequiresRegister());
5928 // Constant area pointer (HMipsComputeBaseMethodAddress).
5929 locations->SetInAt(1, Location::RequiresRegister());
5930}
5931
5932void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5933 int32_t lower_bound = switch_instr->GetStartValue();
5934 uint32_t num_entries = switch_instr->GetNumEntries();
5935 LocationSummary* locations = switch_instr->GetLocations();
5936 Register value_reg = locations->InAt(0).AsRegister<Register>();
5937 Register constant_area = locations->InAt(1).AsRegister<Register>();
5938 HBasicBlock* switch_block = switch_instr->GetBlock();
5939 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5940
5941 // This is an R2-only path. HPackedSwitch has been changed to
5942 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
5943 // required to address the jump table relative to PC.
5944 GenTableBasedPackedSwitch(value_reg,
5945 constant_area,
5946 lower_bound,
5947 num_entries,
5948 switch_block,
5949 default_block);
5950}
5951
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005952void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5953 HMipsComputeBaseMethodAddress* insn) {
5954 LocationSummary* locations =
5955 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5956 locations->SetOut(Location::RequiresRegister());
5957}
5958
5959void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5960 HMipsComputeBaseMethodAddress* insn) {
5961 LocationSummary* locations = insn->GetLocations();
5962 Register reg = locations->Out().AsRegister<Register>();
5963
5964 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5965
5966 // Generate a dummy PC-relative call to obtain PC.
5967 __ Nal();
5968 // Grab the return address off RA.
5969 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005970 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005971
5972 // Remember this offset (the obtained PC value) for later use with constant area.
5973 __ BindPcRelBaseLabel();
5974}
5975
5976void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5978 locations->SetOut(Location::RequiresRegister());
5979}
5980
5981void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5982 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5983 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5984 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005985 bool reordering = __ SetReorder(false);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005986 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5987 __ Bind(&info->high_label);
5988 __ Bind(&info->pc_rel_label);
5989 // Add a 32-bit offset to PC.
5990 __ Auipc(reg, /* placeholder */ 0x1234);
5991 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5992 } else {
5993 // Generate a dummy PC-relative call to obtain PC.
5994 __ Nal();
5995 __ Bind(&info->high_label);
5996 __ Lui(reg, /* placeholder */ 0x1234);
5997 __ Bind(&info->pc_rel_label);
5998 __ Ori(reg, reg, /* placeholder */ 0x5678);
5999 // Add a 32-bit offset to PC.
6000 __ Addu(reg, reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006001 // TODO: Can we share this code with that of VisitMipsComputeBaseMethodAddress()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006002 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006003 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006004}
6005
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006006void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6007 // The trampoline uses the same calling convention as dex calling conventions,
6008 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6009 // the method_idx.
6010 HandleInvoke(invoke);
6011}
6012
6013void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6014 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6015}
6016
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006017void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6018 LocationSummary* locations =
6019 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6020 locations->SetInAt(0, Location::RequiresRegister());
6021 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006022}
6023
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006024void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6025 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006026 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006027 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006028 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006029 __ LoadFromOffset(kLoadWord,
6030 locations->Out().AsRegister<Register>(),
6031 locations->InAt(0).AsRegister<Register>(),
6032 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006033 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006034 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006035 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006036 __ LoadFromOffset(kLoadWord,
6037 locations->Out().AsRegister<Register>(),
6038 locations->InAt(0).AsRegister<Register>(),
6039 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006040 __ LoadFromOffset(kLoadWord,
6041 locations->Out().AsRegister<Register>(),
6042 locations->Out().AsRegister<Register>(),
6043 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006044 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006045}
6046
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006047#undef __
6048#undef QUICK_ENTRY_POINT
6049
6050} // namespace mips
6051} // namespace art