blob: 334d30d90e2f9c8c6e56a16bdb0a51b02c5c6050 [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"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020023#include "entrypoints/quick/quick_entrypoints.h"
24#include "entrypoints/quick/quick_entrypoints_enum.h"
25#include "gc/accounting/card_table.h"
26#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070027#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020028#include "mirror/array-inl.h"
29#include "mirror/class-inl.h"
30#include "offsets.h"
31#include "thread.h"
32#include "utils/assembler.h"
33#include "utils/mips/assembler_mips.h"
34#include "utils/stack_checks.h"
35
36namespace art {
37namespace mips {
38
39static constexpr int kCurrentMethodStackOffset = 0;
40static constexpr Register kMethodRegisterArgument = A0;
41
Alexey Frunzee3fb2452016-05-10 16:08:05 -070042// We'll maximize the range of a single load instruction for dex cache array accesses
43// by aligning offset -32768 with the offset of the first used element.
44static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
45
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020046Location MipsReturnLocation(Primitive::Type return_type) {
47 switch (return_type) {
48 case Primitive::kPrimBoolean:
49 case Primitive::kPrimByte:
50 case Primitive::kPrimChar:
51 case Primitive::kPrimShort:
52 case Primitive::kPrimInt:
53 case Primitive::kPrimNot:
54 return Location::RegisterLocation(V0);
55
56 case Primitive::kPrimLong:
57 return Location::RegisterPairLocation(V0, V1);
58
59 case Primitive::kPrimFloat:
60 case Primitive::kPrimDouble:
61 return Location::FpuRegisterLocation(F0);
62
63 case Primitive::kPrimVoid:
64 return Location();
65 }
66 UNREACHABLE();
67}
68
69Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
70 return MipsReturnLocation(type);
71}
72
73Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
74 return Location::RegisterLocation(kMethodRegisterArgument);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
78 Location next_location;
79
80 switch (type) {
81 case Primitive::kPrimBoolean:
82 case Primitive::kPrimByte:
83 case Primitive::kPrimChar:
84 case Primitive::kPrimShort:
85 case Primitive::kPrimInt:
86 case Primitive::kPrimNot: {
87 uint32_t gp_index = gp_index_++;
88 if (gp_index < calling_convention.GetNumberOfRegisters()) {
89 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
90 } else {
91 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
92 next_location = Location::StackSlot(stack_offset);
93 }
94 break;
95 }
96
97 case Primitive::kPrimLong: {
98 uint32_t gp_index = gp_index_;
99 gp_index_ += 2;
100 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
101 if (calling_convention.GetRegisterAt(gp_index) == A1) {
102 gp_index_++; // Skip A1, and use A2_A3 instead.
103 gp_index++;
104 }
105 Register low_even = calling_convention.GetRegisterAt(gp_index);
106 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
107 DCHECK_EQ(low_even + 1, high_odd);
108 next_location = Location::RegisterPairLocation(low_even, high_odd);
109 } else {
110 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
111 next_location = Location::DoubleStackSlot(stack_offset);
112 }
113 break;
114 }
115
116 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
117 // will take up the even/odd pair, while floats are stored in even regs only.
118 // On 64 bit FPU, both double and float are stored in even registers only.
119 case Primitive::kPrimFloat:
120 case Primitive::kPrimDouble: {
121 uint32_t float_index = float_index_++;
122 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
123 next_location = Location::FpuRegisterLocation(
124 calling_convention.GetFpuRegisterAt(float_index));
125 } else {
126 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
127 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
128 : Location::StackSlot(stack_offset);
129 }
130 break;
131 }
132
133 case Primitive::kPrimVoid:
134 LOG(FATAL) << "Unexpected parameter type " << type;
135 break;
136 }
137
138 // Space on the stack is reserved for all arguments.
139 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
140
141 return next_location;
142}
143
144Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
145 return MipsReturnLocation(type);
146}
147
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700148// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
149#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
151
152class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
153 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000154 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155
156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
157 LocationSummary* locations = instruction_->GetLocations();
158 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
159 __ Bind(GetEntryLabel());
160 if (instruction_->CanThrowIntoCatchBlock()) {
161 // Live registers will be restored in the catch block if caught.
162 SaveLiveRegisters(codegen, instruction_->GetLocations());
163 }
164 // We're moving two locations to locations that could overlap, so we need a parallel
165 // move resolver.
166 InvokeRuntimeCallingConvention calling_convention;
167 codegen->EmitParallelMoves(locations->InAt(0),
168 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
169 Primitive::kPrimInt,
170 locations->InAt(1),
171 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
172 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100173 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
174 ? QUICK_ENTRY_POINT(pThrowStringBounds)
175 : QUICK_ENTRY_POINT(pThrowArrayBounds);
176 mips_codegen->InvokeRuntime(entry_point_offset,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200177 instruction_,
178 instruction_->GetDexPc(),
179 this,
180 IsDirectEntrypoint(kQuickThrowArrayBounds));
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100181 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200182 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
183 }
184
185 bool IsFatal() const OVERRIDE { return true; }
186
187 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
188
189 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200190 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
191};
192
193class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
194 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000195 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196
197 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
198 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
199 __ Bind(GetEntryLabel());
200 if (instruction_->CanThrowIntoCatchBlock()) {
201 // Live registers will be restored in the catch block if caught.
202 SaveLiveRegisters(codegen, instruction_->GetLocations());
203 }
204 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
205 instruction_,
206 instruction_->GetDexPc(),
207 this,
208 IsDirectEntrypoint(kQuickThrowDivZero));
209 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
210 }
211
212 bool IsFatal() const OVERRIDE { return true; }
213
214 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
215
216 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
218};
219
220class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
221 public:
222 LoadClassSlowPathMIPS(HLoadClass* cls,
223 HInstruction* at,
224 uint32_t dex_pc,
225 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000226 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 LocationSummary* locations = at_->GetLocations();
232 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
233
234 __ Bind(GetEntryLabel());
235 SaveLiveRegisters(codegen, locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
238 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
239
240 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
241 : QUICK_ENTRY_POINT(pInitializeType);
242 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
243 : IsDirectEntrypoint(kQuickInitializeType);
244
245 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
246 if (do_clinit_) {
247 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
248 } else {
249 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
250 }
251
252 // Move the class to the desired location.
253 Location out = locations->Out();
254 if (out.IsValid()) {
255 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
256 Primitive::Type type = at_->GetType();
257 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
258 }
259
260 RestoreLiveRegisters(codegen, locations);
261 __ B(GetExitLabel());
262 }
263
264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
265
266 private:
267 // The class this slow path will load.
268 HLoadClass* const cls_;
269
270 // The instruction where this slow path is happening.
271 // (Might be the load class or an initialization check).
272 HInstruction* const at_;
273
274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
281};
282
283class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
284 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286
287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
288 LocationSummary* locations = instruction_->GetLocations();
289 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
290 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
291
292 __ Bind(GetEntryLabel());
293 SaveLiveRegisters(codegen, locations);
294
295 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000296 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
297 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200298 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
299 instruction_,
300 instruction_->GetDexPc(),
301 this,
302 IsDirectEntrypoint(kQuickResolveString));
303 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
304 Primitive::Type type = instruction_->GetType();
305 mips_codegen->MoveLocation(locations->Out(),
306 calling_convention.GetReturnLocation(type),
307 type);
308
309 RestoreLiveRegisters(codegen, locations);
310 __ B(GetExitLabel());
311 }
312
313 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
314
315 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
317};
318
319class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
320 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
325 __ Bind(GetEntryLabel());
326 if (instruction_->CanThrowIntoCatchBlock()) {
327 // Live registers will be restored in the catch block if caught.
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 }
330 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
331 instruction_,
332 instruction_->GetDexPc(),
333 this,
334 IsDirectEntrypoint(kQuickThrowNullPointer));
335 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
336 }
337
338 bool IsFatal() const OVERRIDE { return true; }
339
340 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
341
342 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200343 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
344};
345
346class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
347 public:
348 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
355 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
356 instruction_,
357 instruction_->GetDexPc(),
358 this,
359 IsDirectEntrypoint(kQuickTestSuspend));
360 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
361 RestoreLiveRegisters(codegen, instruction_->GetLocations());
362 if (successor_ == nullptr) {
363 __ B(GetReturnLabel());
364 } else {
365 __ B(mips_codegen->GetLabelOf(successor_));
366 }
367 }
368
369 MipsLabel* GetReturnLabel() {
370 DCHECK(successor_ == nullptr);
371 return &return_label_;
372 }
373
374 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
375
376 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377 // If not null, the block to branch to after the suspend check.
378 HBasicBlock* const successor_;
379
380 // If `successor_` is null, the label to branch to after the suspend check.
381 MipsLabel return_label_;
382
383 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
384};
385
386class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
387 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
392 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
393 uint32_t dex_pc = instruction_->GetDexPc();
394 DCHECK(instruction_->IsCheckCast()
395 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
396 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
397
398 __ Bind(GetEntryLabel());
399 SaveLiveRegisters(codegen, locations);
400
401 // We're moving two locations to locations that could overlap, so we need a parallel
402 // move resolver.
403 InvokeRuntimeCallingConvention calling_convention;
404 codegen->EmitParallelMoves(locations->InAt(1),
405 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
406 Primitive::kPrimNot,
407 object_class,
408 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
409 Primitive::kPrimNot);
410
411 if (instruction_->IsInstanceOf()) {
412 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
413 instruction_,
414 dex_pc,
415 this,
416 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000417 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700418 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200419 Primitive::Type ret_type = instruction_->GetType();
420 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
421 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200422 } else {
423 DCHECK(instruction_->IsCheckCast());
424 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
425 instruction_,
426 dex_pc,
427 this,
428 IsDirectEntrypoint(kQuickCheckCast));
429 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
430 }
431
432 RestoreLiveRegisters(codegen, locations);
433 __ B(GetExitLabel());
434 }
435
436 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
437
438 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200439 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
440};
441
442class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
443 public:
Aart Bik42249c32016-01-07 15:33:50 -0800444 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000445 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446
447 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800448 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200449 __ Bind(GetEntryLabel());
450 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200451 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
452 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800453 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 this,
455 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000456 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200457 }
458
459 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
460
461 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200462 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
463};
464
465CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
466 const MipsInstructionSetFeatures& isa_features,
467 const CompilerOptions& compiler_options,
468 OptimizingCompilerStats* stats)
469 : CodeGenerator(graph,
470 kNumberOfCoreRegisters,
471 kNumberOfFRegisters,
472 kNumberOfRegisterPairs,
473 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
474 arraysize(kCoreCalleeSaves)),
475 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
476 arraysize(kFpuCalleeSaves)),
477 compiler_options,
478 stats),
479 block_labels_(nullptr),
480 location_builder_(graph, this),
481 instruction_visitor_(graph, this),
482 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100483 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700484 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700485 uint32_literals_(std::less<uint32_t>(),
486 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700487 method_patches_(MethodReferenceComparator(),
488 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
489 call_patches_(MethodReferenceComparator(),
490 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700491 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
492 boot_image_string_patches_(StringReferenceValueComparator(),
493 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
494 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
495 boot_image_type_patches_(TypeReferenceValueComparator(),
496 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
497 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
498 boot_image_address_patches_(std::less<uint32_t>(),
499 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
500 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200501 // Save RA (containing the return address) to mimic Quick.
502 AddAllocatedRegister(Location::RegisterLocation(RA));
503}
504
505#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700506// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
507#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200508#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
509
510void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
511 // Ensure that we fix up branches.
512 __ FinalizeCode();
513
514 // Adjust native pc offsets in stack maps.
515 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
516 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
517 uint32_t new_position = __ GetAdjustedPosition(old_position);
518 DCHECK_GE(new_position, old_position);
519 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
520 }
521
522 // Adjust pc offsets for the disassembly information.
523 if (disasm_info_ != nullptr) {
524 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
525 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
526 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
527 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
528 it.second.start = __ GetAdjustedPosition(it.second.start);
529 it.second.end = __ GetAdjustedPosition(it.second.end);
530 }
531 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
532 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
533 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
534 }
535 }
536
537 CodeGenerator::Finalize(allocator);
538}
539
540MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
541 return codegen_->GetAssembler();
542}
543
544void ParallelMoveResolverMIPS::EmitMove(size_t index) {
545 DCHECK_LT(index, moves_.size());
546 MoveOperands* move = moves_[index];
547 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
548}
549
550void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
551 DCHECK_LT(index, moves_.size());
552 MoveOperands* move = moves_[index];
553 Primitive::Type type = move->GetType();
554 Location loc1 = move->GetDestination();
555 Location loc2 = move->GetSource();
556
557 DCHECK(!loc1.IsConstant());
558 DCHECK(!loc2.IsConstant());
559
560 if (loc1.Equals(loc2)) {
561 return;
562 }
563
564 if (loc1.IsRegister() && loc2.IsRegister()) {
565 // Swap 2 GPRs.
566 Register r1 = loc1.AsRegister<Register>();
567 Register r2 = loc2.AsRegister<Register>();
568 __ Move(TMP, r2);
569 __ Move(r2, r1);
570 __ Move(r1, TMP);
571 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
572 FRegister f1 = loc1.AsFpuRegister<FRegister>();
573 FRegister f2 = loc2.AsFpuRegister<FRegister>();
574 if (type == Primitive::kPrimFloat) {
575 __ MovS(FTMP, f2);
576 __ MovS(f2, f1);
577 __ MovS(f1, FTMP);
578 } else {
579 DCHECK_EQ(type, Primitive::kPrimDouble);
580 __ MovD(FTMP, f2);
581 __ MovD(f2, f1);
582 __ MovD(f1, FTMP);
583 }
584 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
585 (loc1.IsFpuRegister() && loc2.IsRegister())) {
586 // Swap FPR and GPR.
587 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
588 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
589 : loc2.AsFpuRegister<FRegister>();
590 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
591 : loc2.AsRegister<Register>();
592 __ Move(TMP, r2);
593 __ Mfc1(r2, f1);
594 __ Mtc1(TMP, f1);
595 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
596 // Swap 2 GPR register pairs.
597 Register r1 = loc1.AsRegisterPairLow<Register>();
598 Register r2 = loc2.AsRegisterPairLow<Register>();
599 __ Move(TMP, r2);
600 __ Move(r2, r1);
601 __ Move(r1, TMP);
602 r1 = loc1.AsRegisterPairHigh<Register>();
603 r2 = loc2.AsRegisterPairHigh<Register>();
604 __ Move(TMP, r2);
605 __ Move(r2, r1);
606 __ Move(r1, TMP);
607 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
608 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
609 // Swap FPR and GPR register pair.
610 DCHECK_EQ(type, Primitive::kPrimDouble);
611 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
612 : loc2.AsFpuRegister<FRegister>();
613 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
614 : loc2.AsRegisterPairLow<Register>();
615 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
616 : loc2.AsRegisterPairHigh<Register>();
617 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
618 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
619 // unpredictable and the following mfch1 will fail.
620 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800621 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200622 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800623 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200624 __ Move(r2_l, TMP);
625 __ Move(r2_h, AT);
626 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
627 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
628 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
629 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000630 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
631 (loc1.IsStackSlot() && loc2.IsRegister())) {
632 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
633 : loc2.AsRegister<Register>();
634 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
635 : loc2.GetStackIndex();
636 __ Move(TMP, reg);
637 __ LoadFromOffset(kLoadWord, reg, SP, offset);
638 __ StoreToOffset(kStoreWord, TMP, SP, offset);
639 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
640 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
641 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
642 : loc2.AsRegisterPairLow<Register>();
643 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
644 : loc2.AsRegisterPairHigh<Register>();
645 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
646 : loc2.GetStackIndex();
647 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
648 : loc2.GetHighStackIndex(kMipsWordSize);
649 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000650 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000651 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000652 __ Move(TMP, reg_h);
653 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
654 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200655 } else {
656 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
657 }
658}
659
660void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
661 __ Pop(static_cast<Register>(reg));
662}
663
664void ParallelMoveResolverMIPS::SpillScratch(int reg) {
665 __ Push(static_cast<Register>(reg));
666}
667
668void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
669 // Allocate a scratch register other than TMP, if available.
670 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
671 // automatically unspilled when the scratch scope object is destroyed).
672 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
673 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
674 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
675 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
676 __ LoadFromOffset(kLoadWord,
677 Register(ensure_scratch.GetRegister()),
678 SP,
679 index1 + stack_offset);
680 __ LoadFromOffset(kLoadWord,
681 TMP,
682 SP,
683 index2 + stack_offset);
684 __ StoreToOffset(kStoreWord,
685 Register(ensure_scratch.GetRegister()),
686 SP,
687 index2 + stack_offset);
688 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
689 }
690}
691
Alexey Frunze73296a72016-06-03 22:51:46 -0700692void CodeGeneratorMIPS::ComputeSpillMask() {
693 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
694 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
695 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
696 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
697 // registers, include the ZERO register to force alignment of FPU callee-saved registers
698 // within the stack frame.
699 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
700 core_spill_mask_ |= (1 << ZERO);
701 }
Alexey Frunze06a46c42016-07-19 15:00:40 -0700702 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
703 // (this can happen in leaf methods), artificially spill the ZERO register in order to
704 // force explicit saving and restoring of RA. RA isn't saved/restored when it's the only
705 // spilled register.
706 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
707 // saved in an unused temporary register) and saving of RA and the current method pointer
708 // in the frame.
709 if (clobbered_ra_ && core_spill_mask_ == (1u << RA) && fpu_spill_mask_ == 0) {
710 core_spill_mask_ |= (1 << ZERO);
711 }
Alexey Frunze73296a72016-06-03 22:51:46 -0700712}
713
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200714static dwarf::Reg DWARFReg(Register reg) {
715 return dwarf::Reg::MipsCore(static_cast<int>(reg));
716}
717
718// TODO: mapping of floating-point registers to DWARF.
719
720void CodeGeneratorMIPS::GenerateFrameEntry() {
721 __ Bind(&frame_entry_label_);
722
723 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
724
725 if (do_overflow_check) {
726 __ LoadFromOffset(kLoadWord,
727 ZERO,
728 SP,
729 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
730 RecordPcInfo(nullptr, 0);
731 }
732
733 if (HasEmptyFrame()) {
734 return;
735 }
736
737 // Make sure the frame size isn't unreasonably large.
738 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
739 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
740 }
741
742 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200743
Alexey Frunze73296a72016-06-03 22:51:46 -0700744 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200745 __ IncreaseFrameSize(ofs);
746
Alexey Frunze73296a72016-06-03 22:51:46 -0700747 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
748 Register reg = static_cast<Register>(MostSignificantBit(mask));
749 mask ^= 1u << reg;
750 ofs -= kMipsWordSize;
751 // The ZERO register is only included for alignment.
752 if (reg != ZERO) {
753 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200754 __ cfi().RelOffset(DWARFReg(reg), ofs);
755 }
756 }
757
Alexey Frunze73296a72016-06-03 22:51:46 -0700758 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
759 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
760 mask ^= 1u << reg;
761 ofs -= kMipsDoublewordSize;
762 __ StoreDToOffset(reg, SP, ofs);
763 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200764 }
765
Alexey Frunze73296a72016-06-03 22:51:46 -0700766 // Store the current method pointer.
767 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200768}
769
770void CodeGeneratorMIPS::GenerateFrameExit() {
771 __ cfi().RememberState();
772
773 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200774 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200775
Alexey Frunze73296a72016-06-03 22:51:46 -0700776 // For better instruction scheduling restore RA before other registers.
777 uint32_t ofs = GetFrameSize();
778 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
779 Register reg = static_cast<Register>(MostSignificantBit(mask));
780 mask ^= 1u << reg;
781 ofs -= kMipsWordSize;
782 // The ZERO register is only included for alignment.
783 if (reg != ZERO) {
784 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200785 __ cfi().Restore(DWARFReg(reg));
786 }
787 }
788
Alexey Frunze73296a72016-06-03 22:51:46 -0700789 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
790 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
791 mask ^= 1u << reg;
792 ofs -= kMipsDoublewordSize;
793 __ LoadDFromOffset(reg, SP, ofs);
794 // TODO: __ cfi().Restore(DWARFReg(reg));
795 }
796
797 __ DecreaseFrameSize(GetFrameSize());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200798 }
799
800 __ Jr(RA);
801 __ Nop();
802
803 __ cfi().RestoreState();
804 __ cfi().DefCFAOffset(GetFrameSize());
805}
806
807void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
808 __ Bind(GetLabelOf(block));
809}
810
811void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
812 if (src.Equals(dst)) {
813 return;
814 }
815
816 if (src.IsConstant()) {
817 MoveConstant(dst, src.GetConstant());
818 } else {
819 if (Primitive::Is64BitType(dst_type)) {
820 Move64(dst, src);
821 } else {
822 Move32(dst, src);
823 }
824 }
825}
826
827void CodeGeneratorMIPS::Move32(Location destination, Location source) {
828 if (source.Equals(destination)) {
829 return;
830 }
831
832 if (destination.IsRegister()) {
833 if (source.IsRegister()) {
834 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
835 } else if (source.IsFpuRegister()) {
836 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
837 } else {
838 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
839 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
840 }
841 } else if (destination.IsFpuRegister()) {
842 if (source.IsRegister()) {
843 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
844 } else if (source.IsFpuRegister()) {
845 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
846 } else {
847 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
848 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
849 }
850 } else {
851 DCHECK(destination.IsStackSlot()) << destination;
852 if (source.IsRegister()) {
853 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
854 } else if (source.IsFpuRegister()) {
855 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
856 } else {
857 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
858 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
859 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
860 }
861 }
862}
863
864void CodeGeneratorMIPS::Move64(Location destination, Location source) {
865 if (source.Equals(destination)) {
866 return;
867 }
868
869 if (destination.IsRegisterPair()) {
870 if (source.IsRegisterPair()) {
871 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
872 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
873 } else if (source.IsFpuRegister()) {
874 Register dst_high = destination.AsRegisterPairHigh<Register>();
875 Register dst_low = destination.AsRegisterPairLow<Register>();
876 FRegister src = source.AsFpuRegister<FRegister>();
877 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800878 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200879 } else {
880 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
881 int32_t off = source.GetStackIndex();
882 Register r = destination.AsRegisterPairLow<Register>();
883 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
884 }
885 } else if (destination.IsFpuRegister()) {
886 if (source.IsRegisterPair()) {
887 FRegister dst = destination.AsFpuRegister<FRegister>();
888 Register src_high = source.AsRegisterPairHigh<Register>();
889 Register src_low = source.AsRegisterPairLow<Register>();
890 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800891 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200892 } else if (source.IsFpuRegister()) {
893 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
894 } else {
895 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
896 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
897 }
898 } else {
899 DCHECK(destination.IsDoubleStackSlot()) << destination;
900 int32_t off = destination.GetStackIndex();
901 if (source.IsRegisterPair()) {
902 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
903 } else if (source.IsFpuRegister()) {
904 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
905 } else {
906 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
907 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
908 __ StoreToOffset(kStoreWord, TMP, SP, off);
909 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
910 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
911 }
912 }
913}
914
915void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
916 if (c->IsIntConstant() || c->IsNullConstant()) {
917 // Move 32 bit constant.
918 int32_t value = GetInt32ValueOf(c);
919 if (destination.IsRegister()) {
920 Register dst = destination.AsRegister<Register>();
921 __ LoadConst32(dst, value);
922 } else {
923 DCHECK(destination.IsStackSlot())
924 << "Cannot move " << c->DebugName() << " to " << destination;
925 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
926 }
927 } else if (c->IsLongConstant()) {
928 // Move 64 bit constant.
929 int64_t value = GetInt64ValueOf(c);
930 if (destination.IsRegisterPair()) {
931 Register r_h = destination.AsRegisterPairHigh<Register>();
932 Register r_l = destination.AsRegisterPairLow<Register>();
933 __ LoadConst64(r_h, r_l, value);
934 } else {
935 DCHECK(destination.IsDoubleStackSlot())
936 << "Cannot move " << c->DebugName() << " to " << destination;
937 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
938 }
939 } else if (c->IsFloatConstant()) {
940 // Move 32 bit float constant.
941 int32_t value = GetInt32ValueOf(c);
942 if (destination.IsFpuRegister()) {
943 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
944 } else {
945 DCHECK(destination.IsStackSlot())
946 << "Cannot move " << c->DebugName() << " to " << destination;
947 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
948 }
949 } else {
950 // Move 64 bit double constant.
951 DCHECK(c->IsDoubleConstant()) << c->DebugName();
952 int64_t value = GetInt64ValueOf(c);
953 if (destination.IsFpuRegister()) {
954 FRegister fd = destination.AsFpuRegister<FRegister>();
955 __ LoadDConst64(fd, value, TMP);
956 } else {
957 DCHECK(destination.IsDoubleStackSlot())
958 << "Cannot move " << c->DebugName() << " to " << destination;
959 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
960 }
961 }
962}
963
964void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
965 DCHECK(destination.IsRegister());
966 Register dst = destination.AsRegister<Register>();
967 __ LoadConst32(dst, value);
968}
969
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200970void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
971 if (location.IsRegister()) {
972 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700973 } else if (location.IsRegisterPair()) {
974 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
975 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200976 } else {
977 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
978 }
979}
980
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700981void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
982 DCHECK(linker_patches->empty());
983 size_t size =
984 method_patches_.size() +
985 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700986 pc_relative_dex_cache_patches_.size() +
987 pc_relative_string_patches_.size() +
988 pc_relative_type_patches_.size() +
989 boot_image_string_patches_.size() +
990 boot_image_type_patches_.size() +
991 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700992 linker_patches->reserve(size);
993 for (const auto& entry : method_patches_) {
994 const MethodReference& target_method = entry.first;
995 Literal* literal = entry.second;
996 DCHECK(literal->GetLabel()->IsBound());
997 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
998 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
999 target_method.dex_file,
1000 target_method.dex_method_index));
1001 }
1002 for (const auto& entry : call_patches_) {
1003 const MethodReference& target_method = entry.first;
1004 Literal* literal = entry.second;
1005 DCHECK(literal->GetLabel()->IsBound());
1006 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1007 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1008 target_method.dex_file,
1009 target_method.dex_method_index));
1010 }
1011 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
1012 const DexFile& dex_file = info.target_dex_file;
1013 size_t base_element_offset = info.offset_or_index;
1014 DCHECK(info.high_label.IsBound());
1015 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1016 DCHECK(info.pc_rel_label.IsBound());
1017 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1018 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
1019 &dex_file,
1020 pc_rel_offset,
1021 base_element_offset));
1022 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001023 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
1024 const DexFile& dex_file = info.target_dex_file;
1025 size_t string_index = info.offset_or_index;
1026 DCHECK(info.high_label.IsBound());
1027 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1028 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1029 // the assembler's base label used for PC-relative literals.
1030 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1031 ? __ GetLabelLocation(&info.pc_rel_label)
1032 : __ GetPcRelBaseLabelLocation();
1033 linker_patches->push_back(LinkerPatch::RelativeStringPatch(high_offset,
1034 &dex_file,
1035 pc_rel_offset,
1036 string_index));
1037 }
1038 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
1039 const DexFile& dex_file = info.target_dex_file;
1040 size_t type_index = info.offset_or_index;
1041 DCHECK(info.high_label.IsBound());
1042 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1043 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1044 // the assembler's base label used for PC-relative literals.
1045 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1046 ? __ GetLabelLocation(&info.pc_rel_label)
1047 : __ GetPcRelBaseLabelLocation();
1048 linker_patches->push_back(LinkerPatch::RelativeTypePatch(high_offset,
1049 &dex_file,
1050 pc_rel_offset,
1051 type_index));
1052 }
1053 for (const auto& entry : boot_image_string_patches_) {
1054 const StringReference& target_string = entry.first;
1055 Literal* literal = entry.second;
1056 DCHECK(literal->GetLabel()->IsBound());
1057 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1058 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1059 target_string.dex_file,
1060 target_string.string_index));
1061 }
1062 for (const auto& entry : boot_image_type_patches_) {
1063 const TypeReference& target_type = entry.first;
1064 Literal* literal = entry.second;
1065 DCHECK(literal->GetLabel()->IsBound());
1066 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1067 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1068 target_type.dex_file,
1069 target_type.type_index));
1070 }
1071 for (const auto& entry : boot_image_address_patches_) {
1072 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1073 Literal* literal = entry.second;
1074 DCHECK(literal->GetLabel()->IsBound());
1075 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1076 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1077 }
1078}
1079
1080CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1081 const DexFile& dex_file, uint32_t string_index) {
1082 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1083}
1084
1085CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1086 const DexFile& dex_file, uint32_t type_index) {
1087 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001088}
1089
1090CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1091 const DexFile& dex_file, uint32_t element_offset) {
1092 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1093}
1094
1095CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1096 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1097 patches->emplace_back(dex_file, offset_or_index);
1098 return &patches->back();
1099}
1100
Alexey Frunze06a46c42016-07-19 15:00:40 -07001101Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1102 return map->GetOrCreate(
1103 value,
1104 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1105}
1106
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001107Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1108 MethodToLiteralMap* map) {
1109 return map->GetOrCreate(
1110 target_method,
1111 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1112}
1113
1114Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1115 return DeduplicateMethodLiteral(target_method, &method_patches_);
1116}
1117
1118Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1119 return DeduplicateMethodLiteral(target_method, &call_patches_);
1120}
1121
Alexey Frunze06a46c42016-07-19 15:00:40 -07001122Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1123 uint32_t string_index) {
1124 return boot_image_string_patches_.GetOrCreate(
1125 StringReference(&dex_file, string_index),
1126 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1127}
1128
1129Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1130 uint32_t type_index) {
1131 return boot_image_type_patches_.GetOrCreate(
1132 TypeReference(&dex_file, type_index),
1133 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1134}
1135
1136Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1137 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1138 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1139 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1140}
1141
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001142void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1143 MipsLabel done;
1144 Register card = AT;
1145 Register temp = TMP;
1146 __ Beqz(value, &done);
1147 __ LoadFromOffset(kLoadWord,
1148 card,
1149 TR,
1150 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1151 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1152 __ Addu(temp, card, temp);
1153 __ Sb(card, temp, 0);
1154 __ Bind(&done);
1155}
1156
David Brazdil58282f42016-01-14 12:45:10 +00001157void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001158 // Don't allocate the dalvik style register pair passing.
1159 blocked_register_pairs_[A1_A2] = true;
1160
1161 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1162 blocked_core_registers_[ZERO] = true;
1163 blocked_core_registers_[K0] = true;
1164 blocked_core_registers_[K1] = true;
1165 blocked_core_registers_[GP] = true;
1166 blocked_core_registers_[SP] = true;
1167 blocked_core_registers_[RA] = true;
1168
1169 // AT and TMP(T8) are used as temporary/scratch registers
1170 // (similar to how AT is used by MIPS assemblers).
1171 blocked_core_registers_[AT] = true;
1172 blocked_core_registers_[TMP] = true;
1173 blocked_fpu_registers_[FTMP] = true;
1174
1175 // Reserve suspend and thread registers.
1176 blocked_core_registers_[S0] = true;
1177 blocked_core_registers_[TR] = true;
1178
1179 // Reserve T9 for function calls
1180 blocked_core_registers_[T9] = true;
1181
1182 // Reserve odd-numbered FPU registers.
1183 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1184 blocked_fpu_registers_[i] = true;
1185 }
1186
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001187 if (GetGraph()->IsDebuggable()) {
1188 // Stubs do not save callee-save floating point registers. If the graph
1189 // is debuggable, we need to deal with these registers differently. For
1190 // now, just block them.
1191 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1192 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1193 }
1194 }
1195
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001196 UpdateBlockedPairRegisters();
1197}
1198
1199void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1200 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1201 MipsManagedRegister current =
1202 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1203 if (blocked_core_registers_[current.AsRegisterPairLow()]
1204 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1205 blocked_register_pairs_[i] = true;
1206 }
1207 }
1208}
1209
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1211 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1212 return kMipsWordSize;
1213}
1214
1215size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1216 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1217 return kMipsWordSize;
1218}
1219
1220size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1221 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1222 return kMipsDoublewordSize;
1223}
1224
1225size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1226 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1227 return kMipsDoublewordSize;
1228}
1229
1230void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001231 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001232}
1233
1234void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001235 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001236}
1237
1238void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1239 HInstruction* instruction,
1240 uint32_t dex_pc,
1241 SlowPathCode* slow_path) {
1242 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1243 instruction,
1244 dex_pc,
1245 slow_path,
1246 IsDirectEntrypoint(entrypoint));
1247}
1248
1249constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1250
1251void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1252 HInstruction* instruction,
1253 uint32_t dex_pc,
1254 SlowPathCode* slow_path,
1255 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001256 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1257 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001258 if (is_direct_entrypoint) {
1259 // Reserve argument space on stack (for $a0-$a3) for
1260 // entrypoints that directly reference native implementations.
1261 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001262 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001263 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001264 } else {
1265 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001266 }
1267 RecordPcInfo(instruction, dex_pc, slow_path);
1268}
1269
1270void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1271 Register class_reg) {
1272 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1273 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1274 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1275 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1276 __ Sync(0);
1277 __ Bind(slow_path->GetExitLabel());
1278}
1279
1280void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1281 __ Sync(0); // Only stype 0 is supported.
1282}
1283
1284void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1285 HBasicBlock* successor) {
1286 SuspendCheckSlowPathMIPS* slow_path =
1287 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1288 codegen_->AddSlowPath(slow_path);
1289
1290 __ LoadFromOffset(kLoadUnsignedHalfword,
1291 TMP,
1292 TR,
1293 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1294 if (successor == nullptr) {
1295 __ Bnez(TMP, slow_path->GetEntryLabel());
1296 __ Bind(slow_path->GetReturnLabel());
1297 } else {
1298 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1299 __ B(slow_path->GetEntryLabel());
1300 // slow_path will return to GetLabelOf(successor).
1301 }
1302}
1303
1304InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1305 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001306 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001307 assembler_(codegen->GetAssembler()),
1308 codegen_(codegen) {}
1309
1310void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1311 DCHECK_EQ(instruction->InputCount(), 2U);
1312 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1313 Primitive::Type type = instruction->GetResultType();
1314 switch (type) {
1315 case Primitive::kPrimInt: {
1316 locations->SetInAt(0, Location::RequiresRegister());
1317 HInstruction* right = instruction->InputAt(1);
1318 bool can_use_imm = false;
1319 if (right->IsConstant()) {
1320 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1321 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1322 can_use_imm = IsUint<16>(imm);
1323 } else if (instruction->IsAdd()) {
1324 can_use_imm = IsInt<16>(imm);
1325 } else {
1326 DCHECK(instruction->IsSub());
1327 can_use_imm = IsInt<16>(-imm);
1328 }
1329 }
1330 if (can_use_imm)
1331 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1332 else
1333 locations->SetInAt(1, Location::RequiresRegister());
1334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1335 break;
1336 }
1337
1338 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001339 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001340 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1341 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001342 break;
1343 }
1344
1345 case Primitive::kPrimFloat:
1346 case Primitive::kPrimDouble:
1347 DCHECK(instruction->IsAdd() || instruction->IsSub());
1348 locations->SetInAt(0, Location::RequiresFpuRegister());
1349 locations->SetInAt(1, Location::RequiresFpuRegister());
1350 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1351 break;
1352
1353 default:
1354 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1355 }
1356}
1357
1358void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1359 Primitive::Type type = instruction->GetType();
1360 LocationSummary* locations = instruction->GetLocations();
1361
1362 switch (type) {
1363 case Primitive::kPrimInt: {
1364 Register dst = locations->Out().AsRegister<Register>();
1365 Register lhs = locations->InAt(0).AsRegister<Register>();
1366 Location rhs_location = locations->InAt(1);
1367
1368 Register rhs_reg = ZERO;
1369 int32_t rhs_imm = 0;
1370 bool use_imm = rhs_location.IsConstant();
1371 if (use_imm) {
1372 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1373 } else {
1374 rhs_reg = rhs_location.AsRegister<Register>();
1375 }
1376
1377 if (instruction->IsAnd()) {
1378 if (use_imm)
1379 __ Andi(dst, lhs, rhs_imm);
1380 else
1381 __ And(dst, lhs, rhs_reg);
1382 } else if (instruction->IsOr()) {
1383 if (use_imm)
1384 __ Ori(dst, lhs, rhs_imm);
1385 else
1386 __ Or(dst, lhs, rhs_reg);
1387 } else if (instruction->IsXor()) {
1388 if (use_imm)
1389 __ Xori(dst, lhs, rhs_imm);
1390 else
1391 __ Xor(dst, lhs, rhs_reg);
1392 } else if (instruction->IsAdd()) {
1393 if (use_imm)
1394 __ Addiu(dst, lhs, rhs_imm);
1395 else
1396 __ Addu(dst, lhs, rhs_reg);
1397 } else {
1398 DCHECK(instruction->IsSub());
1399 if (use_imm)
1400 __ Addiu(dst, lhs, -rhs_imm);
1401 else
1402 __ Subu(dst, lhs, rhs_reg);
1403 }
1404 break;
1405 }
1406
1407 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001408 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1409 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1410 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1411 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001412 Location rhs_location = locations->InAt(1);
1413 bool use_imm = rhs_location.IsConstant();
1414 if (!use_imm) {
1415 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1416 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1417 if (instruction->IsAnd()) {
1418 __ And(dst_low, lhs_low, rhs_low);
1419 __ And(dst_high, lhs_high, rhs_high);
1420 } else if (instruction->IsOr()) {
1421 __ Or(dst_low, lhs_low, rhs_low);
1422 __ Or(dst_high, lhs_high, rhs_high);
1423 } else if (instruction->IsXor()) {
1424 __ Xor(dst_low, lhs_low, rhs_low);
1425 __ Xor(dst_high, lhs_high, rhs_high);
1426 } else if (instruction->IsAdd()) {
1427 if (lhs_low == rhs_low) {
1428 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1429 __ Slt(TMP, lhs_low, ZERO);
1430 __ Addu(dst_low, lhs_low, rhs_low);
1431 } else {
1432 __ Addu(dst_low, lhs_low, rhs_low);
1433 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1434 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1435 }
1436 __ Addu(dst_high, lhs_high, rhs_high);
1437 __ Addu(dst_high, dst_high, TMP);
1438 } else {
1439 DCHECK(instruction->IsSub());
1440 __ Sltu(TMP, lhs_low, rhs_low);
1441 __ Subu(dst_low, lhs_low, rhs_low);
1442 __ Subu(dst_high, lhs_high, rhs_high);
1443 __ Subu(dst_high, dst_high, TMP);
1444 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001445 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001446 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1447 if (instruction->IsOr()) {
1448 uint32_t low = Low32Bits(value);
1449 uint32_t high = High32Bits(value);
1450 if (IsUint<16>(low)) {
1451 if (dst_low != lhs_low || low != 0) {
1452 __ Ori(dst_low, lhs_low, low);
1453 }
1454 } else {
1455 __ LoadConst32(TMP, low);
1456 __ Or(dst_low, lhs_low, TMP);
1457 }
1458 if (IsUint<16>(high)) {
1459 if (dst_high != lhs_high || high != 0) {
1460 __ Ori(dst_high, lhs_high, high);
1461 }
1462 } else {
1463 if (high != low) {
1464 __ LoadConst32(TMP, high);
1465 }
1466 __ Or(dst_high, lhs_high, TMP);
1467 }
1468 } else if (instruction->IsXor()) {
1469 uint32_t low = Low32Bits(value);
1470 uint32_t high = High32Bits(value);
1471 if (IsUint<16>(low)) {
1472 if (dst_low != lhs_low || low != 0) {
1473 __ Xori(dst_low, lhs_low, low);
1474 }
1475 } else {
1476 __ LoadConst32(TMP, low);
1477 __ Xor(dst_low, lhs_low, TMP);
1478 }
1479 if (IsUint<16>(high)) {
1480 if (dst_high != lhs_high || high != 0) {
1481 __ Xori(dst_high, lhs_high, high);
1482 }
1483 } else {
1484 if (high != low) {
1485 __ LoadConst32(TMP, high);
1486 }
1487 __ Xor(dst_high, lhs_high, TMP);
1488 }
1489 } else if (instruction->IsAnd()) {
1490 uint32_t low = Low32Bits(value);
1491 uint32_t high = High32Bits(value);
1492 if (IsUint<16>(low)) {
1493 __ Andi(dst_low, lhs_low, low);
1494 } else if (low != 0xFFFFFFFF) {
1495 __ LoadConst32(TMP, low);
1496 __ And(dst_low, lhs_low, TMP);
1497 } else if (dst_low != lhs_low) {
1498 __ Move(dst_low, lhs_low);
1499 }
1500 if (IsUint<16>(high)) {
1501 __ Andi(dst_high, lhs_high, high);
1502 } else if (high != 0xFFFFFFFF) {
1503 if (high != low) {
1504 __ LoadConst32(TMP, high);
1505 }
1506 __ And(dst_high, lhs_high, TMP);
1507 } else if (dst_high != lhs_high) {
1508 __ Move(dst_high, lhs_high);
1509 }
1510 } else {
1511 if (instruction->IsSub()) {
1512 value = -value;
1513 } else {
1514 DCHECK(instruction->IsAdd());
1515 }
1516 int32_t low = Low32Bits(value);
1517 int32_t high = High32Bits(value);
1518 if (IsInt<16>(low)) {
1519 if (dst_low != lhs_low || low != 0) {
1520 __ Addiu(dst_low, lhs_low, low);
1521 }
1522 if (low != 0) {
1523 __ Sltiu(AT, dst_low, low);
1524 }
1525 } else {
1526 __ LoadConst32(TMP, low);
1527 __ Addu(dst_low, lhs_low, TMP);
1528 __ Sltu(AT, dst_low, TMP);
1529 }
1530 if (IsInt<16>(high)) {
1531 if (dst_high != lhs_high || high != 0) {
1532 __ Addiu(dst_high, lhs_high, high);
1533 }
1534 } else {
1535 if (high != low) {
1536 __ LoadConst32(TMP, high);
1537 }
1538 __ Addu(dst_high, lhs_high, TMP);
1539 }
1540 if (low != 0) {
1541 __ Addu(dst_high, dst_high, AT);
1542 }
1543 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001544 }
1545 break;
1546 }
1547
1548 case Primitive::kPrimFloat:
1549 case Primitive::kPrimDouble: {
1550 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1551 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1552 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1553 if (instruction->IsAdd()) {
1554 if (type == Primitive::kPrimFloat) {
1555 __ AddS(dst, lhs, rhs);
1556 } else {
1557 __ AddD(dst, lhs, rhs);
1558 }
1559 } else {
1560 DCHECK(instruction->IsSub());
1561 if (type == Primitive::kPrimFloat) {
1562 __ SubS(dst, lhs, rhs);
1563 } else {
1564 __ SubD(dst, lhs, rhs);
1565 }
1566 }
1567 break;
1568 }
1569
1570 default:
1571 LOG(FATAL) << "Unexpected binary operation type " << type;
1572 }
1573}
1574
1575void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001576 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001577
1578 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1579 Primitive::Type type = instr->GetResultType();
1580 switch (type) {
1581 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001582 locations->SetInAt(0, Location::RequiresRegister());
1583 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1584 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1585 break;
1586 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001587 locations->SetInAt(0, Location::RequiresRegister());
1588 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1589 locations->SetOut(Location::RequiresRegister());
1590 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001591 default:
1592 LOG(FATAL) << "Unexpected shift type " << type;
1593 }
1594}
1595
1596static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1597
1598void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001599 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001600 LocationSummary* locations = instr->GetLocations();
1601 Primitive::Type type = instr->GetType();
1602
1603 Location rhs_location = locations->InAt(1);
1604 bool use_imm = rhs_location.IsConstant();
1605 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1606 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001607 const uint32_t shift_mask =
1608 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001609 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001610 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1611 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001612
1613 switch (type) {
1614 case Primitive::kPrimInt: {
1615 Register dst = locations->Out().AsRegister<Register>();
1616 Register lhs = locations->InAt(0).AsRegister<Register>();
1617 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001618 if (shift_value == 0) {
1619 if (dst != lhs) {
1620 __ Move(dst, lhs);
1621 }
1622 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001623 __ Sll(dst, lhs, shift_value);
1624 } else if (instr->IsShr()) {
1625 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001626 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001627 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001628 } else {
1629 if (has_ins_rotr) {
1630 __ Rotr(dst, lhs, shift_value);
1631 } else {
1632 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1633 __ Srl(dst, lhs, shift_value);
1634 __ Or(dst, dst, TMP);
1635 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001636 }
1637 } else {
1638 if (instr->IsShl()) {
1639 __ Sllv(dst, lhs, rhs_reg);
1640 } else if (instr->IsShr()) {
1641 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001642 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001643 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001644 } else {
1645 if (has_ins_rotr) {
1646 __ Rotrv(dst, lhs, rhs_reg);
1647 } else {
1648 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001649 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1650 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1651 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1652 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1653 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001654 __ Sllv(TMP, lhs, TMP);
1655 __ Srlv(dst, lhs, rhs_reg);
1656 __ Or(dst, dst, TMP);
1657 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001658 }
1659 }
1660 break;
1661 }
1662
1663 case Primitive::kPrimLong: {
1664 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1665 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1666 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1667 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1668 if (use_imm) {
1669 if (shift_value == 0) {
1670 codegen_->Move64(locations->Out(), locations->InAt(0));
1671 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001672 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001673 if (instr->IsShl()) {
1674 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1675 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1676 __ Sll(dst_low, lhs_low, shift_value);
1677 } else if (instr->IsShr()) {
1678 __ Srl(dst_low, lhs_low, shift_value);
1679 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1680 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001681 } else if (instr->IsUShr()) {
1682 __ Srl(dst_low, lhs_low, shift_value);
1683 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1684 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001685 } else {
1686 __ Srl(dst_low, lhs_low, shift_value);
1687 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1688 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001689 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001690 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001691 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001692 if (instr->IsShl()) {
1693 __ Sll(dst_low, lhs_low, shift_value);
1694 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1695 __ Sll(dst_high, lhs_high, shift_value);
1696 __ Or(dst_high, dst_high, TMP);
1697 } else if (instr->IsShr()) {
1698 __ Sra(dst_high, lhs_high, shift_value);
1699 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1700 __ Srl(dst_low, lhs_low, shift_value);
1701 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001702 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001703 __ Srl(dst_high, lhs_high, shift_value);
1704 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1705 __ Srl(dst_low, lhs_low, shift_value);
1706 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001707 } else {
1708 __ Srl(TMP, lhs_low, shift_value);
1709 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1710 __ Or(dst_low, dst_low, TMP);
1711 __ Srl(TMP, lhs_high, shift_value);
1712 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1713 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001714 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001715 }
1716 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001717 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001719 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001720 __ Move(dst_low, ZERO);
1721 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001722 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001723 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001724 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001725 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001726 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001727 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001728 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001729 // 64-bit rotation by 32 is just a swap.
1730 __ Move(dst_low, lhs_high);
1731 __ Move(dst_high, lhs_low);
1732 } else {
1733 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001734 __ Srl(dst_low, lhs_high, shift_value_high);
1735 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1736 __ Srl(dst_high, lhs_low, shift_value_high);
1737 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001738 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001739 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1740 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001741 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001742 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1743 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001744 __ Or(dst_high, dst_high, TMP);
1745 }
1746 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001747 }
1748 }
1749 } else {
1750 MipsLabel done;
1751 if (instr->IsShl()) {
1752 __ Sllv(dst_low, lhs_low, rhs_reg);
1753 __ Nor(AT, ZERO, rhs_reg);
1754 __ Srl(TMP, lhs_low, 1);
1755 __ Srlv(TMP, TMP, AT);
1756 __ Sllv(dst_high, lhs_high, rhs_reg);
1757 __ Or(dst_high, dst_high, TMP);
1758 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1759 __ Beqz(TMP, &done);
1760 __ Move(dst_high, dst_low);
1761 __ Move(dst_low, ZERO);
1762 } else if (instr->IsShr()) {
1763 __ Srav(dst_high, lhs_high, rhs_reg);
1764 __ Nor(AT, ZERO, rhs_reg);
1765 __ Sll(TMP, lhs_high, 1);
1766 __ Sllv(TMP, TMP, AT);
1767 __ Srlv(dst_low, lhs_low, rhs_reg);
1768 __ Or(dst_low, dst_low, TMP);
1769 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1770 __ Beqz(TMP, &done);
1771 __ Move(dst_low, dst_high);
1772 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001773 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001774 __ Srlv(dst_high, lhs_high, rhs_reg);
1775 __ Nor(AT, ZERO, rhs_reg);
1776 __ Sll(TMP, lhs_high, 1);
1777 __ Sllv(TMP, TMP, AT);
1778 __ Srlv(dst_low, lhs_low, rhs_reg);
1779 __ Or(dst_low, dst_low, TMP);
1780 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1781 __ Beqz(TMP, &done);
1782 __ Move(dst_low, dst_high);
1783 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001784 } else {
1785 __ Nor(AT, ZERO, rhs_reg);
1786 __ Srlv(TMP, lhs_low, rhs_reg);
1787 __ Sll(dst_low, lhs_high, 1);
1788 __ Sllv(dst_low, dst_low, AT);
1789 __ Or(dst_low, dst_low, TMP);
1790 __ Srlv(TMP, lhs_high, rhs_reg);
1791 __ Sll(dst_high, lhs_low, 1);
1792 __ Sllv(dst_high, dst_high, AT);
1793 __ Or(dst_high, dst_high, TMP);
1794 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1795 __ Beqz(TMP, &done);
1796 __ Move(TMP, dst_high);
1797 __ Move(dst_high, dst_low);
1798 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001799 }
1800 __ Bind(&done);
1801 }
1802 break;
1803 }
1804
1805 default:
1806 LOG(FATAL) << "Unexpected shift operation type " << type;
1807 }
1808}
1809
1810void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1811 HandleBinaryOp(instruction);
1812}
1813
1814void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1815 HandleBinaryOp(instruction);
1816}
1817
1818void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1819 HandleBinaryOp(instruction);
1820}
1821
1822void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1823 HandleBinaryOp(instruction);
1824}
1825
1826void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1827 LocationSummary* locations =
1828 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1829 locations->SetInAt(0, Location::RequiresRegister());
1830 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1831 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1832 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1833 } else {
1834 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1835 }
1836}
1837
1838void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1839 LocationSummary* locations = instruction->GetLocations();
1840 Register obj = locations->InAt(0).AsRegister<Register>();
1841 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001842 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001843
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001844 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001845 switch (type) {
1846 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001847 Register out = locations->Out().AsRegister<Register>();
1848 if (index.IsConstant()) {
1849 size_t offset =
1850 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1851 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1852 } else {
1853 __ Addu(TMP, obj, index.AsRegister<Register>());
1854 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1855 }
1856 break;
1857 }
1858
1859 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001860 Register out = locations->Out().AsRegister<Register>();
1861 if (index.IsConstant()) {
1862 size_t offset =
1863 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1864 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1865 } else {
1866 __ Addu(TMP, obj, index.AsRegister<Register>());
1867 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1868 }
1869 break;
1870 }
1871
1872 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001873 Register out = locations->Out().AsRegister<Register>();
1874 if (index.IsConstant()) {
1875 size_t offset =
1876 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1877 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1878 } else {
1879 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1880 __ Addu(TMP, obj, TMP);
1881 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1882 }
1883 break;
1884 }
1885
1886 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887 Register out = locations->Out().AsRegister<Register>();
1888 if (index.IsConstant()) {
1889 size_t offset =
1890 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1891 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1892 } else {
1893 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1894 __ Addu(TMP, obj, TMP);
1895 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1896 }
1897 break;
1898 }
1899
1900 case Primitive::kPrimInt:
1901 case Primitive::kPrimNot: {
1902 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001903 Register out = locations->Out().AsRegister<Register>();
1904 if (index.IsConstant()) {
1905 size_t offset =
1906 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1907 __ LoadFromOffset(kLoadWord, out, obj, offset);
1908 } else {
1909 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1910 __ Addu(TMP, obj, TMP);
1911 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1912 }
1913 break;
1914 }
1915
1916 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001917 Register out = locations->Out().AsRegisterPairLow<Register>();
1918 if (index.IsConstant()) {
1919 size_t offset =
1920 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1921 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1922 } else {
1923 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1924 __ Addu(TMP, obj, TMP);
1925 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1926 }
1927 break;
1928 }
1929
1930 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001931 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1932 if (index.IsConstant()) {
1933 size_t offset =
1934 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1935 __ LoadSFromOffset(out, obj, offset);
1936 } else {
1937 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1938 __ Addu(TMP, obj, TMP);
1939 __ LoadSFromOffset(out, TMP, data_offset);
1940 }
1941 break;
1942 }
1943
1944 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001945 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1946 if (index.IsConstant()) {
1947 size_t offset =
1948 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1949 __ LoadDFromOffset(out, obj, offset);
1950 } else {
1951 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1952 __ Addu(TMP, obj, TMP);
1953 __ LoadDFromOffset(out, TMP, data_offset);
1954 }
1955 break;
1956 }
1957
1958 case Primitive::kPrimVoid:
1959 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1960 UNREACHABLE();
1961 }
1962 codegen_->MaybeRecordImplicitNullCheck(instruction);
1963}
1964
1965void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1966 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1967 locations->SetInAt(0, Location::RequiresRegister());
1968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1969}
1970
1971void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1972 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001973 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001974 Register obj = locations->InAt(0).AsRegister<Register>();
1975 Register out = locations->Out().AsRegister<Register>();
1976 __ LoadFromOffset(kLoadWord, out, obj, offset);
1977 codegen_->MaybeRecordImplicitNullCheck(instruction);
1978}
1979
1980void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001981 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001982 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1983 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001984 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001985 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001986 InvokeRuntimeCallingConvention calling_convention;
1987 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1988 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1989 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1990 } else {
1991 locations->SetInAt(0, Location::RequiresRegister());
1992 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1993 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1994 locations->SetInAt(2, Location::RequiresFpuRegister());
1995 } else {
1996 locations->SetInAt(2, Location::RequiresRegister());
1997 }
1998 }
1999}
2000
2001void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2002 LocationSummary* locations = instruction->GetLocations();
2003 Register obj = locations->InAt(0).AsRegister<Register>();
2004 Location index = locations->InAt(1);
2005 Primitive::Type value_type = instruction->GetComponentType();
2006 bool needs_runtime_call = locations->WillCall();
2007 bool needs_write_barrier =
2008 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2009
2010 switch (value_type) {
2011 case Primitive::kPrimBoolean:
2012 case Primitive::kPrimByte: {
2013 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
2014 Register value = locations->InAt(2).AsRegister<Register>();
2015 if (index.IsConstant()) {
2016 size_t offset =
2017 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
2018 __ StoreToOffset(kStoreByte, value, obj, offset);
2019 } else {
2020 __ Addu(TMP, obj, index.AsRegister<Register>());
2021 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
2022 }
2023 break;
2024 }
2025
2026 case Primitive::kPrimShort:
2027 case Primitive::kPrimChar: {
2028 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
2029 Register value = locations->InAt(2).AsRegister<Register>();
2030 if (index.IsConstant()) {
2031 size_t offset =
2032 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2033 __ StoreToOffset(kStoreHalfword, value, obj, offset);
2034 } else {
2035 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
2036 __ Addu(TMP, obj, TMP);
2037 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
2038 }
2039 break;
2040 }
2041
2042 case Primitive::kPrimInt:
2043 case Primitive::kPrimNot: {
2044 if (!needs_runtime_call) {
2045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2046 Register value = locations->InAt(2).AsRegister<Register>();
2047 if (index.IsConstant()) {
2048 size_t offset =
2049 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2050 __ StoreToOffset(kStoreWord, value, obj, offset);
2051 } else {
2052 DCHECK(index.IsRegister()) << index;
2053 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2054 __ Addu(TMP, obj, TMP);
2055 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
2056 }
2057 codegen_->MaybeRecordImplicitNullCheck(instruction);
2058 if (needs_write_barrier) {
2059 DCHECK_EQ(value_type, Primitive::kPrimNot);
2060 codegen_->MarkGCCard(obj, value);
2061 }
2062 } else {
2063 DCHECK_EQ(value_type, Primitive::kPrimNot);
2064 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
2065 instruction,
2066 instruction->GetDexPc(),
2067 nullptr,
2068 IsDirectEntrypoint(kQuickAputObject));
2069 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2070 }
2071 break;
2072 }
2073
2074 case Primitive::kPrimLong: {
2075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2076 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2077 if (index.IsConstant()) {
2078 size_t offset =
2079 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2080 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
2081 } else {
2082 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2083 __ Addu(TMP, obj, TMP);
2084 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
2085 }
2086 break;
2087 }
2088
2089 case Primitive::kPrimFloat: {
2090 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2091 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2092 DCHECK(locations->InAt(2).IsFpuRegister());
2093 if (index.IsConstant()) {
2094 size_t offset =
2095 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2096 __ StoreSToOffset(value, obj, offset);
2097 } else {
2098 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2099 __ Addu(TMP, obj, TMP);
2100 __ StoreSToOffset(value, TMP, data_offset);
2101 }
2102 break;
2103 }
2104
2105 case Primitive::kPrimDouble: {
2106 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2107 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2108 DCHECK(locations->InAt(2).IsFpuRegister());
2109 if (index.IsConstant()) {
2110 size_t offset =
2111 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2112 __ StoreDToOffset(value, obj, offset);
2113 } else {
2114 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2115 __ Addu(TMP, obj, TMP);
2116 __ StoreDToOffset(value, TMP, data_offset);
2117 }
2118 break;
2119 }
2120
2121 case Primitive::kPrimVoid:
2122 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2123 UNREACHABLE();
2124 }
2125
2126 // Ints and objects are handled in the switch.
2127 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
2128 codegen_->MaybeRecordImplicitNullCheck(instruction);
2129 }
2130}
2131
2132void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2133 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2134 ? LocationSummary::kCallOnSlowPath
2135 : LocationSummary::kNoCall;
2136 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2137 locations->SetInAt(0, Location::RequiresRegister());
2138 locations->SetInAt(1, Location::RequiresRegister());
2139 if (instruction->HasUses()) {
2140 locations->SetOut(Location::SameAsFirstInput());
2141 }
2142}
2143
2144void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2145 LocationSummary* locations = instruction->GetLocations();
2146 BoundsCheckSlowPathMIPS* slow_path =
2147 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2148 codegen_->AddSlowPath(slow_path);
2149
2150 Register index = locations->InAt(0).AsRegister<Register>();
2151 Register length = locations->InAt(1).AsRegister<Register>();
2152
2153 // length is limited by the maximum positive signed 32-bit integer.
2154 // Unsigned comparison of length and index checks for index < 0
2155 // and for length <= index simultaneously.
2156 __ Bgeu(index, length, slow_path->GetEntryLabel());
2157}
2158
2159void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2160 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2161 instruction,
2162 LocationSummary::kCallOnSlowPath);
2163 locations->SetInAt(0, Location::RequiresRegister());
2164 locations->SetInAt(1, Location::RequiresRegister());
2165 // Note that TypeCheckSlowPathMIPS uses this register too.
2166 locations->AddTemp(Location::RequiresRegister());
2167}
2168
2169void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2170 LocationSummary* locations = instruction->GetLocations();
2171 Register obj = locations->InAt(0).AsRegister<Register>();
2172 Register cls = locations->InAt(1).AsRegister<Register>();
2173 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2174
2175 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2176 codegen_->AddSlowPath(slow_path);
2177
2178 // TODO: avoid this check if we know obj is not null.
2179 __ Beqz(obj, slow_path->GetExitLabel());
2180 // Compare the class of `obj` with `cls`.
2181 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2182 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2183 __ Bind(slow_path->GetExitLabel());
2184}
2185
2186void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2187 LocationSummary* locations =
2188 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2189 locations->SetInAt(0, Location::RequiresRegister());
2190 if (check->HasUses()) {
2191 locations->SetOut(Location::SameAsFirstInput());
2192 }
2193}
2194
2195void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2196 // We assume the class is not null.
2197 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2198 check->GetLoadClass(),
2199 check,
2200 check->GetDexPc(),
2201 true);
2202 codegen_->AddSlowPath(slow_path);
2203 GenerateClassInitializationCheck(slow_path,
2204 check->GetLocations()->InAt(0).AsRegister<Register>());
2205}
2206
2207void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2208 Primitive::Type in_type = compare->InputAt(0)->GetType();
2209
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002210 LocationSummary* locations =
2211 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002212
2213 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002214 case Primitive::kPrimBoolean:
2215 case Primitive::kPrimByte:
2216 case Primitive::kPrimShort:
2217 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002218 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002219 case Primitive::kPrimLong:
2220 locations->SetInAt(0, Location::RequiresRegister());
2221 locations->SetInAt(1, Location::RequiresRegister());
2222 // Output overlaps because it is written before doing the low comparison.
2223 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2224 break;
2225
2226 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002227 case Primitive::kPrimDouble:
2228 locations->SetInAt(0, Location::RequiresFpuRegister());
2229 locations->SetInAt(1, Location::RequiresFpuRegister());
2230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002231 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002232
2233 default:
2234 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2235 }
2236}
2237
2238void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2239 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002240 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002241 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002242 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002243
2244 // 0 if: left == right
2245 // 1 if: left > right
2246 // -1 if: left < right
2247 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002248 case Primitive::kPrimBoolean:
2249 case Primitive::kPrimByte:
2250 case Primitive::kPrimShort:
2251 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002252 case Primitive::kPrimInt: {
2253 Register lhs = locations->InAt(0).AsRegister<Register>();
2254 Register rhs = locations->InAt(1).AsRegister<Register>();
2255 __ Slt(TMP, lhs, rhs);
2256 __ Slt(res, rhs, lhs);
2257 __ Subu(res, res, TMP);
2258 break;
2259 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 case Primitive::kPrimLong: {
2261 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2263 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2264 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2265 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2266 // TODO: more efficient (direct) comparison with a constant.
2267 __ Slt(TMP, lhs_high, rhs_high);
2268 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2269 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2270 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2271 __ Sltu(TMP, lhs_low, rhs_low);
2272 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2273 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2274 __ Bind(&done);
2275 break;
2276 }
2277
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002278 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002279 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002280 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2281 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2282 MipsLabel done;
2283 if (isR6) {
2284 __ CmpEqS(FTMP, lhs, rhs);
2285 __ LoadConst32(res, 0);
2286 __ Bc1nez(FTMP, &done);
2287 if (gt_bias) {
2288 __ CmpLtS(FTMP, lhs, rhs);
2289 __ LoadConst32(res, -1);
2290 __ Bc1nez(FTMP, &done);
2291 __ LoadConst32(res, 1);
2292 } else {
2293 __ CmpLtS(FTMP, rhs, lhs);
2294 __ LoadConst32(res, 1);
2295 __ Bc1nez(FTMP, &done);
2296 __ LoadConst32(res, -1);
2297 }
2298 } else {
2299 if (gt_bias) {
2300 __ ColtS(0, lhs, rhs);
2301 __ LoadConst32(res, -1);
2302 __ Bc1t(0, &done);
2303 __ CeqS(0, lhs, rhs);
2304 __ LoadConst32(res, 1);
2305 __ Movt(res, ZERO, 0);
2306 } else {
2307 __ ColtS(0, rhs, lhs);
2308 __ LoadConst32(res, 1);
2309 __ Bc1t(0, &done);
2310 __ CeqS(0, lhs, rhs);
2311 __ LoadConst32(res, -1);
2312 __ Movt(res, ZERO, 0);
2313 }
2314 }
2315 __ Bind(&done);
2316 break;
2317 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002318 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002319 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002320 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2321 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2322 MipsLabel done;
2323 if (isR6) {
2324 __ CmpEqD(FTMP, lhs, rhs);
2325 __ LoadConst32(res, 0);
2326 __ Bc1nez(FTMP, &done);
2327 if (gt_bias) {
2328 __ CmpLtD(FTMP, lhs, rhs);
2329 __ LoadConst32(res, -1);
2330 __ Bc1nez(FTMP, &done);
2331 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002332 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002333 __ CmpLtD(FTMP, rhs, lhs);
2334 __ LoadConst32(res, 1);
2335 __ Bc1nez(FTMP, &done);
2336 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 }
2338 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002339 if (gt_bias) {
2340 __ ColtD(0, lhs, rhs);
2341 __ LoadConst32(res, -1);
2342 __ Bc1t(0, &done);
2343 __ CeqD(0, lhs, rhs);
2344 __ LoadConst32(res, 1);
2345 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002346 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002347 __ ColtD(0, rhs, lhs);
2348 __ LoadConst32(res, 1);
2349 __ Bc1t(0, &done);
2350 __ CeqD(0, lhs, rhs);
2351 __ LoadConst32(res, -1);
2352 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002353 }
2354 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002355 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 break;
2357 }
2358
2359 default:
2360 LOG(FATAL) << "Unimplemented compare type " << in_type;
2361 }
2362}
2363
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002364void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002366 switch (instruction->InputAt(0)->GetType()) {
2367 default:
2368 case Primitive::kPrimLong:
2369 locations->SetInAt(0, Location::RequiresRegister());
2370 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2371 break;
2372
2373 case Primitive::kPrimFloat:
2374 case Primitive::kPrimDouble:
2375 locations->SetInAt(0, Location::RequiresFpuRegister());
2376 locations->SetInAt(1, Location::RequiresFpuRegister());
2377 break;
2378 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002379 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2381 }
2382}
2383
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002384void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002385 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002386 return;
2387 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002388
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002389 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002390 LocationSummary* locations = instruction->GetLocations();
2391 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002392 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002393
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002394 switch (type) {
2395 default:
2396 // Integer case.
2397 GenerateIntCompare(instruction->GetCondition(), locations);
2398 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002399
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002400 case Primitive::kPrimLong:
2401 // TODO: don't use branches.
2402 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002403 break;
2404
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002405 case Primitive::kPrimFloat:
2406 case Primitive::kPrimDouble:
2407 // TODO: don't use branches.
2408 GenerateFpCompareAndBranch(instruction->GetCondition(),
2409 instruction->IsGtBias(),
2410 type,
2411 locations,
2412 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002413 break;
2414 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002415
2416 // Convert the branches into the result.
2417 MipsLabel done;
2418
2419 // False case: result = 0.
2420 __ LoadConst32(dst, 0);
2421 __ B(&done);
2422
2423 // True case: result = 1.
2424 __ Bind(&true_label);
2425 __ LoadConst32(dst, 1);
2426 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427}
2428
Alexey Frunze7e99e052015-11-24 19:28:01 -08002429void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2430 DCHECK(instruction->IsDiv() || instruction->IsRem());
2431 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2432
2433 LocationSummary* locations = instruction->GetLocations();
2434 Location second = locations->InAt(1);
2435 DCHECK(second.IsConstant());
2436
2437 Register out = locations->Out().AsRegister<Register>();
2438 Register dividend = locations->InAt(0).AsRegister<Register>();
2439 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2440 DCHECK(imm == 1 || imm == -1);
2441
2442 if (instruction->IsRem()) {
2443 __ Move(out, ZERO);
2444 } else {
2445 if (imm == -1) {
2446 __ Subu(out, ZERO, dividend);
2447 } else if (out != dividend) {
2448 __ Move(out, dividend);
2449 }
2450 }
2451}
2452
2453void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2454 DCHECK(instruction->IsDiv() || instruction->IsRem());
2455 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2456
2457 LocationSummary* locations = instruction->GetLocations();
2458 Location second = locations->InAt(1);
2459 DCHECK(second.IsConstant());
2460
2461 Register out = locations->Out().AsRegister<Register>();
2462 Register dividend = locations->InAt(0).AsRegister<Register>();
2463 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002464 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002465 int ctz_imm = CTZ(abs_imm);
2466
2467 if (instruction->IsDiv()) {
2468 if (ctz_imm == 1) {
2469 // Fast path for division by +/-2, which is very common.
2470 __ Srl(TMP, dividend, 31);
2471 } else {
2472 __ Sra(TMP, dividend, 31);
2473 __ Srl(TMP, TMP, 32 - ctz_imm);
2474 }
2475 __ Addu(out, dividend, TMP);
2476 __ Sra(out, out, ctz_imm);
2477 if (imm < 0) {
2478 __ Subu(out, ZERO, out);
2479 }
2480 } else {
2481 if (ctz_imm == 1) {
2482 // Fast path for modulo +/-2, which is very common.
2483 __ Sra(TMP, dividend, 31);
2484 __ Subu(out, dividend, TMP);
2485 __ Andi(out, out, 1);
2486 __ Addu(out, out, TMP);
2487 } else {
2488 __ Sra(TMP, dividend, 31);
2489 __ Srl(TMP, TMP, 32 - ctz_imm);
2490 __ Addu(out, dividend, TMP);
2491 if (IsUint<16>(abs_imm - 1)) {
2492 __ Andi(out, out, abs_imm - 1);
2493 } else {
2494 __ Sll(out, out, 32 - ctz_imm);
2495 __ Srl(out, out, 32 - ctz_imm);
2496 }
2497 __ Subu(out, out, TMP);
2498 }
2499 }
2500}
2501
2502void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2503 DCHECK(instruction->IsDiv() || instruction->IsRem());
2504 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2505
2506 LocationSummary* locations = instruction->GetLocations();
2507 Location second = locations->InAt(1);
2508 DCHECK(second.IsConstant());
2509
2510 Register out = locations->Out().AsRegister<Register>();
2511 Register dividend = locations->InAt(0).AsRegister<Register>();
2512 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2513
2514 int64_t magic;
2515 int shift;
2516 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2517
2518 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2519
2520 __ LoadConst32(TMP, magic);
2521 if (isR6) {
2522 __ MuhR6(TMP, dividend, TMP);
2523 } else {
2524 __ MultR2(dividend, TMP);
2525 __ Mfhi(TMP);
2526 }
2527 if (imm > 0 && magic < 0) {
2528 __ Addu(TMP, TMP, dividend);
2529 } else if (imm < 0 && magic > 0) {
2530 __ Subu(TMP, TMP, dividend);
2531 }
2532
2533 if (shift != 0) {
2534 __ Sra(TMP, TMP, shift);
2535 }
2536
2537 if (instruction->IsDiv()) {
2538 __ Sra(out, TMP, 31);
2539 __ Subu(out, TMP, out);
2540 } else {
2541 __ Sra(AT, TMP, 31);
2542 __ Subu(AT, TMP, AT);
2543 __ LoadConst32(TMP, imm);
2544 if (isR6) {
2545 __ MulR6(TMP, AT, TMP);
2546 } else {
2547 __ MulR2(TMP, AT, TMP);
2548 }
2549 __ Subu(out, dividend, TMP);
2550 }
2551}
2552
2553void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2554 DCHECK(instruction->IsDiv() || instruction->IsRem());
2555 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2556
2557 LocationSummary* locations = instruction->GetLocations();
2558 Register out = locations->Out().AsRegister<Register>();
2559 Location second = locations->InAt(1);
2560
2561 if (second.IsConstant()) {
2562 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2563 if (imm == 0) {
2564 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2565 } else if (imm == 1 || imm == -1) {
2566 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002567 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002568 DivRemByPowerOfTwo(instruction);
2569 } else {
2570 DCHECK(imm <= -2 || imm >= 2);
2571 GenerateDivRemWithAnyConstant(instruction);
2572 }
2573 } else {
2574 Register dividend = locations->InAt(0).AsRegister<Register>();
2575 Register divisor = second.AsRegister<Register>();
2576 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2577 if (instruction->IsDiv()) {
2578 if (isR6) {
2579 __ DivR6(out, dividend, divisor);
2580 } else {
2581 __ DivR2(out, dividend, divisor);
2582 }
2583 } else {
2584 if (isR6) {
2585 __ ModR6(out, dividend, divisor);
2586 } else {
2587 __ ModR2(out, dividend, divisor);
2588 }
2589 }
2590 }
2591}
2592
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002593void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2594 Primitive::Type type = div->GetResultType();
2595 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002596 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002597 : LocationSummary::kNoCall;
2598
2599 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2600
2601 switch (type) {
2602 case Primitive::kPrimInt:
2603 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002604 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2606 break;
2607
2608 case Primitive::kPrimLong: {
2609 InvokeRuntimeCallingConvention calling_convention;
2610 locations->SetInAt(0, Location::RegisterPairLocation(
2611 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2612 locations->SetInAt(1, Location::RegisterPairLocation(
2613 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2614 locations->SetOut(calling_convention.GetReturnLocation(type));
2615 break;
2616 }
2617
2618 case Primitive::kPrimFloat:
2619 case Primitive::kPrimDouble:
2620 locations->SetInAt(0, Location::RequiresFpuRegister());
2621 locations->SetInAt(1, Location::RequiresFpuRegister());
2622 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2623 break;
2624
2625 default:
2626 LOG(FATAL) << "Unexpected div type " << type;
2627 }
2628}
2629
2630void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2631 Primitive::Type type = instruction->GetType();
2632 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633
2634 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002635 case Primitive::kPrimInt:
2636 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002637 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002638 case Primitive::kPrimLong: {
2639 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2640 instruction,
2641 instruction->GetDexPc(),
2642 nullptr,
2643 IsDirectEntrypoint(kQuickLdiv));
2644 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2645 break;
2646 }
2647 case Primitive::kPrimFloat:
2648 case Primitive::kPrimDouble: {
2649 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2650 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2651 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2652 if (type == Primitive::kPrimFloat) {
2653 __ DivS(dst, lhs, rhs);
2654 } else {
2655 __ DivD(dst, lhs, rhs);
2656 }
2657 break;
2658 }
2659 default:
2660 LOG(FATAL) << "Unexpected div type " << type;
2661 }
2662}
2663
2664void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2665 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2666 ? LocationSummary::kCallOnSlowPath
2667 : LocationSummary::kNoCall;
2668 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2669 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2670 if (instruction->HasUses()) {
2671 locations->SetOut(Location::SameAsFirstInput());
2672 }
2673}
2674
2675void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2676 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2677 codegen_->AddSlowPath(slow_path);
2678 Location value = instruction->GetLocations()->InAt(0);
2679 Primitive::Type type = instruction->GetType();
2680
2681 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002682 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002683 case Primitive::kPrimByte:
2684 case Primitive::kPrimChar:
2685 case Primitive::kPrimShort:
2686 case Primitive::kPrimInt: {
2687 if (value.IsConstant()) {
2688 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2689 __ B(slow_path->GetEntryLabel());
2690 } else {
2691 // A division by a non-null constant is valid. We don't need to perform
2692 // any check, so simply fall through.
2693 }
2694 } else {
2695 DCHECK(value.IsRegister()) << value;
2696 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2697 }
2698 break;
2699 }
2700 case Primitive::kPrimLong: {
2701 if (value.IsConstant()) {
2702 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2703 __ B(slow_path->GetEntryLabel());
2704 } else {
2705 // A division by a non-null constant is valid. We don't need to perform
2706 // any check, so simply fall through.
2707 }
2708 } else {
2709 DCHECK(value.IsRegisterPair()) << value;
2710 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2711 __ Beqz(TMP, slow_path->GetEntryLabel());
2712 }
2713 break;
2714 }
2715 default:
2716 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2717 }
2718}
2719
2720void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2723 locations->SetOut(Location::ConstantLocation(constant));
2724}
2725
2726void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2727 // Will be generated at use site.
2728}
2729
2730void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2731 exit->SetLocations(nullptr);
2732}
2733
2734void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2735}
2736
2737void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2738 LocationSummary* locations =
2739 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2740 locations->SetOut(Location::ConstantLocation(constant));
2741}
2742
2743void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2744 // Will be generated at use site.
2745}
2746
2747void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2748 got->SetLocations(nullptr);
2749}
2750
2751void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2752 DCHECK(!successor->IsExitBlock());
2753 HBasicBlock* block = got->GetBlock();
2754 HInstruction* previous = got->GetPrevious();
2755 HLoopInformation* info = block->GetLoopInformation();
2756
2757 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2758 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2759 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2760 return;
2761 }
2762 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2763 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2764 }
2765 if (!codegen_->GoesToNextBlock(block, successor)) {
2766 __ B(codegen_->GetLabelOf(successor));
2767 }
2768}
2769
2770void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2771 HandleGoto(got, got->GetSuccessor());
2772}
2773
2774void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2775 try_boundary->SetLocations(nullptr);
2776}
2777
2778void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2779 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2780 if (!successor->IsExitBlock()) {
2781 HandleGoto(try_boundary, successor);
2782 }
2783}
2784
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002785void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2786 LocationSummary* locations) {
2787 Register dst = locations->Out().AsRegister<Register>();
2788 Register lhs = locations->InAt(0).AsRegister<Register>();
2789 Location rhs_location = locations->InAt(1);
2790 Register rhs_reg = ZERO;
2791 int64_t rhs_imm = 0;
2792 bool use_imm = rhs_location.IsConstant();
2793 if (use_imm) {
2794 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2795 } else {
2796 rhs_reg = rhs_location.AsRegister<Register>();
2797 }
2798
2799 switch (cond) {
2800 case kCondEQ:
2801 case kCondNE:
2802 if (use_imm && IsUint<16>(rhs_imm)) {
2803 __ Xori(dst, lhs, rhs_imm);
2804 } else {
2805 if (use_imm) {
2806 rhs_reg = TMP;
2807 __ LoadConst32(rhs_reg, rhs_imm);
2808 }
2809 __ Xor(dst, lhs, rhs_reg);
2810 }
2811 if (cond == kCondEQ) {
2812 __ Sltiu(dst, dst, 1);
2813 } else {
2814 __ Sltu(dst, ZERO, dst);
2815 }
2816 break;
2817
2818 case kCondLT:
2819 case kCondGE:
2820 if (use_imm && IsInt<16>(rhs_imm)) {
2821 __ Slti(dst, lhs, rhs_imm);
2822 } else {
2823 if (use_imm) {
2824 rhs_reg = TMP;
2825 __ LoadConst32(rhs_reg, rhs_imm);
2826 }
2827 __ Slt(dst, lhs, rhs_reg);
2828 }
2829 if (cond == kCondGE) {
2830 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2831 // only the slt instruction but no sge.
2832 __ Xori(dst, dst, 1);
2833 }
2834 break;
2835
2836 case kCondLE:
2837 case kCondGT:
2838 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2839 // Simulate lhs <= rhs via lhs < rhs + 1.
2840 __ Slti(dst, lhs, rhs_imm + 1);
2841 if (cond == kCondGT) {
2842 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2843 // only the slti instruction but no sgti.
2844 __ Xori(dst, dst, 1);
2845 }
2846 } else {
2847 if (use_imm) {
2848 rhs_reg = TMP;
2849 __ LoadConst32(rhs_reg, rhs_imm);
2850 }
2851 __ Slt(dst, rhs_reg, lhs);
2852 if (cond == kCondLE) {
2853 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2854 // only the slt instruction but no sle.
2855 __ Xori(dst, dst, 1);
2856 }
2857 }
2858 break;
2859
2860 case kCondB:
2861 case kCondAE:
2862 if (use_imm && IsInt<16>(rhs_imm)) {
2863 // Sltiu sign-extends its 16-bit immediate operand before
2864 // the comparison and thus lets us compare directly with
2865 // unsigned values in the ranges [0, 0x7fff] and
2866 // [0xffff8000, 0xffffffff].
2867 __ Sltiu(dst, lhs, rhs_imm);
2868 } else {
2869 if (use_imm) {
2870 rhs_reg = TMP;
2871 __ LoadConst32(rhs_reg, rhs_imm);
2872 }
2873 __ Sltu(dst, lhs, rhs_reg);
2874 }
2875 if (cond == kCondAE) {
2876 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2877 // only the sltu instruction but no sgeu.
2878 __ Xori(dst, dst, 1);
2879 }
2880 break;
2881
2882 case kCondBE:
2883 case kCondA:
2884 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2885 // Simulate lhs <= rhs via lhs < rhs + 1.
2886 // Note that this only works if rhs + 1 does not overflow
2887 // to 0, hence the check above.
2888 // Sltiu sign-extends its 16-bit immediate operand before
2889 // the comparison and thus lets us compare directly with
2890 // unsigned values in the ranges [0, 0x7fff] and
2891 // [0xffff8000, 0xffffffff].
2892 __ Sltiu(dst, lhs, rhs_imm + 1);
2893 if (cond == kCondA) {
2894 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2895 // only the sltiu instruction but no sgtiu.
2896 __ Xori(dst, dst, 1);
2897 }
2898 } else {
2899 if (use_imm) {
2900 rhs_reg = TMP;
2901 __ LoadConst32(rhs_reg, rhs_imm);
2902 }
2903 __ Sltu(dst, rhs_reg, lhs);
2904 if (cond == kCondBE) {
2905 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2906 // only the sltu instruction but no sleu.
2907 __ Xori(dst, dst, 1);
2908 }
2909 }
2910 break;
2911 }
2912}
2913
2914void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2915 LocationSummary* locations,
2916 MipsLabel* label) {
2917 Register lhs = locations->InAt(0).AsRegister<Register>();
2918 Location rhs_location = locations->InAt(1);
2919 Register rhs_reg = ZERO;
2920 int32_t rhs_imm = 0;
2921 bool use_imm = rhs_location.IsConstant();
2922 if (use_imm) {
2923 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2924 } else {
2925 rhs_reg = rhs_location.AsRegister<Register>();
2926 }
2927
2928 if (use_imm && rhs_imm == 0) {
2929 switch (cond) {
2930 case kCondEQ:
2931 case kCondBE: // <= 0 if zero
2932 __ Beqz(lhs, label);
2933 break;
2934 case kCondNE:
2935 case kCondA: // > 0 if non-zero
2936 __ Bnez(lhs, label);
2937 break;
2938 case kCondLT:
2939 __ Bltz(lhs, label);
2940 break;
2941 case kCondGE:
2942 __ Bgez(lhs, label);
2943 break;
2944 case kCondLE:
2945 __ Blez(lhs, label);
2946 break;
2947 case kCondGT:
2948 __ Bgtz(lhs, label);
2949 break;
2950 case kCondB: // always false
2951 break;
2952 case kCondAE: // always true
2953 __ B(label);
2954 break;
2955 }
2956 } else {
2957 if (use_imm) {
2958 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2959 rhs_reg = TMP;
2960 __ LoadConst32(rhs_reg, rhs_imm);
2961 }
2962 switch (cond) {
2963 case kCondEQ:
2964 __ Beq(lhs, rhs_reg, label);
2965 break;
2966 case kCondNE:
2967 __ Bne(lhs, rhs_reg, label);
2968 break;
2969 case kCondLT:
2970 __ Blt(lhs, rhs_reg, label);
2971 break;
2972 case kCondGE:
2973 __ Bge(lhs, rhs_reg, label);
2974 break;
2975 case kCondLE:
2976 __ Bge(rhs_reg, lhs, label);
2977 break;
2978 case kCondGT:
2979 __ Blt(rhs_reg, lhs, label);
2980 break;
2981 case kCondB:
2982 __ Bltu(lhs, rhs_reg, label);
2983 break;
2984 case kCondAE:
2985 __ Bgeu(lhs, rhs_reg, label);
2986 break;
2987 case kCondBE:
2988 __ Bgeu(rhs_reg, lhs, label);
2989 break;
2990 case kCondA:
2991 __ Bltu(rhs_reg, lhs, label);
2992 break;
2993 }
2994 }
2995}
2996
2997void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2998 LocationSummary* locations,
2999 MipsLabel* label) {
3000 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3001 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3002 Location rhs_location = locations->InAt(1);
3003 Register rhs_high = ZERO;
3004 Register rhs_low = ZERO;
3005 int64_t imm = 0;
3006 uint32_t imm_high = 0;
3007 uint32_t imm_low = 0;
3008 bool use_imm = rhs_location.IsConstant();
3009 if (use_imm) {
3010 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3011 imm_high = High32Bits(imm);
3012 imm_low = Low32Bits(imm);
3013 } else {
3014 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3015 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3016 }
3017
3018 if (use_imm && imm == 0) {
3019 switch (cond) {
3020 case kCondEQ:
3021 case kCondBE: // <= 0 if zero
3022 __ Or(TMP, lhs_high, lhs_low);
3023 __ Beqz(TMP, label);
3024 break;
3025 case kCondNE:
3026 case kCondA: // > 0 if non-zero
3027 __ Or(TMP, lhs_high, lhs_low);
3028 __ Bnez(TMP, label);
3029 break;
3030 case kCondLT:
3031 __ Bltz(lhs_high, label);
3032 break;
3033 case kCondGE:
3034 __ Bgez(lhs_high, label);
3035 break;
3036 case kCondLE:
3037 __ Or(TMP, lhs_high, lhs_low);
3038 __ Sra(AT, lhs_high, 31);
3039 __ Bgeu(AT, TMP, label);
3040 break;
3041 case kCondGT:
3042 __ Or(TMP, lhs_high, lhs_low);
3043 __ Sra(AT, lhs_high, 31);
3044 __ Bltu(AT, TMP, label);
3045 break;
3046 case kCondB: // always false
3047 break;
3048 case kCondAE: // always true
3049 __ B(label);
3050 break;
3051 }
3052 } else if (use_imm) {
3053 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3054 switch (cond) {
3055 case kCondEQ:
3056 __ LoadConst32(TMP, imm_high);
3057 __ Xor(TMP, TMP, lhs_high);
3058 __ LoadConst32(AT, imm_low);
3059 __ Xor(AT, AT, lhs_low);
3060 __ Or(TMP, TMP, AT);
3061 __ Beqz(TMP, label);
3062 break;
3063 case kCondNE:
3064 __ LoadConst32(TMP, imm_high);
3065 __ Xor(TMP, TMP, lhs_high);
3066 __ LoadConst32(AT, imm_low);
3067 __ Xor(AT, AT, lhs_low);
3068 __ Or(TMP, TMP, AT);
3069 __ Bnez(TMP, label);
3070 break;
3071 case kCondLT:
3072 __ LoadConst32(TMP, imm_high);
3073 __ Blt(lhs_high, TMP, label);
3074 __ Slt(TMP, TMP, lhs_high);
3075 __ LoadConst32(AT, imm_low);
3076 __ Sltu(AT, lhs_low, AT);
3077 __ Blt(TMP, AT, label);
3078 break;
3079 case kCondGE:
3080 __ LoadConst32(TMP, imm_high);
3081 __ Blt(TMP, lhs_high, label);
3082 __ Slt(TMP, lhs_high, TMP);
3083 __ LoadConst32(AT, imm_low);
3084 __ Sltu(AT, lhs_low, AT);
3085 __ Or(TMP, TMP, AT);
3086 __ Beqz(TMP, label);
3087 break;
3088 case kCondLE:
3089 __ LoadConst32(TMP, imm_high);
3090 __ Blt(lhs_high, TMP, label);
3091 __ Slt(TMP, TMP, lhs_high);
3092 __ LoadConst32(AT, imm_low);
3093 __ Sltu(AT, AT, lhs_low);
3094 __ Or(TMP, TMP, AT);
3095 __ Beqz(TMP, label);
3096 break;
3097 case kCondGT:
3098 __ LoadConst32(TMP, imm_high);
3099 __ Blt(TMP, lhs_high, label);
3100 __ Slt(TMP, lhs_high, TMP);
3101 __ LoadConst32(AT, imm_low);
3102 __ Sltu(AT, AT, lhs_low);
3103 __ Blt(TMP, AT, label);
3104 break;
3105 case kCondB:
3106 __ LoadConst32(TMP, imm_high);
3107 __ Bltu(lhs_high, TMP, label);
3108 __ Sltu(TMP, TMP, lhs_high);
3109 __ LoadConst32(AT, imm_low);
3110 __ Sltu(AT, lhs_low, AT);
3111 __ Blt(TMP, AT, label);
3112 break;
3113 case kCondAE:
3114 __ LoadConst32(TMP, imm_high);
3115 __ Bltu(TMP, lhs_high, label);
3116 __ Sltu(TMP, lhs_high, TMP);
3117 __ LoadConst32(AT, imm_low);
3118 __ Sltu(AT, lhs_low, AT);
3119 __ Or(TMP, TMP, AT);
3120 __ Beqz(TMP, label);
3121 break;
3122 case kCondBE:
3123 __ LoadConst32(TMP, imm_high);
3124 __ Bltu(lhs_high, TMP, label);
3125 __ Sltu(TMP, TMP, lhs_high);
3126 __ LoadConst32(AT, imm_low);
3127 __ Sltu(AT, AT, lhs_low);
3128 __ Or(TMP, TMP, AT);
3129 __ Beqz(TMP, label);
3130 break;
3131 case kCondA:
3132 __ LoadConst32(TMP, imm_high);
3133 __ Bltu(TMP, lhs_high, label);
3134 __ Sltu(TMP, lhs_high, TMP);
3135 __ LoadConst32(AT, imm_low);
3136 __ Sltu(AT, AT, lhs_low);
3137 __ Blt(TMP, AT, label);
3138 break;
3139 }
3140 } else {
3141 switch (cond) {
3142 case kCondEQ:
3143 __ Xor(TMP, lhs_high, rhs_high);
3144 __ Xor(AT, lhs_low, rhs_low);
3145 __ Or(TMP, TMP, AT);
3146 __ Beqz(TMP, label);
3147 break;
3148 case kCondNE:
3149 __ Xor(TMP, lhs_high, rhs_high);
3150 __ Xor(AT, lhs_low, rhs_low);
3151 __ Or(TMP, TMP, AT);
3152 __ Bnez(TMP, label);
3153 break;
3154 case kCondLT:
3155 __ Blt(lhs_high, rhs_high, label);
3156 __ Slt(TMP, rhs_high, lhs_high);
3157 __ Sltu(AT, lhs_low, rhs_low);
3158 __ Blt(TMP, AT, label);
3159 break;
3160 case kCondGE:
3161 __ Blt(rhs_high, lhs_high, label);
3162 __ Slt(TMP, lhs_high, rhs_high);
3163 __ Sltu(AT, lhs_low, rhs_low);
3164 __ Or(TMP, TMP, AT);
3165 __ Beqz(TMP, label);
3166 break;
3167 case kCondLE:
3168 __ Blt(lhs_high, rhs_high, label);
3169 __ Slt(TMP, rhs_high, lhs_high);
3170 __ Sltu(AT, rhs_low, lhs_low);
3171 __ Or(TMP, TMP, AT);
3172 __ Beqz(TMP, label);
3173 break;
3174 case kCondGT:
3175 __ Blt(rhs_high, lhs_high, label);
3176 __ Slt(TMP, lhs_high, rhs_high);
3177 __ Sltu(AT, rhs_low, lhs_low);
3178 __ Blt(TMP, AT, label);
3179 break;
3180 case kCondB:
3181 __ Bltu(lhs_high, rhs_high, label);
3182 __ Sltu(TMP, rhs_high, lhs_high);
3183 __ Sltu(AT, lhs_low, rhs_low);
3184 __ Blt(TMP, AT, label);
3185 break;
3186 case kCondAE:
3187 __ Bltu(rhs_high, lhs_high, label);
3188 __ Sltu(TMP, lhs_high, rhs_high);
3189 __ Sltu(AT, lhs_low, rhs_low);
3190 __ Or(TMP, TMP, AT);
3191 __ Beqz(TMP, label);
3192 break;
3193 case kCondBE:
3194 __ Bltu(lhs_high, rhs_high, label);
3195 __ Sltu(TMP, rhs_high, lhs_high);
3196 __ Sltu(AT, rhs_low, lhs_low);
3197 __ Or(TMP, TMP, AT);
3198 __ Beqz(TMP, label);
3199 break;
3200 case kCondA:
3201 __ Bltu(rhs_high, lhs_high, label);
3202 __ Sltu(TMP, lhs_high, rhs_high);
3203 __ Sltu(AT, rhs_low, lhs_low);
3204 __ Blt(TMP, AT, label);
3205 break;
3206 }
3207 }
3208}
3209
3210void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3211 bool gt_bias,
3212 Primitive::Type type,
3213 LocationSummary* locations,
3214 MipsLabel* label) {
3215 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3216 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3217 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3218 if (type == Primitive::kPrimFloat) {
3219 if (isR6) {
3220 switch (cond) {
3221 case kCondEQ:
3222 __ CmpEqS(FTMP, lhs, rhs);
3223 __ Bc1nez(FTMP, label);
3224 break;
3225 case kCondNE:
3226 __ CmpEqS(FTMP, lhs, rhs);
3227 __ Bc1eqz(FTMP, label);
3228 break;
3229 case kCondLT:
3230 if (gt_bias) {
3231 __ CmpLtS(FTMP, lhs, rhs);
3232 } else {
3233 __ CmpUltS(FTMP, lhs, rhs);
3234 }
3235 __ Bc1nez(FTMP, label);
3236 break;
3237 case kCondLE:
3238 if (gt_bias) {
3239 __ CmpLeS(FTMP, lhs, rhs);
3240 } else {
3241 __ CmpUleS(FTMP, lhs, rhs);
3242 }
3243 __ Bc1nez(FTMP, label);
3244 break;
3245 case kCondGT:
3246 if (gt_bias) {
3247 __ CmpUltS(FTMP, rhs, lhs);
3248 } else {
3249 __ CmpLtS(FTMP, rhs, lhs);
3250 }
3251 __ Bc1nez(FTMP, label);
3252 break;
3253 case kCondGE:
3254 if (gt_bias) {
3255 __ CmpUleS(FTMP, rhs, lhs);
3256 } else {
3257 __ CmpLeS(FTMP, rhs, lhs);
3258 }
3259 __ Bc1nez(FTMP, label);
3260 break;
3261 default:
3262 LOG(FATAL) << "Unexpected non-floating-point condition";
3263 }
3264 } else {
3265 switch (cond) {
3266 case kCondEQ:
3267 __ CeqS(0, lhs, rhs);
3268 __ Bc1t(0, label);
3269 break;
3270 case kCondNE:
3271 __ CeqS(0, lhs, rhs);
3272 __ Bc1f(0, label);
3273 break;
3274 case kCondLT:
3275 if (gt_bias) {
3276 __ ColtS(0, lhs, rhs);
3277 } else {
3278 __ CultS(0, lhs, rhs);
3279 }
3280 __ Bc1t(0, label);
3281 break;
3282 case kCondLE:
3283 if (gt_bias) {
3284 __ ColeS(0, lhs, rhs);
3285 } else {
3286 __ CuleS(0, lhs, rhs);
3287 }
3288 __ Bc1t(0, label);
3289 break;
3290 case kCondGT:
3291 if (gt_bias) {
3292 __ CultS(0, rhs, lhs);
3293 } else {
3294 __ ColtS(0, rhs, lhs);
3295 }
3296 __ Bc1t(0, label);
3297 break;
3298 case kCondGE:
3299 if (gt_bias) {
3300 __ CuleS(0, rhs, lhs);
3301 } else {
3302 __ ColeS(0, rhs, lhs);
3303 }
3304 __ Bc1t(0, label);
3305 break;
3306 default:
3307 LOG(FATAL) << "Unexpected non-floating-point condition";
3308 }
3309 }
3310 } else {
3311 DCHECK_EQ(type, Primitive::kPrimDouble);
3312 if (isR6) {
3313 switch (cond) {
3314 case kCondEQ:
3315 __ CmpEqD(FTMP, lhs, rhs);
3316 __ Bc1nez(FTMP, label);
3317 break;
3318 case kCondNE:
3319 __ CmpEqD(FTMP, lhs, rhs);
3320 __ Bc1eqz(FTMP, label);
3321 break;
3322 case kCondLT:
3323 if (gt_bias) {
3324 __ CmpLtD(FTMP, lhs, rhs);
3325 } else {
3326 __ CmpUltD(FTMP, lhs, rhs);
3327 }
3328 __ Bc1nez(FTMP, label);
3329 break;
3330 case kCondLE:
3331 if (gt_bias) {
3332 __ CmpLeD(FTMP, lhs, rhs);
3333 } else {
3334 __ CmpUleD(FTMP, lhs, rhs);
3335 }
3336 __ Bc1nez(FTMP, label);
3337 break;
3338 case kCondGT:
3339 if (gt_bias) {
3340 __ CmpUltD(FTMP, rhs, lhs);
3341 } else {
3342 __ CmpLtD(FTMP, rhs, lhs);
3343 }
3344 __ Bc1nez(FTMP, label);
3345 break;
3346 case kCondGE:
3347 if (gt_bias) {
3348 __ CmpUleD(FTMP, rhs, lhs);
3349 } else {
3350 __ CmpLeD(FTMP, rhs, lhs);
3351 }
3352 __ Bc1nez(FTMP, label);
3353 break;
3354 default:
3355 LOG(FATAL) << "Unexpected non-floating-point condition";
3356 }
3357 } else {
3358 switch (cond) {
3359 case kCondEQ:
3360 __ CeqD(0, lhs, rhs);
3361 __ Bc1t(0, label);
3362 break;
3363 case kCondNE:
3364 __ CeqD(0, lhs, rhs);
3365 __ Bc1f(0, label);
3366 break;
3367 case kCondLT:
3368 if (gt_bias) {
3369 __ ColtD(0, lhs, rhs);
3370 } else {
3371 __ CultD(0, lhs, rhs);
3372 }
3373 __ Bc1t(0, label);
3374 break;
3375 case kCondLE:
3376 if (gt_bias) {
3377 __ ColeD(0, lhs, rhs);
3378 } else {
3379 __ CuleD(0, lhs, rhs);
3380 }
3381 __ Bc1t(0, label);
3382 break;
3383 case kCondGT:
3384 if (gt_bias) {
3385 __ CultD(0, rhs, lhs);
3386 } else {
3387 __ ColtD(0, rhs, lhs);
3388 }
3389 __ Bc1t(0, label);
3390 break;
3391 case kCondGE:
3392 if (gt_bias) {
3393 __ CuleD(0, rhs, lhs);
3394 } else {
3395 __ ColeD(0, rhs, lhs);
3396 }
3397 __ Bc1t(0, label);
3398 break;
3399 default:
3400 LOG(FATAL) << "Unexpected non-floating-point condition";
3401 }
3402 }
3403 }
3404}
3405
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003406void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003407 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003408 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003409 MipsLabel* false_target) {
3410 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003411
David Brazdil0debae72015-11-12 18:37:00 +00003412 if (true_target == nullptr && false_target == nullptr) {
3413 // Nothing to do. The code always falls through.
3414 return;
3415 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003416 // Constant condition, statically compared against "true" (integer value 1).
3417 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003418 if (true_target != nullptr) {
3419 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003420 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003421 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003422 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003423 if (false_target != nullptr) {
3424 __ B(false_target);
3425 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003426 }
David Brazdil0debae72015-11-12 18:37:00 +00003427 return;
3428 }
3429
3430 // The following code generates these patterns:
3431 // (1) true_target == nullptr && false_target != nullptr
3432 // - opposite condition true => branch to false_target
3433 // (2) true_target != nullptr && false_target == nullptr
3434 // - condition true => branch to true_target
3435 // (3) true_target != nullptr && false_target != nullptr
3436 // - condition true => branch to true_target
3437 // - branch to false_target
3438 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003439 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003440 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003441 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003442 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003443 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3444 } else {
3445 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3446 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003447 } else {
3448 // The condition instruction has not been materialized, use its inputs as
3449 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003450 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003451 Primitive::Type type = condition->InputAt(0)->GetType();
3452 LocationSummary* locations = cond->GetLocations();
3453 IfCondition if_cond = condition->GetCondition();
3454 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003455
David Brazdil0debae72015-11-12 18:37:00 +00003456 if (true_target == nullptr) {
3457 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003458 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003459 }
3460
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003461 switch (type) {
3462 default:
3463 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3464 break;
3465 case Primitive::kPrimLong:
3466 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3467 break;
3468 case Primitive::kPrimFloat:
3469 case Primitive::kPrimDouble:
3470 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3471 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003472 }
3473 }
David Brazdil0debae72015-11-12 18:37:00 +00003474
3475 // If neither branch falls through (case 3), the conditional branch to `true_target`
3476 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3477 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003478 __ B(false_target);
3479 }
3480}
3481
3482void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003484 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003485 locations->SetInAt(0, Location::RequiresRegister());
3486 }
3487}
3488
3489void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003490 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3491 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3492 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3493 nullptr : codegen_->GetLabelOf(true_successor);
3494 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3495 nullptr : codegen_->GetLabelOf(false_successor);
3496 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003497}
3498
3499void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3500 LocationSummary* locations = new (GetGraph()->GetArena())
3501 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003502 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003503 locations->SetInAt(0, Location::RequiresRegister());
3504 }
3505}
3506
3507void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003508 SlowPathCodeMIPS* slow_path =
3509 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003510 GenerateTestAndBranch(deoptimize,
3511 /* condition_input_index */ 0,
3512 slow_path->GetEntryLabel(),
3513 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003514}
3515
David Brazdil74eb1b22015-12-14 11:44:01 +00003516void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3518 if (Primitive::IsFloatingPointType(select->GetType())) {
3519 locations->SetInAt(0, Location::RequiresFpuRegister());
3520 locations->SetInAt(1, Location::RequiresFpuRegister());
3521 } else {
3522 locations->SetInAt(0, Location::RequiresRegister());
3523 locations->SetInAt(1, Location::RequiresRegister());
3524 }
3525 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3526 locations->SetInAt(2, Location::RequiresRegister());
3527 }
3528 locations->SetOut(Location::SameAsFirstInput());
3529}
3530
3531void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3532 LocationSummary* locations = select->GetLocations();
3533 MipsLabel false_target;
3534 GenerateTestAndBranch(select,
3535 /* condition_input_index */ 2,
3536 /* true_target */ nullptr,
3537 &false_target);
3538 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3539 __ Bind(&false_target);
3540}
3541
David Srbecky0cf44932015-12-09 14:09:59 +00003542void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3543 new (GetGraph()->GetArena()) LocationSummary(info);
3544}
3545
David Srbeckyd28f4a02016-03-14 17:14:24 +00003546void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3547 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003548}
3549
3550void CodeGeneratorMIPS::GenerateNop() {
3551 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003552}
3553
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003554void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3555 Primitive::Type field_type = field_info.GetFieldType();
3556 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3557 bool generate_volatile = field_info.IsVolatile() && is_wide;
3558 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003559 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003560
3561 locations->SetInAt(0, Location::RequiresRegister());
3562 if (generate_volatile) {
3563 InvokeRuntimeCallingConvention calling_convention;
3564 // need A0 to hold base + offset
3565 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3566 if (field_type == Primitive::kPrimLong) {
3567 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3568 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003569 // Use Location::Any() to prevent situations when running out of available fp registers.
3570 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003571 // Need some temp core regs since FP results are returned in core registers
3572 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3573 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3574 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3575 }
3576 } else {
3577 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3578 locations->SetOut(Location::RequiresFpuRegister());
3579 } else {
3580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3581 }
3582 }
3583}
3584
3585void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3586 const FieldInfo& field_info,
3587 uint32_t dex_pc) {
3588 Primitive::Type type = field_info.GetFieldType();
3589 LocationSummary* locations = instruction->GetLocations();
3590 Register obj = locations->InAt(0).AsRegister<Register>();
3591 LoadOperandType load_type = kLoadUnsignedByte;
3592 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003593 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003594
3595 switch (type) {
3596 case Primitive::kPrimBoolean:
3597 load_type = kLoadUnsignedByte;
3598 break;
3599 case Primitive::kPrimByte:
3600 load_type = kLoadSignedByte;
3601 break;
3602 case Primitive::kPrimShort:
3603 load_type = kLoadSignedHalfword;
3604 break;
3605 case Primitive::kPrimChar:
3606 load_type = kLoadUnsignedHalfword;
3607 break;
3608 case Primitive::kPrimInt:
3609 case Primitive::kPrimFloat:
3610 case Primitive::kPrimNot:
3611 load_type = kLoadWord;
3612 break;
3613 case Primitive::kPrimLong:
3614 case Primitive::kPrimDouble:
3615 load_type = kLoadDoubleword;
3616 break;
3617 case Primitive::kPrimVoid:
3618 LOG(FATAL) << "Unreachable type " << type;
3619 UNREACHABLE();
3620 }
3621
3622 if (is_volatile && load_type == kLoadDoubleword) {
3623 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003624 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003625 // Do implicit Null check
3626 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3627 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3628 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3629 instruction,
3630 dex_pc,
3631 nullptr,
3632 IsDirectEntrypoint(kQuickA64Load));
3633 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3634 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003635 // FP results are returned in core registers. Need to move them.
3636 Location out = locations->Out();
3637 if (out.IsFpuRegister()) {
3638 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3639 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3640 out.AsFpuRegister<FRegister>());
3641 } else {
3642 DCHECK(out.IsDoubleStackSlot());
3643 __ StoreToOffset(kStoreWord,
3644 locations->GetTemp(1).AsRegister<Register>(),
3645 SP,
3646 out.GetStackIndex());
3647 __ StoreToOffset(kStoreWord,
3648 locations->GetTemp(2).AsRegister<Register>(),
3649 SP,
3650 out.GetStackIndex() + 4);
3651 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003652 }
3653 } else {
3654 if (!Primitive::IsFloatingPointType(type)) {
3655 Register dst;
3656 if (type == Primitive::kPrimLong) {
3657 DCHECK(locations->Out().IsRegisterPair());
3658 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003659 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3660 if (obj == dst) {
3661 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3662 codegen_->MaybeRecordImplicitNullCheck(instruction);
3663 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3664 } else {
3665 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3666 codegen_->MaybeRecordImplicitNullCheck(instruction);
3667 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3668 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003669 } else {
3670 DCHECK(locations->Out().IsRegister());
3671 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003672 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003673 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003674 } else {
3675 DCHECK(locations->Out().IsFpuRegister());
3676 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3677 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003678 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003679 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003680 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003681 }
3682 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003683 // Longs are handled earlier.
3684 if (type != Primitive::kPrimLong) {
3685 codegen_->MaybeRecordImplicitNullCheck(instruction);
3686 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003687 }
3688
3689 if (is_volatile) {
3690 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3691 }
3692}
3693
3694void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3695 Primitive::Type field_type = field_info.GetFieldType();
3696 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3697 bool generate_volatile = field_info.IsVolatile() && is_wide;
3698 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003699 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003700
3701 locations->SetInAt(0, Location::RequiresRegister());
3702 if (generate_volatile) {
3703 InvokeRuntimeCallingConvention calling_convention;
3704 // need A0 to hold base + offset
3705 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3706 if (field_type == Primitive::kPrimLong) {
3707 locations->SetInAt(1, Location::RegisterPairLocation(
3708 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3709 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003710 // Use Location::Any() to prevent situations when running out of available fp registers.
3711 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003712 // Pass FP parameters in core registers.
3713 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3714 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3715 }
3716 } else {
3717 if (Primitive::IsFloatingPointType(field_type)) {
3718 locations->SetInAt(1, Location::RequiresFpuRegister());
3719 } else {
3720 locations->SetInAt(1, Location::RequiresRegister());
3721 }
3722 }
3723}
3724
3725void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3726 const FieldInfo& field_info,
3727 uint32_t dex_pc) {
3728 Primitive::Type type = field_info.GetFieldType();
3729 LocationSummary* locations = instruction->GetLocations();
3730 Register obj = locations->InAt(0).AsRegister<Register>();
3731 StoreOperandType store_type = kStoreByte;
3732 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003733 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003734
3735 switch (type) {
3736 case Primitive::kPrimBoolean:
3737 case Primitive::kPrimByte:
3738 store_type = kStoreByte;
3739 break;
3740 case Primitive::kPrimShort:
3741 case Primitive::kPrimChar:
3742 store_type = kStoreHalfword;
3743 break;
3744 case Primitive::kPrimInt:
3745 case Primitive::kPrimFloat:
3746 case Primitive::kPrimNot:
3747 store_type = kStoreWord;
3748 break;
3749 case Primitive::kPrimLong:
3750 case Primitive::kPrimDouble:
3751 store_type = kStoreDoubleword;
3752 break;
3753 case Primitive::kPrimVoid:
3754 LOG(FATAL) << "Unreachable type " << type;
3755 UNREACHABLE();
3756 }
3757
3758 if (is_volatile) {
3759 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3760 }
3761
3762 if (is_volatile && store_type == kStoreDoubleword) {
3763 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003764 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003765 // Do implicit Null check.
3766 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3767 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3768 if (type == Primitive::kPrimDouble) {
3769 // Pass FP parameters in core registers.
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003770 Location in = locations->InAt(1);
3771 if (in.IsFpuRegister()) {
3772 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(), in.AsFpuRegister<FRegister>());
3773 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3774 in.AsFpuRegister<FRegister>());
3775 } else if (in.IsDoubleStackSlot()) {
3776 __ LoadFromOffset(kLoadWord,
3777 locations->GetTemp(1).AsRegister<Register>(),
3778 SP,
3779 in.GetStackIndex());
3780 __ LoadFromOffset(kLoadWord,
3781 locations->GetTemp(2).AsRegister<Register>(),
3782 SP,
3783 in.GetStackIndex() + 4);
3784 } else {
3785 DCHECK(in.IsConstant());
3786 DCHECK(in.GetConstant()->IsDoubleConstant());
3787 int64_t value = bit_cast<int64_t, double>(in.GetConstant()->AsDoubleConstant()->GetValue());
3788 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
3789 locations->GetTemp(1).AsRegister<Register>(),
3790 value);
3791 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003792 }
3793 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3794 instruction,
3795 dex_pc,
3796 nullptr,
3797 IsDirectEntrypoint(kQuickA64Store));
3798 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3799 } else {
3800 if (!Primitive::IsFloatingPointType(type)) {
3801 Register src;
3802 if (type == Primitive::kPrimLong) {
3803 DCHECK(locations->InAt(1).IsRegisterPair());
3804 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003805 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3806 __ StoreToOffset(kStoreWord, src, obj, offset);
3807 codegen_->MaybeRecordImplicitNullCheck(instruction);
3808 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003809 } else {
3810 DCHECK(locations->InAt(1).IsRegister());
3811 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003812 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003813 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003814 } else {
3815 DCHECK(locations->InAt(1).IsFpuRegister());
3816 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3817 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003818 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003819 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003820 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003821 }
3822 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003823 // Longs are handled earlier.
3824 if (type != Primitive::kPrimLong) {
3825 codegen_->MaybeRecordImplicitNullCheck(instruction);
3826 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003827 }
3828
3829 // TODO: memory barriers?
3830 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3831 DCHECK(locations->InAt(1).IsRegister());
3832 Register src = locations->InAt(1).AsRegister<Register>();
3833 codegen_->MarkGCCard(obj, src);
3834 }
3835
3836 if (is_volatile) {
3837 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3838 }
3839}
3840
3841void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3842 HandleFieldGet(instruction, instruction->GetFieldInfo());
3843}
3844
3845void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3846 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3847}
3848
3849void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3850 HandleFieldSet(instruction, instruction->GetFieldInfo());
3851}
3852
3853void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3854 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3855}
3856
Alexey Frunze06a46c42016-07-19 15:00:40 -07003857void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
3858 HInstruction* instruction ATTRIBUTE_UNUSED,
3859 Location root,
3860 Register obj,
3861 uint32_t offset) {
3862 Register root_reg = root.AsRegister<Register>();
3863 if (kEmitCompilerReadBarrier) {
3864 UNIMPLEMENTED(FATAL) << "for read barrier";
3865 } else {
3866 // Plain GC root load with no read barrier.
3867 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3868 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
3869 // Note that GC roots are not affected by heap poisoning, thus we
3870 // do not have to unpoison `root_reg` here.
3871 }
3872}
3873
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003874void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3875 LocationSummary::CallKind call_kind =
3876 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3877 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3878 locations->SetInAt(0, Location::RequiresRegister());
3879 locations->SetInAt(1, Location::RequiresRegister());
3880 // The output does overlap inputs.
3881 // Note that TypeCheckSlowPathMIPS uses this register too.
3882 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3883}
3884
3885void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3886 LocationSummary* locations = instruction->GetLocations();
3887 Register obj = locations->InAt(0).AsRegister<Register>();
3888 Register cls = locations->InAt(1).AsRegister<Register>();
3889 Register out = locations->Out().AsRegister<Register>();
3890
3891 MipsLabel done;
3892
3893 // Return 0 if `obj` is null.
3894 // TODO: Avoid this check if we know `obj` is not null.
3895 __ Move(out, ZERO);
3896 __ Beqz(obj, &done);
3897
3898 // Compare the class of `obj` with `cls`.
3899 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3900 if (instruction->IsExactCheck()) {
3901 // Classes must be equal for the instanceof to succeed.
3902 __ Xor(out, out, cls);
3903 __ Sltiu(out, out, 1);
3904 } else {
3905 // If the classes are not equal, we go into a slow path.
3906 DCHECK(locations->OnlyCallsOnSlowPath());
3907 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3908 codegen_->AddSlowPath(slow_path);
3909 __ Bne(out, cls, slow_path->GetEntryLabel());
3910 __ LoadConst32(out, 1);
3911 __ Bind(slow_path->GetExitLabel());
3912 }
3913
3914 __ Bind(&done);
3915}
3916
3917void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3919 locations->SetOut(Location::ConstantLocation(constant));
3920}
3921
3922void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3923 // Will be generated at use site.
3924}
3925
3926void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3928 locations->SetOut(Location::ConstantLocation(constant));
3929}
3930
3931void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3932 // Will be generated at use site.
3933}
3934
3935void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3936 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3937 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3938}
3939
3940void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3941 HandleInvoke(invoke);
3942 // The register T0 is required to be used for the hidden argument in
3943 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3944 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3945}
3946
3947void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3948 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3949 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003950 Location receiver = invoke->GetLocations()->InAt(0);
3951 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3952 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3953
3954 // Set the hidden argument.
3955 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3956 invoke->GetDexMethodIndex());
3957
3958 // temp = object->GetClass();
3959 if (receiver.IsStackSlot()) {
3960 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3961 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3962 } else {
3963 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3964 }
3965 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003966 __ LoadFromOffset(kLoadWord, temp, temp,
3967 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3968 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003969 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003970 // temp = temp->GetImtEntryAt(method_offset);
3971 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3972 // T9 = temp->GetEntryPoint();
3973 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3974 // T9();
3975 __ Jalr(T9);
3976 __ Nop();
3977 DCHECK(!codegen_->IsLeafMethod());
3978 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3979}
3980
3981void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003982 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3983 if (intrinsic.TryDispatch(invoke)) {
3984 return;
3985 }
3986
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003987 HandleInvoke(invoke);
3988}
3989
3990void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003991 // Explicit clinit checks triggered by static invokes must have been pruned by
3992 // art::PrepareForRegisterAllocation.
3993 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003994
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003995 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3996 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3997 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3998
3999 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
4000 // R6 has PC-relative addressing.
4001 bool has_extra_input = !isR6 &&
4002 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4003 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4004
4005 if (invoke->HasPcRelativeDexCache()) {
4006 // kDexCachePcRelative is mutually exclusive with
4007 // kDirectAddressWithFixup/kCallDirectWithFixup.
4008 CHECK(!has_extra_input);
4009 has_extra_input = true;
4010 }
4011
Chris Larsen701566a2015-10-27 15:29:13 -07004012 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4013 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004014 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4015 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4016 }
Chris Larsen701566a2015-10-27 15:29:13 -07004017 return;
4018 }
4019
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004020 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004021
4022 // Add the extra input register if either the dex cache array base register
4023 // or the PC-relative base register for accessing literals is needed.
4024 if (has_extra_input) {
4025 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4026 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004027}
4028
Chris Larsen701566a2015-10-27 15:29:13 -07004029static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004030 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004031 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4032 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004033 return true;
4034 }
4035 return false;
4036}
4037
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004038HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004039 HLoadString::LoadKind desired_string_load_kind) {
4040 if (kEmitCompilerReadBarrier) {
4041 UNIMPLEMENTED(FATAL) << "for read barrier";
4042 }
4043 // We disable PC-relative load when there is an irreducible loop, as the optimization
4044 // is incompatible with it.
4045 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4046 bool fallback_load = has_irreducible_loops;
4047 switch (desired_string_load_kind) {
4048 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4049 DCHECK(!GetCompilerOptions().GetCompilePic());
4050 break;
4051 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4052 DCHECK(GetCompilerOptions().GetCompilePic());
4053 break;
4054 case HLoadString::LoadKind::kBootImageAddress:
4055 break;
4056 case HLoadString::LoadKind::kDexCacheAddress:
4057 DCHECK(Runtime::Current()->UseJitCompilation());
4058 fallback_load = false;
4059 break;
4060 case HLoadString::LoadKind::kDexCachePcRelative:
4061 DCHECK(!Runtime::Current()->UseJitCompilation());
4062 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4063 // with irreducible loops.
4064 break;
4065 case HLoadString::LoadKind::kDexCacheViaMethod:
4066 fallback_load = false;
4067 break;
4068 }
4069 if (fallback_load) {
4070 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4071 }
4072 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004073}
4074
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004075HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4076 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004077 if (kEmitCompilerReadBarrier) {
4078 UNIMPLEMENTED(FATAL) << "for read barrier";
4079 }
4080 // We disable pc-relative load when there is an irreducible loop, as the optimization
4081 // is incompatible with it.
4082 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4083 bool fallback_load = has_irreducible_loops;
4084 switch (desired_class_load_kind) {
4085 case HLoadClass::LoadKind::kReferrersClass:
4086 fallback_load = false;
4087 break;
4088 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4089 DCHECK(!GetCompilerOptions().GetCompilePic());
4090 break;
4091 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4092 DCHECK(GetCompilerOptions().GetCompilePic());
4093 break;
4094 case HLoadClass::LoadKind::kBootImageAddress:
4095 break;
4096 case HLoadClass::LoadKind::kDexCacheAddress:
4097 DCHECK(Runtime::Current()->UseJitCompilation());
4098 fallback_load = false;
4099 break;
4100 case HLoadClass::LoadKind::kDexCachePcRelative:
4101 DCHECK(!Runtime::Current()->UseJitCompilation());
4102 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4103 // with irreducible loops.
4104 break;
4105 case HLoadClass::LoadKind::kDexCacheViaMethod:
4106 fallback_load = false;
4107 break;
4108 }
4109 if (fallback_load) {
4110 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4111 }
4112 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004113}
4114
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004115Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4116 Register temp) {
4117 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4118 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4119 if (!invoke->GetLocations()->Intrinsified()) {
4120 return location.AsRegister<Register>();
4121 }
4122 // For intrinsics we allow any location, so it may be on the stack.
4123 if (!location.IsRegister()) {
4124 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4125 return temp;
4126 }
4127 // For register locations, check if the register was saved. If so, get it from the stack.
4128 // Note: There is a chance that the register was saved but not overwritten, so we could
4129 // save one load. However, since this is just an intrinsic slow path we prefer this
4130 // simple and more robust approach rather that trying to determine if that's the case.
4131 SlowPathCode* slow_path = GetCurrentSlowPath();
4132 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4133 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4134 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4135 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4136 return temp;
4137 }
4138 return location.AsRegister<Register>();
4139}
4140
Vladimir Markodc151b22015-10-15 18:02:30 +01004141HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4142 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4143 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004144 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4145 // We disable PC-relative load when there is an irreducible loop, as the optimization
4146 // is incompatible with it.
4147 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4148 bool fallback_load = true;
4149 bool fallback_call = true;
4150 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004151 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4152 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004153 fallback_load = has_irreducible_loops;
4154 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004155 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004156 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004157 break;
4158 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004159 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004160 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004161 fallback_call = has_irreducible_loops;
4162 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004163 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004164 // TODO: Implement this type.
4165 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004166 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004167 fallback_call = false;
4168 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004169 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004170 if (fallback_load) {
4171 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4172 dispatch_info.method_load_data = 0;
4173 }
4174 if (fallback_call) {
4175 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4176 dispatch_info.direct_code_ptr = 0;
4177 }
4178 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004179}
4180
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004181void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4182 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004183 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004184 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4185 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4186 bool isR6 = isa_features_.IsR6();
4187 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4188 // R6 has PC-relative addressing.
4189 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4190 (!isR6 &&
4191 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4192 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4193 Register base_reg = has_extra_input
4194 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4195 : ZERO;
4196
4197 // For better instruction scheduling we load the direct code pointer before the method pointer.
4198 switch (code_ptr_location) {
4199 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4200 // T9 = invoke->GetDirectCodePtr();
4201 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4202 break;
4203 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4204 // T9 = code address from literal pool with link-time patch.
4205 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4206 break;
4207 default:
4208 break;
4209 }
4210
4211 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004212 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4213 // temp = thread->string_init_entrypoint
4214 __ LoadFromOffset(kLoadWord,
4215 temp.AsRegister<Register>(),
4216 TR,
4217 invoke->GetStringInitOffset());
4218 break;
4219 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004220 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004221 break;
4222 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4223 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4224 break;
4225 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004226 __ LoadLiteral(temp.AsRegister<Register>(),
4227 base_reg,
4228 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4229 break;
4230 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4231 HMipsDexCacheArraysBase* base =
4232 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4233 int32_t offset =
4234 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4235 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4236 break;
4237 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004238 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004239 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004240 Register reg = temp.AsRegister<Register>();
4241 Register method_reg;
4242 if (current_method.IsRegister()) {
4243 method_reg = current_method.AsRegister<Register>();
4244 } else {
4245 // TODO: use the appropriate DCHECK() here if possible.
4246 // DCHECK(invoke->GetLocations()->Intrinsified());
4247 DCHECK(!current_method.IsValid());
4248 method_reg = reg;
4249 __ Lw(reg, SP, kCurrentMethodStackOffset);
4250 }
4251
4252 // temp = temp->dex_cache_resolved_methods_;
4253 __ LoadFromOffset(kLoadWord,
4254 reg,
4255 method_reg,
4256 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004257 // temp = temp[index_in_cache];
4258 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4259 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004260 __ LoadFromOffset(kLoadWord,
4261 reg,
4262 reg,
4263 CodeGenerator::GetCachePointerOffset(index_in_cache));
4264 break;
4265 }
4266 }
4267
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004268 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004269 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004270 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004271 break;
4272 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004273 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4274 // T9 prepared above for better instruction scheduling.
4275 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004276 __ Jalr(T9);
4277 __ Nop();
4278 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004279 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004280 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004281 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4282 LOG(FATAL) << "Unsupported";
4283 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004284 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4285 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004286 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004287 T9,
4288 callee_method.AsRegister<Register>(),
4289 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4290 kMipsWordSize).Int32Value());
4291 // T9()
4292 __ Jalr(T9);
4293 __ Nop();
4294 break;
4295 }
4296 DCHECK(!IsLeafMethod());
4297}
4298
4299void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004300 // Explicit clinit checks triggered by static invokes must have been pruned by
4301 // art::PrepareForRegisterAllocation.
4302 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004303
4304 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4305 return;
4306 }
4307
4308 LocationSummary* locations = invoke->GetLocations();
4309 codegen_->GenerateStaticOrDirectCall(invoke,
4310 locations->HasTemps()
4311 ? locations->GetTemp(0)
4312 : Location::NoLocation());
4313 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4314}
4315
Chris Larsen3acee732015-11-18 13:31:08 -08004316void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004317 LocationSummary* locations = invoke->GetLocations();
4318 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004319 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004320 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4321 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4322 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4323 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4324
4325 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004326 DCHECK(receiver.IsRegister());
4327 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4328 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004329 // temp = temp->GetMethodAt(method_offset);
4330 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4331 // T9 = temp->GetEntryPoint();
4332 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4333 // T9();
4334 __ Jalr(T9);
4335 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08004336}
4337
4338void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4339 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4340 return;
4341 }
4342
4343 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004344 DCHECK(!codegen_->IsLeafMethod());
4345 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4346}
4347
4348void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004349 if (cls->NeedsAccessCheck()) {
4350 InvokeRuntimeCallingConvention calling_convention;
4351 CodeGenerator::CreateLoadClassLocationSummary(
4352 cls,
4353 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4354 Location::RegisterLocation(V0),
4355 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4356 return;
4357 }
4358
4359 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4360 ? LocationSummary::kCallOnSlowPath
4361 : LocationSummary::kNoCall;
4362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4363 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4364 switch (load_kind) {
4365 // We need an extra register for PC-relative literals on R2.
4366 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4367 case HLoadClass::LoadKind::kBootImageAddress:
4368 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4369 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4370 break;
4371 }
4372 FALLTHROUGH_INTENDED;
4373 // We need an extra register for PC-relative dex cache accesses.
4374 case HLoadClass::LoadKind::kDexCachePcRelative:
4375 case HLoadClass::LoadKind::kReferrersClass:
4376 case HLoadClass::LoadKind::kDexCacheViaMethod:
4377 locations->SetInAt(0, Location::RequiresRegister());
4378 break;
4379 default:
4380 break;
4381 }
4382 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004383}
4384
4385void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4386 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004387 if (cls->NeedsAccessCheck()) {
4388 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4389 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4390 cls,
4391 cls->GetDexPc(),
4392 nullptr,
4393 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004394 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004395 return;
4396 }
4397
Alexey Frunze06a46c42016-07-19 15:00:40 -07004398 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4399 Location out_loc = locations->Out();
4400 Register out = out_loc.AsRegister<Register>();
4401 Register base_or_current_method_reg;
4402 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4403 switch (load_kind) {
4404 // We need an extra register for PC-relative literals on R2.
4405 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4406 case HLoadClass::LoadKind::kBootImageAddress:
4407 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4408 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4409 break;
4410 // We need an extra register for PC-relative dex cache accesses.
4411 case HLoadClass::LoadKind::kDexCachePcRelative:
4412 case HLoadClass::LoadKind::kReferrersClass:
4413 case HLoadClass::LoadKind::kDexCacheViaMethod:
4414 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4415 break;
4416 default:
4417 base_or_current_method_reg = ZERO;
4418 break;
4419 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004420
Alexey Frunze06a46c42016-07-19 15:00:40 -07004421 bool generate_null_check = false;
4422 switch (load_kind) {
4423 case HLoadClass::LoadKind::kReferrersClass: {
4424 DCHECK(!cls->CanCallRuntime());
4425 DCHECK(!cls->MustGenerateClinitCheck());
4426 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4427 GenerateGcRootFieldLoad(cls,
4428 out_loc,
4429 base_or_current_method_reg,
4430 ArtMethod::DeclaringClassOffset().Int32Value());
4431 break;
4432 }
4433 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4434 DCHECK(!kEmitCompilerReadBarrier);
4435 __ LoadLiteral(out,
4436 base_or_current_method_reg,
4437 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4438 cls->GetTypeIndex()));
4439 break;
4440 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4441 DCHECK(!kEmitCompilerReadBarrier);
4442 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4443 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
4444 if (isR6) {
4445 __ Bind(&info->high_label);
4446 __ Bind(&info->pc_rel_label);
4447 // Add a 32-bit offset to PC.
4448 __ Auipc(out, /* placeholder */ 0x1234);
4449 __ Addiu(out, out, /* placeholder */ 0x5678);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004450 } else {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004451 __ Bind(&info->high_label);
4452 __ Lui(out, /* placeholder */ 0x1234);
4453 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4454 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4455 __ Ori(out, out, /* placeholder */ 0x5678);
4456 // Add a 32-bit offset to PC.
4457 __ Addu(out, out, base_or_current_method_reg);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004458 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004459 break;
4460 }
4461 case HLoadClass::LoadKind::kBootImageAddress: {
4462 DCHECK(!kEmitCompilerReadBarrier);
4463 DCHECK_NE(cls->GetAddress(), 0u);
4464 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4465 __ LoadLiteral(out,
4466 base_or_current_method_reg,
4467 codegen_->DeduplicateBootImageAddressLiteral(address));
4468 break;
4469 }
4470 case HLoadClass::LoadKind::kDexCacheAddress: {
4471 DCHECK_NE(cls->GetAddress(), 0u);
4472 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4473 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4474 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4475 int16_t offset = Low16Bits(address);
4476 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4477 __ Lui(out, High16Bits(base_address));
4478 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4479 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4480 generate_null_check = !cls->IsInDexCache();
4481 break;
4482 }
4483 case HLoadClass::LoadKind::kDexCachePcRelative: {
4484 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4485 int32_t offset =
4486 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4487 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4488 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4489 generate_null_check = !cls->IsInDexCache();
4490 break;
4491 }
4492 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4493 // /* GcRoot<mirror::Class>[] */ out =
4494 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4495 __ LoadFromOffset(kLoadWord,
4496 out,
4497 base_or_current_method_reg,
4498 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4499 // /* GcRoot<mirror::Class> */ out = out[type_index]
4500 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4501 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4502 generate_null_check = !cls->IsInDexCache();
4503 }
4504 }
4505
4506 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4507 DCHECK(cls->CanCallRuntime());
4508 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4509 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4510 codegen_->AddSlowPath(slow_path);
4511 if (generate_null_check) {
4512 __ Beqz(out, slow_path->GetEntryLabel());
4513 }
4514 if (cls->MustGenerateClinitCheck()) {
4515 GenerateClassInitializationCheck(slow_path, out);
4516 } else {
4517 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004518 }
4519 }
4520}
4521
4522static int32_t GetExceptionTlsOffset() {
4523 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4524}
4525
4526void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4527 LocationSummary* locations =
4528 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4529 locations->SetOut(Location::RequiresRegister());
4530}
4531
4532void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4533 Register out = load->GetLocations()->Out().AsRegister<Register>();
4534 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4535}
4536
4537void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4538 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4539}
4540
4541void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4542 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4543}
4544
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004545void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004546 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004547 ? LocationSummary::kCallOnSlowPath
4548 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004550 HLoadString::LoadKind load_kind = load->GetLoadKind();
4551 switch (load_kind) {
4552 // We need an extra register for PC-relative literals on R2.
4553 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4554 case HLoadString::LoadKind::kBootImageAddress:
4555 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4556 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4557 break;
4558 }
4559 FALLTHROUGH_INTENDED;
4560 // We need an extra register for PC-relative dex cache accesses.
4561 case HLoadString::LoadKind::kDexCachePcRelative:
4562 case HLoadString::LoadKind::kDexCacheViaMethod:
4563 locations->SetInAt(0, Location::RequiresRegister());
4564 break;
4565 default:
4566 break;
4567 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004568 locations->SetOut(Location::RequiresRegister());
4569}
4570
4571void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004572 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004573 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004574 Location out_loc = locations->Out();
4575 Register out = out_loc.AsRegister<Register>();
4576 Register base_or_current_method_reg;
4577 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4578 switch (load_kind) {
4579 // We need an extra register for PC-relative literals on R2.
4580 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4581 case HLoadString::LoadKind::kBootImageAddress:
4582 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4583 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4584 break;
4585 // We need an extra register for PC-relative dex cache accesses.
4586 case HLoadString::LoadKind::kDexCachePcRelative:
4587 case HLoadString::LoadKind::kDexCacheViaMethod:
4588 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4589 break;
4590 default:
4591 base_or_current_method_reg = ZERO;
4592 break;
4593 }
4594
4595 switch (load_kind) {
4596 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4597 DCHECK(!kEmitCompilerReadBarrier);
4598 __ LoadLiteral(out,
4599 base_or_current_method_reg,
4600 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4601 load->GetStringIndex()));
4602 return; // No dex cache slow path.
4603 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4604 DCHECK(!kEmitCompilerReadBarrier);
4605 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4606 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
4607 if (isR6) {
4608 __ Bind(&info->high_label);
4609 __ Bind(&info->pc_rel_label);
4610 // Add a 32-bit offset to PC.
4611 __ Auipc(out, /* placeholder */ 0x1234);
4612 __ Addiu(out, out, /* placeholder */ 0x5678);
4613 } else {
4614 __ Bind(&info->high_label);
4615 __ Lui(out, /* placeholder */ 0x1234);
4616 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4617 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4618 __ Ori(out, out, /* placeholder */ 0x5678);
4619 // Add a 32-bit offset to PC.
4620 __ Addu(out, out, base_or_current_method_reg);
4621 }
4622 return; // No dex cache slow path.
4623 }
4624 case HLoadString::LoadKind::kBootImageAddress: {
4625 DCHECK(!kEmitCompilerReadBarrier);
4626 DCHECK_NE(load->GetAddress(), 0u);
4627 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4628 __ LoadLiteral(out,
4629 base_or_current_method_reg,
4630 codegen_->DeduplicateBootImageAddressLiteral(address));
4631 return; // No dex cache slow path.
4632 }
4633 case HLoadString::LoadKind::kDexCacheAddress: {
4634 DCHECK_NE(load->GetAddress(), 0u);
4635 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4636 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4637 DCHECK_ALIGNED(load->GetAddress(), 4u);
4638 int16_t offset = Low16Bits(address);
4639 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4640 __ Lui(out, High16Bits(base_address));
4641 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
4642 GenerateGcRootFieldLoad(load, out_loc, out, offset);
4643 break;
4644 }
4645 case HLoadString::LoadKind::kDexCachePcRelative: {
4646 HMipsDexCacheArraysBase* base = load->InputAt(0)->AsMipsDexCacheArraysBase();
4647 int32_t offset =
4648 load->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4649 // /* GcRoot<mirror::String> */ out = *(dex_cache_arrays_base + offset)
4650 GenerateGcRootFieldLoad(load, out_loc, base_or_current_method_reg, offset);
4651 break;
4652 }
4653 case HLoadString::LoadKind::kDexCacheViaMethod: {
4654 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4655 GenerateGcRootFieldLoad(load,
4656 out_loc,
4657 base_or_current_method_reg,
4658 ArtMethod::DeclaringClassOffset().Int32Value());
4659 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4660 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4661 // /* GcRoot<mirror::String> */ out = out[string_index]
4662 GenerateGcRootFieldLoad(load,
4663 out_loc,
4664 out,
4665 CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4666 break;
4667 }
4668 default:
4669 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4670 UNREACHABLE();
4671 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004672
4673 if (!load->IsInDexCache()) {
4674 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4675 codegen_->AddSlowPath(slow_path);
4676 __ Beqz(out, slow_path->GetEntryLabel());
4677 __ Bind(slow_path->GetExitLabel());
4678 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004679}
4680
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004681void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4682 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4683 locations->SetOut(Location::ConstantLocation(constant));
4684}
4685
4686void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4687 // Will be generated at use site.
4688}
4689
4690void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4691 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004692 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004693 InvokeRuntimeCallingConvention calling_convention;
4694 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4695}
4696
4697void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4698 if (instruction->IsEnter()) {
4699 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4700 instruction,
4701 instruction->GetDexPc(),
4702 nullptr,
4703 IsDirectEntrypoint(kQuickLockObject));
4704 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4705 } else {
4706 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4707 instruction,
4708 instruction->GetDexPc(),
4709 nullptr,
4710 IsDirectEntrypoint(kQuickUnlockObject));
4711 }
4712 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4713}
4714
4715void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4716 LocationSummary* locations =
4717 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4718 switch (mul->GetResultType()) {
4719 case Primitive::kPrimInt:
4720 case Primitive::kPrimLong:
4721 locations->SetInAt(0, Location::RequiresRegister());
4722 locations->SetInAt(1, Location::RequiresRegister());
4723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4724 break;
4725
4726 case Primitive::kPrimFloat:
4727 case Primitive::kPrimDouble:
4728 locations->SetInAt(0, Location::RequiresFpuRegister());
4729 locations->SetInAt(1, Location::RequiresFpuRegister());
4730 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4731 break;
4732
4733 default:
4734 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4735 }
4736}
4737
4738void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4739 Primitive::Type type = instruction->GetType();
4740 LocationSummary* locations = instruction->GetLocations();
4741 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4742
4743 switch (type) {
4744 case Primitive::kPrimInt: {
4745 Register dst = locations->Out().AsRegister<Register>();
4746 Register lhs = locations->InAt(0).AsRegister<Register>();
4747 Register rhs = locations->InAt(1).AsRegister<Register>();
4748
4749 if (isR6) {
4750 __ MulR6(dst, lhs, rhs);
4751 } else {
4752 __ MulR2(dst, lhs, rhs);
4753 }
4754 break;
4755 }
4756 case Primitive::kPrimLong: {
4757 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4758 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4759 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4760 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4761 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4762 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4763
4764 // Extra checks to protect caused by the existance of A1_A2.
4765 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4766 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4767 DCHECK_NE(dst_high, lhs_low);
4768 DCHECK_NE(dst_high, rhs_low);
4769
4770 // A_B * C_D
4771 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4772 // dst_lo: [ low(B*D) ]
4773 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4774
4775 if (isR6) {
4776 __ MulR6(TMP, lhs_high, rhs_low);
4777 __ MulR6(dst_high, lhs_low, rhs_high);
4778 __ Addu(dst_high, dst_high, TMP);
4779 __ MuhuR6(TMP, lhs_low, rhs_low);
4780 __ Addu(dst_high, dst_high, TMP);
4781 __ MulR6(dst_low, lhs_low, rhs_low);
4782 } else {
4783 __ MulR2(TMP, lhs_high, rhs_low);
4784 __ MulR2(dst_high, lhs_low, rhs_high);
4785 __ Addu(dst_high, dst_high, TMP);
4786 __ MultuR2(lhs_low, rhs_low);
4787 __ Mfhi(TMP);
4788 __ Addu(dst_high, dst_high, TMP);
4789 __ Mflo(dst_low);
4790 }
4791 break;
4792 }
4793 case Primitive::kPrimFloat:
4794 case Primitive::kPrimDouble: {
4795 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4796 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4797 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4798 if (type == Primitive::kPrimFloat) {
4799 __ MulS(dst, lhs, rhs);
4800 } else {
4801 __ MulD(dst, lhs, rhs);
4802 }
4803 break;
4804 }
4805 default:
4806 LOG(FATAL) << "Unexpected mul type " << type;
4807 }
4808}
4809
4810void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4811 LocationSummary* locations =
4812 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4813 switch (neg->GetResultType()) {
4814 case Primitive::kPrimInt:
4815 case Primitive::kPrimLong:
4816 locations->SetInAt(0, Location::RequiresRegister());
4817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4818 break;
4819
4820 case Primitive::kPrimFloat:
4821 case Primitive::kPrimDouble:
4822 locations->SetInAt(0, Location::RequiresFpuRegister());
4823 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4824 break;
4825
4826 default:
4827 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4828 }
4829}
4830
4831void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4832 Primitive::Type type = instruction->GetType();
4833 LocationSummary* locations = instruction->GetLocations();
4834
4835 switch (type) {
4836 case Primitive::kPrimInt: {
4837 Register dst = locations->Out().AsRegister<Register>();
4838 Register src = locations->InAt(0).AsRegister<Register>();
4839 __ Subu(dst, ZERO, src);
4840 break;
4841 }
4842 case Primitive::kPrimLong: {
4843 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4844 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4845 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4846 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4847 __ Subu(dst_low, ZERO, src_low);
4848 __ Sltu(TMP, ZERO, dst_low);
4849 __ Subu(dst_high, ZERO, src_high);
4850 __ Subu(dst_high, dst_high, TMP);
4851 break;
4852 }
4853 case Primitive::kPrimFloat:
4854 case Primitive::kPrimDouble: {
4855 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4856 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4857 if (type == Primitive::kPrimFloat) {
4858 __ NegS(dst, src);
4859 } else {
4860 __ NegD(dst, src);
4861 }
4862 break;
4863 }
4864 default:
4865 LOG(FATAL) << "Unexpected neg type " << type;
4866 }
4867}
4868
4869void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4870 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004871 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004872 InvokeRuntimeCallingConvention calling_convention;
4873 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4874 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4875 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4876 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4877}
4878
4879void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4880 InvokeRuntimeCallingConvention calling_convention;
4881 Register current_method_register = calling_convention.GetRegisterAt(2);
4882 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4883 // Move an uint16_t value to a register.
4884 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4885 codegen_->InvokeRuntime(
4886 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4887 instruction,
4888 instruction->GetDexPc(),
4889 nullptr,
4890 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4891 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4892 void*, uint32_t, int32_t, ArtMethod*>();
4893}
4894
4895void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4896 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004897 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004898 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004899 if (instruction->IsStringAlloc()) {
4900 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4901 } else {
4902 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4903 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4904 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004905 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4906}
4907
4908void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004909 if (instruction->IsStringAlloc()) {
4910 // String is allocated through StringFactory. Call NewEmptyString entry point.
4911 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4912 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4913 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4914 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4915 __ Jalr(T9);
4916 __ Nop();
4917 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4918 } else {
4919 codegen_->InvokeRuntime(
4920 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4921 instruction,
4922 instruction->GetDexPc(),
4923 nullptr,
4924 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4925 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4926 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004927}
4928
4929void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4930 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4931 locations->SetInAt(0, Location::RequiresRegister());
4932 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4933}
4934
4935void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4936 Primitive::Type type = instruction->GetType();
4937 LocationSummary* locations = instruction->GetLocations();
4938
4939 switch (type) {
4940 case Primitive::kPrimInt: {
4941 Register dst = locations->Out().AsRegister<Register>();
4942 Register src = locations->InAt(0).AsRegister<Register>();
4943 __ Nor(dst, src, ZERO);
4944 break;
4945 }
4946
4947 case Primitive::kPrimLong: {
4948 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4949 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4950 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4951 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4952 __ Nor(dst_high, src_high, ZERO);
4953 __ Nor(dst_low, src_low, ZERO);
4954 break;
4955 }
4956
4957 default:
4958 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4959 }
4960}
4961
4962void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4963 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4964 locations->SetInAt(0, Location::RequiresRegister());
4965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4966}
4967
4968void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4969 LocationSummary* locations = instruction->GetLocations();
4970 __ Xori(locations->Out().AsRegister<Register>(),
4971 locations->InAt(0).AsRegister<Register>(),
4972 1);
4973}
4974
4975void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4976 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4977 ? LocationSummary::kCallOnSlowPath
4978 : LocationSummary::kNoCall;
4979 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4980 locations->SetInAt(0, Location::RequiresRegister());
4981 if (instruction->HasUses()) {
4982 locations->SetOut(Location::SameAsFirstInput());
4983 }
4984}
4985
Calin Juravle2ae48182016-03-16 14:05:09 +00004986void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4987 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004988 return;
4989 }
4990 Location obj = instruction->GetLocations()->InAt(0);
4991
4992 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004993 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004994}
4995
Calin Juravle2ae48182016-03-16 14:05:09 +00004996void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004997 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004998 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004999
5000 Location obj = instruction->GetLocations()->InAt(0);
5001
5002 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5003}
5004
5005void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005006 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005007}
5008
5009void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5010 HandleBinaryOp(instruction);
5011}
5012
5013void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5014 HandleBinaryOp(instruction);
5015}
5016
5017void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5018 LOG(FATAL) << "Unreachable";
5019}
5020
5021void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
5022 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5023}
5024
5025void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
5026 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5027 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5028 if (location.IsStackSlot()) {
5029 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5030 } else if (location.IsDoubleStackSlot()) {
5031 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5032 }
5033 locations->SetOut(location);
5034}
5035
5036void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
5037 ATTRIBUTE_UNUSED) {
5038 // Nothing to do, the parameter is already at its location.
5039}
5040
5041void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5042 LocationSummary* locations =
5043 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5044 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5045}
5046
5047void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5048 ATTRIBUTE_UNUSED) {
5049 // Nothing to do, the method is already at its location.
5050}
5051
5052void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5053 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005054 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005055 locations->SetInAt(i, Location::Any());
5056 }
5057 locations->SetOut(Location::Any());
5058}
5059
5060void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5061 LOG(FATAL) << "Unreachable";
5062}
5063
5064void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5065 Primitive::Type type = rem->GetResultType();
5066 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005067 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005068 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5069
5070 switch (type) {
5071 case Primitive::kPrimInt:
5072 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005073 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5075 break;
5076
5077 case Primitive::kPrimLong: {
5078 InvokeRuntimeCallingConvention calling_convention;
5079 locations->SetInAt(0, Location::RegisterPairLocation(
5080 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5081 locations->SetInAt(1, Location::RegisterPairLocation(
5082 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5083 locations->SetOut(calling_convention.GetReturnLocation(type));
5084 break;
5085 }
5086
5087 case Primitive::kPrimFloat:
5088 case Primitive::kPrimDouble: {
5089 InvokeRuntimeCallingConvention calling_convention;
5090 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5091 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5092 locations->SetOut(calling_convention.GetReturnLocation(type));
5093 break;
5094 }
5095
5096 default:
5097 LOG(FATAL) << "Unexpected rem type " << type;
5098 }
5099}
5100
5101void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5102 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005103
5104 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005105 case Primitive::kPrimInt:
5106 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005107 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005108 case Primitive::kPrimLong: {
5109 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
5110 instruction,
5111 instruction->GetDexPc(),
5112 nullptr,
5113 IsDirectEntrypoint(kQuickLmod));
5114 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5115 break;
5116 }
5117 case Primitive::kPrimFloat: {
5118 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
5119 instruction, instruction->GetDexPc(),
5120 nullptr,
5121 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00005122 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005123 break;
5124 }
5125 case Primitive::kPrimDouble: {
5126 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
5127 instruction, instruction->GetDexPc(),
5128 nullptr,
5129 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00005130 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005131 break;
5132 }
5133 default:
5134 LOG(FATAL) << "Unexpected rem type " << type;
5135 }
5136}
5137
5138void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5139 memory_barrier->SetLocations(nullptr);
5140}
5141
5142void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5143 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5144}
5145
5146void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5148 Primitive::Type return_type = ret->InputAt(0)->GetType();
5149 locations->SetInAt(0, MipsReturnLocation(return_type));
5150}
5151
5152void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5153 codegen_->GenerateFrameExit();
5154}
5155
5156void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5157 ret->SetLocations(nullptr);
5158}
5159
5160void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5161 codegen_->GenerateFrameExit();
5162}
5163
Alexey Frunze92d90602015-12-18 18:16:36 -08005164void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5165 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005166}
5167
Alexey Frunze92d90602015-12-18 18:16:36 -08005168void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5169 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005170}
5171
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005172void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5173 HandleShift(shl);
5174}
5175
5176void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5177 HandleShift(shl);
5178}
5179
5180void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5181 HandleShift(shr);
5182}
5183
5184void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5185 HandleShift(shr);
5186}
5187
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005188void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5189 HandleBinaryOp(instruction);
5190}
5191
5192void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5193 HandleBinaryOp(instruction);
5194}
5195
5196void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5197 HandleFieldGet(instruction, instruction->GetFieldInfo());
5198}
5199
5200void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5201 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5202}
5203
5204void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5205 HandleFieldSet(instruction, instruction->GetFieldInfo());
5206}
5207
5208void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5209 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5210}
5211
5212void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5213 HUnresolvedInstanceFieldGet* instruction) {
5214 FieldAccessCallingConventionMIPS calling_convention;
5215 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5216 instruction->GetFieldType(),
5217 calling_convention);
5218}
5219
5220void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5221 HUnresolvedInstanceFieldGet* instruction) {
5222 FieldAccessCallingConventionMIPS calling_convention;
5223 codegen_->GenerateUnresolvedFieldAccess(instruction,
5224 instruction->GetFieldType(),
5225 instruction->GetFieldIndex(),
5226 instruction->GetDexPc(),
5227 calling_convention);
5228}
5229
5230void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5231 HUnresolvedInstanceFieldSet* instruction) {
5232 FieldAccessCallingConventionMIPS calling_convention;
5233 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5234 instruction->GetFieldType(),
5235 calling_convention);
5236}
5237
5238void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5239 HUnresolvedInstanceFieldSet* instruction) {
5240 FieldAccessCallingConventionMIPS calling_convention;
5241 codegen_->GenerateUnresolvedFieldAccess(instruction,
5242 instruction->GetFieldType(),
5243 instruction->GetFieldIndex(),
5244 instruction->GetDexPc(),
5245 calling_convention);
5246}
5247
5248void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5249 HUnresolvedStaticFieldGet* instruction) {
5250 FieldAccessCallingConventionMIPS calling_convention;
5251 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5252 instruction->GetFieldType(),
5253 calling_convention);
5254}
5255
5256void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5257 HUnresolvedStaticFieldGet* instruction) {
5258 FieldAccessCallingConventionMIPS calling_convention;
5259 codegen_->GenerateUnresolvedFieldAccess(instruction,
5260 instruction->GetFieldType(),
5261 instruction->GetFieldIndex(),
5262 instruction->GetDexPc(),
5263 calling_convention);
5264}
5265
5266void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5267 HUnresolvedStaticFieldSet* instruction) {
5268 FieldAccessCallingConventionMIPS calling_convention;
5269 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5270 instruction->GetFieldType(),
5271 calling_convention);
5272}
5273
5274void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5275 HUnresolvedStaticFieldSet* instruction) {
5276 FieldAccessCallingConventionMIPS calling_convention;
5277 codegen_->GenerateUnresolvedFieldAccess(instruction,
5278 instruction->GetFieldType(),
5279 instruction->GetFieldIndex(),
5280 instruction->GetDexPc(),
5281 calling_convention);
5282}
5283
5284void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5285 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5286}
5287
5288void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5289 HBasicBlock* block = instruction->GetBlock();
5290 if (block->GetLoopInformation() != nullptr) {
5291 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5292 // The back edge will generate the suspend check.
5293 return;
5294 }
5295 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5296 // The goto will generate the suspend check.
5297 return;
5298 }
5299 GenerateSuspendCheck(instruction, nullptr);
5300}
5301
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005302void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5303 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005304 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005305 InvokeRuntimeCallingConvention calling_convention;
5306 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5307}
5308
5309void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
5310 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5311 instruction,
5312 instruction->GetDexPc(),
5313 nullptr,
5314 IsDirectEntrypoint(kQuickDeliverException));
5315 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5316}
5317
5318void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5319 Primitive::Type input_type = conversion->GetInputType();
5320 Primitive::Type result_type = conversion->GetResultType();
5321 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005322 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005323
5324 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5325 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5326 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5327 }
5328
5329 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005330 if (!isR6 &&
5331 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5332 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005333 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005334 }
5335
5336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5337
5338 if (call_kind == LocationSummary::kNoCall) {
5339 if (Primitive::IsFloatingPointType(input_type)) {
5340 locations->SetInAt(0, Location::RequiresFpuRegister());
5341 } else {
5342 locations->SetInAt(0, Location::RequiresRegister());
5343 }
5344
5345 if (Primitive::IsFloatingPointType(result_type)) {
5346 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5347 } else {
5348 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5349 }
5350 } else {
5351 InvokeRuntimeCallingConvention calling_convention;
5352
5353 if (Primitive::IsFloatingPointType(input_type)) {
5354 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5355 } else {
5356 DCHECK_EQ(input_type, Primitive::kPrimLong);
5357 locations->SetInAt(0, Location::RegisterPairLocation(
5358 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5359 }
5360
5361 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5362 }
5363}
5364
5365void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5366 LocationSummary* locations = conversion->GetLocations();
5367 Primitive::Type result_type = conversion->GetResultType();
5368 Primitive::Type input_type = conversion->GetInputType();
5369 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005370 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005371
5372 DCHECK_NE(input_type, result_type);
5373
5374 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5375 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5376 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5377 Register src = locations->InAt(0).AsRegister<Register>();
5378
Alexey Frunzea871ef12016-06-27 15:20:11 -07005379 if (dst_low != src) {
5380 __ Move(dst_low, src);
5381 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005382 __ Sra(dst_high, src, 31);
5383 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5384 Register dst = locations->Out().AsRegister<Register>();
5385 Register src = (input_type == Primitive::kPrimLong)
5386 ? locations->InAt(0).AsRegisterPairLow<Register>()
5387 : locations->InAt(0).AsRegister<Register>();
5388
5389 switch (result_type) {
5390 case Primitive::kPrimChar:
5391 __ Andi(dst, src, 0xFFFF);
5392 break;
5393 case Primitive::kPrimByte:
5394 if (has_sign_extension) {
5395 __ Seb(dst, src);
5396 } else {
5397 __ Sll(dst, src, 24);
5398 __ Sra(dst, dst, 24);
5399 }
5400 break;
5401 case Primitive::kPrimShort:
5402 if (has_sign_extension) {
5403 __ Seh(dst, src);
5404 } else {
5405 __ Sll(dst, src, 16);
5406 __ Sra(dst, dst, 16);
5407 }
5408 break;
5409 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005410 if (dst != src) {
5411 __ Move(dst, src);
5412 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005413 break;
5414
5415 default:
5416 LOG(FATAL) << "Unexpected type conversion from " << input_type
5417 << " to " << result_type;
5418 }
5419 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005420 if (input_type == Primitive::kPrimLong) {
5421 if (isR6) {
5422 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5423 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5424 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5425 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5426 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5427 __ Mtc1(src_low, FTMP);
5428 __ Mthc1(src_high, FTMP);
5429 if (result_type == Primitive::kPrimFloat) {
5430 __ Cvtsl(dst, FTMP);
5431 } else {
5432 __ Cvtdl(dst, FTMP);
5433 }
5434 } else {
5435 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
5436 : QUICK_ENTRY_POINT(pL2d);
5437 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
5438 : IsDirectEntrypoint(kQuickL2d);
5439 codegen_->InvokeRuntime(entry_offset,
5440 conversion,
5441 conversion->GetDexPc(),
5442 nullptr,
5443 direct);
5444 if (result_type == Primitive::kPrimFloat) {
5445 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5446 } else {
5447 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5448 }
5449 }
5450 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005451 Register src = locations->InAt(0).AsRegister<Register>();
5452 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5453 __ Mtc1(src, FTMP);
5454 if (result_type == Primitive::kPrimFloat) {
5455 __ Cvtsw(dst, FTMP);
5456 } else {
5457 __ Cvtdw(dst, FTMP);
5458 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005459 }
5460 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5461 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005462 if (result_type == Primitive::kPrimLong) {
5463 if (isR6) {
5464 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5465 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5466 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5467 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5468 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5469 MipsLabel truncate;
5470 MipsLabel done;
5471
5472 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5473 // value when the input is either a NaN or is outside of the range of the output type
5474 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5475 // the same result.
5476 //
5477 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5478 // value of the output type if the input is outside of the range after the truncation or
5479 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5480 // results. This matches the desired float/double-to-int/long conversion exactly.
5481 //
5482 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5483 //
5484 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5485 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5486 // even though it must be NAN2008=1 on R6.
5487 //
5488 // The code takes care of the different behaviors by first comparing the input to the
5489 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5490 // If the input is greater than or equal to the minimum, it procedes to the truncate
5491 // instruction, which will handle such an input the same way irrespective of NAN2008.
5492 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5493 // in order to return either zero or the minimum value.
5494 //
5495 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5496 // truncate instruction for MIPS64R6.
5497 if (input_type == Primitive::kPrimFloat) {
5498 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5499 __ LoadConst32(TMP, min_val);
5500 __ Mtc1(TMP, FTMP);
5501 __ CmpLeS(FTMP, FTMP, src);
5502 } else {
5503 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5504 __ LoadConst32(TMP, High32Bits(min_val));
5505 __ Mtc1(ZERO, FTMP);
5506 __ Mthc1(TMP, FTMP);
5507 __ CmpLeD(FTMP, FTMP, src);
5508 }
5509
5510 __ Bc1nez(FTMP, &truncate);
5511
5512 if (input_type == Primitive::kPrimFloat) {
5513 __ CmpEqS(FTMP, src, src);
5514 } else {
5515 __ CmpEqD(FTMP, src, src);
5516 }
5517 __ Move(dst_low, ZERO);
5518 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5519 __ Mfc1(TMP, FTMP);
5520 __ And(dst_high, dst_high, TMP);
5521
5522 __ B(&done);
5523
5524 __ Bind(&truncate);
5525
5526 if (input_type == Primitive::kPrimFloat) {
5527 __ TruncLS(FTMP, src);
5528 } else {
5529 __ TruncLD(FTMP, src);
5530 }
5531 __ Mfc1(dst_low, FTMP);
5532 __ Mfhc1(dst_high, FTMP);
5533
5534 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005535 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005536 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5537 : QUICK_ENTRY_POINT(pD2l);
5538 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5539 : IsDirectEntrypoint(kQuickD2l);
5540 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5541 if (input_type == Primitive::kPrimFloat) {
5542 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5543 } else {
5544 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5545 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005546 }
5547 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005548 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5549 Register dst = locations->Out().AsRegister<Register>();
5550 MipsLabel truncate;
5551 MipsLabel done;
5552
5553 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5554 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5555 // even though it must be NAN2008=1 on R6.
5556 //
5557 // For details see the large comment above for the truncation of float/double to long on R6.
5558 //
5559 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5560 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005561 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005562 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5563 __ LoadConst32(TMP, min_val);
5564 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005565 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005566 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5567 __ LoadConst32(TMP, High32Bits(min_val));
5568 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005569 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005570 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005571
5572 if (isR6) {
5573 if (input_type == Primitive::kPrimFloat) {
5574 __ CmpLeS(FTMP, FTMP, src);
5575 } else {
5576 __ CmpLeD(FTMP, FTMP, src);
5577 }
5578 __ Bc1nez(FTMP, &truncate);
5579
5580 if (input_type == Primitive::kPrimFloat) {
5581 __ CmpEqS(FTMP, src, src);
5582 } else {
5583 __ CmpEqD(FTMP, src, src);
5584 }
5585 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5586 __ Mfc1(TMP, FTMP);
5587 __ And(dst, dst, TMP);
5588 } else {
5589 if (input_type == Primitive::kPrimFloat) {
5590 __ ColeS(0, FTMP, src);
5591 } else {
5592 __ ColeD(0, FTMP, src);
5593 }
5594 __ Bc1t(0, &truncate);
5595
5596 if (input_type == Primitive::kPrimFloat) {
5597 __ CeqS(0, src, src);
5598 } else {
5599 __ CeqD(0, src, src);
5600 }
5601 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5602 __ Movf(dst, ZERO, 0);
5603 }
5604
5605 __ B(&done);
5606
5607 __ Bind(&truncate);
5608
5609 if (input_type == Primitive::kPrimFloat) {
5610 __ TruncWS(FTMP, src);
5611 } else {
5612 __ TruncWD(FTMP, src);
5613 }
5614 __ Mfc1(dst, FTMP);
5615
5616 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005617 }
5618 } else if (Primitive::IsFloatingPointType(result_type) &&
5619 Primitive::IsFloatingPointType(input_type)) {
5620 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5621 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5622 if (result_type == Primitive::kPrimFloat) {
5623 __ Cvtsd(dst, src);
5624 } else {
5625 __ Cvtds(dst, src);
5626 }
5627 } else {
5628 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5629 << " to " << result_type;
5630 }
5631}
5632
5633void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5634 HandleShift(ushr);
5635}
5636
5637void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5638 HandleShift(ushr);
5639}
5640
5641void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5642 HandleBinaryOp(instruction);
5643}
5644
5645void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5646 HandleBinaryOp(instruction);
5647}
5648
5649void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5650 // Nothing to do, this should be removed during prepare for register allocator.
5651 LOG(FATAL) << "Unreachable";
5652}
5653
5654void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5655 // Nothing to do, this should be removed during prepare for register allocator.
5656 LOG(FATAL) << "Unreachable";
5657}
5658
5659void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005660 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005661}
5662
5663void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005664 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005665}
5666
5667void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005668 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005669}
5670
5671void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005672 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005673}
5674
5675void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005676 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005677}
5678
5679void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005680 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005681}
5682
5683void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005684 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005685}
5686
5687void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005688 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005689}
5690
5691void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005692 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005693}
5694
5695void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005696 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005697}
5698
5699void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005700 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005701}
5702
5703void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005704 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005705}
5706
5707void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005708 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005709}
5710
5711void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005712 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005713}
5714
5715void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005716 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005717}
5718
5719void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005720 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005721}
5722
5723void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005724 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005725}
5726
5727void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005728 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005729}
5730
5731void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005732 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005733}
5734
5735void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005736 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005737}
5738
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005739void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5740 LocationSummary* locations =
5741 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5742 locations->SetInAt(0, Location::RequiresRegister());
5743}
5744
5745void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5746 int32_t lower_bound = switch_instr->GetStartValue();
5747 int32_t num_entries = switch_instr->GetNumEntries();
5748 LocationSummary* locations = switch_instr->GetLocations();
5749 Register value_reg = locations->InAt(0).AsRegister<Register>();
5750 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5751
5752 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005753 Register temp_reg = TMP;
5754 __ Addiu32(temp_reg, value_reg, -lower_bound);
5755 // Jump to default if index is negative
5756 // Note: We don't check the case that index is positive while value < lower_bound, because in
5757 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5758 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5759
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005760 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005761 // Jump to successors[0] if value == lower_bound.
5762 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5763 int32_t last_index = 0;
5764 for (; num_entries - last_index > 2; last_index += 2) {
5765 __ Addiu(temp_reg, temp_reg, -2);
5766 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5767 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5768 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5769 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5770 }
5771 if (num_entries - last_index == 2) {
5772 // The last missing case_value.
5773 __ Addiu(temp_reg, temp_reg, -1);
5774 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005775 }
5776
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005777 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005778 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5779 __ B(codegen_->GetLabelOf(default_block));
5780 }
5781}
5782
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005783void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5784 HMipsComputeBaseMethodAddress* insn) {
5785 LocationSummary* locations =
5786 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5787 locations->SetOut(Location::RequiresRegister());
5788}
5789
5790void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5791 HMipsComputeBaseMethodAddress* insn) {
5792 LocationSummary* locations = insn->GetLocations();
5793 Register reg = locations->Out().AsRegister<Register>();
5794
5795 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5796
5797 // Generate a dummy PC-relative call to obtain PC.
5798 __ Nal();
5799 // Grab the return address off RA.
5800 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005801 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005802
5803 // Remember this offset (the obtained PC value) for later use with constant area.
5804 __ BindPcRelBaseLabel();
5805}
5806
5807void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5808 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5809 locations->SetOut(Location::RequiresRegister());
5810}
5811
5812void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5813 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5814 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5815 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
5816
5817 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5818 __ Bind(&info->high_label);
5819 __ Bind(&info->pc_rel_label);
5820 // Add a 32-bit offset to PC.
5821 __ Auipc(reg, /* placeholder */ 0x1234);
5822 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5823 } else {
5824 // Generate a dummy PC-relative call to obtain PC.
5825 __ Nal();
5826 __ Bind(&info->high_label);
5827 __ Lui(reg, /* placeholder */ 0x1234);
5828 __ Bind(&info->pc_rel_label);
5829 __ Ori(reg, reg, /* placeholder */ 0x5678);
5830 // Add a 32-bit offset to PC.
5831 __ Addu(reg, reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005832 // TODO: Can we share this code with that of VisitMipsComputeBaseMethodAddress()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005833 }
5834}
5835
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005836void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5837 // The trampoline uses the same calling convention as dex calling conventions,
5838 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5839 // the method_idx.
5840 HandleInvoke(invoke);
5841}
5842
5843void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5844 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5845}
5846
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005847void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5848 LocationSummary* locations =
5849 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5850 locations->SetInAt(0, Location::RequiresRegister());
5851 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005852}
5853
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005854void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5855 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005856 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005857 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005858 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005859 __ LoadFromOffset(kLoadWord,
5860 locations->Out().AsRegister<Register>(),
5861 locations->InAt(0).AsRegister<Register>(),
5862 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005863 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005864 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005865 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005866 __ LoadFromOffset(kLoadWord,
5867 locations->Out().AsRegister<Register>(),
5868 locations->InAt(0).AsRegister<Register>(),
5869 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005870 __ LoadFromOffset(kLoadWord,
5871 locations->Out().AsRegister<Register>(),
5872 locations->Out().AsRegister<Register>(),
5873 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005874 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005875}
5876
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005877#undef __
5878#undef QUICK_ENTRY_POINT
5879
5880} // namespace mips
5881} // namespace art