blob: 58879bc2f1e043bad4e278aede8a270d2ba2d633 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020023#include "entrypoints/quick/quick_entrypoints.h"
24#include "entrypoints/quick/quick_entrypoints_enum.h"
25#include "gc/accounting/card_table.h"
26#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070027#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020028#include "mirror/array-inl.h"
29#include "mirror/class-inl.h"
30#include "offsets.h"
31#include "thread.h"
32#include "utils/assembler.h"
33#include "utils/mips/assembler_mips.h"
34#include "utils/stack_checks.h"
35
36namespace art {
37namespace mips {
38
39static constexpr int kCurrentMethodStackOffset = 0;
40static constexpr Register kMethodRegisterArgument = A0;
41
Alexey Frunzee3fb2452016-05-10 16:08:05 -070042// We'll maximize the range of a single load instruction for dex cache array accesses
43// by aligning offset -32768 with the offset of the first used element.
44static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
45
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020046Location MipsReturnLocation(Primitive::Type return_type) {
47 switch (return_type) {
48 case Primitive::kPrimBoolean:
49 case Primitive::kPrimByte:
50 case Primitive::kPrimChar:
51 case Primitive::kPrimShort:
52 case Primitive::kPrimInt:
53 case Primitive::kPrimNot:
54 return Location::RegisterLocation(V0);
55
56 case Primitive::kPrimLong:
57 return Location::RegisterPairLocation(V0, V1);
58
59 case Primitive::kPrimFloat:
60 case Primitive::kPrimDouble:
61 return Location::FpuRegisterLocation(F0);
62
63 case Primitive::kPrimVoid:
64 return Location();
65 }
66 UNREACHABLE();
67}
68
69Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
70 return MipsReturnLocation(type);
71}
72
73Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
74 return Location::RegisterLocation(kMethodRegisterArgument);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
78 Location next_location;
79
80 switch (type) {
81 case Primitive::kPrimBoolean:
82 case Primitive::kPrimByte:
83 case Primitive::kPrimChar:
84 case Primitive::kPrimShort:
85 case Primitive::kPrimInt:
86 case Primitive::kPrimNot: {
87 uint32_t gp_index = gp_index_++;
88 if (gp_index < calling_convention.GetNumberOfRegisters()) {
89 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
90 } else {
91 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
92 next_location = Location::StackSlot(stack_offset);
93 }
94 break;
95 }
96
97 case Primitive::kPrimLong: {
98 uint32_t gp_index = gp_index_;
99 gp_index_ += 2;
100 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
101 if (calling_convention.GetRegisterAt(gp_index) == A1) {
102 gp_index_++; // Skip A1, and use A2_A3 instead.
103 gp_index++;
104 }
105 Register low_even = calling_convention.GetRegisterAt(gp_index);
106 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
107 DCHECK_EQ(low_even + 1, high_odd);
108 next_location = Location::RegisterPairLocation(low_even, high_odd);
109 } else {
110 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
111 next_location = Location::DoubleStackSlot(stack_offset);
112 }
113 break;
114 }
115
116 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
117 // will take up the even/odd pair, while floats are stored in even regs only.
118 // On 64 bit FPU, both double and float are stored in even registers only.
119 case Primitive::kPrimFloat:
120 case Primitive::kPrimDouble: {
121 uint32_t float_index = float_index_++;
122 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
123 next_location = Location::FpuRegisterLocation(
124 calling_convention.GetFpuRegisterAt(float_index));
125 } else {
126 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
127 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
128 : Location::StackSlot(stack_offset);
129 }
130 break;
131 }
132
133 case Primitive::kPrimVoid:
134 LOG(FATAL) << "Unexpected parameter type " << type;
135 break;
136 }
137
138 // Space on the stack is reserved for all arguments.
139 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
140
141 return next_location;
142}
143
144Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
145 return MipsReturnLocation(type);
146}
147
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100148// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
149#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700150#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200151
152class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
153 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000154 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155
156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
157 LocationSummary* locations = instruction_->GetLocations();
158 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
159 __ Bind(GetEntryLabel());
160 if (instruction_->CanThrowIntoCatchBlock()) {
161 // Live registers will be restored in the catch block if caught.
162 SaveLiveRegisters(codegen, instruction_->GetLocations());
163 }
164 // We're moving two locations to locations that could overlap, so we need a parallel
165 // move resolver.
166 InvokeRuntimeCallingConvention calling_convention;
167 codegen->EmitParallelMoves(locations->InAt(0),
168 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
169 Primitive::kPrimInt,
170 locations->InAt(1),
171 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
172 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100173 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
174 ? QUICK_ENTRY_POINT(pThrowStringBounds)
175 : QUICK_ENTRY_POINT(pThrowArrayBounds);
176 mips_codegen->InvokeRuntime(entry_point_offset,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200177 instruction_,
178 instruction_->GetDexPc(),
179 this,
180 IsDirectEntrypoint(kQuickThrowArrayBounds));
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100181 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200182 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
183 }
184
185 bool IsFatal() const OVERRIDE { return true; }
186
187 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
188
189 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200190 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
191};
192
193class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
194 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000195 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196
197 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
198 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
199 __ Bind(GetEntryLabel());
200 if (instruction_->CanThrowIntoCatchBlock()) {
201 // Live registers will be restored in the catch block if caught.
202 SaveLiveRegisters(codegen, instruction_->GetLocations());
203 }
204 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
205 instruction_,
206 instruction_->GetDexPc(),
207 this,
208 IsDirectEntrypoint(kQuickThrowDivZero));
209 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
210 }
211
212 bool IsFatal() const OVERRIDE { return true; }
213
214 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
215
216 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
218};
219
220class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
221 public:
222 LoadClassSlowPathMIPS(HLoadClass* cls,
223 HInstruction* at,
224 uint32_t dex_pc,
225 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000226 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 LocationSummary* locations = at_->GetLocations();
232 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
233
234 __ Bind(GetEntryLabel());
235 SaveLiveRegisters(codegen, locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
238 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
239
240 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
241 : QUICK_ENTRY_POINT(pInitializeType);
242 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
243 : IsDirectEntrypoint(kQuickInitializeType);
244
245 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
246 if (do_clinit_) {
247 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
248 } else {
249 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
250 }
251
252 // Move the class to the desired location.
253 Location out = locations->Out();
254 if (out.IsValid()) {
255 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
256 Primitive::Type type = at_->GetType();
257 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
258 }
259
260 RestoreLiveRegisters(codegen, locations);
261 __ B(GetExitLabel());
262 }
263
264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
265
266 private:
267 // The class this slow path will load.
268 HLoadClass* const cls_;
269
270 // The instruction where this slow path is happening.
271 // (Might be the load class or an initialization check).
272 HInstruction* const at_;
273
274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
281};
282
283class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
284 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286
287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
288 LocationSummary* locations = instruction_->GetLocations();
289 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
290 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
291
292 __ Bind(GetEntryLabel());
293 SaveLiveRegisters(codegen, locations);
294
295 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000296 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
297 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200298 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
299 instruction_,
300 instruction_->GetDexPc(),
301 this,
302 IsDirectEntrypoint(kQuickResolveString));
303 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
304 Primitive::Type type = instruction_->GetType();
305 mips_codegen->MoveLocation(locations->Out(),
306 calling_convention.GetReturnLocation(type),
307 type);
308
309 RestoreLiveRegisters(codegen, locations);
310 __ B(GetExitLabel());
311 }
312
313 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
314
315 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
317};
318
319class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
320 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
325 __ Bind(GetEntryLabel());
326 if (instruction_->CanThrowIntoCatchBlock()) {
327 // Live registers will be restored in the catch block if caught.
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 }
330 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
331 instruction_,
332 instruction_->GetDexPc(),
333 this,
334 IsDirectEntrypoint(kQuickThrowNullPointer));
335 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
336 }
337
338 bool IsFatal() const OVERRIDE { return true; }
339
340 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
341
342 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200343 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
344};
345
346class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
347 public:
348 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
353 __ Bind(GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200354 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
355 instruction_,
356 instruction_->GetDexPc(),
357 this,
358 IsDirectEntrypoint(kQuickTestSuspend));
359 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200360 if (successor_ == nullptr) {
361 __ B(GetReturnLabel());
362 } else {
363 __ B(mips_codegen->GetLabelOf(successor_));
364 }
365 }
366
367 MipsLabel* GetReturnLabel() {
368 DCHECK(successor_ == nullptr);
369 return &return_label_;
370 }
371
372 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
373
374 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200375 // If not null, the block to branch to after the suspend check.
376 HBasicBlock* const successor_;
377
378 // If `successor_` is null, the label to branch to after the suspend check.
379 MipsLabel return_label_;
380
381 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
382};
383
384class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
385 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000386 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200387
388 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
389 LocationSummary* locations = instruction_->GetLocations();
390 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
391 uint32_t dex_pc = instruction_->GetDexPc();
392 DCHECK(instruction_->IsCheckCast()
393 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
394 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
395
396 __ Bind(GetEntryLabel());
397 SaveLiveRegisters(codegen, locations);
398
399 // We're moving two locations to locations that could overlap, so we need a parallel
400 // move resolver.
401 InvokeRuntimeCallingConvention calling_convention;
402 codegen->EmitParallelMoves(locations->InAt(1),
403 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
404 Primitive::kPrimNot,
405 object_class,
406 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
407 Primitive::kPrimNot);
408
409 if (instruction_->IsInstanceOf()) {
410 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
411 instruction_,
412 dex_pc,
413 this,
414 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000415 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700416 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 Primitive::Type ret_type = instruction_->GetType();
418 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
419 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 } else {
421 DCHECK(instruction_->IsCheckCast());
422 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
423 instruction_,
424 dex_pc,
425 this,
426 IsDirectEntrypoint(kQuickCheckCast));
427 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
428 }
429
430 RestoreLiveRegisters(codegen, locations);
431 __ B(GetExitLabel());
432 }
433
434 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
435
436 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
438};
439
440class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
441 public:
Aart Bik42249c32016-01-07 15:33:50 -0800442 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000443 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800446 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 __ Bind(GetEntryLabel());
448 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200449 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
450 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800451 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200452 this,
453 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000454 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 }
456
457 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
458
459 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200460 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
461};
462
463CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
464 const MipsInstructionSetFeatures& isa_features,
465 const CompilerOptions& compiler_options,
466 OptimizingCompilerStats* stats)
467 : CodeGenerator(graph,
468 kNumberOfCoreRegisters,
469 kNumberOfFRegisters,
470 kNumberOfRegisterPairs,
471 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
472 arraysize(kCoreCalleeSaves)),
473 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
474 arraysize(kFpuCalleeSaves)),
475 compiler_options,
476 stats),
477 block_labels_(nullptr),
478 location_builder_(graph, this),
479 instruction_visitor_(graph, this),
480 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100481 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700482 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700483 uint32_literals_(std::less<uint32_t>(),
484 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700485 method_patches_(MethodReferenceComparator(),
486 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
487 call_patches_(MethodReferenceComparator(),
488 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700489 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
490 boot_image_string_patches_(StringReferenceValueComparator(),
491 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
492 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
493 boot_image_type_patches_(TypeReferenceValueComparator(),
494 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
495 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
496 boot_image_address_patches_(std::less<uint32_t>(),
497 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
498 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200499 // Save RA (containing the return address) to mimic Quick.
500 AddAllocatedRegister(Location::RegisterLocation(RA));
501}
502
503#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100504// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
505#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700506#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200507
508void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
509 // Ensure that we fix up branches.
510 __ FinalizeCode();
511
512 // Adjust native pc offsets in stack maps.
513 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
514 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
515 uint32_t new_position = __ GetAdjustedPosition(old_position);
516 DCHECK_GE(new_position, old_position);
517 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
518 }
519
520 // Adjust pc offsets for the disassembly information.
521 if (disasm_info_ != nullptr) {
522 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
523 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
524 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
525 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
526 it.second.start = __ GetAdjustedPosition(it.second.start);
527 it.second.end = __ GetAdjustedPosition(it.second.end);
528 }
529 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
530 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
531 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
532 }
533 }
534
535 CodeGenerator::Finalize(allocator);
536}
537
538MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
539 return codegen_->GetAssembler();
540}
541
542void ParallelMoveResolverMIPS::EmitMove(size_t index) {
543 DCHECK_LT(index, moves_.size());
544 MoveOperands* move = moves_[index];
545 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
546}
547
548void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
549 DCHECK_LT(index, moves_.size());
550 MoveOperands* move = moves_[index];
551 Primitive::Type type = move->GetType();
552 Location loc1 = move->GetDestination();
553 Location loc2 = move->GetSource();
554
555 DCHECK(!loc1.IsConstant());
556 DCHECK(!loc2.IsConstant());
557
558 if (loc1.Equals(loc2)) {
559 return;
560 }
561
562 if (loc1.IsRegister() && loc2.IsRegister()) {
563 // Swap 2 GPRs.
564 Register r1 = loc1.AsRegister<Register>();
565 Register r2 = loc2.AsRegister<Register>();
566 __ Move(TMP, r2);
567 __ Move(r2, r1);
568 __ Move(r1, TMP);
569 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
570 FRegister f1 = loc1.AsFpuRegister<FRegister>();
571 FRegister f2 = loc2.AsFpuRegister<FRegister>();
572 if (type == Primitive::kPrimFloat) {
573 __ MovS(FTMP, f2);
574 __ MovS(f2, f1);
575 __ MovS(f1, FTMP);
576 } else {
577 DCHECK_EQ(type, Primitive::kPrimDouble);
578 __ MovD(FTMP, f2);
579 __ MovD(f2, f1);
580 __ MovD(f1, FTMP);
581 }
582 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
583 (loc1.IsFpuRegister() && loc2.IsRegister())) {
584 // Swap FPR and GPR.
585 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
586 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
587 : loc2.AsFpuRegister<FRegister>();
588 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
589 : loc2.AsRegister<Register>();
590 __ Move(TMP, r2);
591 __ Mfc1(r2, f1);
592 __ Mtc1(TMP, f1);
593 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
594 // Swap 2 GPR register pairs.
595 Register r1 = loc1.AsRegisterPairLow<Register>();
596 Register r2 = loc2.AsRegisterPairLow<Register>();
597 __ Move(TMP, r2);
598 __ Move(r2, r1);
599 __ Move(r1, TMP);
600 r1 = loc1.AsRegisterPairHigh<Register>();
601 r2 = loc2.AsRegisterPairHigh<Register>();
602 __ Move(TMP, r2);
603 __ Move(r2, r1);
604 __ Move(r1, TMP);
605 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
606 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
607 // Swap FPR and GPR register pair.
608 DCHECK_EQ(type, Primitive::kPrimDouble);
609 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
610 : loc2.AsFpuRegister<FRegister>();
611 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
612 : loc2.AsRegisterPairLow<Register>();
613 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
614 : loc2.AsRegisterPairHigh<Register>();
615 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
616 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
617 // unpredictable and the following mfch1 will fail.
618 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800619 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200620 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800621 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200622 __ Move(r2_l, TMP);
623 __ Move(r2_h, AT);
624 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
625 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
626 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
627 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000628 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
629 (loc1.IsStackSlot() && loc2.IsRegister())) {
630 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
631 : loc2.AsRegister<Register>();
632 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
633 : loc2.GetStackIndex();
634 __ Move(TMP, reg);
635 __ LoadFromOffset(kLoadWord, reg, SP, offset);
636 __ StoreToOffset(kStoreWord, TMP, SP, offset);
637 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
638 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
639 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
640 : loc2.AsRegisterPairLow<Register>();
641 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
642 : loc2.AsRegisterPairHigh<Register>();
643 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
644 : loc2.GetStackIndex();
645 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
646 : loc2.GetHighStackIndex(kMipsWordSize);
647 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000648 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000649 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000650 __ Move(TMP, reg_h);
651 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
652 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200653 } else {
654 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
655 }
656}
657
658void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
659 __ Pop(static_cast<Register>(reg));
660}
661
662void ParallelMoveResolverMIPS::SpillScratch(int reg) {
663 __ Push(static_cast<Register>(reg));
664}
665
666void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
667 // Allocate a scratch register other than TMP, if available.
668 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
669 // automatically unspilled when the scratch scope object is destroyed).
670 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
671 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
672 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
673 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
674 __ LoadFromOffset(kLoadWord,
675 Register(ensure_scratch.GetRegister()),
676 SP,
677 index1 + stack_offset);
678 __ LoadFromOffset(kLoadWord,
679 TMP,
680 SP,
681 index2 + stack_offset);
682 __ StoreToOffset(kStoreWord,
683 Register(ensure_scratch.GetRegister()),
684 SP,
685 index2 + stack_offset);
686 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
687 }
688}
689
Alexey Frunze73296a72016-06-03 22:51:46 -0700690void CodeGeneratorMIPS::ComputeSpillMask() {
691 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
692 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
693 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
694 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
695 // registers, include the ZERO register to force alignment of FPU callee-saved registers
696 // within the stack frame.
697 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
698 core_spill_mask_ |= (1 << ZERO);
699 }
Alexey Frunze06a46c42016-07-19 15:00:40 -0700700 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
701 // (this can happen in leaf methods), artificially spill the ZERO register in order to
702 // force explicit saving and restoring of RA. RA isn't saved/restored when it's the only
703 // spilled register.
704 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
705 // saved in an unused temporary register) and saving of RA and the current method pointer
706 // in the frame.
707 if (clobbered_ra_ && core_spill_mask_ == (1u << RA) && fpu_spill_mask_ == 0) {
708 core_spill_mask_ |= (1 << ZERO);
709 }
Alexey Frunze73296a72016-06-03 22:51:46 -0700710}
711
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200712static dwarf::Reg DWARFReg(Register reg) {
713 return dwarf::Reg::MipsCore(static_cast<int>(reg));
714}
715
716// TODO: mapping of floating-point registers to DWARF.
717
718void CodeGeneratorMIPS::GenerateFrameEntry() {
719 __ Bind(&frame_entry_label_);
720
721 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
722
723 if (do_overflow_check) {
724 __ LoadFromOffset(kLoadWord,
725 ZERO,
726 SP,
727 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
728 RecordPcInfo(nullptr, 0);
729 }
730
731 if (HasEmptyFrame()) {
732 return;
733 }
734
735 // Make sure the frame size isn't unreasonably large.
736 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
737 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
738 }
739
740 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200741
Alexey Frunze73296a72016-06-03 22:51:46 -0700742 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200743 __ IncreaseFrameSize(ofs);
744
Alexey Frunze73296a72016-06-03 22:51:46 -0700745 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
746 Register reg = static_cast<Register>(MostSignificantBit(mask));
747 mask ^= 1u << reg;
748 ofs -= kMipsWordSize;
749 // The ZERO register is only included for alignment.
750 if (reg != ZERO) {
751 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200752 __ cfi().RelOffset(DWARFReg(reg), ofs);
753 }
754 }
755
Alexey Frunze73296a72016-06-03 22:51:46 -0700756 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
757 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
758 mask ^= 1u << reg;
759 ofs -= kMipsDoublewordSize;
760 __ StoreDToOffset(reg, SP, ofs);
761 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200762 }
763
Alexey Frunze73296a72016-06-03 22:51:46 -0700764 // Store the current method pointer.
765 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200766}
767
768void CodeGeneratorMIPS::GenerateFrameExit() {
769 __ cfi().RememberState();
770
771 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200772 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200773
Alexey Frunze73296a72016-06-03 22:51:46 -0700774 // For better instruction scheduling restore RA before other registers.
775 uint32_t ofs = GetFrameSize();
776 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
777 Register reg = static_cast<Register>(MostSignificantBit(mask));
778 mask ^= 1u << reg;
779 ofs -= kMipsWordSize;
780 // The ZERO register is only included for alignment.
781 if (reg != ZERO) {
782 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200783 __ cfi().Restore(DWARFReg(reg));
784 }
785 }
786
Alexey Frunze73296a72016-06-03 22:51:46 -0700787 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
788 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
789 mask ^= 1u << reg;
790 ofs -= kMipsDoublewordSize;
791 __ LoadDFromOffset(reg, SP, ofs);
792 // TODO: __ cfi().Restore(DWARFReg(reg));
793 }
794
795 __ DecreaseFrameSize(GetFrameSize());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200796 }
797
798 __ Jr(RA);
799 __ Nop();
800
801 __ cfi().RestoreState();
802 __ cfi().DefCFAOffset(GetFrameSize());
803}
804
805void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
806 __ Bind(GetLabelOf(block));
807}
808
809void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
810 if (src.Equals(dst)) {
811 return;
812 }
813
814 if (src.IsConstant()) {
815 MoveConstant(dst, src.GetConstant());
816 } else {
817 if (Primitive::Is64BitType(dst_type)) {
818 Move64(dst, src);
819 } else {
820 Move32(dst, src);
821 }
822 }
823}
824
825void CodeGeneratorMIPS::Move32(Location destination, Location source) {
826 if (source.Equals(destination)) {
827 return;
828 }
829
830 if (destination.IsRegister()) {
831 if (source.IsRegister()) {
832 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
833 } else if (source.IsFpuRegister()) {
834 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
835 } else {
836 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
837 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
838 }
839 } else if (destination.IsFpuRegister()) {
840 if (source.IsRegister()) {
841 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
842 } else if (source.IsFpuRegister()) {
843 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
844 } else {
845 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
846 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
847 }
848 } else {
849 DCHECK(destination.IsStackSlot()) << destination;
850 if (source.IsRegister()) {
851 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
852 } else if (source.IsFpuRegister()) {
853 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
854 } else {
855 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
856 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
857 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
858 }
859 }
860}
861
862void CodeGeneratorMIPS::Move64(Location destination, Location source) {
863 if (source.Equals(destination)) {
864 return;
865 }
866
867 if (destination.IsRegisterPair()) {
868 if (source.IsRegisterPair()) {
869 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
870 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
871 } else if (source.IsFpuRegister()) {
872 Register dst_high = destination.AsRegisterPairHigh<Register>();
873 Register dst_low = destination.AsRegisterPairLow<Register>();
874 FRegister src = source.AsFpuRegister<FRegister>();
875 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800876 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200877 } else {
878 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
879 int32_t off = source.GetStackIndex();
880 Register r = destination.AsRegisterPairLow<Register>();
881 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
882 }
883 } else if (destination.IsFpuRegister()) {
884 if (source.IsRegisterPair()) {
885 FRegister dst = destination.AsFpuRegister<FRegister>();
886 Register src_high = source.AsRegisterPairHigh<Register>();
887 Register src_low = source.AsRegisterPairLow<Register>();
888 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800889 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200890 } else if (source.IsFpuRegister()) {
891 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
892 } else {
893 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
894 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
895 }
896 } else {
897 DCHECK(destination.IsDoubleStackSlot()) << destination;
898 int32_t off = destination.GetStackIndex();
899 if (source.IsRegisterPair()) {
900 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
901 } else if (source.IsFpuRegister()) {
902 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
903 } else {
904 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
905 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
906 __ StoreToOffset(kStoreWord, TMP, SP, off);
907 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
908 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
909 }
910 }
911}
912
913void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
914 if (c->IsIntConstant() || c->IsNullConstant()) {
915 // Move 32 bit constant.
916 int32_t value = GetInt32ValueOf(c);
917 if (destination.IsRegister()) {
918 Register dst = destination.AsRegister<Register>();
919 __ LoadConst32(dst, value);
920 } else {
921 DCHECK(destination.IsStackSlot())
922 << "Cannot move " << c->DebugName() << " to " << destination;
923 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
924 }
925 } else if (c->IsLongConstant()) {
926 // Move 64 bit constant.
927 int64_t value = GetInt64ValueOf(c);
928 if (destination.IsRegisterPair()) {
929 Register r_h = destination.AsRegisterPairHigh<Register>();
930 Register r_l = destination.AsRegisterPairLow<Register>();
931 __ LoadConst64(r_h, r_l, value);
932 } else {
933 DCHECK(destination.IsDoubleStackSlot())
934 << "Cannot move " << c->DebugName() << " to " << destination;
935 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
936 }
937 } else if (c->IsFloatConstant()) {
938 // Move 32 bit float constant.
939 int32_t value = GetInt32ValueOf(c);
940 if (destination.IsFpuRegister()) {
941 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
942 } else {
943 DCHECK(destination.IsStackSlot())
944 << "Cannot move " << c->DebugName() << " to " << destination;
945 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
946 }
947 } else {
948 // Move 64 bit double constant.
949 DCHECK(c->IsDoubleConstant()) << c->DebugName();
950 int64_t value = GetInt64ValueOf(c);
951 if (destination.IsFpuRegister()) {
952 FRegister fd = destination.AsFpuRegister<FRegister>();
953 __ LoadDConst64(fd, value, TMP);
954 } else {
955 DCHECK(destination.IsDoubleStackSlot())
956 << "Cannot move " << c->DebugName() << " to " << destination;
957 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
958 }
959 }
960}
961
962void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
963 DCHECK(destination.IsRegister());
964 Register dst = destination.AsRegister<Register>();
965 __ LoadConst32(dst, value);
966}
967
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200968void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
969 if (location.IsRegister()) {
970 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700971 } else if (location.IsRegisterPair()) {
972 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
973 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200974 } else {
975 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
976 }
977}
978
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700979void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
980 DCHECK(linker_patches->empty());
981 size_t size =
982 method_patches_.size() +
983 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700984 pc_relative_dex_cache_patches_.size() +
985 pc_relative_string_patches_.size() +
986 pc_relative_type_patches_.size() +
987 boot_image_string_patches_.size() +
988 boot_image_type_patches_.size() +
989 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700990 linker_patches->reserve(size);
991 for (const auto& entry : method_patches_) {
992 const MethodReference& target_method = entry.first;
993 Literal* literal = entry.second;
994 DCHECK(literal->GetLabel()->IsBound());
995 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
996 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
997 target_method.dex_file,
998 target_method.dex_method_index));
999 }
1000 for (const auto& entry : call_patches_) {
1001 const MethodReference& target_method = entry.first;
1002 Literal* literal = entry.second;
1003 DCHECK(literal->GetLabel()->IsBound());
1004 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1005 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1006 target_method.dex_file,
1007 target_method.dex_method_index));
1008 }
1009 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
1010 const DexFile& dex_file = info.target_dex_file;
1011 size_t base_element_offset = info.offset_or_index;
1012 DCHECK(info.high_label.IsBound());
1013 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1014 DCHECK(info.pc_rel_label.IsBound());
1015 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1016 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
1017 &dex_file,
1018 pc_rel_offset,
1019 base_element_offset));
1020 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001021 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
1022 const DexFile& dex_file = info.target_dex_file;
1023 size_t string_index = info.offset_or_index;
1024 DCHECK(info.high_label.IsBound());
1025 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1026 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1027 // the assembler's base label used for PC-relative literals.
1028 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1029 ? __ GetLabelLocation(&info.pc_rel_label)
1030 : __ GetPcRelBaseLabelLocation();
1031 linker_patches->push_back(LinkerPatch::RelativeStringPatch(high_offset,
1032 &dex_file,
1033 pc_rel_offset,
1034 string_index));
1035 }
1036 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
1037 const DexFile& dex_file = info.target_dex_file;
1038 size_t type_index = info.offset_or_index;
1039 DCHECK(info.high_label.IsBound());
1040 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1041 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1042 // the assembler's base label used for PC-relative literals.
1043 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1044 ? __ GetLabelLocation(&info.pc_rel_label)
1045 : __ GetPcRelBaseLabelLocation();
1046 linker_patches->push_back(LinkerPatch::RelativeTypePatch(high_offset,
1047 &dex_file,
1048 pc_rel_offset,
1049 type_index));
1050 }
1051 for (const auto& entry : boot_image_string_patches_) {
1052 const StringReference& target_string = entry.first;
1053 Literal* literal = entry.second;
1054 DCHECK(literal->GetLabel()->IsBound());
1055 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1056 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1057 target_string.dex_file,
1058 target_string.string_index));
1059 }
1060 for (const auto& entry : boot_image_type_patches_) {
1061 const TypeReference& target_type = entry.first;
1062 Literal* literal = entry.second;
1063 DCHECK(literal->GetLabel()->IsBound());
1064 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1065 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1066 target_type.dex_file,
1067 target_type.type_index));
1068 }
1069 for (const auto& entry : boot_image_address_patches_) {
1070 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1071 Literal* literal = entry.second;
1072 DCHECK(literal->GetLabel()->IsBound());
1073 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1074 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1075 }
1076}
1077
1078CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1079 const DexFile& dex_file, uint32_t string_index) {
1080 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1081}
1082
1083CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1084 const DexFile& dex_file, uint32_t type_index) {
1085 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001086}
1087
1088CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1089 const DexFile& dex_file, uint32_t element_offset) {
1090 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1091}
1092
1093CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1094 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1095 patches->emplace_back(dex_file, offset_or_index);
1096 return &patches->back();
1097}
1098
Alexey Frunze06a46c42016-07-19 15:00:40 -07001099Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1100 return map->GetOrCreate(
1101 value,
1102 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1103}
1104
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001105Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1106 MethodToLiteralMap* map) {
1107 return map->GetOrCreate(
1108 target_method,
1109 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1110}
1111
1112Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1113 return DeduplicateMethodLiteral(target_method, &method_patches_);
1114}
1115
1116Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1117 return DeduplicateMethodLiteral(target_method, &call_patches_);
1118}
1119
Alexey Frunze06a46c42016-07-19 15:00:40 -07001120Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1121 uint32_t string_index) {
1122 return boot_image_string_patches_.GetOrCreate(
1123 StringReference(&dex_file, string_index),
1124 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1125}
1126
1127Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1128 uint32_t type_index) {
1129 return boot_image_type_patches_.GetOrCreate(
1130 TypeReference(&dex_file, type_index),
1131 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1132}
1133
1134Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1135 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1136 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1137 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1138}
1139
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001140void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1141 MipsLabel done;
1142 Register card = AT;
1143 Register temp = TMP;
1144 __ Beqz(value, &done);
1145 __ LoadFromOffset(kLoadWord,
1146 card,
1147 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001148 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001149 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1150 __ Addu(temp, card, temp);
1151 __ Sb(card, temp, 0);
1152 __ Bind(&done);
1153}
1154
David Brazdil58282f42016-01-14 12:45:10 +00001155void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001156 // Don't allocate the dalvik style register pair passing.
1157 blocked_register_pairs_[A1_A2] = true;
1158
1159 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1160 blocked_core_registers_[ZERO] = true;
1161 blocked_core_registers_[K0] = true;
1162 blocked_core_registers_[K1] = true;
1163 blocked_core_registers_[GP] = true;
1164 blocked_core_registers_[SP] = true;
1165 blocked_core_registers_[RA] = true;
1166
1167 // AT and TMP(T8) are used as temporary/scratch registers
1168 // (similar to how AT is used by MIPS assemblers).
1169 blocked_core_registers_[AT] = true;
1170 blocked_core_registers_[TMP] = true;
1171 blocked_fpu_registers_[FTMP] = true;
1172
1173 // Reserve suspend and thread registers.
1174 blocked_core_registers_[S0] = true;
1175 blocked_core_registers_[TR] = true;
1176
1177 // Reserve T9 for function calls
1178 blocked_core_registers_[T9] = true;
1179
1180 // Reserve odd-numbered FPU registers.
1181 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1182 blocked_fpu_registers_[i] = true;
1183 }
1184
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001185 if (GetGraph()->IsDebuggable()) {
1186 // Stubs do not save callee-save floating point registers. If the graph
1187 // is debuggable, we need to deal with these registers differently. For
1188 // now, just block them.
1189 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1190 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1191 }
1192 }
1193
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001194 UpdateBlockedPairRegisters();
1195}
1196
1197void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1198 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1199 MipsManagedRegister current =
1200 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1201 if (blocked_core_registers_[current.AsRegisterPairLow()]
1202 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1203 blocked_register_pairs_[i] = true;
1204 }
1205 }
1206}
1207
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001208size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1209 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1210 return kMipsWordSize;
1211}
1212
1213size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1214 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1215 return kMipsWordSize;
1216}
1217
1218size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1219 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1220 return kMipsDoublewordSize;
1221}
1222
1223size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1224 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1225 return kMipsDoublewordSize;
1226}
1227
1228void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001229 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001230}
1231
1232void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001233 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001234}
1235
1236void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1237 HInstruction* instruction,
1238 uint32_t dex_pc,
1239 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001240 InvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001241 instruction,
1242 dex_pc,
1243 slow_path,
1244 IsDirectEntrypoint(entrypoint));
1245}
1246
1247constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1248
1249void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1250 HInstruction* instruction,
1251 uint32_t dex_pc,
1252 SlowPathCode* slow_path,
1253 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001254 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1255 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001256 if (is_direct_entrypoint) {
1257 // Reserve argument space on stack (for $a0-$a3) for
1258 // entrypoints that directly reference native implementations.
1259 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001260 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001261 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001262 } else {
1263 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001264 }
1265 RecordPcInfo(instruction, dex_pc, slow_path);
1266}
1267
1268void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1269 Register class_reg) {
1270 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1271 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1272 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1273 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1274 __ Sync(0);
1275 __ Bind(slow_path->GetExitLabel());
1276}
1277
1278void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1279 __ Sync(0); // Only stype 0 is supported.
1280}
1281
1282void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1283 HBasicBlock* successor) {
1284 SuspendCheckSlowPathMIPS* slow_path =
1285 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1286 codegen_->AddSlowPath(slow_path);
1287
1288 __ LoadFromOffset(kLoadUnsignedHalfword,
1289 TMP,
1290 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001291 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001292 if (successor == nullptr) {
1293 __ Bnez(TMP, slow_path->GetEntryLabel());
1294 __ Bind(slow_path->GetReturnLabel());
1295 } else {
1296 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1297 __ B(slow_path->GetEntryLabel());
1298 // slow_path will return to GetLabelOf(successor).
1299 }
1300}
1301
1302InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1303 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001304 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001305 assembler_(codegen->GetAssembler()),
1306 codegen_(codegen) {}
1307
1308void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1309 DCHECK_EQ(instruction->InputCount(), 2U);
1310 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1311 Primitive::Type type = instruction->GetResultType();
1312 switch (type) {
1313 case Primitive::kPrimInt: {
1314 locations->SetInAt(0, Location::RequiresRegister());
1315 HInstruction* right = instruction->InputAt(1);
1316 bool can_use_imm = false;
1317 if (right->IsConstant()) {
1318 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1319 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1320 can_use_imm = IsUint<16>(imm);
1321 } else if (instruction->IsAdd()) {
1322 can_use_imm = IsInt<16>(imm);
1323 } else {
1324 DCHECK(instruction->IsSub());
1325 can_use_imm = IsInt<16>(-imm);
1326 }
1327 }
1328 if (can_use_imm)
1329 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1330 else
1331 locations->SetInAt(1, Location::RequiresRegister());
1332 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1333 break;
1334 }
1335
1336 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001337 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001338 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001340 break;
1341 }
1342
1343 case Primitive::kPrimFloat:
1344 case Primitive::kPrimDouble:
1345 DCHECK(instruction->IsAdd() || instruction->IsSub());
1346 locations->SetInAt(0, Location::RequiresFpuRegister());
1347 locations->SetInAt(1, Location::RequiresFpuRegister());
1348 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1349 break;
1350
1351 default:
1352 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1353 }
1354}
1355
1356void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1357 Primitive::Type type = instruction->GetType();
1358 LocationSummary* locations = instruction->GetLocations();
1359
1360 switch (type) {
1361 case Primitive::kPrimInt: {
1362 Register dst = locations->Out().AsRegister<Register>();
1363 Register lhs = locations->InAt(0).AsRegister<Register>();
1364 Location rhs_location = locations->InAt(1);
1365
1366 Register rhs_reg = ZERO;
1367 int32_t rhs_imm = 0;
1368 bool use_imm = rhs_location.IsConstant();
1369 if (use_imm) {
1370 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1371 } else {
1372 rhs_reg = rhs_location.AsRegister<Register>();
1373 }
1374
1375 if (instruction->IsAnd()) {
1376 if (use_imm)
1377 __ Andi(dst, lhs, rhs_imm);
1378 else
1379 __ And(dst, lhs, rhs_reg);
1380 } else if (instruction->IsOr()) {
1381 if (use_imm)
1382 __ Ori(dst, lhs, rhs_imm);
1383 else
1384 __ Or(dst, lhs, rhs_reg);
1385 } else if (instruction->IsXor()) {
1386 if (use_imm)
1387 __ Xori(dst, lhs, rhs_imm);
1388 else
1389 __ Xor(dst, lhs, rhs_reg);
1390 } else if (instruction->IsAdd()) {
1391 if (use_imm)
1392 __ Addiu(dst, lhs, rhs_imm);
1393 else
1394 __ Addu(dst, lhs, rhs_reg);
1395 } else {
1396 DCHECK(instruction->IsSub());
1397 if (use_imm)
1398 __ Addiu(dst, lhs, -rhs_imm);
1399 else
1400 __ Subu(dst, lhs, rhs_reg);
1401 }
1402 break;
1403 }
1404
1405 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001406 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1407 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1408 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1409 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001410 Location rhs_location = locations->InAt(1);
1411 bool use_imm = rhs_location.IsConstant();
1412 if (!use_imm) {
1413 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1414 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1415 if (instruction->IsAnd()) {
1416 __ And(dst_low, lhs_low, rhs_low);
1417 __ And(dst_high, lhs_high, rhs_high);
1418 } else if (instruction->IsOr()) {
1419 __ Or(dst_low, lhs_low, rhs_low);
1420 __ Or(dst_high, lhs_high, rhs_high);
1421 } else if (instruction->IsXor()) {
1422 __ Xor(dst_low, lhs_low, rhs_low);
1423 __ Xor(dst_high, lhs_high, rhs_high);
1424 } else if (instruction->IsAdd()) {
1425 if (lhs_low == rhs_low) {
1426 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1427 __ Slt(TMP, lhs_low, ZERO);
1428 __ Addu(dst_low, lhs_low, rhs_low);
1429 } else {
1430 __ Addu(dst_low, lhs_low, rhs_low);
1431 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1432 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1433 }
1434 __ Addu(dst_high, lhs_high, rhs_high);
1435 __ Addu(dst_high, dst_high, TMP);
1436 } else {
1437 DCHECK(instruction->IsSub());
1438 __ Sltu(TMP, lhs_low, rhs_low);
1439 __ Subu(dst_low, lhs_low, rhs_low);
1440 __ Subu(dst_high, lhs_high, rhs_high);
1441 __ Subu(dst_high, dst_high, TMP);
1442 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001443 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001444 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1445 if (instruction->IsOr()) {
1446 uint32_t low = Low32Bits(value);
1447 uint32_t high = High32Bits(value);
1448 if (IsUint<16>(low)) {
1449 if (dst_low != lhs_low || low != 0) {
1450 __ Ori(dst_low, lhs_low, low);
1451 }
1452 } else {
1453 __ LoadConst32(TMP, low);
1454 __ Or(dst_low, lhs_low, TMP);
1455 }
1456 if (IsUint<16>(high)) {
1457 if (dst_high != lhs_high || high != 0) {
1458 __ Ori(dst_high, lhs_high, high);
1459 }
1460 } else {
1461 if (high != low) {
1462 __ LoadConst32(TMP, high);
1463 }
1464 __ Or(dst_high, lhs_high, TMP);
1465 }
1466 } else if (instruction->IsXor()) {
1467 uint32_t low = Low32Bits(value);
1468 uint32_t high = High32Bits(value);
1469 if (IsUint<16>(low)) {
1470 if (dst_low != lhs_low || low != 0) {
1471 __ Xori(dst_low, lhs_low, low);
1472 }
1473 } else {
1474 __ LoadConst32(TMP, low);
1475 __ Xor(dst_low, lhs_low, TMP);
1476 }
1477 if (IsUint<16>(high)) {
1478 if (dst_high != lhs_high || high != 0) {
1479 __ Xori(dst_high, lhs_high, high);
1480 }
1481 } else {
1482 if (high != low) {
1483 __ LoadConst32(TMP, high);
1484 }
1485 __ Xor(dst_high, lhs_high, TMP);
1486 }
1487 } else if (instruction->IsAnd()) {
1488 uint32_t low = Low32Bits(value);
1489 uint32_t high = High32Bits(value);
1490 if (IsUint<16>(low)) {
1491 __ Andi(dst_low, lhs_low, low);
1492 } else if (low != 0xFFFFFFFF) {
1493 __ LoadConst32(TMP, low);
1494 __ And(dst_low, lhs_low, TMP);
1495 } else if (dst_low != lhs_low) {
1496 __ Move(dst_low, lhs_low);
1497 }
1498 if (IsUint<16>(high)) {
1499 __ Andi(dst_high, lhs_high, high);
1500 } else if (high != 0xFFFFFFFF) {
1501 if (high != low) {
1502 __ LoadConst32(TMP, high);
1503 }
1504 __ And(dst_high, lhs_high, TMP);
1505 } else if (dst_high != lhs_high) {
1506 __ Move(dst_high, lhs_high);
1507 }
1508 } else {
1509 if (instruction->IsSub()) {
1510 value = -value;
1511 } else {
1512 DCHECK(instruction->IsAdd());
1513 }
1514 int32_t low = Low32Bits(value);
1515 int32_t high = High32Bits(value);
1516 if (IsInt<16>(low)) {
1517 if (dst_low != lhs_low || low != 0) {
1518 __ Addiu(dst_low, lhs_low, low);
1519 }
1520 if (low != 0) {
1521 __ Sltiu(AT, dst_low, low);
1522 }
1523 } else {
1524 __ LoadConst32(TMP, low);
1525 __ Addu(dst_low, lhs_low, TMP);
1526 __ Sltu(AT, dst_low, TMP);
1527 }
1528 if (IsInt<16>(high)) {
1529 if (dst_high != lhs_high || high != 0) {
1530 __ Addiu(dst_high, lhs_high, high);
1531 }
1532 } else {
1533 if (high != low) {
1534 __ LoadConst32(TMP, high);
1535 }
1536 __ Addu(dst_high, lhs_high, TMP);
1537 }
1538 if (low != 0) {
1539 __ Addu(dst_high, dst_high, AT);
1540 }
1541 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001542 }
1543 break;
1544 }
1545
1546 case Primitive::kPrimFloat:
1547 case Primitive::kPrimDouble: {
1548 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1549 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1550 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1551 if (instruction->IsAdd()) {
1552 if (type == Primitive::kPrimFloat) {
1553 __ AddS(dst, lhs, rhs);
1554 } else {
1555 __ AddD(dst, lhs, rhs);
1556 }
1557 } else {
1558 DCHECK(instruction->IsSub());
1559 if (type == Primitive::kPrimFloat) {
1560 __ SubS(dst, lhs, rhs);
1561 } else {
1562 __ SubD(dst, lhs, rhs);
1563 }
1564 }
1565 break;
1566 }
1567
1568 default:
1569 LOG(FATAL) << "Unexpected binary operation type " << type;
1570 }
1571}
1572
1573void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001574 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001575
1576 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1577 Primitive::Type type = instr->GetResultType();
1578 switch (type) {
1579 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001580 locations->SetInAt(0, Location::RequiresRegister());
1581 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1583 break;
1584 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001585 locations->SetInAt(0, Location::RequiresRegister());
1586 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1587 locations->SetOut(Location::RequiresRegister());
1588 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001589 default:
1590 LOG(FATAL) << "Unexpected shift type " << type;
1591 }
1592}
1593
1594static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1595
1596void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001597 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001598 LocationSummary* locations = instr->GetLocations();
1599 Primitive::Type type = instr->GetType();
1600
1601 Location rhs_location = locations->InAt(1);
1602 bool use_imm = rhs_location.IsConstant();
1603 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1604 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001605 const uint32_t shift_mask =
1606 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001607 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001608 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1609 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001610
1611 switch (type) {
1612 case Primitive::kPrimInt: {
1613 Register dst = locations->Out().AsRegister<Register>();
1614 Register lhs = locations->InAt(0).AsRegister<Register>();
1615 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001616 if (shift_value == 0) {
1617 if (dst != lhs) {
1618 __ Move(dst, lhs);
1619 }
1620 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001621 __ Sll(dst, lhs, shift_value);
1622 } else if (instr->IsShr()) {
1623 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001624 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001625 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001626 } else {
1627 if (has_ins_rotr) {
1628 __ Rotr(dst, lhs, shift_value);
1629 } else {
1630 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1631 __ Srl(dst, lhs, shift_value);
1632 __ Or(dst, dst, TMP);
1633 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001634 }
1635 } else {
1636 if (instr->IsShl()) {
1637 __ Sllv(dst, lhs, rhs_reg);
1638 } else if (instr->IsShr()) {
1639 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001640 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001641 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001642 } else {
1643 if (has_ins_rotr) {
1644 __ Rotrv(dst, lhs, rhs_reg);
1645 } else {
1646 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001647 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1648 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1649 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1650 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1651 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001652 __ Sllv(TMP, lhs, TMP);
1653 __ Srlv(dst, lhs, rhs_reg);
1654 __ Or(dst, dst, TMP);
1655 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001656 }
1657 }
1658 break;
1659 }
1660
1661 case Primitive::kPrimLong: {
1662 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1663 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1664 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1665 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1666 if (use_imm) {
1667 if (shift_value == 0) {
1668 codegen_->Move64(locations->Out(), locations->InAt(0));
1669 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001670 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001671 if (instr->IsShl()) {
1672 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1673 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1674 __ Sll(dst_low, lhs_low, shift_value);
1675 } else if (instr->IsShr()) {
1676 __ Srl(dst_low, lhs_low, shift_value);
1677 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1678 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001679 } else if (instr->IsUShr()) {
1680 __ Srl(dst_low, lhs_low, shift_value);
1681 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1682 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001683 } else {
1684 __ Srl(dst_low, lhs_low, shift_value);
1685 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1686 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001687 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001688 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001689 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001690 if (instr->IsShl()) {
1691 __ Sll(dst_low, lhs_low, shift_value);
1692 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1693 __ Sll(dst_high, lhs_high, shift_value);
1694 __ Or(dst_high, dst_high, TMP);
1695 } else if (instr->IsShr()) {
1696 __ Sra(dst_high, lhs_high, shift_value);
1697 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1698 __ Srl(dst_low, lhs_low, shift_value);
1699 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001700 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001701 __ Srl(dst_high, lhs_high, shift_value);
1702 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1703 __ Srl(dst_low, lhs_low, shift_value);
1704 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 } else {
1706 __ Srl(TMP, lhs_low, shift_value);
1707 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1708 __ Or(dst_low, dst_low, TMP);
1709 __ Srl(TMP, lhs_high, shift_value);
1710 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1711 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001712 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001713 }
1714 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001715 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001716 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001717 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 __ Move(dst_low, ZERO);
1719 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001720 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001722 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001723 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001724 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001725 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001726 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001727 // 64-bit rotation by 32 is just a swap.
1728 __ Move(dst_low, lhs_high);
1729 __ Move(dst_high, lhs_low);
1730 } else {
1731 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001732 __ Srl(dst_low, lhs_high, shift_value_high);
1733 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1734 __ Srl(dst_high, lhs_low, shift_value_high);
1735 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001736 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001737 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1738 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001739 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001740 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1741 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001742 __ Or(dst_high, dst_high, TMP);
1743 }
1744 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001745 }
1746 }
1747 } else {
1748 MipsLabel done;
1749 if (instr->IsShl()) {
1750 __ Sllv(dst_low, lhs_low, rhs_reg);
1751 __ Nor(AT, ZERO, rhs_reg);
1752 __ Srl(TMP, lhs_low, 1);
1753 __ Srlv(TMP, TMP, AT);
1754 __ Sllv(dst_high, lhs_high, rhs_reg);
1755 __ Or(dst_high, dst_high, TMP);
1756 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1757 __ Beqz(TMP, &done);
1758 __ Move(dst_high, dst_low);
1759 __ Move(dst_low, ZERO);
1760 } else if (instr->IsShr()) {
1761 __ Srav(dst_high, lhs_high, rhs_reg);
1762 __ Nor(AT, ZERO, rhs_reg);
1763 __ Sll(TMP, lhs_high, 1);
1764 __ Sllv(TMP, TMP, AT);
1765 __ Srlv(dst_low, lhs_low, rhs_reg);
1766 __ Or(dst_low, dst_low, TMP);
1767 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1768 __ Beqz(TMP, &done);
1769 __ Move(dst_low, dst_high);
1770 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001771 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001772 __ Srlv(dst_high, lhs_high, rhs_reg);
1773 __ Nor(AT, ZERO, rhs_reg);
1774 __ Sll(TMP, lhs_high, 1);
1775 __ Sllv(TMP, TMP, AT);
1776 __ Srlv(dst_low, lhs_low, rhs_reg);
1777 __ Or(dst_low, dst_low, TMP);
1778 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1779 __ Beqz(TMP, &done);
1780 __ Move(dst_low, dst_high);
1781 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001782 } else {
1783 __ Nor(AT, ZERO, rhs_reg);
1784 __ Srlv(TMP, lhs_low, rhs_reg);
1785 __ Sll(dst_low, lhs_high, 1);
1786 __ Sllv(dst_low, dst_low, AT);
1787 __ Or(dst_low, dst_low, TMP);
1788 __ Srlv(TMP, lhs_high, rhs_reg);
1789 __ Sll(dst_high, lhs_low, 1);
1790 __ Sllv(dst_high, dst_high, AT);
1791 __ Or(dst_high, dst_high, TMP);
1792 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1793 __ Beqz(TMP, &done);
1794 __ Move(TMP, dst_high);
1795 __ Move(dst_high, dst_low);
1796 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001797 }
1798 __ Bind(&done);
1799 }
1800 break;
1801 }
1802
1803 default:
1804 LOG(FATAL) << "Unexpected shift operation type " << type;
1805 }
1806}
1807
1808void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1809 HandleBinaryOp(instruction);
1810}
1811
1812void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1813 HandleBinaryOp(instruction);
1814}
1815
1816void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1817 HandleBinaryOp(instruction);
1818}
1819
1820void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1821 HandleBinaryOp(instruction);
1822}
1823
1824void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1825 LocationSummary* locations =
1826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1827 locations->SetInAt(0, Location::RequiresRegister());
1828 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1829 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1830 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1831 } else {
1832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1833 }
1834}
1835
1836void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1837 LocationSummary* locations = instruction->GetLocations();
1838 Register obj = locations->InAt(0).AsRegister<Register>();
1839 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001840 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001841
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001842 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001843 switch (type) {
1844 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001845 Register out = locations->Out().AsRegister<Register>();
1846 if (index.IsConstant()) {
1847 size_t offset =
1848 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1849 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1850 } else {
1851 __ Addu(TMP, obj, index.AsRegister<Register>());
1852 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1853 }
1854 break;
1855 }
1856
1857 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 Register out = locations->Out().AsRegister<Register>();
1859 if (index.IsConstant()) {
1860 size_t offset =
1861 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1862 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1863 } else {
1864 __ Addu(TMP, obj, index.AsRegister<Register>());
1865 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 Register out = locations->Out().AsRegister<Register>();
1872 if (index.IsConstant()) {
1873 size_t offset =
1874 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1875 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1876 } else {
1877 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1878 __ Addu(TMP, obj, TMP);
1879 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1880 }
1881 break;
1882 }
1883
1884 case Primitive::kPrimChar: {
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_2) + data_offset;
1889 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1890 } else {
1891 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1892 __ Addu(TMP, obj, TMP);
1893 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1894 }
1895 break;
1896 }
1897
1898 case Primitive::kPrimInt:
1899 case Primitive::kPrimNot: {
1900 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901 Register out = locations->Out().AsRegister<Register>();
1902 if (index.IsConstant()) {
1903 size_t offset =
1904 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1905 __ LoadFromOffset(kLoadWord, out, obj, offset);
1906 } else {
1907 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1908 __ Addu(TMP, obj, TMP);
1909 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1910 }
1911 break;
1912 }
1913
1914 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 Register out = locations->Out().AsRegisterPairLow<Register>();
1916 if (index.IsConstant()) {
1917 size_t offset =
1918 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1919 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1920 } else {
1921 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1922 __ Addu(TMP, obj, TMP);
1923 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1924 }
1925 break;
1926 }
1927
1928 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1930 if (index.IsConstant()) {
1931 size_t offset =
1932 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1933 __ LoadSFromOffset(out, obj, offset);
1934 } else {
1935 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1936 __ Addu(TMP, obj, TMP);
1937 __ LoadSFromOffset(out, TMP, data_offset);
1938 }
1939 break;
1940 }
1941
1942 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001943 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1944 if (index.IsConstant()) {
1945 size_t offset =
1946 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1947 __ LoadDFromOffset(out, obj, offset);
1948 } else {
1949 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1950 __ Addu(TMP, obj, TMP);
1951 __ LoadDFromOffset(out, TMP, data_offset);
1952 }
1953 break;
1954 }
1955
1956 case Primitive::kPrimVoid:
1957 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1958 UNREACHABLE();
1959 }
1960 codegen_->MaybeRecordImplicitNullCheck(instruction);
1961}
1962
1963void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1964 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1965 locations->SetInAt(0, Location::RequiresRegister());
1966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1967}
1968
1969void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1970 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001971 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001972 Register obj = locations->InAt(0).AsRegister<Register>();
1973 Register out = locations->Out().AsRegister<Register>();
1974 __ LoadFromOffset(kLoadWord, out, obj, offset);
1975 codegen_->MaybeRecordImplicitNullCheck(instruction);
1976}
1977
1978void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001979 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001980 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1981 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001982 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001983 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 InvokeRuntimeCallingConvention calling_convention;
1985 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1986 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1987 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1988 } else {
1989 locations->SetInAt(0, Location::RequiresRegister());
1990 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1991 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1992 locations->SetInAt(2, Location::RequiresFpuRegister());
1993 } else {
1994 locations->SetInAt(2, Location::RequiresRegister());
1995 }
1996 }
1997}
1998
1999void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2000 LocationSummary* locations = instruction->GetLocations();
2001 Register obj = locations->InAt(0).AsRegister<Register>();
2002 Location index = locations->InAt(1);
2003 Primitive::Type value_type = instruction->GetComponentType();
2004 bool needs_runtime_call = locations->WillCall();
2005 bool needs_write_barrier =
2006 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2007
2008 switch (value_type) {
2009 case Primitive::kPrimBoolean:
2010 case Primitive::kPrimByte: {
2011 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
2012 Register value = locations->InAt(2).AsRegister<Register>();
2013 if (index.IsConstant()) {
2014 size_t offset =
2015 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
2016 __ StoreToOffset(kStoreByte, value, obj, offset);
2017 } else {
2018 __ Addu(TMP, obj, index.AsRegister<Register>());
2019 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
2020 }
2021 break;
2022 }
2023
2024 case Primitive::kPrimShort:
2025 case Primitive::kPrimChar: {
2026 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
2027 Register value = locations->InAt(2).AsRegister<Register>();
2028 if (index.IsConstant()) {
2029 size_t offset =
2030 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2031 __ StoreToOffset(kStoreHalfword, value, obj, offset);
2032 } else {
2033 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
2034 __ Addu(TMP, obj, TMP);
2035 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
2036 }
2037 break;
2038 }
2039
2040 case Primitive::kPrimInt:
2041 case Primitive::kPrimNot: {
2042 if (!needs_runtime_call) {
2043 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2044 Register value = locations->InAt(2).AsRegister<Register>();
2045 if (index.IsConstant()) {
2046 size_t offset =
2047 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2048 __ StoreToOffset(kStoreWord, value, obj, offset);
2049 } else {
2050 DCHECK(index.IsRegister()) << index;
2051 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2052 __ Addu(TMP, obj, TMP);
2053 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
2054 }
2055 codegen_->MaybeRecordImplicitNullCheck(instruction);
2056 if (needs_write_barrier) {
2057 DCHECK_EQ(value_type, Primitive::kPrimNot);
2058 codegen_->MarkGCCard(obj, value);
2059 }
2060 } else {
2061 DCHECK_EQ(value_type, Primitive::kPrimNot);
2062 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
2063 instruction,
2064 instruction->GetDexPc(),
2065 nullptr,
2066 IsDirectEntrypoint(kQuickAputObject));
2067 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2068 }
2069 break;
2070 }
2071
2072 case Primitive::kPrimLong: {
2073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2074 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2075 if (index.IsConstant()) {
2076 size_t offset =
2077 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2078 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
2079 } else {
2080 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2081 __ Addu(TMP, obj, TMP);
2082 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
2083 }
2084 break;
2085 }
2086
2087 case Primitive::kPrimFloat: {
2088 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2089 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2090 DCHECK(locations->InAt(2).IsFpuRegister());
2091 if (index.IsConstant()) {
2092 size_t offset =
2093 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2094 __ StoreSToOffset(value, obj, offset);
2095 } else {
2096 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2097 __ Addu(TMP, obj, TMP);
2098 __ StoreSToOffset(value, TMP, data_offset);
2099 }
2100 break;
2101 }
2102
2103 case Primitive::kPrimDouble: {
2104 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2105 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2106 DCHECK(locations->InAt(2).IsFpuRegister());
2107 if (index.IsConstant()) {
2108 size_t offset =
2109 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2110 __ StoreDToOffset(value, obj, offset);
2111 } else {
2112 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2113 __ Addu(TMP, obj, TMP);
2114 __ StoreDToOffset(value, TMP, data_offset);
2115 }
2116 break;
2117 }
2118
2119 case Primitive::kPrimVoid:
2120 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2121 UNREACHABLE();
2122 }
2123
2124 // Ints and objects are handled in the switch.
2125 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
2126 codegen_->MaybeRecordImplicitNullCheck(instruction);
2127 }
2128}
2129
2130void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2131 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2132 ? LocationSummary::kCallOnSlowPath
2133 : LocationSummary::kNoCall;
2134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2135 locations->SetInAt(0, Location::RequiresRegister());
2136 locations->SetInAt(1, Location::RequiresRegister());
2137 if (instruction->HasUses()) {
2138 locations->SetOut(Location::SameAsFirstInput());
2139 }
2140}
2141
2142void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2143 LocationSummary* locations = instruction->GetLocations();
2144 BoundsCheckSlowPathMIPS* slow_path =
2145 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2146 codegen_->AddSlowPath(slow_path);
2147
2148 Register index = locations->InAt(0).AsRegister<Register>();
2149 Register length = locations->InAt(1).AsRegister<Register>();
2150
2151 // length is limited by the maximum positive signed 32-bit integer.
2152 // Unsigned comparison of length and index checks for index < 0
2153 // and for length <= index simultaneously.
2154 __ Bgeu(index, length, slow_path->GetEntryLabel());
2155}
2156
2157void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2158 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2159 instruction,
2160 LocationSummary::kCallOnSlowPath);
2161 locations->SetInAt(0, Location::RequiresRegister());
2162 locations->SetInAt(1, Location::RequiresRegister());
2163 // Note that TypeCheckSlowPathMIPS uses this register too.
2164 locations->AddTemp(Location::RequiresRegister());
2165}
2166
2167void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2168 LocationSummary* locations = instruction->GetLocations();
2169 Register obj = locations->InAt(0).AsRegister<Register>();
2170 Register cls = locations->InAt(1).AsRegister<Register>();
2171 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2172
2173 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2174 codegen_->AddSlowPath(slow_path);
2175
2176 // TODO: avoid this check if we know obj is not null.
2177 __ Beqz(obj, slow_path->GetExitLabel());
2178 // Compare the class of `obj` with `cls`.
2179 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2180 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2181 __ Bind(slow_path->GetExitLabel());
2182}
2183
2184void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2185 LocationSummary* locations =
2186 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2187 locations->SetInAt(0, Location::RequiresRegister());
2188 if (check->HasUses()) {
2189 locations->SetOut(Location::SameAsFirstInput());
2190 }
2191}
2192
2193void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2194 // We assume the class is not null.
2195 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2196 check->GetLoadClass(),
2197 check,
2198 check->GetDexPc(),
2199 true);
2200 codegen_->AddSlowPath(slow_path);
2201 GenerateClassInitializationCheck(slow_path,
2202 check->GetLocations()->InAt(0).AsRegister<Register>());
2203}
2204
2205void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2206 Primitive::Type in_type = compare->InputAt(0)->GetType();
2207
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002208 LocationSummary* locations =
2209 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002210
2211 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002212 case Primitive::kPrimBoolean:
2213 case Primitive::kPrimByte:
2214 case Primitive::kPrimShort:
2215 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002216 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002217 case Primitive::kPrimLong:
2218 locations->SetInAt(0, Location::RequiresRegister());
2219 locations->SetInAt(1, Location::RequiresRegister());
2220 // Output overlaps because it is written before doing the low comparison.
2221 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2222 break;
2223
2224 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002225 case Primitive::kPrimDouble:
2226 locations->SetInAt(0, Location::RequiresFpuRegister());
2227 locations->SetInAt(1, Location::RequiresFpuRegister());
2228 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002229 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002230
2231 default:
2232 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2233 }
2234}
2235
2236void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2237 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002238 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002239 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002240 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002241
2242 // 0 if: left == right
2243 // 1 if: left > right
2244 // -1 if: left < right
2245 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002246 case Primitive::kPrimBoolean:
2247 case Primitive::kPrimByte:
2248 case Primitive::kPrimShort:
2249 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002250 case Primitive::kPrimInt: {
2251 Register lhs = locations->InAt(0).AsRegister<Register>();
2252 Register rhs = locations->InAt(1).AsRegister<Register>();
2253 __ Slt(TMP, lhs, rhs);
2254 __ Slt(res, rhs, lhs);
2255 __ Subu(res, res, TMP);
2256 break;
2257 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002258 case Primitive::kPrimLong: {
2259 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2261 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2262 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2263 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2264 // TODO: more efficient (direct) comparison with a constant.
2265 __ Slt(TMP, lhs_high, rhs_high);
2266 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2267 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2268 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2269 __ Sltu(TMP, lhs_low, rhs_low);
2270 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2271 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2272 __ Bind(&done);
2273 break;
2274 }
2275
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002276 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002277 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002278 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2279 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2280 MipsLabel done;
2281 if (isR6) {
2282 __ CmpEqS(FTMP, lhs, rhs);
2283 __ LoadConst32(res, 0);
2284 __ Bc1nez(FTMP, &done);
2285 if (gt_bias) {
2286 __ CmpLtS(FTMP, lhs, rhs);
2287 __ LoadConst32(res, -1);
2288 __ Bc1nez(FTMP, &done);
2289 __ LoadConst32(res, 1);
2290 } else {
2291 __ CmpLtS(FTMP, rhs, lhs);
2292 __ LoadConst32(res, 1);
2293 __ Bc1nez(FTMP, &done);
2294 __ LoadConst32(res, -1);
2295 }
2296 } else {
2297 if (gt_bias) {
2298 __ ColtS(0, lhs, rhs);
2299 __ LoadConst32(res, -1);
2300 __ Bc1t(0, &done);
2301 __ CeqS(0, lhs, rhs);
2302 __ LoadConst32(res, 1);
2303 __ Movt(res, ZERO, 0);
2304 } else {
2305 __ ColtS(0, rhs, lhs);
2306 __ LoadConst32(res, 1);
2307 __ Bc1t(0, &done);
2308 __ CeqS(0, lhs, rhs);
2309 __ LoadConst32(res, -1);
2310 __ Movt(res, ZERO, 0);
2311 }
2312 }
2313 __ Bind(&done);
2314 break;
2315 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002317 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002318 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2319 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2320 MipsLabel done;
2321 if (isR6) {
2322 __ CmpEqD(FTMP, lhs, rhs);
2323 __ LoadConst32(res, 0);
2324 __ Bc1nez(FTMP, &done);
2325 if (gt_bias) {
2326 __ CmpLtD(FTMP, lhs, rhs);
2327 __ LoadConst32(res, -1);
2328 __ Bc1nez(FTMP, &done);
2329 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002330 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002331 __ CmpLtD(FTMP, rhs, lhs);
2332 __ LoadConst32(res, 1);
2333 __ Bc1nez(FTMP, &done);
2334 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002335 }
2336 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002337 if (gt_bias) {
2338 __ ColtD(0, lhs, rhs);
2339 __ LoadConst32(res, -1);
2340 __ Bc1t(0, &done);
2341 __ CeqD(0, lhs, rhs);
2342 __ LoadConst32(res, 1);
2343 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002344 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002345 __ ColtD(0, rhs, lhs);
2346 __ LoadConst32(res, 1);
2347 __ Bc1t(0, &done);
2348 __ CeqD(0, lhs, rhs);
2349 __ LoadConst32(res, -1);
2350 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002351 }
2352 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002353 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002354 break;
2355 }
2356
2357 default:
2358 LOG(FATAL) << "Unimplemented compare type " << in_type;
2359 }
2360}
2361
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002362void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002363 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002364 switch (instruction->InputAt(0)->GetType()) {
2365 default:
2366 case Primitive::kPrimLong:
2367 locations->SetInAt(0, Location::RequiresRegister());
2368 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2369 break;
2370
2371 case Primitive::kPrimFloat:
2372 case Primitive::kPrimDouble:
2373 locations->SetInAt(0, Location::RequiresFpuRegister());
2374 locations->SetInAt(1, Location::RequiresFpuRegister());
2375 break;
2376 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002377 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2379 }
2380}
2381
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002382void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002383 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002384 return;
2385 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002386
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002387 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002388 LocationSummary* locations = instruction->GetLocations();
2389 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002390 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002391
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002392 switch (type) {
2393 default:
2394 // Integer case.
2395 GenerateIntCompare(instruction->GetCondition(), locations);
2396 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002397
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002398 case Primitive::kPrimLong:
2399 // TODO: don't use branches.
2400 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002401 break;
2402
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002403 case Primitive::kPrimFloat:
2404 case Primitive::kPrimDouble:
2405 // TODO: don't use branches.
2406 GenerateFpCompareAndBranch(instruction->GetCondition(),
2407 instruction->IsGtBias(),
2408 type,
2409 locations,
2410 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002411 break;
2412 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002413
2414 // Convert the branches into the result.
2415 MipsLabel done;
2416
2417 // False case: result = 0.
2418 __ LoadConst32(dst, 0);
2419 __ B(&done);
2420
2421 // True case: result = 1.
2422 __ Bind(&true_label);
2423 __ LoadConst32(dst, 1);
2424 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002425}
2426
Alexey Frunze7e99e052015-11-24 19:28:01 -08002427void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2428 DCHECK(instruction->IsDiv() || instruction->IsRem());
2429 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2430
2431 LocationSummary* locations = instruction->GetLocations();
2432 Location second = locations->InAt(1);
2433 DCHECK(second.IsConstant());
2434
2435 Register out = locations->Out().AsRegister<Register>();
2436 Register dividend = locations->InAt(0).AsRegister<Register>();
2437 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2438 DCHECK(imm == 1 || imm == -1);
2439
2440 if (instruction->IsRem()) {
2441 __ Move(out, ZERO);
2442 } else {
2443 if (imm == -1) {
2444 __ Subu(out, ZERO, dividend);
2445 } else if (out != dividend) {
2446 __ Move(out, dividend);
2447 }
2448 }
2449}
2450
2451void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2452 DCHECK(instruction->IsDiv() || instruction->IsRem());
2453 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2454
2455 LocationSummary* locations = instruction->GetLocations();
2456 Location second = locations->InAt(1);
2457 DCHECK(second.IsConstant());
2458
2459 Register out = locations->Out().AsRegister<Register>();
2460 Register dividend = locations->InAt(0).AsRegister<Register>();
2461 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002462 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002463 int ctz_imm = CTZ(abs_imm);
2464
2465 if (instruction->IsDiv()) {
2466 if (ctz_imm == 1) {
2467 // Fast path for division by +/-2, which is very common.
2468 __ Srl(TMP, dividend, 31);
2469 } else {
2470 __ Sra(TMP, dividend, 31);
2471 __ Srl(TMP, TMP, 32 - ctz_imm);
2472 }
2473 __ Addu(out, dividend, TMP);
2474 __ Sra(out, out, ctz_imm);
2475 if (imm < 0) {
2476 __ Subu(out, ZERO, out);
2477 }
2478 } else {
2479 if (ctz_imm == 1) {
2480 // Fast path for modulo +/-2, which is very common.
2481 __ Sra(TMP, dividend, 31);
2482 __ Subu(out, dividend, TMP);
2483 __ Andi(out, out, 1);
2484 __ Addu(out, out, TMP);
2485 } else {
2486 __ Sra(TMP, dividend, 31);
2487 __ Srl(TMP, TMP, 32 - ctz_imm);
2488 __ Addu(out, dividend, TMP);
2489 if (IsUint<16>(abs_imm - 1)) {
2490 __ Andi(out, out, abs_imm - 1);
2491 } else {
2492 __ Sll(out, out, 32 - ctz_imm);
2493 __ Srl(out, out, 32 - ctz_imm);
2494 }
2495 __ Subu(out, out, TMP);
2496 }
2497 }
2498}
2499
2500void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2501 DCHECK(instruction->IsDiv() || instruction->IsRem());
2502 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2503
2504 LocationSummary* locations = instruction->GetLocations();
2505 Location second = locations->InAt(1);
2506 DCHECK(second.IsConstant());
2507
2508 Register out = locations->Out().AsRegister<Register>();
2509 Register dividend = locations->InAt(0).AsRegister<Register>();
2510 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2511
2512 int64_t magic;
2513 int shift;
2514 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2515
2516 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2517
2518 __ LoadConst32(TMP, magic);
2519 if (isR6) {
2520 __ MuhR6(TMP, dividend, TMP);
2521 } else {
2522 __ MultR2(dividend, TMP);
2523 __ Mfhi(TMP);
2524 }
2525 if (imm > 0 && magic < 0) {
2526 __ Addu(TMP, TMP, dividend);
2527 } else if (imm < 0 && magic > 0) {
2528 __ Subu(TMP, TMP, dividend);
2529 }
2530
2531 if (shift != 0) {
2532 __ Sra(TMP, TMP, shift);
2533 }
2534
2535 if (instruction->IsDiv()) {
2536 __ Sra(out, TMP, 31);
2537 __ Subu(out, TMP, out);
2538 } else {
2539 __ Sra(AT, TMP, 31);
2540 __ Subu(AT, TMP, AT);
2541 __ LoadConst32(TMP, imm);
2542 if (isR6) {
2543 __ MulR6(TMP, AT, TMP);
2544 } else {
2545 __ MulR2(TMP, AT, TMP);
2546 }
2547 __ Subu(out, dividend, TMP);
2548 }
2549}
2550
2551void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2552 DCHECK(instruction->IsDiv() || instruction->IsRem());
2553 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2554
2555 LocationSummary* locations = instruction->GetLocations();
2556 Register out = locations->Out().AsRegister<Register>();
2557 Location second = locations->InAt(1);
2558
2559 if (second.IsConstant()) {
2560 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2561 if (imm == 0) {
2562 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2563 } else if (imm == 1 || imm == -1) {
2564 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002565 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002566 DivRemByPowerOfTwo(instruction);
2567 } else {
2568 DCHECK(imm <= -2 || imm >= 2);
2569 GenerateDivRemWithAnyConstant(instruction);
2570 }
2571 } else {
2572 Register dividend = locations->InAt(0).AsRegister<Register>();
2573 Register divisor = second.AsRegister<Register>();
2574 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2575 if (instruction->IsDiv()) {
2576 if (isR6) {
2577 __ DivR6(out, dividend, divisor);
2578 } else {
2579 __ DivR2(out, dividend, divisor);
2580 }
2581 } else {
2582 if (isR6) {
2583 __ ModR6(out, dividend, divisor);
2584 } else {
2585 __ ModR2(out, dividend, divisor);
2586 }
2587 }
2588 }
2589}
2590
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002591void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2592 Primitive::Type type = div->GetResultType();
2593 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002594 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002595 : LocationSummary::kNoCall;
2596
2597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2598
2599 switch (type) {
2600 case Primitive::kPrimInt:
2601 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002602 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2604 break;
2605
2606 case Primitive::kPrimLong: {
2607 InvokeRuntimeCallingConvention calling_convention;
2608 locations->SetInAt(0, Location::RegisterPairLocation(
2609 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2610 locations->SetInAt(1, Location::RegisterPairLocation(
2611 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2612 locations->SetOut(calling_convention.GetReturnLocation(type));
2613 break;
2614 }
2615
2616 case Primitive::kPrimFloat:
2617 case Primitive::kPrimDouble:
2618 locations->SetInAt(0, Location::RequiresFpuRegister());
2619 locations->SetInAt(1, Location::RequiresFpuRegister());
2620 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2621 break;
2622
2623 default:
2624 LOG(FATAL) << "Unexpected div type " << type;
2625 }
2626}
2627
2628void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2629 Primitive::Type type = instruction->GetType();
2630 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002631
2632 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002633 case Primitive::kPrimInt:
2634 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002635 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002636 case Primitive::kPrimLong: {
2637 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2638 instruction,
2639 instruction->GetDexPc(),
2640 nullptr,
2641 IsDirectEntrypoint(kQuickLdiv));
2642 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2643 break;
2644 }
2645 case Primitive::kPrimFloat:
2646 case Primitive::kPrimDouble: {
2647 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2648 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2649 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2650 if (type == Primitive::kPrimFloat) {
2651 __ DivS(dst, lhs, rhs);
2652 } else {
2653 __ DivD(dst, lhs, rhs);
2654 }
2655 break;
2656 }
2657 default:
2658 LOG(FATAL) << "Unexpected div type " << type;
2659 }
2660}
2661
2662void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2663 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2664 ? LocationSummary::kCallOnSlowPath
2665 : LocationSummary::kNoCall;
2666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2667 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2668 if (instruction->HasUses()) {
2669 locations->SetOut(Location::SameAsFirstInput());
2670 }
2671}
2672
2673void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2674 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2675 codegen_->AddSlowPath(slow_path);
2676 Location value = instruction->GetLocations()->InAt(0);
2677 Primitive::Type type = instruction->GetType();
2678
2679 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002680 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002681 case Primitive::kPrimByte:
2682 case Primitive::kPrimChar:
2683 case Primitive::kPrimShort:
2684 case Primitive::kPrimInt: {
2685 if (value.IsConstant()) {
2686 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2687 __ B(slow_path->GetEntryLabel());
2688 } else {
2689 // A division by a non-null constant is valid. We don't need to perform
2690 // any check, so simply fall through.
2691 }
2692 } else {
2693 DCHECK(value.IsRegister()) << value;
2694 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2695 }
2696 break;
2697 }
2698 case Primitive::kPrimLong: {
2699 if (value.IsConstant()) {
2700 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2701 __ B(slow_path->GetEntryLabel());
2702 } else {
2703 // A division by a non-null constant is valid. We don't need to perform
2704 // any check, so simply fall through.
2705 }
2706 } else {
2707 DCHECK(value.IsRegisterPair()) << value;
2708 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2709 __ Beqz(TMP, slow_path->GetEntryLabel());
2710 }
2711 break;
2712 }
2713 default:
2714 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2715 }
2716}
2717
2718void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2719 LocationSummary* locations =
2720 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2721 locations->SetOut(Location::ConstantLocation(constant));
2722}
2723
2724void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2725 // Will be generated at use site.
2726}
2727
2728void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2729 exit->SetLocations(nullptr);
2730}
2731
2732void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2733}
2734
2735void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2736 LocationSummary* locations =
2737 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2738 locations->SetOut(Location::ConstantLocation(constant));
2739}
2740
2741void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2742 // Will be generated at use site.
2743}
2744
2745void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2746 got->SetLocations(nullptr);
2747}
2748
2749void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2750 DCHECK(!successor->IsExitBlock());
2751 HBasicBlock* block = got->GetBlock();
2752 HInstruction* previous = got->GetPrevious();
2753 HLoopInformation* info = block->GetLoopInformation();
2754
2755 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2756 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2757 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2758 return;
2759 }
2760 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2761 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2762 }
2763 if (!codegen_->GoesToNextBlock(block, successor)) {
2764 __ B(codegen_->GetLabelOf(successor));
2765 }
2766}
2767
2768void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2769 HandleGoto(got, got->GetSuccessor());
2770}
2771
2772void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2773 try_boundary->SetLocations(nullptr);
2774}
2775
2776void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2777 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2778 if (!successor->IsExitBlock()) {
2779 HandleGoto(try_boundary, successor);
2780 }
2781}
2782
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002783void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2784 LocationSummary* locations) {
2785 Register dst = locations->Out().AsRegister<Register>();
2786 Register lhs = locations->InAt(0).AsRegister<Register>();
2787 Location rhs_location = locations->InAt(1);
2788 Register rhs_reg = ZERO;
2789 int64_t rhs_imm = 0;
2790 bool use_imm = rhs_location.IsConstant();
2791 if (use_imm) {
2792 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2793 } else {
2794 rhs_reg = rhs_location.AsRegister<Register>();
2795 }
2796
2797 switch (cond) {
2798 case kCondEQ:
2799 case kCondNE:
2800 if (use_imm && IsUint<16>(rhs_imm)) {
2801 __ Xori(dst, lhs, rhs_imm);
2802 } else {
2803 if (use_imm) {
2804 rhs_reg = TMP;
2805 __ LoadConst32(rhs_reg, rhs_imm);
2806 }
2807 __ Xor(dst, lhs, rhs_reg);
2808 }
2809 if (cond == kCondEQ) {
2810 __ Sltiu(dst, dst, 1);
2811 } else {
2812 __ Sltu(dst, ZERO, dst);
2813 }
2814 break;
2815
2816 case kCondLT:
2817 case kCondGE:
2818 if (use_imm && IsInt<16>(rhs_imm)) {
2819 __ Slti(dst, lhs, rhs_imm);
2820 } else {
2821 if (use_imm) {
2822 rhs_reg = TMP;
2823 __ LoadConst32(rhs_reg, rhs_imm);
2824 }
2825 __ Slt(dst, lhs, rhs_reg);
2826 }
2827 if (cond == kCondGE) {
2828 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2829 // only the slt instruction but no sge.
2830 __ Xori(dst, dst, 1);
2831 }
2832 break;
2833
2834 case kCondLE:
2835 case kCondGT:
2836 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2837 // Simulate lhs <= rhs via lhs < rhs + 1.
2838 __ Slti(dst, lhs, rhs_imm + 1);
2839 if (cond == kCondGT) {
2840 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2841 // only the slti instruction but no sgti.
2842 __ Xori(dst, dst, 1);
2843 }
2844 } else {
2845 if (use_imm) {
2846 rhs_reg = TMP;
2847 __ LoadConst32(rhs_reg, rhs_imm);
2848 }
2849 __ Slt(dst, rhs_reg, lhs);
2850 if (cond == kCondLE) {
2851 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2852 // only the slt instruction but no sle.
2853 __ Xori(dst, dst, 1);
2854 }
2855 }
2856 break;
2857
2858 case kCondB:
2859 case kCondAE:
2860 if (use_imm && IsInt<16>(rhs_imm)) {
2861 // Sltiu sign-extends its 16-bit immediate operand before
2862 // the comparison and thus lets us compare directly with
2863 // unsigned values in the ranges [0, 0x7fff] and
2864 // [0xffff8000, 0xffffffff].
2865 __ Sltiu(dst, lhs, rhs_imm);
2866 } else {
2867 if (use_imm) {
2868 rhs_reg = TMP;
2869 __ LoadConst32(rhs_reg, rhs_imm);
2870 }
2871 __ Sltu(dst, lhs, rhs_reg);
2872 }
2873 if (cond == kCondAE) {
2874 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2875 // only the sltu instruction but no sgeu.
2876 __ Xori(dst, dst, 1);
2877 }
2878 break;
2879
2880 case kCondBE:
2881 case kCondA:
2882 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2883 // Simulate lhs <= rhs via lhs < rhs + 1.
2884 // Note that this only works if rhs + 1 does not overflow
2885 // to 0, hence the check above.
2886 // Sltiu sign-extends its 16-bit immediate operand before
2887 // the comparison and thus lets us compare directly with
2888 // unsigned values in the ranges [0, 0x7fff] and
2889 // [0xffff8000, 0xffffffff].
2890 __ Sltiu(dst, lhs, rhs_imm + 1);
2891 if (cond == kCondA) {
2892 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2893 // only the sltiu instruction but no sgtiu.
2894 __ Xori(dst, dst, 1);
2895 }
2896 } else {
2897 if (use_imm) {
2898 rhs_reg = TMP;
2899 __ LoadConst32(rhs_reg, rhs_imm);
2900 }
2901 __ Sltu(dst, rhs_reg, lhs);
2902 if (cond == kCondBE) {
2903 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2904 // only the sltu instruction but no sleu.
2905 __ Xori(dst, dst, 1);
2906 }
2907 }
2908 break;
2909 }
2910}
2911
2912void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2913 LocationSummary* locations,
2914 MipsLabel* label) {
2915 Register lhs = locations->InAt(0).AsRegister<Register>();
2916 Location rhs_location = locations->InAt(1);
2917 Register rhs_reg = ZERO;
2918 int32_t rhs_imm = 0;
2919 bool use_imm = rhs_location.IsConstant();
2920 if (use_imm) {
2921 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2922 } else {
2923 rhs_reg = rhs_location.AsRegister<Register>();
2924 }
2925
2926 if (use_imm && rhs_imm == 0) {
2927 switch (cond) {
2928 case kCondEQ:
2929 case kCondBE: // <= 0 if zero
2930 __ Beqz(lhs, label);
2931 break;
2932 case kCondNE:
2933 case kCondA: // > 0 if non-zero
2934 __ Bnez(lhs, label);
2935 break;
2936 case kCondLT:
2937 __ Bltz(lhs, label);
2938 break;
2939 case kCondGE:
2940 __ Bgez(lhs, label);
2941 break;
2942 case kCondLE:
2943 __ Blez(lhs, label);
2944 break;
2945 case kCondGT:
2946 __ Bgtz(lhs, label);
2947 break;
2948 case kCondB: // always false
2949 break;
2950 case kCondAE: // always true
2951 __ B(label);
2952 break;
2953 }
2954 } else {
2955 if (use_imm) {
2956 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2957 rhs_reg = TMP;
2958 __ LoadConst32(rhs_reg, rhs_imm);
2959 }
2960 switch (cond) {
2961 case kCondEQ:
2962 __ Beq(lhs, rhs_reg, label);
2963 break;
2964 case kCondNE:
2965 __ Bne(lhs, rhs_reg, label);
2966 break;
2967 case kCondLT:
2968 __ Blt(lhs, rhs_reg, label);
2969 break;
2970 case kCondGE:
2971 __ Bge(lhs, rhs_reg, label);
2972 break;
2973 case kCondLE:
2974 __ Bge(rhs_reg, lhs, label);
2975 break;
2976 case kCondGT:
2977 __ Blt(rhs_reg, lhs, label);
2978 break;
2979 case kCondB:
2980 __ Bltu(lhs, rhs_reg, label);
2981 break;
2982 case kCondAE:
2983 __ Bgeu(lhs, rhs_reg, label);
2984 break;
2985 case kCondBE:
2986 __ Bgeu(rhs_reg, lhs, label);
2987 break;
2988 case kCondA:
2989 __ Bltu(rhs_reg, lhs, label);
2990 break;
2991 }
2992 }
2993}
2994
2995void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2996 LocationSummary* locations,
2997 MipsLabel* label) {
2998 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2999 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3000 Location rhs_location = locations->InAt(1);
3001 Register rhs_high = ZERO;
3002 Register rhs_low = ZERO;
3003 int64_t imm = 0;
3004 uint32_t imm_high = 0;
3005 uint32_t imm_low = 0;
3006 bool use_imm = rhs_location.IsConstant();
3007 if (use_imm) {
3008 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3009 imm_high = High32Bits(imm);
3010 imm_low = Low32Bits(imm);
3011 } else {
3012 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3013 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3014 }
3015
3016 if (use_imm && imm == 0) {
3017 switch (cond) {
3018 case kCondEQ:
3019 case kCondBE: // <= 0 if zero
3020 __ Or(TMP, lhs_high, lhs_low);
3021 __ Beqz(TMP, label);
3022 break;
3023 case kCondNE:
3024 case kCondA: // > 0 if non-zero
3025 __ Or(TMP, lhs_high, lhs_low);
3026 __ Bnez(TMP, label);
3027 break;
3028 case kCondLT:
3029 __ Bltz(lhs_high, label);
3030 break;
3031 case kCondGE:
3032 __ Bgez(lhs_high, label);
3033 break;
3034 case kCondLE:
3035 __ Or(TMP, lhs_high, lhs_low);
3036 __ Sra(AT, lhs_high, 31);
3037 __ Bgeu(AT, TMP, label);
3038 break;
3039 case kCondGT:
3040 __ Or(TMP, lhs_high, lhs_low);
3041 __ Sra(AT, lhs_high, 31);
3042 __ Bltu(AT, TMP, label);
3043 break;
3044 case kCondB: // always false
3045 break;
3046 case kCondAE: // always true
3047 __ B(label);
3048 break;
3049 }
3050 } else if (use_imm) {
3051 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3052 switch (cond) {
3053 case kCondEQ:
3054 __ LoadConst32(TMP, imm_high);
3055 __ Xor(TMP, TMP, lhs_high);
3056 __ LoadConst32(AT, imm_low);
3057 __ Xor(AT, AT, lhs_low);
3058 __ Or(TMP, TMP, AT);
3059 __ Beqz(TMP, label);
3060 break;
3061 case kCondNE:
3062 __ LoadConst32(TMP, imm_high);
3063 __ Xor(TMP, TMP, lhs_high);
3064 __ LoadConst32(AT, imm_low);
3065 __ Xor(AT, AT, lhs_low);
3066 __ Or(TMP, TMP, AT);
3067 __ Bnez(TMP, label);
3068 break;
3069 case kCondLT:
3070 __ LoadConst32(TMP, imm_high);
3071 __ Blt(lhs_high, TMP, label);
3072 __ Slt(TMP, TMP, lhs_high);
3073 __ LoadConst32(AT, imm_low);
3074 __ Sltu(AT, lhs_low, AT);
3075 __ Blt(TMP, AT, label);
3076 break;
3077 case kCondGE:
3078 __ LoadConst32(TMP, imm_high);
3079 __ Blt(TMP, lhs_high, label);
3080 __ Slt(TMP, lhs_high, TMP);
3081 __ LoadConst32(AT, imm_low);
3082 __ Sltu(AT, lhs_low, AT);
3083 __ Or(TMP, TMP, AT);
3084 __ Beqz(TMP, label);
3085 break;
3086 case kCondLE:
3087 __ LoadConst32(TMP, imm_high);
3088 __ Blt(lhs_high, TMP, label);
3089 __ Slt(TMP, TMP, lhs_high);
3090 __ LoadConst32(AT, imm_low);
3091 __ Sltu(AT, AT, lhs_low);
3092 __ Or(TMP, TMP, AT);
3093 __ Beqz(TMP, label);
3094 break;
3095 case kCondGT:
3096 __ LoadConst32(TMP, imm_high);
3097 __ Blt(TMP, lhs_high, label);
3098 __ Slt(TMP, lhs_high, TMP);
3099 __ LoadConst32(AT, imm_low);
3100 __ Sltu(AT, AT, lhs_low);
3101 __ Blt(TMP, AT, label);
3102 break;
3103 case kCondB:
3104 __ LoadConst32(TMP, imm_high);
3105 __ Bltu(lhs_high, TMP, label);
3106 __ Sltu(TMP, TMP, lhs_high);
3107 __ LoadConst32(AT, imm_low);
3108 __ Sltu(AT, lhs_low, AT);
3109 __ Blt(TMP, AT, label);
3110 break;
3111 case kCondAE:
3112 __ LoadConst32(TMP, imm_high);
3113 __ Bltu(TMP, lhs_high, label);
3114 __ Sltu(TMP, lhs_high, TMP);
3115 __ LoadConst32(AT, imm_low);
3116 __ Sltu(AT, lhs_low, AT);
3117 __ Or(TMP, TMP, AT);
3118 __ Beqz(TMP, label);
3119 break;
3120 case kCondBE:
3121 __ LoadConst32(TMP, imm_high);
3122 __ Bltu(lhs_high, TMP, label);
3123 __ Sltu(TMP, TMP, lhs_high);
3124 __ LoadConst32(AT, imm_low);
3125 __ Sltu(AT, AT, lhs_low);
3126 __ Or(TMP, TMP, AT);
3127 __ Beqz(TMP, label);
3128 break;
3129 case kCondA:
3130 __ LoadConst32(TMP, imm_high);
3131 __ Bltu(TMP, lhs_high, label);
3132 __ Sltu(TMP, lhs_high, TMP);
3133 __ LoadConst32(AT, imm_low);
3134 __ Sltu(AT, AT, lhs_low);
3135 __ Blt(TMP, AT, label);
3136 break;
3137 }
3138 } else {
3139 switch (cond) {
3140 case kCondEQ:
3141 __ Xor(TMP, lhs_high, rhs_high);
3142 __ Xor(AT, lhs_low, rhs_low);
3143 __ Or(TMP, TMP, AT);
3144 __ Beqz(TMP, label);
3145 break;
3146 case kCondNE:
3147 __ Xor(TMP, lhs_high, rhs_high);
3148 __ Xor(AT, lhs_low, rhs_low);
3149 __ Or(TMP, TMP, AT);
3150 __ Bnez(TMP, label);
3151 break;
3152 case kCondLT:
3153 __ Blt(lhs_high, rhs_high, label);
3154 __ Slt(TMP, rhs_high, lhs_high);
3155 __ Sltu(AT, lhs_low, rhs_low);
3156 __ Blt(TMP, AT, label);
3157 break;
3158 case kCondGE:
3159 __ Blt(rhs_high, lhs_high, label);
3160 __ Slt(TMP, lhs_high, rhs_high);
3161 __ Sltu(AT, lhs_low, rhs_low);
3162 __ Or(TMP, TMP, AT);
3163 __ Beqz(TMP, label);
3164 break;
3165 case kCondLE:
3166 __ Blt(lhs_high, rhs_high, label);
3167 __ Slt(TMP, rhs_high, lhs_high);
3168 __ Sltu(AT, rhs_low, lhs_low);
3169 __ Or(TMP, TMP, AT);
3170 __ Beqz(TMP, label);
3171 break;
3172 case kCondGT:
3173 __ Blt(rhs_high, lhs_high, label);
3174 __ Slt(TMP, lhs_high, rhs_high);
3175 __ Sltu(AT, rhs_low, lhs_low);
3176 __ Blt(TMP, AT, label);
3177 break;
3178 case kCondB:
3179 __ Bltu(lhs_high, rhs_high, label);
3180 __ Sltu(TMP, rhs_high, lhs_high);
3181 __ Sltu(AT, lhs_low, rhs_low);
3182 __ Blt(TMP, AT, label);
3183 break;
3184 case kCondAE:
3185 __ Bltu(rhs_high, lhs_high, label);
3186 __ Sltu(TMP, lhs_high, rhs_high);
3187 __ Sltu(AT, lhs_low, rhs_low);
3188 __ Or(TMP, TMP, AT);
3189 __ Beqz(TMP, label);
3190 break;
3191 case kCondBE:
3192 __ Bltu(lhs_high, rhs_high, label);
3193 __ Sltu(TMP, rhs_high, lhs_high);
3194 __ Sltu(AT, rhs_low, lhs_low);
3195 __ Or(TMP, TMP, AT);
3196 __ Beqz(TMP, label);
3197 break;
3198 case kCondA:
3199 __ Bltu(rhs_high, lhs_high, label);
3200 __ Sltu(TMP, lhs_high, rhs_high);
3201 __ Sltu(AT, rhs_low, lhs_low);
3202 __ Blt(TMP, AT, label);
3203 break;
3204 }
3205 }
3206}
3207
3208void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3209 bool gt_bias,
3210 Primitive::Type type,
3211 LocationSummary* locations,
3212 MipsLabel* label) {
3213 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3214 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3215 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3216 if (type == Primitive::kPrimFloat) {
3217 if (isR6) {
3218 switch (cond) {
3219 case kCondEQ:
3220 __ CmpEqS(FTMP, lhs, rhs);
3221 __ Bc1nez(FTMP, label);
3222 break;
3223 case kCondNE:
3224 __ CmpEqS(FTMP, lhs, rhs);
3225 __ Bc1eqz(FTMP, label);
3226 break;
3227 case kCondLT:
3228 if (gt_bias) {
3229 __ CmpLtS(FTMP, lhs, rhs);
3230 } else {
3231 __ CmpUltS(FTMP, lhs, rhs);
3232 }
3233 __ Bc1nez(FTMP, label);
3234 break;
3235 case kCondLE:
3236 if (gt_bias) {
3237 __ CmpLeS(FTMP, lhs, rhs);
3238 } else {
3239 __ CmpUleS(FTMP, lhs, rhs);
3240 }
3241 __ Bc1nez(FTMP, label);
3242 break;
3243 case kCondGT:
3244 if (gt_bias) {
3245 __ CmpUltS(FTMP, rhs, lhs);
3246 } else {
3247 __ CmpLtS(FTMP, rhs, lhs);
3248 }
3249 __ Bc1nez(FTMP, label);
3250 break;
3251 case kCondGE:
3252 if (gt_bias) {
3253 __ CmpUleS(FTMP, rhs, lhs);
3254 } else {
3255 __ CmpLeS(FTMP, rhs, lhs);
3256 }
3257 __ Bc1nez(FTMP, label);
3258 break;
3259 default:
3260 LOG(FATAL) << "Unexpected non-floating-point condition";
3261 }
3262 } else {
3263 switch (cond) {
3264 case kCondEQ:
3265 __ CeqS(0, lhs, rhs);
3266 __ Bc1t(0, label);
3267 break;
3268 case kCondNE:
3269 __ CeqS(0, lhs, rhs);
3270 __ Bc1f(0, label);
3271 break;
3272 case kCondLT:
3273 if (gt_bias) {
3274 __ ColtS(0, lhs, rhs);
3275 } else {
3276 __ CultS(0, lhs, rhs);
3277 }
3278 __ Bc1t(0, label);
3279 break;
3280 case kCondLE:
3281 if (gt_bias) {
3282 __ ColeS(0, lhs, rhs);
3283 } else {
3284 __ CuleS(0, lhs, rhs);
3285 }
3286 __ Bc1t(0, label);
3287 break;
3288 case kCondGT:
3289 if (gt_bias) {
3290 __ CultS(0, rhs, lhs);
3291 } else {
3292 __ ColtS(0, rhs, lhs);
3293 }
3294 __ Bc1t(0, label);
3295 break;
3296 case kCondGE:
3297 if (gt_bias) {
3298 __ CuleS(0, rhs, lhs);
3299 } else {
3300 __ ColeS(0, rhs, lhs);
3301 }
3302 __ Bc1t(0, label);
3303 break;
3304 default:
3305 LOG(FATAL) << "Unexpected non-floating-point condition";
3306 }
3307 }
3308 } else {
3309 DCHECK_EQ(type, Primitive::kPrimDouble);
3310 if (isR6) {
3311 switch (cond) {
3312 case kCondEQ:
3313 __ CmpEqD(FTMP, lhs, rhs);
3314 __ Bc1nez(FTMP, label);
3315 break;
3316 case kCondNE:
3317 __ CmpEqD(FTMP, lhs, rhs);
3318 __ Bc1eqz(FTMP, label);
3319 break;
3320 case kCondLT:
3321 if (gt_bias) {
3322 __ CmpLtD(FTMP, lhs, rhs);
3323 } else {
3324 __ CmpUltD(FTMP, lhs, rhs);
3325 }
3326 __ Bc1nez(FTMP, label);
3327 break;
3328 case kCondLE:
3329 if (gt_bias) {
3330 __ CmpLeD(FTMP, lhs, rhs);
3331 } else {
3332 __ CmpUleD(FTMP, lhs, rhs);
3333 }
3334 __ Bc1nez(FTMP, label);
3335 break;
3336 case kCondGT:
3337 if (gt_bias) {
3338 __ CmpUltD(FTMP, rhs, lhs);
3339 } else {
3340 __ CmpLtD(FTMP, rhs, lhs);
3341 }
3342 __ Bc1nez(FTMP, label);
3343 break;
3344 case kCondGE:
3345 if (gt_bias) {
3346 __ CmpUleD(FTMP, rhs, lhs);
3347 } else {
3348 __ CmpLeD(FTMP, rhs, lhs);
3349 }
3350 __ Bc1nez(FTMP, label);
3351 break;
3352 default:
3353 LOG(FATAL) << "Unexpected non-floating-point condition";
3354 }
3355 } else {
3356 switch (cond) {
3357 case kCondEQ:
3358 __ CeqD(0, lhs, rhs);
3359 __ Bc1t(0, label);
3360 break;
3361 case kCondNE:
3362 __ CeqD(0, lhs, rhs);
3363 __ Bc1f(0, label);
3364 break;
3365 case kCondLT:
3366 if (gt_bias) {
3367 __ ColtD(0, lhs, rhs);
3368 } else {
3369 __ CultD(0, lhs, rhs);
3370 }
3371 __ Bc1t(0, label);
3372 break;
3373 case kCondLE:
3374 if (gt_bias) {
3375 __ ColeD(0, lhs, rhs);
3376 } else {
3377 __ CuleD(0, lhs, rhs);
3378 }
3379 __ Bc1t(0, label);
3380 break;
3381 case kCondGT:
3382 if (gt_bias) {
3383 __ CultD(0, rhs, lhs);
3384 } else {
3385 __ ColtD(0, rhs, lhs);
3386 }
3387 __ Bc1t(0, label);
3388 break;
3389 case kCondGE:
3390 if (gt_bias) {
3391 __ CuleD(0, rhs, lhs);
3392 } else {
3393 __ ColeD(0, rhs, lhs);
3394 }
3395 __ Bc1t(0, label);
3396 break;
3397 default:
3398 LOG(FATAL) << "Unexpected non-floating-point condition";
3399 }
3400 }
3401 }
3402}
3403
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003404void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003405 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003406 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003407 MipsLabel* false_target) {
3408 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003409
David Brazdil0debae72015-11-12 18:37:00 +00003410 if (true_target == nullptr && false_target == nullptr) {
3411 // Nothing to do. The code always falls through.
3412 return;
3413 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003414 // Constant condition, statically compared against "true" (integer value 1).
3415 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003416 if (true_target != nullptr) {
3417 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003418 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003419 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003420 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003421 if (false_target != nullptr) {
3422 __ B(false_target);
3423 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003424 }
David Brazdil0debae72015-11-12 18:37:00 +00003425 return;
3426 }
3427
3428 // The following code generates these patterns:
3429 // (1) true_target == nullptr && false_target != nullptr
3430 // - opposite condition true => branch to false_target
3431 // (2) true_target != nullptr && false_target == nullptr
3432 // - condition true => branch to true_target
3433 // (3) true_target != nullptr && false_target != nullptr
3434 // - condition true => branch to true_target
3435 // - branch to false_target
3436 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003437 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003438 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003439 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003440 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003441 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3442 } else {
3443 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3444 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003445 } else {
3446 // The condition instruction has not been materialized, use its inputs as
3447 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003448 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003449 Primitive::Type type = condition->InputAt(0)->GetType();
3450 LocationSummary* locations = cond->GetLocations();
3451 IfCondition if_cond = condition->GetCondition();
3452 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003453
David Brazdil0debae72015-11-12 18:37:00 +00003454 if (true_target == nullptr) {
3455 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003456 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003457 }
3458
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003459 switch (type) {
3460 default:
3461 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3462 break;
3463 case Primitive::kPrimLong:
3464 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3465 break;
3466 case Primitive::kPrimFloat:
3467 case Primitive::kPrimDouble:
3468 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3469 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003470 }
3471 }
David Brazdil0debae72015-11-12 18:37:00 +00003472
3473 // If neither branch falls through (case 3), the conditional branch to `true_target`
3474 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3475 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003476 __ B(false_target);
3477 }
3478}
3479
3480void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3481 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003482 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003483 locations->SetInAt(0, Location::RequiresRegister());
3484 }
3485}
3486
3487void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003488 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3489 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3490 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3491 nullptr : codegen_->GetLabelOf(true_successor);
3492 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3493 nullptr : codegen_->GetLabelOf(false_successor);
3494 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003495}
3496
3497void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3498 LocationSummary* locations = new (GetGraph()->GetArena())
3499 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003500 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003501 locations->SetInAt(0, Location::RequiresRegister());
3502 }
3503}
3504
3505void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003506 SlowPathCodeMIPS* slow_path =
3507 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003508 GenerateTestAndBranch(deoptimize,
3509 /* condition_input_index */ 0,
3510 slow_path->GetEntryLabel(),
3511 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003512}
3513
David Brazdil74eb1b22015-12-14 11:44:01 +00003514void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3516 if (Primitive::IsFloatingPointType(select->GetType())) {
3517 locations->SetInAt(0, Location::RequiresFpuRegister());
3518 locations->SetInAt(1, Location::RequiresFpuRegister());
3519 } else {
3520 locations->SetInAt(0, Location::RequiresRegister());
3521 locations->SetInAt(1, Location::RequiresRegister());
3522 }
3523 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3524 locations->SetInAt(2, Location::RequiresRegister());
3525 }
3526 locations->SetOut(Location::SameAsFirstInput());
3527}
3528
3529void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3530 LocationSummary* locations = select->GetLocations();
3531 MipsLabel false_target;
3532 GenerateTestAndBranch(select,
3533 /* condition_input_index */ 2,
3534 /* true_target */ nullptr,
3535 &false_target);
3536 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3537 __ Bind(&false_target);
3538}
3539
David Srbecky0cf44932015-12-09 14:09:59 +00003540void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3541 new (GetGraph()->GetArena()) LocationSummary(info);
3542}
3543
David Srbeckyd28f4a02016-03-14 17:14:24 +00003544void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3545 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003546}
3547
3548void CodeGeneratorMIPS::GenerateNop() {
3549 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003550}
3551
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003552void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3553 Primitive::Type field_type = field_info.GetFieldType();
3554 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3555 bool generate_volatile = field_info.IsVolatile() && is_wide;
3556 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003557 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003558
3559 locations->SetInAt(0, Location::RequiresRegister());
3560 if (generate_volatile) {
3561 InvokeRuntimeCallingConvention calling_convention;
3562 // need A0 to hold base + offset
3563 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3564 if (field_type == Primitive::kPrimLong) {
3565 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3566 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003567 // Use Location::Any() to prevent situations when running out of available fp registers.
3568 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003569 // Need some temp core regs since FP results are returned in core registers
3570 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3571 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3572 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3573 }
3574 } else {
3575 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3576 locations->SetOut(Location::RequiresFpuRegister());
3577 } else {
3578 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3579 }
3580 }
3581}
3582
3583void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3584 const FieldInfo& field_info,
3585 uint32_t dex_pc) {
3586 Primitive::Type type = field_info.GetFieldType();
3587 LocationSummary* locations = instruction->GetLocations();
3588 Register obj = locations->InAt(0).AsRegister<Register>();
3589 LoadOperandType load_type = kLoadUnsignedByte;
3590 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003591 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003592
3593 switch (type) {
3594 case Primitive::kPrimBoolean:
3595 load_type = kLoadUnsignedByte;
3596 break;
3597 case Primitive::kPrimByte:
3598 load_type = kLoadSignedByte;
3599 break;
3600 case Primitive::kPrimShort:
3601 load_type = kLoadSignedHalfword;
3602 break;
3603 case Primitive::kPrimChar:
3604 load_type = kLoadUnsignedHalfword;
3605 break;
3606 case Primitive::kPrimInt:
3607 case Primitive::kPrimFloat:
3608 case Primitive::kPrimNot:
3609 load_type = kLoadWord;
3610 break;
3611 case Primitive::kPrimLong:
3612 case Primitive::kPrimDouble:
3613 load_type = kLoadDoubleword;
3614 break;
3615 case Primitive::kPrimVoid:
3616 LOG(FATAL) << "Unreachable type " << type;
3617 UNREACHABLE();
3618 }
3619
3620 if (is_volatile && load_type == kLoadDoubleword) {
3621 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003622 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003623 // Do implicit Null check
3624 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3625 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3626 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3627 instruction,
3628 dex_pc,
3629 nullptr,
3630 IsDirectEntrypoint(kQuickA64Load));
3631 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3632 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003633 // FP results are returned in core registers. Need to move them.
3634 Location out = locations->Out();
3635 if (out.IsFpuRegister()) {
3636 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3637 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3638 out.AsFpuRegister<FRegister>());
3639 } else {
3640 DCHECK(out.IsDoubleStackSlot());
3641 __ StoreToOffset(kStoreWord,
3642 locations->GetTemp(1).AsRegister<Register>(),
3643 SP,
3644 out.GetStackIndex());
3645 __ StoreToOffset(kStoreWord,
3646 locations->GetTemp(2).AsRegister<Register>(),
3647 SP,
3648 out.GetStackIndex() + 4);
3649 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003650 }
3651 } else {
3652 if (!Primitive::IsFloatingPointType(type)) {
3653 Register dst;
3654 if (type == Primitive::kPrimLong) {
3655 DCHECK(locations->Out().IsRegisterPair());
3656 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003657 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3658 if (obj == dst) {
3659 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3660 codegen_->MaybeRecordImplicitNullCheck(instruction);
3661 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3662 } else {
3663 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3664 codegen_->MaybeRecordImplicitNullCheck(instruction);
3665 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3666 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003667 } else {
3668 DCHECK(locations->Out().IsRegister());
3669 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003670 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003671 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003672 } else {
3673 DCHECK(locations->Out().IsFpuRegister());
3674 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3675 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003676 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003677 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003678 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003679 }
3680 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003681 // Longs are handled earlier.
3682 if (type != Primitive::kPrimLong) {
3683 codegen_->MaybeRecordImplicitNullCheck(instruction);
3684 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003685 }
3686
3687 if (is_volatile) {
3688 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3689 }
3690}
3691
3692void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3693 Primitive::Type field_type = field_info.GetFieldType();
3694 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3695 bool generate_volatile = field_info.IsVolatile() && is_wide;
3696 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003697 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003698
3699 locations->SetInAt(0, Location::RequiresRegister());
3700 if (generate_volatile) {
3701 InvokeRuntimeCallingConvention calling_convention;
3702 // need A0 to hold base + offset
3703 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3704 if (field_type == Primitive::kPrimLong) {
3705 locations->SetInAt(1, Location::RegisterPairLocation(
3706 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3707 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003708 // Use Location::Any() to prevent situations when running out of available fp registers.
3709 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003710 // Pass FP parameters in core registers.
3711 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3712 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3713 }
3714 } else {
3715 if (Primitive::IsFloatingPointType(field_type)) {
3716 locations->SetInAt(1, Location::RequiresFpuRegister());
3717 } else {
3718 locations->SetInAt(1, Location::RequiresRegister());
3719 }
3720 }
3721}
3722
3723void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3724 const FieldInfo& field_info,
3725 uint32_t dex_pc) {
3726 Primitive::Type type = field_info.GetFieldType();
3727 LocationSummary* locations = instruction->GetLocations();
3728 Register obj = locations->InAt(0).AsRegister<Register>();
3729 StoreOperandType store_type = kStoreByte;
3730 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003731 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003732
3733 switch (type) {
3734 case Primitive::kPrimBoolean:
3735 case Primitive::kPrimByte:
3736 store_type = kStoreByte;
3737 break;
3738 case Primitive::kPrimShort:
3739 case Primitive::kPrimChar:
3740 store_type = kStoreHalfword;
3741 break;
3742 case Primitive::kPrimInt:
3743 case Primitive::kPrimFloat:
3744 case Primitive::kPrimNot:
3745 store_type = kStoreWord;
3746 break;
3747 case Primitive::kPrimLong:
3748 case Primitive::kPrimDouble:
3749 store_type = kStoreDoubleword;
3750 break;
3751 case Primitive::kPrimVoid:
3752 LOG(FATAL) << "Unreachable type " << type;
3753 UNREACHABLE();
3754 }
3755
3756 if (is_volatile) {
3757 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3758 }
3759
3760 if (is_volatile && store_type == kStoreDoubleword) {
3761 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003762 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003763 // Do implicit Null check.
3764 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3765 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3766 if (type == Primitive::kPrimDouble) {
3767 // Pass FP parameters in core registers.
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003768 Location in = locations->InAt(1);
3769 if (in.IsFpuRegister()) {
3770 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(), in.AsFpuRegister<FRegister>());
3771 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3772 in.AsFpuRegister<FRegister>());
3773 } else if (in.IsDoubleStackSlot()) {
3774 __ LoadFromOffset(kLoadWord,
3775 locations->GetTemp(1).AsRegister<Register>(),
3776 SP,
3777 in.GetStackIndex());
3778 __ LoadFromOffset(kLoadWord,
3779 locations->GetTemp(2).AsRegister<Register>(),
3780 SP,
3781 in.GetStackIndex() + 4);
3782 } else {
3783 DCHECK(in.IsConstant());
3784 DCHECK(in.GetConstant()->IsDoubleConstant());
3785 int64_t value = bit_cast<int64_t, double>(in.GetConstant()->AsDoubleConstant()->GetValue());
3786 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
3787 locations->GetTemp(1).AsRegister<Register>(),
3788 value);
3789 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003790 }
3791 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3792 instruction,
3793 dex_pc,
3794 nullptr,
3795 IsDirectEntrypoint(kQuickA64Store));
3796 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3797 } else {
3798 if (!Primitive::IsFloatingPointType(type)) {
3799 Register src;
3800 if (type == Primitive::kPrimLong) {
3801 DCHECK(locations->InAt(1).IsRegisterPair());
3802 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003803 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3804 __ StoreToOffset(kStoreWord, src, obj, offset);
3805 codegen_->MaybeRecordImplicitNullCheck(instruction);
3806 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003807 } else {
3808 DCHECK(locations->InAt(1).IsRegister());
3809 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003810 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003811 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003812 } else {
3813 DCHECK(locations->InAt(1).IsFpuRegister());
3814 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3815 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003816 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003817 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003818 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003819 }
3820 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003821 // Longs are handled earlier.
3822 if (type != Primitive::kPrimLong) {
3823 codegen_->MaybeRecordImplicitNullCheck(instruction);
3824 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003825 }
3826
3827 // TODO: memory barriers?
3828 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3829 DCHECK(locations->InAt(1).IsRegister());
3830 Register src = locations->InAt(1).AsRegister<Register>();
3831 codegen_->MarkGCCard(obj, src);
3832 }
3833
3834 if (is_volatile) {
3835 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3836 }
3837}
3838
3839void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3840 HandleFieldGet(instruction, instruction->GetFieldInfo());
3841}
3842
3843void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3844 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3845}
3846
3847void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3848 HandleFieldSet(instruction, instruction->GetFieldInfo());
3849}
3850
3851void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3852 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3853}
3854
Alexey Frunze06a46c42016-07-19 15:00:40 -07003855void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
3856 HInstruction* instruction ATTRIBUTE_UNUSED,
3857 Location root,
3858 Register obj,
3859 uint32_t offset) {
3860 Register root_reg = root.AsRegister<Register>();
3861 if (kEmitCompilerReadBarrier) {
3862 UNIMPLEMENTED(FATAL) << "for read barrier";
3863 } else {
3864 // Plain GC root load with no read barrier.
3865 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3866 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
3867 // Note that GC roots are not affected by heap poisoning, thus we
3868 // do not have to unpoison `root_reg` here.
3869 }
3870}
3871
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003872void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3873 LocationSummary::CallKind call_kind =
3874 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3875 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3876 locations->SetInAt(0, Location::RequiresRegister());
3877 locations->SetInAt(1, Location::RequiresRegister());
3878 // The output does overlap inputs.
3879 // Note that TypeCheckSlowPathMIPS uses this register too.
3880 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3881}
3882
3883void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3884 LocationSummary* locations = instruction->GetLocations();
3885 Register obj = locations->InAt(0).AsRegister<Register>();
3886 Register cls = locations->InAt(1).AsRegister<Register>();
3887 Register out = locations->Out().AsRegister<Register>();
3888
3889 MipsLabel done;
3890
3891 // Return 0 if `obj` is null.
3892 // TODO: Avoid this check if we know `obj` is not null.
3893 __ Move(out, ZERO);
3894 __ Beqz(obj, &done);
3895
3896 // Compare the class of `obj` with `cls`.
3897 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3898 if (instruction->IsExactCheck()) {
3899 // Classes must be equal for the instanceof to succeed.
3900 __ Xor(out, out, cls);
3901 __ Sltiu(out, out, 1);
3902 } else {
3903 // If the classes are not equal, we go into a slow path.
3904 DCHECK(locations->OnlyCallsOnSlowPath());
3905 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3906 codegen_->AddSlowPath(slow_path);
3907 __ Bne(out, cls, slow_path->GetEntryLabel());
3908 __ LoadConst32(out, 1);
3909 __ Bind(slow_path->GetExitLabel());
3910 }
3911
3912 __ Bind(&done);
3913}
3914
3915void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3916 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3917 locations->SetOut(Location::ConstantLocation(constant));
3918}
3919
3920void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3921 // Will be generated at use site.
3922}
3923
3924void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3925 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3926 locations->SetOut(Location::ConstantLocation(constant));
3927}
3928
3929void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3930 // Will be generated at use site.
3931}
3932
3933void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3934 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3935 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3936}
3937
3938void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3939 HandleInvoke(invoke);
3940 // The register T0 is required to be used for the hidden argument in
3941 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3942 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3943}
3944
3945void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3946 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3947 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003948 Location receiver = invoke->GetLocations()->InAt(0);
3949 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003950 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003951
3952 // Set the hidden argument.
3953 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3954 invoke->GetDexMethodIndex());
3955
3956 // temp = object->GetClass();
3957 if (receiver.IsStackSlot()) {
3958 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3959 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3960 } else {
3961 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3962 }
3963 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003964 __ LoadFromOffset(kLoadWord, temp, temp,
3965 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3966 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003967 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003968 // temp = temp->GetImtEntryAt(method_offset);
3969 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3970 // T9 = temp->GetEntryPoint();
3971 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3972 // T9();
3973 __ Jalr(T9);
3974 __ Nop();
3975 DCHECK(!codegen_->IsLeafMethod());
3976 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3977}
3978
3979void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003980 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3981 if (intrinsic.TryDispatch(invoke)) {
3982 return;
3983 }
3984
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003985 HandleInvoke(invoke);
3986}
3987
3988void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003989 // Explicit clinit checks triggered by static invokes must have been pruned by
3990 // art::PrepareForRegisterAllocation.
3991 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003992
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003993 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3994 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3995 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3996
3997 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
3998 // R6 has PC-relative addressing.
3999 bool has_extra_input = !isR6 &&
4000 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4001 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4002
4003 if (invoke->HasPcRelativeDexCache()) {
4004 // kDexCachePcRelative is mutually exclusive with
4005 // kDirectAddressWithFixup/kCallDirectWithFixup.
4006 CHECK(!has_extra_input);
4007 has_extra_input = true;
4008 }
4009
Chris Larsen701566a2015-10-27 15:29:13 -07004010 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4011 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004012 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4013 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4014 }
Chris Larsen701566a2015-10-27 15:29:13 -07004015 return;
4016 }
4017
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004018 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004019
4020 // Add the extra input register if either the dex cache array base register
4021 // or the PC-relative base register for accessing literals is needed.
4022 if (has_extra_input) {
4023 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4024 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004025}
4026
Chris Larsen701566a2015-10-27 15:29:13 -07004027static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004028 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004029 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4030 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004031 return true;
4032 }
4033 return false;
4034}
4035
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004036HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004037 HLoadString::LoadKind desired_string_load_kind) {
4038 if (kEmitCompilerReadBarrier) {
4039 UNIMPLEMENTED(FATAL) << "for read barrier";
4040 }
4041 // We disable PC-relative load when there is an irreducible loop, as the optimization
4042 // is incompatible with it.
4043 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4044 bool fallback_load = has_irreducible_loops;
4045 switch (desired_string_load_kind) {
4046 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4047 DCHECK(!GetCompilerOptions().GetCompilePic());
4048 break;
4049 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4050 DCHECK(GetCompilerOptions().GetCompilePic());
4051 break;
4052 case HLoadString::LoadKind::kBootImageAddress:
4053 break;
4054 case HLoadString::LoadKind::kDexCacheAddress:
4055 DCHECK(Runtime::Current()->UseJitCompilation());
4056 fallback_load = false;
4057 break;
4058 case HLoadString::LoadKind::kDexCachePcRelative:
4059 DCHECK(!Runtime::Current()->UseJitCompilation());
4060 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4061 // with irreducible loops.
4062 break;
4063 case HLoadString::LoadKind::kDexCacheViaMethod:
4064 fallback_load = false;
4065 break;
4066 }
4067 if (fallback_load) {
4068 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4069 }
4070 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004071}
4072
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004073HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4074 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004075 if (kEmitCompilerReadBarrier) {
4076 UNIMPLEMENTED(FATAL) << "for read barrier";
4077 }
4078 // We disable pc-relative load when there is an irreducible loop, as the optimization
4079 // is incompatible with it.
4080 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4081 bool fallback_load = has_irreducible_loops;
4082 switch (desired_class_load_kind) {
4083 case HLoadClass::LoadKind::kReferrersClass:
4084 fallback_load = false;
4085 break;
4086 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4087 DCHECK(!GetCompilerOptions().GetCompilePic());
4088 break;
4089 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4090 DCHECK(GetCompilerOptions().GetCompilePic());
4091 break;
4092 case HLoadClass::LoadKind::kBootImageAddress:
4093 break;
4094 case HLoadClass::LoadKind::kDexCacheAddress:
4095 DCHECK(Runtime::Current()->UseJitCompilation());
4096 fallback_load = false;
4097 break;
4098 case HLoadClass::LoadKind::kDexCachePcRelative:
4099 DCHECK(!Runtime::Current()->UseJitCompilation());
4100 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4101 // with irreducible loops.
4102 break;
4103 case HLoadClass::LoadKind::kDexCacheViaMethod:
4104 fallback_load = false;
4105 break;
4106 }
4107 if (fallback_load) {
4108 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4109 }
4110 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004111}
4112
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004113Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4114 Register temp) {
4115 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4116 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4117 if (!invoke->GetLocations()->Intrinsified()) {
4118 return location.AsRegister<Register>();
4119 }
4120 // For intrinsics we allow any location, so it may be on the stack.
4121 if (!location.IsRegister()) {
4122 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4123 return temp;
4124 }
4125 // For register locations, check if the register was saved. If so, get it from the stack.
4126 // Note: There is a chance that the register was saved but not overwritten, so we could
4127 // save one load. However, since this is just an intrinsic slow path we prefer this
4128 // simple and more robust approach rather that trying to determine if that's the case.
4129 SlowPathCode* slow_path = GetCurrentSlowPath();
4130 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4131 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4132 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4133 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4134 return temp;
4135 }
4136 return location.AsRegister<Register>();
4137}
4138
Vladimir Markodc151b22015-10-15 18:02:30 +01004139HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4140 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4141 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004142 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4143 // We disable PC-relative load when there is an irreducible loop, as the optimization
4144 // is incompatible with it.
4145 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4146 bool fallback_load = true;
4147 bool fallback_call = true;
4148 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004149 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4150 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004151 fallback_load = has_irreducible_loops;
4152 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004153 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004154 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004155 break;
4156 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004157 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004158 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004159 fallback_call = has_irreducible_loops;
4160 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004161 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004162 // TODO: Implement this type.
4163 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004164 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004165 fallback_call = false;
4166 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004167 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004168 if (fallback_load) {
4169 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4170 dispatch_info.method_load_data = 0;
4171 }
4172 if (fallback_call) {
4173 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4174 dispatch_info.direct_code_ptr = 0;
4175 }
4176 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004177}
4178
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004179void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4180 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004181 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004182 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4183 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4184 bool isR6 = isa_features_.IsR6();
4185 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4186 // R6 has PC-relative addressing.
4187 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4188 (!isR6 &&
4189 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4190 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4191 Register base_reg = has_extra_input
4192 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4193 : ZERO;
4194
4195 // For better instruction scheduling we load the direct code pointer before the method pointer.
4196 switch (code_ptr_location) {
4197 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4198 // T9 = invoke->GetDirectCodePtr();
4199 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4200 break;
4201 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4202 // T9 = code address from literal pool with link-time patch.
4203 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4204 break;
4205 default:
4206 break;
4207 }
4208
4209 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004210 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4211 // temp = thread->string_init_entrypoint
4212 __ LoadFromOffset(kLoadWord,
4213 temp.AsRegister<Register>(),
4214 TR,
4215 invoke->GetStringInitOffset());
4216 break;
4217 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004218 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004219 break;
4220 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4221 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4222 break;
4223 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004224 __ LoadLiteral(temp.AsRegister<Register>(),
4225 base_reg,
4226 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4227 break;
4228 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4229 HMipsDexCacheArraysBase* base =
4230 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4231 int32_t offset =
4232 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4233 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4234 break;
4235 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004236 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004237 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004238 Register reg = temp.AsRegister<Register>();
4239 Register method_reg;
4240 if (current_method.IsRegister()) {
4241 method_reg = current_method.AsRegister<Register>();
4242 } else {
4243 // TODO: use the appropriate DCHECK() here if possible.
4244 // DCHECK(invoke->GetLocations()->Intrinsified());
4245 DCHECK(!current_method.IsValid());
4246 method_reg = reg;
4247 __ Lw(reg, SP, kCurrentMethodStackOffset);
4248 }
4249
4250 // temp = temp->dex_cache_resolved_methods_;
4251 __ LoadFromOffset(kLoadWord,
4252 reg,
4253 method_reg,
4254 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004255 // temp = temp[index_in_cache];
4256 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4257 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004258 __ LoadFromOffset(kLoadWord,
4259 reg,
4260 reg,
4261 CodeGenerator::GetCachePointerOffset(index_in_cache));
4262 break;
4263 }
4264 }
4265
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004266 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004267 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004268 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004269 break;
4270 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004271 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4272 // T9 prepared above for better instruction scheduling.
4273 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004274 __ Jalr(T9);
4275 __ Nop();
4276 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004277 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004278 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004279 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4280 LOG(FATAL) << "Unsupported";
4281 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004282 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4283 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004284 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004285 T9,
4286 callee_method.AsRegister<Register>(),
4287 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004288 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004289 // T9()
4290 __ Jalr(T9);
4291 __ Nop();
4292 break;
4293 }
4294 DCHECK(!IsLeafMethod());
4295}
4296
4297void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004298 // Explicit clinit checks triggered by static invokes must have been pruned by
4299 // art::PrepareForRegisterAllocation.
4300 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004301
4302 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4303 return;
4304 }
4305
4306 LocationSummary* locations = invoke->GetLocations();
4307 codegen_->GenerateStaticOrDirectCall(invoke,
4308 locations->HasTemps()
4309 ? locations->GetTemp(0)
4310 : Location::NoLocation());
4311 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4312}
4313
Chris Larsen3acee732015-11-18 13:31:08 -08004314void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004315 LocationSummary* locations = invoke->GetLocations();
4316 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004317 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004318 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4319 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4320 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004321 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004322
4323 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004324 DCHECK(receiver.IsRegister());
4325 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4326 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004327 // temp = temp->GetMethodAt(method_offset);
4328 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4329 // T9 = temp->GetEntryPoint();
4330 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4331 // T9();
4332 __ Jalr(T9);
4333 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08004334}
4335
4336void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4337 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4338 return;
4339 }
4340
4341 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004342 DCHECK(!codegen_->IsLeafMethod());
4343 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4344}
4345
4346void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004347 if (cls->NeedsAccessCheck()) {
4348 InvokeRuntimeCallingConvention calling_convention;
4349 CodeGenerator::CreateLoadClassLocationSummary(
4350 cls,
4351 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4352 Location::RegisterLocation(V0),
4353 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4354 return;
4355 }
4356
4357 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4358 ? LocationSummary::kCallOnSlowPath
4359 : LocationSummary::kNoCall;
4360 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4361 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4362 switch (load_kind) {
4363 // We need an extra register for PC-relative literals on R2.
4364 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4365 case HLoadClass::LoadKind::kBootImageAddress:
4366 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4367 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4368 break;
4369 }
4370 FALLTHROUGH_INTENDED;
4371 // We need an extra register for PC-relative dex cache accesses.
4372 case HLoadClass::LoadKind::kDexCachePcRelative:
4373 case HLoadClass::LoadKind::kReferrersClass:
4374 case HLoadClass::LoadKind::kDexCacheViaMethod:
4375 locations->SetInAt(0, Location::RequiresRegister());
4376 break;
4377 default:
4378 break;
4379 }
4380 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004381}
4382
4383void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4384 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004385 if (cls->NeedsAccessCheck()) {
4386 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4387 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4388 cls,
4389 cls->GetDexPc(),
4390 nullptr,
4391 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004392 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004393 return;
4394 }
4395
Alexey Frunze06a46c42016-07-19 15:00:40 -07004396 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4397 Location out_loc = locations->Out();
4398 Register out = out_loc.AsRegister<Register>();
4399 Register base_or_current_method_reg;
4400 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4401 switch (load_kind) {
4402 // We need an extra register for PC-relative literals on R2.
4403 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4404 case HLoadClass::LoadKind::kBootImageAddress:
4405 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4406 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4407 break;
4408 // We need an extra register for PC-relative dex cache accesses.
4409 case HLoadClass::LoadKind::kDexCachePcRelative:
4410 case HLoadClass::LoadKind::kReferrersClass:
4411 case HLoadClass::LoadKind::kDexCacheViaMethod:
4412 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4413 break;
4414 default:
4415 base_or_current_method_reg = ZERO;
4416 break;
4417 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004418
Alexey Frunze06a46c42016-07-19 15:00:40 -07004419 bool generate_null_check = false;
4420 switch (load_kind) {
4421 case HLoadClass::LoadKind::kReferrersClass: {
4422 DCHECK(!cls->CanCallRuntime());
4423 DCHECK(!cls->MustGenerateClinitCheck());
4424 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4425 GenerateGcRootFieldLoad(cls,
4426 out_loc,
4427 base_or_current_method_reg,
4428 ArtMethod::DeclaringClassOffset().Int32Value());
4429 break;
4430 }
4431 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4432 DCHECK(!kEmitCompilerReadBarrier);
4433 __ LoadLiteral(out,
4434 base_or_current_method_reg,
4435 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4436 cls->GetTypeIndex()));
4437 break;
4438 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4439 DCHECK(!kEmitCompilerReadBarrier);
4440 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4441 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
4442 if (isR6) {
4443 __ Bind(&info->high_label);
4444 __ Bind(&info->pc_rel_label);
4445 // Add a 32-bit offset to PC.
4446 __ Auipc(out, /* placeholder */ 0x1234);
4447 __ Addiu(out, out, /* placeholder */ 0x5678);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004448 } else {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004449 __ Bind(&info->high_label);
4450 __ Lui(out, /* placeholder */ 0x1234);
4451 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4452 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4453 __ Ori(out, out, /* placeholder */ 0x5678);
4454 // Add a 32-bit offset to PC.
4455 __ Addu(out, out, base_or_current_method_reg);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004456 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004457 break;
4458 }
4459 case HLoadClass::LoadKind::kBootImageAddress: {
4460 DCHECK(!kEmitCompilerReadBarrier);
4461 DCHECK_NE(cls->GetAddress(), 0u);
4462 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4463 __ LoadLiteral(out,
4464 base_or_current_method_reg,
4465 codegen_->DeduplicateBootImageAddressLiteral(address));
4466 break;
4467 }
4468 case HLoadClass::LoadKind::kDexCacheAddress: {
4469 DCHECK_NE(cls->GetAddress(), 0u);
4470 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4471 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4472 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4473 int16_t offset = Low16Bits(address);
4474 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4475 __ Lui(out, High16Bits(base_address));
4476 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4477 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4478 generate_null_check = !cls->IsInDexCache();
4479 break;
4480 }
4481 case HLoadClass::LoadKind::kDexCachePcRelative: {
4482 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4483 int32_t offset =
4484 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4485 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4486 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4487 generate_null_check = !cls->IsInDexCache();
4488 break;
4489 }
4490 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4491 // /* GcRoot<mirror::Class>[] */ out =
4492 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4493 __ LoadFromOffset(kLoadWord,
4494 out,
4495 base_or_current_method_reg,
4496 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4497 // /* GcRoot<mirror::Class> */ out = out[type_index]
4498 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4499 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4500 generate_null_check = !cls->IsInDexCache();
4501 }
4502 }
4503
4504 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4505 DCHECK(cls->CanCallRuntime());
4506 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4507 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4508 codegen_->AddSlowPath(slow_path);
4509 if (generate_null_check) {
4510 __ Beqz(out, slow_path->GetEntryLabel());
4511 }
4512 if (cls->MustGenerateClinitCheck()) {
4513 GenerateClassInitializationCheck(slow_path, out);
4514 } else {
4515 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004516 }
4517 }
4518}
4519
4520static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004521 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004522}
4523
4524void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4525 LocationSummary* locations =
4526 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4527 locations->SetOut(Location::RequiresRegister());
4528}
4529
4530void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4531 Register out = load->GetLocations()->Out().AsRegister<Register>();
4532 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4533}
4534
4535void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4536 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4537}
4538
4539void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4540 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4541}
4542
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004543void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004544 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004545 ? LocationSummary::kCallOnSlowPath
4546 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004548 HLoadString::LoadKind load_kind = load->GetLoadKind();
4549 switch (load_kind) {
4550 // We need an extra register for PC-relative literals on R2.
4551 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4552 case HLoadString::LoadKind::kBootImageAddress:
4553 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4554 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4555 break;
4556 }
4557 FALLTHROUGH_INTENDED;
4558 // We need an extra register for PC-relative dex cache accesses.
4559 case HLoadString::LoadKind::kDexCachePcRelative:
4560 case HLoadString::LoadKind::kDexCacheViaMethod:
4561 locations->SetInAt(0, Location::RequiresRegister());
4562 break;
4563 default:
4564 break;
4565 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004566 locations->SetOut(Location::RequiresRegister());
4567}
4568
4569void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004570 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004571 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004572 Location out_loc = locations->Out();
4573 Register out = out_loc.AsRegister<Register>();
4574 Register base_or_current_method_reg;
4575 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4576 switch (load_kind) {
4577 // We need an extra register for PC-relative literals on R2.
4578 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4579 case HLoadString::LoadKind::kBootImageAddress:
4580 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4581 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4582 break;
4583 // We need an extra register for PC-relative dex cache accesses.
4584 case HLoadString::LoadKind::kDexCachePcRelative:
4585 case HLoadString::LoadKind::kDexCacheViaMethod:
4586 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4587 break;
4588 default:
4589 base_or_current_method_reg = ZERO;
4590 break;
4591 }
4592
4593 switch (load_kind) {
4594 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4595 DCHECK(!kEmitCompilerReadBarrier);
4596 __ LoadLiteral(out,
4597 base_or_current_method_reg,
4598 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4599 load->GetStringIndex()));
4600 return; // No dex cache slow path.
4601 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4602 DCHECK(!kEmitCompilerReadBarrier);
4603 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4604 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
4605 if (isR6) {
4606 __ Bind(&info->high_label);
4607 __ Bind(&info->pc_rel_label);
4608 // Add a 32-bit offset to PC.
4609 __ Auipc(out, /* placeholder */ 0x1234);
4610 __ Addiu(out, out, /* placeholder */ 0x5678);
4611 } else {
4612 __ Bind(&info->high_label);
4613 __ Lui(out, /* placeholder */ 0x1234);
4614 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4615 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4616 __ Ori(out, out, /* placeholder */ 0x5678);
4617 // Add a 32-bit offset to PC.
4618 __ Addu(out, out, base_or_current_method_reg);
4619 }
4620 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 }
4631 case HLoadString::LoadKind::kDexCacheAddress: {
4632 DCHECK_NE(load->GetAddress(), 0u);
4633 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4634 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4635 DCHECK_ALIGNED(load->GetAddress(), 4u);
4636 int16_t offset = Low16Bits(address);
4637 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4638 __ Lui(out, High16Bits(base_address));
4639 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
4640 GenerateGcRootFieldLoad(load, out_loc, out, offset);
4641 break;
4642 }
4643 case HLoadString::LoadKind::kDexCachePcRelative: {
4644 HMipsDexCacheArraysBase* base = load->InputAt(0)->AsMipsDexCacheArraysBase();
4645 int32_t offset =
4646 load->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4647 // /* GcRoot<mirror::String> */ out = *(dex_cache_arrays_base + offset)
4648 GenerateGcRootFieldLoad(load, out_loc, base_or_current_method_reg, offset);
4649 break;
4650 }
4651 case HLoadString::LoadKind::kDexCacheViaMethod: {
4652 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4653 GenerateGcRootFieldLoad(load,
4654 out_loc,
4655 base_or_current_method_reg,
4656 ArtMethod::DeclaringClassOffset().Int32Value());
4657 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4658 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4659 // /* GcRoot<mirror::String> */ out = out[string_index]
4660 GenerateGcRootFieldLoad(load,
4661 out_loc,
4662 out,
4663 CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4664 break;
4665 }
4666 default:
4667 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4668 UNREACHABLE();
4669 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004670
4671 if (!load->IsInDexCache()) {
4672 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4673 codegen_->AddSlowPath(slow_path);
4674 __ Beqz(out, slow_path->GetEntryLabel());
4675 __ Bind(slow_path->GetExitLabel());
4676 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004677}
4678
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004679void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4680 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4681 locations->SetOut(Location::ConstantLocation(constant));
4682}
4683
4684void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4685 // Will be generated at use site.
4686}
4687
4688void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4689 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004690 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004691 InvokeRuntimeCallingConvention calling_convention;
4692 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4693}
4694
4695void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4696 if (instruction->IsEnter()) {
4697 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4698 instruction,
4699 instruction->GetDexPc(),
4700 nullptr,
4701 IsDirectEntrypoint(kQuickLockObject));
4702 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4703 } else {
4704 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4705 instruction,
4706 instruction->GetDexPc(),
4707 nullptr,
4708 IsDirectEntrypoint(kQuickUnlockObject));
4709 }
4710 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4711}
4712
4713void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4714 LocationSummary* locations =
4715 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4716 switch (mul->GetResultType()) {
4717 case Primitive::kPrimInt:
4718 case Primitive::kPrimLong:
4719 locations->SetInAt(0, Location::RequiresRegister());
4720 locations->SetInAt(1, Location::RequiresRegister());
4721 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4722 break;
4723
4724 case Primitive::kPrimFloat:
4725 case Primitive::kPrimDouble:
4726 locations->SetInAt(0, Location::RequiresFpuRegister());
4727 locations->SetInAt(1, Location::RequiresFpuRegister());
4728 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4729 break;
4730
4731 default:
4732 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4733 }
4734}
4735
4736void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4737 Primitive::Type type = instruction->GetType();
4738 LocationSummary* locations = instruction->GetLocations();
4739 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4740
4741 switch (type) {
4742 case Primitive::kPrimInt: {
4743 Register dst = locations->Out().AsRegister<Register>();
4744 Register lhs = locations->InAt(0).AsRegister<Register>();
4745 Register rhs = locations->InAt(1).AsRegister<Register>();
4746
4747 if (isR6) {
4748 __ MulR6(dst, lhs, rhs);
4749 } else {
4750 __ MulR2(dst, lhs, rhs);
4751 }
4752 break;
4753 }
4754 case Primitive::kPrimLong: {
4755 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4756 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4757 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4758 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4759 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4760 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4761
4762 // Extra checks to protect caused by the existance of A1_A2.
4763 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4764 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4765 DCHECK_NE(dst_high, lhs_low);
4766 DCHECK_NE(dst_high, rhs_low);
4767
4768 // A_B * C_D
4769 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4770 // dst_lo: [ low(B*D) ]
4771 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4772
4773 if (isR6) {
4774 __ MulR6(TMP, lhs_high, rhs_low);
4775 __ MulR6(dst_high, lhs_low, rhs_high);
4776 __ Addu(dst_high, dst_high, TMP);
4777 __ MuhuR6(TMP, lhs_low, rhs_low);
4778 __ Addu(dst_high, dst_high, TMP);
4779 __ MulR6(dst_low, lhs_low, rhs_low);
4780 } else {
4781 __ MulR2(TMP, lhs_high, rhs_low);
4782 __ MulR2(dst_high, lhs_low, rhs_high);
4783 __ Addu(dst_high, dst_high, TMP);
4784 __ MultuR2(lhs_low, rhs_low);
4785 __ Mfhi(TMP);
4786 __ Addu(dst_high, dst_high, TMP);
4787 __ Mflo(dst_low);
4788 }
4789 break;
4790 }
4791 case Primitive::kPrimFloat:
4792 case Primitive::kPrimDouble: {
4793 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4794 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4795 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4796 if (type == Primitive::kPrimFloat) {
4797 __ MulS(dst, lhs, rhs);
4798 } else {
4799 __ MulD(dst, lhs, rhs);
4800 }
4801 break;
4802 }
4803 default:
4804 LOG(FATAL) << "Unexpected mul type " << type;
4805 }
4806}
4807
4808void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4809 LocationSummary* locations =
4810 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4811 switch (neg->GetResultType()) {
4812 case Primitive::kPrimInt:
4813 case Primitive::kPrimLong:
4814 locations->SetInAt(0, Location::RequiresRegister());
4815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4816 break;
4817
4818 case Primitive::kPrimFloat:
4819 case Primitive::kPrimDouble:
4820 locations->SetInAt(0, Location::RequiresFpuRegister());
4821 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4822 break;
4823
4824 default:
4825 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4826 }
4827}
4828
4829void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4830 Primitive::Type type = instruction->GetType();
4831 LocationSummary* locations = instruction->GetLocations();
4832
4833 switch (type) {
4834 case Primitive::kPrimInt: {
4835 Register dst = locations->Out().AsRegister<Register>();
4836 Register src = locations->InAt(0).AsRegister<Register>();
4837 __ Subu(dst, ZERO, src);
4838 break;
4839 }
4840 case Primitive::kPrimLong: {
4841 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4842 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4843 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4844 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4845 __ Subu(dst_low, ZERO, src_low);
4846 __ Sltu(TMP, ZERO, dst_low);
4847 __ Subu(dst_high, ZERO, src_high);
4848 __ Subu(dst_high, dst_high, TMP);
4849 break;
4850 }
4851 case Primitive::kPrimFloat:
4852 case Primitive::kPrimDouble: {
4853 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4854 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4855 if (type == Primitive::kPrimFloat) {
4856 __ NegS(dst, src);
4857 } else {
4858 __ NegD(dst, src);
4859 }
4860 break;
4861 }
4862 default:
4863 LOG(FATAL) << "Unexpected neg type " << type;
4864 }
4865}
4866
4867void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4868 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004869 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004870 InvokeRuntimeCallingConvention calling_convention;
4871 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4872 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4873 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4874 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4875}
4876
4877void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4878 InvokeRuntimeCallingConvention calling_convention;
4879 Register current_method_register = calling_convention.GetRegisterAt(2);
4880 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4881 // Move an uint16_t value to a register.
4882 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4883 codegen_->InvokeRuntime(
Andreas Gampe542451c2016-07-26 09:02:02 -07004884 GetThreadOffset<kMipsPointerSize>(instruction->GetEntrypoint()).Int32Value(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004885 instruction,
4886 instruction->GetDexPc(),
4887 nullptr,
4888 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4889 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4890 void*, uint32_t, int32_t, ArtMethod*>();
4891}
4892
4893void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4894 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004895 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004896 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004897 if (instruction->IsStringAlloc()) {
4898 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4899 } else {
4900 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4901 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4902 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004903 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4904}
4905
4906void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004907 if (instruction->IsStringAlloc()) {
4908 // String is allocated through StringFactory. Call NewEmptyString entry point.
4909 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004910 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004911 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4912 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4913 __ Jalr(T9);
4914 __ Nop();
4915 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4916 } else {
4917 codegen_->InvokeRuntime(
Andreas Gampe542451c2016-07-26 09:02:02 -07004918 GetThreadOffset<kMipsPointerSize>(instruction->GetEntrypoint()).Int32Value(),
David Brazdil6de19382016-01-08 17:37:10 +00004919 instruction,
4920 instruction->GetDexPc(),
4921 nullptr,
4922 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4923 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4924 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004925}
4926
4927void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4928 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4929 locations->SetInAt(0, Location::RequiresRegister());
4930 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4931}
4932
4933void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4934 Primitive::Type type = instruction->GetType();
4935 LocationSummary* locations = instruction->GetLocations();
4936
4937 switch (type) {
4938 case Primitive::kPrimInt: {
4939 Register dst = locations->Out().AsRegister<Register>();
4940 Register src = locations->InAt(0).AsRegister<Register>();
4941 __ Nor(dst, src, ZERO);
4942 break;
4943 }
4944
4945 case Primitive::kPrimLong: {
4946 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4947 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4948 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4949 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4950 __ Nor(dst_high, src_high, ZERO);
4951 __ Nor(dst_low, src_low, ZERO);
4952 break;
4953 }
4954
4955 default:
4956 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4957 }
4958}
4959
4960void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4961 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4962 locations->SetInAt(0, Location::RequiresRegister());
4963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4964}
4965
4966void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4967 LocationSummary* locations = instruction->GetLocations();
4968 __ Xori(locations->Out().AsRegister<Register>(),
4969 locations->InAt(0).AsRegister<Register>(),
4970 1);
4971}
4972
4973void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4974 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4975 ? LocationSummary::kCallOnSlowPath
4976 : LocationSummary::kNoCall;
4977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4978 locations->SetInAt(0, Location::RequiresRegister());
4979 if (instruction->HasUses()) {
4980 locations->SetOut(Location::SameAsFirstInput());
4981 }
4982}
4983
Calin Juravle2ae48182016-03-16 14:05:09 +00004984void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4985 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004986 return;
4987 }
4988 Location obj = instruction->GetLocations()->InAt(0);
4989
4990 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004991 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004992}
4993
Calin Juravle2ae48182016-03-16 14:05:09 +00004994void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004995 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004996 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004997
4998 Location obj = instruction->GetLocations()->InAt(0);
4999
5000 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5001}
5002
5003void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005004 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005005}
5006
5007void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5008 HandleBinaryOp(instruction);
5009}
5010
5011void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5012 HandleBinaryOp(instruction);
5013}
5014
5015void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5016 LOG(FATAL) << "Unreachable";
5017}
5018
5019void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
5020 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5021}
5022
5023void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
5024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5025 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5026 if (location.IsStackSlot()) {
5027 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5028 } else if (location.IsDoubleStackSlot()) {
5029 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5030 }
5031 locations->SetOut(location);
5032}
5033
5034void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
5035 ATTRIBUTE_UNUSED) {
5036 // Nothing to do, the parameter is already at its location.
5037}
5038
5039void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5040 LocationSummary* locations =
5041 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5042 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5043}
5044
5045void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5046 ATTRIBUTE_UNUSED) {
5047 // Nothing to do, the method is already at its location.
5048}
5049
5050void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5051 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005052 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005053 locations->SetInAt(i, Location::Any());
5054 }
5055 locations->SetOut(Location::Any());
5056}
5057
5058void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5059 LOG(FATAL) << "Unreachable";
5060}
5061
5062void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5063 Primitive::Type type = rem->GetResultType();
5064 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005065 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5067
5068 switch (type) {
5069 case Primitive::kPrimInt:
5070 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005071 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005072 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5073 break;
5074
5075 case Primitive::kPrimLong: {
5076 InvokeRuntimeCallingConvention calling_convention;
5077 locations->SetInAt(0, Location::RegisterPairLocation(
5078 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5079 locations->SetInAt(1, Location::RegisterPairLocation(
5080 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5081 locations->SetOut(calling_convention.GetReturnLocation(type));
5082 break;
5083 }
5084
5085 case Primitive::kPrimFloat:
5086 case Primitive::kPrimDouble: {
5087 InvokeRuntimeCallingConvention calling_convention;
5088 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5089 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5090 locations->SetOut(calling_convention.GetReturnLocation(type));
5091 break;
5092 }
5093
5094 default:
5095 LOG(FATAL) << "Unexpected rem type " << type;
5096 }
5097}
5098
5099void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5100 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005101
5102 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005103 case Primitive::kPrimInt:
5104 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005105 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005106 case Primitive::kPrimLong: {
5107 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
5108 instruction,
5109 instruction->GetDexPc(),
5110 nullptr,
5111 IsDirectEntrypoint(kQuickLmod));
5112 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5113 break;
5114 }
5115 case Primitive::kPrimFloat: {
5116 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
5117 instruction, instruction->GetDexPc(),
5118 nullptr,
5119 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00005120 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005121 break;
5122 }
5123 case Primitive::kPrimDouble: {
5124 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
5125 instruction, instruction->GetDexPc(),
5126 nullptr,
5127 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00005128 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005129 break;
5130 }
5131 default:
5132 LOG(FATAL) << "Unexpected rem type " << type;
5133 }
5134}
5135
5136void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5137 memory_barrier->SetLocations(nullptr);
5138}
5139
5140void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5141 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5142}
5143
5144void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5146 Primitive::Type return_type = ret->InputAt(0)->GetType();
5147 locations->SetInAt(0, MipsReturnLocation(return_type));
5148}
5149
5150void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5151 codegen_->GenerateFrameExit();
5152}
5153
5154void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5155 ret->SetLocations(nullptr);
5156}
5157
5158void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5159 codegen_->GenerateFrameExit();
5160}
5161
Alexey Frunze92d90602015-12-18 18:16:36 -08005162void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5163 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005164}
5165
Alexey Frunze92d90602015-12-18 18:16:36 -08005166void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5167 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005168}
5169
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005170void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5171 HandleShift(shl);
5172}
5173
5174void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5175 HandleShift(shl);
5176}
5177
5178void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5179 HandleShift(shr);
5180}
5181
5182void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5183 HandleShift(shr);
5184}
5185
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005186void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5187 HandleBinaryOp(instruction);
5188}
5189
5190void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5191 HandleBinaryOp(instruction);
5192}
5193
5194void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5195 HandleFieldGet(instruction, instruction->GetFieldInfo());
5196}
5197
5198void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5199 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5200}
5201
5202void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5203 HandleFieldSet(instruction, instruction->GetFieldInfo());
5204}
5205
5206void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5207 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5208}
5209
5210void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5211 HUnresolvedInstanceFieldGet* instruction) {
5212 FieldAccessCallingConventionMIPS calling_convention;
5213 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5214 instruction->GetFieldType(),
5215 calling_convention);
5216}
5217
5218void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5219 HUnresolvedInstanceFieldGet* instruction) {
5220 FieldAccessCallingConventionMIPS calling_convention;
5221 codegen_->GenerateUnresolvedFieldAccess(instruction,
5222 instruction->GetFieldType(),
5223 instruction->GetFieldIndex(),
5224 instruction->GetDexPc(),
5225 calling_convention);
5226}
5227
5228void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5229 HUnresolvedInstanceFieldSet* instruction) {
5230 FieldAccessCallingConventionMIPS calling_convention;
5231 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5232 instruction->GetFieldType(),
5233 calling_convention);
5234}
5235
5236void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5237 HUnresolvedInstanceFieldSet* instruction) {
5238 FieldAccessCallingConventionMIPS calling_convention;
5239 codegen_->GenerateUnresolvedFieldAccess(instruction,
5240 instruction->GetFieldType(),
5241 instruction->GetFieldIndex(),
5242 instruction->GetDexPc(),
5243 calling_convention);
5244}
5245
5246void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5247 HUnresolvedStaticFieldGet* instruction) {
5248 FieldAccessCallingConventionMIPS calling_convention;
5249 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5250 instruction->GetFieldType(),
5251 calling_convention);
5252}
5253
5254void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5255 HUnresolvedStaticFieldGet* instruction) {
5256 FieldAccessCallingConventionMIPS calling_convention;
5257 codegen_->GenerateUnresolvedFieldAccess(instruction,
5258 instruction->GetFieldType(),
5259 instruction->GetFieldIndex(),
5260 instruction->GetDexPc(),
5261 calling_convention);
5262}
5263
5264void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5265 HUnresolvedStaticFieldSet* instruction) {
5266 FieldAccessCallingConventionMIPS calling_convention;
5267 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5268 instruction->GetFieldType(),
5269 calling_convention);
5270}
5271
5272void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5273 HUnresolvedStaticFieldSet* instruction) {
5274 FieldAccessCallingConventionMIPS calling_convention;
5275 codegen_->GenerateUnresolvedFieldAccess(instruction,
5276 instruction->GetFieldType(),
5277 instruction->GetFieldIndex(),
5278 instruction->GetDexPc(),
5279 calling_convention);
5280}
5281
5282void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5283 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5284}
5285
5286void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5287 HBasicBlock* block = instruction->GetBlock();
5288 if (block->GetLoopInformation() != nullptr) {
5289 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5290 // The back edge will generate the suspend check.
5291 return;
5292 }
5293 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5294 // The goto will generate the suspend check.
5295 return;
5296 }
5297 GenerateSuspendCheck(instruction, nullptr);
5298}
5299
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005300void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5301 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005302 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005303 InvokeRuntimeCallingConvention calling_convention;
5304 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5305}
5306
5307void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
5308 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5309 instruction,
5310 instruction->GetDexPc(),
5311 nullptr,
5312 IsDirectEntrypoint(kQuickDeliverException));
5313 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5314}
5315
5316void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5317 Primitive::Type input_type = conversion->GetInputType();
5318 Primitive::Type result_type = conversion->GetResultType();
5319 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005320 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005321
5322 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5323 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5324 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5325 }
5326
5327 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005328 if (!isR6 &&
5329 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5330 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005331 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005332 }
5333
5334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5335
5336 if (call_kind == LocationSummary::kNoCall) {
5337 if (Primitive::IsFloatingPointType(input_type)) {
5338 locations->SetInAt(0, Location::RequiresFpuRegister());
5339 } else {
5340 locations->SetInAt(0, Location::RequiresRegister());
5341 }
5342
5343 if (Primitive::IsFloatingPointType(result_type)) {
5344 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5345 } else {
5346 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5347 }
5348 } else {
5349 InvokeRuntimeCallingConvention calling_convention;
5350
5351 if (Primitive::IsFloatingPointType(input_type)) {
5352 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5353 } else {
5354 DCHECK_EQ(input_type, Primitive::kPrimLong);
5355 locations->SetInAt(0, Location::RegisterPairLocation(
5356 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5357 }
5358
5359 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5360 }
5361}
5362
5363void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5364 LocationSummary* locations = conversion->GetLocations();
5365 Primitive::Type result_type = conversion->GetResultType();
5366 Primitive::Type input_type = conversion->GetInputType();
5367 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005368 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005369
5370 DCHECK_NE(input_type, result_type);
5371
5372 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5373 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5374 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5375 Register src = locations->InAt(0).AsRegister<Register>();
5376
Alexey Frunzea871ef12016-06-27 15:20:11 -07005377 if (dst_low != src) {
5378 __ Move(dst_low, src);
5379 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005380 __ Sra(dst_high, src, 31);
5381 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5382 Register dst = locations->Out().AsRegister<Register>();
5383 Register src = (input_type == Primitive::kPrimLong)
5384 ? locations->InAt(0).AsRegisterPairLow<Register>()
5385 : locations->InAt(0).AsRegister<Register>();
5386
5387 switch (result_type) {
5388 case Primitive::kPrimChar:
5389 __ Andi(dst, src, 0xFFFF);
5390 break;
5391 case Primitive::kPrimByte:
5392 if (has_sign_extension) {
5393 __ Seb(dst, src);
5394 } else {
5395 __ Sll(dst, src, 24);
5396 __ Sra(dst, dst, 24);
5397 }
5398 break;
5399 case Primitive::kPrimShort:
5400 if (has_sign_extension) {
5401 __ Seh(dst, src);
5402 } else {
5403 __ Sll(dst, src, 16);
5404 __ Sra(dst, dst, 16);
5405 }
5406 break;
5407 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005408 if (dst != src) {
5409 __ Move(dst, src);
5410 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005411 break;
5412
5413 default:
5414 LOG(FATAL) << "Unexpected type conversion from " << input_type
5415 << " to " << result_type;
5416 }
5417 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005418 if (input_type == Primitive::kPrimLong) {
5419 if (isR6) {
5420 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5421 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5422 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5423 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5424 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5425 __ Mtc1(src_low, FTMP);
5426 __ Mthc1(src_high, FTMP);
5427 if (result_type == Primitive::kPrimFloat) {
5428 __ Cvtsl(dst, FTMP);
5429 } else {
5430 __ Cvtdl(dst, FTMP);
5431 }
5432 } else {
5433 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
5434 : QUICK_ENTRY_POINT(pL2d);
5435 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
5436 : IsDirectEntrypoint(kQuickL2d);
5437 codegen_->InvokeRuntime(entry_offset,
5438 conversion,
5439 conversion->GetDexPc(),
5440 nullptr,
5441 direct);
5442 if (result_type == Primitive::kPrimFloat) {
5443 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5444 } else {
5445 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5446 }
5447 }
5448 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005449 Register src = locations->InAt(0).AsRegister<Register>();
5450 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5451 __ Mtc1(src, FTMP);
5452 if (result_type == Primitive::kPrimFloat) {
5453 __ Cvtsw(dst, FTMP);
5454 } else {
5455 __ Cvtdw(dst, FTMP);
5456 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005457 }
5458 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5459 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005460 if (result_type == Primitive::kPrimLong) {
5461 if (isR6) {
5462 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5463 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5464 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5465 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5466 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5467 MipsLabel truncate;
5468 MipsLabel done;
5469
5470 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5471 // value when the input is either a NaN or is outside of the range of the output type
5472 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5473 // the same result.
5474 //
5475 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5476 // value of the output type if the input is outside of the range after the truncation or
5477 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5478 // results. This matches the desired float/double-to-int/long conversion exactly.
5479 //
5480 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5481 //
5482 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5483 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5484 // even though it must be NAN2008=1 on R6.
5485 //
5486 // The code takes care of the different behaviors by first comparing the input to the
5487 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5488 // If the input is greater than or equal to the minimum, it procedes to the truncate
5489 // instruction, which will handle such an input the same way irrespective of NAN2008.
5490 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5491 // in order to return either zero or the minimum value.
5492 //
5493 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5494 // truncate instruction for MIPS64R6.
5495 if (input_type == Primitive::kPrimFloat) {
5496 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5497 __ LoadConst32(TMP, min_val);
5498 __ Mtc1(TMP, FTMP);
5499 __ CmpLeS(FTMP, FTMP, src);
5500 } else {
5501 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5502 __ LoadConst32(TMP, High32Bits(min_val));
5503 __ Mtc1(ZERO, FTMP);
5504 __ Mthc1(TMP, FTMP);
5505 __ CmpLeD(FTMP, FTMP, src);
5506 }
5507
5508 __ Bc1nez(FTMP, &truncate);
5509
5510 if (input_type == Primitive::kPrimFloat) {
5511 __ CmpEqS(FTMP, src, src);
5512 } else {
5513 __ CmpEqD(FTMP, src, src);
5514 }
5515 __ Move(dst_low, ZERO);
5516 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5517 __ Mfc1(TMP, FTMP);
5518 __ And(dst_high, dst_high, TMP);
5519
5520 __ B(&done);
5521
5522 __ Bind(&truncate);
5523
5524 if (input_type == Primitive::kPrimFloat) {
5525 __ TruncLS(FTMP, src);
5526 } else {
5527 __ TruncLD(FTMP, src);
5528 }
5529 __ Mfc1(dst_low, FTMP);
5530 __ Mfhc1(dst_high, FTMP);
5531
5532 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005533 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005534 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5535 : QUICK_ENTRY_POINT(pD2l);
5536 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5537 : IsDirectEntrypoint(kQuickD2l);
5538 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5539 if (input_type == Primitive::kPrimFloat) {
5540 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5541 } else {
5542 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5543 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005544 }
5545 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005546 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5547 Register dst = locations->Out().AsRegister<Register>();
5548 MipsLabel truncate;
5549 MipsLabel done;
5550
5551 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5552 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5553 // even though it must be NAN2008=1 on R6.
5554 //
5555 // For details see the large comment above for the truncation of float/double to long on R6.
5556 //
5557 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5558 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005559 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005560 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5561 __ LoadConst32(TMP, min_val);
5562 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005563 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005564 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5565 __ LoadConst32(TMP, High32Bits(min_val));
5566 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005567 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005568 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005569
5570 if (isR6) {
5571 if (input_type == Primitive::kPrimFloat) {
5572 __ CmpLeS(FTMP, FTMP, src);
5573 } else {
5574 __ CmpLeD(FTMP, FTMP, src);
5575 }
5576 __ Bc1nez(FTMP, &truncate);
5577
5578 if (input_type == Primitive::kPrimFloat) {
5579 __ CmpEqS(FTMP, src, src);
5580 } else {
5581 __ CmpEqD(FTMP, src, src);
5582 }
5583 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5584 __ Mfc1(TMP, FTMP);
5585 __ And(dst, dst, TMP);
5586 } else {
5587 if (input_type == Primitive::kPrimFloat) {
5588 __ ColeS(0, FTMP, src);
5589 } else {
5590 __ ColeD(0, FTMP, src);
5591 }
5592 __ Bc1t(0, &truncate);
5593
5594 if (input_type == Primitive::kPrimFloat) {
5595 __ CeqS(0, src, src);
5596 } else {
5597 __ CeqD(0, src, src);
5598 }
5599 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5600 __ Movf(dst, ZERO, 0);
5601 }
5602
5603 __ B(&done);
5604
5605 __ Bind(&truncate);
5606
5607 if (input_type == Primitive::kPrimFloat) {
5608 __ TruncWS(FTMP, src);
5609 } else {
5610 __ TruncWD(FTMP, src);
5611 }
5612 __ Mfc1(dst, FTMP);
5613
5614 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005615 }
5616 } else if (Primitive::IsFloatingPointType(result_type) &&
5617 Primitive::IsFloatingPointType(input_type)) {
5618 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5619 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5620 if (result_type == Primitive::kPrimFloat) {
5621 __ Cvtsd(dst, src);
5622 } else {
5623 __ Cvtds(dst, src);
5624 }
5625 } else {
5626 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5627 << " to " << result_type;
5628 }
5629}
5630
5631void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5632 HandleShift(ushr);
5633}
5634
5635void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5636 HandleShift(ushr);
5637}
5638
5639void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5640 HandleBinaryOp(instruction);
5641}
5642
5643void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5644 HandleBinaryOp(instruction);
5645}
5646
5647void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5648 // Nothing to do, this should be removed during prepare for register allocator.
5649 LOG(FATAL) << "Unreachable";
5650}
5651
5652void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5653 // Nothing to do, this should be removed during prepare for register allocator.
5654 LOG(FATAL) << "Unreachable";
5655}
5656
5657void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005658 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005659}
5660
5661void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005662 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005663}
5664
5665void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005666 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005667}
5668
5669void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005670 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005671}
5672
5673void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005674 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005675}
5676
5677void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005678 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005679}
5680
5681void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005682 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005683}
5684
5685void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005686 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005687}
5688
5689void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005690 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005691}
5692
5693void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005694 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005695}
5696
5697void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005698 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005699}
5700
5701void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005702 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005703}
5704
5705void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005706 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005707}
5708
5709void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005710 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005711}
5712
5713void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005714 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005715}
5716
5717void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005718 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005719}
5720
5721void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005722 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005723}
5724
5725void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005726 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005727}
5728
5729void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005730 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005731}
5732
5733void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005734 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005735}
5736
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005737void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5738 LocationSummary* locations =
5739 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5740 locations->SetInAt(0, Location::RequiresRegister());
5741}
5742
5743void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5744 int32_t lower_bound = switch_instr->GetStartValue();
5745 int32_t num_entries = switch_instr->GetNumEntries();
5746 LocationSummary* locations = switch_instr->GetLocations();
5747 Register value_reg = locations->InAt(0).AsRegister<Register>();
5748 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5749
5750 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005751 Register temp_reg = TMP;
5752 __ Addiu32(temp_reg, value_reg, -lower_bound);
5753 // Jump to default if index is negative
5754 // Note: We don't check the case that index is positive while value < lower_bound, because in
5755 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5756 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5757
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005758 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005759 // Jump to successors[0] if value == lower_bound.
5760 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5761 int32_t last_index = 0;
5762 for (; num_entries - last_index > 2; last_index += 2) {
5763 __ Addiu(temp_reg, temp_reg, -2);
5764 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5765 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5766 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5767 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5768 }
5769 if (num_entries - last_index == 2) {
5770 // The last missing case_value.
5771 __ Addiu(temp_reg, temp_reg, -1);
5772 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005773 }
5774
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005775 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005776 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5777 __ B(codegen_->GetLabelOf(default_block));
5778 }
5779}
5780
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005781void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5782 HMipsComputeBaseMethodAddress* insn) {
5783 LocationSummary* locations =
5784 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5785 locations->SetOut(Location::RequiresRegister());
5786}
5787
5788void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5789 HMipsComputeBaseMethodAddress* insn) {
5790 LocationSummary* locations = insn->GetLocations();
5791 Register reg = locations->Out().AsRegister<Register>();
5792
5793 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5794
5795 // Generate a dummy PC-relative call to obtain PC.
5796 __ Nal();
5797 // Grab the return address off RA.
5798 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005799 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005800
5801 // Remember this offset (the obtained PC value) for later use with constant area.
5802 __ BindPcRelBaseLabel();
5803}
5804
5805void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5806 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5807 locations->SetOut(Location::RequiresRegister());
5808}
5809
5810void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5811 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5812 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5813 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
5814
5815 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5816 __ Bind(&info->high_label);
5817 __ Bind(&info->pc_rel_label);
5818 // Add a 32-bit offset to PC.
5819 __ Auipc(reg, /* placeholder */ 0x1234);
5820 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5821 } else {
5822 // Generate a dummy PC-relative call to obtain PC.
5823 __ Nal();
5824 __ Bind(&info->high_label);
5825 __ Lui(reg, /* placeholder */ 0x1234);
5826 __ Bind(&info->pc_rel_label);
5827 __ Ori(reg, reg, /* placeholder */ 0x5678);
5828 // Add a 32-bit offset to PC.
5829 __ Addu(reg, reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005830 // TODO: Can we share this code with that of VisitMipsComputeBaseMethodAddress()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005831 }
5832}
5833
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005834void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5835 // The trampoline uses the same calling convention as dex calling conventions,
5836 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5837 // the method_idx.
5838 HandleInvoke(invoke);
5839}
5840
5841void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5842 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5843}
5844
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005845void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5846 LocationSummary* locations =
5847 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5848 locations->SetInAt(0, Location::RequiresRegister());
5849 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005850}
5851
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005852void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5853 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005854 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005855 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005856 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005857 __ LoadFromOffset(kLoadWord,
5858 locations->Out().AsRegister<Register>(),
5859 locations->InAt(0).AsRegister<Register>(),
5860 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005861 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005862 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005863 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005864 __ LoadFromOffset(kLoadWord,
5865 locations->Out().AsRegister<Register>(),
5866 locations->InAt(0).AsRegister<Register>(),
5867 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005868 __ LoadFromOffset(kLoadWord,
5869 locations->Out().AsRegister<Register>(),
5870 locations->Out().AsRegister<Register>(),
5871 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005872 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005873}
5874
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005875#undef __
5876#undef QUICK_ENTRY_POINT
5877
5878} // namespace mips
5879} // namespace art