blob: 1e6251351c9266cd147e6b10f2efaa5b631f3e9d [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 Frunze58320ce2016-08-30 21:40:46 -0700700}
701
702bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700703 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700704 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
705 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
706 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700707 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
708 // saved in an unused temporary register) and saving of RA and the current method pointer
709 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700710 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700711}
712
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200713static dwarf::Reg DWARFReg(Register reg) {
714 return dwarf::Reg::MipsCore(static_cast<int>(reg));
715}
716
717// TODO: mapping of floating-point registers to DWARF.
718
719void CodeGeneratorMIPS::GenerateFrameEntry() {
720 __ Bind(&frame_entry_label_);
721
722 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
723
724 if (do_overflow_check) {
725 __ LoadFromOffset(kLoadWord,
726 ZERO,
727 SP,
728 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
729 RecordPcInfo(nullptr, 0);
730 }
731
732 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700733 CHECK_EQ(fpu_spill_mask_, 0u);
734 CHECK_EQ(core_spill_mask_, 1u << RA);
735 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200736 return;
737 }
738
739 // Make sure the frame size isn't unreasonably large.
740 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
741 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
742 }
743
744 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200745
Alexey Frunze73296a72016-06-03 22:51:46 -0700746 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200747 __ IncreaseFrameSize(ofs);
748
Alexey Frunze73296a72016-06-03 22:51:46 -0700749 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
750 Register reg = static_cast<Register>(MostSignificantBit(mask));
751 mask ^= 1u << reg;
752 ofs -= kMipsWordSize;
753 // The ZERO register is only included for alignment.
754 if (reg != ZERO) {
755 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200756 __ cfi().RelOffset(DWARFReg(reg), ofs);
757 }
758 }
759
Alexey Frunze73296a72016-06-03 22:51:46 -0700760 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
761 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
762 mask ^= 1u << reg;
763 ofs -= kMipsDoublewordSize;
764 __ StoreDToOffset(reg, SP, ofs);
765 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200766 }
767
Alexey Frunze73296a72016-06-03 22:51:46 -0700768 // Store the current method pointer.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700769 // TODO: can we not do this if RequiresCurrentMethod() returns false?
Alexey Frunze73296a72016-06-03 22:51:46 -0700770 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200771}
772
773void CodeGeneratorMIPS::GenerateFrameExit() {
774 __ cfi().RememberState();
775
776 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200777 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200778
Alexey Frunze73296a72016-06-03 22:51:46 -0700779 // For better instruction scheduling restore RA before other registers.
780 uint32_t ofs = GetFrameSize();
781 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
782 Register reg = static_cast<Register>(MostSignificantBit(mask));
783 mask ^= 1u << reg;
784 ofs -= kMipsWordSize;
785 // The ZERO register is only included for alignment.
786 if (reg != ZERO) {
787 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200788 __ cfi().Restore(DWARFReg(reg));
789 }
790 }
791
Alexey Frunze73296a72016-06-03 22:51:46 -0700792 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
793 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
794 mask ^= 1u << reg;
795 ofs -= kMipsDoublewordSize;
796 __ LoadDFromOffset(reg, SP, ofs);
797 // TODO: __ cfi().Restore(DWARFReg(reg));
798 }
799
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700800 size_t frame_size = GetFrameSize();
801 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
802 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
803 bool reordering = __ SetReorder(false);
804 if (exchange) {
805 __ Jr(RA);
806 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
807 } else {
808 __ DecreaseFrameSize(frame_size);
809 __ Jr(RA);
810 __ Nop(); // In delay slot.
811 }
812 __ SetReorder(reordering);
813 } else {
814 __ Jr(RA);
815 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200816 }
817
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200818 __ cfi().RestoreState();
819 __ cfi().DefCFAOffset(GetFrameSize());
820}
821
822void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
823 __ Bind(GetLabelOf(block));
824}
825
826void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
827 if (src.Equals(dst)) {
828 return;
829 }
830
831 if (src.IsConstant()) {
832 MoveConstant(dst, src.GetConstant());
833 } else {
834 if (Primitive::Is64BitType(dst_type)) {
835 Move64(dst, src);
836 } else {
837 Move32(dst, src);
838 }
839 }
840}
841
842void CodeGeneratorMIPS::Move32(Location destination, Location source) {
843 if (source.Equals(destination)) {
844 return;
845 }
846
847 if (destination.IsRegister()) {
848 if (source.IsRegister()) {
849 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
850 } else if (source.IsFpuRegister()) {
851 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
852 } else {
853 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
854 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
855 }
856 } else if (destination.IsFpuRegister()) {
857 if (source.IsRegister()) {
858 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
859 } else if (source.IsFpuRegister()) {
860 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
861 } else {
862 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
863 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
864 }
865 } else {
866 DCHECK(destination.IsStackSlot()) << destination;
867 if (source.IsRegister()) {
868 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
869 } else if (source.IsFpuRegister()) {
870 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
871 } else {
872 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
873 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
874 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
875 }
876 }
877}
878
879void CodeGeneratorMIPS::Move64(Location destination, Location source) {
880 if (source.Equals(destination)) {
881 return;
882 }
883
884 if (destination.IsRegisterPair()) {
885 if (source.IsRegisterPair()) {
886 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
887 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
888 } else if (source.IsFpuRegister()) {
889 Register dst_high = destination.AsRegisterPairHigh<Register>();
890 Register dst_low = destination.AsRegisterPairLow<Register>();
891 FRegister src = source.AsFpuRegister<FRegister>();
892 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800893 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200894 } else {
895 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
896 int32_t off = source.GetStackIndex();
897 Register r = destination.AsRegisterPairLow<Register>();
898 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
899 }
900 } else if (destination.IsFpuRegister()) {
901 if (source.IsRegisterPair()) {
902 FRegister dst = destination.AsFpuRegister<FRegister>();
903 Register src_high = source.AsRegisterPairHigh<Register>();
904 Register src_low = source.AsRegisterPairLow<Register>();
905 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800906 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200907 } else if (source.IsFpuRegister()) {
908 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
909 } else {
910 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
911 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
912 }
913 } else {
914 DCHECK(destination.IsDoubleStackSlot()) << destination;
915 int32_t off = destination.GetStackIndex();
916 if (source.IsRegisterPair()) {
917 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
918 } else if (source.IsFpuRegister()) {
919 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
920 } else {
921 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
922 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
923 __ StoreToOffset(kStoreWord, TMP, SP, off);
924 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
925 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
926 }
927 }
928}
929
930void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
931 if (c->IsIntConstant() || c->IsNullConstant()) {
932 // Move 32 bit constant.
933 int32_t value = GetInt32ValueOf(c);
934 if (destination.IsRegister()) {
935 Register dst = destination.AsRegister<Register>();
936 __ LoadConst32(dst, value);
937 } else {
938 DCHECK(destination.IsStackSlot())
939 << "Cannot move " << c->DebugName() << " to " << destination;
940 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
941 }
942 } else if (c->IsLongConstant()) {
943 // Move 64 bit constant.
944 int64_t value = GetInt64ValueOf(c);
945 if (destination.IsRegisterPair()) {
946 Register r_h = destination.AsRegisterPairHigh<Register>();
947 Register r_l = destination.AsRegisterPairLow<Register>();
948 __ LoadConst64(r_h, r_l, value);
949 } else {
950 DCHECK(destination.IsDoubleStackSlot())
951 << "Cannot move " << c->DebugName() << " to " << destination;
952 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
953 }
954 } else if (c->IsFloatConstant()) {
955 // Move 32 bit float constant.
956 int32_t value = GetInt32ValueOf(c);
957 if (destination.IsFpuRegister()) {
958 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
959 } else {
960 DCHECK(destination.IsStackSlot())
961 << "Cannot move " << c->DebugName() << " to " << destination;
962 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
963 }
964 } else {
965 // Move 64 bit double constant.
966 DCHECK(c->IsDoubleConstant()) << c->DebugName();
967 int64_t value = GetInt64ValueOf(c);
968 if (destination.IsFpuRegister()) {
969 FRegister fd = destination.AsFpuRegister<FRegister>();
970 __ LoadDConst64(fd, value, TMP);
971 } else {
972 DCHECK(destination.IsDoubleStackSlot())
973 << "Cannot move " << c->DebugName() << " to " << destination;
974 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
975 }
976 }
977}
978
979void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
980 DCHECK(destination.IsRegister());
981 Register dst = destination.AsRegister<Register>();
982 __ LoadConst32(dst, value);
983}
984
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200985void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
986 if (location.IsRegister()) {
987 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700988 } else if (location.IsRegisterPair()) {
989 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
990 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200991 } else {
992 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
993 }
994}
995
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700996void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
997 DCHECK(linker_patches->empty());
998 size_t size =
999 method_patches_.size() +
1000 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001001 pc_relative_dex_cache_patches_.size() +
1002 pc_relative_string_patches_.size() +
1003 pc_relative_type_patches_.size() +
1004 boot_image_string_patches_.size() +
1005 boot_image_type_patches_.size() +
1006 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001007 linker_patches->reserve(size);
1008 for (const auto& entry : method_patches_) {
1009 const MethodReference& target_method = entry.first;
1010 Literal* literal = entry.second;
1011 DCHECK(literal->GetLabel()->IsBound());
1012 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1013 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1014 target_method.dex_file,
1015 target_method.dex_method_index));
1016 }
1017 for (const auto& entry : call_patches_) {
1018 const MethodReference& target_method = entry.first;
1019 Literal* literal = entry.second;
1020 DCHECK(literal->GetLabel()->IsBound());
1021 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1022 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1023 target_method.dex_file,
1024 target_method.dex_method_index));
1025 }
1026 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
1027 const DexFile& dex_file = info.target_dex_file;
1028 size_t base_element_offset = info.offset_or_index;
1029 DCHECK(info.high_label.IsBound());
1030 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1031 DCHECK(info.pc_rel_label.IsBound());
1032 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1033 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
1034 &dex_file,
1035 pc_rel_offset,
1036 base_element_offset));
1037 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001038 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
1039 const DexFile& dex_file = info.target_dex_file;
1040 size_t string_index = info.offset_or_index;
1041 DCHECK(info.high_label.IsBound());
1042 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1043 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1044 // the assembler's base label used for PC-relative literals.
1045 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1046 ? __ GetLabelLocation(&info.pc_rel_label)
1047 : __ GetPcRelBaseLabelLocation();
1048 linker_patches->push_back(LinkerPatch::RelativeStringPatch(high_offset,
1049 &dex_file,
1050 pc_rel_offset,
1051 string_index));
1052 }
1053 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
1054 const DexFile& dex_file = info.target_dex_file;
1055 size_t type_index = info.offset_or_index;
1056 DCHECK(info.high_label.IsBound());
1057 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1058 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1059 // the assembler's base label used for PC-relative literals.
1060 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1061 ? __ GetLabelLocation(&info.pc_rel_label)
1062 : __ GetPcRelBaseLabelLocation();
1063 linker_patches->push_back(LinkerPatch::RelativeTypePatch(high_offset,
1064 &dex_file,
1065 pc_rel_offset,
1066 type_index));
1067 }
1068 for (const auto& entry : boot_image_string_patches_) {
1069 const StringReference& target_string = entry.first;
1070 Literal* literal = entry.second;
1071 DCHECK(literal->GetLabel()->IsBound());
1072 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1073 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1074 target_string.dex_file,
1075 target_string.string_index));
1076 }
1077 for (const auto& entry : boot_image_type_patches_) {
1078 const TypeReference& target_type = entry.first;
1079 Literal* literal = entry.second;
1080 DCHECK(literal->GetLabel()->IsBound());
1081 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1082 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1083 target_type.dex_file,
1084 target_type.type_index));
1085 }
1086 for (const auto& entry : boot_image_address_patches_) {
1087 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1088 Literal* literal = entry.second;
1089 DCHECK(literal->GetLabel()->IsBound());
1090 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1091 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1092 }
1093}
1094
1095CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1096 const DexFile& dex_file, uint32_t string_index) {
1097 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1098}
1099
1100CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1101 const DexFile& dex_file, uint32_t type_index) {
1102 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001103}
1104
1105CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1106 const DexFile& dex_file, uint32_t element_offset) {
1107 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1108}
1109
1110CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1111 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1112 patches->emplace_back(dex_file, offset_or_index);
1113 return &patches->back();
1114}
1115
Alexey Frunze06a46c42016-07-19 15:00:40 -07001116Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1117 return map->GetOrCreate(
1118 value,
1119 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1120}
1121
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001122Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1123 MethodToLiteralMap* map) {
1124 return map->GetOrCreate(
1125 target_method,
1126 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1127}
1128
1129Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1130 return DeduplicateMethodLiteral(target_method, &method_patches_);
1131}
1132
1133Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1134 return DeduplicateMethodLiteral(target_method, &call_patches_);
1135}
1136
Alexey Frunze06a46c42016-07-19 15:00:40 -07001137Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1138 uint32_t string_index) {
1139 return boot_image_string_patches_.GetOrCreate(
1140 StringReference(&dex_file, string_index),
1141 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1142}
1143
1144Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1145 uint32_t type_index) {
1146 return boot_image_type_patches_.GetOrCreate(
1147 TypeReference(&dex_file, type_index),
1148 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1149}
1150
1151Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1152 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1153 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1154 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1155}
1156
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001157void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1158 MipsLabel done;
1159 Register card = AT;
1160 Register temp = TMP;
1161 __ Beqz(value, &done);
1162 __ LoadFromOffset(kLoadWord,
1163 card,
1164 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001165 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001166 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1167 __ Addu(temp, card, temp);
1168 __ Sb(card, temp, 0);
1169 __ Bind(&done);
1170}
1171
David Brazdil58282f42016-01-14 12:45:10 +00001172void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001173 // Don't allocate the dalvik style register pair passing.
1174 blocked_register_pairs_[A1_A2] = true;
1175
1176 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1177 blocked_core_registers_[ZERO] = true;
1178 blocked_core_registers_[K0] = true;
1179 blocked_core_registers_[K1] = true;
1180 blocked_core_registers_[GP] = true;
1181 blocked_core_registers_[SP] = true;
1182 blocked_core_registers_[RA] = true;
1183
1184 // AT and TMP(T8) are used as temporary/scratch registers
1185 // (similar to how AT is used by MIPS assemblers).
1186 blocked_core_registers_[AT] = true;
1187 blocked_core_registers_[TMP] = true;
1188 blocked_fpu_registers_[FTMP] = true;
1189
1190 // Reserve suspend and thread registers.
1191 blocked_core_registers_[S0] = true;
1192 blocked_core_registers_[TR] = true;
1193
1194 // Reserve T9 for function calls
1195 blocked_core_registers_[T9] = true;
1196
1197 // Reserve odd-numbered FPU registers.
1198 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1199 blocked_fpu_registers_[i] = true;
1200 }
1201
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001202 if (GetGraph()->IsDebuggable()) {
1203 // Stubs do not save callee-save floating point registers. If the graph
1204 // is debuggable, we need to deal with these registers differently. For
1205 // now, just block them.
1206 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1207 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1208 }
1209 }
1210
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001211 UpdateBlockedPairRegisters();
1212}
1213
1214void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1215 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1216 MipsManagedRegister current =
1217 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1218 if (blocked_core_registers_[current.AsRegisterPairLow()]
1219 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1220 blocked_register_pairs_[i] = true;
1221 }
1222 }
1223}
1224
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001225size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1226 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1227 return kMipsWordSize;
1228}
1229
1230size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1231 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1232 return kMipsWordSize;
1233}
1234
1235size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1236 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1237 return kMipsDoublewordSize;
1238}
1239
1240size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1241 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1242 return kMipsDoublewordSize;
1243}
1244
1245void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001246 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001247}
1248
1249void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001250 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001251}
1252
1253void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1254 HInstruction* instruction,
1255 uint32_t dex_pc,
1256 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001257 InvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001258 instruction,
1259 dex_pc,
1260 slow_path,
1261 IsDirectEntrypoint(entrypoint));
1262}
1263
1264constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1265
1266void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1267 HInstruction* instruction,
1268 uint32_t dex_pc,
1269 SlowPathCode* slow_path,
1270 bool is_direct_entrypoint) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001271 bool reordering = __ SetReorder(false);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001272 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1273 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274 if (is_direct_entrypoint) {
1275 // Reserve argument space on stack (for $a0-$a3) for
1276 // entrypoints that directly reference native implementations.
1277 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001278 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001279 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001280 } else {
1281 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001282 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001283 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001284 RecordPcInfo(instruction, dex_pc, slow_path);
1285}
1286
1287void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1288 Register class_reg) {
1289 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1290 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1291 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1292 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1293 __ Sync(0);
1294 __ Bind(slow_path->GetExitLabel());
1295}
1296
1297void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1298 __ Sync(0); // Only stype 0 is supported.
1299}
1300
1301void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1302 HBasicBlock* successor) {
1303 SuspendCheckSlowPathMIPS* slow_path =
1304 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1305 codegen_->AddSlowPath(slow_path);
1306
1307 __ LoadFromOffset(kLoadUnsignedHalfword,
1308 TMP,
1309 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001310 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311 if (successor == nullptr) {
1312 __ Bnez(TMP, slow_path->GetEntryLabel());
1313 __ Bind(slow_path->GetReturnLabel());
1314 } else {
1315 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1316 __ B(slow_path->GetEntryLabel());
1317 // slow_path will return to GetLabelOf(successor).
1318 }
1319}
1320
1321InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1322 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001323 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001324 assembler_(codegen->GetAssembler()),
1325 codegen_(codegen) {}
1326
1327void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1328 DCHECK_EQ(instruction->InputCount(), 2U);
1329 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1330 Primitive::Type type = instruction->GetResultType();
1331 switch (type) {
1332 case Primitive::kPrimInt: {
1333 locations->SetInAt(0, Location::RequiresRegister());
1334 HInstruction* right = instruction->InputAt(1);
1335 bool can_use_imm = false;
1336 if (right->IsConstant()) {
1337 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1338 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1339 can_use_imm = IsUint<16>(imm);
1340 } else if (instruction->IsAdd()) {
1341 can_use_imm = IsInt<16>(imm);
1342 } else {
1343 DCHECK(instruction->IsSub());
1344 can_use_imm = IsInt<16>(-imm);
1345 }
1346 }
1347 if (can_use_imm)
1348 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1349 else
1350 locations->SetInAt(1, Location::RequiresRegister());
1351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1352 break;
1353 }
1354
1355 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001356 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001357 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1358 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001359 break;
1360 }
1361
1362 case Primitive::kPrimFloat:
1363 case Primitive::kPrimDouble:
1364 DCHECK(instruction->IsAdd() || instruction->IsSub());
1365 locations->SetInAt(0, Location::RequiresFpuRegister());
1366 locations->SetInAt(1, Location::RequiresFpuRegister());
1367 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1368 break;
1369
1370 default:
1371 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1372 }
1373}
1374
1375void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1376 Primitive::Type type = instruction->GetType();
1377 LocationSummary* locations = instruction->GetLocations();
1378
1379 switch (type) {
1380 case Primitive::kPrimInt: {
1381 Register dst = locations->Out().AsRegister<Register>();
1382 Register lhs = locations->InAt(0).AsRegister<Register>();
1383 Location rhs_location = locations->InAt(1);
1384
1385 Register rhs_reg = ZERO;
1386 int32_t rhs_imm = 0;
1387 bool use_imm = rhs_location.IsConstant();
1388 if (use_imm) {
1389 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1390 } else {
1391 rhs_reg = rhs_location.AsRegister<Register>();
1392 }
1393
1394 if (instruction->IsAnd()) {
1395 if (use_imm)
1396 __ Andi(dst, lhs, rhs_imm);
1397 else
1398 __ And(dst, lhs, rhs_reg);
1399 } else if (instruction->IsOr()) {
1400 if (use_imm)
1401 __ Ori(dst, lhs, rhs_imm);
1402 else
1403 __ Or(dst, lhs, rhs_reg);
1404 } else if (instruction->IsXor()) {
1405 if (use_imm)
1406 __ Xori(dst, lhs, rhs_imm);
1407 else
1408 __ Xor(dst, lhs, rhs_reg);
1409 } else if (instruction->IsAdd()) {
1410 if (use_imm)
1411 __ Addiu(dst, lhs, rhs_imm);
1412 else
1413 __ Addu(dst, lhs, rhs_reg);
1414 } else {
1415 DCHECK(instruction->IsSub());
1416 if (use_imm)
1417 __ Addiu(dst, lhs, -rhs_imm);
1418 else
1419 __ Subu(dst, lhs, rhs_reg);
1420 }
1421 break;
1422 }
1423
1424 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001425 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1426 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1427 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1428 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001429 Location rhs_location = locations->InAt(1);
1430 bool use_imm = rhs_location.IsConstant();
1431 if (!use_imm) {
1432 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1433 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1434 if (instruction->IsAnd()) {
1435 __ And(dst_low, lhs_low, rhs_low);
1436 __ And(dst_high, lhs_high, rhs_high);
1437 } else if (instruction->IsOr()) {
1438 __ Or(dst_low, lhs_low, rhs_low);
1439 __ Or(dst_high, lhs_high, rhs_high);
1440 } else if (instruction->IsXor()) {
1441 __ Xor(dst_low, lhs_low, rhs_low);
1442 __ Xor(dst_high, lhs_high, rhs_high);
1443 } else if (instruction->IsAdd()) {
1444 if (lhs_low == rhs_low) {
1445 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1446 __ Slt(TMP, lhs_low, ZERO);
1447 __ Addu(dst_low, lhs_low, rhs_low);
1448 } else {
1449 __ Addu(dst_low, lhs_low, rhs_low);
1450 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1451 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1452 }
1453 __ Addu(dst_high, lhs_high, rhs_high);
1454 __ Addu(dst_high, dst_high, TMP);
1455 } else {
1456 DCHECK(instruction->IsSub());
1457 __ Sltu(TMP, lhs_low, rhs_low);
1458 __ Subu(dst_low, lhs_low, rhs_low);
1459 __ Subu(dst_high, lhs_high, rhs_high);
1460 __ Subu(dst_high, dst_high, TMP);
1461 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001462 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001463 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1464 if (instruction->IsOr()) {
1465 uint32_t low = Low32Bits(value);
1466 uint32_t high = High32Bits(value);
1467 if (IsUint<16>(low)) {
1468 if (dst_low != lhs_low || low != 0) {
1469 __ Ori(dst_low, lhs_low, low);
1470 }
1471 } else {
1472 __ LoadConst32(TMP, low);
1473 __ Or(dst_low, lhs_low, TMP);
1474 }
1475 if (IsUint<16>(high)) {
1476 if (dst_high != lhs_high || high != 0) {
1477 __ Ori(dst_high, lhs_high, high);
1478 }
1479 } else {
1480 if (high != low) {
1481 __ LoadConst32(TMP, high);
1482 }
1483 __ Or(dst_high, lhs_high, TMP);
1484 }
1485 } else if (instruction->IsXor()) {
1486 uint32_t low = Low32Bits(value);
1487 uint32_t high = High32Bits(value);
1488 if (IsUint<16>(low)) {
1489 if (dst_low != lhs_low || low != 0) {
1490 __ Xori(dst_low, lhs_low, low);
1491 }
1492 } else {
1493 __ LoadConst32(TMP, low);
1494 __ Xor(dst_low, lhs_low, TMP);
1495 }
1496 if (IsUint<16>(high)) {
1497 if (dst_high != lhs_high || high != 0) {
1498 __ Xori(dst_high, lhs_high, high);
1499 }
1500 } else {
1501 if (high != low) {
1502 __ LoadConst32(TMP, high);
1503 }
1504 __ Xor(dst_high, lhs_high, TMP);
1505 }
1506 } else if (instruction->IsAnd()) {
1507 uint32_t low = Low32Bits(value);
1508 uint32_t high = High32Bits(value);
1509 if (IsUint<16>(low)) {
1510 __ Andi(dst_low, lhs_low, low);
1511 } else if (low != 0xFFFFFFFF) {
1512 __ LoadConst32(TMP, low);
1513 __ And(dst_low, lhs_low, TMP);
1514 } else if (dst_low != lhs_low) {
1515 __ Move(dst_low, lhs_low);
1516 }
1517 if (IsUint<16>(high)) {
1518 __ Andi(dst_high, lhs_high, high);
1519 } else if (high != 0xFFFFFFFF) {
1520 if (high != low) {
1521 __ LoadConst32(TMP, high);
1522 }
1523 __ And(dst_high, lhs_high, TMP);
1524 } else if (dst_high != lhs_high) {
1525 __ Move(dst_high, lhs_high);
1526 }
1527 } else {
1528 if (instruction->IsSub()) {
1529 value = -value;
1530 } else {
1531 DCHECK(instruction->IsAdd());
1532 }
1533 int32_t low = Low32Bits(value);
1534 int32_t high = High32Bits(value);
1535 if (IsInt<16>(low)) {
1536 if (dst_low != lhs_low || low != 0) {
1537 __ Addiu(dst_low, lhs_low, low);
1538 }
1539 if (low != 0) {
1540 __ Sltiu(AT, dst_low, low);
1541 }
1542 } else {
1543 __ LoadConst32(TMP, low);
1544 __ Addu(dst_low, lhs_low, TMP);
1545 __ Sltu(AT, dst_low, TMP);
1546 }
1547 if (IsInt<16>(high)) {
1548 if (dst_high != lhs_high || high != 0) {
1549 __ Addiu(dst_high, lhs_high, high);
1550 }
1551 } else {
1552 if (high != low) {
1553 __ LoadConst32(TMP, high);
1554 }
1555 __ Addu(dst_high, lhs_high, TMP);
1556 }
1557 if (low != 0) {
1558 __ Addu(dst_high, dst_high, AT);
1559 }
1560 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001561 }
1562 break;
1563 }
1564
1565 case Primitive::kPrimFloat:
1566 case Primitive::kPrimDouble: {
1567 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1568 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1569 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1570 if (instruction->IsAdd()) {
1571 if (type == Primitive::kPrimFloat) {
1572 __ AddS(dst, lhs, rhs);
1573 } else {
1574 __ AddD(dst, lhs, rhs);
1575 }
1576 } else {
1577 DCHECK(instruction->IsSub());
1578 if (type == Primitive::kPrimFloat) {
1579 __ SubS(dst, lhs, rhs);
1580 } else {
1581 __ SubD(dst, lhs, rhs);
1582 }
1583 }
1584 break;
1585 }
1586
1587 default:
1588 LOG(FATAL) << "Unexpected binary operation type " << type;
1589 }
1590}
1591
1592void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001593 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001594
1595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1596 Primitive::Type type = instr->GetResultType();
1597 switch (type) {
1598 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001599 locations->SetInAt(0, Location::RequiresRegister());
1600 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1602 break;
1603 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001604 locations->SetInAt(0, Location::RequiresRegister());
1605 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1606 locations->SetOut(Location::RequiresRegister());
1607 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001608 default:
1609 LOG(FATAL) << "Unexpected shift type " << type;
1610 }
1611}
1612
1613static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1614
1615void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001616 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001617 LocationSummary* locations = instr->GetLocations();
1618 Primitive::Type type = instr->GetType();
1619
1620 Location rhs_location = locations->InAt(1);
1621 bool use_imm = rhs_location.IsConstant();
1622 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1623 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001624 const uint32_t shift_mask =
1625 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001626 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001627 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1628 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001629
1630 switch (type) {
1631 case Primitive::kPrimInt: {
1632 Register dst = locations->Out().AsRegister<Register>();
1633 Register lhs = locations->InAt(0).AsRegister<Register>();
1634 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001635 if (shift_value == 0) {
1636 if (dst != lhs) {
1637 __ Move(dst, lhs);
1638 }
1639 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001640 __ Sll(dst, lhs, shift_value);
1641 } else if (instr->IsShr()) {
1642 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001643 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001644 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001645 } else {
1646 if (has_ins_rotr) {
1647 __ Rotr(dst, lhs, shift_value);
1648 } else {
1649 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1650 __ Srl(dst, lhs, shift_value);
1651 __ Or(dst, dst, TMP);
1652 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001653 }
1654 } else {
1655 if (instr->IsShl()) {
1656 __ Sllv(dst, lhs, rhs_reg);
1657 } else if (instr->IsShr()) {
1658 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001659 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001660 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001661 } else {
1662 if (has_ins_rotr) {
1663 __ Rotrv(dst, lhs, rhs_reg);
1664 } else {
1665 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001666 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1667 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1668 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1669 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1670 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001671 __ Sllv(TMP, lhs, TMP);
1672 __ Srlv(dst, lhs, rhs_reg);
1673 __ Or(dst, dst, TMP);
1674 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001675 }
1676 }
1677 break;
1678 }
1679
1680 case Primitive::kPrimLong: {
1681 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1682 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1683 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1684 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1685 if (use_imm) {
1686 if (shift_value == 0) {
1687 codegen_->Move64(locations->Out(), locations->InAt(0));
1688 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001689 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001690 if (instr->IsShl()) {
1691 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1692 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1693 __ Sll(dst_low, lhs_low, shift_value);
1694 } else if (instr->IsShr()) {
1695 __ Srl(dst_low, lhs_low, shift_value);
1696 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1697 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001698 } else if (instr->IsUShr()) {
1699 __ Srl(dst_low, lhs_low, shift_value);
1700 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1701 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001702 } else {
1703 __ Srl(dst_low, lhs_low, shift_value);
1704 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1705 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001706 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001707 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001708 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001709 if (instr->IsShl()) {
1710 __ Sll(dst_low, lhs_low, shift_value);
1711 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1712 __ Sll(dst_high, lhs_high, shift_value);
1713 __ Or(dst_high, dst_high, TMP);
1714 } else if (instr->IsShr()) {
1715 __ Sra(dst_high, lhs_high, shift_value);
1716 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1717 __ Srl(dst_low, lhs_low, shift_value);
1718 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001719 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001720 __ Srl(dst_high, lhs_high, shift_value);
1721 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1722 __ Srl(dst_low, lhs_low, shift_value);
1723 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001724 } else {
1725 __ Srl(TMP, lhs_low, shift_value);
1726 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1727 __ Or(dst_low, dst_low, TMP);
1728 __ Srl(TMP, lhs_high, shift_value);
1729 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1730 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001731 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001732 }
1733 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001734 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001735 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001736 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001737 __ Move(dst_low, ZERO);
1738 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001739 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001740 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001741 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001742 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001743 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001744 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001745 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001746 // 64-bit rotation by 32 is just a swap.
1747 __ Move(dst_low, lhs_high);
1748 __ Move(dst_high, lhs_low);
1749 } else {
1750 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001751 __ Srl(dst_low, lhs_high, shift_value_high);
1752 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1753 __ Srl(dst_high, lhs_low, shift_value_high);
1754 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001755 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001756 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1757 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001758 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001759 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1760 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001761 __ Or(dst_high, dst_high, TMP);
1762 }
1763 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001764 }
1765 }
1766 } else {
1767 MipsLabel done;
1768 if (instr->IsShl()) {
1769 __ Sllv(dst_low, lhs_low, rhs_reg);
1770 __ Nor(AT, ZERO, rhs_reg);
1771 __ Srl(TMP, lhs_low, 1);
1772 __ Srlv(TMP, TMP, AT);
1773 __ Sllv(dst_high, lhs_high, rhs_reg);
1774 __ Or(dst_high, dst_high, TMP);
1775 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1776 __ Beqz(TMP, &done);
1777 __ Move(dst_high, dst_low);
1778 __ Move(dst_low, ZERO);
1779 } else if (instr->IsShr()) {
1780 __ Srav(dst_high, lhs_high, rhs_reg);
1781 __ Nor(AT, ZERO, rhs_reg);
1782 __ Sll(TMP, lhs_high, 1);
1783 __ Sllv(TMP, TMP, AT);
1784 __ Srlv(dst_low, lhs_low, rhs_reg);
1785 __ Or(dst_low, dst_low, TMP);
1786 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1787 __ Beqz(TMP, &done);
1788 __ Move(dst_low, dst_high);
1789 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001790 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001791 __ Srlv(dst_high, lhs_high, rhs_reg);
1792 __ Nor(AT, ZERO, rhs_reg);
1793 __ Sll(TMP, lhs_high, 1);
1794 __ Sllv(TMP, TMP, AT);
1795 __ Srlv(dst_low, lhs_low, rhs_reg);
1796 __ Or(dst_low, dst_low, TMP);
1797 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1798 __ Beqz(TMP, &done);
1799 __ Move(dst_low, dst_high);
1800 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001801 } else {
1802 __ Nor(AT, ZERO, rhs_reg);
1803 __ Srlv(TMP, lhs_low, rhs_reg);
1804 __ Sll(dst_low, lhs_high, 1);
1805 __ Sllv(dst_low, dst_low, AT);
1806 __ Or(dst_low, dst_low, TMP);
1807 __ Srlv(TMP, lhs_high, rhs_reg);
1808 __ Sll(dst_high, lhs_low, 1);
1809 __ Sllv(dst_high, dst_high, AT);
1810 __ Or(dst_high, dst_high, TMP);
1811 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1812 __ Beqz(TMP, &done);
1813 __ Move(TMP, dst_high);
1814 __ Move(dst_high, dst_low);
1815 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001816 }
1817 __ Bind(&done);
1818 }
1819 break;
1820 }
1821
1822 default:
1823 LOG(FATAL) << "Unexpected shift operation type " << type;
1824 }
1825}
1826
1827void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1828 HandleBinaryOp(instruction);
1829}
1830
1831void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1832 HandleBinaryOp(instruction);
1833}
1834
1835void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1836 HandleBinaryOp(instruction);
1837}
1838
1839void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1840 HandleBinaryOp(instruction);
1841}
1842
1843void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1844 LocationSummary* locations =
1845 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1846 locations->SetInAt(0, Location::RequiresRegister());
1847 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1848 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1849 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1850 } else {
1851 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1852 }
1853}
1854
Alexey Frunze2923db72016-08-20 01:55:47 -07001855auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1856 auto null_checker = [this, instruction]() {
1857 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1858 };
1859 return null_checker;
1860}
1861
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001862void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1863 LocationSummary* locations = instruction->GetLocations();
1864 Register obj = locations->InAt(0).AsRegister<Register>();
1865 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001866 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001867 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001868
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001869 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001870 switch (type) {
1871 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872 Register out = locations->Out().AsRegister<Register>();
1873 if (index.IsConstant()) {
1874 size_t offset =
1875 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001876 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001877 } else {
1878 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001879 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880 }
1881 break;
1882 }
1883
1884 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001885 Register out = locations->Out().AsRegister<Register>();
1886 if (index.IsConstant()) {
1887 size_t offset =
1888 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001889 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001890 } else {
1891 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001892 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893 }
1894 break;
1895 }
1896
1897 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001898 Register out = locations->Out().AsRegister<Register>();
1899 if (index.IsConstant()) {
1900 size_t offset =
1901 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001902 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001903 } else {
1904 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1905 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001906 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001907 }
1908 break;
1909 }
1910
1911 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912 Register out = locations->Out().AsRegister<Register>();
1913 if (index.IsConstant()) {
1914 size_t offset =
1915 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001916 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001917 } else {
1918 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1919 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001920 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921 }
1922 break;
1923 }
1924
1925 case Primitive::kPrimInt:
1926 case Primitive::kPrimNot: {
1927 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001928 Register out = locations->Out().AsRegister<Register>();
1929 if (index.IsConstant()) {
1930 size_t offset =
1931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001932 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933 } else {
1934 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1935 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001936 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937 }
1938 break;
1939 }
1940
1941 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942 Register out = locations->Out().AsRegisterPairLow<Register>();
1943 if (index.IsConstant()) {
1944 size_t offset =
1945 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001946 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947 } else {
1948 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1949 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001950 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951 }
1952 break;
1953 }
1954
1955 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1957 if (index.IsConstant()) {
1958 size_t offset =
1959 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001960 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961 } else {
1962 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1963 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001964 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001965 }
1966 break;
1967 }
1968
1969 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001970 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1971 if (index.IsConstant()) {
1972 size_t offset =
1973 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001974 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001975 } else {
1976 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1977 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001978 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001979 }
1980 break;
1981 }
1982
1983 case Primitive::kPrimVoid:
1984 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1985 UNREACHABLE();
1986 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001987}
1988
1989void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1990 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1991 locations->SetInAt(0, Location::RequiresRegister());
1992 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1993}
1994
1995void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1996 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001997 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001998 Register obj = locations->InAt(0).AsRegister<Register>();
1999 Register out = locations->Out().AsRegister<Register>();
2000 __ LoadFromOffset(kLoadWord, out, obj, offset);
2001 codegen_->MaybeRecordImplicitNullCheck(instruction);
2002}
2003
2004void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002005 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2007 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002008 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002009 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002010 InvokeRuntimeCallingConvention calling_convention;
2011 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2012 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2013 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2014 } else {
2015 locations->SetInAt(0, Location::RequiresRegister());
2016 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2017 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2018 locations->SetInAt(2, Location::RequiresFpuRegister());
2019 } else {
2020 locations->SetInAt(2, Location::RequiresRegister());
2021 }
2022 }
2023}
2024
2025void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2026 LocationSummary* locations = instruction->GetLocations();
2027 Register obj = locations->InAt(0).AsRegister<Register>();
2028 Location index = locations->InAt(1);
2029 Primitive::Type value_type = instruction->GetComponentType();
2030 bool needs_runtime_call = locations->WillCall();
2031 bool needs_write_barrier =
2032 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002033 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002034
2035 switch (value_type) {
2036 case Primitive::kPrimBoolean:
2037 case Primitive::kPrimByte: {
2038 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
2039 Register value = locations->InAt(2).AsRegister<Register>();
2040 if (index.IsConstant()) {
2041 size_t offset =
2042 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002043 __ StoreToOffset(kStoreByte, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044 } else {
2045 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002046 __ StoreToOffset(kStoreByte, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002047 }
2048 break;
2049 }
2050
2051 case Primitive::kPrimShort:
2052 case Primitive::kPrimChar: {
2053 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
2054 Register value = locations->InAt(2).AsRegister<Register>();
2055 if (index.IsConstant()) {
2056 size_t offset =
2057 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002058 __ StoreToOffset(kStoreHalfword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059 } else {
2060 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
2061 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002062 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 }
2064 break;
2065 }
2066
2067 case Primitive::kPrimInt:
2068 case Primitive::kPrimNot: {
2069 if (!needs_runtime_call) {
2070 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2071 Register value = locations->InAt(2).AsRegister<Register>();
2072 if (index.IsConstant()) {
2073 size_t offset =
2074 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002075 __ StoreToOffset(kStoreWord, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 } else {
2077 DCHECK(index.IsRegister()) << index;
2078 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2079 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002080 __ StoreToOffset(kStoreWord, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002081 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002082 if (needs_write_barrier) {
2083 DCHECK_EQ(value_type, Primitive::kPrimNot);
2084 codegen_->MarkGCCard(obj, value);
2085 }
2086 } else {
2087 DCHECK_EQ(value_type, Primitive::kPrimNot);
2088 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
2089 instruction,
2090 instruction->GetDexPc(),
2091 nullptr,
2092 IsDirectEntrypoint(kQuickAputObject));
2093 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2094 }
2095 break;
2096 }
2097
2098 case Primitive::kPrimLong: {
2099 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2100 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2101 if (index.IsConstant()) {
2102 size_t offset =
2103 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002104 __ StoreToOffset(kStoreDoubleword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002105 } else {
2106 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2107 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002108 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002109 }
2110 break;
2111 }
2112
2113 case Primitive::kPrimFloat: {
2114 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2115 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2116 DCHECK(locations->InAt(2).IsFpuRegister());
2117 if (index.IsConstant()) {
2118 size_t offset =
2119 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002120 __ StoreSToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002121 } else {
2122 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2123 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002124 __ StoreSToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002125 }
2126 break;
2127 }
2128
2129 case Primitive::kPrimDouble: {
2130 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2131 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2132 DCHECK(locations->InAt(2).IsFpuRegister());
2133 if (index.IsConstant()) {
2134 size_t offset =
2135 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002136 __ StoreDToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002137 } else {
2138 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2139 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002140 __ StoreDToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002141 }
2142 break;
2143 }
2144
2145 case Primitive::kPrimVoid:
2146 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2147 UNREACHABLE();
2148 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002149}
2150
2151void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2152 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2153 ? LocationSummary::kCallOnSlowPath
2154 : LocationSummary::kNoCall;
2155 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2156 locations->SetInAt(0, Location::RequiresRegister());
2157 locations->SetInAt(1, Location::RequiresRegister());
2158 if (instruction->HasUses()) {
2159 locations->SetOut(Location::SameAsFirstInput());
2160 }
2161}
2162
2163void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2164 LocationSummary* locations = instruction->GetLocations();
2165 BoundsCheckSlowPathMIPS* slow_path =
2166 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2167 codegen_->AddSlowPath(slow_path);
2168
2169 Register index = locations->InAt(0).AsRegister<Register>();
2170 Register length = locations->InAt(1).AsRegister<Register>();
2171
2172 // length is limited by the maximum positive signed 32-bit integer.
2173 // Unsigned comparison of length and index checks for index < 0
2174 // and for length <= index simultaneously.
2175 __ Bgeu(index, length, slow_path->GetEntryLabel());
2176}
2177
2178void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2179 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2180 instruction,
2181 LocationSummary::kCallOnSlowPath);
2182 locations->SetInAt(0, Location::RequiresRegister());
2183 locations->SetInAt(1, Location::RequiresRegister());
2184 // Note that TypeCheckSlowPathMIPS uses this register too.
2185 locations->AddTemp(Location::RequiresRegister());
2186}
2187
2188void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2189 LocationSummary* locations = instruction->GetLocations();
2190 Register obj = locations->InAt(0).AsRegister<Register>();
2191 Register cls = locations->InAt(1).AsRegister<Register>();
2192 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2193
2194 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2195 codegen_->AddSlowPath(slow_path);
2196
2197 // TODO: avoid this check if we know obj is not null.
2198 __ Beqz(obj, slow_path->GetExitLabel());
2199 // Compare the class of `obj` with `cls`.
2200 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2201 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2202 __ Bind(slow_path->GetExitLabel());
2203}
2204
2205void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2206 LocationSummary* locations =
2207 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2208 locations->SetInAt(0, Location::RequiresRegister());
2209 if (check->HasUses()) {
2210 locations->SetOut(Location::SameAsFirstInput());
2211 }
2212}
2213
2214void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2215 // We assume the class is not null.
2216 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2217 check->GetLoadClass(),
2218 check,
2219 check->GetDexPc(),
2220 true);
2221 codegen_->AddSlowPath(slow_path);
2222 GenerateClassInitializationCheck(slow_path,
2223 check->GetLocations()->InAt(0).AsRegister<Register>());
2224}
2225
2226void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2227 Primitive::Type in_type = compare->InputAt(0)->GetType();
2228
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002229 LocationSummary* locations =
2230 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002231
2232 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002233 case Primitive::kPrimBoolean:
2234 case Primitive::kPrimByte:
2235 case Primitive::kPrimShort:
2236 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002237 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002238 case Primitive::kPrimLong:
2239 locations->SetInAt(0, Location::RequiresRegister());
2240 locations->SetInAt(1, Location::RequiresRegister());
2241 // Output overlaps because it is written before doing the low comparison.
2242 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2243 break;
2244
2245 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002246 case Primitive::kPrimDouble:
2247 locations->SetInAt(0, Location::RequiresFpuRegister());
2248 locations->SetInAt(1, Location::RequiresFpuRegister());
2249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002250 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002251
2252 default:
2253 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2254 }
2255}
2256
2257void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2258 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002259 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002261 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262
2263 // 0 if: left == right
2264 // 1 if: left > right
2265 // -1 if: left < right
2266 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002267 case Primitive::kPrimBoolean:
2268 case Primitive::kPrimByte:
2269 case Primitive::kPrimShort:
2270 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002271 case Primitive::kPrimInt: {
2272 Register lhs = locations->InAt(0).AsRegister<Register>();
2273 Register rhs = locations->InAt(1).AsRegister<Register>();
2274 __ Slt(TMP, lhs, rhs);
2275 __ Slt(res, rhs, lhs);
2276 __ Subu(res, res, TMP);
2277 break;
2278 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002279 case Primitive::kPrimLong: {
2280 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002281 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2282 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2283 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2284 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2285 // TODO: more efficient (direct) comparison with a constant.
2286 __ Slt(TMP, lhs_high, rhs_high);
2287 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2288 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2289 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2290 __ Sltu(TMP, lhs_low, rhs_low);
2291 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2292 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2293 __ Bind(&done);
2294 break;
2295 }
2296
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002297 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002298 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002299 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2300 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2301 MipsLabel done;
2302 if (isR6) {
2303 __ CmpEqS(FTMP, lhs, rhs);
2304 __ LoadConst32(res, 0);
2305 __ Bc1nez(FTMP, &done);
2306 if (gt_bias) {
2307 __ CmpLtS(FTMP, lhs, rhs);
2308 __ LoadConst32(res, -1);
2309 __ Bc1nez(FTMP, &done);
2310 __ LoadConst32(res, 1);
2311 } else {
2312 __ CmpLtS(FTMP, rhs, lhs);
2313 __ LoadConst32(res, 1);
2314 __ Bc1nez(FTMP, &done);
2315 __ LoadConst32(res, -1);
2316 }
2317 } else {
2318 if (gt_bias) {
2319 __ ColtS(0, lhs, rhs);
2320 __ LoadConst32(res, -1);
2321 __ Bc1t(0, &done);
2322 __ CeqS(0, lhs, rhs);
2323 __ LoadConst32(res, 1);
2324 __ Movt(res, ZERO, 0);
2325 } else {
2326 __ ColtS(0, rhs, lhs);
2327 __ LoadConst32(res, 1);
2328 __ Bc1t(0, &done);
2329 __ CeqS(0, lhs, rhs);
2330 __ LoadConst32(res, -1);
2331 __ Movt(res, ZERO, 0);
2332 }
2333 }
2334 __ Bind(&done);
2335 break;
2336 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002338 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002339 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2340 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2341 MipsLabel done;
2342 if (isR6) {
2343 __ CmpEqD(FTMP, lhs, rhs);
2344 __ LoadConst32(res, 0);
2345 __ Bc1nez(FTMP, &done);
2346 if (gt_bias) {
2347 __ CmpLtD(FTMP, lhs, rhs);
2348 __ LoadConst32(res, -1);
2349 __ Bc1nez(FTMP, &done);
2350 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002351 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002352 __ CmpLtD(FTMP, rhs, lhs);
2353 __ LoadConst32(res, 1);
2354 __ Bc1nez(FTMP, &done);
2355 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 }
2357 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002358 if (gt_bias) {
2359 __ ColtD(0, lhs, rhs);
2360 __ LoadConst32(res, -1);
2361 __ Bc1t(0, &done);
2362 __ CeqD(0, lhs, rhs);
2363 __ LoadConst32(res, 1);
2364 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002366 __ ColtD(0, rhs, lhs);
2367 __ LoadConst32(res, 1);
2368 __ Bc1t(0, &done);
2369 __ CeqD(0, lhs, rhs);
2370 __ LoadConst32(res, -1);
2371 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002372 }
2373 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002374 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002375 break;
2376 }
2377
2378 default:
2379 LOG(FATAL) << "Unimplemented compare type " << in_type;
2380 }
2381}
2382
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002383void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002385 switch (instruction->InputAt(0)->GetType()) {
2386 default:
2387 case Primitive::kPrimLong:
2388 locations->SetInAt(0, Location::RequiresRegister());
2389 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2390 break;
2391
2392 case Primitive::kPrimFloat:
2393 case Primitive::kPrimDouble:
2394 locations->SetInAt(0, Location::RequiresFpuRegister());
2395 locations->SetInAt(1, Location::RequiresFpuRegister());
2396 break;
2397 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002398 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2400 }
2401}
2402
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002403void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002404 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002405 return;
2406 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002407
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002408 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002409 LocationSummary* locations = instruction->GetLocations();
2410 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002411 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002412
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002413 switch (type) {
2414 default:
2415 // Integer case.
2416 GenerateIntCompare(instruction->GetCondition(), locations);
2417 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002419 case Primitive::kPrimLong:
2420 // TODO: don't use branches.
2421 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002422 break;
2423
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002424 case Primitive::kPrimFloat:
2425 case Primitive::kPrimDouble:
2426 // TODO: don't use branches.
2427 GenerateFpCompareAndBranch(instruction->GetCondition(),
2428 instruction->IsGtBias(),
2429 type,
2430 locations,
2431 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 break;
2433 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002434
2435 // Convert the branches into the result.
2436 MipsLabel done;
2437
2438 // False case: result = 0.
2439 __ LoadConst32(dst, 0);
2440 __ B(&done);
2441
2442 // True case: result = 1.
2443 __ Bind(&true_label);
2444 __ LoadConst32(dst, 1);
2445 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002446}
2447
Alexey Frunze7e99e052015-11-24 19:28:01 -08002448void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2449 DCHECK(instruction->IsDiv() || instruction->IsRem());
2450 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2451
2452 LocationSummary* locations = instruction->GetLocations();
2453 Location second = locations->InAt(1);
2454 DCHECK(second.IsConstant());
2455
2456 Register out = locations->Out().AsRegister<Register>();
2457 Register dividend = locations->InAt(0).AsRegister<Register>();
2458 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2459 DCHECK(imm == 1 || imm == -1);
2460
2461 if (instruction->IsRem()) {
2462 __ Move(out, ZERO);
2463 } else {
2464 if (imm == -1) {
2465 __ Subu(out, ZERO, dividend);
2466 } else if (out != dividend) {
2467 __ Move(out, dividend);
2468 }
2469 }
2470}
2471
2472void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2473 DCHECK(instruction->IsDiv() || instruction->IsRem());
2474 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2475
2476 LocationSummary* locations = instruction->GetLocations();
2477 Location second = locations->InAt(1);
2478 DCHECK(second.IsConstant());
2479
2480 Register out = locations->Out().AsRegister<Register>();
2481 Register dividend = locations->InAt(0).AsRegister<Register>();
2482 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002483 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002484 int ctz_imm = CTZ(abs_imm);
2485
2486 if (instruction->IsDiv()) {
2487 if (ctz_imm == 1) {
2488 // Fast path for division by +/-2, which is very common.
2489 __ Srl(TMP, dividend, 31);
2490 } else {
2491 __ Sra(TMP, dividend, 31);
2492 __ Srl(TMP, TMP, 32 - ctz_imm);
2493 }
2494 __ Addu(out, dividend, TMP);
2495 __ Sra(out, out, ctz_imm);
2496 if (imm < 0) {
2497 __ Subu(out, ZERO, out);
2498 }
2499 } else {
2500 if (ctz_imm == 1) {
2501 // Fast path for modulo +/-2, which is very common.
2502 __ Sra(TMP, dividend, 31);
2503 __ Subu(out, dividend, TMP);
2504 __ Andi(out, out, 1);
2505 __ Addu(out, out, TMP);
2506 } else {
2507 __ Sra(TMP, dividend, 31);
2508 __ Srl(TMP, TMP, 32 - ctz_imm);
2509 __ Addu(out, dividend, TMP);
2510 if (IsUint<16>(abs_imm - 1)) {
2511 __ Andi(out, out, abs_imm - 1);
2512 } else {
2513 __ Sll(out, out, 32 - ctz_imm);
2514 __ Srl(out, out, 32 - ctz_imm);
2515 }
2516 __ Subu(out, out, TMP);
2517 }
2518 }
2519}
2520
2521void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2522 DCHECK(instruction->IsDiv() || instruction->IsRem());
2523 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2524
2525 LocationSummary* locations = instruction->GetLocations();
2526 Location second = locations->InAt(1);
2527 DCHECK(second.IsConstant());
2528
2529 Register out = locations->Out().AsRegister<Register>();
2530 Register dividend = locations->InAt(0).AsRegister<Register>();
2531 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2532
2533 int64_t magic;
2534 int shift;
2535 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2536
2537 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2538
2539 __ LoadConst32(TMP, magic);
2540 if (isR6) {
2541 __ MuhR6(TMP, dividend, TMP);
2542 } else {
2543 __ MultR2(dividend, TMP);
2544 __ Mfhi(TMP);
2545 }
2546 if (imm > 0 && magic < 0) {
2547 __ Addu(TMP, TMP, dividend);
2548 } else if (imm < 0 && magic > 0) {
2549 __ Subu(TMP, TMP, dividend);
2550 }
2551
2552 if (shift != 0) {
2553 __ Sra(TMP, TMP, shift);
2554 }
2555
2556 if (instruction->IsDiv()) {
2557 __ Sra(out, TMP, 31);
2558 __ Subu(out, TMP, out);
2559 } else {
2560 __ Sra(AT, TMP, 31);
2561 __ Subu(AT, TMP, AT);
2562 __ LoadConst32(TMP, imm);
2563 if (isR6) {
2564 __ MulR6(TMP, AT, TMP);
2565 } else {
2566 __ MulR2(TMP, AT, TMP);
2567 }
2568 __ Subu(out, dividend, TMP);
2569 }
2570}
2571
2572void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2573 DCHECK(instruction->IsDiv() || instruction->IsRem());
2574 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2575
2576 LocationSummary* locations = instruction->GetLocations();
2577 Register out = locations->Out().AsRegister<Register>();
2578 Location second = locations->InAt(1);
2579
2580 if (second.IsConstant()) {
2581 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2582 if (imm == 0) {
2583 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2584 } else if (imm == 1 || imm == -1) {
2585 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002586 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002587 DivRemByPowerOfTwo(instruction);
2588 } else {
2589 DCHECK(imm <= -2 || imm >= 2);
2590 GenerateDivRemWithAnyConstant(instruction);
2591 }
2592 } else {
2593 Register dividend = locations->InAt(0).AsRegister<Register>();
2594 Register divisor = second.AsRegister<Register>();
2595 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2596 if (instruction->IsDiv()) {
2597 if (isR6) {
2598 __ DivR6(out, dividend, divisor);
2599 } else {
2600 __ DivR2(out, dividend, divisor);
2601 }
2602 } else {
2603 if (isR6) {
2604 __ ModR6(out, dividend, divisor);
2605 } else {
2606 __ ModR2(out, dividend, divisor);
2607 }
2608 }
2609 }
2610}
2611
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002612void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2613 Primitive::Type type = div->GetResultType();
2614 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002615 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002616 : LocationSummary::kNoCall;
2617
2618 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2619
2620 switch (type) {
2621 case Primitive::kPrimInt:
2622 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002623 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2625 break;
2626
2627 case Primitive::kPrimLong: {
2628 InvokeRuntimeCallingConvention calling_convention;
2629 locations->SetInAt(0, Location::RegisterPairLocation(
2630 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2631 locations->SetInAt(1, Location::RegisterPairLocation(
2632 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2633 locations->SetOut(calling_convention.GetReturnLocation(type));
2634 break;
2635 }
2636
2637 case Primitive::kPrimFloat:
2638 case Primitive::kPrimDouble:
2639 locations->SetInAt(0, Location::RequiresFpuRegister());
2640 locations->SetInAt(1, Location::RequiresFpuRegister());
2641 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2642 break;
2643
2644 default:
2645 LOG(FATAL) << "Unexpected div type " << type;
2646 }
2647}
2648
2649void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2650 Primitive::Type type = instruction->GetType();
2651 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002652
2653 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002654 case Primitive::kPrimInt:
2655 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002657 case Primitive::kPrimLong: {
2658 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2659 instruction,
2660 instruction->GetDexPc(),
2661 nullptr,
2662 IsDirectEntrypoint(kQuickLdiv));
2663 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2664 break;
2665 }
2666 case Primitive::kPrimFloat:
2667 case Primitive::kPrimDouble: {
2668 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2669 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2670 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2671 if (type == Primitive::kPrimFloat) {
2672 __ DivS(dst, lhs, rhs);
2673 } else {
2674 __ DivD(dst, lhs, rhs);
2675 }
2676 break;
2677 }
2678 default:
2679 LOG(FATAL) << "Unexpected div type " << type;
2680 }
2681}
2682
2683void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2684 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2685 ? LocationSummary::kCallOnSlowPath
2686 : LocationSummary::kNoCall;
2687 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2688 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2689 if (instruction->HasUses()) {
2690 locations->SetOut(Location::SameAsFirstInput());
2691 }
2692}
2693
2694void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2695 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2696 codegen_->AddSlowPath(slow_path);
2697 Location value = instruction->GetLocations()->InAt(0);
2698 Primitive::Type type = instruction->GetType();
2699
2700 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002701 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002702 case Primitive::kPrimByte:
2703 case Primitive::kPrimChar:
2704 case Primitive::kPrimShort:
2705 case Primitive::kPrimInt: {
2706 if (value.IsConstant()) {
2707 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2708 __ B(slow_path->GetEntryLabel());
2709 } else {
2710 // A division by a non-null constant is valid. We don't need to perform
2711 // any check, so simply fall through.
2712 }
2713 } else {
2714 DCHECK(value.IsRegister()) << value;
2715 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2716 }
2717 break;
2718 }
2719 case Primitive::kPrimLong: {
2720 if (value.IsConstant()) {
2721 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2722 __ B(slow_path->GetEntryLabel());
2723 } else {
2724 // A division by a non-null constant is valid. We don't need to perform
2725 // any check, so simply fall through.
2726 }
2727 } else {
2728 DCHECK(value.IsRegisterPair()) << value;
2729 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2730 __ Beqz(TMP, slow_path->GetEntryLabel());
2731 }
2732 break;
2733 }
2734 default:
2735 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2736 }
2737}
2738
2739void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2740 LocationSummary* locations =
2741 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2742 locations->SetOut(Location::ConstantLocation(constant));
2743}
2744
2745void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2746 // Will be generated at use site.
2747}
2748
2749void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2750 exit->SetLocations(nullptr);
2751}
2752
2753void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2754}
2755
2756void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2757 LocationSummary* locations =
2758 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2759 locations->SetOut(Location::ConstantLocation(constant));
2760}
2761
2762void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2763 // Will be generated at use site.
2764}
2765
2766void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2767 got->SetLocations(nullptr);
2768}
2769
2770void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2771 DCHECK(!successor->IsExitBlock());
2772 HBasicBlock* block = got->GetBlock();
2773 HInstruction* previous = got->GetPrevious();
2774 HLoopInformation* info = block->GetLoopInformation();
2775
2776 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2777 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2778 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2779 return;
2780 }
2781 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2782 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2783 }
2784 if (!codegen_->GoesToNextBlock(block, successor)) {
2785 __ B(codegen_->GetLabelOf(successor));
2786 }
2787}
2788
2789void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2790 HandleGoto(got, got->GetSuccessor());
2791}
2792
2793void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2794 try_boundary->SetLocations(nullptr);
2795}
2796
2797void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2798 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2799 if (!successor->IsExitBlock()) {
2800 HandleGoto(try_boundary, successor);
2801 }
2802}
2803
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002804void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2805 LocationSummary* locations) {
2806 Register dst = locations->Out().AsRegister<Register>();
2807 Register lhs = locations->InAt(0).AsRegister<Register>();
2808 Location rhs_location = locations->InAt(1);
2809 Register rhs_reg = ZERO;
2810 int64_t rhs_imm = 0;
2811 bool use_imm = rhs_location.IsConstant();
2812 if (use_imm) {
2813 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2814 } else {
2815 rhs_reg = rhs_location.AsRegister<Register>();
2816 }
2817
2818 switch (cond) {
2819 case kCondEQ:
2820 case kCondNE:
2821 if (use_imm && IsUint<16>(rhs_imm)) {
2822 __ Xori(dst, lhs, rhs_imm);
2823 } else {
2824 if (use_imm) {
2825 rhs_reg = TMP;
2826 __ LoadConst32(rhs_reg, rhs_imm);
2827 }
2828 __ Xor(dst, lhs, rhs_reg);
2829 }
2830 if (cond == kCondEQ) {
2831 __ Sltiu(dst, dst, 1);
2832 } else {
2833 __ Sltu(dst, ZERO, dst);
2834 }
2835 break;
2836
2837 case kCondLT:
2838 case kCondGE:
2839 if (use_imm && IsInt<16>(rhs_imm)) {
2840 __ Slti(dst, lhs, rhs_imm);
2841 } else {
2842 if (use_imm) {
2843 rhs_reg = TMP;
2844 __ LoadConst32(rhs_reg, rhs_imm);
2845 }
2846 __ Slt(dst, lhs, rhs_reg);
2847 }
2848 if (cond == kCondGE) {
2849 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2850 // only the slt instruction but no sge.
2851 __ Xori(dst, dst, 1);
2852 }
2853 break;
2854
2855 case kCondLE:
2856 case kCondGT:
2857 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2858 // Simulate lhs <= rhs via lhs < rhs + 1.
2859 __ Slti(dst, lhs, rhs_imm + 1);
2860 if (cond == kCondGT) {
2861 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2862 // only the slti instruction but no sgti.
2863 __ Xori(dst, dst, 1);
2864 }
2865 } else {
2866 if (use_imm) {
2867 rhs_reg = TMP;
2868 __ LoadConst32(rhs_reg, rhs_imm);
2869 }
2870 __ Slt(dst, rhs_reg, lhs);
2871 if (cond == kCondLE) {
2872 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2873 // only the slt instruction but no sle.
2874 __ Xori(dst, dst, 1);
2875 }
2876 }
2877 break;
2878
2879 case kCondB:
2880 case kCondAE:
2881 if (use_imm && IsInt<16>(rhs_imm)) {
2882 // Sltiu sign-extends its 16-bit immediate operand before
2883 // the comparison and thus lets us compare directly with
2884 // unsigned values in the ranges [0, 0x7fff] and
2885 // [0xffff8000, 0xffffffff].
2886 __ Sltiu(dst, lhs, rhs_imm);
2887 } else {
2888 if (use_imm) {
2889 rhs_reg = TMP;
2890 __ LoadConst32(rhs_reg, rhs_imm);
2891 }
2892 __ Sltu(dst, lhs, rhs_reg);
2893 }
2894 if (cond == kCondAE) {
2895 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2896 // only the sltu instruction but no sgeu.
2897 __ Xori(dst, dst, 1);
2898 }
2899 break;
2900
2901 case kCondBE:
2902 case kCondA:
2903 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2904 // Simulate lhs <= rhs via lhs < rhs + 1.
2905 // Note that this only works if rhs + 1 does not overflow
2906 // to 0, hence the check above.
2907 // Sltiu sign-extends its 16-bit immediate operand before
2908 // the comparison and thus lets us compare directly with
2909 // unsigned values in the ranges [0, 0x7fff] and
2910 // [0xffff8000, 0xffffffff].
2911 __ Sltiu(dst, lhs, rhs_imm + 1);
2912 if (cond == kCondA) {
2913 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2914 // only the sltiu instruction but no sgtiu.
2915 __ Xori(dst, dst, 1);
2916 }
2917 } else {
2918 if (use_imm) {
2919 rhs_reg = TMP;
2920 __ LoadConst32(rhs_reg, rhs_imm);
2921 }
2922 __ Sltu(dst, rhs_reg, lhs);
2923 if (cond == kCondBE) {
2924 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2925 // only the sltu instruction but no sleu.
2926 __ Xori(dst, dst, 1);
2927 }
2928 }
2929 break;
2930 }
2931}
2932
2933void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2934 LocationSummary* locations,
2935 MipsLabel* label) {
2936 Register lhs = locations->InAt(0).AsRegister<Register>();
2937 Location rhs_location = locations->InAt(1);
2938 Register rhs_reg = ZERO;
2939 int32_t rhs_imm = 0;
2940 bool use_imm = rhs_location.IsConstant();
2941 if (use_imm) {
2942 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2943 } else {
2944 rhs_reg = rhs_location.AsRegister<Register>();
2945 }
2946
2947 if (use_imm && rhs_imm == 0) {
2948 switch (cond) {
2949 case kCondEQ:
2950 case kCondBE: // <= 0 if zero
2951 __ Beqz(lhs, label);
2952 break;
2953 case kCondNE:
2954 case kCondA: // > 0 if non-zero
2955 __ Bnez(lhs, label);
2956 break;
2957 case kCondLT:
2958 __ Bltz(lhs, label);
2959 break;
2960 case kCondGE:
2961 __ Bgez(lhs, label);
2962 break;
2963 case kCondLE:
2964 __ Blez(lhs, label);
2965 break;
2966 case kCondGT:
2967 __ Bgtz(lhs, label);
2968 break;
2969 case kCondB: // always false
2970 break;
2971 case kCondAE: // always true
2972 __ B(label);
2973 break;
2974 }
2975 } else {
2976 if (use_imm) {
2977 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2978 rhs_reg = TMP;
2979 __ LoadConst32(rhs_reg, rhs_imm);
2980 }
2981 switch (cond) {
2982 case kCondEQ:
2983 __ Beq(lhs, rhs_reg, label);
2984 break;
2985 case kCondNE:
2986 __ Bne(lhs, rhs_reg, label);
2987 break;
2988 case kCondLT:
2989 __ Blt(lhs, rhs_reg, label);
2990 break;
2991 case kCondGE:
2992 __ Bge(lhs, rhs_reg, label);
2993 break;
2994 case kCondLE:
2995 __ Bge(rhs_reg, lhs, label);
2996 break;
2997 case kCondGT:
2998 __ Blt(rhs_reg, lhs, label);
2999 break;
3000 case kCondB:
3001 __ Bltu(lhs, rhs_reg, label);
3002 break;
3003 case kCondAE:
3004 __ Bgeu(lhs, rhs_reg, label);
3005 break;
3006 case kCondBE:
3007 __ Bgeu(rhs_reg, lhs, label);
3008 break;
3009 case kCondA:
3010 __ Bltu(rhs_reg, lhs, label);
3011 break;
3012 }
3013 }
3014}
3015
3016void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3017 LocationSummary* locations,
3018 MipsLabel* label) {
3019 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3020 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3021 Location rhs_location = locations->InAt(1);
3022 Register rhs_high = ZERO;
3023 Register rhs_low = ZERO;
3024 int64_t imm = 0;
3025 uint32_t imm_high = 0;
3026 uint32_t imm_low = 0;
3027 bool use_imm = rhs_location.IsConstant();
3028 if (use_imm) {
3029 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3030 imm_high = High32Bits(imm);
3031 imm_low = Low32Bits(imm);
3032 } else {
3033 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3034 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3035 }
3036
3037 if (use_imm && imm == 0) {
3038 switch (cond) {
3039 case kCondEQ:
3040 case kCondBE: // <= 0 if zero
3041 __ Or(TMP, lhs_high, lhs_low);
3042 __ Beqz(TMP, label);
3043 break;
3044 case kCondNE:
3045 case kCondA: // > 0 if non-zero
3046 __ Or(TMP, lhs_high, lhs_low);
3047 __ Bnez(TMP, label);
3048 break;
3049 case kCondLT:
3050 __ Bltz(lhs_high, label);
3051 break;
3052 case kCondGE:
3053 __ Bgez(lhs_high, label);
3054 break;
3055 case kCondLE:
3056 __ Or(TMP, lhs_high, lhs_low);
3057 __ Sra(AT, lhs_high, 31);
3058 __ Bgeu(AT, TMP, label);
3059 break;
3060 case kCondGT:
3061 __ Or(TMP, lhs_high, lhs_low);
3062 __ Sra(AT, lhs_high, 31);
3063 __ Bltu(AT, TMP, label);
3064 break;
3065 case kCondB: // always false
3066 break;
3067 case kCondAE: // always true
3068 __ B(label);
3069 break;
3070 }
3071 } else if (use_imm) {
3072 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3073 switch (cond) {
3074 case kCondEQ:
3075 __ LoadConst32(TMP, imm_high);
3076 __ Xor(TMP, TMP, lhs_high);
3077 __ LoadConst32(AT, imm_low);
3078 __ Xor(AT, AT, lhs_low);
3079 __ Or(TMP, TMP, AT);
3080 __ Beqz(TMP, label);
3081 break;
3082 case kCondNE:
3083 __ LoadConst32(TMP, imm_high);
3084 __ Xor(TMP, TMP, lhs_high);
3085 __ LoadConst32(AT, imm_low);
3086 __ Xor(AT, AT, lhs_low);
3087 __ Or(TMP, TMP, AT);
3088 __ Bnez(TMP, label);
3089 break;
3090 case kCondLT:
3091 __ LoadConst32(TMP, imm_high);
3092 __ Blt(lhs_high, TMP, label);
3093 __ Slt(TMP, TMP, lhs_high);
3094 __ LoadConst32(AT, imm_low);
3095 __ Sltu(AT, lhs_low, AT);
3096 __ Blt(TMP, AT, label);
3097 break;
3098 case kCondGE:
3099 __ LoadConst32(TMP, imm_high);
3100 __ Blt(TMP, lhs_high, label);
3101 __ Slt(TMP, lhs_high, TMP);
3102 __ LoadConst32(AT, imm_low);
3103 __ Sltu(AT, lhs_low, AT);
3104 __ Or(TMP, TMP, AT);
3105 __ Beqz(TMP, label);
3106 break;
3107 case kCondLE:
3108 __ LoadConst32(TMP, imm_high);
3109 __ Blt(lhs_high, TMP, label);
3110 __ Slt(TMP, TMP, lhs_high);
3111 __ LoadConst32(AT, imm_low);
3112 __ Sltu(AT, AT, lhs_low);
3113 __ Or(TMP, TMP, AT);
3114 __ Beqz(TMP, label);
3115 break;
3116 case kCondGT:
3117 __ LoadConst32(TMP, imm_high);
3118 __ Blt(TMP, lhs_high, label);
3119 __ Slt(TMP, lhs_high, TMP);
3120 __ LoadConst32(AT, imm_low);
3121 __ Sltu(AT, AT, lhs_low);
3122 __ Blt(TMP, AT, label);
3123 break;
3124 case kCondB:
3125 __ LoadConst32(TMP, imm_high);
3126 __ Bltu(lhs_high, TMP, label);
3127 __ Sltu(TMP, TMP, lhs_high);
3128 __ LoadConst32(AT, imm_low);
3129 __ Sltu(AT, lhs_low, AT);
3130 __ Blt(TMP, AT, label);
3131 break;
3132 case kCondAE:
3133 __ LoadConst32(TMP, imm_high);
3134 __ Bltu(TMP, lhs_high, label);
3135 __ Sltu(TMP, lhs_high, TMP);
3136 __ LoadConst32(AT, imm_low);
3137 __ Sltu(AT, lhs_low, AT);
3138 __ Or(TMP, TMP, AT);
3139 __ Beqz(TMP, label);
3140 break;
3141 case kCondBE:
3142 __ LoadConst32(TMP, imm_high);
3143 __ Bltu(lhs_high, TMP, label);
3144 __ Sltu(TMP, TMP, lhs_high);
3145 __ LoadConst32(AT, imm_low);
3146 __ Sltu(AT, AT, lhs_low);
3147 __ Or(TMP, TMP, AT);
3148 __ Beqz(TMP, label);
3149 break;
3150 case kCondA:
3151 __ LoadConst32(TMP, imm_high);
3152 __ Bltu(TMP, lhs_high, label);
3153 __ Sltu(TMP, lhs_high, TMP);
3154 __ LoadConst32(AT, imm_low);
3155 __ Sltu(AT, AT, lhs_low);
3156 __ Blt(TMP, AT, label);
3157 break;
3158 }
3159 } else {
3160 switch (cond) {
3161 case kCondEQ:
3162 __ Xor(TMP, lhs_high, rhs_high);
3163 __ Xor(AT, lhs_low, rhs_low);
3164 __ Or(TMP, TMP, AT);
3165 __ Beqz(TMP, label);
3166 break;
3167 case kCondNE:
3168 __ Xor(TMP, lhs_high, rhs_high);
3169 __ Xor(AT, lhs_low, rhs_low);
3170 __ Or(TMP, TMP, AT);
3171 __ Bnez(TMP, label);
3172 break;
3173 case kCondLT:
3174 __ Blt(lhs_high, rhs_high, label);
3175 __ Slt(TMP, rhs_high, lhs_high);
3176 __ Sltu(AT, lhs_low, rhs_low);
3177 __ Blt(TMP, AT, label);
3178 break;
3179 case kCondGE:
3180 __ Blt(rhs_high, lhs_high, label);
3181 __ Slt(TMP, lhs_high, rhs_high);
3182 __ Sltu(AT, lhs_low, rhs_low);
3183 __ Or(TMP, TMP, AT);
3184 __ Beqz(TMP, label);
3185 break;
3186 case kCondLE:
3187 __ Blt(lhs_high, rhs_high, label);
3188 __ Slt(TMP, rhs_high, lhs_high);
3189 __ Sltu(AT, rhs_low, lhs_low);
3190 __ Or(TMP, TMP, AT);
3191 __ Beqz(TMP, label);
3192 break;
3193 case kCondGT:
3194 __ Blt(rhs_high, lhs_high, label);
3195 __ Slt(TMP, lhs_high, rhs_high);
3196 __ Sltu(AT, rhs_low, lhs_low);
3197 __ Blt(TMP, AT, label);
3198 break;
3199 case kCondB:
3200 __ Bltu(lhs_high, rhs_high, label);
3201 __ Sltu(TMP, rhs_high, lhs_high);
3202 __ Sltu(AT, lhs_low, rhs_low);
3203 __ Blt(TMP, AT, label);
3204 break;
3205 case kCondAE:
3206 __ Bltu(rhs_high, lhs_high, label);
3207 __ Sltu(TMP, lhs_high, rhs_high);
3208 __ Sltu(AT, lhs_low, rhs_low);
3209 __ Or(TMP, TMP, AT);
3210 __ Beqz(TMP, label);
3211 break;
3212 case kCondBE:
3213 __ Bltu(lhs_high, rhs_high, label);
3214 __ Sltu(TMP, rhs_high, lhs_high);
3215 __ Sltu(AT, rhs_low, lhs_low);
3216 __ Or(TMP, TMP, AT);
3217 __ Beqz(TMP, label);
3218 break;
3219 case kCondA:
3220 __ Bltu(rhs_high, lhs_high, label);
3221 __ Sltu(TMP, lhs_high, rhs_high);
3222 __ Sltu(AT, rhs_low, lhs_low);
3223 __ Blt(TMP, AT, label);
3224 break;
3225 }
3226 }
3227}
3228
3229void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3230 bool gt_bias,
3231 Primitive::Type type,
3232 LocationSummary* locations,
3233 MipsLabel* label) {
3234 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3235 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3236 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3237 if (type == Primitive::kPrimFloat) {
3238 if (isR6) {
3239 switch (cond) {
3240 case kCondEQ:
3241 __ CmpEqS(FTMP, lhs, rhs);
3242 __ Bc1nez(FTMP, label);
3243 break;
3244 case kCondNE:
3245 __ CmpEqS(FTMP, lhs, rhs);
3246 __ Bc1eqz(FTMP, label);
3247 break;
3248 case kCondLT:
3249 if (gt_bias) {
3250 __ CmpLtS(FTMP, lhs, rhs);
3251 } else {
3252 __ CmpUltS(FTMP, lhs, rhs);
3253 }
3254 __ Bc1nez(FTMP, label);
3255 break;
3256 case kCondLE:
3257 if (gt_bias) {
3258 __ CmpLeS(FTMP, lhs, rhs);
3259 } else {
3260 __ CmpUleS(FTMP, lhs, rhs);
3261 }
3262 __ Bc1nez(FTMP, label);
3263 break;
3264 case kCondGT:
3265 if (gt_bias) {
3266 __ CmpUltS(FTMP, rhs, lhs);
3267 } else {
3268 __ CmpLtS(FTMP, rhs, lhs);
3269 }
3270 __ Bc1nez(FTMP, label);
3271 break;
3272 case kCondGE:
3273 if (gt_bias) {
3274 __ CmpUleS(FTMP, rhs, lhs);
3275 } else {
3276 __ CmpLeS(FTMP, rhs, lhs);
3277 }
3278 __ Bc1nez(FTMP, label);
3279 break;
3280 default:
3281 LOG(FATAL) << "Unexpected non-floating-point condition";
3282 }
3283 } else {
3284 switch (cond) {
3285 case kCondEQ:
3286 __ CeqS(0, lhs, rhs);
3287 __ Bc1t(0, label);
3288 break;
3289 case kCondNE:
3290 __ CeqS(0, lhs, rhs);
3291 __ Bc1f(0, label);
3292 break;
3293 case kCondLT:
3294 if (gt_bias) {
3295 __ ColtS(0, lhs, rhs);
3296 } else {
3297 __ CultS(0, lhs, rhs);
3298 }
3299 __ Bc1t(0, label);
3300 break;
3301 case kCondLE:
3302 if (gt_bias) {
3303 __ ColeS(0, lhs, rhs);
3304 } else {
3305 __ CuleS(0, lhs, rhs);
3306 }
3307 __ Bc1t(0, label);
3308 break;
3309 case kCondGT:
3310 if (gt_bias) {
3311 __ CultS(0, rhs, lhs);
3312 } else {
3313 __ ColtS(0, rhs, lhs);
3314 }
3315 __ Bc1t(0, label);
3316 break;
3317 case kCondGE:
3318 if (gt_bias) {
3319 __ CuleS(0, rhs, lhs);
3320 } else {
3321 __ ColeS(0, rhs, lhs);
3322 }
3323 __ Bc1t(0, label);
3324 break;
3325 default:
3326 LOG(FATAL) << "Unexpected non-floating-point condition";
3327 }
3328 }
3329 } else {
3330 DCHECK_EQ(type, Primitive::kPrimDouble);
3331 if (isR6) {
3332 switch (cond) {
3333 case kCondEQ:
3334 __ CmpEqD(FTMP, lhs, rhs);
3335 __ Bc1nez(FTMP, label);
3336 break;
3337 case kCondNE:
3338 __ CmpEqD(FTMP, lhs, rhs);
3339 __ Bc1eqz(FTMP, label);
3340 break;
3341 case kCondLT:
3342 if (gt_bias) {
3343 __ CmpLtD(FTMP, lhs, rhs);
3344 } else {
3345 __ CmpUltD(FTMP, lhs, rhs);
3346 }
3347 __ Bc1nez(FTMP, label);
3348 break;
3349 case kCondLE:
3350 if (gt_bias) {
3351 __ CmpLeD(FTMP, lhs, rhs);
3352 } else {
3353 __ CmpUleD(FTMP, lhs, rhs);
3354 }
3355 __ Bc1nez(FTMP, label);
3356 break;
3357 case kCondGT:
3358 if (gt_bias) {
3359 __ CmpUltD(FTMP, rhs, lhs);
3360 } else {
3361 __ CmpLtD(FTMP, rhs, lhs);
3362 }
3363 __ Bc1nez(FTMP, label);
3364 break;
3365 case kCondGE:
3366 if (gt_bias) {
3367 __ CmpUleD(FTMP, rhs, lhs);
3368 } else {
3369 __ CmpLeD(FTMP, rhs, lhs);
3370 }
3371 __ Bc1nez(FTMP, label);
3372 break;
3373 default:
3374 LOG(FATAL) << "Unexpected non-floating-point condition";
3375 }
3376 } else {
3377 switch (cond) {
3378 case kCondEQ:
3379 __ CeqD(0, lhs, rhs);
3380 __ Bc1t(0, label);
3381 break;
3382 case kCondNE:
3383 __ CeqD(0, lhs, rhs);
3384 __ Bc1f(0, label);
3385 break;
3386 case kCondLT:
3387 if (gt_bias) {
3388 __ ColtD(0, lhs, rhs);
3389 } else {
3390 __ CultD(0, lhs, rhs);
3391 }
3392 __ Bc1t(0, label);
3393 break;
3394 case kCondLE:
3395 if (gt_bias) {
3396 __ ColeD(0, lhs, rhs);
3397 } else {
3398 __ CuleD(0, lhs, rhs);
3399 }
3400 __ Bc1t(0, label);
3401 break;
3402 case kCondGT:
3403 if (gt_bias) {
3404 __ CultD(0, rhs, lhs);
3405 } else {
3406 __ ColtD(0, rhs, lhs);
3407 }
3408 __ Bc1t(0, label);
3409 break;
3410 case kCondGE:
3411 if (gt_bias) {
3412 __ CuleD(0, rhs, lhs);
3413 } else {
3414 __ ColeD(0, rhs, lhs);
3415 }
3416 __ Bc1t(0, label);
3417 break;
3418 default:
3419 LOG(FATAL) << "Unexpected non-floating-point condition";
3420 }
3421 }
3422 }
3423}
3424
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003425void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003426 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003427 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003428 MipsLabel* false_target) {
3429 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003430
David Brazdil0debae72015-11-12 18:37:00 +00003431 if (true_target == nullptr && false_target == nullptr) {
3432 // Nothing to do. The code always falls through.
3433 return;
3434 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003435 // Constant condition, statically compared against "true" (integer value 1).
3436 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003437 if (true_target != nullptr) {
3438 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003439 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003440 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003441 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003442 if (false_target != nullptr) {
3443 __ B(false_target);
3444 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003445 }
David Brazdil0debae72015-11-12 18:37:00 +00003446 return;
3447 }
3448
3449 // The following code generates these patterns:
3450 // (1) true_target == nullptr && false_target != nullptr
3451 // - opposite condition true => branch to false_target
3452 // (2) true_target != nullptr && false_target == nullptr
3453 // - condition true => branch to true_target
3454 // (3) true_target != nullptr && false_target != nullptr
3455 // - condition true => branch to true_target
3456 // - branch to false_target
3457 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003458 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003459 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003460 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003461 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003462 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3463 } else {
3464 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3465 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003466 } else {
3467 // The condition instruction has not been materialized, use its inputs as
3468 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003469 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003470 Primitive::Type type = condition->InputAt(0)->GetType();
3471 LocationSummary* locations = cond->GetLocations();
3472 IfCondition if_cond = condition->GetCondition();
3473 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003474
David Brazdil0debae72015-11-12 18:37:00 +00003475 if (true_target == nullptr) {
3476 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003477 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003478 }
3479
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003480 switch (type) {
3481 default:
3482 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3483 break;
3484 case Primitive::kPrimLong:
3485 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3486 break;
3487 case Primitive::kPrimFloat:
3488 case Primitive::kPrimDouble:
3489 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3490 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003491 }
3492 }
David Brazdil0debae72015-11-12 18:37:00 +00003493
3494 // If neither branch falls through (case 3), the conditional branch to `true_target`
3495 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3496 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003497 __ B(false_target);
3498 }
3499}
3500
3501void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3502 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003503 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003504 locations->SetInAt(0, Location::RequiresRegister());
3505 }
3506}
3507
3508void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003509 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3510 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3511 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3512 nullptr : codegen_->GetLabelOf(true_successor);
3513 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3514 nullptr : codegen_->GetLabelOf(false_successor);
3515 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003516}
3517
3518void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3519 LocationSummary* locations = new (GetGraph()->GetArena())
3520 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003521 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003522 locations->SetInAt(0, Location::RequiresRegister());
3523 }
3524}
3525
3526void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003527 SlowPathCodeMIPS* slow_path =
3528 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003529 GenerateTestAndBranch(deoptimize,
3530 /* condition_input_index */ 0,
3531 slow_path->GetEntryLabel(),
3532 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003533}
3534
David Brazdil74eb1b22015-12-14 11:44:01 +00003535void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3536 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3537 if (Primitive::IsFloatingPointType(select->GetType())) {
3538 locations->SetInAt(0, Location::RequiresFpuRegister());
3539 locations->SetInAt(1, Location::RequiresFpuRegister());
3540 } else {
3541 locations->SetInAt(0, Location::RequiresRegister());
3542 locations->SetInAt(1, Location::RequiresRegister());
3543 }
3544 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3545 locations->SetInAt(2, Location::RequiresRegister());
3546 }
3547 locations->SetOut(Location::SameAsFirstInput());
3548}
3549
3550void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3551 LocationSummary* locations = select->GetLocations();
3552 MipsLabel false_target;
3553 GenerateTestAndBranch(select,
3554 /* condition_input_index */ 2,
3555 /* true_target */ nullptr,
3556 &false_target);
3557 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3558 __ Bind(&false_target);
3559}
3560
David Srbecky0cf44932015-12-09 14:09:59 +00003561void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3562 new (GetGraph()->GetArena()) LocationSummary(info);
3563}
3564
David Srbeckyd28f4a02016-03-14 17:14:24 +00003565void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3566 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003567}
3568
3569void CodeGeneratorMIPS::GenerateNop() {
3570 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003571}
3572
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003573void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3574 Primitive::Type field_type = field_info.GetFieldType();
3575 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3576 bool generate_volatile = field_info.IsVolatile() && is_wide;
3577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003578 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003579
3580 locations->SetInAt(0, Location::RequiresRegister());
3581 if (generate_volatile) {
3582 InvokeRuntimeCallingConvention calling_convention;
3583 // need A0 to hold base + offset
3584 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3585 if (field_type == Primitive::kPrimLong) {
3586 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3587 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003588 // Use Location::Any() to prevent situations when running out of available fp registers.
3589 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003590 // Need some temp core regs since FP results are returned in core registers
3591 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3592 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3593 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3594 }
3595 } else {
3596 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3597 locations->SetOut(Location::RequiresFpuRegister());
3598 } else {
3599 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3600 }
3601 }
3602}
3603
3604void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3605 const FieldInfo& field_info,
3606 uint32_t dex_pc) {
3607 Primitive::Type type = field_info.GetFieldType();
3608 LocationSummary* locations = instruction->GetLocations();
3609 Register obj = locations->InAt(0).AsRegister<Register>();
3610 LoadOperandType load_type = kLoadUnsignedByte;
3611 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003612 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003613 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003614
3615 switch (type) {
3616 case Primitive::kPrimBoolean:
3617 load_type = kLoadUnsignedByte;
3618 break;
3619 case Primitive::kPrimByte:
3620 load_type = kLoadSignedByte;
3621 break;
3622 case Primitive::kPrimShort:
3623 load_type = kLoadSignedHalfword;
3624 break;
3625 case Primitive::kPrimChar:
3626 load_type = kLoadUnsignedHalfword;
3627 break;
3628 case Primitive::kPrimInt:
3629 case Primitive::kPrimFloat:
3630 case Primitive::kPrimNot:
3631 load_type = kLoadWord;
3632 break;
3633 case Primitive::kPrimLong:
3634 case Primitive::kPrimDouble:
3635 load_type = kLoadDoubleword;
3636 break;
3637 case Primitive::kPrimVoid:
3638 LOG(FATAL) << "Unreachable type " << type;
3639 UNREACHABLE();
3640 }
3641
3642 if (is_volatile && load_type == kLoadDoubleword) {
3643 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003644 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003645 // Do implicit Null check
3646 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3647 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3648 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3649 instruction,
3650 dex_pc,
3651 nullptr,
3652 IsDirectEntrypoint(kQuickA64Load));
3653 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3654 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003655 // FP results are returned in core registers. Need to move them.
3656 Location out = locations->Out();
3657 if (out.IsFpuRegister()) {
3658 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3659 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3660 out.AsFpuRegister<FRegister>());
3661 } else {
3662 DCHECK(out.IsDoubleStackSlot());
3663 __ StoreToOffset(kStoreWord,
3664 locations->GetTemp(1).AsRegister<Register>(),
3665 SP,
3666 out.GetStackIndex());
3667 __ StoreToOffset(kStoreWord,
3668 locations->GetTemp(2).AsRegister<Register>(),
3669 SP,
3670 out.GetStackIndex() + 4);
3671 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003672 }
3673 } else {
3674 if (!Primitive::IsFloatingPointType(type)) {
3675 Register dst;
3676 if (type == Primitive::kPrimLong) {
3677 DCHECK(locations->Out().IsRegisterPair());
3678 dst = locations->Out().AsRegisterPairLow<Register>();
3679 } else {
3680 DCHECK(locations->Out().IsRegister());
3681 dst = locations->Out().AsRegister<Register>();
3682 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003683 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003684 } else {
3685 DCHECK(locations->Out().IsFpuRegister());
3686 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3687 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003688 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003689 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003690 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003691 }
3692 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003693 }
3694
3695 if (is_volatile) {
3696 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3697 }
3698}
3699
3700void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3701 Primitive::Type field_type = field_info.GetFieldType();
3702 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3703 bool generate_volatile = field_info.IsVolatile() && is_wide;
3704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003705 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003706
3707 locations->SetInAt(0, Location::RequiresRegister());
3708 if (generate_volatile) {
3709 InvokeRuntimeCallingConvention calling_convention;
3710 // need A0 to hold base + offset
3711 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3712 if (field_type == Primitive::kPrimLong) {
3713 locations->SetInAt(1, Location::RegisterPairLocation(
3714 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3715 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003716 // Use Location::Any() to prevent situations when running out of available fp registers.
3717 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003718 // Pass FP parameters in core registers.
3719 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3720 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3721 }
3722 } else {
3723 if (Primitive::IsFloatingPointType(field_type)) {
3724 locations->SetInAt(1, Location::RequiresFpuRegister());
3725 } else {
3726 locations->SetInAt(1, Location::RequiresRegister());
3727 }
3728 }
3729}
3730
3731void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3732 const FieldInfo& field_info,
3733 uint32_t dex_pc) {
3734 Primitive::Type type = field_info.GetFieldType();
3735 LocationSummary* locations = instruction->GetLocations();
3736 Register obj = locations->InAt(0).AsRegister<Register>();
3737 StoreOperandType store_type = kStoreByte;
3738 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003739 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003740 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003741
3742 switch (type) {
3743 case Primitive::kPrimBoolean:
3744 case Primitive::kPrimByte:
3745 store_type = kStoreByte;
3746 break;
3747 case Primitive::kPrimShort:
3748 case Primitive::kPrimChar:
3749 store_type = kStoreHalfword;
3750 break;
3751 case Primitive::kPrimInt:
3752 case Primitive::kPrimFloat:
3753 case Primitive::kPrimNot:
3754 store_type = kStoreWord;
3755 break;
3756 case Primitive::kPrimLong:
3757 case Primitive::kPrimDouble:
3758 store_type = kStoreDoubleword;
3759 break;
3760 case Primitive::kPrimVoid:
3761 LOG(FATAL) << "Unreachable type " << type;
3762 UNREACHABLE();
3763 }
3764
3765 if (is_volatile) {
3766 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3767 }
3768
3769 if (is_volatile && store_type == kStoreDoubleword) {
3770 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003771 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003772 // Do implicit Null check.
3773 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3774 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3775 if (type == Primitive::kPrimDouble) {
3776 // Pass FP parameters in core registers.
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003777 Location in = locations->InAt(1);
3778 if (in.IsFpuRegister()) {
3779 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(), in.AsFpuRegister<FRegister>());
3780 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3781 in.AsFpuRegister<FRegister>());
3782 } else if (in.IsDoubleStackSlot()) {
3783 __ LoadFromOffset(kLoadWord,
3784 locations->GetTemp(1).AsRegister<Register>(),
3785 SP,
3786 in.GetStackIndex());
3787 __ LoadFromOffset(kLoadWord,
3788 locations->GetTemp(2).AsRegister<Register>(),
3789 SP,
3790 in.GetStackIndex() + 4);
3791 } else {
3792 DCHECK(in.IsConstant());
3793 DCHECK(in.GetConstant()->IsDoubleConstant());
3794 int64_t value = bit_cast<int64_t, double>(in.GetConstant()->AsDoubleConstant()->GetValue());
3795 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
3796 locations->GetTemp(1).AsRegister<Register>(),
3797 value);
3798 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003799 }
3800 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3801 instruction,
3802 dex_pc,
3803 nullptr,
3804 IsDirectEntrypoint(kQuickA64Store));
3805 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3806 } else {
3807 if (!Primitive::IsFloatingPointType(type)) {
3808 Register src;
3809 if (type == Primitive::kPrimLong) {
3810 DCHECK(locations->InAt(1).IsRegisterPair());
3811 src = locations->InAt(1).AsRegisterPairLow<Register>();
3812 } else {
3813 DCHECK(locations->InAt(1).IsRegister());
3814 src = locations->InAt(1).AsRegister<Register>();
3815 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003816 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003817 } else {
3818 DCHECK(locations->InAt(1).IsFpuRegister());
3819 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3820 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003821 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003822 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003823 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003824 }
3825 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003826 }
3827
3828 // TODO: memory barriers?
3829 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3830 DCHECK(locations->InAt(1).IsRegister());
3831 Register src = locations->InAt(1).AsRegister<Register>();
3832 codegen_->MarkGCCard(obj, src);
3833 }
3834
3835 if (is_volatile) {
3836 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3837 }
3838}
3839
3840void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3841 HandleFieldGet(instruction, instruction->GetFieldInfo());
3842}
3843
3844void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3845 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3846}
3847
3848void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3849 HandleFieldSet(instruction, instruction->GetFieldInfo());
3850}
3851
3852void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3853 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3854}
3855
Alexey Frunze06a46c42016-07-19 15:00:40 -07003856void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
3857 HInstruction* instruction ATTRIBUTE_UNUSED,
3858 Location root,
3859 Register obj,
3860 uint32_t offset) {
3861 Register root_reg = root.AsRegister<Register>();
3862 if (kEmitCompilerReadBarrier) {
3863 UNIMPLEMENTED(FATAL) << "for read barrier";
3864 } else {
3865 // Plain GC root load with no read barrier.
3866 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3867 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
3868 // Note that GC roots are not affected by heap poisoning, thus we
3869 // do not have to unpoison `root_reg` here.
3870 }
3871}
3872
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003873void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3874 LocationSummary::CallKind call_kind =
3875 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3877 locations->SetInAt(0, Location::RequiresRegister());
3878 locations->SetInAt(1, Location::RequiresRegister());
3879 // The output does overlap inputs.
3880 // Note that TypeCheckSlowPathMIPS uses this register too.
3881 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3882}
3883
3884void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3885 LocationSummary* locations = instruction->GetLocations();
3886 Register obj = locations->InAt(0).AsRegister<Register>();
3887 Register cls = locations->InAt(1).AsRegister<Register>();
3888 Register out = locations->Out().AsRegister<Register>();
3889
3890 MipsLabel done;
3891
3892 // Return 0 if `obj` is null.
3893 // TODO: Avoid this check if we know `obj` is not null.
3894 __ Move(out, ZERO);
3895 __ Beqz(obj, &done);
3896
3897 // Compare the class of `obj` with `cls`.
3898 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3899 if (instruction->IsExactCheck()) {
3900 // Classes must be equal for the instanceof to succeed.
3901 __ Xor(out, out, cls);
3902 __ Sltiu(out, out, 1);
3903 } else {
3904 // If the classes are not equal, we go into a slow path.
3905 DCHECK(locations->OnlyCallsOnSlowPath());
3906 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3907 codegen_->AddSlowPath(slow_path);
3908 __ Bne(out, cls, slow_path->GetEntryLabel());
3909 __ LoadConst32(out, 1);
3910 __ Bind(slow_path->GetExitLabel());
3911 }
3912
3913 __ Bind(&done);
3914}
3915
3916void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3918 locations->SetOut(Location::ConstantLocation(constant));
3919}
3920
3921void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3922 // Will be generated at use site.
3923}
3924
3925void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3926 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3927 locations->SetOut(Location::ConstantLocation(constant));
3928}
3929
3930void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3931 // Will be generated at use site.
3932}
3933
3934void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3935 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3936 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3937}
3938
3939void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3940 HandleInvoke(invoke);
3941 // The register T0 is required to be used for the hidden argument in
3942 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3943 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3944}
3945
3946void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3947 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3948 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003949 Location receiver = invoke->GetLocations()->InAt(0);
3950 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003951 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003952
3953 // Set the hidden argument.
3954 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3955 invoke->GetDexMethodIndex());
3956
3957 // temp = object->GetClass();
3958 if (receiver.IsStackSlot()) {
3959 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3960 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3961 } else {
3962 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3963 }
3964 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003965 __ LoadFromOffset(kLoadWord, temp, temp,
3966 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3967 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003968 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003969 // temp = temp->GetImtEntryAt(method_offset);
3970 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3971 // T9 = temp->GetEntryPoint();
3972 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3973 // T9();
3974 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003975 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003976 DCHECK(!codegen_->IsLeafMethod());
3977 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3978}
3979
3980void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003981 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3982 if (intrinsic.TryDispatch(invoke)) {
3983 return;
3984 }
3985
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003986 HandleInvoke(invoke);
3987}
3988
3989void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003990 // Explicit clinit checks triggered by static invokes must have been pruned by
3991 // art::PrepareForRegisterAllocation.
3992 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003993
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003994 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3995 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3996 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3997
3998 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
3999 // R6 has PC-relative addressing.
4000 bool has_extra_input = !isR6 &&
4001 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4002 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4003
4004 if (invoke->HasPcRelativeDexCache()) {
4005 // kDexCachePcRelative is mutually exclusive with
4006 // kDirectAddressWithFixup/kCallDirectWithFixup.
4007 CHECK(!has_extra_input);
4008 has_extra_input = true;
4009 }
4010
Chris Larsen701566a2015-10-27 15:29:13 -07004011 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4012 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004013 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4014 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4015 }
Chris Larsen701566a2015-10-27 15:29:13 -07004016 return;
4017 }
4018
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004019 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004020
4021 // Add the extra input register if either the dex cache array base register
4022 // or the PC-relative base register for accessing literals is needed.
4023 if (has_extra_input) {
4024 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4025 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004026}
4027
Chris Larsen701566a2015-10-27 15:29:13 -07004028static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004029 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004030 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4031 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004032 return true;
4033 }
4034 return false;
4035}
4036
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004037HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004038 HLoadString::LoadKind desired_string_load_kind) {
4039 if (kEmitCompilerReadBarrier) {
4040 UNIMPLEMENTED(FATAL) << "for read barrier";
4041 }
4042 // We disable PC-relative load when there is an irreducible loop, as the optimization
4043 // is incompatible with it.
4044 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4045 bool fallback_load = has_irreducible_loops;
4046 switch (desired_string_load_kind) {
4047 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4048 DCHECK(!GetCompilerOptions().GetCompilePic());
4049 break;
4050 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4051 DCHECK(GetCompilerOptions().GetCompilePic());
4052 break;
4053 case HLoadString::LoadKind::kBootImageAddress:
4054 break;
4055 case HLoadString::LoadKind::kDexCacheAddress:
4056 DCHECK(Runtime::Current()->UseJitCompilation());
4057 fallback_load = false;
4058 break;
4059 case HLoadString::LoadKind::kDexCachePcRelative:
4060 DCHECK(!Runtime::Current()->UseJitCompilation());
4061 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4062 // with irreducible loops.
4063 break;
4064 case HLoadString::LoadKind::kDexCacheViaMethod:
4065 fallback_load = false;
4066 break;
4067 }
4068 if (fallback_load) {
4069 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4070 }
4071 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004072}
4073
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004074HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4075 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004076 if (kEmitCompilerReadBarrier) {
4077 UNIMPLEMENTED(FATAL) << "for read barrier";
4078 }
4079 // We disable pc-relative load when there is an irreducible loop, as the optimization
4080 // is incompatible with it.
4081 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4082 bool fallback_load = has_irreducible_loops;
4083 switch (desired_class_load_kind) {
4084 case HLoadClass::LoadKind::kReferrersClass:
4085 fallback_load = false;
4086 break;
4087 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4088 DCHECK(!GetCompilerOptions().GetCompilePic());
4089 break;
4090 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4091 DCHECK(GetCompilerOptions().GetCompilePic());
4092 break;
4093 case HLoadClass::LoadKind::kBootImageAddress:
4094 break;
4095 case HLoadClass::LoadKind::kDexCacheAddress:
4096 DCHECK(Runtime::Current()->UseJitCompilation());
4097 fallback_load = false;
4098 break;
4099 case HLoadClass::LoadKind::kDexCachePcRelative:
4100 DCHECK(!Runtime::Current()->UseJitCompilation());
4101 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4102 // with irreducible loops.
4103 break;
4104 case HLoadClass::LoadKind::kDexCacheViaMethod:
4105 fallback_load = false;
4106 break;
4107 }
4108 if (fallback_load) {
4109 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4110 }
4111 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004112}
4113
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004114Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4115 Register temp) {
4116 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4117 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4118 if (!invoke->GetLocations()->Intrinsified()) {
4119 return location.AsRegister<Register>();
4120 }
4121 // For intrinsics we allow any location, so it may be on the stack.
4122 if (!location.IsRegister()) {
4123 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4124 return temp;
4125 }
4126 // For register locations, check if the register was saved. If so, get it from the stack.
4127 // Note: There is a chance that the register was saved but not overwritten, so we could
4128 // save one load. However, since this is just an intrinsic slow path we prefer this
4129 // simple and more robust approach rather that trying to determine if that's the case.
4130 SlowPathCode* slow_path = GetCurrentSlowPath();
4131 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4132 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4133 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4134 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4135 return temp;
4136 }
4137 return location.AsRegister<Register>();
4138}
4139
Vladimir Markodc151b22015-10-15 18:02:30 +01004140HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4141 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4142 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004143 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4144 // We disable PC-relative load when there is an irreducible loop, as the optimization
4145 // is incompatible with it.
4146 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4147 bool fallback_load = true;
4148 bool fallback_call = true;
4149 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004150 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4151 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004152 fallback_load = has_irreducible_loops;
4153 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004154 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004155 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004156 break;
4157 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004158 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004159 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004160 fallback_call = has_irreducible_loops;
4161 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004162 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004163 // TODO: Implement this type.
4164 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004165 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004166 fallback_call = false;
4167 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004168 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004169 if (fallback_load) {
4170 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4171 dispatch_info.method_load_data = 0;
4172 }
4173 if (fallback_call) {
4174 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4175 dispatch_info.direct_code_ptr = 0;
4176 }
4177 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004178}
4179
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004180void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4181 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004182 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004183 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4184 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4185 bool isR6 = isa_features_.IsR6();
4186 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4187 // R6 has PC-relative addressing.
4188 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4189 (!isR6 &&
4190 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4191 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4192 Register base_reg = has_extra_input
4193 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4194 : ZERO;
4195
4196 // For better instruction scheduling we load the direct code pointer before the method pointer.
4197 switch (code_ptr_location) {
4198 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4199 // T9 = invoke->GetDirectCodePtr();
4200 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4201 break;
4202 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4203 // T9 = code address from literal pool with link-time patch.
4204 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4205 break;
4206 default:
4207 break;
4208 }
4209
4210 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004211 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4212 // temp = thread->string_init_entrypoint
4213 __ LoadFromOffset(kLoadWord,
4214 temp.AsRegister<Register>(),
4215 TR,
4216 invoke->GetStringInitOffset());
4217 break;
4218 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004219 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004220 break;
4221 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4222 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4223 break;
4224 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004225 __ LoadLiteral(temp.AsRegister<Register>(),
4226 base_reg,
4227 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4228 break;
4229 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4230 HMipsDexCacheArraysBase* base =
4231 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4232 int32_t offset =
4233 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4234 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4235 break;
4236 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004237 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004238 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004239 Register reg = temp.AsRegister<Register>();
4240 Register method_reg;
4241 if (current_method.IsRegister()) {
4242 method_reg = current_method.AsRegister<Register>();
4243 } else {
4244 // TODO: use the appropriate DCHECK() here if possible.
4245 // DCHECK(invoke->GetLocations()->Intrinsified());
4246 DCHECK(!current_method.IsValid());
4247 method_reg = reg;
4248 __ Lw(reg, SP, kCurrentMethodStackOffset);
4249 }
4250
4251 // temp = temp->dex_cache_resolved_methods_;
4252 __ LoadFromOffset(kLoadWord,
4253 reg,
4254 method_reg,
4255 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004256 // temp = temp[index_in_cache];
4257 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4258 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004259 __ LoadFromOffset(kLoadWord,
4260 reg,
4261 reg,
4262 CodeGenerator::GetCachePointerOffset(index_in_cache));
4263 break;
4264 }
4265 }
4266
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004267 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004268 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004269 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004270 break;
4271 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004272 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4273 // T9 prepared above for better instruction scheduling.
4274 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004275 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004276 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004277 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004278 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004279 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004280 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4281 LOG(FATAL) << "Unsupported";
4282 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004283 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4284 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004285 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004286 T9,
4287 callee_method.AsRegister<Register>(),
4288 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004289 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004290 // T9()
4291 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004292 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004293 break;
4294 }
4295 DCHECK(!IsLeafMethod());
4296}
4297
4298void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004299 // Explicit clinit checks triggered by static invokes must have been pruned by
4300 // art::PrepareForRegisterAllocation.
4301 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004302
4303 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4304 return;
4305 }
4306
4307 LocationSummary* locations = invoke->GetLocations();
4308 codegen_->GenerateStaticOrDirectCall(invoke,
4309 locations->HasTemps()
4310 ? locations->GetTemp(0)
4311 : Location::NoLocation());
4312 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4313}
4314
Chris Larsen3acee732015-11-18 13:31:08 -08004315void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004316 LocationSummary* locations = invoke->GetLocations();
4317 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004318 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004319 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4320 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4321 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004322 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004323
4324 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004325 DCHECK(receiver.IsRegister());
4326 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4327 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004328 // temp = temp->GetMethodAt(method_offset);
4329 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4330 // T9 = temp->GetEntryPoint();
4331 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4332 // T9();
4333 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004334 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004335}
4336
4337void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4338 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4339 return;
4340 }
4341
4342 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004343 DCHECK(!codegen_->IsLeafMethod());
4344 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4345}
4346
4347void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004348 if (cls->NeedsAccessCheck()) {
4349 InvokeRuntimeCallingConvention calling_convention;
4350 CodeGenerator::CreateLoadClassLocationSummary(
4351 cls,
4352 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4353 Location::RegisterLocation(V0),
4354 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4355 return;
4356 }
4357
4358 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4359 ? LocationSummary::kCallOnSlowPath
4360 : LocationSummary::kNoCall;
4361 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4362 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4363 switch (load_kind) {
4364 // We need an extra register for PC-relative literals on R2.
4365 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4366 case HLoadClass::LoadKind::kBootImageAddress:
4367 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4368 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4369 break;
4370 }
4371 FALLTHROUGH_INTENDED;
4372 // We need an extra register for PC-relative dex cache accesses.
4373 case HLoadClass::LoadKind::kDexCachePcRelative:
4374 case HLoadClass::LoadKind::kReferrersClass:
4375 case HLoadClass::LoadKind::kDexCacheViaMethod:
4376 locations->SetInAt(0, Location::RequiresRegister());
4377 break;
4378 default:
4379 break;
4380 }
4381 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004382}
4383
4384void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4385 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004386 if (cls->NeedsAccessCheck()) {
4387 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4388 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4389 cls,
4390 cls->GetDexPc(),
4391 nullptr,
4392 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004393 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004394 return;
4395 }
4396
Alexey Frunze06a46c42016-07-19 15:00:40 -07004397 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4398 Location out_loc = locations->Out();
4399 Register out = out_loc.AsRegister<Register>();
4400 Register base_or_current_method_reg;
4401 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4402 switch (load_kind) {
4403 // We need an extra register for PC-relative literals on R2.
4404 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4405 case HLoadClass::LoadKind::kBootImageAddress:
4406 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4407 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4408 break;
4409 // We need an extra register for PC-relative dex cache accesses.
4410 case HLoadClass::LoadKind::kDexCachePcRelative:
4411 case HLoadClass::LoadKind::kReferrersClass:
4412 case HLoadClass::LoadKind::kDexCacheViaMethod:
4413 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4414 break;
4415 default:
4416 base_or_current_method_reg = ZERO;
4417 break;
4418 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004419
Alexey Frunze06a46c42016-07-19 15:00:40 -07004420 bool generate_null_check = false;
4421 switch (load_kind) {
4422 case HLoadClass::LoadKind::kReferrersClass: {
4423 DCHECK(!cls->CanCallRuntime());
4424 DCHECK(!cls->MustGenerateClinitCheck());
4425 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4426 GenerateGcRootFieldLoad(cls,
4427 out_loc,
4428 base_or_current_method_reg,
4429 ArtMethod::DeclaringClassOffset().Int32Value());
4430 break;
4431 }
4432 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4433 DCHECK(!kEmitCompilerReadBarrier);
4434 __ LoadLiteral(out,
4435 base_or_current_method_reg,
4436 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4437 cls->GetTypeIndex()));
4438 break;
4439 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4440 DCHECK(!kEmitCompilerReadBarrier);
4441 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4442 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004443 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004444 if (isR6) {
4445 __ Bind(&info->high_label);
4446 __ Bind(&info->pc_rel_label);
4447 // Add a 32-bit offset to PC.
4448 __ Auipc(out, /* placeholder */ 0x1234);
4449 __ Addiu(out, out, /* placeholder */ 0x5678);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004450 } else {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004451 __ Bind(&info->high_label);
4452 __ Lui(out, /* placeholder */ 0x1234);
4453 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4454 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4455 __ Ori(out, out, /* placeholder */ 0x5678);
4456 // Add a 32-bit offset to PC.
4457 __ Addu(out, out, base_or_current_method_reg);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004458 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004459 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004460 break;
4461 }
4462 case HLoadClass::LoadKind::kBootImageAddress: {
4463 DCHECK(!kEmitCompilerReadBarrier);
4464 DCHECK_NE(cls->GetAddress(), 0u);
4465 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4466 __ LoadLiteral(out,
4467 base_or_current_method_reg,
4468 codegen_->DeduplicateBootImageAddressLiteral(address));
4469 break;
4470 }
4471 case HLoadClass::LoadKind::kDexCacheAddress: {
4472 DCHECK_NE(cls->GetAddress(), 0u);
4473 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4474 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4475 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4476 int16_t offset = Low16Bits(address);
4477 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4478 __ Lui(out, High16Bits(base_address));
4479 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4480 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4481 generate_null_check = !cls->IsInDexCache();
4482 break;
4483 }
4484 case HLoadClass::LoadKind::kDexCachePcRelative: {
4485 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4486 int32_t offset =
4487 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4488 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4489 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4490 generate_null_check = !cls->IsInDexCache();
4491 break;
4492 }
4493 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4494 // /* GcRoot<mirror::Class>[] */ out =
4495 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4496 __ LoadFromOffset(kLoadWord,
4497 out,
4498 base_or_current_method_reg,
4499 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4500 // /* GcRoot<mirror::Class> */ out = out[type_index]
4501 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4502 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4503 generate_null_check = !cls->IsInDexCache();
4504 }
4505 }
4506
4507 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4508 DCHECK(cls->CanCallRuntime());
4509 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4510 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4511 codegen_->AddSlowPath(slow_path);
4512 if (generate_null_check) {
4513 __ Beqz(out, slow_path->GetEntryLabel());
4514 }
4515 if (cls->MustGenerateClinitCheck()) {
4516 GenerateClassInitializationCheck(slow_path, out);
4517 } else {
4518 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004519 }
4520 }
4521}
4522
4523static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004524 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004525}
4526
4527void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4528 LocationSummary* locations =
4529 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4530 locations->SetOut(Location::RequiresRegister());
4531}
4532
4533void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4534 Register out = load->GetLocations()->Out().AsRegister<Register>();
4535 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4536}
4537
4538void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4539 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4540}
4541
4542void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4543 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4544}
4545
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004546void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004547 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004548 ? LocationSummary::kCallOnSlowPath
4549 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004551 HLoadString::LoadKind load_kind = load->GetLoadKind();
4552 switch (load_kind) {
4553 // We need an extra register for PC-relative literals on R2.
4554 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4555 case HLoadString::LoadKind::kBootImageAddress:
4556 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4557 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4558 break;
4559 }
4560 FALLTHROUGH_INTENDED;
4561 // We need an extra register for PC-relative dex cache accesses.
4562 case HLoadString::LoadKind::kDexCachePcRelative:
4563 case HLoadString::LoadKind::kDexCacheViaMethod:
4564 locations->SetInAt(0, Location::RequiresRegister());
4565 break;
4566 default:
4567 break;
4568 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004569 locations->SetOut(Location::RequiresRegister());
4570}
4571
4572void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004573 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004574 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004575 Location out_loc = locations->Out();
4576 Register out = out_loc.AsRegister<Register>();
4577 Register base_or_current_method_reg;
4578 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4579 switch (load_kind) {
4580 // We need an extra register for PC-relative literals on R2.
4581 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4582 case HLoadString::LoadKind::kBootImageAddress:
4583 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4584 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4585 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004586 default:
4587 base_or_current_method_reg = ZERO;
4588 break;
4589 }
4590
4591 switch (load_kind) {
4592 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4593 DCHECK(!kEmitCompilerReadBarrier);
4594 __ LoadLiteral(out,
4595 base_or_current_method_reg,
4596 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4597 load->GetStringIndex()));
4598 return; // No dex cache slow path.
4599 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4600 DCHECK(!kEmitCompilerReadBarrier);
4601 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4602 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004603 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004604 if (isR6) {
4605 __ Bind(&info->high_label);
4606 __ Bind(&info->pc_rel_label);
4607 // Add a 32-bit offset to PC.
4608 __ Auipc(out, /* placeholder */ 0x1234);
4609 __ Addiu(out, out, /* placeholder */ 0x5678);
4610 } else {
4611 __ Bind(&info->high_label);
4612 __ Lui(out, /* placeholder */ 0x1234);
4613 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4614 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4615 __ Ori(out, out, /* placeholder */ 0x5678);
4616 // Add a 32-bit offset to PC.
4617 __ Addu(out, out, base_or_current_method_reg);
4618 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004619 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004620 return; // No dex cache slow path.
4621 }
4622 case HLoadString::LoadKind::kBootImageAddress: {
4623 DCHECK(!kEmitCompilerReadBarrier);
4624 DCHECK_NE(load->GetAddress(), 0u);
4625 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4626 __ LoadLiteral(out,
4627 base_or_current_method_reg,
4628 codegen_->DeduplicateBootImageAddressLiteral(address));
4629 return; // No dex cache slow path.
4630 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004631 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004632 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004633 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004634
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004635 // TODO: Re-add the compiler code to do string dex cache lookup again.
4636 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4637 codegen_->AddSlowPath(slow_path);
4638 __ B(slow_path->GetEntryLabel());
4639 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004640}
4641
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004642void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4643 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4644 locations->SetOut(Location::ConstantLocation(constant));
4645}
4646
4647void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4648 // Will be generated at use site.
4649}
4650
4651void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4652 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004653 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004654 InvokeRuntimeCallingConvention calling_convention;
4655 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4656}
4657
4658void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4659 if (instruction->IsEnter()) {
4660 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4661 instruction,
4662 instruction->GetDexPc(),
4663 nullptr,
4664 IsDirectEntrypoint(kQuickLockObject));
4665 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4666 } else {
4667 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4668 instruction,
4669 instruction->GetDexPc(),
4670 nullptr,
4671 IsDirectEntrypoint(kQuickUnlockObject));
4672 }
4673 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4674}
4675
4676void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4677 LocationSummary* locations =
4678 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4679 switch (mul->GetResultType()) {
4680 case Primitive::kPrimInt:
4681 case Primitive::kPrimLong:
4682 locations->SetInAt(0, Location::RequiresRegister());
4683 locations->SetInAt(1, Location::RequiresRegister());
4684 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4685 break;
4686
4687 case Primitive::kPrimFloat:
4688 case Primitive::kPrimDouble:
4689 locations->SetInAt(0, Location::RequiresFpuRegister());
4690 locations->SetInAt(1, Location::RequiresFpuRegister());
4691 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4692 break;
4693
4694 default:
4695 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4696 }
4697}
4698
4699void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4700 Primitive::Type type = instruction->GetType();
4701 LocationSummary* locations = instruction->GetLocations();
4702 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4703
4704 switch (type) {
4705 case Primitive::kPrimInt: {
4706 Register dst = locations->Out().AsRegister<Register>();
4707 Register lhs = locations->InAt(0).AsRegister<Register>();
4708 Register rhs = locations->InAt(1).AsRegister<Register>();
4709
4710 if (isR6) {
4711 __ MulR6(dst, lhs, rhs);
4712 } else {
4713 __ MulR2(dst, lhs, rhs);
4714 }
4715 break;
4716 }
4717 case Primitive::kPrimLong: {
4718 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4719 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4720 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4721 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4722 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4723 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4724
4725 // Extra checks to protect caused by the existance of A1_A2.
4726 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4727 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4728 DCHECK_NE(dst_high, lhs_low);
4729 DCHECK_NE(dst_high, rhs_low);
4730
4731 // A_B * C_D
4732 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4733 // dst_lo: [ low(B*D) ]
4734 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4735
4736 if (isR6) {
4737 __ MulR6(TMP, lhs_high, rhs_low);
4738 __ MulR6(dst_high, lhs_low, rhs_high);
4739 __ Addu(dst_high, dst_high, TMP);
4740 __ MuhuR6(TMP, lhs_low, rhs_low);
4741 __ Addu(dst_high, dst_high, TMP);
4742 __ MulR6(dst_low, lhs_low, rhs_low);
4743 } else {
4744 __ MulR2(TMP, lhs_high, rhs_low);
4745 __ MulR2(dst_high, lhs_low, rhs_high);
4746 __ Addu(dst_high, dst_high, TMP);
4747 __ MultuR2(lhs_low, rhs_low);
4748 __ Mfhi(TMP);
4749 __ Addu(dst_high, dst_high, TMP);
4750 __ Mflo(dst_low);
4751 }
4752 break;
4753 }
4754 case Primitive::kPrimFloat:
4755 case Primitive::kPrimDouble: {
4756 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4757 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4758 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4759 if (type == Primitive::kPrimFloat) {
4760 __ MulS(dst, lhs, rhs);
4761 } else {
4762 __ MulD(dst, lhs, rhs);
4763 }
4764 break;
4765 }
4766 default:
4767 LOG(FATAL) << "Unexpected mul type " << type;
4768 }
4769}
4770
4771void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4772 LocationSummary* locations =
4773 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4774 switch (neg->GetResultType()) {
4775 case Primitive::kPrimInt:
4776 case Primitive::kPrimLong:
4777 locations->SetInAt(0, Location::RequiresRegister());
4778 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4779 break;
4780
4781 case Primitive::kPrimFloat:
4782 case Primitive::kPrimDouble:
4783 locations->SetInAt(0, Location::RequiresFpuRegister());
4784 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4785 break;
4786
4787 default:
4788 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4789 }
4790}
4791
4792void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4793 Primitive::Type type = instruction->GetType();
4794 LocationSummary* locations = instruction->GetLocations();
4795
4796 switch (type) {
4797 case Primitive::kPrimInt: {
4798 Register dst = locations->Out().AsRegister<Register>();
4799 Register src = locations->InAt(0).AsRegister<Register>();
4800 __ Subu(dst, ZERO, src);
4801 break;
4802 }
4803 case Primitive::kPrimLong: {
4804 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4805 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4806 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4807 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4808 __ Subu(dst_low, ZERO, src_low);
4809 __ Sltu(TMP, ZERO, dst_low);
4810 __ Subu(dst_high, ZERO, src_high);
4811 __ Subu(dst_high, dst_high, TMP);
4812 break;
4813 }
4814 case Primitive::kPrimFloat:
4815 case Primitive::kPrimDouble: {
4816 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4817 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4818 if (type == Primitive::kPrimFloat) {
4819 __ NegS(dst, src);
4820 } else {
4821 __ NegD(dst, src);
4822 }
4823 break;
4824 }
4825 default:
4826 LOG(FATAL) << "Unexpected neg type " << type;
4827 }
4828}
4829
4830void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4831 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004832 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004833 InvokeRuntimeCallingConvention calling_convention;
4834 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4835 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4836 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4837 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4838}
4839
4840void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4841 InvokeRuntimeCallingConvention calling_convention;
4842 Register current_method_register = calling_convention.GetRegisterAt(2);
4843 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4844 // Move an uint16_t value to a register.
4845 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4846 codegen_->InvokeRuntime(
Andreas Gampe542451c2016-07-26 09:02:02 -07004847 GetThreadOffset<kMipsPointerSize>(instruction->GetEntrypoint()).Int32Value(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004848 instruction,
4849 instruction->GetDexPc(),
4850 nullptr,
4851 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4852 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4853 void*, uint32_t, int32_t, ArtMethod*>();
4854}
4855
4856void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4857 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004858 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004859 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004860 if (instruction->IsStringAlloc()) {
4861 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4862 } else {
4863 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4864 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4865 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004866 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4867}
4868
4869void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004870 if (instruction->IsStringAlloc()) {
4871 // String is allocated through StringFactory. Call NewEmptyString entry point.
4872 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004873 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004874 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4875 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4876 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004877 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00004878 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4879 } else {
4880 codegen_->InvokeRuntime(
Andreas Gampe542451c2016-07-26 09:02:02 -07004881 GetThreadOffset<kMipsPointerSize>(instruction->GetEntrypoint()).Int32Value(),
David Brazdil6de19382016-01-08 17:37:10 +00004882 instruction,
4883 instruction->GetDexPc(),
4884 nullptr,
4885 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4886 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4887 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004888}
4889
4890void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4891 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4892 locations->SetInAt(0, Location::RequiresRegister());
4893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4894}
4895
4896void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4897 Primitive::Type type = instruction->GetType();
4898 LocationSummary* locations = instruction->GetLocations();
4899
4900 switch (type) {
4901 case Primitive::kPrimInt: {
4902 Register dst = locations->Out().AsRegister<Register>();
4903 Register src = locations->InAt(0).AsRegister<Register>();
4904 __ Nor(dst, src, ZERO);
4905 break;
4906 }
4907
4908 case Primitive::kPrimLong: {
4909 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4910 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4911 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4912 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4913 __ Nor(dst_high, src_high, ZERO);
4914 __ Nor(dst_low, src_low, ZERO);
4915 break;
4916 }
4917
4918 default:
4919 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4920 }
4921}
4922
4923void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4924 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4925 locations->SetInAt(0, Location::RequiresRegister());
4926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4927}
4928
4929void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4930 LocationSummary* locations = instruction->GetLocations();
4931 __ Xori(locations->Out().AsRegister<Register>(),
4932 locations->InAt(0).AsRegister<Register>(),
4933 1);
4934}
4935
4936void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4937 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4938 ? LocationSummary::kCallOnSlowPath
4939 : LocationSummary::kNoCall;
4940 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4941 locations->SetInAt(0, Location::RequiresRegister());
4942 if (instruction->HasUses()) {
4943 locations->SetOut(Location::SameAsFirstInput());
4944 }
4945}
4946
Calin Juravle2ae48182016-03-16 14:05:09 +00004947void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4948 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004949 return;
4950 }
4951 Location obj = instruction->GetLocations()->InAt(0);
4952
4953 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004954 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004955}
4956
Calin Juravle2ae48182016-03-16 14:05:09 +00004957void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004958 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004959 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004960
4961 Location obj = instruction->GetLocations()->InAt(0);
4962
4963 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4964}
4965
4966void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004967 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004968}
4969
4970void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4971 HandleBinaryOp(instruction);
4972}
4973
4974void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4975 HandleBinaryOp(instruction);
4976}
4977
4978void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4979 LOG(FATAL) << "Unreachable";
4980}
4981
4982void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4983 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4984}
4985
4986void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4988 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4989 if (location.IsStackSlot()) {
4990 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4991 } else if (location.IsDoubleStackSlot()) {
4992 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4993 }
4994 locations->SetOut(location);
4995}
4996
4997void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4998 ATTRIBUTE_UNUSED) {
4999 // Nothing to do, the parameter is already at its location.
5000}
5001
5002void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5003 LocationSummary* locations =
5004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5005 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5006}
5007
5008void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5009 ATTRIBUTE_UNUSED) {
5010 // Nothing to do, the method is already at its location.
5011}
5012
5013void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005015 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005016 locations->SetInAt(i, Location::Any());
5017 }
5018 locations->SetOut(Location::Any());
5019}
5020
5021void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5022 LOG(FATAL) << "Unreachable";
5023}
5024
5025void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5026 Primitive::Type type = rem->GetResultType();
5027 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005028 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005029 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5030
5031 switch (type) {
5032 case Primitive::kPrimInt:
5033 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005034 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5036 break;
5037
5038 case Primitive::kPrimLong: {
5039 InvokeRuntimeCallingConvention calling_convention;
5040 locations->SetInAt(0, Location::RegisterPairLocation(
5041 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5042 locations->SetInAt(1, Location::RegisterPairLocation(
5043 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5044 locations->SetOut(calling_convention.GetReturnLocation(type));
5045 break;
5046 }
5047
5048 case Primitive::kPrimFloat:
5049 case Primitive::kPrimDouble: {
5050 InvokeRuntimeCallingConvention calling_convention;
5051 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5052 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5053 locations->SetOut(calling_convention.GetReturnLocation(type));
5054 break;
5055 }
5056
5057 default:
5058 LOG(FATAL) << "Unexpected rem type " << type;
5059 }
5060}
5061
5062void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5063 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005064
5065 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005066 case Primitive::kPrimInt:
5067 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005068 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005069 case Primitive::kPrimLong: {
5070 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
5071 instruction,
5072 instruction->GetDexPc(),
5073 nullptr,
5074 IsDirectEntrypoint(kQuickLmod));
5075 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5076 break;
5077 }
5078 case Primitive::kPrimFloat: {
5079 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
5080 instruction, instruction->GetDexPc(),
5081 nullptr,
5082 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00005083 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005084 break;
5085 }
5086 case Primitive::kPrimDouble: {
5087 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
5088 instruction, instruction->GetDexPc(),
5089 nullptr,
5090 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00005091 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005092 break;
5093 }
5094 default:
5095 LOG(FATAL) << "Unexpected rem type " << type;
5096 }
5097}
5098
5099void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5100 memory_barrier->SetLocations(nullptr);
5101}
5102
5103void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5104 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5105}
5106
5107void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5108 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5109 Primitive::Type return_type = ret->InputAt(0)->GetType();
5110 locations->SetInAt(0, MipsReturnLocation(return_type));
5111}
5112
5113void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5114 codegen_->GenerateFrameExit();
5115}
5116
5117void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5118 ret->SetLocations(nullptr);
5119}
5120
5121void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5122 codegen_->GenerateFrameExit();
5123}
5124
Alexey Frunze92d90602015-12-18 18:16:36 -08005125void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5126 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005127}
5128
Alexey Frunze92d90602015-12-18 18:16:36 -08005129void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5130 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005131}
5132
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005133void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5134 HandleShift(shl);
5135}
5136
5137void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5138 HandleShift(shl);
5139}
5140
5141void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5142 HandleShift(shr);
5143}
5144
5145void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5146 HandleShift(shr);
5147}
5148
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005149void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5150 HandleBinaryOp(instruction);
5151}
5152
5153void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5154 HandleBinaryOp(instruction);
5155}
5156
5157void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5158 HandleFieldGet(instruction, instruction->GetFieldInfo());
5159}
5160
5161void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5162 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5163}
5164
5165void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5166 HandleFieldSet(instruction, instruction->GetFieldInfo());
5167}
5168
5169void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5170 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5171}
5172
5173void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5174 HUnresolvedInstanceFieldGet* instruction) {
5175 FieldAccessCallingConventionMIPS calling_convention;
5176 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5177 instruction->GetFieldType(),
5178 calling_convention);
5179}
5180
5181void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5182 HUnresolvedInstanceFieldGet* instruction) {
5183 FieldAccessCallingConventionMIPS calling_convention;
5184 codegen_->GenerateUnresolvedFieldAccess(instruction,
5185 instruction->GetFieldType(),
5186 instruction->GetFieldIndex(),
5187 instruction->GetDexPc(),
5188 calling_convention);
5189}
5190
5191void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5192 HUnresolvedInstanceFieldSet* instruction) {
5193 FieldAccessCallingConventionMIPS calling_convention;
5194 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5195 instruction->GetFieldType(),
5196 calling_convention);
5197}
5198
5199void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5200 HUnresolvedInstanceFieldSet* instruction) {
5201 FieldAccessCallingConventionMIPS calling_convention;
5202 codegen_->GenerateUnresolvedFieldAccess(instruction,
5203 instruction->GetFieldType(),
5204 instruction->GetFieldIndex(),
5205 instruction->GetDexPc(),
5206 calling_convention);
5207}
5208
5209void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5210 HUnresolvedStaticFieldGet* instruction) {
5211 FieldAccessCallingConventionMIPS calling_convention;
5212 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5213 instruction->GetFieldType(),
5214 calling_convention);
5215}
5216
5217void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5218 HUnresolvedStaticFieldGet* instruction) {
5219 FieldAccessCallingConventionMIPS calling_convention;
5220 codegen_->GenerateUnresolvedFieldAccess(instruction,
5221 instruction->GetFieldType(),
5222 instruction->GetFieldIndex(),
5223 instruction->GetDexPc(),
5224 calling_convention);
5225}
5226
5227void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5228 HUnresolvedStaticFieldSet* instruction) {
5229 FieldAccessCallingConventionMIPS calling_convention;
5230 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5231 instruction->GetFieldType(),
5232 calling_convention);
5233}
5234
5235void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5236 HUnresolvedStaticFieldSet* instruction) {
5237 FieldAccessCallingConventionMIPS calling_convention;
5238 codegen_->GenerateUnresolvedFieldAccess(instruction,
5239 instruction->GetFieldType(),
5240 instruction->GetFieldIndex(),
5241 instruction->GetDexPc(),
5242 calling_convention);
5243}
5244
5245void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5247}
5248
5249void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5250 HBasicBlock* block = instruction->GetBlock();
5251 if (block->GetLoopInformation() != nullptr) {
5252 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5253 // The back edge will generate the suspend check.
5254 return;
5255 }
5256 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5257 // The goto will generate the suspend check.
5258 return;
5259 }
5260 GenerateSuspendCheck(instruction, nullptr);
5261}
5262
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005263void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5264 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005265 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005266 InvokeRuntimeCallingConvention calling_convention;
5267 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5268}
5269
5270void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
5271 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5272 instruction,
5273 instruction->GetDexPc(),
5274 nullptr,
5275 IsDirectEntrypoint(kQuickDeliverException));
5276 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5277}
5278
5279void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5280 Primitive::Type input_type = conversion->GetInputType();
5281 Primitive::Type result_type = conversion->GetResultType();
5282 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005283 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005284
5285 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5286 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5287 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5288 }
5289
5290 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005291 if (!isR6 &&
5292 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5293 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005294 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005295 }
5296
5297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5298
5299 if (call_kind == LocationSummary::kNoCall) {
5300 if (Primitive::IsFloatingPointType(input_type)) {
5301 locations->SetInAt(0, Location::RequiresFpuRegister());
5302 } else {
5303 locations->SetInAt(0, Location::RequiresRegister());
5304 }
5305
5306 if (Primitive::IsFloatingPointType(result_type)) {
5307 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5308 } else {
5309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5310 }
5311 } else {
5312 InvokeRuntimeCallingConvention calling_convention;
5313
5314 if (Primitive::IsFloatingPointType(input_type)) {
5315 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5316 } else {
5317 DCHECK_EQ(input_type, Primitive::kPrimLong);
5318 locations->SetInAt(0, Location::RegisterPairLocation(
5319 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5320 }
5321
5322 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5323 }
5324}
5325
5326void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5327 LocationSummary* locations = conversion->GetLocations();
5328 Primitive::Type result_type = conversion->GetResultType();
5329 Primitive::Type input_type = conversion->GetInputType();
5330 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005331 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005332
5333 DCHECK_NE(input_type, result_type);
5334
5335 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5336 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5337 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5338 Register src = locations->InAt(0).AsRegister<Register>();
5339
Alexey Frunzea871ef12016-06-27 15:20:11 -07005340 if (dst_low != src) {
5341 __ Move(dst_low, src);
5342 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005343 __ Sra(dst_high, src, 31);
5344 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5345 Register dst = locations->Out().AsRegister<Register>();
5346 Register src = (input_type == Primitive::kPrimLong)
5347 ? locations->InAt(0).AsRegisterPairLow<Register>()
5348 : locations->InAt(0).AsRegister<Register>();
5349
5350 switch (result_type) {
5351 case Primitive::kPrimChar:
5352 __ Andi(dst, src, 0xFFFF);
5353 break;
5354 case Primitive::kPrimByte:
5355 if (has_sign_extension) {
5356 __ Seb(dst, src);
5357 } else {
5358 __ Sll(dst, src, 24);
5359 __ Sra(dst, dst, 24);
5360 }
5361 break;
5362 case Primitive::kPrimShort:
5363 if (has_sign_extension) {
5364 __ Seh(dst, src);
5365 } else {
5366 __ Sll(dst, src, 16);
5367 __ Sra(dst, dst, 16);
5368 }
5369 break;
5370 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005371 if (dst != src) {
5372 __ Move(dst, src);
5373 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005374 break;
5375
5376 default:
5377 LOG(FATAL) << "Unexpected type conversion from " << input_type
5378 << " to " << result_type;
5379 }
5380 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005381 if (input_type == Primitive::kPrimLong) {
5382 if (isR6) {
5383 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5384 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5385 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5386 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5387 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5388 __ Mtc1(src_low, FTMP);
5389 __ Mthc1(src_high, FTMP);
5390 if (result_type == Primitive::kPrimFloat) {
5391 __ Cvtsl(dst, FTMP);
5392 } else {
5393 __ Cvtdl(dst, FTMP);
5394 }
5395 } else {
5396 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
5397 : QUICK_ENTRY_POINT(pL2d);
5398 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
5399 : IsDirectEntrypoint(kQuickL2d);
5400 codegen_->InvokeRuntime(entry_offset,
5401 conversion,
5402 conversion->GetDexPc(),
5403 nullptr,
5404 direct);
5405 if (result_type == Primitive::kPrimFloat) {
5406 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5407 } else {
5408 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5409 }
5410 }
5411 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005412 Register src = locations->InAt(0).AsRegister<Register>();
5413 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5414 __ Mtc1(src, FTMP);
5415 if (result_type == Primitive::kPrimFloat) {
5416 __ Cvtsw(dst, FTMP);
5417 } else {
5418 __ Cvtdw(dst, FTMP);
5419 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005420 }
5421 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5422 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005423 if (result_type == Primitive::kPrimLong) {
5424 if (isR6) {
5425 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5426 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5427 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5428 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5429 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5430 MipsLabel truncate;
5431 MipsLabel done;
5432
5433 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5434 // value when the input is either a NaN or is outside of the range of the output type
5435 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5436 // the same result.
5437 //
5438 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5439 // value of the output type if the input is outside of the range after the truncation or
5440 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5441 // results. This matches the desired float/double-to-int/long conversion exactly.
5442 //
5443 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5444 //
5445 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5446 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5447 // even though it must be NAN2008=1 on R6.
5448 //
5449 // The code takes care of the different behaviors by first comparing the input to the
5450 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5451 // If the input is greater than or equal to the minimum, it procedes to the truncate
5452 // instruction, which will handle such an input the same way irrespective of NAN2008.
5453 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5454 // in order to return either zero or the minimum value.
5455 //
5456 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5457 // truncate instruction for MIPS64R6.
5458 if (input_type == Primitive::kPrimFloat) {
5459 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5460 __ LoadConst32(TMP, min_val);
5461 __ Mtc1(TMP, FTMP);
5462 __ CmpLeS(FTMP, FTMP, src);
5463 } else {
5464 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5465 __ LoadConst32(TMP, High32Bits(min_val));
5466 __ Mtc1(ZERO, FTMP);
5467 __ Mthc1(TMP, FTMP);
5468 __ CmpLeD(FTMP, FTMP, src);
5469 }
5470
5471 __ Bc1nez(FTMP, &truncate);
5472
5473 if (input_type == Primitive::kPrimFloat) {
5474 __ CmpEqS(FTMP, src, src);
5475 } else {
5476 __ CmpEqD(FTMP, src, src);
5477 }
5478 __ Move(dst_low, ZERO);
5479 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5480 __ Mfc1(TMP, FTMP);
5481 __ And(dst_high, dst_high, TMP);
5482
5483 __ B(&done);
5484
5485 __ Bind(&truncate);
5486
5487 if (input_type == Primitive::kPrimFloat) {
5488 __ TruncLS(FTMP, src);
5489 } else {
5490 __ TruncLD(FTMP, src);
5491 }
5492 __ Mfc1(dst_low, FTMP);
5493 __ Mfhc1(dst_high, FTMP);
5494
5495 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005496 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005497 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5498 : QUICK_ENTRY_POINT(pD2l);
5499 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5500 : IsDirectEntrypoint(kQuickD2l);
5501 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5502 if (input_type == Primitive::kPrimFloat) {
5503 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5504 } else {
5505 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5506 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005507 }
5508 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005509 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5510 Register dst = locations->Out().AsRegister<Register>();
5511 MipsLabel truncate;
5512 MipsLabel done;
5513
5514 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5515 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5516 // even though it must be NAN2008=1 on R6.
5517 //
5518 // For details see the large comment above for the truncation of float/double to long on R6.
5519 //
5520 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5521 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005522 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005523 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5524 __ LoadConst32(TMP, min_val);
5525 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005526 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005527 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5528 __ LoadConst32(TMP, High32Bits(min_val));
5529 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005530 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005531 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005532
5533 if (isR6) {
5534 if (input_type == Primitive::kPrimFloat) {
5535 __ CmpLeS(FTMP, FTMP, src);
5536 } else {
5537 __ CmpLeD(FTMP, FTMP, src);
5538 }
5539 __ Bc1nez(FTMP, &truncate);
5540
5541 if (input_type == Primitive::kPrimFloat) {
5542 __ CmpEqS(FTMP, src, src);
5543 } else {
5544 __ CmpEqD(FTMP, src, src);
5545 }
5546 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5547 __ Mfc1(TMP, FTMP);
5548 __ And(dst, dst, TMP);
5549 } else {
5550 if (input_type == Primitive::kPrimFloat) {
5551 __ ColeS(0, FTMP, src);
5552 } else {
5553 __ ColeD(0, FTMP, src);
5554 }
5555 __ Bc1t(0, &truncate);
5556
5557 if (input_type == Primitive::kPrimFloat) {
5558 __ CeqS(0, src, src);
5559 } else {
5560 __ CeqD(0, src, src);
5561 }
5562 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5563 __ Movf(dst, ZERO, 0);
5564 }
5565
5566 __ B(&done);
5567
5568 __ Bind(&truncate);
5569
5570 if (input_type == Primitive::kPrimFloat) {
5571 __ TruncWS(FTMP, src);
5572 } else {
5573 __ TruncWD(FTMP, src);
5574 }
5575 __ Mfc1(dst, FTMP);
5576
5577 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005578 }
5579 } else if (Primitive::IsFloatingPointType(result_type) &&
5580 Primitive::IsFloatingPointType(input_type)) {
5581 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5582 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5583 if (result_type == Primitive::kPrimFloat) {
5584 __ Cvtsd(dst, src);
5585 } else {
5586 __ Cvtds(dst, src);
5587 }
5588 } else {
5589 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5590 << " to " << result_type;
5591 }
5592}
5593
5594void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5595 HandleShift(ushr);
5596}
5597
5598void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5599 HandleShift(ushr);
5600}
5601
5602void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5603 HandleBinaryOp(instruction);
5604}
5605
5606void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5607 HandleBinaryOp(instruction);
5608}
5609
5610void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5611 // Nothing to do, this should be removed during prepare for register allocator.
5612 LOG(FATAL) << "Unreachable";
5613}
5614
5615void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5616 // Nothing to do, this should be removed during prepare for register allocator.
5617 LOG(FATAL) << "Unreachable";
5618}
5619
5620void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005621 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005622}
5623
5624void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005625 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005626}
5627
5628void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005629 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005630}
5631
5632void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005633 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005634}
5635
5636void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005637 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005638}
5639
5640void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005641 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005642}
5643
5644void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005645 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005646}
5647
5648void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005649 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005650}
5651
5652void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005653 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005654}
5655
5656void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005657 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005658}
5659
5660void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005661 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005662}
5663
5664void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005665 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005666}
5667
5668void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005669 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005670}
5671
5672void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005673 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005674}
5675
5676void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005677 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005678}
5679
5680void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005681 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005682}
5683
5684void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005685 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005686}
5687
5688void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005689 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005690}
5691
5692void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005693 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005694}
5695
5696void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005697 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005698}
5699
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005700void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5701 LocationSummary* locations =
5702 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5703 locations->SetInAt(0, Location::RequiresRegister());
5704}
5705
5706void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5707 int32_t lower_bound = switch_instr->GetStartValue();
5708 int32_t num_entries = switch_instr->GetNumEntries();
5709 LocationSummary* locations = switch_instr->GetLocations();
5710 Register value_reg = locations->InAt(0).AsRegister<Register>();
5711 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5712
5713 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005714 Register temp_reg = TMP;
5715 __ Addiu32(temp_reg, value_reg, -lower_bound);
5716 // Jump to default if index is negative
5717 // Note: We don't check the case that index is positive while value < lower_bound, because in
5718 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5719 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5720
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005721 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005722 // Jump to successors[0] if value == lower_bound.
5723 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5724 int32_t last_index = 0;
5725 for (; num_entries - last_index > 2; last_index += 2) {
5726 __ Addiu(temp_reg, temp_reg, -2);
5727 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5728 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5729 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5730 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5731 }
5732 if (num_entries - last_index == 2) {
5733 // The last missing case_value.
5734 __ Addiu(temp_reg, temp_reg, -1);
5735 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005736 }
5737
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005738 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005739 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5740 __ B(codegen_->GetLabelOf(default_block));
5741 }
5742}
5743
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005744void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5745 HMipsComputeBaseMethodAddress* insn) {
5746 LocationSummary* locations =
5747 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5748 locations->SetOut(Location::RequiresRegister());
5749}
5750
5751void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5752 HMipsComputeBaseMethodAddress* insn) {
5753 LocationSummary* locations = insn->GetLocations();
5754 Register reg = locations->Out().AsRegister<Register>();
5755
5756 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5757
5758 // Generate a dummy PC-relative call to obtain PC.
5759 __ Nal();
5760 // Grab the return address off RA.
5761 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005762 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005763
5764 // Remember this offset (the obtained PC value) for later use with constant area.
5765 __ BindPcRelBaseLabel();
5766}
5767
5768void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5769 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5770 locations->SetOut(Location::RequiresRegister());
5771}
5772
5773void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5774 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5775 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5776 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005777 bool reordering = __ SetReorder(false);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005778 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5779 __ Bind(&info->high_label);
5780 __ Bind(&info->pc_rel_label);
5781 // Add a 32-bit offset to PC.
5782 __ Auipc(reg, /* placeholder */ 0x1234);
5783 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5784 } else {
5785 // Generate a dummy PC-relative call to obtain PC.
5786 __ Nal();
5787 __ Bind(&info->high_label);
5788 __ Lui(reg, /* placeholder */ 0x1234);
5789 __ Bind(&info->pc_rel_label);
5790 __ Ori(reg, reg, /* placeholder */ 0x5678);
5791 // Add a 32-bit offset to PC.
5792 __ Addu(reg, reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005793 // TODO: Can we share this code with that of VisitMipsComputeBaseMethodAddress()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005794 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005795 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005796}
5797
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005798void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5799 // The trampoline uses the same calling convention as dex calling conventions,
5800 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5801 // the method_idx.
5802 HandleInvoke(invoke);
5803}
5804
5805void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5806 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5807}
5808
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005809void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5810 LocationSummary* locations =
5811 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5812 locations->SetInAt(0, Location::RequiresRegister());
5813 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005814}
5815
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005816void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5817 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005818 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005819 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005820 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005821 __ LoadFromOffset(kLoadWord,
5822 locations->Out().AsRegister<Register>(),
5823 locations->InAt(0).AsRegister<Register>(),
5824 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005825 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005826 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005827 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005828 __ LoadFromOffset(kLoadWord,
5829 locations->Out().AsRegister<Register>(),
5830 locations->InAt(0).AsRegister<Register>(),
5831 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005832 __ LoadFromOffset(kLoadWord,
5833 locations->Out().AsRegister<Register>(),
5834 locations->Out().AsRegister<Register>(),
5835 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005836 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005837}
5838
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005839#undef __
5840#undef QUICK_ENTRY_POINT
5841
5842} // namespace mips
5843} // namespace art