blob: af998e9729499f9a7e89e9ef476375337fe2cf24 [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
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100148// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
149#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700150#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200151
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());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200354 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
355 instruction_,
356 instruction_->GetDexPc(),
357 this,
358 IsDirectEntrypoint(kQuickTestSuspend));
359 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200360 if (successor_ == nullptr) {
361 __ B(GetReturnLabel());
362 } else {
363 __ B(mips_codegen->GetLabelOf(successor_));
364 }
365 }
366
367 MipsLabel* GetReturnLabel() {
368 DCHECK(successor_ == nullptr);
369 return &return_label_;
370 }
371
372 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
373
374 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200375 // If not null, the block to branch to after the suspend check.
376 HBasicBlock* const successor_;
377
378 // If `successor_` is null, the label to branch to after the suspend check.
379 MipsLabel return_label_;
380
381 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
382};
383
384class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
385 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000386 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200387
388 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
389 LocationSummary* locations = instruction_->GetLocations();
390 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
391 uint32_t dex_pc = instruction_->GetDexPc();
392 DCHECK(instruction_->IsCheckCast()
393 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
394 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
395
396 __ Bind(GetEntryLabel());
397 SaveLiveRegisters(codegen, locations);
398
399 // We're moving two locations to locations that could overlap, so we need a parallel
400 // move resolver.
401 InvokeRuntimeCallingConvention calling_convention;
402 codegen->EmitParallelMoves(locations->InAt(1),
403 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
404 Primitive::kPrimNot,
405 object_class,
406 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
407 Primitive::kPrimNot);
408
409 if (instruction_->IsInstanceOf()) {
410 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
411 instruction_,
412 dex_pc,
413 this,
414 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000415 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700416 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 Primitive::Type ret_type = instruction_->GetType();
418 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
419 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 } else {
421 DCHECK(instruction_->IsCheckCast());
422 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
423 instruction_,
424 dex_pc,
425 this,
426 IsDirectEntrypoint(kQuickCheckCast));
427 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
428 }
429
430 RestoreLiveRegisters(codegen, locations);
431 __ B(GetExitLabel());
432 }
433
434 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
435
436 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
438};
439
440class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
441 public:
Aart Bik42249c32016-01-07 15:33:50 -0800442 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000443 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800446 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 __ Bind(GetEntryLabel());
448 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200449 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
450 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800451 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200452 this,
453 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000454 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 }
456
457 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
458
459 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200460 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
461};
462
463CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
464 const MipsInstructionSetFeatures& isa_features,
465 const CompilerOptions& compiler_options,
466 OptimizingCompilerStats* stats)
467 : CodeGenerator(graph,
468 kNumberOfCoreRegisters,
469 kNumberOfFRegisters,
470 kNumberOfRegisterPairs,
471 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
472 arraysize(kCoreCalleeSaves)),
473 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
474 arraysize(kFpuCalleeSaves)),
475 compiler_options,
476 stats),
477 block_labels_(nullptr),
478 location_builder_(graph, this),
479 instruction_visitor_(graph, this),
480 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100481 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700482 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700483 uint32_literals_(std::less<uint32_t>(),
484 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700485 method_patches_(MethodReferenceComparator(),
486 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
487 call_patches_(MethodReferenceComparator(),
488 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700489 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
490 boot_image_string_patches_(StringReferenceValueComparator(),
491 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
492 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
493 boot_image_type_patches_(TypeReferenceValueComparator(),
494 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
495 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
496 boot_image_address_patches_(std::less<uint32_t>(),
497 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
498 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200499 // Save RA (containing the return address) to mimic Quick.
500 AddAllocatedRegister(Location::RegisterLocation(RA));
501}
502
503#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100504// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
505#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700506#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200507
508void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
509 // Ensure that we fix up branches.
510 __ FinalizeCode();
511
512 // Adjust native pc offsets in stack maps.
513 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
514 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
515 uint32_t new_position = __ GetAdjustedPosition(old_position);
516 DCHECK_GE(new_position, old_position);
517 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
518 }
519
520 // Adjust pc offsets for the disassembly information.
521 if (disasm_info_ != nullptr) {
522 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
523 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
524 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
525 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
526 it.second.start = __ GetAdjustedPosition(it.second.start);
527 it.second.end = __ GetAdjustedPosition(it.second.end);
528 }
529 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
530 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
531 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
532 }
533 }
534
535 CodeGenerator::Finalize(allocator);
536}
537
538MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
539 return codegen_->GetAssembler();
540}
541
542void ParallelMoveResolverMIPS::EmitMove(size_t index) {
543 DCHECK_LT(index, moves_.size());
544 MoveOperands* move = moves_[index];
545 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
546}
547
548void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
549 DCHECK_LT(index, moves_.size());
550 MoveOperands* move = moves_[index];
551 Primitive::Type type = move->GetType();
552 Location loc1 = move->GetDestination();
553 Location loc2 = move->GetSource();
554
555 DCHECK(!loc1.IsConstant());
556 DCHECK(!loc2.IsConstant());
557
558 if (loc1.Equals(loc2)) {
559 return;
560 }
561
562 if (loc1.IsRegister() && loc2.IsRegister()) {
563 // Swap 2 GPRs.
564 Register r1 = loc1.AsRegister<Register>();
565 Register r2 = loc2.AsRegister<Register>();
566 __ Move(TMP, r2);
567 __ Move(r2, r1);
568 __ Move(r1, TMP);
569 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
570 FRegister f1 = loc1.AsFpuRegister<FRegister>();
571 FRegister f2 = loc2.AsFpuRegister<FRegister>();
572 if (type == Primitive::kPrimFloat) {
573 __ MovS(FTMP, f2);
574 __ MovS(f2, f1);
575 __ MovS(f1, FTMP);
576 } else {
577 DCHECK_EQ(type, Primitive::kPrimDouble);
578 __ MovD(FTMP, f2);
579 __ MovD(f2, f1);
580 __ MovD(f1, FTMP);
581 }
582 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
583 (loc1.IsFpuRegister() && loc2.IsRegister())) {
584 // Swap FPR and GPR.
585 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
586 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
587 : loc2.AsFpuRegister<FRegister>();
588 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
589 : loc2.AsRegister<Register>();
590 __ Move(TMP, r2);
591 __ Mfc1(r2, f1);
592 __ Mtc1(TMP, f1);
593 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
594 // Swap 2 GPR register pairs.
595 Register r1 = loc1.AsRegisterPairLow<Register>();
596 Register r2 = loc2.AsRegisterPairLow<Register>();
597 __ Move(TMP, r2);
598 __ Move(r2, r1);
599 __ Move(r1, TMP);
600 r1 = loc1.AsRegisterPairHigh<Register>();
601 r2 = loc2.AsRegisterPairHigh<Register>();
602 __ Move(TMP, r2);
603 __ Move(r2, r1);
604 __ Move(r1, TMP);
605 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
606 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
607 // Swap FPR and GPR register pair.
608 DCHECK_EQ(type, Primitive::kPrimDouble);
609 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
610 : loc2.AsFpuRegister<FRegister>();
611 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
612 : loc2.AsRegisterPairLow<Register>();
613 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
614 : loc2.AsRegisterPairHigh<Register>();
615 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
616 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
617 // unpredictable and the following mfch1 will fail.
618 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800619 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200620 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800621 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200622 __ Move(r2_l, TMP);
623 __ Move(r2_h, AT);
624 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
625 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
626 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
627 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000628 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
629 (loc1.IsStackSlot() && loc2.IsRegister())) {
630 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
631 : loc2.AsRegister<Register>();
632 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
633 : loc2.GetStackIndex();
634 __ Move(TMP, reg);
635 __ LoadFromOffset(kLoadWord, reg, SP, offset);
636 __ StoreToOffset(kStoreWord, TMP, SP, offset);
637 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
638 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
639 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
640 : loc2.AsRegisterPairLow<Register>();
641 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
642 : loc2.AsRegisterPairHigh<Register>();
643 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
644 : loc2.GetStackIndex();
645 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
646 : loc2.GetHighStackIndex(kMipsWordSize);
647 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000648 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000649 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000650 __ Move(TMP, reg_h);
651 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
652 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200653 } else {
654 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
655 }
656}
657
658void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
659 __ Pop(static_cast<Register>(reg));
660}
661
662void ParallelMoveResolverMIPS::SpillScratch(int reg) {
663 __ Push(static_cast<Register>(reg));
664}
665
666void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
667 // Allocate a scratch register other than TMP, if available.
668 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
669 // automatically unspilled when the scratch scope object is destroyed).
670 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
671 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
672 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
673 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
674 __ LoadFromOffset(kLoadWord,
675 Register(ensure_scratch.GetRegister()),
676 SP,
677 index1 + stack_offset);
678 __ LoadFromOffset(kLoadWord,
679 TMP,
680 SP,
681 index2 + stack_offset);
682 __ StoreToOffset(kStoreWord,
683 Register(ensure_scratch.GetRegister()),
684 SP,
685 index2 + stack_offset);
686 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
687 }
688}
689
Alexey Frunze73296a72016-06-03 22:51:46 -0700690void CodeGeneratorMIPS::ComputeSpillMask() {
691 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
692 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
693 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
694 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
695 // registers, include the ZERO register to force alignment of FPU callee-saved registers
696 // within the stack frame.
697 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
698 core_spill_mask_ |= (1 << ZERO);
699 }
Alexey Frunze06a46c42016-07-19 15:00:40 -0700700 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
701 // (this can happen in leaf methods), artificially spill the ZERO register in order to
702 // force explicit saving and restoring of RA. RA isn't saved/restored when it's the only
703 // spilled register.
704 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
705 // saved in an unused temporary register) and saving of RA and the current method pointer
706 // in the frame.
707 if (clobbered_ra_ && core_spill_mask_ == (1u << RA) && fpu_spill_mask_ == 0) {
708 core_spill_mask_ |= (1 << ZERO);
709 }
Alexey Frunze73296a72016-06-03 22:51:46 -0700710}
711
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200712static dwarf::Reg DWARFReg(Register reg) {
713 return dwarf::Reg::MipsCore(static_cast<int>(reg));
714}
715
716// TODO: mapping of floating-point registers to DWARF.
717
718void CodeGeneratorMIPS::GenerateFrameEntry() {
719 __ Bind(&frame_entry_label_);
720
721 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
722
723 if (do_overflow_check) {
724 __ LoadFromOffset(kLoadWord,
725 ZERO,
726 SP,
727 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
728 RecordPcInfo(nullptr, 0);
729 }
730
731 if (HasEmptyFrame()) {
732 return;
733 }
734
735 // Make sure the frame size isn't unreasonably large.
736 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
737 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
738 }
739
740 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200741
Alexey Frunze73296a72016-06-03 22:51:46 -0700742 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200743 __ IncreaseFrameSize(ofs);
744
Alexey Frunze73296a72016-06-03 22:51:46 -0700745 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
746 Register reg = static_cast<Register>(MostSignificantBit(mask));
747 mask ^= 1u << reg;
748 ofs -= kMipsWordSize;
749 // The ZERO register is only included for alignment.
750 if (reg != ZERO) {
751 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200752 __ cfi().RelOffset(DWARFReg(reg), ofs);
753 }
754 }
755
Alexey Frunze73296a72016-06-03 22:51:46 -0700756 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
757 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
758 mask ^= 1u << reg;
759 ofs -= kMipsDoublewordSize;
760 __ StoreDToOffset(reg, SP, ofs);
761 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200762 }
763
Alexey Frunze73296a72016-06-03 22:51:46 -0700764 // Store the current method pointer.
765 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200766}
767
768void CodeGeneratorMIPS::GenerateFrameExit() {
769 __ cfi().RememberState();
770
771 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200772 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200773
Alexey Frunze73296a72016-06-03 22:51:46 -0700774 // For better instruction scheduling restore RA before other registers.
775 uint32_t ofs = GetFrameSize();
776 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
777 Register reg = static_cast<Register>(MostSignificantBit(mask));
778 mask ^= 1u << reg;
779 ofs -= kMipsWordSize;
780 // The ZERO register is only included for alignment.
781 if (reg != ZERO) {
782 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200783 __ cfi().Restore(DWARFReg(reg));
784 }
785 }
786
Alexey Frunze73296a72016-06-03 22:51:46 -0700787 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
788 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
789 mask ^= 1u << reg;
790 ofs -= kMipsDoublewordSize;
791 __ LoadDFromOffset(reg, SP, ofs);
792 // TODO: __ cfi().Restore(DWARFReg(reg));
793 }
794
795 __ DecreaseFrameSize(GetFrameSize());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200796 }
797
798 __ Jr(RA);
799 __ Nop();
800
801 __ cfi().RestoreState();
802 __ cfi().DefCFAOffset(GetFrameSize());
803}
804
805void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
806 __ Bind(GetLabelOf(block));
807}
808
809void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
810 if (src.Equals(dst)) {
811 return;
812 }
813
814 if (src.IsConstant()) {
815 MoveConstant(dst, src.GetConstant());
816 } else {
817 if (Primitive::Is64BitType(dst_type)) {
818 Move64(dst, src);
819 } else {
820 Move32(dst, src);
821 }
822 }
823}
824
825void CodeGeneratorMIPS::Move32(Location destination, Location source) {
826 if (source.Equals(destination)) {
827 return;
828 }
829
830 if (destination.IsRegister()) {
831 if (source.IsRegister()) {
832 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
833 } else if (source.IsFpuRegister()) {
834 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
835 } else {
836 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
837 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
838 }
839 } else if (destination.IsFpuRegister()) {
840 if (source.IsRegister()) {
841 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
842 } else if (source.IsFpuRegister()) {
843 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
844 } else {
845 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
846 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
847 }
848 } else {
849 DCHECK(destination.IsStackSlot()) << destination;
850 if (source.IsRegister()) {
851 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
852 } else if (source.IsFpuRegister()) {
853 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
854 } else {
855 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
856 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
857 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
858 }
859 }
860}
861
862void CodeGeneratorMIPS::Move64(Location destination, Location source) {
863 if (source.Equals(destination)) {
864 return;
865 }
866
867 if (destination.IsRegisterPair()) {
868 if (source.IsRegisterPair()) {
869 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
870 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
871 } else if (source.IsFpuRegister()) {
872 Register dst_high = destination.AsRegisterPairHigh<Register>();
873 Register dst_low = destination.AsRegisterPairLow<Register>();
874 FRegister src = source.AsFpuRegister<FRegister>();
875 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800876 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200877 } else {
878 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
879 int32_t off = source.GetStackIndex();
880 Register r = destination.AsRegisterPairLow<Register>();
881 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
882 }
883 } else if (destination.IsFpuRegister()) {
884 if (source.IsRegisterPair()) {
885 FRegister dst = destination.AsFpuRegister<FRegister>();
886 Register src_high = source.AsRegisterPairHigh<Register>();
887 Register src_low = source.AsRegisterPairLow<Register>();
888 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800889 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200890 } else if (source.IsFpuRegister()) {
891 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
892 } else {
893 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
894 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
895 }
896 } else {
897 DCHECK(destination.IsDoubleStackSlot()) << destination;
898 int32_t off = destination.GetStackIndex();
899 if (source.IsRegisterPair()) {
900 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
901 } else if (source.IsFpuRegister()) {
902 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
903 } else {
904 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
905 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
906 __ StoreToOffset(kStoreWord, TMP, SP, off);
907 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
908 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
909 }
910 }
911}
912
913void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
914 if (c->IsIntConstant() || c->IsNullConstant()) {
915 // Move 32 bit constant.
916 int32_t value = GetInt32ValueOf(c);
917 if (destination.IsRegister()) {
918 Register dst = destination.AsRegister<Register>();
919 __ LoadConst32(dst, value);
920 } else {
921 DCHECK(destination.IsStackSlot())
922 << "Cannot move " << c->DebugName() << " to " << destination;
923 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
924 }
925 } else if (c->IsLongConstant()) {
926 // Move 64 bit constant.
927 int64_t value = GetInt64ValueOf(c);
928 if (destination.IsRegisterPair()) {
929 Register r_h = destination.AsRegisterPairHigh<Register>();
930 Register r_l = destination.AsRegisterPairLow<Register>();
931 __ LoadConst64(r_h, r_l, value);
932 } else {
933 DCHECK(destination.IsDoubleStackSlot())
934 << "Cannot move " << c->DebugName() << " to " << destination;
935 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
936 }
937 } else if (c->IsFloatConstant()) {
938 // Move 32 bit float constant.
939 int32_t value = GetInt32ValueOf(c);
940 if (destination.IsFpuRegister()) {
941 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
942 } else {
943 DCHECK(destination.IsStackSlot())
944 << "Cannot move " << c->DebugName() << " to " << destination;
945 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
946 }
947 } else {
948 // Move 64 bit double constant.
949 DCHECK(c->IsDoubleConstant()) << c->DebugName();
950 int64_t value = GetInt64ValueOf(c);
951 if (destination.IsFpuRegister()) {
952 FRegister fd = destination.AsFpuRegister<FRegister>();
953 __ LoadDConst64(fd, value, TMP);
954 } else {
955 DCHECK(destination.IsDoubleStackSlot())
956 << "Cannot move " << c->DebugName() << " to " << destination;
957 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
958 }
959 }
960}
961
962void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
963 DCHECK(destination.IsRegister());
964 Register dst = destination.AsRegister<Register>();
965 __ LoadConst32(dst, value);
966}
967
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200968void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
969 if (location.IsRegister()) {
970 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700971 } else if (location.IsRegisterPair()) {
972 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
973 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200974 } else {
975 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
976 }
977}
978
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700979void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
980 DCHECK(linker_patches->empty());
981 size_t size =
982 method_patches_.size() +
983 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700984 pc_relative_dex_cache_patches_.size() +
985 pc_relative_string_patches_.size() +
986 pc_relative_type_patches_.size() +
987 boot_image_string_patches_.size() +
988 boot_image_type_patches_.size() +
989 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700990 linker_patches->reserve(size);
991 for (const auto& entry : method_patches_) {
992 const MethodReference& target_method = entry.first;
993 Literal* literal = entry.second;
994 DCHECK(literal->GetLabel()->IsBound());
995 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
996 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
997 target_method.dex_file,
998 target_method.dex_method_index));
999 }
1000 for (const auto& entry : call_patches_) {
1001 const MethodReference& target_method = entry.first;
1002 Literal* literal = entry.second;
1003 DCHECK(literal->GetLabel()->IsBound());
1004 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1005 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1006 target_method.dex_file,
1007 target_method.dex_method_index));
1008 }
1009 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
1010 const DexFile& dex_file = info.target_dex_file;
1011 size_t base_element_offset = info.offset_or_index;
1012 DCHECK(info.high_label.IsBound());
1013 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1014 DCHECK(info.pc_rel_label.IsBound());
1015 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1016 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
1017 &dex_file,
1018 pc_rel_offset,
1019 base_element_offset));
1020 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001021 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
1022 const DexFile& dex_file = info.target_dex_file;
1023 size_t string_index = info.offset_or_index;
1024 DCHECK(info.high_label.IsBound());
1025 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1026 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1027 // the assembler's base label used for PC-relative literals.
1028 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1029 ? __ GetLabelLocation(&info.pc_rel_label)
1030 : __ GetPcRelBaseLabelLocation();
1031 linker_patches->push_back(LinkerPatch::RelativeStringPatch(high_offset,
1032 &dex_file,
1033 pc_rel_offset,
1034 string_index));
1035 }
1036 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
1037 const DexFile& dex_file = info.target_dex_file;
1038 size_t type_index = info.offset_or_index;
1039 DCHECK(info.high_label.IsBound());
1040 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1041 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1042 // the assembler's base label used for PC-relative literals.
1043 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1044 ? __ GetLabelLocation(&info.pc_rel_label)
1045 : __ GetPcRelBaseLabelLocation();
1046 linker_patches->push_back(LinkerPatch::RelativeTypePatch(high_offset,
1047 &dex_file,
1048 pc_rel_offset,
1049 type_index));
1050 }
1051 for (const auto& entry : boot_image_string_patches_) {
1052 const StringReference& target_string = entry.first;
1053 Literal* literal = entry.second;
1054 DCHECK(literal->GetLabel()->IsBound());
1055 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1056 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1057 target_string.dex_file,
1058 target_string.string_index));
1059 }
1060 for (const auto& entry : boot_image_type_patches_) {
1061 const TypeReference& target_type = entry.first;
1062 Literal* literal = entry.second;
1063 DCHECK(literal->GetLabel()->IsBound());
1064 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1065 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1066 target_type.dex_file,
1067 target_type.type_index));
1068 }
1069 for (const auto& entry : boot_image_address_patches_) {
1070 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1071 Literal* literal = entry.second;
1072 DCHECK(literal->GetLabel()->IsBound());
1073 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1074 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1075 }
1076}
1077
1078CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1079 const DexFile& dex_file, uint32_t string_index) {
1080 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1081}
1082
1083CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1084 const DexFile& dex_file, uint32_t type_index) {
1085 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001086}
1087
1088CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1089 const DexFile& dex_file, uint32_t element_offset) {
1090 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1091}
1092
1093CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1094 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1095 patches->emplace_back(dex_file, offset_or_index);
1096 return &patches->back();
1097}
1098
Alexey Frunze06a46c42016-07-19 15:00:40 -07001099Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1100 return map->GetOrCreate(
1101 value,
1102 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1103}
1104
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001105Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1106 MethodToLiteralMap* map) {
1107 return map->GetOrCreate(
1108 target_method,
1109 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1110}
1111
1112Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1113 return DeduplicateMethodLiteral(target_method, &method_patches_);
1114}
1115
1116Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1117 return DeduplicateMethodLiteral(target_method, &call_patches_);
1118}
1119
Alexey Frunze06a46c42016-07-19 15:00:40 -07001120Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1121 uint32_t string_index) {
1122 return boot_image_string_patches_.GetOrCreate(
1123 StringReference(&dex_file, string_index),
1124 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1125}
1126
1127Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1128 uint32_t type_index) {
1129 return boot_image_type_patches_.GetOrCreate(
1130 TypeReference(&dex_file, type_index),
1131 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1132}
1133
1134Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1135 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1136 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1137 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1138}
1139
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001140void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1141 MipsLabel done;
1142 Register card = AT;
1143 Register temp = TMP;
1144 __ Beqz(value, &done);
1145 __ LoadFromOffset(kLoadWord,
1146 card,
1147 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001148 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001149 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1150 __ Addu(temp, card, temp);
1151 __ Sb(card, temp, 0);
1152 __ Bind(&done);
1153}
1154
David Brazdil58282f42016-01-14 12:45:10 +00001155void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001156 // Don't allocate the dalvik style register pair passing.
1157 blocked_register_pairs_[A1_A2] = true;
1158
1159 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1160 blocked_core_registers_[ZERO] = true;
1161 blocked_core_registers_[K0] = true;
1162 blocked_core_registers_[K1] = true;
1163 blocked_core_registers_[GP] = true;
1164 blocked_core_registers_[SP] = true;
1165 blocked_core_registers_[RA] = true;
1166
1167 // AT and TMP(T8) are used as temporary/scratch registers
1168 // (similar to how AT is used by MIPS assemblers).
1169 blocked_core_registers_[AT] = true;
1170 blocked_core_registers_[TMP] = true;
1171 blocked_fpu_registers_[FTMP] = true;
1172
1173 // Reserve suspend and thread registers.
1174 blocked_core_registers_[S0] = true;
1175 blocked_core_registers_[TR] = true;
1176
1177 // Reserve T9 for function calls
1178 blocked_core_registers_[T9] = true;
1179
1180 // Reserve odd-numbered FPU registers.
1181 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1182 blocked_fpu_registers_[i] = true;
1183 }
1184
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001185 if (GetGraph()->IsDebuggable()) {
1186 // Stubs do not save callee-save floating point registers. If the graph
1187 // is debuggable, we need to deal with these registers differently. For
1188 // now, just block them.
1189 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1190 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1191 }
1192 }
1193
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001194 UpdateBlockedPairRegisters();
1195}
1196
1197void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1198 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1199 MipsManagedRegister current =
1200 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1201 if (blocked_core_registers_[current.AsRegisterPairLow()]
1202 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1203 blocked_register_pairs_[i] = true;
1204 }
1205 }
1206}
1207
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001208size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1209 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1210 return kMipsWordSize;
1211}
1212
1213size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1214 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1215 return kMipsWordSize;
1216}
1217
1218size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1219 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1220 return kMipsDoublewordSize;
1221}
1222
1223size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1224 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1225 return kMipsDoublewordSize;
1226}
1227
1228void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001229 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001230}
1231
1232void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001233 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001234}
1235
1236void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1237 HInstruction* instruction,
1238 uint32_t dex_pc,
1239 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001240 InvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001241 instruction,
1242 dex_pc,
1243 slow_path,
1244 IsDirectEntrypoint(entrypoint));
1245}
1246
1247constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1248
1249void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1250 HInstruction* instruction,
1251 uint32_t dex_pc,
1252 SlowPathCode* slow_path,
1253 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001254 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1255 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001256 if (is_direct_entrypoint) {
1257 // Reserve argument space on stack (for $a0-$a3) for
1258 // entrypoints that directly reference native implementations.
1259 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001260 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001261 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001262 } else {
1263 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001264 }
1265 RecordPcInfo(instruction, dex_pc, slow_path);
1266}
1267
1268void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1269 Register class_reg) {
1270 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1271 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1272 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1273 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1274 __ Sync(0);
1275 __ Bind(slow_path->GetExitLabel());
1276}
1277
1278void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1279 __ Sync(0); // Only stype 0 is supported.
1280}
1281
1282void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1283 HBasicBlock* successor) {
1284 SuspendCheckSlowPathMIPS* slow_path =
1285 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1286 codegen_->AddSlowPath(slow_path);
1287
1288 __ LoadFromOffset(kLoadUnsignedHalfword,
1289 TMP,
1290 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001291 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001292 if (successor == nullptr) {
1293 __ Bnez(TMP, slow_path->GetEntryLabel());
1294 __ Bind(slow_path->GetReturnLabel());
1295 } else {
1296 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1297 __ B(slow_path->GetEntryLabel());
1298 // slow_path will return to GetLabelOf(successor).
1299 }
1300}
1301
1302InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1303 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001304 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001305 assembler_(codegen->GetAssembler()),
1306 codegen_(codegen) {}
1307
1308void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1309 DCHECK_EQ(instruction->InputCount(), 2U);
1310 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1311 Primitive::Type type = instruction->GetResultType();
1312 switch (type) {
1313 case Primitive::kPrimInt: {
1314 locations->SetInAt(0, Location::RequiresRegister());
1315 HInstruction* right = instruction->InputAt(1);
1316 bool can_use_imm = false;
1317 if (right->IsConstant()) {
1318 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1319 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1320 can_use_imm = IsUint<16>(imm);
1321 } else if (instruction->IsAdd()) {
1322 can_use_imm = IsInt<16>(imm);
1323 } else {
1324 DCHECK(instruction->IsSub());
1325 can_use_imm = IsInt<16>(-imm);
1326 }
1327 }
1328 if (can_use_imm)
1329 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1330 else
1331 locations->SetInAt(1, Location::RequiresRegister());
1332 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1333 break;
1334 }
1335
1336 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001337 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001338 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001340 break;
1341 }
1342
1343 case Primitive::kPrimFloat:
1344 case Primitive::kPrimDouble:
1345 DCHECK(instruction->IsAdd() || instruction->IsSub());
1346 locations->SetInAt(0, Location::RequiresFpuRegister());
1347 locations->SetInAt(1, Location::RequiresFpuRegister());
1348 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1349 break;
1350
1351 default:
1352 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1353 }
1354}
1355
1356void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1357 Primitive::Type type = instruction->GetType();
1358 LocationSummary* locations = instruction->GetLocations();
1359
1360 switch (type) {
1361 case Primitive::kPrimInt: {
1362 Register dst = locations->Out().AsRegister<Register>();
1363 Register lhs = locations->InAt(0).AsRegister<Register>();
1364 Location rhs_location = locations->InAt(1);
1365
1366 Register rhs_reg = ZERO;
1367 int32_t rhs_imm = 0;
1368 bool use_imm = rhs_location.IsConstant();
1369 if (use_imm) {
1370 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1371 } else {
1372 rhs_reg = rhs_location.AsRegister<Register>();
1373 }
1374
1375 if (instruction->IsAnd()) {
1376 if (use_imm)
1377 __ Andi(dst, lhs, rhs_imm);
1378 else
1379 __ And(dst, lhs, rhs_reg);
1380 } else if (instruction->IsOr()) {
1381 if (use_imm)
1382 __ Ori(dst, lhs, rhs_imm);
1383 else
1384 __ Or(dst, lhs, rhs_reg);
1385 } else if (instruction->IsXor()) {
1386 if (use_imm)
1387 __ Xori(dst, lhs, rhs_imm);
1388 else
1389 __ Xor(dst, lhs, rhs_reg);
1390 } else if (instruction->IsAdd()) {
1391 if (use_imm)
1392 __ Addiu(dst, lhs, rhs_imm);
1393 else
1394 __ Addu(dst, lhs, rhs_reg);
1395 } else {
1396 DCHECK(instruction->IsSub());
1397 if (use_imm)
1398 __ Addiu(dst, lhs, -rhs_imm);
1399 else
1400 __ Subu(dst, lhs, rhs_reg);
1401 }
1402 break;
1403 }
1404
1405 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001406 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1407 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1408 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1409 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001410 Location rhs_location = locations->InAt(1);
1411 bool use_imm = rhs_location.IsConstant();
1412 if (!use_imm) {
1413 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1414 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1415 if (instruction->IsAnd()) {
1416 __ And(dst_low, lhs_low, rhs_low);
1417 __ And(dst_high, lhs_high, rhs_high);
1418 } else if (instruction->IsOr()) {
1419 __ Or(dst_low, lhs_low, rhs_low);
1420 __ Or(dst_high, lhs_high, rhs_high);
1421 } else if (instruction->IsXor()) {
1422 __ Xor(dst_low, lhs_low, rhs_low);
1423 __ Xor(dst_high, lhs_high, rhs_high);
1424 } else if (instruction->IsAdd()) {
1425 if (lhs_low == rhs_low) {
1426 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1427 __ Slt(TMP, lhs_low, ZERO);
1428 __ Addu(dst_low, lhs_low, rhs_low);
1429 } else {
1430 __ Addu(dst_low, lhs_low, rhs_low);
1431 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1432 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1433 }
1434 __ Addu(dst_high, lhs_high, rhs_high);
1435 __ Addu(dst_high, dst_high, TMP);
1436 } else {
1437 DCHECK(instruction->IsSub());
1438 __ Sltu(TMP, lhs_low, rhs_low);
1439 __ Subu(dst_low, lhs_low, rhs_low);
1440 __ Subu(dst_high, lhs_high, rhs_high);
1441 __ Subu(dst_high, dst_high, TMP);
1442 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001443 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001444 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1445 if (instruction->IsOr()) {
1446 uint32_t low = Low32Bits(value);
1447 uint32_t high = High32Bits(value);
1448 if (IsUint<16>(low)) {
1449 if (dst_low != lhs_low || low != 0) {
1450 __ Ori(dst_low, lhs_low, low);
1451 }
1452 } else {
1453 __ LoadConst32(TMP, low);
1454 __ Or(dst_low, lhs_low, TMP);
1455 }
1456 if (IsUint<16>(high)) {
1457 if (dst_high != lhs_high || high != 0) {
1458 __ Ori(dst_high, lhs_high, high);
1459 }
1460 } else {
1461 if (high != low) {
1462 __ LoadConst32(TMP, high);
1463 }
1464 __ Or(dst_high, lhs_high, TMP);
1465 }
1466 } else if (instruction->IsXor()) {
1467 uint32_t low = Low32Bits(value);
1468 uint32_t high = High32Bits(value);
1469 if (IsUint<16>(low)) {
1470 if (dst_low != lhs_low || low != 0) {
1471 __ Xori(dst_low, lhs_low, low);
1472 }
1473 } else {
1474 __ LoadConst32(TMP, low);
1475 __ Xor(dst_low, lhs_low, TMP);
1476 }
1477 if (IsUint<16>(high)) {
1478 if (dst_high != lhs_high || high != 0) {
1479 __ Xori(dst_high, lhs_high, high);
1480 }
1481 } else {
1482 if (high != low) {
1483 __ LoadConst32(TMP, high);
1484 }
1485 __ Xor(dst_high, lhs_high, TMP);
1486 }
1487 } else if (instruction->IsAnd()) {
1488 uint32_t low = Low32Bits(value);
1489 uint32_t high = High32Bits(value);
1490 if (IsUint<16>(low)) {
1491 __ Andi(dst_low, lhs_low, low);
1492 } else if (low != 0xFFFFFFFF) {
1493 __ LoadConst32(TMP, low);
1494 __ And(dst_low, lhs_low, TMP);
1495 } else if (dst_low != lhs_low) {
1496 __ Move(dst_low, lhs_low);
1497 }
1498 if (IsUint<16>(high)) {
1499 __ Andi(dst_high, lhs_high, high);
1500 } else if (high != 0xFFFFFFFF) {
1501 if (high != low) {
1502 __ LoadConst32(TMP, high);
1503 }
1504 __ And(dst_high, lhs_high, TMP);
1505 } else if (dst_high != lhs_high) {
1506 __ Move(dst_high, lhs_high);
1507 }
1508 } else {
1509 if (instruction->IsSub()) {
1510 value = -value;
1511 } else {
1512 DCHECK(instruction->IsAdd());
1513 }
1514 int32_t low = Low32Bits(value);
1515 int32_t high = High32Bits(value);
1516 if (IsInt<16>(low)) {
1517 if (dst_low != lhs_low || low != 0) {
1518 __ Addiu(dst_low, lhs_low, low);
1519 }
1520 if (low != 0) {
1521 __ Sltiu(AT, dst_low, low);
1522 }
1523 } else {
1524 __ LoadConst32(TMP, low);
1525 __ Addu(dst_low, lhs_low, TMP);
1526 __ Sltu(AT, dst_low, TMP);
1527 }
1528 if (IsInt<16>(high)) {
1529 if (dst_high != lhs_high || high != 0) {
1530 __ Addiu(dst_high, lhs_high, high);
1531 }
1532 } else {
1533 if (high != low) {
1534 __ LoadConst32(TMP, high);
1535 }
1536 __ Addu(dst_high, lhs_high, TMP);
1537 }
1538 if (low != 0) {
1539 __ Addu(dst_high, dst_high, AT);
1540 }
1541 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001542 }
1543 break;
1544 }
1545
1546 case Primitive::kPrimFloat:
1547 case Primitive::kPrimDouble: {
1548 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1549 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1550 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1551 if (instruction->IsAdd()) {
1552 if (type == Primitive::kPrimFloat) {
1553 __ AddS(dst, lhs, rhs);
1554 } else {
1555 __ AddD(dst, lhs, rhs);
1556 }
1557 } else {
1558 DCHECK(instruction->IsSub());
1559 if (type == Primitive::kPrimFloat) {
1560 __ SubS(dst, lhs, rhs);
1561 } else {
1562 __ SubD(dst, lhs, rhs);
1563 }
1564 }
1565 break;
1566 }
1567
1568 default:
1569 LOG(FATAL) << "Unexpected binary operation type " << type;
1570 }
1571}
1572
1573void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001574 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001575
1576 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1577 Primitive::Type type = instr->GetResultType();
1578 switch (type) {
1579 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001580 locations->SetInAt(0, Location::RequiresRegister());
1581 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1583 break;
1584 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001585 locations->SetInAt(0, Location::RequiresRegister());
1586 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1587 locations->SetOut(Location::RequiresRegister());
1588 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001589 default:
1590 LOG(FATAL) << "Unexpected shift type " << type;
1591 }
1592}
1593
1594static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1595
1596void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001597 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001598 LocationSummary* locations = instr->GetLocations();
1599 Primitive::Type type = instr->GetType();
1600
1601 Location rhs_location = locations->InAt(1);
1602 bool use_imm = rhs_location.IsConstant();
1603 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1604 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001605 const uint32_t shift_mask =
1606 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001607 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001608 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1609 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001610
1611 switch (type) {
1612 case Primitive::kPrimInt: {
1613 Register dst = locations->Out().AsRegister<Register>();
1614 Register lhs = locations->InAt(0).AsRegister<Register>();
1615 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001616 if (shift_value == 0) {
1617 if (dst != lhs) {
1618 __ Move(dst, lhs);
1619 }
1620 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001621 __ Sll(dst, lhs, shift_value);
1622 } else if (instr->IsShr()) {
1623 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001624 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001625 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001626 } else {
1627 if (has_ins_rotr) {
1628 __ Rotr(dst, lhs, shift_value);
1629 } else {
1630 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1631 __ Srl(dst, lhs, shift_value);
1632 __ Or(dst, dst, TMP);
1633 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001634 }
1635 } else {
1636 if (instr->IsShl()) {
1637 __ Sllv(dst, lhs, rhs_reg);
1638 } else if (instr->IsShr()) {
1639 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001640 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001641 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001642 } else {
1643 if (has_ins_rotr) {
1644 __ Rotrv(dst, lhs, rhs_reg);
1645 } else {
1646 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001647 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1648 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1649 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1650 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1651 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001652 __ Sllv(TMP, lhs, TMP);
1653 __ Srlv(dst, lhs, rhs_reg);
1654 __ Or(dst, dst, TMP);
1655 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001656 }
1657 }
1658 break;
1659 }
1660
1661 case Primitive::kPrimLong: {
1662 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1663 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1664 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1665 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1666 if (use_imm) {
1667 if (shift_value == 0) {
1668 codegen_->Move64(locations->Out(), locations->InAt(0));
1669 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001670 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001671 if (instr->IsShl()) {
1672 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1673 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1674 __ Sll(dst_low, lhs_low, shift_value);
1675 } else if (instr->IsShr()) {
1676 __ Srl(dst_low, lhs_low, shift_value);
1677 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1678 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001679 } else if (instr->IsUShr()) {
1680 __ Srl(dst_low, lhs_low, shift_value);
1681 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1682 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001683 } else {
1684 __ Srl(dst_low, lhs_low, shift_value);
1685 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1686 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001687 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001688 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001689 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001690 if (instr->IsShl()) {
1691 __ Sll(dst_low, lhs_low, shift_value);
1692 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1693 __ Sll(dst_high, lhs_high, shift_value);
1694 __ Or(dst_high, dst_high, TMP);
1695 } else if (instr->IsShr()) {
1696 __ Sra(dst_high, lhs_high, shift_value);
1697 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1698 __ Srl(dst_low, lhs_low, shift_value);
1699 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001700 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001701 __ Srl(dst_high, lhs_high, shift_value);
1702 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1703 __ Srl(dst_low, lhs_low, shift_value);
1704 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 } else {
1706 __ Srl(TMP, lhs_low, shift_value);
1707 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1708 __ Or(dst_low, dst_low, TMP);
1709 __ Srl(TMP, lhs_high, shift_value);
1710 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1711 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001712 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001713 }
1714 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001715 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001716 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001717 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 __ Move(dst_low, ZERO);
1719 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001720 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001722 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001723 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001724 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001725 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001726 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001727 // 64-bit rotation by 32 is just a swap.
1728 __ Move(dst_low, lhs_high);
1729 __ Move(dst_high, lhs_low);
1730 } else {
1731 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001732 __ Srl(dst_low, lhs_high, shift_value_high);
1733 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1734 __ Srl(dst_high, lhs_low, shift_value_high);
1735 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001736 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001737 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1738 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001739 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001740 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1741 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001742 __ Or(dst_high, dst_high, TMP);
1743 }
1744 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001745 }
1746 }
1747 } else {
1748 MipsLabel done;
1749 if (instr->IsShl()) {
1750 __ Sllv(dst_low, lhs_low, rhs_reg);
1751 __ Nor(AT, ZERO, rhs_reg);
1752 __ Srl(TMP, lhs_low, 1);
1753 __ Srlv(TMP, TMP, AT);
1754 __ Sllv(dst_high, lhs_high, rhs_reg);
1755 __ Or(dst_high, dst_high, TMP);
1756 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1757 __ Beqz(TMP, &done);
1758 __ Move(dst_high, dst_low);
1759 __ Move(dst_low, ZERO);
1760 } else if (instr->IsShr()) {
1761 __ Srav(dst_high, lhs_high, rhs_reg);
1762 __ Nor(AT, ZERO, rhs_reg);
1763 __ Sll(TMP, lhs_high, 1);
1764 __ Sllv(TMP, TMP, AT);
1765 __ Srlv(dst_low, lhs_low, rhs_reg);
1766 __ Or(dst_low, dst_low, TMP);
1767 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1768 __ Beqz(TMP, &done);
1769 __ Move(dst_low, dst_high);
1770 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001771 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001772 __ Srlv(dst_high, lhs_high, rhs_reg);
1773 __ Nor(AT, ZERO, rhs_reg);
1774 __ Sll(TMP, lhs_high, 1);
1775 __ Sllv(TMP, TMP, AT);
1776 __ Srlv(dst_low, lhs_low, rhs_reg);
1777 __ Or(dst_low, dst_low, TMP);
1778 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1779 __ Beqz(TMP, &done);
1780 __ Move(dst_low, dst_high);
1781 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001782 } else {
1783 __ Nor(AT, ZERO, rhs_reg);
1784 __ Srlv(TMP, lhs_low, rhs_reg);
1785 __ Sll(dst_low, lhs_high, 1);
1786 __ Sllv(dst_low, dst_low, AT);
1787 __ Or(dst_low, dst_low, TMP);
1788 __ Srlv(TMP, lhs_high, rhs_reg);
1789 __ Sll(dst_high, lhs_low, 1);
1790 __ Sllv(dst_high, dst_high, AT);
1791 __ Or(dst_high, dst_high, TMP);
1792 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1793 __ Beqz(TMP, &done);
1794 __ Move(TMP, dst_high);
1795 __ Move(dst_high, dst_low);
1796 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001797 }
1798 __ Bind(&done);
1799 }
1800 break;
1801 }
1802
1803 default:
1804 LOG(FATAL) << "Unexpected shift operation type " << type;
1805 }
1806}
1807
1808void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1809 HandleBinaryOp(instruction);
1810}
1811
1812void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1813 HandleBinaryOp(instruction);
1814}
1815
1816void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1817 HandleBinaryOp(instruction);
1818}
1819
1820void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1821 HandleBinaryOp(instruction);
1822}
1823
1824void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1825 LocationSummary* locations =
1826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1827 locations->SetInAt(0, Location::RequiresRegister());
1828 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1829 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1830 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1831 } else {
1832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1833 }
1834}
1835
Alexey Frunze2923db72016-08-20 01:55:47 -07001836auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1837 auto null_checker = [this, instruction]() {
1838 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1839 };
1840 return null_checker;
1841}
1842
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001843void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1844 LocationSummary* locations = instruction->GetLocations();
1845 Register obj = locations->InAt(0).AsRegister<Register>();
1846 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001847 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001848 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001849
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001850 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001851 switch (type) {
1852 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001853 Register out = locations->Out().AsRegister<Register>();
1854 if (index.IsConstant()) {
1855 size_t offset =
1856 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001857 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 } else {
1859 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001860 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 }
1862 break;
1863 }
1864
1865 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 Register out = locations->Out().AsRegister<Register>();
1867 if (index.IsConstant()) {
1868 size_t offset =
1869 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001870 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 } else {
1872 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001873 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001874 }
1875 break;
1876 }
1877
1878 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879 Register out = locations->Out().AsRegister<Register>();
1880 if (index.IsConstant()) {
1881 size_t offset =
1882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001883 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884 } else {
1885 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1886 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001887 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001888 }
1889 break;
1890 }
1891
1892 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893 Register out = locations->Out().AsRegister<Register>();
1894 if (index.IsConstant()) {
1895 size_t offset =
1896 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001897 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001898 } else {
1899 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1900 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001901 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001902 }
1903 break;
1904 }
1905
1906 case Primitive::kPrimInt:
1907 case Primitive::kPrimNot: {
1908 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909 Register out = locations->Out().AsRegister<Register>();
1910 if (index.IsConstant()) {
1911 size_t offset =
1912 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001913 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914 } else {
1915 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1916 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001917 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001918 }
1919 break;
1920 }
1921
1922 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923 Register out = locations->Out().AsRegisterPairLow<Register>();
1924 if (index.IsConstant()) {
1925 size_t offset =
1926 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001927 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001928 } else {
1929 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1930 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001931 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001932 }
1933 break;
1934 }
1935
1936 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1938 if (index.IsConstant()) {
1939 size_t offset =
1940 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001941 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942 } else {
1943 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1944 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001945 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946 }
1947 break;
1948 }
1949
1950 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1952 if (index.IsConstant()) {
1953 size_t offset =
1954 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001955 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 } else {
1957 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1958 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001959 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001960 }
1961 break;
1962 }
1963
1964 case Primitive::kPrimVoid:
1965 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1966 UNREACHABLE();
1967 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001968}
1969
1970void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1972 locations->SetInAt(0, Location::RequiresRegister());
1973 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1974}
1975
1976void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1977 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001978 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001979 Register obj = locations->InAt(0).AsRegister<Register>();
1980 Register out = locations->Out().AsRegister<Register>();
1981 __ LoadFromOffset(kLoadWord, out, obj, offset);
1982 codegen_->MaybeRecordImplicitNullCheck(instruction);
1983}
1984
1985void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001986 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1988 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001989 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001990 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001991 InvokeRuntimeCallingConvention calling_convention;
1992 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1993 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1994 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1995 } else {
1996 locations->SetInAt(0, Location::RequiresRegister());
1997 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1998 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1999 locations->SetInAt(2, Location::RequiresFpuRegister());
2000 } else {
2001 locations->SetInAt(2, Location::RequiresRegister());
2002 }
2003 }
2004}
2005
2006void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2007 LocationSummary* locations = instruction->GetLocations();
2008 Register obj = locations->InAt(0).AsRegister<Register>();
2009 Location index = locations->InAt(1);
2010 Primitive::Type value_type = instruction->GetComponentType();
2011 bool needs_runtime_call = locations->WillCall();
2012 bool needs_write_barrier =
2013 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002014 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002015
2016 switch (value_type) {
2017 case Primitive::kPrimBoolean:
2018 case Primitive::kPrimByte: {
2019 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
2020 Register value = locations->InAt(2).AsRegister<Register>();
2021 if (index.IsConstant()) {
2022 size_t offset =
2023 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002024 __ StoreToOffset(kStoreByte, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002025 } else {
2026 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002027 __ StoreToOffset(kStoreByte, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002028 }
2029 break;
2030 }
2031
2032 case Primitive::kPrimShort:
2033 case Primitive::kPrimChar: {
2034 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
2035 Register value = locations->InAt(2).AsRegister<Register>();
2036 if (index.IsConstant()) {
2037 size_t offset =
2038 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002039 __ StoreToOffset(kStoreHalfword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002040 } else {
2041 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
2042 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002043 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044 }
2045 break;
2046 }
2047
2048 case Primitive::kPrimInt:
2049 case Primitive::kPrimNot: {
2050 if (!needs_runtime_call) {
2051 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2052 Register value = locations->InAt(2).AsRegister<Register>();
2053 if (index.IsConstant()) {
2054 size_t offset =
2055 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002056 __ StoreToOffset(kStoreWord, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002057 } else {
2058 DCHECK(index.IsRegister()) << index;
2059 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2060 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002061 __ StoreToOffset(kStoreWord, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002062 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 if (needs_write_barrier) {
2064 DCHECK_EQ(value_type, Primitive::kPrimNot);
2065 codegen_->MarkGCCard(obj, value);
2066 }
2067 } else {
2068 DCHECK_EQ(value_type, Primitive::kPrimNot);
2069 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
2070 instruction,
2071 instruction->GetDexPc(),
2072 nullptr,
2073 IsDirectEntrypoint(kQuickAputObject));
2074 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2075 }
2076 break;
2077 }
2078
2079 case Primitive::kPrimLong: {
2080 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2081 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2082 if (index.IsConstant()) {
2083 size_t offset =
2084 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002085 __ StoreToOffset(kStoreDoubleword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 } else {
2087 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2088 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002089 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002090 }
2091 break;
2092 }
2093
2094 case Primitive::kPrimFloat: {
2095 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2096 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2097 DCHECK(locations->InAt(2).IsFpuRegister());
2098 if (index.IsConstant()) {
2099 size_t offset =
2100 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002101 __ StoreSToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002102 } else {
2103 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2104 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002105 __ StoreSToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002106 }
2107 break;
2108 }
2109
2110 case Primitive::kPrimDouble: {
2111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2112 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2113 DCHECK(locations->InAt(2).IsFpuRegister());
2114 if (index.IsConstant()) {
2115 size_t offset =
2116 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002117 __ StoreDToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118 } else {
2119 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2120 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002121 __ StoreDToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002122 }
2123 break;
2124 }
2125
2126 case Primitive::kPrimVoid:
2127 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2128 UNREACHABLE();
2129 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130}
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();
Alexey Frunze2923db72016-08-20 01:55:47 -07003594 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595
3596 switch (type) {
3597 case Primitive::kPrimBoolean:
3598 load_type = kLoadUnsignedByte;
3599 break;
3600 case Primitive::kPrimByte:
3601 load_type = kLoadSignedByte;
3602 break;
3603 case Primitive::kPrimShort:
3604 load_type = kLoadSignedHalfword;
3605 break;
3606 case Primitive::kPrimChar:
3607 load_type = kLoadUnsignedHalfword;
3608 break;
3609 case Primitive::kPrimInt:
3610 case Primitive::kPrimFloat:
3611 case Primitive::kPrimNot:
3612 load_type = kLoadWord;
3613 break;
3614 case Primitive::kPrimLong:
3615 case Primitive::kPrimDouble:
3616 load_type = kLoadDoubleword;
3617 break;
3618 case Primitive::kPrimVoid:
3619 LOG(FATAL) << "Unreachable type " << type;
3620 UNREACHABLE();
3621 }
3622
3623 if (is_volatile && load_type == kLoadDoubleword) {
3624 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003625 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003626 // Do implicit Null check
3627 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3628 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3629 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3630 instruction,
3631 dex_pc,
3632 nullptr,
3633 IsDirectEntrypoint(kQuickA64Load));
3634 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3635 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003636 // FP results are returned in core registers. Need to move them.
3637 Location out = locations->Out();
3638 if (out.IsFpuRegister()) {
3639 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3640 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3641 out.AsFpuRegister<FRegister>());
3642 } else {
3643 DCHECK(out.IsDoubleStackSlot());
3644 __ StoreToOffset(kStoreWord,
3645 locations->GetTemp(1).AsRegister<Register>(),
3646 SP,
3647 out.GetStackIndex());
3648 __ StoreToOffset(kStoreWord,
3649 locations->GetTemp(2).AsRegister<Register>(),
3650 SP,
3651 out.GetStackIndex() + 4);
3652 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003653 }
3654 } else {
3655 if (!Primitive::IsFloatingPointType(type)) {
3656 Register dst;
3657 if (type == Primitive::kPrimLong) {
3658 DCHECK(locations->Out().IsRegisterPair());
3659 dst = locations->Out().AsRegisterPairLow<Register>();
3660 } else {
3661 DCHECK(locations->Out().IsRegister());
3662 dst = locations->Out().AsRegister<Register>();
3663 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003664 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003665 } else {
3666 DCHECK(locations->Out().IsFpuRegister());
3667 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3668 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003669 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003670 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003671 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003672 }
3673 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003674 }
3675
3676 if (is_volatile) {
3677 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3678 }
3679}
3680
3681void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3682 Primitive::Type field_type = field_info.GetFieldType();
3683 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3684 bool generate_volatile = field_info.IsVolatile() && is_wide;
3685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003686 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003687
3688 locations->SetInAt(0, Location::RequiresRegister());
3689 if (generate_volatile) {
3690 InvokeRuntimeCallingConvention calling_convention;
3691 // need A0 to hold base + offset
3692 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3693 if (field_type == Primitive::kPrimLong) {
3694 locations->SetInAt(1, Location::RegisterPairLocation(
3695 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3696 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003697 // Use Location::Any() to prevent situations when running out of available fp registers.
3698 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003699 // Pass FP parameters in core registers.
3700 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3701 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3702 }
3703 } else {
3704 if (Primitive::IsFloatingPointType(field_type)) {
3705 locations->SetInAt(1, Location::RequiresFpuRegister());
3706 } else {
3707 locations->SetInAt(1, Location::RequiresRegister());
3708 }
3709 }
3710}
3711
3712void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3713 const FieldInfo& field_info,
3714 uint32_t dex_pc) {
3715 Primitive::Type type = field_info.GetFieldType();
3716 LocationSummary* locations = instruction->GetLocations();
3717 Register obj = locations->InAt(0).AsRegister<Register>();
3718 StoreOperandType store_type = kStoreByte;
3719 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003720 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003721 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003722
3723 switch (type) {
3724 case Primitive::kPrimBoolean:
3725 case Primitive::kPrimByte:
3726 store_type = kStoreByte;
3727 break;
3728 case Primitive::kPrimShort:
3729 case Primitive::kPrimChar:
3730 store_type = kStoreHalfword;
3731 break;
3732 case Primitive::kPrimInt:
3733 case Primitive::kPrimFloat:
3734 case Primitive::kPrimNot:
3735 store_type = kStoreWord;
3736 break;
3737 case Primitive::kPrimLong:
3738 case Primitive::kPrimDouble:
3739 store_type = kStoreDoubleword;
3740 break;
3741 case Primitive::kPrimVoid:
3742 LOG(FATAL) << "Unreachable type " << type;
3743 UNREACHABLE();
3744 }
3745
3746 if (is_volatile) {
3747 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3748 }
3749
3750 if (is_volatile && store_type == kStoreDoubleword) {
3751 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003752 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003753 // Do implicit Null check.
3754 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3755 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3756 if (type == Primitive::kPrimDouble) {
3757 // Pass FP parameters in core registers.
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003758 Location in = locations->InAt(1);
3759 if (in.IsFpuRegister()) {
3760 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(), in.AsFpuRegister<FRegister>());
3761 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3762 in.AsFpuRegister<FRegister>());
3763 } else if (in.IsDoubleStackSlot()) {
3764 __ LoadFromOffset(kLoadWord,
3765 locations->GetTemp(1).AsRegister<Register>(),
3766 SP,
3767 in.GetStackIndex());
3768 __ LoadFromOffset(kLoadWord,
3769 locations->GetTemp(2).AsRegister<Register>(),
3770 SP,
3771 in.GetStackIndex() + 4);
3772 } else {
3773 DCHECK(in.IsConstant());
3774 DCHECK(in.GetConstant()->IsDoubleConstant());
3775 int64_t value = bit_cast<int64_t, double>(in.GetConstant()->AsDoubleConstant()->GetValue());
3776 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
3777 locations->GetTemp(1).AsRegister<Register>(),
3778 value);
3779 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003780 }
3781 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3782 instruction,
3783 dex_pc,
3784 nullptr,
3785 IsDirectEntrypoint(kQuickA64Store));
3786 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3787 } else {
3788 if (!Primitive::IsFloatingPointType(type)) {
3789 Register src;
3790 if (type == Primitive::kPrimLong) {
3791 DCHECK(locations->InAt(1).IsRegisterPair());
3792 src = locations->InAt(1).AsRegisterPairLow<Register>();
3793 } else {
3794 DCHECK(locations->InAt(1).IsRegister());
3795 src = locations->InAt(1).AsRegister<Register>();
3796 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003797 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003798 } else {
3799 DCHECK(locations->InAt(1).IsFpuRegister());
3800 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3801 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003802 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003803 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003804 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003805 }
3806 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003807 }
3808
3809 // TODO: memory barriers?
3810 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3811 DCHECK(locations->InAt(1).IsRegister());
3812 Register src = locations->InAt(1).AsRegister<Register>();
3813 codegen_->MarkGCCard(obj, src);
3814 }
3815
3816 if (is_volatile) {
3817 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3818 }
3819}
3820
3821void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3822 HandleFieldGet(instruction, instruction->GetFieldInfo());
3823}
3824
3825void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3826 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3827}
3828
3829void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3830 HandleFieldSet(instruction, instruction->GetFieldInfo());
3831}
3832
3833void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3834 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3835}
3836
Alexey Frunze06a46c42016-07-19 15:00:40 -07003837void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
3838 HInstruction* instruction ATTRIBUTE_UNUSED,
3839 Location root,
3840 Register obj,
3841 uint32_t offset) {
3842 Register root_reg = root.AsRegister<Register>();
3843 if (kEmitCompilerReadBarrier) {
3844 UNIMPLEMENTED(FATAL) << "for read barrier";
3845 } else {
3846 // Plain GC root load with no read barrier.
3847 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3848 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
3849 // Note that GC roots are not affected by heap poisoning, thus we
3850 // do not have to unpoison `root_reg` here.
3851 }
3852}
3853
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003854void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3855 LocationSummary::CallKind call_kind =
3856 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3858 locations->SetInAt(0, Location::RequiresRegister());
3859 locations->SetInAt(1, Location::RequiresRegister());
3860 // The output does overlap inputs.
3861 // Note that TypeCheckSlowPathMIPS uses this register too.
3862 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3863}
3864
3865void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3866 LocationSummary* locations = instruction->GetLocations();
3867 Register obj = locations->InAt(0).AsRegister<Register>();
3868 Register cls = locations->InAt(1).AsRegister<Register>();
3869 Register out = locations->Out().AsRegister<Register>();
3870
3871 MipsLabel done;
3872
3873 // Return 0 if `obj` is null.
3874 // TODO: Avoid this check if we know `obj` is not null.
3875 __ Move(out, ZERO);
3876 __ Beqz(obj, &done);
3877
3878 // Compare the class of `obj` with `cls`.
3879 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3880 if (instruction->IsExactCheck()) {
3881 // Classes must be equal for the instanceof to succeed.
3882 __ Xor(out, out, cls);
3883 __ Sltiu(out, out, 1);
3884 } else {
3885 // If the classes are not equal, we go into a slow path.
3886 DCHECK(locations->OnlyCallsOnSlowPath());
3887 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3888 codegen_->AddSlowPath(slow_path);
3889 __ Bne(out, cls, slow_path->GetEntryLabel());
3890 __ LoadConst32(out, 1);
3891 __ Bind(slow_path->GetExitLabel());
3892 }
3893
3894 __ Bind(&done);
3895}
3896
3897void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3899 locations->SetOut(Location::ConstantLocation(constant));
3900}
3901
3902void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3903 // Will be generated at use site.
3904}
3905
3906void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3907 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3908 locations->SetOut(Location::ConstantLocation(constant));
3909}
3910
3911void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3912 // Will be generated at use site.
3913}
3914
3915void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3916 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3917 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3918}
3919
3920void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3921 HandleInvoke(invoke);
3922 // The register T0 is required to be used for the hidden argument in
3923 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3924 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3925}
3926
3927void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3928 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3929 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003930 Location receiver = invoke->GetLocations()->InAt(0);
3931 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003932 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003933
3934 // Set the hidden argument.
3935 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3936 invoke->GetDexMethodIndex());
3937
3938 // temp = object->GetClass();
3939 if (receiver.IsStackSlot()) {
3940 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3941 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3942 } else {
3943 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3944 }
3945 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003946 __ LoadFromOffset(kLoadWord, temp, temp,
3947 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3948 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003949 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003950 // temp = temp->GetImtEntryAt(method_offset);
3951 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3952 // T9 = temp->GetEntryPoint();
3953 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3954 // T9();
3955 __ Jalr(T9);
3956 __ Nop();
3957 DCHECK(!codegen_->IsLeafMethod());
3958 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3959}
3960
3961void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003962 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3963 if (intrinsic.TryDispatch(invoke)) {
3964 return;
3965 }
3966
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003967 HandleInvoke(invoke);
3968}
3969
3970void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003971 // Explicit clinit checks triggered by static invokes must have been pruned by
3972 // art::PrepareForRegisterAllocation.
3973 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003974
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003975 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3976 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3977 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3978
3979 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
3980 // R6 has PC-relative addressing.
3981 bool has_extra_input = !isR6 &&
3982 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3983 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
3984
3985 if (invoke->HasPcRelativeDexCache()) {
3986 // kDexCachePcRelative is mutually exclusive with
3987 // kDirectAddressWithFixup/kCallDirectWithFixup.
3988 CHECK(!has_extra_input);
3989 has_extra_input = true;
3990 }
3991
Chris Larsen701566a2015-10-27 15:29:13 -07003992 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3993 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003994 if (invoke->GetLocations()->CanCall() && has_extra_input) {
3995 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3996 }
Chris Larsen701566a2015-10-27 15:29:13 -07003997 return;
3998 }
3999
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004000 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004001
4002 // Add the extra input register if either the dex cache array base register
4003 // or the PC-relative base register for accessing literals is needed.
4004 if (has_extra_input) {
4005 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4006 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004007}
4008
Chris Larsen701566a2015-10-27 15:29:13 -07004009static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004010 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004011 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4012 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004013 return true;
4014 }
4015 return false;
4016}
4017
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004018HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004019 HLoadString::LoadKind desired_string_load_kind) {
4020 if (kEmitCompilerReadBarrier) {
4021 UNIMPLEMENTED(FATAL) << "for read barrier";
4022 }
4023 // We disable PC-relative load when there is an irreducible loop, as the optimization
4024 // is incompatible with it.
4025 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4026 bool fallback_load = has_irreducible_loops;
4027 switch (desired_string_load_kind) {
4028 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4029 DCHECK(!GetCompilerOptions().GetCompilePic());
4030 break;
4031 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4032 DCHECK(GetCompilerOptions().GetCompilePic());
4033 break;
4034 case HLoadString::LoadKind::kBootImageAddress:
4035 break;
4036 case HLoadString::LoadKind::kDexCacheAddress:
4037 DCHECK(Runtime::Current()->UseJitCompilation());
4038 fallback_load = false;
4039 break;
4040 case HLoadString::LoadKind::kDexCachePcRelative:
4041 DCHECK(!Runtime::Current()->UseJitCompilation());
4042 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4043 // with irreducible loops.
4044 break;
4045 case HLoadString::LoadKind::kDexCacheViaMethod:
4046 fallback_load = false;
4047 break;
4048 }
4049 if (fallback_load) {
4050 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4051 }
4052 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004053}
4054
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004055HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4056 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004057 if (kEmitCompilerReadBarrier) {
4058 UNIMPLEMENTED(FATAL) << "for read barrier";
4059 }
4060 // We disable pc-relative load when there is an irreducible loop, as the optimization
4061 // is incompatible with it.
4062 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4063 bool fallback_load = has_irreducible_loops;
4064 switch (desired_class_load_kind) {
4065 case HLoadClass::LoadKind::kReferrersClass:
4066 fallback_load = false;
4067 break;
4068 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4069 DCHECK(!GetCompilerOptions().GetCompilePic());
4070 break;
4071 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4072 DCHECK(GetCompilerOptions().GetCompilePic());
4073 break;
4074 case HLoadClass::LoadKind::kBootImageAddress:
4075 break;
4076 case HLoadClass::LoadKind::kDexCacheAddress:
4077 DCHECK(Runtime::Current()->UseJitCompilation());
4078 fallback_load = false;
4079 break;
4080 case HLoadClass::LoadKind::kDexCachePcRelative:
4081 DCHECK(!Runtime::Current()->UseJitCompilation());
4082 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4083 // with irreducible loops.
4084 break;
4085 case HLoadClass::LoadKind::kDexCacheViaMethod:
4086 fallback_load = false;
4087 break;
4088 }
4089 if (fallback_load) {
4090 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4091 }
4092 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004093}
4094
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004095Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4096 Register temp) {
4097 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4098 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4099 if (!invoke->GetLocations()->Intrinsified()) {
4100 return location.AsRegister<Register>();
4101 }
4102 // For intrinsics we allow any location, so it may be on the stack.
4103 if (!location.IsRegister()) {
4104 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4105 return temp;
4106 }
4107 // For register locations, check if the register was saved. If so, get it from the stack.
4108 // Note: There is a chance that the register was saved but not overwritten, so we could
4109 // save one load. However, since this is just an intrinsic slow path we prefer this
4110 // simple and more robust approach rather that trying to determine if that's the case.
4111 SlowPathCode* slow_path = GetCurrentSlowPath();
4112 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4113 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4114 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4115 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4116 return temp;
4117 }
4118 return location.AsRegister<Register>();
4119}
4120
Vladimir Markodc151b22015-10-15 18:02:30 +01004121HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4122 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4123 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004124 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4125 // We disable PC-relative load when there is an irreducible loop, as the optimization
4126 // is incompatible with it.
4127 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4128 bool fallback_load = true;
4129 bool fallback_call = true;
4130 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004131 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4132 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004133 fallback_load = has_irreducible_loops;
4134 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004135 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004136 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004137 break;
4138 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004139 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004140 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004141 fallback_call = has_irreducible_loops;
4142 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004143 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004144 // TODO: Implement this type.
4145 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004146 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004147 fallback_call = false;
4148 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004149 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004150 if (fallback_load) {
4151 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4152 dispatch_info.method_load_data = 0;
4153 }
4154 if (fallback_call) {
4155 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4156 dispatch_info.direct_code_ptr = 0;
4157 }
4158 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004159}
4160
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004161void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4162 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004163 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004164 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4165 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4166 bool isR6 = isa_features_.IsR6();
4167 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4168 // R6 has PC-relative addressing.
4169 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4170 (!isR6 &&
4171 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4172 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4173 Register base_reg = has_extra_input
4174 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4175 : ZERO;
4176
4177 // For better instruction scheduling we load the direct code pointer before the method pointer.
4178 switch (code_ptr_location) {
4179 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4180 // T9 = invoke->GetDirectCodePtr();
4181 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4182 break;
4183 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4184 // T9 = code address from literal pool with link-time patch.
4185 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4186 break;
4187 default:
4188 break;
4189 }
4190
4191 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004192 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4193 // temp = thread->string_init_entrypoint
4194 __ LoadFromOffset(kLoadWord,
4195 temp.AsRegister<Register>(),
4196 TR,
4197 invoke->GetStringInitOffset());
4198 break;
4199 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004200 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004201 break;
4202 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4203 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4204 break;
4205 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004206 __ LoadLiteral(temp.AsRegister<Register>(),
4207 base_reg,
4208 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4209 break;
4210 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4211 HMipsDexCacheArraysBase* base =
4212 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4213 int32_t offset =
4214 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4215 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4216 break;
4217 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004218 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004219 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004220 Register reg = temp.AsRegister<Register>();
4221 Register method_reg;
4222 if (current_method.IsRegister()) {
4223 method_reg = current_method.AsRegister<Register>();
4224 } else {
4225 // TODO: use the appropriate DCHECK() here if possible.
4226 // DCHECK(invoke->GetLocations()->Intrinsified());
4227 DCHECK(!current_method.IsValid());
4228 method_reg = reg;
4229 __ Lw(reg, SP, kCurrentMethodStackOffset);
4230 }
4231
4232 // temp = temp->dex_cache_resolved_methods_;
4233 __ LoadFromOffset(kLoadWord,
4234 reg,
4235 method_reg,
4236 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004237 // temp = temp[index_in_cache];
4238 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4239 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004240 __ LoadFromOffset(kLoadWord,
4241 reg,
4242 reg,
4243 CodeGenerator::GetCachePointerOffset(index_in_cache));
4244 break;
4245 }
4246 }
4247
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004248 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004249 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004250 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004251 break;
4252 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004253 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4254 // T9 prepared above for better instruction scheduling.
4255 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004256 __ Jalr(T9);
4257 __ Nop();
4258 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004259 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004260 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004261 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4262 LOG(FATAL) << "Unsupported";
4263 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004264 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4265 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004266 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004267 T9,
4268 callee_method.AsRegister<Register>(),
4269 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004270 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004271 // T9()
4272 __ Jalr(T9);
4273 __ Nop();
4274 break;
4275 }
4276 DCHECK(!IsLeafMethod());
4277}
4278
4279void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004280 // Explicit clinit checks triggered by static invokes must have been pruned by
4281 // art::PrepareForRegisterAllocation.
4282 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004283
4284 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4285 return;
4286 }
4287
4288 LocationSummary* locations = invoke->GetLocations();
4289 codegen_->GenerateStaticOrDirectCall(invoke,
4290 locations->HasTemps()
4291 ? locations->GetTemp(0)
4292 : Location::NoLocation());
4293 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4294}
4295
Chris Larsen3acee732015-11-18 13:31:08 -08004296void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004297 LocationSummary* locations = invoke->GetLocations();
4298 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004299 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004300 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4301 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4302 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004303 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004304
4305 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004306 DCHECK(receiver.IsRegister());
4307 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4308 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004309 // temp = temp->GetMethodAt(method_offset);
4310 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4311 // T9 = temp->GetEntryPoint();
4312 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4313 // T9();
4314 __ Jalr(T9);
4315 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08004316}
4317
4318void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4319 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4320 return;
4321 }
4322
4323 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004324 DCHECK(!codegen_->IsLeafMethod());
4325 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4326}
4327
4328void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004329 if (cls->NeedsAccessCheck()) {
4330 InvokeRuntimeCallingConvention calling_convention;
4331 CodeGenerator::CreateLoadClassLocationSummary(
4332 cls,
4333 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4334 Location::RegisterLocation(V0),
4335 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4336 return;
4337 }
4338
4339 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4340 ? LocationSummary::kCallOnSlowPath
4341 : LocationSummary::kNoCall;
4342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4343 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4344 switch (load_kind) {
4345 // We need an extra register for PC-relative literals on R2.
4346 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4347 case HLoadClass::LoadKind::kBootImageAddress:
4348 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4349 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4350 break;
4351 }
4352 FALLTHROUGH_INTENDED;
4353 // We need an extra register for PC-relative dex cache accesses.
4354 case HLoadClass::LoadKind::kDexCachePcRelative:
4355 case HLoadClass::LoadKind::kReferrersClass:
4356 case HLoadClass::LoadKind::kDexCacheViaMethod:
4357 locations->SetInAt(0, Location::RequiresRegister());
4358 break;
4359 default:
4360 break;
4361 }
4362 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004363}
4364
4365void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4366 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004367 if (cls->NeedsAccessCheck()) {
4368 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4369 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4370 cls,
4371 cls->GetDexPc(),
4372 nullptr,
4373 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004374 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004375 return;
4376 }
4377
Alexey Frunze06a46c42016-07-19 15:00:40 -07004378 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4379 Location out_loc = locations->Out();
4380 Register out = out_loc.AsRegister<Register>();
4381 Register base_or_current_method_reg;
4382 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4383 switch (load_kind) {
4384 // We need an extra register for PC-relative literals on R2.
4385 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4386 case HLoadClass::LoadKind::kBootImageAddress:
4387 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4388 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4389 break;
4390 // We need an extra register for PC-relative dex cache accesses.
4391 case HLoadClass::LoadKind::kDexCachePcRelative:
4392 case HLoadClass::LoadKind::kReferrersClass:
4393 case HLoadClass::LoadKind::kDexCacheViaMethod:
4394 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4395 break;
4396 default:
4397 base_or_current_method_reg = ZERO;
4398 break;
4399 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004400
Alexey Frunze06a46c42016-07-19 15:00:40 -07004401 bool generate_null_check = false;
4402 switch (load_kind) {
4403 case HLoadClass::LoadKind::kReferrersClass: {
4404 DCHECK(!cls->CanCallRuntime());
4405 DCHECK(!cls->MustGenerateClinitCheck());
4406 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4407 GenerateGcRootFieldLoad(cls,
4408 out_loc,
4409 base_or_current_method_reg,
4410 ArtMethod::DeclaringClassOffset().Int32Value());
4411 break;
4412 }
4413 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4414 DCHECK(!kEmitCompilerReadBarrier);
4415 __ LoadLiteral(out,
4416 base_or_current_method_reg,
4417 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4418 cls->GetTypeIndex()));
4419 break;
4420 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4421 DCHECK(!kEmitCompilerReadBarrier);
4422 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4423 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
4424 if (isR6) {
4425 __ Bind(&info->high_label);
4426 __ Bind(&info->pc_rel_label);
4427 // Add a 32-bit offset to PC.
4428 __ Auipc(out, /* placeholder */ 0x1234);
4429 __ Addiu(out, out, /* placeholder */ 0x5678);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004430 } else {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004431 __ Bind(&info->high_label);
4432 __ Lui(out, /* placeholder */ 0x1234);
4433 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4434 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4435 __ Ori(out, out, /* placeholder */ 0x5678);
4436 // Add a 32-bit offset to PC.
4437 __ Addu(out, out, base_or_current_method_reg);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004438 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004439 break;
4440 }
4441 case HLoadClass::LoadKind::kBootImageAddress: {
4442 DCHECK(!kEmitCompilerReadBarrier);
4443 DCHECK_NE(cls->GetAddress(), 0u);
4444 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4445 __ LoadLiteral(out,
4446 base_or_current_method_reg,
4447 codegen_->DeduplicateBootImageAddressLiteral(address));
4448 break;
4449 }
4450 case HLoadClass::LoadKind::kDexCacheAddress: {
4451 DCHECK_NE(cls->GetAddress(), 0u);
4452 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4453 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4454 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4455 int16_t offset = Low16Bits(address);
4456 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4457 __ Lui(out, High16Bits(base_address));
4458 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4459 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4460 generate_null_check = !cls->IsInDexCache();
4461 break;
4462 }
4463 case HLoadClass::LoadKind::kDexCachePcRelative: {
4464 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4465 int32_t offset =
4466 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4467 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4468 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4469 generate_null_check = !cls->IsInDexCache();
4470 break;
4471 }
4472 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4473 // /* GcRoot<mirror::Class>[] */ out =
4474 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4475 __ LoadFromOffset(kLoadWord,
4476 out,
4477 base_or_current_method_reg,
4478 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4479 // /* GcRoot<mirror::Class> */ out = out[type_index]
4480 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4481 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4482 generate_null_check = !cls->IsInDexCache();
4483 }
4484 }
4485
4486 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4487 DCHECK(cls->CanCallRuntime());
4488 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4489 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4490 codegen_->AddSlowPath(slow_path);
4491 if (generate_null_check) {
4492 __ Beqz(out, slow_path->GetEntryLabel());
4493 }
4494 if (cls->MustGenerateClinitCheck()) {
4495 GenerateClassInitializationCheck(slow_path, out);
4496 } else {
4497 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004498 }
4499 }
4500}
4501
4502static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004503 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004504}
4505
4506void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4507 LocationSummary* locations =
4508 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4509 locations->SetOut(Location::RequiresRegister());
4510}
4511
4512void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4513 Register out = load->GetLocations()->Out().AsRegister<Register>();
4514 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4515}
4516
4517void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4518 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4519}
4520
4521void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4522 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4523}
4524
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004525void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004526 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004527 ? LocationSummary::kCallOnSlowPath
4528 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004530 HLoadString::LoadKind load_kind = load->GetLoadKind();
4531 switch (load_kind) {
4532 // We need an extra register for PC-relative literals on R2.
4533 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4534 case HLoadString::LoadKind::kBootImageAddress:
4535 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4536 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4537 break;
4538 }
4539 FALLTHROUGH_INTENDED;
4540 // We need an extra register for PC-relative dex cache accesses.
4541 case HLoadString::LoadKind::kDexCachePcRelative:
4542 case HLoadString::LoadKind::kDexCacheViaMethod:
4543 locations->SetInAt(0, Location::RequiresRegister());
4544 break;
4545 default:
4546 break;
4547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004548 locations->SetOut(Location::RequiresRegister());
4549}
4550
4551void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004552 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004553 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004554 Location out_loc = locations->Out();
4555 Register out = out_loc.AsRegister<Register>();
4556 Register base_or_current_method_reg;
4557 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4558 switch (load_kind) {
4559 // We need an extra register for PC-relative literals on R2.
4560 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4561 case HLoadString::LoadKind::kBootImageAddress:
4562 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4563 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4564 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004565 default:
4566 base_or_current_method_reg = ZERO;
4567 break;
4568 }
4569
4570 switch (load_kind) {
4571 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4572 DCHECK(!kEmitCompilerReadBarrier);
4573 __ LoadLiteral(out,
4574 base_or_current_method_reg,
4575 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4576 load->GetStringIndex()));
4577 return; // No dex cache slow path.
4578 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4579 DCHECK(!kEmitCompilerReadBarrier);
4580 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4581 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
4582 if (isR6) {
4583 __ Bind(&info->high_label);
4584 __ Bind(&info->pc_rel_label);
4585 // Add a 32-bit offset to PC.
4586 __ Auipc(out, /* placeholder */ 0x1234);
4587 __ Addiu(out, out, /* placeholder */ 0x5678);
4588 } else {
4589 __ Bind(&info->high_label);
4590 __ Lui(out, /* placeholder */ 0x1234);
4591 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4592 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4593 __ Ori(out, out, /* placeholder */ 0x5678);
4594 // Add a 32-bit offset to PC.
4595 __ Addu(out, out, base_or_current_method_reg);
4596 }
4597 return; // No dex cache slow path.
4598 }
4599 case HLoadString::LoadKind::kBootImageAddress: {
4600 DCHECK(!kEmitCompilerReadBarrier);
4601 DCHECK_NE(load->GetAddress(), 0u);
4602 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4603 __ LoadLiteral(out,
4604 base_or_current_method_reg,
4605 codegen_->DeduplicateBootImageAddressLiteral(address));
4606 return; // No dex cache slow path.
4607 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004608 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004609 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004610 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004611
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004612 // TODO: Re-add the compiler code to do string dex cache lookup again.
4613 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4614 codegen_->AddSlowPath(slow_path);
4615 __ B(slow_path->GetEntryLabel());
4616 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004617}
4618
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004619void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4621 locations->SetOut(Location::ConstantLocation(constant));
4622}
4623
4624void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4625 // Will be generated at use site.
4626}
4627
4628void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4629 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004630 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004631 InvokeRuntimeCallingConvention calling_convention;
4632 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4633}
4634
4635void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4636 if (instruction->IsEnter()) {
4637 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4638 instruction,
4639 instruction->GetDexPc(),
4640 nullptr,
4641 IsDirectEntrypoint(kQuickLockObject));
4642 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4643 } else {
4644 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4645 instruction,
4646 instruction->GetDexPc(),
4647 nullptr,
4648 IsDirectEntrypoint(kQuickUnlockObject));
4649 }
4650 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4651}
4652
4653void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4654 LocationSummary* locations =
4655 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4656 switch (mul->GetResultType()) {
4657 case Primitive::kPrimInt:
4658 case Primitive::kPrimLong:
4659 locations->SetInAt(0, Location::RequiresRegister());
4660 locations->SetInAt(1, Location::RequiresRegister());
4661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4662 break;
4663
4664 case Primitive::kPrimFloat:
4665 case Primitive::kPrimDouble:
4666 locations->SetInAt(0, Location::RequiresFpuRegister());
4667 locations->SetInAt(1, Location::RequiresFpuRegister());
4668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4669 break;
4670
4671 default:
4672 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4673 }
4674}
4675
4676void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4677 Primitive::Type type = instruction->GetType();
4678 LocationSummary* locations = instruction->GetLocations();
4679 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4680
4681 switch (type) {
4682 case Primitive::kPrimInt: {
4683 Register dst = locations->Out().AsRegister<Register>();
4684 Register lhs = locations->InAt(0).AsRegister<Register>();
4685 Register rhs = locations->InAt(1).AsRegister<Register>();
4686
4687 if (isR6) {
4688 __ MulR6(dst, lhs, rhs);
4689 } else {
4690 __ MulR2(dst, lhs, rhs);
4691 }
4692 break;
4693 }
4694 case Primitive::kPrimLong: {
4695 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4696 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4697 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4698 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4699 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4700 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4701
4702 // Extra checks to protect caused by the existance of A1_A2.
4703 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4704 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4705 DCHECK_NE(dst_high, lhs_low);
4706 DCHECK_NE(dst_high, rhs_low);
4707
4708 // A_B * C_D
4709 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4710 // dst_lo: [ low(B*D) ]
4711 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4712
4713 if (isR6) {
4714 __ MulR6(TMP, lhs_high, rhs_low);
4715 __ MulR6(dst_high, lhs_low, rhs_high);
4716 __ Addu(dst_high, dst_high, TMP);
4717 __ MuhuR6(TMP, lhs_low, rhs_low);
4718 __ Addu(dst_high, dst_high, TMP);
4719 __ MulR6(dst_low, lhs_low, rhs_low);
4720 } else {
4721 __ MulR2(TMP, lhs_high, rhs_low);
4722 __ MulR2(dst_high, lhs_low, rhs_high);
4723 __ Addu(dst_high, dst_high, TMP);
4724 __ MultuR2(lhs_low, rhs_low);
4725 __ Mfhi(TMP);
4726 __ Addu(dst_high, dst_high, TMP);
4727 __ Mflo(dst_low);
4728 }
4729 break;
4730 }
4731 case Primitive::kPrimFloat:
4732 case Primitive::kPrimDouble: {
4733 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4734 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4735 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4736 if (type == Primitive::kPrimFloat) {
4737 __ MulS(dst, lhs, rhs);
4738 } else {
4739 __ MulD(dst, lhs, rhs);
4740 }
4741 break;
4742 }
4743 default:
4744 LOG(FATAL) << "Unexpected mul type " << type;
4745 }
4746}
4747
4748void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4749 LocationSummary* locations =
4750 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4751 switch (neg->GetResultType()) {
4752 case Primitive::kPrimInt:
4753 case Primitive::kPrimLong:
4754 locations->SetInAt(0, Location::RequiresRegister());
4755 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4756 break;
4757
4758 case Primitive::kPrimFloat:
4759 case Primitive::kPrimDouble:
4760 locations->SetInAt(0, Location::RequiresFpuRegister());
4761 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4762 break;
4763
4764 default:
4765 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4766 }
4767}
4768
4769void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4770 Primitive::Type type = instruction->GetType();
4771 LocationSummary* locations = instruction->GetLocations();
4772
4773 switch (type) {
4774 case Primitive::kPrimInt: {
4775 Register dst = locations->Out().AsRegister<Register>();
4776 Register src = locations->InAt(0).AsRegister<Register>();
4777 __ Subu(dst, ZERO, src);
4778 break;
4779 }
4780 case Primitive::kPrimLong: {
4781 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4782 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4783 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4784 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4785 __ Subu(dst_low, ZERO, src_low);
4786 __ Sltu(TMP, ZERO, dst_low);
4787 __ Subu(dst_high, ZERO, src_high);
4788 __ Subu(dst_high, dst_high, TMP);
4789 break;
4790 }
4791 case Primitive::kPrimFloat:
4792 case Primitive::kPrimDouble: {
4793 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4794 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4795 if (type == Primitive::kPrimFloat) {
4796 __ NegS(dst, src);
4797 } else {
4798 __ NegD(dst, src);
4799 }
4800 break;
4801 }
4802 default:
4803 LOG(FATAL) << "Unexpected neg type " << type;
4804 }
4805}
4806
4807void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4808 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004809 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004810 InvokeRuntimeCallingConvention calling_convention;
4811 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4812 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4813 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4814 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4815}
4816
4817void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4818 InvokeRuntimeCallingConvention calling_convention;
4819 Register current_method_register = calling_convention.GetRegisterAt(2);
4820 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4821 // Move an uint16_t value to a register.
4822 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4823 codegen_->InvokeRuntime(
Andreas Gampe542451c2016-07-26 09:02:02 -07004824 GetThreadOffset<kMipsPointerSize>(instruction->GetEntrypoint()).Int32Value(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004825 instruction,
4826 instruction->GetDexPc(),
4827 nullptr,
4828 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4829 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4830 void*, uint32_t, int32_t, ArtMethod*>();
4831}
4832
4833void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4834 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004835 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004836 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004837 if (instruction->IsStringAlloc()) {
4838 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4839 } else {
4840 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4841 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4842 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004843 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4844}
4845
4846void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004847 if (instruction->IsStringAlloc()) {
4848 // String is allocated through StringFactory. Call NewEmptyString entry point.
4849 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004850 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004851 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4852 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4853 __ Jalr(T9);
4854 __ Nop();
4855 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4856 } else {
4857 codegen_->InvokeRuntime(
Andreas Gampe542451c2016-07-26 09:02:02 -07004858 GetThreadOffset<kMipsPointerSize>(instruction->GetEntrypoint()).Int32Value(),
David Brazdil6de19382016-01-08 17:37:10 +00004859 instruction,
4860 instruction->GetDexPc(),
4861 nullptr,
4862 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4863 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4864 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004865}
4866
4867void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4868 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4869 locations->SetInAt(0, Location::RequiresRegister());
4870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4871}
4872
4873void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4874 Primitive::Type type = instruction->GetType();
4875 LocationSummary* locations = instruction->GetLocations();
4876
4877 switch (type) {
4878 case Primitive::kPrimInt: {
4879 Register dst = locations->Out().AsRegister<Register>();
4880 Register src = locations->InAt(0).AsRegister<Register>();
4881 __ Nor(dst, src, ZERO);
4882 break;
4883 }
4884
4885 case Primitive::kPrimLong: {
4886 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4887 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4888 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4889 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4890 __ Nor(dst_high, src_high, ZERO);
4891 __ Nor(dst_low, src_low, ZERO);
4892 break;
4893 }
4894
4895 default:
4896 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4897 }
4898}
4899
4900void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4902 locations->SetInAt(0, Location::RequiresRegister());
4903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4904}
4905
4906void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4907 LocationSummary* locations = instruction->GetLocations();
4908 __ Xori(locations->Out().AsRegister<Register>(),
4909 locations->InAt(0).AsRegister<Register>(),
4910 1);
4911}
4912
4913void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4914 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4915 ? LocationSummary::kCallOnSlowPath
4916 : LocationSummary::kNoCall;
4917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4918 locations->SetInAt(0, Location::RequiresRegister());
4919 if (instruction->HasUses()) {
4920 locations->SetOut(Location::SameAsFirstInput());
4921 }
4922}
4923
Calin Juravle2ae48182016-03-16 14:05:09 +00004924void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4925 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004926 return;
4927 }
4928 Location obj = instruction->GetLocations()->InAt(0);
4929
4930 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004931 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004932}
4933
Calin Juravle2ae48182016-03-16 14:05:09 +00004934void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004935 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004936 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004937
4938 Location obj = instruction->GetLocations()->InAt(0);
4939
4940 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4941}
4942
4943void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004944 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004945}
4946
4947void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4948 HandleBinaryOp(instruction);
4949}
4950
4951void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4952 HandleBinaryOp(instruction);
4953}
4954
4955void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4956 LOG(FATAL) << "Unreachable";
4957}
4958
4959void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4960 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4961}
4962
4963void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4964 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4965 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4966 if (location.IsStackSlot()) {
4967 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4968 } else if (location.IsDoubleStackSlot()) {
4969 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4970 }
4971 locations->SetOut(location);
4972}
4973
4974void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4975 ATTRIBUTE_UNUSED) {
4976 // Nothing to do, the parameter is already at its location.
4977}
4978
4979void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4980 LocationSummary* locations =
4981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4982 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4983}
4984
4985void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4986 ATTRIBUTE_UNUSED) {
4987 // Nothing to do, the method is already at its location.
4988}
4989
4990void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4991 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004992 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004993 locations->SetInAt(i, Location::Any());
4994 }
4995 locations->SetOut(Location::Any());
4996}
4997
4998void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4999 LOG(FATAL) << "Unreachable";
5000}
5001
5002void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5003 Primitive::Type type = rem->GetResultType();
5004 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005005 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5007
5008 switch (type) {
5009 case Primitive::kPrimInt:
5010 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005011 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5013 break;
5014
5015 case Primitive::kPrimLong: {
5016 InvokeRuntimeCallingConvention calling_convention;
5017 locations->SetInAt(0, Location::RegisterPairLocation(
5018 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5019 locations->SetInAt(1, Location::RegisterPairLocation(
5020 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5021 locations->SetOut(calling_convention.GetReturnLocation(type));
5022 break;
5023 }
5024
5025 case Primitive::kPrimFloat:
5026 case Primitive::kPrimDouble: {
5027 InvokeRuntimeCallingConvention calling_convention;
5028 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5029 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5030 locations->SetOut(calling_convention.GetReturnLocation(type));
5031 break;
5032 }
5033
5034 default:
5035 LOG(FATAL) << "Unexpected rem type " << type;
5036 }
5037}
5038
5039void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5040 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005041
5042 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005043 case Primitive::kPrimInt:
5044 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005045 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005046 case Primitive::kPrimLong: {
5047 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
5048 instruction,
5049 instruction->GetDexPc(),
5050 nullptr,
5051 IsDirectEntrypoint(kQuickLmod));
5052 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5053 break;
5054 }
5055 case Primitive::kPrimFloat: {
5056 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
5057 instruction, instruction->GetDexPc(),
5058 nullptr,
5059 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00005060 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005061 break;
5062 }
5063 case Primitive::kPrimDouble: {
5064 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
5065 instruction, instruction->GetDexPc(),
5066 nullptr,
5067 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00005068 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005069 break;
5070 }
5071 default:
5072 LOG(FATAL) << "Unexpected rem type " << type;
5073 }
5074}
5075
5076void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5077 memory_barrier->SetLocations(nullptr);
5078}
5079
5080void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5081 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5082}
5083
5084void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5085 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5086 Primitive::Type return_type = ret->InputAt(0)->GetType();
5087 locations->SetInAt(0, MipsReturnLocation(return_type));
5088}
5089
5090void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5091 codegen_->GenerateFrameExit();
5092}
5093
5094void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5095 ret->SetLocations(nullptr);
5096}
5097
5098void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5099 codegen_->GenerateFrameExit();
5100}
5101
Alexey Frunze92d90602015-12-18 18:16:36 -08005102void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5103 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005104}
5105
Alexey Frunze92d90602015-12-18 18:16:36 -08005106void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5107 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005108}
5109
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005110void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5111 HandleShift(shl);
5112}
5113
5114void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5115 HandleShift(shl);
5116}
5117
5118void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5119 HandleShift(shr);
5120}
5121
5122void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5123 HandleShift(shr);
5124}
5125
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005126void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5127 HandleBinaryOp(instruction);
5128}
5129
5130void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5131 HandleBinaryOp(instruction);
5132}
5133
5134void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5135 HandleFieldGet(instruction, instruction->GetFieldInfo());
5136}
5137
5138void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5139 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5140}
5141
5142void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5143 HandleFieldSet(instruction, instruction->GetFieldInfo());
5144}
5145
5146void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5147 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5148}
5149
5150void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5151 HUnresolvedInstanceFieldGet* instruction) {
5152 FieldAccessCallingConventionMIPS calling_convention;
5153 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5154 instruction->GetFieldType(),
5155 calling_convention);
5156}
5157
5158void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5159 HUnresolvedInstanceFieldGet* instruction) {
5160 FieldAccessCallingConventionMIPS calling_convention;
5161 codegen_->GenerateUnresolvedFieldAccess(instruction,
5162 instruction->GetFieldType(),
5163 instruction->GetFieldIndex(),
5164 instruction->GetDexPc(),
5165 calling_convention);
5166}
5167
5168void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5169 HUnresolvedInstanceFieldSet* instruction) {
5170 FieldAccessCallingConventionMIPS calling_convention;
5171 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5172 instruction->GetFieldType(),
5173 calling_convention);
5174}
5175
5176void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5177 HUnresolvedInstanceFieldSet* instruction) {
5178 FieldAccessCallingConventionMIPS calling_convention;
5179 codegen_->GenerateUnresolvedFieldAccess(instruction,
5180 instruction->GetFieldType(),
5181 instruction->GetFieldIndex(),
5182 instruction->GetDexPc(),
5183 calling_convention);
5184}
5185
5186void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5187 HUnresolvedStaticFieldGet* instruction) {
5188 FieldAccessCallingConventionMIPS calling_convention;
5189 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5190 instruction->GetFieldType(),
5191 calling_convention);
5192}
5193
5194void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5195 HUnresolvedStaticFieldGet* instruction) {
5196 FieldAccessCallingConventionMIPS calling_convention;
5197 codegen_->GenerateUnresolvedFieldAccess(instruction,
5198 instruction->GetFieldType(),
5199 instruction->GetFieldIndex(),
5200 instruction->GetDexPc(),
5201 calling_convention);
5202}
5203
5204void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5205 HUnresolvedStaticFieldSet* instruction) {
5206 FieldAccessCallingConventionMIPS calling_convention;
5207 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5208 instruction->GetFieldType(),
5209 calling_convention);
5210}
5211
5212void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5213 HUnresolvedStaticFieldSet* instruction) {
5214 FieldAccessCallingConventionMIPS calling_convention;
5215 codegen_->GenerateUnresolvedFieldAccess(instruction,
5216 instruction->GetFieldType(),
5217 instruction->GetFieldIndex(),
5218 instruction->GetDexPc(),
5219 calling_convention);
5220}
5221
5222void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005223 LocationSummary* locations =
5224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5225 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005226}
5227
5228void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5229 HBasicBlock* block = instruction->GetBlock();
5230 if (block->GetLoopInformation() != nullptr) {
5231 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5232 // The back edge will generate the suspend check.
5233 return;
5234 }
5235 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5236 // The goto will generate the suspend check.
5237 return;
5238 }
5239 GenerateSuspendCheck(instruction, nullptr);
5240}
5241
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005242void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5243 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005244 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005245 InvokeRuntimeCallingConvention calling_convention;
5246 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5247}
5248
5249void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
5250 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5251 instruction,
5252 instruction->GetDexPc(),
5253 nullptr,
5254 IsDirectEntrypoint(kQuickDeliverException));
5255 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5256}
5257
5258void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5259 Primitive::Type input_type = conversion->GetInputType();
5260 Primitive::Type result_type = conversion->GetResultType();
5261 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005262 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005263
5264 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5265 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5266 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5267 }
5268
5269 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005270 if (!isR6 &&
5271 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5272 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005273 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005274 }
5275
5276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5277
5278 if (call_kind == LocationSummary::kNoCall) {
5279 if (Primitive::IsFloatingPointType(input_type)) {
5280 locations->SetInAt(0, Location::RequiresFpuRegister());
5281 } else {
5282 locations->SetInAt(0, Location::RequiresRegister());
5283 }
5284
5285 if (Primitive::IsFloatingPointType(result_type)) {
5286 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5287 } else {
5288 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5289 }
5290 } else {
5291 InvokeRuntimeCallingConvention calling_convention;
5292
5293 if (Primitive::IsFloatingPointType(input_type)) {
5294 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5295 } else {
5296 DCHECK_EQ(input_type, Primitive::kPrimLong);
5297 locations->SetInAt(0, Location::RegisterPairLocation(
5298 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5299 }
5300
5301 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5302 }
5303}
5304
5305void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5306 LocationSummary* locations = conversion->GetLocations();
5307 Primitive::Type result_type = conversion->GetResultType();
5308 Primitive::Type input_type = conversion->GetInputType();
5309 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005310 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005311
5312 DCHECK_NE(input_type, result_type);
5313
5314 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5315 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5316 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5317 Register src = locations->InAt(0).AsRegister<Register>();
5318
Alexey Frunzea871ef12016-06-27 15:20:11 -07005319 if (dst_low != src) {
5320 __ Move(dst_low, src);
5321 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005322 __ Sra(dst_high, src, 31);
5323 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5324 Register dst = locations->Out().AsRegister<Register>();
5325 Register src = (input_type == Primitive::kPrimLong)
5326 ? locations->InAt(0).AsRegisterPairLow<Register>()
5327 : locations->InAt(0).AsRegister<Register>();
5328
5329 switch (result_type) {
5330 case Primitive::kPrimChar:
5331 __ Andi(dst, src, 0xFFFF);
5332 break;
5333 case Primitive::kPrimByte:
5334 if (has_sign_extension) {
5335 __ Seb(dst, src);
5336 } else {
5337 __ Sll(dst, src, 24);
5338 __ Sra(dst, dst, 24);
5339 }
5340 break;
5341 case Primitive::kPrimShort:
5342 if (has_sign_extension) {
5343 __ Seh(dst, src);
5344 } else {
5345 __ Sll(dst, src, 16);
5346 __ Sra(dst, dst, 16);
5347 }
5348 break;
5349 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005350 if (dst != src) {
5351 __ Move(dst, src);
5352 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005353 break;
5354
5355 default:
5356 LOG(FATAL) << "Unexpected type conversion from " << input_type
5357 << " to " << result_type;
5358 }
5359 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005360 if (input_type == Primitive::kPrimLong) {
5361 if (isR6) {
5362 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5363 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5364 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5365 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5366 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5367 __ Mtc1(src_low, FTMP);
5368 __ Mthc1(src_high, FTMP);
5369 if (result_type == Primitive::kPrimFloat) {
5370 __ Cvtsl(dst, FTMP);
5371 } else {
5372 __ Cvtdl(dst, FTMP);
5373 }
5374 } else {
5375 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
5376 : QUICK_ENTRY_POINT(pL2d);
5377 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
5378 : IsDirectEntrypoint(kQuickL2d);
5379 codegen_->InvokeRuntime(entry_offset,
5380 conversion,
5381 conversion->GetDexPc(),
5382 nullptr,
5383 direct);
5384 if (result_type == Primitive::kPrimFloat) {
5385 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5386 } else {
5387 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5388 }
5389 }
5390 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005391 Register src = locations->InAt(0).AsRegister<Register>();
5392 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5393 __ Mtc1(src, FTMP);
5394 if (result_type == Primitive::kPrimFloat) {
5395 __ Cvtsw(dst, FTMP);
5396 } else {
5397 __ Cvtdw(dst, FTMP);
5398 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005399 }
5400 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5401 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005402 if (result_type == Primitive::kPrimLong) {
5403 if (isR6) {
5404 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5405 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5406 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5407 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5408 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5409 MipsLabel truncate;
5410 MipsLabel done;
5411
5412 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5413 // value when the input is either a NaN or is outside of the range of the output type
5414 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5415 // the same result.
5416 //
5417 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5418 // value of the output type if the input is outside of the range after the truncation or
5419 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5420 // results. This matches the desired float/double-to-int/long conversion exactly.
5421 //
5422 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5423 //
5424 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5425 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5426 // even though it must be NAN2008=1 on R6.
5427 //
5428 // The code takes care of the different behaviors by first comparing the input to the
5429 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5430 // If the input is greater than or equal to the minimum, it procedes to the truncate
5431 // instruction, which will handle such an input the same way irrespective of NAN2008.
5432 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5433 // in order to return either zero or the minimum value.
5434 //
5435 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5436 // truncate instruction for MIPS64R6.
5437 if (input_type == Primitive::kPrimFloat) {
5438 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5439 __ LoadConst32(TMP, min_val);
5440 __ Mtc1(TMP, FTMP);
5441 __ CmpLeS(FTMP, FTMP, src);
5442 } else {
5443 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5444 __ LoadConst32(TMP, High32Bits(min_val));
5445 __ Mtc1(ZERO, FTMP);
5446 __ Mthc1(TMP, FTMP);
5447 __ CmpLeD(FTMP, FTMP, src);
5448 }
5449
5450 __ Bc1nez(FTMP, &truncate);
5451
5452 if (input_type == Primitive::kPrimFloat) {
5453 __ CmpEqS(FTMP, src, src);
5454 } else {
5455 __ CmpEqD(FTMP, src, src);
5456 }
5457 __ Move(dst_low, ZERO);
5458 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5459 __ Mfc1(TMP, FTMP);
5460 __ And(dst_high, dst_high, TMP);
5461
5462 __ B(&done);
5463
5464 __ Bind(&truncate);
5465
5466 if (input_type == Primitive::kPrimFloat) {
5467 __ TruncLS(FTMP, src);
5468 } else {
5469 __ TruncLD(FTMP, src);
5470 }
5471 __ Mfc1(dst_low, FTMP);
5472 __ Mfhc1(dst_high, FTMP);
5473
5474 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005475 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005476 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5477 : QUICK_ENTRY_POINT(pD2l);
5478 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5479 : IsDirectEntrypoint(kQuickD2l);
5480 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5481 if (input_type == Primitive::kPrimFloat) {
5482 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5483 } else {
5484 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5485 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005486 }
5487 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005488 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5489 Register dst = locations->Out().AsRegister<Register>();
5490 MipsLabel truncate;
5491 MipsLabel done;
5492
5493 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5494 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5495 // even though it must be NAN2008=1 on R6.
5496 //
5497 // For details see the large comment above for the truncation of float/double to long on R6.
5498 //
5499 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5500 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005501 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005502 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5503 __ LoadConst32(TMP, min_val);
5504 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005505 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005506 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5507 __ LoadConst32(TMP, High32Bits(min_val));
5508 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005509 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005510 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005511
5512 if (isR6) {
5513 if (input_type == Primitive::kPrimFloat) {
5514 __ CmpLeS(FTMP, FTMP, src);
5515 } else {
5516 __ CmpLeD(FTMP, FTMP, src);
5517 }
5518 __ Bc1nez(FTMP, &truncate);
5519
5520 if (input_type == Primitive::kPrimFloat) {
5521 __ CmpEqS(FTMP, src, src);
5522 } else {
5523 __ CmpEqD(FTMP, src, src);
5524 }
5525 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5526 __ Mfc1(TMP, FTMP);
5527 __ And(dst, dst, TMP);
5528 } else {
5529 if (input_type == Primitive::kPrimFloat) {
5530 __ ColeS(0, FTMP, src);
5531 } else {
5532 __ ColeD(0, FTMP, src);
5533 }
5534 __ Bc1t(0, &truncate);
5535
5536 if (input_type == Primitive::kPrimFloat) {
5537 __ CeqS(0, src, src);
5538 } else {
5539 __ CeqD(0, src, src);
5540 }
5541 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5542 __ Movf(dst, ZERO, 0);
5543 }
5544
5545 __ B(&done);
5546
5547 __ Bind(&truncate);
5548
5549 if (input_type == Primitive::kPrimFloat) {
5550 __ TruncWS(FTMP, src);
5551 } else {
5552 __ TruncWD(FTMP, src);
5553 }
5554 __ Mfc1(dst, FTMP);
5555
5556 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005557 }
5558 } else if (Primitive::IsFloatingPointType(result_type) &&
5559 Primitive::IsFloatingPointType(input_type)) {
5560 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5561 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5562 if (result_type == Primitive::kPrimFloat) {
5563 __ Cvtsd(dst, src);
5564 } else {
5565 __ Cvtds(dst, src);
5566 }
5567 } else {
5568 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5569 << " to " << result_type;
5570 }
5571}
5572
5573void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5574 HandleShift(ushr);
5575}
5576
5577void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5578 HandleShift(ushr);
5579}
5580
5581void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5582 HandleBinaryOp(instruction);
5583}
5584
5585void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5586 HandleBinaryOp(instruction);
5587}
5588
5589void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5590 // Nothing to do, this should be removed during prepare for register allocator.
5591 LOG(FATAL) << "Unreachable";
5592}
5593
5594void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5595 // Nothing to do, this should be removed during prepare for register allocator.
5596 LOG(FATAL) << "Unreachable";
5597}
5598
5599void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005600 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005601}
5602
5603void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005604 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005605}
5606
5607void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005608 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005609}
5610
5611void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005612 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005613}
5614
5615void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005616 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005617}
5618
5619void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005620 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005621}
5622
5623void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005624 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005625}
5626
5627void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005628 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005629}
5630
5631void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005632 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005633}
5634
5635void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005636 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005637}
5638
5639void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005640 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005641}
5642
5643void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005644 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005645}
5646
5647void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005648 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005649}
5650
5651void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005652 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005653}
5654
5655void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005656 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005657}
5658
5659void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005660 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005661}
5662
5663void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005664 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005665}
5666
5667void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005668 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005669}
5670
5671void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005672 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005673}
5674
5675void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005676 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005677}
5678
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005679void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5680 LocationSummary* locations =
5681 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5682 locations->SetInAt(0, Location::RequiresRegister());
5683}
5684
5685void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5686 int32_t lower_bound = switch_instr->GetStartValue();
5687 int32_t num_entries = switch_instr->GetNumEntries();
5688 LocationSummary* locations = switch_instr->GetLocations();
5689 Register value_reg = locations->InAt(0).AsRegister<Register>();
5690 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5691
5692 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005693 Register temp_reg = TMP;
5694 __ Addiu32(temp_reg, value_reg, -lower_bound);
5695 // Jump to default if index is negative
5696 // Note: We don't check the case that index is positive while value < lower_bound, because in
5697 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5698 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5699
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005700 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005701 // Jump to successors[0] if value == lower_bound.
5702 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5703 int32_t last_index = 0;
5704 for (; num_entries - last_index > 2; last_index += 2) {
5705 __ Addiu(temp_reg, temp_reg, -2);
5706 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5707 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5708 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5709 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5710 }
5711 if (num_entries - last_index == 2) {
5712 // The last missing case_value.
5713 __ Addiu(temp_reg, temp_reg, -1);
5714 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005715 }
5716
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005717 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005718 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5719 __ B(codegen_->GetLabelOf(default_block));
5720 }
5721}
5722
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005723void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5724 HMipsComputeBaseMethodAddress* insn) {
5725 LocationSummary* locations =
5726 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5727 locations->SetOut(Location::RequiresRegister());
5728}
5729
5730void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5731 HMipsComputeBaseMethodAddress* insn) {
5732 LocationSummary* locations = insn->GetLocations();
5733 Register reg = locations->Out().AsRegister<Register>();
5734
5735 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5736
5737 // Generate a dummy PC-relative call to obtain PC.
5738 __ Nal();
5739 // Grab the return address off RA.
5740 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005741 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005742
5743 // Remember this offset (the obtained PC value) for later use with constant area.
5744 __ BindPcRelBaseLabel();
5745}
5746
5747void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5749 locations->SetOut(Location::RequiresRegister());
5750}
5751
5752void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5753 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5754 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5755 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
5756
5757 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5758 __ Bind(&info->high_label);
5759 __ Bind(&info->pc_rel_label);
5760 // Add a 32-bit offset to PC.
5761 __ Auipc(reg, /* placeholder */ 0x1234);
5762 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5763 } else {
5764 // Generate a dummy PC-relative call to obtain PC.
5765 __ Nal();
5766 __ Bind(&info->high_label);
5767 __ Lui(reg, /* placeholder */ 0x1234);
5768 __ Bind(&info->pc_rel_label);
5769 __ Ori(reg, reg, /* placeholder */ 0x5678);
5770 // Add a 32-bit offset to PC.
5771 __ Addu(reg, reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005772 // TODO: Can we share this code with that of VisitMipsComputeBaseMethodAddress()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005773 }
5774}
5775
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005776void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5777 // The trampoline uses the same calling convention as dex calling conventions,
5778 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5779 // the method_idx.
5780 HandleInvoke(invoke);
5781}
5782
5783void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5784 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5785}
5786
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005787void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5788 LocationSummary* locations =
5789 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5790 locations->SetInAt(0, Location::RequiresRegister());
5791 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005792}
5793
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005794void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5795 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005796 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005797 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005798 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005799 __ LoadFromOffset(kLoadWord,
5800 locations->Out().AsRegister<Register>(),
5801 locations->InAt(0).AsRegister<Register>(),
5802 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005803 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005804 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005805 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005806 __ LoadFromOffset(kLoadWord,
5807 locations->Out().AsRegister<Register>(),
5808 locations->InAt(0).AsRegister<Register>(),
5809 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005810 __ LoadFromOffset(kLoadWord,
5811 locations->Out().AsRegister<Register>(),
5812 locations->Out().AsRegister<Register>(),
5813 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005814 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005815}
5816
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005817#undef __
5818#undef QUICK_ENTRY_POINT
5819
5820} // namespace mips
5821} // namespace art