blob: b2e75952a00354bcee7fc5d44c639f2f66f3efaa [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Alexey Frunzee3fb2452016-05-10 16:08:05 -070043// We'll maximize the range of a single load instruction for dex cache array accesses
44// by aligning offset -32768 with the offset of the first used element.
45static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
46
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020047Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
102 if (calling_convention.GetRegisterAt(gp_index) == A1) {
103 gp_index_++; // Skip A1, and use A2_A3 instead.
104 gp_index++;
105 }
106 Register low_even = calling_convention.GetRegisterAt(gp_index);
107 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
108 DCHECK_EQ(low_even + 1, high_odd);
109 next_location = Location::RegisterPairLocation(low_even, high_odd);
110 } else {
111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
112 next_location = Location::DoubleStackSlot(stack_offset);
113 }
114 break;
115 }
116
117 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
118 // will take up the even/odd pair, while floats are stored in even regs only.
119 // On 64 bit FPU, both double and float are stored in even registers only.
120 case Primitive::kPrimFloat:
121 case Primitive::kPrimDouble: {
122 uint32_t float_index = float_index_++;
123 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
124 next_location = Location::FpuRegisterLocation(
125 calling_convention.GetFpuRegisterAt(float_index));
126 } else {
127 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
128 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
129 : Location::StackSlot(stack_offset);
130 }
131 break;
132 }
133
134 case Primitive::kPrimVoid:
135 LOG(FATAL) << "Unexpected parameter type " << type;
136 break;
137 }
138
139 // Space on the stack is reserved for all arguments.
140 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
141
142 return next_location;
143}
144
145Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
146 return MipsReturnLocation(type);
147}
148
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100149// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
150#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700151#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200152
153class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
154 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000155 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200156
157 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
158 LocationSummary* locations = instruction_->GetLocations();
159 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
160 __ Bind(GetEntryLabel());
161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
167 InvokeRuntimeCallingConvention calling_convention;
168 codegen->EmitParallelMoves(locations->InAt(0),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
170 Primitive::kPrimInt,
171 locations->InAt(1),
172 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
173 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100174 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
175 ? kQuickThrowStringBounds
176 : kQuickThrowArrayBounds;
177 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100178 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200179 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
180 }
181
182 bool IsFatal() const OVERRIDE { return true; }
183
184 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
185
186 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
188};
189
190class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
191 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000192 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193
194 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
196 __ Bind(GetEntryLabel());
197 if (instruction_->CanThrowIntoCatchBlock()) {
198 // Live registers will be restored in the catch block if caught.
199 SaveLiveRegisters(codegen, instruction_->GetLocations());
200 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100201 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200202 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
203 }
204
205 bool IsFatal() const OVERRIDE { return true; }
206
207 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
208
209 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200210 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
211};
212
213class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
214 public:
215 LoadClassSlowPathMIPS(HLoadClass* cls,
216 HInstruction* at,
217 uint32_t dex_pc,
218 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000219 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200220 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
221 }
222
223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
224 LocationSummary* locations = at_->GetLocations();
225 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
226
227 __ Bind(GetEntryLabel());
228 SaveLiveRegisters(codegen, locations);
229
230 InvokeRuntimeCallingConvention calling_convention;
231 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
232
Serban Constantinescufca16662016-07-14 09:21:59 +0100233 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
234 : kQuickInitializeType;
235 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200236 if (do_clinit_) {
237 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
238 } else {
239 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
240 }
241
242 // Move the class to the desired location.
243 Location out = locations->Out();
244 if (out.IsValid()) {
245 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
246 Primitive::Type type = at_->GetType();
247 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
248 }
249
250 RestoreLiveRegisters(codegen, locations);
251 __ B(GetExitLabel());
252 }
253
254 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
255
256 private:
257 // The class this slow path will load.
258 HLoadClass* const cls_;
259
260 // The instruction where this slow path is happening.
261 // (Might be the load class or an initialization check).
262 HInstruction* const at_;
263
264 // The dex PC of `at_`.
265 const uint32_t dex_pc_;
266
267 // Whether to initialize the class.
268 const bool do_clinit_;
269
270 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
271};
272
273class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
274 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000275 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200276
277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
278 LocationSummary* locations = instruction_->GetLocations();
279 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
280 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
281
282 __ Bind(GetEntryLabel());
283 SaveLiveRegisters(codegen, locations);
284
285 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
287 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufca16662016-07-14 09:21:59 +0100288 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
290 Primitive::Type type = instruction_->GetType();
291 mips_codegen->MoveLocation(locations->Out(),
292 calling_convention.GetReturnLocation(type),
293 type);
294
295 RestoreLiveRegisters(codegen, locations);
296 __ B(GetExitLabel());
297 }
298
299 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
300
301 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200302 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
303};
304
305class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
306 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000307 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200308
309 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
310 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
311 __ Bind(GetEntryLabel());
312 if (instruction_->CanThrowIntoCatchBlock()) {
313 // Live registers will be restored in the catch block if caught.
314 SaveLiveRegisters(codegen, instruction_->GetLocations());
315 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100316 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200317 instruction_,
318 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100319 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200320 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
321 }
322
323 bool IsFatal() const OVERRIDE { return true; }
324
325 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
326
327 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200328 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
329};
330
331class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
332 public:
333 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000334 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335
336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
337 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
338 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100339 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200340 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 if (successor_ == nullptr) {
342 __ B(GetReturnLabel());
343 } else {
344 __ B(mips_codegen->GetLabelOf(successor_));
345 }
346 }
347
348 MipsLabel* GetReturnLabel() {
349 DCHECK(successor_ == nullptr);
350 return &return_label_;
351 }
352
353 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
354
355 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200356 // If not null, the block to branch to after the suspend check.
357 HBasicBlock* const successor_;
358
359 // If `successor_` is null, the label to branch to after the suspend check.
360 MipsLabel return_label_;
361
362 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
363};
364
365class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
366 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000367 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200368
369 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
370 LocationSummary* locations = instruction_->GetLocations();
371 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
372 uint32_t dex_pc = instruction_->GetDexPc();
373 DCHECK(instruction_->IsCheckCast()
374 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
375 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
376
377 __ Bind(GetEntryLabel());
378 SaveLiveRegisters(codegen, locations);
379
380 // We're moving two locations to locations that could overlap, so we need a parallel
381 // move resolver.
382 InvokeRuntimeCallingConvention calling_convention;
383 codegen->EmitParallelMoves(locations->InAt(1),
384 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
385 Primitive::kPrimNot,
386 object_class,
387 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
388 Primitive::kPrimNot);
389
390 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100391 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000392 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700393 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200394 Primitive::Type ret_type = instruction_->GetType();
395 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
396 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200397 } else {
398 DCHECK(instruction_->IsCheckCast());
Serban Constantinescufca16662016-07-14 09:21:59 +0100399 mips_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200400 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
401 }
402
403 RestoreLiveRegisters(codegen, locations);
404 __ B(GetExitLabel());
405 }
406
407 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
408
409 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
411};
412
413class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
414 public:
Aart Bik42249c32016-01-07 15:33:50 -0800415 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000416 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417
418 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800419 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100421 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000422 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 }
424
425 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
426
427 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200428 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
429};
430
431CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
432 const MipsInstructionSetFeatures& isa_features,
433 const CompilerOptions& compiler_options,
434 OptimizingCompilerStats* stats)
435 : CodeGenerator(graph,
436 kNumberOfCoreRegisters,
437 kNumberOfFRegisters,
438 kNumberOfRegisterPairs,
439 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
440 arraysize(kCoreCalleeSaves)),
441 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
442 arraysize(kFpuCalleeSaves)),
443 compiler_options,
444 stats),
445 block_labels_(nullptr),
446 location_builder_(graph, this),
447 instruction_visitor_(graph, this),
448 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100449 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700450 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700451 uint32_literals_(std::less<uint32_t>(),
452 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700453 method_patches_(MethodReferenceComparator(),
454 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
455 call_patches_(MethodReferenceComparator(),
456 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700457 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
458 boot_image_string_patches_(StringReferenceValueComparator(),
459 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
460 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
461 boot_image_type_patches_(TypeReferenceValueComparator(),
462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
463 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
464 boot_image_address_patches_(std::less<uint32_t>(),
465 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
466 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200467 // Save RA (containing the return address) to mimic Quick.
468 AddAllocatedRegister(Location::RegisterLocation(RA));
469}
470
471#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100472// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
473#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700474#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200475
476void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
477 // Ensure that we fix up branches.
478 __ FinalizeCode();
479
480 // Adjust native pc offsets in stack maps.
481 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
482 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
483 uint32_t new_position = __ GetAdjustedPosition(old_position);
484 DCHECK_GE(new_position, old_position);
485 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
486 }
487
488 // Adjust pc offsets for the disassembly information.
489 if (disasm_info_ != nullptr) {
490 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
491 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
492 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
493 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
494 it.second.start = __ GetAdjustedPosition(it.second.start);
495 it.second.end = __ GetAdjustedPosition(it.second.end);
496 }
497 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
498 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
499 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
500 }
501 }
502
503 CodeGenerator::Finalize(allocator);
504}
505
506MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
507 return codegen_->GetAssembler();
508}
509
510void ParallelMoveResolverMIPS::EmitMove(size_t index) {
511 DCHECK_LT(index, moves_.size());
512 MoveOperands* move = moves_[index];
513 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
514}
515
516void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
517 DCHECK_LT(index, moves_.size());
518 MoveOperands* move = moves_[index];
519 Primitive::Type type = move->GetType();
520 Location loc1 = move->GetDestination();
521 Location loc2 = move->GetSource();
522
523 DCHECK(!loc1.IsConstant());
524 DCHECK(!loc2.IsConstant());
525
526 if (loc1.Equals(loc2)) {
527 return;
528 }
529
530 if (loc1.IsRegister() && loc2.IsRegister()) {
531 // Swap 2 GPRs.
532 Register r1 = loc1.AsRegister<Register>();
533 Register r2 = loc2.AsRegister<Register>();
534 __ Move(TMP, r2);
535 __ Move(r2, r1);
536 __ Move(r1, TMP);
537 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
538 FRegister f1 = loc1.AsFpuRegister<FRegister>();
539 FRegister f2 = loc2.AsFpuRegister<FRegister>();
540 if (type == Primitive::kPrimFloat) {
541 __ MovS(FTMP, f2);
542 __ MovS(f2, f1);
543 __ MovS(f1, FTMP);
544 } else {
545 DCHECK_EQ(type, Primitive::kPrimDouble);
546 __ MovD(FTMP, f2);
547 __ MovD(f2, f1);
548 __ MovD(f1, FTMP);
549 }
550 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
551 (loc1.IsFpuRegister() && loc2.IsRegister())) {
552 // Swap FPR and GPR.
553 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
554 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
555 : loc2.AsFpuRegister<FRegister>();
556 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
557 : loc2.AsRegister<Register>();
558 __ Move(TMP, r2);
559 __ Mfc1(r2, f1);
560 __ Mtc1(TMP, f1);
561 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
562 // Swap 2 GPR register pairs.
563 Register r1 = loc1.AsRegisterPairLow<Register>();
564 Register r2 = loc2.AsRegisterPairLow<Register>();
565 __ Move(TMP, r2);
566 __ Move(r2, r1);
567 __ Move(r1, TMP);
568 r1 = loc1.AsRegisterPairHigh<Register>();
569 r2 = loc2.AsRegisterPairHigh<Register>();
570 __ Move(TMP, r2);
571 __ Move(r2, r1);
572 __ Move(r1, TMP);
573 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
574 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
575 // Swap FPR and GPR register pair.
576 DCHECK_EQ(type, Primitive::kPrimDouble);
577 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
578 : loc2.AsFpuRegister<FRegister>();
579 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
580 : loc2.AsRegisterPairLow<Register>();
581 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
582 : loc2.AsRegisterPairHigh<Register>();
583 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
584 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
585 // unpredictable and the following mfch1 will fail.
586 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800587 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200588 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800589 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200590 __ Move(r2_l, TMP);
591 __ Move(r2_h, AT);
592 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
593 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
594 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
595 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000596 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
597 (loc1.IsStackSlot() && loc2.IsRegister())) {
598 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
599 : loc2.AsRegister<Register>();
600 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
601 : loc2.GetStackIndex();
602 __ Move(TMP, reg);
603 __ LoadFromOffset(kLoadWord, reg, SP, offset);
604 __ StoreToOffset(kStoreWord, TMP, SP, offset);
605 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
606 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
607 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
608 : loc2.AsRegisterPairLow<Register>();
609 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
610 : loc2.AsRegisterPairHigh<Register>();
611 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
612 : loc2.GetStackIndex();
613 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
614 : loc2.GetHighStackIndex(kMipsWordSize);
615 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000616 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000617 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000618 __ Move(TMP, reg_h);
619 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
620 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200621 } else {
622 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
623 }
624}
625
626void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
627 __ Pop(static_cast<Register>(reg));
628}
629
630void ParallelMoveResolverMIPS::SpillScratch(int reg) {
631 __ Push(static_cast<Register>(reg));
632}
633
634void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
635 // Allocate a scratch register other than TMP, if available.
636 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
637 // automatically unspilled when the scratch scope object is destroyed).
638 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
639 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
640 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
641 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
642 __ LoadFromOffset(kLoadWord,
643 Register(ensure_scratch.GetRegister()),
644 SP,
645 index1 + stack_offset);
646 __ LoadFromOffset(kLoadWord,
647 TMP,
648 SP,
649 index2 + stack_offset);
650 __ StoreToOffset(kStoreWord,
651 Register(ensure_scratch.GetRegister()),
652 SP,
653 index2 + stack_offset);
654 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
655 }
656}
657
Alexey Frunze73296a72016-06-03 22:51:46 -0700658void CodeGeneratorMIPS::ComputeSpillMask() {
659 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
660 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
661 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
662 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
663 // registers, include the ZERO register to force alignment of FPU callee-saved registers
664 // within the stack frame.
665 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
666 core_spill_mask_ |= (1 << ZERO);
667 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700668}
669
670bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700671 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700672 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
673 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
674 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700675 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
676 // saved in an unused temporary register) and saving of RA and the current method pointer
677 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700678 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700679}
680
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200681static dwarf::Reg DWARFReg(Register reg) {
682 return dwarf::Reg::MipsCore(static_cast<int>(reg));
683}
684
685// TODO: mapping of floating-point registers to DWARF.
686
687void CodeGeneratorMIPS::GenerateFrameEntry() {
688 __ Bind(&frame_entry_label_);
689
690 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
691
692 if (do_overflow_check) {
693 __ LoadFromOffset(kLoadWord,
694 ZERO,
695 SP,
696 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
697 RecordPcInfo(nullptr, 0);
698 }
699
700 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700701 CHECK_EQ(fpu_spill_mask_, 0u);
702 CHECK_EQ(core_spill_mask_, 1u << RA);
703 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200704 return;
705 }
706
707 // Make sure the frame size isn't unreasonably large.
708 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
709 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
710 }
711
712 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200713
Alexey Frunze73296a72016-06-03 22:51:46 -0700714 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200715 __ IncreaseFrameSize(ofs);
716
Alexey Frunze73296a72016-06-03 22:51:46 -0700717 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
718 Register reg = static_cast<Register>(MostSignificantBit(mask));
719 mask ^= 1u << reg;
720 ofs -= kMipsWordSize;
721 // The ZERO register is only included for alignment.
722 if (reg != ZERO) {
723 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200724 __ cfi().RelOffset(DWARFReg(reg), ofs);
725 }
726 }
727
Alexey Frunze73296a72016-06-03 22:51:46 -0700728 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
729 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
730 mask ^= 1u << reg;
731 ofs -= kMipsDoublewordSize;
732 __ StoreDToOffset(reg, SP, ofs);
733 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200734 }
735
Alexey Frunze73296a72016-06-03 22:51:46 -0700736 // Store the current method pointer.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700737 // TODO: can we not do this if RequiresCurrentMethod() returns false?
Alexey Frunze73296a72016-06-03 22:51:46 -0700738 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200739}
740
741void CodeGeneratorMIPS::GenerateFrameExit() {
742 __ cfi().RememberState();
743
744 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200745 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200746
Alexey Frunze73296a72016-06-03 22:51:46 -0700747 // For better instruction scheduling restore RA before other registers.
748 uint32_t ofs = GetFrameSize();
749 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
750 Register reg = static_cast<Register>(MostSignificantBit(mask));
751 mask ^= 1u << reg;
752 ofs -= kMipsWordSize;
753 // The ZERO register is only included for alignment.
754 if (reg != ZERO) {
755 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200756 __ cfi().Restore(DWARFReg(reg));
757 }
758 }
759
Alexey Frunze73296a72016-06-03 22:51:46 -0700760 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
761 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
762 mask ^= 1u << reg;
763 ofs -= kMipsDoublewordSize;
764 __ LoadDFromOffset(reg, SP, ofs);
765 // TODO: __ cfi().Restore(DWARFReg(reg));
766 }
767
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700768 size_t frame_size = GetFrameSize();
769 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
770 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
771 bool reordering = __ SetReorder(false);
772 if (exchange) {
773 __ Jr(RA);
774 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
775 } else {
776 __ DecreaseFrameSize(frame_size);
777 __ Jr(RA);
778 __ Nop(); // In delay slot.
779 }
780 __ SetReorder(reordering);
781 } else {
782 __ Jr(RA);
783 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200784 }
785
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200786 __ cfi().RestoreState();
787 __ cfi().DefCFAOffset(GetFrameSize());
788}
789
790void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
791 __ Bind(GetLabelOf(block));
792}
793
794void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
795 if (src.Equals(dst)) {
796 return;
797 }
798
799 if (src.IsConstant()) {
800 MoveConstant(dst, src.GetConstant());
801 } else {
802 if (Primitive::Is64BitType(dst_type)) {
803 Move64(dst, src);
804 } else {
805 Move32(dst, src);
806 }
807 }
808}
809
810void CodeGeneratorMIPS::Move32(Location destination, Location source) {
811 if (source.Equals(destination)) {
812 return;
813 }
814
815 if (destination.IsRegister()) {
816 if (source.IsRegister()) {
817 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
818 } else if (source.IsFpuRegister()) {
819 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
820 } else {
821 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
822 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
823 }
824 } else if (destination.IsFpuRegister()) {
825 if (source.IsRegister()) {
826 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
827 } else if (source.IsFpuRegister()) {
828 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
829 } else {
830 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
831 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
832 }
833 } else {
834 DCHECK(destination.IsStackSlot()) << destination;
835 if (source.IsRegister()) {
836 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
837 } else if (source.IsFpuRegister()) {
838 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
839 } else {
840 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
841 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
842 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
843 }
844 }
845}
846
847void CodeGeneratorMIPS::Move64(Location destination, Location source) {
848 if (source.Equals(destination)) {
849 return;
850 }
851
852 if (destination.IsRegisterPair()) {
853 if (source.IsRegisterPair()) {
854 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
855 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
856 } else if (source.IsFpuRegister()) {
857 Register dst_high = destination.AsRegisterPairHigh<Register>();
858 Register dst_low = destination.AsRegisterPairLow<Register>();
859 FRegister src = source.AsFpuRegister<FRegister>();
860 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800861 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200862 } else {
863 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
864 int32_t off = source.GetStackIndex();
865 Register r = destination.AsRegisterPairLow<Register>();
866 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
867 }
868 } else if (destination.IsFpuRegister()) {
869 if (source.IsRegisterPair()) {
870 FRegister dst = destination.AsFpuRegister<FRegister>();
871 Register src_high = source.AsRegisterPairHigh<Register>();
872 Register src_low = source.AsRegisterPairLow<Register>();
873 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800874 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200875 } else if (source.IsFpuRegister()) {
876 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
877 } else {
878 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
879 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
880 }
881 } else {
882 DCHECK(destination.IsDoubleStackSlot()) << destination;
883 int32_t off = destination.GetStackIndex();
884 if (source.IsRegisterPair()) {
885 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
886 } else if (source.IsFpuRegister()) {
887 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
888 } else {
889 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
890 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
891 __ StoreToOffset(kStoreWord, TMP, SP, off);
892 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
893 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
894 }
895 }
896}
897
898void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
899 if (c->IsIntConstant() || c->IsNullConstant()) {
900 // Move 32 bit constant.
901 int32_t value = GetInt32ValueOf(c);
902 if (destination.IsRegister()) {
903 Register dst = destination.AsRegister<Register>();
904 __ LoadConst32(dst, value);
905 } else {
906 DCHECK(destination.IsStackSlot())
907 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700908 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200909 }
910 } else if (c->IsLongConstant()) {
911 // Move 64 bit constant.
912 int64_t value = GetInt64ValueOf(c);
913 if (destination.IsRegisterPair()) {
914 Register r_h = destination.AsRegisterPairHigh<Register>();
915 Register r_l = destination.AsRegisterPairLow<Register>();
916 __ LoadConst64(r_h, r_l, value);
917 } else {
918 DCHECK(destination.IsDoubleStackSlot())
919 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700920 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200921 }
922 } else if (c->IsFloatConstant()) {
923 // Move 32 bit float constant.
924 int32_t value = GetInt32ValueOf(c);
925 if (destination.IsFpuRegister()) {
926 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
927 } else {
928 DCHECK(destination.IsStackSlot())
929 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700930 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200931 }
932 } else {
933 // Move 64 bit double constant.
934 DCHECK(c->IsDoubleConstant()) << c->DebugName();
935 int64_t value = GetInt64ValueOf(c);
936 if (destination.IsFpuRegister()) {
937 FRegister fd = destination.AsFpuRegister<FRegister>();
938 __ LoadDConst64(fd, value, TMP);
939 } else {
940 DCHECK(destination.IsDoubleStackSlot())
941 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700942 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200943 }
944 }
945}
946
947void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
948 DCHECK(destination.IsRegister());
949 Register dst = destination.AsRegister<Register>();
950 __ LoadConst32(dst, value);
951}
952
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200953void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
954 if (location.IsRegister()) {
955 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700956 } else if (location.IsRegisterPair()) {
957 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
958 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200959 } else {
960 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
961 }
962}
963
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700964void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
965 DCHECK(linker_patches->empty());
966 size_t size =
967 method_patches_.size() +
968 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700969 pc_relative_dex_cache_patches_.size() +
970 pc_relative_string_patches_.size() +
971 pc_relative_type_patches_.size() +
972 boot_image_string_patches_.size() +
973 boot_image_type_patches_.size() +
974 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700975 linker_patches->reserve(size);
976 for (const auto& entry : method_patches_) {
977 const MethodReference& target_method = entry.first;
978 Literal* literal = entry.second;
979 DCHECK(literal->GetLabel()->IsBound());
980 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
981 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
982 target_method.dex_file,
983 target_method.dex_method_index));
984 }
985 for (const auto& entry : call_patches_) {
986 const MethodReference& target_method = entry.first;
987 Literal* literal = entry.second;
988 DCHECK(literal->GetLabel()->IsBound());
989 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
990 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
991 target_method.dex_file,
992 target_method.dex_method_index));
993 }
994 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
995 const DexFile& dex_file = info.target_dex_file;
996 size_t base_element_offset = info.offset_or_index;
997 DCHECK(info.high_label.IsBound());
998 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
999 DCHECK(info.pc_rel_label.IsBound());
1000 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1001 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
1002 &dex_file,
1003 pc_rel_offset,
1004 base_element_offset));
1005 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001006 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
1007 const DexFile& dex_file = info.target_dex_file;
1008 size_t string_index = info.offset_or_index;
1009 DCHECK(info.high_label.IsBound());
1010 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1011 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1012 // the assembler's base label used for PC-relative literals.
1013 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1014 ? __ GetLabelLocation(&info.pc_rel_label)
1015 : __ GetPcRelBaseLabelLocation();
1016 linker_patches->push_back(LinkerPatch::RelativeStringPatch(high_offset,
1017 &dex_file,
1018 pc_rel_offset,
1019 string_index));
1020 }
1021 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
1022 const DexFile& dex_file = info.target_dex_file;
1023 size_t type_index = info.offset_or_index;
1024 DCHECK(info.high_label.IsBound());
1025 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1026 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1027 // the assembler's base label used for PC-relative literals.
1028 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1029 ? __ GetLabelLocation(&info.pc_rel_label)
1030 : __ GetPcRelBaseLabelLocation();
1031 linker_patches->push_back(LinkerPatch::RelativeTypePatch(high_offset,
1032 &dex_file,
1033 pc_rel_offset,
1034 type_index));
1035 }
1036 for (const auto& entry : boot_image_string_patches_) {
1037 const StringReference& target_string = entry.first;
1038 Literal* literal = entry.second;
1039 DCHECK(literal->GetLabel()->IsBound());
1040 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1041 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1042 target_string.dex_file,
1043 target_string.string_index));
1044 }
1045 for (const auto& entry : boot_image_type_patches_) {
1046 const TypeReference& target_type = entry.first;
1047 Literal* literal = entry.second;
1048 DCHECK(literal->GetLabel()->IsBound());
1049 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1050 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1051 target_type.dex_file,
1052 target_type.type_index));
1053 }
1054 for (const auto& entry : boot_image_address_patches_) {
1055 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1056 Literal* literal = entry.second;
1057 DCHECK(literal->GetLabel()->IsBound());
1058 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1059 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1060 }
1061}
1062
1063CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1064 const DexFile& dex_file, uint32_t string_index) {
1065 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1066}
1067
1068CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1069 const DexFile& dex_file, uint32_t type_index) {
1070 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001071}
1072
1073CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1074 const DexFile& dex_file, uint32_t element_offset) {
1075 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1076}
1077
1078CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1079 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1080 patches->emplace_back(dex_file, offset_or_index);
1081 return &patches->back();
1082}
1083
Alexey Frunze06a46c42016-07-19 15:00:40 -07001084Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1085 return map->GetOrCreate(
1086 value,
1087 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1088}
1089
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001090Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1091 MethodToLiteralMap* map) {
1092 return map->GetOrCreate(
1093 target_method,
1094 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1095}
1096
1097Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1098 return DeduplicateMethodLiteral(target_method, &method_patches_);
1099}
1100
1101Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1102 return DeduplicateMethodLiteral(target_method, &call_patches_);
1103}
1104
Alexey Frunze06a46c42016-07-19 15:00:40 -07001105Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1106 uint32_t string_index) {
1107 return boot_image_string_patches_.GetOrCreate(
1108 StringReference(&dex_file, string_index),
1109 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1110}
1111
1112Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1113 uint32_t type_index) {
1114 return boot_image_type_patches_.GetOrCreate(
1115 TypeReference(&dex_file, type_index),
1116 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1117}
1118
1119Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1120 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1121 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1122 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1123}
1124
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001125void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1126 MipsLabel done;
1127 Register card = AT;
1128 Register temp = TMP;
1129 __ Beqz(value, &done);
1130 __ LoadFromOffset(kLoadWord,
1131 card,
1132 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001133 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001134 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1135 __ Addu(temp, card, temp);
1136 __ Sb(card, temp, 0);
1137 __ Bind(&done);
1138}
1139
David Brazdil58282f42016-01-14 12:45:10 +00001140void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001141 // Don't allocate the dalvik style register pair passing.
1142 blocked_register_pairs_[A1_A2] = true;
1143
1144 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1145 blocked_core_registers_[ZERO] = true;
1146 blocked_core_registers_[K0] = true;
1147 blocked_core_registers_[K1] = true;
1148 blocked_core_registers_[GP] = true;
1149 blocked_core_registers_[SP] = true;
1150 blocked_core_registers_[RA] = true;
1151
1152 // AT and TMP(T8) are used as temporary/scratch registers
1153 // (similar to how AT is used by MIPS assemblers).
1154 blocked_core_registers_[AT] = true;
1155 blocked_core_registers_[TMP] = true;
1156 blocked_fpu_registers_[FTMP] = true;
1157
1158 // Reserve suspend and thread registers.
1159 blocked_core_registers_[S0] = true;
1160 blocked_core_registers_[TR] = true;
1161
1162 // Reserve T9 for function calls
1163 blocked_core_registers_[T9] = true;
1164
1165 // Reserve odd-numbered FPU registers.
1166 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1167 blocked_fpu_registers_[i] = true;
1168 }
1169
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001170 if (GetGraph()->IsDebuggable()) {
1171 // Stubs do not save callee-save floating point registers. If the graph
1172 // is debuggable, we need to deal with these registers differently. For
1173 // now, just block them.
1174 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1175 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1176 }
1177 }
1178
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001179 UpdateBlockedPairRegisters();
1180}
1181
1182void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1183 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1184 MipsManagedRegister current =
1185 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1186 if (blocked_core_registers_[current.AsRegisterPairLow()]
1187 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1188 blocked_register_pairs_[i] = true;
1189 }
1190 }
1191}
1192
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001193size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1194 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1195 return kMipsWordSize;
1196}
1197
1198size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1199 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1200 return kMipsWordSize;
1201}
1202
1203size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1204 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1205 return kMipsDoublewordSize;
1206}
1207
1208size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1209 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1210 return kMipsDoublewordSize;
1211}
1212
1213void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001214 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001215}
1216
1217void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001218 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001219}
1220
Serban Constantinescufca16662016-07-14 09:21:59 +01001221constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1222
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001223void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1224 HInstruction* instruction,
1225 uint32_t dex_pc,
1226 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001227 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001228 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001229 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001230 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001231 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001232 // Reserve argument space on stack (for $a0-$a3) for
1233 // entrypoints that directly reference native implementations.
1234 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001235 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001236 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001237 } else {
1238 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001239 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001240 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001241 if (EntrypointRequiresStackMap(entrypoint)) {
1242 RecordPcInfo(instruction, dex_pc, slow_path);
1243 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001244}
1245
1246void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1247 Register class_reg) {
1248 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1249 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1250 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1251 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1252 __ Sync(0);
1253 __ Bind(slow_path->GetExitLabel());
1254}
1255
1256void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1257 __ Sync(0); // Only stype 0 is supported.
1258}
1259
1260void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1261 HBasicBlock* successor) {
1262 SuspendCheckSlowPathMIPS* slow_path =
1263 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1264 codegen_->AddSlowPath(slow_path);
1265
1266 __ LoadFromOffset(kLoadUnsignedHalfword,
1267 TMP,
1268 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001269 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001270 if (successor == nullptr) {
1271 __ Bnez(TMP, slow_path->GetEntryLabel());
1272 __ Bind(slow_path->GetReturnLabel());
1273 } else {
1274 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1275 __ B(slow_path->GetEntryLabel());
1276 // slow_path will return to GetLabelOf(successor).
1277 }
1278}
1279
1280InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1281 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001282 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001283 assembler_(codegen->GetAssembler()),
1284 codegen_(codegen) {}
1285
1286void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1287 DCHECK_EQ(instruction->InputCount(), 2U);
1288 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1289 Primitive::Type type = instruction->GetResultType();
1290 switch (type) {
1291 case Primitive::kPrimInt: {
1292 locations->SetInAt(0, Location::RequiresRegister());
1293 HInstruction* right = instruction->InputAt(1);
1294 bool can_use_imm = false;
1295 if (right->IsConstant()) {
1296 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1297 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1298 can_use_imm = IsUint<16>(imm);
1299 } else if (instruction->IsAdd()) {
1300 can_use_imm = IsInt<16>(imm);
1301 } else {
1302 DCHECK(instruction->IsSub());
1303 can_use_imm = IsInt<16>(-imm);
1304 }
1305 }
1306 if (can_use_imm)
1307 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1308 else
1309 locations->SetInAt(1, Location::RequiresRegister());
1310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1311 break;
1312 }
1313
1314 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001316 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001318 break;
1319 }
1320
1321 case Primitive::kPrimFloat:
1322 case Primitive::kPrimDouble:
1323 DCHECK(instruction->IsAdd() || instruction->IsSub());
1324 locations->SetInAt(0, Location::RequiresFpuRegister());
1325 locations->SetInAt(1, Location::RequiresFpuRegister());
1326 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1327 break;
1328
1329 default:
1330 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1331 }
1332}
1333
1334void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1335 Primitive::Type type = instruction->GetType();
1336 LocationSummary* locations = instruction->GetLocations();
1337
1338 switch (type) {
1339 case Primitive::kPrimInt: {
1340 Register dst = locations->Out().AsRegister<Register>();
1341 Register lhs = locations->InAt(0).AsRegister<Register>();
1342 Location rhs_location = locations->InAt(1);
1343
1344 Register rhs_reg = ZERO;
1345 int32_t rhs_imm = 0;
1346 bool use_imm = rhs_location.IsConstant();
1347 if (use_imm) {
1348 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1349 } else {
1350 rhs_reg = rhs_location.AsRegister<Register>();
1351 }
1352
1353 if (instruction->IsAnd()) {
1354 if (use_imm)
1355 __ Andi(dst, lhs, rhs_imm);
1356 else
1357 __ And(dst, lhs, rhs_reg);
1358 } else if (instruction->IsOr()) {
1359 if (use_imm)
1360 __ Ori(dst, lhs, rhs_imm);
1361 else
1362 __ Or(dst, lhs, rhs_reg);
1363 } else if (instruction->IsXor()) {
1364 if (use_imm)
1365 __ Xori(dst, lhs, rhs_imm);
1366 else
1367 __ Xor(dst, lhs, rhs_reg);
1368 } else if (instruction->IsAdd()) {
1369 if (use_imm)
1370 __ Addiu(dst, lhs, rhs_imm);
1371 else
1372 __ Addu(dst, lhs, rhs_reg);
1373 } else {
1374 DCHECK(instruction->IsSub());
1375 if (use_imm)
1376 __ Addiu(dst, lhs, -rhs_imm);
1377 else
1378 __ Subu(dst, lhs, rhs_reg);
1379 }
1380 break;
1381 }
1382
1383 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001384 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1385 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1386 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1387 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001388 Location rhs_location = locations->InAt(1);
1389 bool use_imm = rhs_location.IsConstant();
1390 if (!use_imm) {
1391 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1392 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1393 if (instruction->IsAnd()) {
1394 __ And(dst_low, lhs_low, rhs_low);
1395 __ And(dst_high, lhs_high, rhs_high);
1396 } else if (instruction->IsOr()) {
1397 __ Or(dst_low, lhs_low, rhs_low);
1398 __ Or(dst_high, lhs_high, rhs_high);
1399 } else if (instruction->IsXor()) {
1400 __ Xor(dst_low, lhs_low, rhs_low);
1401 __ Xor(dst_high, lhs_high, rhs_high);
1402 } else if (instruction->IsAdd()) {
1403 if (lhs_low == rhs_low) {
1404 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1405 __ Slt(TMP, lhs_low, ZERO);
1406 __ Addu(dst_low, lhs_low, rhs_low);
1407 } else {
1408 __ Addu(dst_low, lhs_low, rhs_low);
1409 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1410 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1411 }
1412 __ Addu(dst_high, lhs_high, rhs_high);
1413 __ Addu(dst_high, dst_high, TMP);
1414 } else {
1415 DCHECK(instruction->IsSub());
1416 __ Sltu(TMP, lhs_low, rhs_low);
1417 __ Subu(dst_low, lhs_low, rhs_low);
1418 __ Subu(dst_high, lhs_high, rhs_high);
1419 __ Subu(dst_high, dst_high, TMP);
1420 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001421 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001422 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1423 if (instruction->IsOr()) {
1424 uint32_t low = Low32Bits(value);
1425 uint32_t high = High32Bits(value);
1426 if (IsUint<16>(low)) {
1427 if (dst_low != lhs_low || low != 0) {
1428 __ Ori(dst_low, lhs_low, low);
1429 }
1430 } else {
1431 __ LoadConst32(TMP, low);
1432 __ Or(dst_low, lhs_low, TMP);
1433 }
1434 if (IsUint<16>(high)) {
1435 if (dst_high != lhs_high || high != 0) {
1436 __ Ori(dst_high, lhs_high, high);
1437 }
1438 } else {
1439 if (high != low) {
1440 __ LoadConst32(TMP, high);
1441 }
1442 __ Or(dst_high, lhs_high, TMP);
1443 }
1444 } else if (instruction->IsXor()) {
1445 uint32_t low = Low32Bits(value);
1446 uint32_t high = High32Bits(value);
1447 if (IsUint<16>(low)) {
1448 if (dst_low != lhs_low || low != 0) {
1449 __ Xori(dst_low, lhs_low, low);
1450 }
1451 } else {
1452 __ LoadConst32(TMP, low);
1453 __ Xor(dst_low, lhs_low, TMP);
1454 }
1455 if (IsUint<16>(high)) {
1456 if (dst_high != lhs_high || high != 0) {
1457 __ Xori(dst_high, lhs_high, high);
1458 }
1459 } else {
1460 if (high != low) {
1461 __ LoadConst32(TMP, high);
1462 }
1463 __ Xor(dst_high, lhs_high, TMP);
1464 }
1465 } else if (instruction->IsAnd()) {
1466 uint32_t low = Low32Bits(value);
1467 uint32_t high = High32Bits(value);
1468 if (IsUint<16>(low)) {
1469 __ Andi(dst_low, lhs_low, low);
1470 } else if (low != 0xFFFFFFFF) {
1471 __ LoadConst32(TMP, low);
1472 __ And(dst_low, lhs_low, TMP);
1473 } else if (dst_low != lhs_low) {
1474 __ Move(dst_low, lhs_low);
1475 }
1476 if (IsUint<16>(high)) {
1477 __ Andi(dst_high, lhs_high, high);
1478 } else if (high != 0xFFFFFFFF) {
1479 if (high != low) {
1480 __ LoadConst32(TMP, high);
1481 }
1482 __ And(dst_high, lhs_high, TMP);
1483 } else if (dst_high != lhs_high) {
1484 __ Move(dst_high, lhs_high);
1485 }
1486 } else {
1487 if (instruction->IsSub()) {
1488 value = -value;
1489 } else {
1490 DCHECK(instruction->IsAdd());
1491 }
1492 int32_t low = Low32Bits(value);
1493 int32_t high = High32Bits(value);
1494 if (IsInt<16>(low)) {
1495 if (dst_low != lhs_low || low != 0) {
1496 __ Addiu(dst_low, lhs_low, low);
1497 }
1498 if (low != 0) {
1499 __ Sltiu(AT, dst_low, low);
1500 }
1501 } else {
1502 __ LoadConst32(TMP, low);
1503 __ Addu(dst_low, lhs_low, TMP);
1504 __ Sltu(AT, dst_low, TMP);
1505 }
1506 if (IsInt<16>(high)) {
1507 if (dst_high != lhs_high || high != 0) {
1508 __ Addiu(dst_high, lhs_high, high);
1509 }
1510 } else {
1511 if (high != low) {
1512 __ LoadConst32(TMP, high);
1513 }
1514 __ Addu(dst_high, lhs_high, TMP);
1515 }
1516 if (low != 0) {
1517 __ Addu(dst_high, dst_high, AT);
1518 }
1519 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001520 }
1521 break;
1522 }
1523
1524 case Primitive::kPrimFloat:
1525 case Primitive::kPrimDouble: {
1526 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1527 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1528 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1529 if (instruction->IsAdd()) {
1530 if (type == Primitive::kPrimFloat) {
1531 __ AddS(dst, lhs, rhs);
1532 } else {
1533 __ AddD(dst, lhs, rhs);
1534 }
1535 } else {
1536 DCHECK(instruction->IsSub());
1537 if (type == Primitive::kPrimFloat) {
1538 __ SubS(dst, lhs, rhs);
1539 } else {
1540 __ SubD(dst, lhs, rhs);
1541 }
1542 }
1543 break;
1544 }
1545
1546 default:
1547 LOG(FATAL) << "Unexpected binary operation type " << type;
1548 }
1549}
1550
1551void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001552 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001553
1554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1555 Primitive::Type type = instr->GetResultType();
1556 switch (type) {
1557 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1560 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1561 break;
1562 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001563 locations->SetInAt(0, Location::RequiresRegister());
1564 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1565 locations->SetOut(Location::RequiresRegister());
1566 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001567 default:
1568 LOG(FATAL) << "Unexpected shift type " << type;
1569 }
1570}
1571
1572static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1573
1574void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001575 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 LocationSummary* locations = instr->GetLocations();
1577 Primitive::Type type = instr->GetType();
1578
1579 Location rhs_location = locations->InAt(1);
1580 bool use_imm = rhs_location.IsConstant();
1581 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1582 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001583 const uint32_t shift_mask =
1584 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001585 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001586 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1587 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001588
1589 switch (type) {
1590 case Primitive::kPrimInt: {
1591 Register dst = locations->Out().AsRegister<Register>();
1592 Register lhs = locations->InAt(0).AsRegister<Register>();
1593 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001594 if (shift_value == 0) {
1595 if (dst != lhs) {
1596 __ Move(dst, lhs);
1597 }
1598 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001599 __ Sll(dst, lhs, shift_value);
1600 } else if (instr->IsShr()) {
1601 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001602 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001604 } else {
1605 if (has_ins_rotr) {
1606 __ Rotr(dst, lhs, shift_value);
1607 } else {
1608 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1609 __ Srl(dst, lhs, shift_value);
1610 __ Or(dst, dst, TMP);
1611 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001612 }
1613 } else {
1614 if (instr->IsShl()) {
1615 __ Sllv(dst, lhs, rhs_reg);
1616 } else if (instr->IsShr()) {
1617 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001618 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001619 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001620 } else {
1621 if (has_ins_rotr) {
1622 __ Rotrv(dst, lhs, rhs_reg);
1623 } else {
1624 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001625 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1626 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1627 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1628 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1629 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001630 __ Sllv(TMP, lhs, TMP);
1631 __ Srlv(dst, lhs, rhs_reg);
1632 __ Or(dst, dst, TMP);
1633 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001634 }
1635 }
1636 break;
1637 }
1638
1639 case Primitive::kPrimLong: {
1640 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1641 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1642 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1643 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1644 if (use_imm) {
1645 if (shift_value == 0) {
1646 codegen_->Move64(locations->Out(), locations->InAt(0));
1647 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001648 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001649 if (instr->IsShl()) {
1650 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1651 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1652 __ Sll(dst_low, lhs_low, shift_value);
1653 } else if (instr->IsShr()) {
1654 __ Srl(dst_low, lhs_low, shift_value);
1655 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1656 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001657 } else if (instr->IsUShr()) {
1658 __ Srl(dst_low, lhs_low, shift_value);
1659 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1660 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001661 } else {
1662 __ Srl(dst_low, lhs_low, shift_value);
1663 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1664 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001665 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001666 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001667 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001668 if (instr->IsShl()) {
1669 __ Sll(dst_low, lhs_low, shift_value);
1670 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1671 __ Sll(dst_high, lhs_high, shift_value);
1672 __ Or(dst_high, dst_high, TMP);
1673 } else if (instr->IsShr()) {
1674 __ Sra(dst_high, lhs_high, shift_value);
1675 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1676 __ Srl(dst_low, lhs_low, shift_value);
1677 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001678 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001679 __ Srl(dst_high, lhs_high, shift_value);
1680 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1681 __ Srl(dst_low, lhs_low, shift_value);
1682 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001683 } else {
1684 __ Srl(TMP, lhs_low, shift_value);
1685 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1686 __ Or(dst_low, dst_low, TMP);
1687 __ Srl(TMP, lhs_high, shift_value);
1688 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1689 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001690 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001691 }
1692 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001693 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001694 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001695 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001696 __ Move(dst_low, ZERO);
1697 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001698 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001699 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001700 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001701 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001702 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001703 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001704 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 // 64-bit rotation by 32 is just a swap.
1706 __ Move(dst_low, lhs_high);
1707 __ Move(dst_high, lhs_low);
1708 } else {
1709 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001710 __ Srl(dst_low, lhs_high, shift_value_high);
1711 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1712 __ Srl(dst_high, lhs_low, shift_value_high);
1713 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001714 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001715 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1716 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001717 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001718 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1719 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001720 __ Or(dst_high, dst_high, TMP);
1721 }
1722 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001723 }
1724 }
1725 } else {
1726 MipsLabel done;
1727 if (instr->IsShl()) {
1728 __ Sllv(dst_low, lhs_low, rhs_reg);
1729 __ Nor(AT, ZERO, rhs_reg);
1730 __ Srl(TMP, lhs_low, 1);
1731 __ Srlv(TMP, TMP, AT);
1732 __ Sllv(dst_high, lhs_high, rhs_reg);
1733 __ Or(dst_high, dst_high, TMP);
1734 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1735 __ Beqz(TMP, &done);
1736 __ Move(dst_high, dst_low);
1737 __ Move(dst_low, ZERO);
1738 } else if (instr->IsShr()) {
1739 __ Srav(dst_high, lhs_high, rhs_reg);
1740 __ Nor(AT, ZERO, rhs_reg);
1741 __ Sll(TMP, lhs_high, 1);
1742 __ Sllv(TMP, TMP, AT);
1743 __ Srlv(dst_low, lhs_low, rhs_reg);
1744 __ Or(dst_low, dst_low, TMP);
1745 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1746 __ Beqz(TMP, &done);
1747 __ Move(dst_low, dst_high);
1748 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001749 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001750 __ Srlv(dst_high, lhs_high, rhs_reg);
1751 __ Nor(AT, ZERO, rhs_reg);
1752 __ Sll(TMP, lhs_high, 1);
1753 __ Sllv(TMP, TMP, AT);
1754 __ Srlv(dst_low, lhs_low, rhs_reg);
1755 __ Or(dst_low, dst_low, TMP);
1756 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1757 __ Beqz(TMP, &done);
1758 __ Move(dst_low, dst_high);
1759 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001760 } else {
1761 __ Nor(AT, ZERO, rhs_reg);
1762 __ Srlv(TMP, lhs_low, rhs_reg);
1763 __ Sll(dst_low, lhs_high, 1);
1764 __ Sllv(dst_low, dst_low, AT);
1765 __ Or(dst_low, dst_low, TMP);
1766 __ Srlv(TMP, lhs_high, rhs_reg);
1767 __ Sll(dst_high, lhs_low, 1);
1768 __ Sllv(dst_high, dst_high, AT);
1769 __ Or(dst_high, dst_high, TMP);
1770 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1771 __ Beqz(TMP, &done);
1772 __ Move(TMP, dst_high);
1773 __ Move(dst_high, dst_low);
1774 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001775 }
1776 __ Bind(&done);
1777 }
1778 break;
1779 }
1780
1781 default:
1782 LOG(FATAL) << "Unexpected shift operation type " << type;
1783 }
1784}
1785
1786void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1787 HandleBinaryOp(instruction);
1788}
1789
1790void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1791 HandleBinaryOp(instruction);
1792}
1793
1794void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1795 HandleBinaryOp(instruction);
1796}
1797
1798void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1799 HandleBinaryOp(instruction);
1800}
1801
1802void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1803 LocationSummary* locations =
1804 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1805 locations->SetInAt(0, Location::RequiresRegister());
1806 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1807 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1808 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1809 } else {
1810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1811 }
1812}
1813
Alexey Frunze2923db72016-08-20 01:55:47 -07001814auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1815 auto null_checker = [this, instruction]() {
1816 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1817 };
1818 return null_checker;
1819}
1820
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001821void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1822 LocationSummary* locations = instruction->GetLocations();
1823 Register obj = locations->InAt(0).AsRegister<Register>();
1824 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001825 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001826 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001827
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001828 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001829 switch (type) {
1830 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001831 Register out = locations->Out().AsRegister<Register>();
1832 if (index.IsConstant()) {
1833 size_t offset =
1834 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001835 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001836 } else {
1837 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001838 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001839 }
1840 break;
1841 }
1842
1843 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001844 Register out = locations->Out().AsRegister<Register>();
1845 if (index.IsConstant()) {
1846 size_t offset =
1847 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001848 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001849 } else {
1850 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001851 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852 }
1853 break;
1854 }
1855
1856 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 Register out = locations->Out().AsRegister<Register>();
1858 if (index.IsConstant()) {
1859 size_t offset =
1860 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001861 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001862 } else {
1863 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1864 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001865 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 Register out = locations->Out().AsRegister<Register>();
1872 if (index.IsConstant()) {
1873 size_t offset =
1874 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001875 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 } else {
1877 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1878 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001879 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880 }
1881 break;
1882 }
1883
1884 case Primitive::kPrimInt:
1885 case Primitive::kPrimNot: {
1886 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887 Register out = locations->Out().AsRegister<Register>();
1888 if (index.IsConstant()) {
1889 size_t offset =
1890 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001891 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001892 } else {
1893 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1894 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001895 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001896 }
1897 break;
1898 }
1899
1900 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901 Register out = locations->Out().AsRegisterPairLow<Register>();
1902 if (index.IsConstant()) {
1903 size_t offset =
1904 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001905 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906 } else {
1907 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1908 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001909 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910 }
1911 break;
1912 }
1913
1914 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1916 if (index.IsConstant()) {
1917 size_t offset =
1918 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001919 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001920 } else {
1921 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1922 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001923 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001924 }
1925 break;
1926 }
1927
1928 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1930 if (index.IsConstant()) {
1931 size_t offset =
1932 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001933 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 } else {
1935 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1936 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001937 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001938 }
1939 break;
1940 }
1941
1942 case Primitive::kPrimVoid:
1943 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1944 UNREACHABLE();
1945 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946}
1947
1948void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1949 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1950 locations->SetInAt(0, Location::RequiresRegister());
1951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1952}
1953
1954void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1955 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001956 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001957 Register obj = locations->InAt(0).AsRegister<Register>();
1958 Register out = locations->Out().AsRegister<Register>();
1959 __ LoadFromOffset(kLoadWord, out, obj, offset);
1960 codegen_->MaybeRecordImplicitNullCheck(instruction);
1961}
1962
Alexey Frunzef58b2482016-09-02 22:14:06 -07001963Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1964 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1965 ? Location::ConstantLocation(instruction->AsConstant())
1966 : Location::RequiresRegister();
1967}
1968
1969Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1970 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1971 // We can store a non-zero float or double constant without first loading it into the FPU,
1972 // but we should only prefer this if the constant has a single use.
1973 if (instruction->IsConstant() &&
1974 (instruction->AsConstant()->IsZeroBitPattern() ||
1975 instruction->GetUses().HasExactlyOneElement())) {
1976 return Location::ConstantLocation(instruction->AsConstant());
1977 // Otherwise fall through and require an FPU register for the constant.
1978 }
1979 return Location::RequiresFpuRegister();
1980}
1981
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001982void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001983 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1985 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001986 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001987 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001988 InvokeRuntimeCallingConvention calling_convention;
1989 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1990 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1991 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1992 } else {
1993 locations->SetInAt(0, Location::RequiresRegister());
1994 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1995 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07001996 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001997 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07001998 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001999 }
2000 }
2001}
2002
2003void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2004 LocationSummary* locations = instruction->GetLocations();
2005 Register obj = locations->InAt(0).AsRegister<Register>();
2006 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002007 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002008 Primitive::Type value_type = instruction->GetComponentType();
2009 bool needs_runtime_call = locations->WillCall();
2010 bool needs_write_barrier =
2011 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002012 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002013 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014
2015 switch (value_type) {
2016 case Primitive::kPrimBoolean:
2017 case Primitive::kPrimByte: {
2018 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002019 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002020 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002021 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002022 __ Addu(base_reg, obj, index.AsRegister<Register>());
2023 }
2024 if (value_location.IsConstant()) {
2025 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2026 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2027 } else {
2028 Register value = value_location.AsRegister<Register>();
2029 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002030 }
2031 break;
2032 }
2033
2034 case Primitive::kPrimShort:
2035 case Primitive::kPrimChar: {
2036 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002037 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002038 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002039 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002040 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2041 __ Addu(base_reg, obj, base_reg);
2042 }
2043 if (value_location.IsConstant()) {
2044 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2045 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2046 } else {
2047 Register value = value_location.AsRegister<Register>();
2048 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002049 }
2050 break;
2051 }
2052
2053 case Primitive::kPrimInt:
2054 case Primitive::kPrimNot: {
2055 if (!needs_runtime_call) {
2056 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002057 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002058 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002060 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2061 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002062 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002063 if (value_location.IsConstant()) {
2064 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2065 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2066 DCHECK(!needs_write_barrier);
2067 } else {
2068 Register value = value_location.AsRegister<Register>();
2069 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2070 if (needs_write_barrier) {
2071 DCHECK_EQ(value_type, Primitive::kPrimNot);
2072 codegen_->MarkGCCard(obj, value);
2073 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 }
2075 } else {
2076 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002077 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002078 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2079 }
2080 break;
2081 }
2082
2083 case Primitive::kPrimLong: {
2084 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002085 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002086 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002088 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2089 __ Addu(base_reg, obj, base_reg);
2090 }
2091 if (value_location.IsConstant()) {
2092 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2093 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2094 } else {
2095 Register value = value_location.AsRegisterPairLow<Register>();
2096 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002097 }
2098 break;
2099 }
2100
2101 case Primitive::kPrimFloat: {
2102 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002103 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002104 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002105 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002106 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2107 __ Addu(base_reg, obj, base_reg);
2108 }
2109 if (value_location.IsConstant()) {
2110 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2111 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2112 } else {
2113 FRegister value = value_location.AsFpuRegister<FRegister>();
2114 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002115 }
2116 break;
2117 }
2118
2119 case Primitive::kPrimDouble: {
2120 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002121 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002122 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002123 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002124 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2125 __ Addu(base_reg, obj, base_reg);
2126 }
2127 if (value_location.IsConstant()) {
2128 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2129 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2130 } else {
2131 FRegister value = value_location.AsFpuRegister<FRegister>();
2132 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002133 }
2134 break;
2135 }
2136
2137 case Primitive::kPrimVoid:
2138 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2139 UNREACHABLE();
2140 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002141}
2142
2143void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2144 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2145 ? LocationSummary::kCallOnSlowPath
2146 : LocationSummary::kNoCall;
2147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2148 locations->SetInAt(0, Location::RequiresRegister());
2149 locations->SetInAt(1, Location::RequiresRegister());
2150 if (instruction->HasUses()) {
2151 locations->SetOut(Location::SameAsFirstInput());
2152 }
2153}
2154
2155void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2156 LocationSummary* locations = instruction->GetLocations();
2157 BoundsCheckSlowPathMIPS* slow_path =
2158 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2159 codegen_->AddSlowPath(slow_path);
2160
2161 Register index = locations->InAt(0).AsRegister<Register>();
2162 Register length = locations->InAt(1).AsRegister<Register>();
2163
2164 // length is limited by the maximum positive signed 32-bit integer.
2165 // Unsigned comparison of length and index checks for index < 0
2166 // and for length <= index simultaneously.
2167 __ Bgeu(index, length, slow_path->GetEntryLabel());
2168}
2169
2170void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2171 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2172 instruction,
2173 LocationSummary::kCallOnSlowPath);
2174 locations->SetInAt(0, Location::RequiresRegister());
2175 locations->SetInAt(1, Location::RequiresRegister());
2176 // Note that TypeCheckSlowPathMIPS uses this register too.
2177 locations->AddTemp(Location::RequiresRegister());
2178}
2179
2180void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2181 LocationSummary* locations = instruction->GetLocations();
2182 Register obj = locations->InAt(0).AsRegister<Register>();
2183 Register cls = locations->InAt(1).AsRegister<Register>();
2184 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2185
2186 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2187 codegen_->AddSlowPath(slow_path);
2188
2189 // TODO: avoid this check if we know obj is not null.
2190 __ Beqz(obj, slow_path->GetExitLabel());
2191 // Compare the class of `obj` with `cls`.
2192 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2193 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2194 __ Bind(slow_path->GetExitLabel());
2195}
2196
2197void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2198 LocationSummary* locations =
2199 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2200 locations->SetInAt(0, Location::RequiresRegister());
2201 if (check->HasUses()) {
2202 locations->SetOut(Location::SameAsFirstInput());
2203 }
2204}
2205
2206void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2207 // We assume the class is not null.
2208 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2209 check->GetLoadClass(),
2210 check,
2211 check->GetDexPc(),
2212 true);
2213 codegen_->AddSlowPath(slow_path);
2214 GenerateClassInitializationCheck(slow_path,
2215 check->GetLocations()->InAt(0).AsRegister<Register>());
2216}
2217
2218void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2219 Primitive::Type in_type = compare->InputAt(0)->GetType();
2220
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002221 LocationSummary* locations =
2222 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002223
2224 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002225 case Primitive::kPrimBoolean:
2226 case Primitive::kPrimByte:
2227 case Primitive::kPrimShort:
2228 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002229 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002230 case Primitive::kPrimLong:
2231 locations->SetInAt(0, Location::RequiresRegister());
2232 locations->SetInAt(1, Location::RequiresRegister());
2233 // Output overlaps because it is written before doing the low comparison.
2234 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2235 break;
2236
2237 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002238 case Primitive::kPrimDouble:
2239 locations->SetInAt(0, Location::RequiresFpuRegister());
2240 locations->SetInAt(1, Location::RequiresFpuRegister());
2241 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002242 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002243
2244 default:
2245 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2246 }
2247}
2248
2249void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2250 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002251 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002252 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002253 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002254
2255 // 0 if: left == right
2256 // 1 if: left > right
2257 // -1 if: left < right
2258 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002259 case Primitive::kPrimBoolean:
2260 case Primitive::kPrimByte:
2261 case Primitive::kPrimShort:
2262 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002263 case Primitive::kPrimInt: {
2264 Register lhs = locations->InAt(0).AsRegister<Register>();
2265 Register rhs = locations->InAt(1).AsRegister<Register>();
2266 __ Slt(TMP, lhs, rhs);
2267 __ Slt(res, rhs, lhs);
2268 __ Subu(res, res, TMP);
2269 break;
2270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271 case Primitive::kPrimLong: {
2272 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002273 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2274 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2275 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2276 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2277 // TODO: more efficient (direct) comparison with a constant.
2278 __ Slt(TMP, lhs_high, rhs_high);
2279 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2280 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2281 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2282 __ Sltu(TMP, lhs_low, rhs_low);
2283 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2284 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2285 __ Bind(&done);
2286 break;
2287 }
2288
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002289 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002290 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002291 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2292 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2293 MipsLabel done;
2294 if (isR6) {
2295 __ CmpEqS(FTMP, lhs, rhs);
2296 __ LoadConst32(res, 0);
2297 __ Bc1nez(FTMP, &done);
2298 if (gt_bias) {
2299 __ CmpLtS(FTMP, lhs, rhs);
2300 __ LoadConst32(res, -1);
2301 __ Bc1nez(FTMP, &done);
2302 __ LoadConst32(res, 1);
2303 } else {
2304 __ CmpLtS(FTMP, rhs, lhs);
2305 __ LoadConst32(res, 1);
2306 __ Bc1nez(FTMP, &done);
2307 __ LoadConst32(res, -1);
2308 }
2309 } else {
2310 if (gt_bias) {
2311 __ ColtS(0, lhs, rhs);
2312 __ LoadConst32(res, -1);
2313 __ Bc1t(0, &done);
2314 __ CeqS(0, lhs, rhs);
2315 __ LoadConst32(res, 1);
2316 __ Movt(res, ZERO, 0);
2317 } else {
2318 __ ColtS(0, rhs, lhs);
2319 __ LoadConst32(res, 1);
2320 __ Bc1t(0, &done);
2321 __ CeqS(0, lhs, rhs);
2322 __ LoadConst32(res, -1);
2323 __ Movt(res, ZERO, 0);
2324 }
2325 }
2326 __ Bind(&done);
2327 break;
2328 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002329 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002330 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002331 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2332 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2333 MipsLabel done;
2334 if (isR6) {
2335 __ CmpEqD(FTMP, lhs, rhs);
2336 __ LoadConst32(res, 0);
2337 __ Bc1nez(FTMP, &done);
2338 if (gt_bias) {
2339 __ CmpLtD(FTMP, lhs, rhs);
2340 __ LoadConst32(res, -1);
2341 __ Bc1nez(FTMP, &done);
2342 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002343 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002344 __ CmpLtD(FTMP, rhs, lhs);
2345 __ LoadConst32(res, 1);
2346 __ Bc1nez(FTMP, &done);
2347 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002348 }
2349 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002350 if (gt_bias) {
2351 __ ColtD(0, lhs, rhs);
2352 __ LoadConst32(res, -1);
2353 __ Bc1t(0, &done);
2354 __ CeqD(0, lhs, rhs);
2355 __ LoadConst32(res, 1);
2356 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002357 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002358 __ ColtD(0, rhs, lhs);
2359 __ LoadConst32(res, 1);
2360 __ Bc1t(0, &done);
2361 __ CeqD(0, lhs, rhs);
2362 __ LoadConst32(res, -1);
2363 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002364 }
2365 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002366 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002367 break;
2368 }
2369
2370 default:
2371 LOG(FATAL) << "Unimplemented compare type " << in_type;
2372 }
2373}
2374
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002375void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002377 switch (instruction->InputAt(0)->GetType()) {
2378 default:
2379 case Primitive::kPrimLong:
2380 locations->SetInAt(0, Location::RequiresRegister());
2381 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2382 break;
2383
2384 case Primitive::kPrimFloat:
2385 case Primitive::kPrimDouble:
2386 locations->SetInAt(0, Location::RequiresFpuRegister());
2387 locations->SetInAt(1, Location::RequiresFpuRegister());
2388 break;
2389 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002390 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2392 }
2393}
2394
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002395void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002396 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002397 return;
2398 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002399
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002400 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002401 LocationSummary* locations = instruction->GetLocations();
2402 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002403 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002404
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002405 switch (type) {
2406 default:
2407 // Integer case.
2408 GenerateIntCompare(instruction->GetCondition(), locations);
2409 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002410
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002411 case Primitive::kPrimLong:
2412 // TODO: don't use branches.
2413 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002414 break;
2415
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002416 case Primitive::kPrimFloat:
2417 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002418 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2419 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002420 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002421
2422 // Convert the branches into the result.
2423 MipsLabel done;
2424
2425 // False case: result = 0.
2426 __ LoadConst32(dst, 0);
2427 __ B(&done);
2428
2429 // True case: result = 1.
2430 __ Bind(&true_label);
2431 __ LoadConst32(dst, 1);
2432 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002433}
2434
Alexey Frunze7e99e052015-11-24 19:28:01 -08002435void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2436 DCHECK(instruction->IsDiv() || instruction->IsRem());
2437 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2438
2439 LocationSummary* locations = instruction->GetLocations();
2440 Location second = locations->InAt(1);
2441 DCHECK(second.IsConstant());
2442
2443 Register out = locations->Out().AsRegister<Register>();
2444 Register dividend = locations->InAt(0).AsRegister<Register>();
2445 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2446 DCHECK(imm == 1 || imm == -1);
2447
2448 if (instruction->IsRem()) {
2449 __ Move(out, ZERO);
2450 } else {
2451 if (imm == -1) {
2452 __ Subu(out, ZERO, dividend);
2453 } else if (out != dividend) {
2454 __ Move(out, dividend);
2455 }
2456 }
2457}
2458
2459void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2460 DCHECK(instruction->IsDiv() || instruction->IsRem());
2461 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2462
2463 LocationSummary* locations = instruction->GetLocations();
2464 Location second = locations->InAt(1);
2465 DCHECK(second.IsConstant());
2466
2467 Register out = locations->Out().AsRegister<Register>();
2468 Register dividend = locations->InAt(0).AsRegister<Register>();
2469 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002470 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002471 int ctz_imm = CTZ(abs_imm);
2472
2473 if (instruction->IsDiv()) {
2474 if (ctz_imm == 1) {
2475 // Fast path for division by +/-2, which is very common.
2476 __ Srl(TMP, dividend, 31);
2477 } else {
2478 __ Sra(TMP, dividend, 31);
2479 __ Srl(TMP, TMP, 32 - ctz_imm);
2480 }
2481 __ Addu(out, dividend, TMP);
2482 __ Sra(out, out, ctz_imm);
2483 if (imm < 0) {
2484 __ Subu(out, ZERO, out);
2485 }
2486 } else {
2487 if (ctz_imm == 1) {
2488 // Fast path for modulo +/-2, which is very common.
2489 __ Sra(TMP, dividend, 31);
2490 __ Subu(out, dividend, TMP);
2491 __ Andi(out, out, 1);
2492 __ Addu(out, out, TMP);
2493 } else {
2494 __ Sra(TMP, dividend, 31);
2495 __ Srl(TMP, TMP, 32 - ctz_imm);
2496 __ Addu(out, dividend, TMP);
2497 if (IsUint<16>(abs_imm - 1)) {
2498 __ Andi(out, out, abs_imm - 1);
2499 } else {
2500 __ Sll(out, out, 32 - ctz_imm);
2501 __ Srl(out, out, 32 - ctz_imm);
2502 }
2503 __ Subu(out, out, TMP);
2504 }
2505 }
2506}
2507
2508void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2509 DCHECK(instruction->IsDiv() || instruction->IsRem());
2510 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2511
2512 LocationSummary* locations = instruction->GetLocations();
2513 Location second = locations->InAt(1);
2514 DCHECK(second.IsConstant());
2515
2516 Register out = locations->Out().AsRegister<Register>();
2517 Register dividend = locations->InAt(0).AsRegister<Register>();
2518 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2519
2520 int64_t magic;
2521 int shift;
2522 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2523
2524 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2525
2526 __ LoadConst32(TMP, magic);
2527 if (isR6) {
2528 __ MuhR6(TMP, dividend, TMP);
2529 } else {
2530 __ MultR2(dividend, TMP);
2531 __ Mfhi(TMP);
2532 }
2533 if (imm > 0 && magic < 0) {
2534 __ Addu(TMP, TMP, dividend);
2535 } else if (imm < 0 && magic > 0) {
2536 __ Subu(TMP, TMP, dividend);
2537 }
2538
2539 if (shift != 0) {
2540 __ Sra(TMP, TMP, shift);
2541 }
2542
2543 if (instruction->IsDiv()) {
2544 __ Sra(out, TMP, 31);
2545 __ Subu(out, TMP, out);
2546 } else {
2547 __ Sra(AT, TMP, 31);
2548 __ Subu(AT, TMP, AT);
2549 __ LoadConst32(TMP, imm);
2550 if (isR6) {
2551 __ MulR6(TMP, AT, TMP);
2552 } else {
2553 __ MulR2(TMP, AT, TMP);
2554 }
2555 __ Subu(out, dividend, TMP);
2556 }
2557}
2558
2559void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2560 DCHECK(instruction->IsDiv() || instruction->IsRem());
2561 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2562
2563 LocationSummary* locations = instruction->GetLocations();
2564 Register out = locations->Out().AsRegister<Register>();
2565 Location second = locations->InAt(1);
2566
2567 if (second.IsConstant()) {
2568 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2569 if (imm == 0) {
2570 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2571 } else if (imm == 1 || imm == -1) {
2572 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002573 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002574 DivRemByPowerOfTwo(instruction);
2575 } else {
2576 DCHECK(imm <= -2 || imm >= 2);
2577 GenerateDivRemWithAnyConstant(instruction);
2578 }
2579 } else {
2580 Register dividend = locations->InAt(0).AsRegister<Register>();
2581 Register divisor = second.AsRegister<Register>();
2582 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2583 if (instruction->IsDiv()) {
2584 if (isR6) {
2585 __ DivR6(out, dividend, divisor);
2586 } else {
2587 __ DivR2(out, dividend, divisor);
2588 }
2589 } else {
2590 if (isR6) {
2591 __ ModR6(out, dividend, divisor);
2592 } else {
2593 __ ModR2(out, dividend, divisor);
2594 }
2595 }
2596 }
2597}
2598
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002599void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2600 Primitive::Type type = div->GetResultType();
2601 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002602 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603 : LocationSummary::kNoCall;
2604
2605 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2606
2607 switch (type) {
2608 case Primitive::kPrimInt:
2609 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002610 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002611 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2612 break;
2613
2614 case Primitive::kPrimLong: {
2615 InvokeRuntimeCallingConvention calling_convention;
2616 locations->SetInAt(0, Location::RegisterPairLocation(
2617 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2618 locations->SetInAt(1, Location::RegisterPairLocation(
2619 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2620 locations->SetOut(calling_convention.GetReturnLocation(type));
2621 break;
2622 }
2623
2624 case Primitive::kPrimFloat:
2625 case Primitive::kPrimDouble:
2626 locations->SetInAt(0, Location::RequiresFpuRegister());
2627 locations->SetInAt(1, Location::RequiresFpuRegister());
2628 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2629 break;
2630
2631 default:
2632 LOG(FATAL) << "Unexpected div type " << type;
2633 }
2634}
2635
2636void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2637 Primitive::Type type = instruction->GetType();
2638 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002639
2640 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002641 case Primitive::kPrimInt:
2642 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002644 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002645 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002646 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2647 break;
2648 }
2649 case Primitive::kPrimFloat:
2650 case Primitive::kPrimDouble: {
2651 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2652 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2653 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2654 if (type == Primitive::kPrimFloat) {
2655 __ DivS(dst, lhs, rhs);
2656 } else {
2657 __ DivD(dst, lhs, rhs);
2658 }
2659 break;
2660 }
2661 default:
2662 LOG(FATAL) << "Unexpected div type " << type;
2663 }
2664}
2665
2666void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2667 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2668 ? LocationSummary::kCallOnSlowPath
2669 : LocationSummary::kNoCall;
2670 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2671 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2672 if (instruction->HasUses()) {
2673 locations->SetOut(Location::SameAsFirstInput());
2674 }
2675}
2676
2677void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2678 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2679 codegen_->AddSlowPath(slow_path);
2680 Location value = instruction->GetLocations()->InAt(0);
2681 Primitive::Type type = instruction->GetType();
2682
2683 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002684 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002685 case Primitive::kPrimByte:
2686 case Primitive::kPrimChar:
2687 case Primitive::kPrimShort:
2688 case Primitive::kPrimInt: {
2689 if (value.IsConstant()) {
2690 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2691 __ B(slow_path->GetEntryLabel());
2692 } else {
2693 // A division by a non-null constant is valid. We don't need to perform
2694 // any check, so simply fall through.
2695 }
2696 } else {
2697 DCHECK(value.IsRegister()) << value;
2698 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2699 }
2700 break;
2701 }
2702 case Primitive::kPrimLong: {
2703 if (value.IsConstant()) {
2704 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2705 __ B(slow_path->GetEntryLabel());
2706 } else {
2707 // A division by a non-null constant is valid. We don't need to perform
2708 // any check, so simply fall through.
2709 }
2710 } else {
2711 DCHECK(value.IsRegisterPair()) << value;
2712 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2713 __ Beqz(TMP, slow_path->GetEntryLabel());
2714 }
2715 break;
2716 }
2717 default:
2718 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2719 }
2720}
2721
2722void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2723 LocationSummary* locations =
2724 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2725 locations->SetOut(Location::ConstantLocation(constant));
2726}
2727
2728void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2729 // Will be generated at use site.
2730}
2731
2732void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2733 exit->SetLocations(nullptr);
2734}
2735
2736void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2737}
2738
2739void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2740 LocationSummary* locations =
2741 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2742 locations->SetOut(Location::ConstantLocation(constant));
2743}
2744
2745void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2746 // Will be generated at use site.
2747}
2748
2749void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2750 got->SetLocations(nullptr);
2751}
2752
2753void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2754 DCHECK(!successor->IsExitBlock());
2755 HBasicBlock* block = got->GetBlock();
2756 HInstruction* previous = got->GetPrevious();
2757 HLoopInformation* info = block->GetLoopInformation();
2758
2759 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2760 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2761 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2762 return;
2763 }
2764 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2765 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2766 }
2767 if (!codegen_->GoesToNextBlock(block, successor)) {
2768 __ B(codegen_->GetLabelOf(successor));
2769 }
2770}
2771
2772void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2773 HandleGoto(got, got->GetSuccessor());
2774}
2775
2776void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2777 try_boundary->SetLocations(nullptr);
2778}
2779
2780void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2781 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2782 if (!successor->IsExitBlock()) {
2783 HandleGoto(try_boundary, successor);
2784 }
2785}
2786
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002787void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2788 LocationSummary* locations) {
2789 Register dst = locations->Out().AsRegister<Register>();
2790 Register lhs = locations->InAt(0).AsRegister<Register>();
2791 Location rhs_location = locations->InAt(1);
2792 Register rhs_reg = ZERO;
2793 int64_t rhs_imm = 0;
2794 bool use_imm = rhs_location.IsConstant();
2795 if (use_imm) {
2796 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2797 } else {
2798 rhs_reg = rhs_location.AsRegister<Register>();
2799 }
2800
2801 switch (cond) {
2802 case kCondEQ:
2803 case kCondNE:
2804 if (use_imm && IsUint<16>(rhs_imm)) {
2805 __ Xori(dst, lhs, rhs_imm);
2806 } else {
2807 if (use_imm) {
2808 rhs_reg = TMP;
2809 __ LoadConst32(rhs_reg, rhs_imm);
2810 }
2811 __ Xor(dst, lhs, rhs_reg);
2812 }
2813 if (cond == kCondEQ) {
2814 __ Sltiu(dst, dst, 1);
2815 } else {
2816 __ Sltu(dst, ZERO, dst);
2817 }
2818 break;
2819
2820 case kCondLT:
2821 case kCondGE:
2822 if (use_imm && IsInt<16>(rhs_imm)) {
2823 __ Slti(dst, lhs, rhs_imm);
2824 } else {
2825 if (use_imm) {
2826 rhs_reg = TMP;
2827 __ LoadConst32(rhs_reg, rhs_imm);
2828 }
2829 __ Slt(dst, lhs, rhs_reg);
2830 }
2831 if (cond == kCondGE) {
2832 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2833 // only the slt instruction but no sge.
2834 __ Xori(dst, dst, 1);
2835 }
2836 break;
2837
2838 case kCondLE:
2839 case kCondGT:
2840 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2841 // Simulate lhs <= rhs via lhs < rhs + 1.
2842 __ Slti(dst, lhs, rhs_imm + 1);
2843 if (cond == kCondGT) {
2844 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2845 // only the slti instruction but no sgti.
2846 __ Xori(dst, dst, 1);
2847 }
2848 } else {
2849 if (use_imm) {
2850 rhs_reg = TMP;
2851 __ LoadConst32(rhs_reg, rhs_imm);
2852 }
2853 __ Slt(dst, rhs_reg, lhs);
2854 if (cond == kCondLE) {
2855 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2856 // only the slt instruction but no sle.
2857 __ Xori(dst, dst, 1);
2858 }
2859 }
2860 break;
2861
2862 case kCondB:
2863 case kCondAE:
2864 if (use_imm && IsInt<16>(rhs_imm)) {
2865 // Sltiu sign-extends its 16-bit immediate operand before
2866 // the comparison and thus lets us compare directly with
2867 // unsigned values in the ranges [0, 0x7fff] and
2868 // [0xffff8000, 0xffffffff].
2869 __ Sltiu(dst, lhs, rhs_imm);
2870 } else {
2871 if (use_imm) {
2872 rhs_reg = TMP;
2873 __ LoadConst32(rhs_reg, rhs_imm);
2874 }
2875 __ Sltu(dst, lhs, rhs_reg);
2876 }
2877 if (cond == kCondAE) {
2878 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2879 // only the sltu instruction but no sgeu.
2880 __ Xori(dst, dst, 1);
2881 }
2882 break;
2883
2884 case kCondBE:
2885 case kCondA:
2886 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2887 // Simulate lhs <= rhs via lhs < rhs + 1.
2888 // Note that this only works if rhs + 1 does not overflow
2889 // to 0, hence the check above.
2890 // Sltiu sign-extends its 16-bit immediate operand before
2891 // the comparison and thus lets us compare directly with
2892 // unsigned values in the ranges [0, 0x7fff] and
2893 // [0xffff8000, 0xffffffff].
2894 __ Sltiu(dst, lhs, rhs_imm + 1);
2895 if (cond == kCondA) {
2896 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2897 // only the sltiu instruction but no sgtiu.
2898 __ Xori(dst, dst, 1);
2899 }
2900 } else {
2901 if (use_imm) {
2902 rhs_reg = TMP;
2903 __ LoadConst32(rhs_reg, rhs_imm);
2904 }
2905 __ Sltu(dst, rhs_reg, lhs);
2906 if (cond == kCondBE) {
2907 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2908 // only the sltu instruction but no sleu.
2909 __ Xori(dst, dst, 1);
2910 }
2911 }
2912 break;
2913 }
2914}
2915
2916void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2917 LocationSummary* locations,
2918 MipsLabel* label) {
2919 Register lhs = locations->InAt(0).AsRegister<Register>();
2920 Location rhs_location = locations->InAt(1);
2921 Register rhs_reg = ZERO;
2922 int32_t rhs_imm = 0;
2923 bool use_imm = rhs_location.IsConstant();
2924 if (use_imm) {
2925 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2926 } else {
2927 rhs_reg = rhs_location.AsRegister<Register>();
2928 }
2929
2930 if (use_imm && rhs_imm == 0) {
2931 switch (cond) {
2932 case kCondEQ:
2933 case kCondBE: // <= 0 if zero
2934 __ Beqz(lhs, label);
2935 break;
2936 case kCondNE:
2937 case kCondA: // > 0 if non-zero
2938 __ Bnez(lhs, label);
2939 break;
2940 case kCondLT:
2941 __ Bltz(lhs, label);
2942 break;
2943 case kCondGE:
2944 __ Bgez(lhs, label);
2945 break;
2946 case kCondLE:
2947 __ Blez(lhs, label);
2948 break;
2949 case kCondGT:
2950 __ Bgtz(lhs, label);
2951 break;
2952 case kCondB: // always false
2953 break;
2954 case kCondAE: // always true
2955 __ B(label);
2956 break;
2957 }
2958 } else {
2959 if (use_imm) {
2960 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2961 rhs_reg = TMP;
2962 __ LoadConst32(rhs_reg, rhs_imm);
2963 }
2964 switch (cond) {
2965 case kCondEQ:
2966 __ Beq(lhs, rhs_reg, label);
2967 break;
2968 case kCondNE:
2969 __ Bne(lhs, rhs_reg, label);
2970 break;
2971 case kCondLT:
2972 __ Blt(lhs, rhs_reg, label);
2973 break;
2974 case kCondGE:
2975 __ Bge(lhs, rhs_reg, label);
2976 break;
2977 case kCondLE:
2978 __ Bge(rhs_reg, lhs, label);
2979 break;
2980 case kCondGT:
2981 __ Blt(rhs_reg, lhs, label);
2982 break;
2983 case kCondB:
2984 __ Bltu(lhs, rhs_reg, label);
2985 break;
2986 case kCondAE:
2987 __ Bgeu(lhs, rhs_reg, label);
2988 break;
2989 case kCondBE:
2990 __ Bgeu(rhs_reg, lhs, label);
2991 break;
2992 case kCondA:
2993 __ Bltu(rhs_reg, lhs, label);
2994 break;
2995 }
2996 }
2997}
2998
2999void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3000 LocationSummary* locations,
3001 MipsLabel* label) {
3002 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3003 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3004 Location rhs_location = locations->InAt(1);
3005 Register rhs_high = ZERO;
3006 Register rhs_low = ZERO;
3007 int64_t imm = 0;
3008 uint32_t imm_high = 0;
3009 uint32_t imm_low = 0;
3010 bool use_imm = rhs_location.IsConstant();
3011 if (use_imm) {
3012 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3013 imm_high = High32Bits(imm);
3014 imm_low = Low32Bits(imm);
3015 } else {
3016 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3017 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3018 }
3019
3020 if (use_imm && imm == 0) {
3021 switch (cond) {
3022 case kCondEQ:
3023 case kCondBE: // <= 0 if zero
3024 __ Or(TMP, lhs_high, lhs_low);
3025 __ Beqz(TMP, label);
3026 break;
3027 case kCondNE:
3028 case kCondA: // > 0 if non-zero
3029 __ Or(TMP, lhs_high, lhs_low);
3030 __ Bnez(TMP, label);
3031 break;
3032 case kCondLT:
3033 __ Bltz(lhs_high, label);
3034 break;
3035 case kCondGE:
3036 __ Bgez(lhs_high, label);
3037 break;
3038 case kCondLE:
3039 __ Or(TMP, lhs_high, lhs_low);
3040 __ Sra(AT, lhs_high, 31);
3041 __ Bgeu(AT, TMP, label);
3042 break;
3043 case kCondGT:
3044 __ Or(TMP, lhs_high, lhs_low);
3045 __ Sra(AT, lhs_high, 31);
3046 __ Bltu(AT, TMP, label);
3047 break;
3048 case kCondB: // always false
3049 break;
3050 case kCondAE: // always true
3051 __ B(label);
3052 break;
3053 }
3054 } else if (use_imm) {
3055 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3056 switch (cond) {
3057 case kCondEQ:
3058 __ LoadConst32(TMP, imm_high);
3059 __ Xor(TMP, TMP, lhs_high);
3060 __ LoadConst32(AT, imm_low);
3061 __ Xor(AT, AT, lhs_low);
3062 __ Or(TMP, TMP, AT);
3063 __ Beqz(TMP, label);
3064 break;
3065 case kCondNE:
3066 __ LoadConst32(TMP, imm_high);
3067 __ Xor(TMP, TMP, lhs_high);
3068 __ LoadConst32(AT, imm_low);
3069 __ Xor(AT, AT, lhs_low);
3070 __ Or(TMP, TMP, AT);
3071 __ Bnez(TMP, label);
3072 break;
3073 case kCondLT:
3074 __ LoadConst32(TMP, imm_high);
3075 __ Blt(lhs_high, TMP, label);
3076 __ Slt(TMP, TMP, lhs_high);
3077 __ LoadConst32(AT, imm_low);
3078 __ Sltu(AT, lhs_low, AT);
3079 __ Blt(TMP, AT, label);
3080 break;
3081 case kCondGE:
3082 __ LoadConst32(TMP, imm_high);
3083 __ Blt(TMP, lhs_high, label);
3084 __ Slt(TMP, lhs_high, TMP);
3085 __ LoadConst32(AT, imm_low);
3086 __ Sltu(AT, lhs_low, AT);
3087 __ Or(TMP, TMP, AT);
3088 __ Beqz(TMP, label);
3089 break;
3090 case kCondLE:
3091 __ LoadConst32(TMP, imm_high);
3092 __ Blt(lhs_high, TMP, label);
3093 __ Slt(TMP, TMP, lhs_high);
3094 __ LoadConst32(AT, imm_low);
3095 __ Sltu(AT, AT, lhs_low);
3096 __ Or(TMP, TMP, AT);
3097 __ Beqz(TMP, label);
3098 break;
3099 case kCondGT:
3100 __ LoadConst32(TMP, imm_high);
3101 __ Blt(TMP, lhs_high, label);
3102 __ Slt(TMP, lhs_high, TMP);
3103 __ LoadConst32(AT, imm_low);
3104 __ Sltu(AT, AT, lhs_low);
3105 __ Blt(TMP, AT, label);
3106 break;
3107 case kCondB:
3108 __ LoadConst32(TMP, imm_high);
3109 __ Bltu(lhs_high, TMP, label);
3110 __ Sltu(TMP, TMP, lhs_high);
3111 __ LoadConst32(AT, imm_low);
3112 __ Sltu(AT, lhs_low, AT);
3113 __ Blt(TMP, AT, label);
3114 break;
3115 case kCondAE:
3116 __ LoadConst32(TMP, imm_high);
3117 __ Bltu(TMP, lhs_high, label);
3118 __ Sltu(TMP, lhs_high, TMP);
3119 __ LoadConst32(AT, imm_low);
3120 __ Sltu(AT, lhs_low, AT);
3121 __ Or(TMP, TMP, AT);
3122 __ Beqz(TMP, label);
3123 break;
3124 case kCondBE:
3125 __ LoadConst32(TMP, imm_high);
3126 __ Bltu(lhs_high, TMP, label);
3127 __ Sltu(TMP, TMP, lhs_high);
3128 __ LoadConst32(AT, imm_low);
3129 __ Sltu(AT, AT, lhs_low);
3130 __ Or(TMP, TMP, AT);
3131 __ Beqz(TMP, label);
3132 break;
3133 case kCondA:
3134 __ LoadConst32(TMP, imm_high);
3135 __ Bltu(TMP, lhs_high, label);
3136 __ Sltu(TMP, lhs_high, TMP);
3137 __ LoadConst32(AT, imm_low);
3138 __ Sltu(AT, AT, lhs_low);
3139 __ Blt(TMP, AT, label);
3140 break;
3141 }
3142 } else {
3143 switch (cond) {
3144 case kCondEQ:
3145 __ Xor(TMP, lhs_high, rhs_high);
3146 __ Xor(AT, lhs_low, rhs_low);
3147 __ Or(TMP, TMP, AT);
3148 __ Beqz(TMP, label);
3149 break;
3150 case kCondNE:
3151 __ Xor(TMP, lhs_high, rhs_high);
3152 __ Xor(AT, lhs_low, rhs_low);
3153 __ Or(TMP, TMP, AT);
3154 __ Bnez(TMP, label);
3155 break;
3156 case kCondLT:
3157 __ Blt(lhs_high, rhs_high, label);
3158 __ Slt(TMP, rhs_high, lhs_high);
3159 __ Sltu(AT, lhs_low, rhs_low);
3160 __ Blt(TMP, AT, label);
3161 break;
3162 case kCondGE:
3163 __ Blt(rhs_high, lhs_high, label);
3164 __ Slt(TMP, lhs_high, rhs_high);
3165 __ Sltu(AT, lhs_low, rhs_low);
3166 __ Or(TMP, TMP, AT);
3167 __ Beqz(TMP, label);
3168 break;
3169 case kCondLE:
3170 __ Blt(lhs_high, rhs_high, label);
3171 __ Slt(TMP, rhs_high, lhs_high);
3172 __ Sltu(AT, rhs_low, lhs_low);
3173 __ Or(TMP, TMP, AT);
3174 __ Beqz(TMP, label);
3175 break;
3176 case kCondGT:
3177 __ Blt(rhs_high, lhs_high, label);
3178 __ Slt(TMP, lhs_high, rhs_high);
3179 __ Sltu(AT, rhs_low, lhs_low);
3180 __ Blt(TMP, AT, label);
3181 break;
3182 case kCondB:
3183 __ Bltu(lhs_high, rhs_high, label);
3184 __ Sltu(TMP, rhs_high, lhs_high);
3185 __ Sltu(AT, lhs_low, rhs_low);
3186 __ Blt(TMP, AT, label);
3187 break;
3188 case kCondAE:
3189 __ Bltu(rhs_high, lhs_high, label);
3190 __ Sltu(TMP, lhs_high, rhs_high);
3191 __ Sltu(AT, lhs_low, rhs_low);
3192 __ Or(TMP, TMP, AT);
3193 __ Beqz(TMP, label);
3194 break;
3195 case kCondBE:
3196 __ Bltu(lhs_high, rhs_high, label);
3197 __ Sltu(TMP, rhs_high, lhs_high);
3198 __ Sltu(AT, rhs_low, lhs_low);
3199 __ Or(TMP, TMP, AT);
3200 __ Beqz(TMP, label);
3201 break;
3202 case kCondA:
3203 __ Bltu(rhs_high, lhs_high, label);
3204 __ Sltu(TMP, lhs_high, rhs_high);
3205 __ Sltu(AT, rhs_low, lhs_low);
3206 __ Blt(TMP, AT, label);
3207 break;
3208 }
3209 }
3210}
3211
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003212void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3213 bool gt_bias,
3214 Primitive::Type type,
3215 LocationSummary* locations) {
3216 Register dst = locations->Out().AsRegister<Register>();
3217 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3218 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3219 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3220 if (type == Primitive::kPrimFloat) {
3221 if (isR6) {
3222 switch (cond) {
3223 case kCondEQ:
3224 __ CmpEqS(FTMP, lhs, rhs);
3225 __ Mfc1(dst, FTMP);
3226 __ Andi(dst, dst, 1);
3227 break;
3228 case kCondNE:
3229 __ CmpEqS(FTMP, lhs, rhs);
3230 __ Mfc1(dst, FTMP);
3231 __ Addiu(dst, dst, 1);
3232 break;
3233 case kCondLT:
3234 if (gt_bias) {
3235 __ CmpLtS(FTMP, lhs, rhs);
3236 } else {
3237 __ CmpUltS(FTMP, lhs, rhs);
3238 }
3239 __ Mfc1(dst, FTMP);
3240 __ Andi(dst, dst, 1);
3241 break;
3242 case kCondLE:
3243 if (gt_bias) {
3244 __ CmpLeS(FTMP, lhs, rhs);
3245 } else {
3246 __ CmpUleS(FTMP, lhs, rhs);
3247 }
3248 __ Mfc1(dst, FTMP);
3249 __ Andi(dst, dst, 1);
3250 break;
3251 case kCondGT:
3252 if (gt_bias) {
3253 __ CmpUltS(FTMP, rhs, lhs);
3254 } else {
3255 __ CmpLtS(FTMP, rhs, lhs);
3256 }
3257 __ Mfc1(dst, FTMP);
3258 __ Andi(dst, dst, 1);
3259 break;
3260 case kCondGE:
3261 if (gt_bias) {
3262 __ CmpUleS(FTMP, rhs, lhs);
3263 } else {
3264 __ CmpLeS(FTMP, rhs, lhs);
3265 }
3266 __ Mfc1(dst, FTMP);
3267 __ Andi(dst, dst, 1);
3268 break;
3269 default:
3270 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3271 UNREACHABLE();
3272 }
3273 } else {
3274 switch (cond) {
3275 case kCondEQ:
3276 __ CeqS(0, lhs, rhs);
3277 __ LoadConst32(dst, 1);
3278 __ Movf(dst, ZERO, 0);
3279 break;
3280 case kCondNE:
3281 __ CeqS(0, lhs, rhs);
3282 __ LoadConst32(dst, 1);
3283 __ Movt(dst, ZERO, 0);
3284 break;
3285 case kCondLT:
3286 if (gt_bias) {
3287 __ ColtS(0, lhs, rhs);
3288 } else {
3289 __ CultS(0, lhs, rhs);
3290 }
3291 __ LoadConst32(dst, 1);
3292 __ Movf(dst, ZERO, 0);
3293 break;
3294 case kCondLE:
3295 if (gt_bias) {
3296 __ ColeS(0, lhs, rhs);
3297 } else {
3298 __ CuleS(0, lhs, rhs);
3299 }
3300 __ LoadConst32(dst, 1);
3301 __ Movf(dst, ZERO, 0);
3302 break;
3303 case kCondGT:
3304 if (gt_bias) {
3305 __ CultS(0, rhs, lhs);
3306 } else {
3307 __ ColtS(0, rhs, lhs);
3308 }
3309 __ LoadConst32(dst, 1);
3310 __ Movf(dst, ZERO, 0);
3311 break;
3312 case kCondGE:
3313 if (gt_bias) {
3314 __ CuleS(0, rhs, lhs);
3315 } else {
3316 __ ColeS(0, rhs, lhs);
3317 }
3318 __ LoadConst32(dst, 1);
3319 __ Movf(dst, ZERO, 0);
3320 break;
3321 default:
3322 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3323 UNREACHABLE();
3324 }
3325 }
3326 } else {
3327 DCHECK_EQ(type, Primitive::kPrimDouble);
3328 if (isR6) {
3329 switch (cond) {
3330 case kCondEQ:
3331 __ CmpEqD(FTMP, lhs, rhs);
3332 __ Mfc1(dst, FTMP);
3333 __ Andi(dst, dst, 1);
3334 break;
3335 case kCondNE:
3336 __ CmpEqD(FTMP, lhs, rhs);
3337 __ Mfc1(dst, FTMP);
3338 __ Addiu(dst, dst, 1);
3339 break;
3340 case kCondLT:
3341 if (gt_bias) {
3342 __ CmpLtD(FTMP, lhs, rhs);
3343 } else {
3344 __ CmpUltD(FTMP, lhs, rhs);
3345 }
3346 __ Mfc1(dst, FTMP);
3347 __ Andi(dst, dst, 1);
3348 break;
3349 case kCondLE:
3350 if (gt_bias) {
3351 __ CmpLeD(FTMP, lhs, rhs);
3352 } else {
3353 __ CmpUleD(FTMP, lhs, rhs);
3354 }
3355 __ Mfc1(dst, FTMP);
3356 __ Andi(dst, dst, 1);
3357 break;
3358 case kCondGT:
3359 if (gt_bias) {
3360 __ CmpUltD(FTMP, rhs, lhs);
3361 } else {
3362 __ CmpLtD(FTMP, rhs, lhs);
3363 }
3364 __ Mfc1(dst, FTMP);
3365 __ Andi(dst, dst, 1);
3366 break;
3367 case kCondGE:
3368 if (gt_bias) {
3369 __ CmpUleD(FTMP, rhs, lhs);
3370 } else {
3371 __ CmpLeD(FTMP, rhs, lhs);
3372 }
3373 __ Mfc1(dst, FTMP);
3374 __ Andi(dst, dst, 1);
3375 break;
3376 default:
3377 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3378 UNREACHABLE();
3379 }
3380 } else {
3381 switch (cond) {
3382 case kCondEQ:
3383 __ CeqD(0, lhs, rhs);
3384 __ LoadConst32(dst, 1);
3385 __ Movf(dst, ZERO, 0);
3386 break;
3387 case kCondNE:
3388 __ CeqD(0, lhs, rhs);
3389 __ LoadConst32(dst, 1);
3390 __ Movt(dst, ZERO, 0);
3391 break;
3392 case kCondLT:
3393 if (gt_bias) {
3394 __ ColtD(0, lhs, rhs);
3395 } else {
3396 __ CultD(0, lhs, rhs);
3397 }
3398 __ LoadConst32(dst, 1);
3399 __ Movf(dst, ZERO, 0);
3400 break;
3401 case kCondLE:
3402 if (gt_bias) {
3403 __ ColeD(0, lhs, rhs);
3404 } else {
3405 __ CuleD(0, lhs, rhs);
3406 }
3407 __ LoadConst32(dst, 1);
3408 __ Movf(dst, ZERO, 0);
3409 break;
3410 case kCondGT:
3411 if (gt_bias) {
3412 __ CultD(0, rhs, lhs);
3413 } else {
3414 __ ColtD(0, rhs, lhs);
3415 }
3416 __ LoadConst32(dst, 1);
3417 __ Movf(dst, ZERO, 0);
3418 break;
3419 case kCondGE:
3420 if (gt_bias) {
3421 __ CuleD(0, rhs, lhs);
3422 } else {
3423 __ ColeD(0, rhs, lhs);
3424 }
3425 __ LoadConst32(dst, 1);
3426 __ Movf(dst, ZERO, 0);
3427 break;
3428 default:
3429 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3430 UNREACHABLE();
3431 }
3432 }
3433 }
3434}
3435
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003436void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3437 bool gt_bias,
3438 Primitive::Type type,
3439 LocationSummary* locations,
3440 MipsLabel* label) {
3441 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3442 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3443 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3444 if (type == Primitive::kPrimFloat) {
3445 if (isR6) {
3446 switch (cond) {
3447 case kCondEQ:
3448 __ CmpEqS(FTMP, lhs, rhs);
3449 __ Bc1nez(FTMP, label);
3450 break;
3451 case kCondNE:
3452 __ CmpEqS(FTMP, lhs, rhs);
3453 __ Bc1eqz(FTMP, label);
3454 break;
3455 case kCondLT:
3456 if (gt_bias) {
3457 __ CmpLtS(FTMP, lhs, rhs);
3458 } else {
3459 __ CmpUltS(FTMP, lhs, rhs);
3460 }
3461 __ Bc1nez(FTMP, label);
3462 break;
3463 case kCondLE:
3464 if (gt_bias) {
3465 __ CmpLeS(FTMP, lhs, rhs);
3466 } else {
3467 __ CmpUleS(FTMP, lhs, rhs);
3468 }
3469 __ Bc1nez(FTMP, label);
3470 break;
3471 case kCondGT:
3472 if (gt_bias) {
3473 __ CmpUltS(FTMP, rhs, lhs);
3474 } else {
3475 __ CmpLtS(FTMP, rhs, lhs);
3476 }
3477 __ Bc1nez(FTMP, label);
3478 break;
3479 case kCondGE:
3480 if (gt_bias) {
3481 __ CmpUleS(FTMP, rhs, lhs);
3482 } else {
3483 __ CmpLeS(FTMP, rhs, lhs);
3484 }
3485 __ Bc1nez(FTMP, label);
3486 break;
3487 default:
3488 LOG(FATAL) << "Unexpected non-floating-point condition";
3489 }
3490 } else {
3491 switch (cond) {
3492 case kCondEQ:
3493 __ CeqS(0, lhs, rhs);
3494 __ Bc1t(0, label);
3495 break;
3496 case kCondNE:
3497 __ CeqS(0, lhs, rhs);
3498 __ Bc1f(0, label);
3499 break;
3500 case kCondLT:
3501 if (gt_bias) {
3502 __ ColtS(0, lhs, rhs);
3503 } else {
3504 __ CultS(0, lhs, rhs);
3505 }
3506 __ Bc1t(0, label);
3507 break;
3508 case kCondLE:
3509 if (gt_bias) {
3510 __ ColeS(0, lhs, rhs);
3511 } else {
3512 __ CuleS(0, lhs, rhs);
3513 }
3514 __ Bc1t(0, label);
3515 break;
3516 case kCondGT:
3517 if (gt_bias) {
3518 __ CultS(0, rhs, lhs);
3519 } else {
3520 __ ColtS(0, rhs, lhs);
3521 }
3522 __ Bc1t(0, label);
3523 break;
3524 case kCondGE:
3525 if (gt_bias) {
3526 __ CuleS(0, rhs, lhs);
3527 } else {
3528 __ ColeS(0, rhs, lhs);
3529 }
3530 __ Bc1t(0, label);
3531 break;
3532 default:
3533 LOG(FATAL) << "Unexpected non-floating-point condition";
3534 }
3535 }
3536 } else {
3537 DCHECK_EQ(type, Primitive::kPrimDouble);
3538 if (isR6) {
3539 switch (cond) {
3540 case kCondEQ:
3541 __ CmpEqD(FTMP, lhs, rhs);
3542 __ Bc1nez(FTMP, label);
3543 break;
3544 case kCondNE:
3545 __ CmpEqD(FTMP, lhs, rhs);
3546 __ Bc1eqz(FTMP, label);
3547 break;
3548 case kCondLT:
3549 if (gt_bias) {
3550 __ CmpLtD(FTMP, lhs, rhs);
3551 } else {
3552 __ CmpUltD(FTMP, lhs, rhs);
3553 }
3554 __ Bc1nez(FTMP, label);
3555 break;
3556 case kCondLE:
3557 if (gt_bias) {
3558 __ CmpLeD(FTMP, lhs, rhs);
3559 } else {
3560 __ CmpUleD(FTMP, lhs, rhs);
3561 }
3562 __ Bc1nez(FTMP, label);
3563 break;
3564 case kCondGT:
3565 if (gt_bias) {
3566 __ CmpUltD(FTMP, rhs, lhs);
3567 } else {
3568 __ CmpLtD(FTMP, rhs, lhs);
3569 }
3570 __ Bc1nez(FTMP, label);
3571 break;
3572 case kCondGE:
3573 if (gt_bias) {
3574 __ CmpUleD(FTMP, rhs, lhs);
3575 } else {
3576 __ CmpLeD(FTMP, rhs, lhs);
3577 }
3578 __ Bc1nez(FTMP, label);
3579 break;
3580 default:
3581 LOG(FATAL) << "Unexpected non-floating-point condition";
3582 }
3583 } else {
3584 switch (cond) {
3585 case kCondEQ:
3586 __ CeqD(0, lhs, rhs);
3587 __ Bc1t(0, label);
3588 break;
3589 case kCondNE:
3590 __ CeqD(0, lhs, rhs);
3591 __ Bc1f(0, label);
3592 break;
3593 case kCondLT:
3594 if (gt_bias) {
3595 __ ColtD(0, lhs, rhs);
3596 } else {
3597 __ CultD(0, lhs, rhs);
3598 }
3599 __ Bc1t(0, label);
3600 break;
3601 case kCondLE:
3602 if (gt_bias) {
3603 __ ColeD(0, lhs, rhs);
3604 } else {
3605 __ CuleD(0, lhs, rhs);
3606 }
3607 __ Bc1t(0, label);
3608 break;
3609 case kCondGT:
3610 if (gt_bias) {
3611 __ CultD(0, rhs, lhs);
3612 } else {
3613 __ ColtD(0, rhs, lhs);
3614 }
3615 __ Bc1t(0, label);
3616 break;
3617 case kCondGE:
3618 if (gt_bias) {
3619 __ CuleD(0, rhs, lhs);
3620 } else {
3621 __ ColeD(0, rhs, lhs);
3622 }
3623 __ Bc1t(0, label);
3624 break;
3625 default:
3626 LOG(FATAL) << "Unexpected non-floating-point condition";
3627 }
3628 }
3629 }
3630}
3631
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003632void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003633 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003634 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003635 MipsLabel* false_target) {
3636 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003637
David Brazdil0debae72015-11-12 18:37:00 +00003638 if (true_target == nullptr && false_target == nullptr) {
3639 // Nothing to do. The code always falls through.
3640 return;
3641 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003642 // Constant condition, statically compared against "true" (integer value 1).
3643 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003644 if (true_target != nullptr) {
3645 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003646 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003647 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003648 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003649 if (false_target != nullptr) {
3650 __ B(false_target);
3651 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003652 }
David Brazdil0debae72015-11-12 18:37:00 +00003653 return;
3654 }
3655
3656 // The following code generates these patterns:
3657 // (1) true_target == nullptr && false_target != nullptr
3658 // - opposite condition true => branch to false_target
3659 // (2) true_target != nullptr && false_target == nullptr
3660 // - condition true => branch to true_target
3661 // (3) true_target != nullptr && false_target != nullptr
3662 // - condition true => branch to true_target
3663 // - branch to false_target
3664 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003665 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003666 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003667 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003668 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003669 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3670 } else {
3671 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3672 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003673 } else {
3674 // The condition instruction has not been materialized, use its inputs as
3675 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003676 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003677 Primitive::Type type = condition->InputAt(0)->GetType();
3678 LocationSummary* locations = cond->GetLocations();
3679 IfCondition if_cond = condition->GetCondition();
3680 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003681
David Brazdil0debae72015-11-12 18:37:00 +00003682 if (true_target == nullptr) {
3683 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003684 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003685 }
3686
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003687 switch (type) {
3688 default:
3689 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3690 break;
3691 case Primitive::kPrimLong:
3692 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3693 break;
3694 case Primitive::kPrimFloat:
3695 case Primitive::kPrimDouble:
3696 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3697 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003698 }
3699 }
David Brazdil0debae72015-11-12 18:37:00 +00003700
3701 // If neither branch falls through (case 3), the conditional branch to `true_target`
3702 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3703 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704 __ B(false_target);
3705 }
3706}
3707
3708void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003710 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003711 locations->SetInAt(0, Location::RequiresRegister());
3712 }
3713}
3714
3715void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003716 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3717 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3718 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3719 nullptr : codegen_->GetLabelOf(true_successor);
3720 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3721 nullptr : codegen_->GetLabelOf(false_successor);
3722 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003723}
3724
3725void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3726 LocationSummary* locations = new (GetGraph()->GetArena())
3727 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01003728 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003729 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003730 locations->SetInAt(0, Location::RequiresRegister());
3731 }
3732}
3733
3734void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003735 SlowPathCodeMIPS* slow_path =
3736 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003737 GenerateTestAndBranch(deoptimize,
3738 /* condition_input_index */ 0,
3739 slow_path->GetEntryLabel(),
3740 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003741}
3742
David Brazdil74eb1b22015-12-14 11:44:01 +00003743void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3744 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3745 if (Primitive::IsFloatingPointType(select->GetType())) {
3746 locations->SetInAt(0, Location::RequiresFpuRegister());
3747 locations->SetInAt(1, Location::RequiresFpuRegister());
3748 } else {
3749 locations->SetInAt(0, Location::RequiresRegister());
3750 locations->SetInAt(1, Location::RequiresRegister());
3751 }
3752 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3753 locations->SetInAt(2, Location::RequiresRegister());
3754 }
3755 locations->SetOut(Location::SameAsFirstInput());
3756}
3757
3758void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3759 LocationSummary* locations = select->GetLocations();
3760 MipsLabel false_target;
3761 GenerateTestAndBranch(select,
3762 /* condition_input_index */ 2,
3763 /* true_target */ nullptr,
3764 &false_target);
3765 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3766 __ Bind(&false_target);
3767}
3768
David Srbecky0cf44932015-12-09 14:09:59 +00003769void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3770 new (GetGraph()->GetArena()) LocationSummary(info);
3771}
3772
David Srbeckyd28f4a02016-03-14 17:14:24 +00003773void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3774 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003775}
3776
3777void CodeGeneratorMIPS::GenerateNop() {
3778 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003779}
3780
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003781void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3782 Primitive::Type field_type = field_info.GetFieldType();
3783 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3784 bool generate_volatile = field_info.IsVolatile() && is_wide;
3785 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003786 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003787
3788 locations->SetInAt(0, Location::RequiresRegister());
3789 if (generate_volatile) {
3790 InvokeRuntimeCallingConvention calling_convention;
3791 // need A0 to hold base + offset
3792 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3793 if (field_type == Primitive::kPrimLong) {
3794 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3795 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003796 // Use Location::Any() to prevent situations when running out of available fp registers.
3797 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003798 // Need some temp core regs since FP results are returned in core registers
3799 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3800 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3801 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3802 }
3803 } else {
3804 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3805 locations->SetOut(Location::RequiresFpuRegister());
3806 } else {
3807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3808 }
3809 }
3810}
3811
3812void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3813 const FieldInfo& field_info,
3814 uint32_t dex_pc) {
3815 Primitive::Type type = field_info.GetFieldType();
3816 LocationSummary* locations = instruction->GetLocations();
3817 Register obj = locations->InAt(0).AsRegister<Register>();
3818 LoadOperandType load_type = kLoadUnsignedByte;
3819 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003820 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003821 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003822
3823 switch (type) {
3824 case Primitive::kPrimBoolean:
3825 load_type = kLoadUnsignedByte;
3826 break;
3827 case Primitive::kPrimByte:
3828 load_type = kLoadSignedByte;
3829 break;
3830 case Primitive::kPrimShort:
3831 load_type = kLoadSignedHalfword;
3832 break;
3833 case Primitive::kPrimChar:
3834 load_type = kLoadUnsignedHalfword;
3835 break;
3836 case Primitive::kPrimInt:
3837 case Primitive::kPrimFloat:
3838 case Primitive::kPrimNot:
3839 load_type = kLoadWord;
3840 break;
3841 case Primitive::kPrimLong:
3842 case Primitive::kPrimDouble:
3843 load_type = kLoadDoubleword;
3844 break;
3845 case Primitive::kPrimVoid:
3846 LOG(FATAL) << "Unreachable type " << type;
3847 UNREACHABLE();
3848 }
3849
3850 if (is_volatile && load_type == kLoadDoubleword) {
3851 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003852 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853 // Do implicit Null check
3854 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3855 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01003856 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003857 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3858 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003859 // FP results are returned in core registers. Need to move them.
3860 Location out = locations->Out();
3861 if (out.IsFpuRegister()) {
3862 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3863 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3864 out.AsFpuRegister<FRegister>());
3865 } else {
3866 DCHECK(out.IsDoubleStackSlot());
3867 __ StoreToOffset(kStoreWord,
3868 locations->GetTemp(1).AsRegister<Register>(),
3869 SP,
3870 out.GetStackIndex());
3871 __ StoreToOffset(kStoreWord,
3872 locations->GetTemp(2).AsRegister<Register>(),
3873 SP,
3874 out.GetStackIndex() + 4);
3875 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003876 }
3877 } else {
3878 if (!Primitive::IsFloatingPointType(type)) {
3879 Register dst;
3880 if (type == Primitive::kPrimLong) {
3881 DCHECK(locations->Out().IsRegisterPair());
3882 dst = locations->Out().AsRegisterPairLow<Register>();
3883 } else {
3884 DCHECK(locations->Out().IsRegister());
3885 dst = locations->Out().AsRegister<Register>();
3886 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003887 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003888 } else {
3889 DCHECK(locations->Out().IsFpuRegister());
3890 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3891 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003892 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003893 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003894 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003895 }
3896 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003897 }
3898
3899 if (is_volatile) {
3900 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3901 }
3902}
3903
3904void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3905 Primitive::Type field_type = field_info.GetFieldType();
3906 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3907 bool generate_volatile = field_info.IsVolatile() && is_wide;
3908 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003909 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003910
3911 locations->SetInAt(0, Location::RequiresRegister());
3912 if (generate_volatile) {
3913 InvokeRuntimeCallingConvention calling_convention;
3914 // need A0 to hold base + offset
3915 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3916 if (field_type == Primitive::kPrimLong) {
3917 locations->SetInAt(1, Location::RegisterPairLocation(
3918 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3919 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003920 // Use Location::Any() to prevent situations when running out of available fp registers.
3921 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003922 // Pass FP parameters in core registers.
3923 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3924 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3925 }
3926 } else {
3927 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003928 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003929 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003930 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003931 }
3932 }
3933}
3934
3935void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3936 const FieldInfo& field_info,
3937 uint32_t dex_pc) {
3938 Primitive::Type type = field_info.GetFieldType();
3939 LocationSummary* locations = instruction->GetLocations();
3940 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07003941 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003942 StoreOperandType store_type = kStoreByte;
3943 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003944 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003945 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003946
3947 switch (type) {
3948 case Primitive::kPrimBoolean:
3949 case Primitive::kPrimByte:
3950 store_type = kStoreByte;
3951 break;
3952 case Primitive::kPrimShort:
3953 case Primitive::kPrimChar:
3954 store_type = kStoreHalfword;
3955 break;
3956 case Primitive::kPrimInt:
3957 case Primitive::kPrimFloat:
3958 case Primitive::kPrimNot:
3959 store_type = kStoreWord;
3960 break;
3961 case Primitive::kPrimLong:
3962 case Primitive::kPrimDouble:
3963 store_type = kStoreDoubleword;
3964 break;
3965 case Primitive::kPrimVoid:
3966 LOG(FATAL) << "Unreachable type " << type;
3967 UNREACHABLE();
3968 }
3969
3970 if (is_volatile) {
3971 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3972 }
3973
3974 if (is_volatile && store_type == kStoreDoubleword) {
3975 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003976 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003977 // Do implicit Null check.
3978 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3979 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3980 if (type == Primitive::kPrimDouble) {
3981 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07003982 if (value_location.IsFpuRegister()) {
3983 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3984 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003985 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07003986 value_location.AsFpuRegister<FRegister>());
3987 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003988 __ LoadFromOffset(kLoadWord,
3989 locations->GetTemp(1).AsRegister<Register>(),
3990 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07003991 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003992 __ LoadFromOffset(kLoadWord,
3993 locations->GetTemp(2).AsRegister<Register>(),
3994 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07003995 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003996 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003997 DCHECK(value_location.IsConstant());
3998 DCHECK(value_location.GetConstant()->IsDoubleConstant());
3999 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004000 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4001 locations->GetTemp(1).AsRegister<Register>(),
4002 value);
4003 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004004 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004005 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004006 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4007 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004008 if (value_location.IsConstant()) {
4009 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4010 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4011 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004012 Register src;
4013 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004014 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004015 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004016 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004017 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004018 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004019 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004020 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004021 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004022 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004023 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004024 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004025 }
4026 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004027 }
4028
4029 // TODO: memory barriers?
4030 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004031 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004032 codegen_->MarkGCCard(obj, src);
4033 }
4034
4035 if (is_volatile) {
4036 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4037 }
4038}
4039
4040void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4041 HandleFieldGet(instruction, instruction->GetFieldInfo());
4042}
4043
4044void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4045 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4046}
4047
4048void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4049 HandleFieldSet(instruction, instruction->GetFieldInfo());
4050}
4051
4052void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4053 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4054}
4055
Alexey Frunze06a46c42016-07-19 15:00:40 -07004056void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
4057 HInstruction* instruction ATTRIBUTE_UNUSED,
4058 Location root,
4059 Register obj,
4060 uint32_t offset) {
4061 Register root_reg = root.AsRegister<Register>();
4062 if (kEmitCompilerReadBarrier) {
4063 UNIMPLEMENTED(FATAL) << "for read barrier";
4064 } else {
4065 // Plain GC root load with no read barrier.
4066 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4067 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
4068 // Note that GC roots are not affected by heap poisoning, thus we
4069 // do not have to unpoison `root_reg` here.
4070 }
4071}
4072
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004073void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4074 LocationSummary::CallKind call_kind =
4075 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
4076 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4077 locations->SetInAt(0, Location::RequiresRegister());
4078 locations->SetInAt(1, Location::RequiresRegister());
4079 // The output does overlap inputs.
4080 // Note that TypeCheckSlowPathMIPS uses this register too.
4081 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4082}
4083
4084void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4085 LocationSummary* locations = instruction->GetLocations();
4086 Register obj = locations->InAt(0).AsRegister<Register>();
4087 Register cls = locations->InAt(1).AsRegister<Register>();
4088 Register out = locations->Out().AsRegister<Register>();
4089
4090 MipsLabel done;
4091
4092 // Return 0 if `obj` is null.
4093 // TODO: Avoid this check if we know `obj` is not null.
4094 __ Move(out, ZERO);
4095 __ Beqz(obj, &done);
4096
4097 // Compare the class of `obj` with `cls`.
4098 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
4099 if (instruction->IsExactCheck()) {
4100 // Classes must be equal for the instanceof to succeed.
4101 __ Xor(out, out, cls);
4102 __ Sltiu(out, out, 1);
4103 } else {
4104 // If the classes are not equal, we go into a slow path.
4105 DCHECK(locations->OnlyCallsOnSlowPath());
4106 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
4107 codegen_->AddSlowPath(slow_path);
4108 __ Bne(out, cls, slow_path->GetEntryLabel());
4109 __ LoadConst32(out, 1);
4110 __ Bind(slow_path->GetExitLabel());
4111 }
4112
4113 __ Bind(&done);
4114}
4115
4116void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
4117 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4118 locations->SetOut(Location::ConstantLocation(constant));
4119}
4120
4121void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4122 // Will be generated at use site.
4123}
4124
4125void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
4126 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4127 locations->SetOut(Location::ConstantLocation(constant));
4128}
4129
4130void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4131 // Will be generated at use site.
4132}
4133
4134void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
4135 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
4136 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4137}
4138
4139void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4140 HandleInvoke(invoke);
4141 // The register T0 is required to be used for the hidden argument in
4142 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4143 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4144}
4145
4146void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4147 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4148 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004149 Location receiver = invoke->GetLocations()->InAt(0);
4150 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004151 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004152
4153 // Set the hidden argument.
4154 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
4155 invoke->GetDexMethodIndex());
4156
4157 // temp = object->GetClass();
4158 if (receiver.IsStackSlot()) {
4159 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4160 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4161 } else {
4162 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4163 }
4164 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004165 __ LoadFromOffset(kLoadWord, temp, temp,
4166 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
4167 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004168 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004169 // temp = temp->GetImtEntryAt(method_offset);
4170 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4171 // T9 = temp->GetEntryPoint();
4172 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4173 // T9();
4174 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004175 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004176 DCHECK(!codegen_->IsLeafMethod());
4177 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4178}
4179
4180void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07004181 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4182 if (intrinsic.TryDispatch(invoke)) {
4183 return;
4184 }
4185
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004186 HandleInvoke(invoke);
4187}
4188
4189void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004190 // Explicit clinit checks triggered by static invokes must have been pruned by
4191 // art::PrepareForRegisterAllocation.
4192 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004193
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004194 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4195 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4196 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4197
4198 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
4199 // R6 has PC-relative addressing.
4200 bool has_extra_input = !isR6 &&
4201 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4202 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4203
4204 if (invoke->HasPcRelativeDexCache()) {
4205 // kDexCachePcRelative is mutually exclusive with
4206 // kDirectAddressWithFixup/kCallDirectWithFixup.
4207 CHECK(!has_extra_input);
4208 has_extra_input = true;
4209 }
4210
Chris Larsen701566a2015-10-27 15:29:13 -07004211 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4212 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004213 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4214 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4215 }
Chris Larsen701566a2015-10-27 15:29:13 -07004216 return;
4217 }
4218
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004219 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004220
4221 // Add the extra input register if either the dex cache array base register
4222 // or the PC-relative base register for accessing literals is needed.
4223 if (has_extra_input) {
4224 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4225 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004226}
4227
Chris Larsen701566a2015-10-27 15:29:13 -07004228static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004229 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004230 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4231 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004232 return true;
4233 }
4234 return false;
4235}
4236
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004237HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004238 HLoadString::LoadKind desired_string_load_kind) {
4239 if (kEmitCompilerReadBarrier) {
4240 UNIMPLEMENTED(FATAL) << "for read barrier";
4241 }
4242 // We disable PC-relative load when there is an irreducible loop, as the optimization
4243 // is incompatible with it.
4244 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4245 bool fallback_load = has_irreducible_loops;
4246 switch (desired_string_load_kind) {
4247 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4248 DCHECK(!GetCompilerOptions().GetCompilePic());
4249 break;
4250 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4251 DCHECK(GetCompilerOptions().GetCompilePic());
4252 break;
4253 case HLoadString::LoadKind::kBootImageAddress:
4254 break;
4255 case HLoadString::LoadKind::kDexCacheAddress:
4256 DCHECK(Runtime::Current()->UseJitCompilation());
4257 fallback_load = false;
4258 break;
4259 case HLoadString::LoadKind::kDexCachePcRelative:
4260 DCHECK(!Runtime::Current()->UseJitCompilation());
4261 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4262 // with irreducible loops.
4263 break;
4264 case HLoadString::LoadKind::kDexCacheViaMethod:
4265 fallback_load = false;
4266 break;
4267 }
4268 if (fallback_load) {
4269 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4270 }
4271 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004272}
4273
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004274HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4275 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004276 if (kEmitCompilerReadBarrier) {
4277 UNIMPLEMENTED(FATAL) << "for read barrier";
4278 }
4279 // We disable pc-relative load when there is an irreducible loop, as the optimization
4280 // is incompatible with it.
4281 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4282 bool fallback_load = has_irreducible_loops;
4283 switch (desired_class_load_kind) {
4284 case HLoadClass::LoadKind::kReferrersClass:
4285 fallback_load = false;
4286 break;
4287 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4288 DCHECK(!GetCompilerOptions().GetCompilePic());
4289 break;
4290 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4291 DCHECK(GetCompilerOptions().GetCompilePic());
4292 break;
4293 case HLoadClass::LoadKind::kBootImageAddress:
4294 break;
4295 case HLoadClass::LoadKind::kDexCacheAddress:
4296 DCHECK(Runtime::Current()->UseJitCompilation());
4297 fallback_load = false;
4298 break;
4299 case HLoadClass::LoadKind::kDexCachePcRelative:
4300 DCHECK(!Runtime::Current()->UseJitCompilation());
4301 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4302 // with irreducible loops.
4303 break;
4304 case HLoadClass::LoadKind::kDexCacheViaMethod:
4305 fallback_load = false;
4306 break;
4307 }
4308 if (fallback_load) {
4309 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4310 }
4311 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004312}
4313
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004314Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4315 Register temp) {
4316 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4317 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4318 if (!invoke->GetLocations()->Intrinsified()) {
4319 return location.AsRegister<Register>();
4320 }
4321 // For intrinsics we allow any location, so it may be on the stack.
4322 if (!location.IsRegister()) {
4323 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4324 return temp;
4325 }
4326 // For register locations, check if the register was saved. If so, get it from the stack.
4327 // Note: There is a chance that the register was saved but not overwritten, so we could
4328 // save one load. However, since this is just an intrinsic slow path we prefer this
4329 // simple and more robust approach rather that trying to determine if that's the case.
4330 SlowPathCode* slow_path = GetCurrentSlowPath();
4331 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4332 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4333 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4334 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4335 return temp;
4336 }
4337 return location.AsRegister<Register>();
4338}
4339
Vladimir Markodc151b22015-10-15 18:02:30 +01004340HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4341 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4342 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004343 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4344 // We disable PC-relative load when there is an irreducible loop, as the optimization
4345 // is incompatible with it.
4346 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4347 bool fallback_load = true;
4348 bool fallback_call = true;
4349 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004350 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4351 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004352 fallback_load = has_irreducible_loops;
4353 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004354 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004355 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004356 break;
4357 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004358 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004359 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004360 fallback_call = has_irreducible_loops;
4361 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004362 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004363 // TODO: Implement this type.
4364 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004365 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004366 fallback_call = false;
4367 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004368 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004369 if (fallback_load) {
4370 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4371 dispatch_info.method_load_data = 0;
4372 }
4373 if (fallback_call) {
4374 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4375 dispatch_info.direct_code_ptr = 0;
4376 }
4377 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004378}
4379
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004380void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4381 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004382 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004383 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4384 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4385 bool isR6 = isa_features_.IsR6();
4386 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4387 // R6 has PC-relative addressing.
4388 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4389 (!isR6 &&
4390 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4391 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4392 Register base_reg = has_extra_input
4393 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4394 : ZERO;
4395
4396 // For better instruction scheduling we load the direct code pointer before the method pointer.
4397 switch (code_ptr_location) {
4398 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4399 // T9 = invoke->GetDirectCodePtr();
4400 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4401 break;
4402 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4403 // T9 = code address from literal pool with link-time patch.
4404 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4405 break;
4406 default:
4407 break;
4408 }
4409
4410 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004411 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4412 // temp = thread->string_init_entrypoint
4413 __ LoadFromOffset(kLoadWord,
4414 temp.AsRegister<Register>(),
4415 TR,
4416 invoke->GetStringInitOffset());
4417 break;
4418 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004419 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004420 break;
4421 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4422 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4423 break;
4424 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004425 __ LoadLiteral(temp.AsRegister<Register>(),
4426 base_reg,
4427 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4428 break;
4429 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4430 HMipsDexCacheArraysBase* base =
4431 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4432 int32_t offset =
4433 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4434 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4435 break;
4436 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004437 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004438 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004439 Register reg = temp.AsRegister<Register>();
4440 Register method_reg;
4441 if (current_method.IsRegister()) {
4442 method_reg = current_method.AsRegister<Register>();
4443 } else {
4444 // TODO: use the appropriate DCHECK() here if possible.
4445 // DCHECK(invoke->GetLocations()->Intrinsified());
4446 DCHECK(!current_method.IsValid());
4447 method_reg = reg;
4448 __ Lw(reg, SP, kCurrentMethodStackOffset);
4449 }
4450
4451 // temp = temp->dex_cache_resolved_methods_;
4452 __ LoadFromOffset(kLoadWord,
4453 reg,
4454 method_reg,
4455 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004456 // temp = temp[index_in_cache];
4457 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4458 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004459 __ LoadFromOffset(kLoadWord,
4460 reg,
4461 reg,
4462 CodeGenerator::GetCachePointerOffset(index_in_cache));
4463 break;
4464 }
4465 }
4466
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004467 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004468 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004469 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004470 break;
4471 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004472 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4473 // T9 prepared above for better instruction scheduling.
4474 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004475 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004476 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004477 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004478 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004479 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004480 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4481 LOG(FATAL) << "Unsupported";
4482 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004483 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4484 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004485 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004486 T9,
4487 callee_method.AsRegister<Register>(),
4488 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004489 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004490 // T9()
4491 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004492 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004493 break;
4494 }
4495 DCHECK(!IsLeafMethod());
4496}
4497
4498void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004499 // Explicit clinit checks triggered by static invokes must have been pruned by
4500 // art::PrepareForRegisterAllocation.
4501 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004502
4503 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4504 return;
4505 }
4506
4507 LocationSummary* locations = invoke->GetLocations();
4508 codegen_->GenerateStaticOrDirectCall(invoke,
4509 locations->HasTemps()
4510 ? locations->GetTemp(0)
4511 : Location::NoLocation());
4512 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4513}
4514
Chris Larsen3acee732015-11-18 13:31:08 -08004515void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004516 LocationSummary* locations = invoke->GetLocations();
4517 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004518 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004519 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4520 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4521 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004522 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004523
4524 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004525 DCHECK(receiver.IsRegister());
4526 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4527 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004528 // temp = temp->GetMethodAt(method_offset);
4529 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4530 // T9 = temp->GetEntryPoint();
4531 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4532 // T9();
4533 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004534 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004535}
4536
4537void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4538 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4539 return;
4540 }
4541
4542 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004543 DCHECK(!codegen_->IsLeafMethod());
4544 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4545}
4546
4547void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004548 if (cls->NeedsAccessCheck()) {
4549 InvokeRuntimeCallingConvention calling_convention;
4550 CodeGenerator::CreateLoadClassLocationSummary(
4551 cls,
4552 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4553 Location::RegisterLocation(V0),
4554 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4555 return;
4556 }
4557
4558 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4559 ? LocationSummary::kCallOnSlowPath
4560 : LocationSummary::kNoCall;
4561 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4562 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4563 switch (load_kind) {
4564 // We need an extra register for PC-relative literals on R2.
4565 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4566 case HLoadClass::LoadKind::kBootImageAddress:
4567 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4568 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4569 break;
4570 }
4571 FALLTHROUGH_INTENDED;
4572 // We need an extra register for PC-relative dex cache accesses.
4573 case HLoadClass::LoadKind::kDexCachePcRelative:
4574 case HLoadClass::LoadKind::kReferrersClass:
4575 case HLoadClass::LoadKind::kDexCacheViaMethod:
4576 locations->SetInAt(0, Location::RequiresRegister());
4577 break;
4578 default:
4579 break;
4580 }
4581 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004582}
4583
4584void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4585 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004586 if (cls->NeedsAccessCheck()) {
4587 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004588 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004589 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004590 return;
4591 }
4592
Alexey Frunze06a46c42016-07-19 15:00:40 -07004593 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4594 Location out_loc = locations->Out();
4595 Register out = out_loc.AsRegister<Register>();
4596 Register base_or_current_method_reg;
4597 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4598 switch (load_kind) {
4599 // We need an extra register for PC-relative literals on R2.
4600 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4601 case HLoadClass::LoadKind::kBootImageAddress:
4602 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4603 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4604 break;
4605 // We need an extra register for PC-relative dex cache accesses.
4606 case HLoadClass::LoadKind::kDexCachePcRelative:
4607 case HLoadClass::LoadKind::kReferrersClass:
4608 case HLoadClass::LoadKind::kDexCacheViaMethod:
4609 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4610 break;
4611 default:
4612 base_or_current_method_reg = ZERO;
4613 break;
4614 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004615
Alexey Frunze06a46c42016-07-19 15:00:40 -07004616 bool generate_null_check = false;
4617 switch (load_kind) {
4618 case HLoadClass::LoadKind::kReferrersClass: {
4619 DCHECK(!cls->CanCallRuntime());
4620 DCHECK(!cls->MustGenerateClinitCheck());
4621 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4622 GenerateGcRootFieldLoad(cls,
4623 out_loc,
4624 base_or_current_method_reg,
4625 ArtMethod::DeclaringClassOffset().Int32Value());
4626 break;
4627 }
4628 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4629 DCHECK(!kEmitCompilerReadBarrier);
4630 __ LoadLiteral(out,
4631 base_or_current_method_reg,
4632 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4633 cls->GetTypeIndex()));
4634 break;
4635 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4636 DCHECK(!kEmitCompilerReadBarrier);
4637 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4638 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004639 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004640 if (isR6) {
4641 __ Bind(&info->high_label);
4642 __ Bind(&info->pc_rel_label);
4643 // Add a 32-bit offset to PC.
4644 __ Auipc(out, /* placeholder */ 0x1234);
4645 __ Addiu(out, out, /* placeholder */ 0x5678);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004646 } else {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004647 __ Bind(&info->high_label);
4648 __ Lui(out, /* placeholder */ 0x1234);
4649 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4650 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4651 __ Ori(out, out, /* placeholder */ 0x5678);
4652 // Add a 32-bit offset to PC.
4653 __ Addu(out, out, base_or_current_method_reg);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004654 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004655 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004656 break;
4657 }
4658 case HLoadClass::LoadKind::kBootImageAddress: {
4659 DCHECK(!kEmitCompilerReadBarrier);
4660 DCHECK_NE(cls->GetAddress(), 0u);
4661 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4662 __ LoadLiteral(out,
4663 base_or_current_method_reg,
4664 codegen_->DeduplicateBootImageAddressLiteral(address));
4665 break;
4666 }
4667 case HLoadClass::LoadKind::kDexCacheAddress: {
4668 DCHECK_NE(cls->GetAddress(), 0u);
4669 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4670 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4671 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4672 int16_t offset = Low16Bits(address);
4673 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4674 __ Lui(out, High16Bits(base_address));
4675 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4676 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4677 generate_null_check = !cls->IsInDexCache();
4678 break;
4679 }
4680 case HLoadClass::LoadKind::kDexCachePcRelative: {
4681 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4682 int32_t offset =
4683 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4684 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4685 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4686 generate_null_check = !cls->IsInDexCache();
4687 break;
4688 }
4689 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4690 // /* GcRoot<mirror::Class>[] */ out =
4691 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4692 __ LoadFromOffset(kLoadWord,
4693 out,
4694 base_or_current_method_reg,
4695 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4696 // /* GcRoot<mirror::Class> */ out = out[type_index]
4697 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4698 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4699 generate_null_check = !cls->IsInDexCache();
4700 }
4701 }
4702
4703 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4704 DCHECK(cls->CanCallRuntime());
4705 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4706 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4707 codegen_->AddSlowPath(slow_path);
4708 if (generate_null_check) {
4709 __ Beqz(out, slow_path->GetEntryLabel());
4710 }
4711 if (cls->MustGenerateClinitCheck()) {
4712 GenerateClassInitializationCheck(slow_path, out);
4713 } else {
4714 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004715 }
4716 }
4717}
4718
4719static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004720 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004721}
4722
4723void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4724 LocationSummary* locations =
4725 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4726 locations->SetOut(Location::RequiresRegister());
4727}
4728
4729void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4730 Register out = load->GetLocations()->Out().AsRegister<Register>();
4731 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4732}
4733
4734void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4735 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4736}
4737
4738void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4739 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4740}
4741
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004742void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004743 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004744 ? LocationSummary::kCallOnSlowPath
4745 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004746 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004747 HLoadString::LoadKind load_kind = load->GetLoadKind();
4748 switch (load_kind) {
4749 // We need an extra register for PC-relative literals on R2.
4750 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4751 case HLoadString::LoadKind::kBootImageAddress:
4752 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4753 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4754 break;
4755 }
4756 FALLTHROUGH_INTENDED;
4757 // We need an extra register for PC-relative dex cache accesses.
4758 case HLoadString::LoadKind::kDexCachePcRelative:
4759 case HLoadString::LoadKind::kDexCacheViaMethod:
4760 locations->SetInAt(0, Location::RequiresRegister());
4761 break;
4762 default:
4763 break;
4764 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004765 locations->SetOut(Location::RequiresRegister());
4766}
4767
4768void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004769 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004770 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004771 Location out_loc = locations->Out();
4772 Register out = out_loc.AsRegister<Register>();
4773 Register base_or_current_method_reg;
4774 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4775 switch (load_kind) {
4776 // We need an extra register for PC-relative literals on R2.
4777 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4778 case HLoadString::LoadKind::kBootImageAddress:
4779 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4780 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4781 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004782 default:
4783 base_or_current_method_reg = ZERO;
4784 break;
4785 }
4786
4787 switch (load_kind) {
4788 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4789 DCHECK(!kEmitCompilerReadBarrier);
4790 __ LoadLiteral(out,
4791 base_or_current_method_reg,
4792 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4793 load->GetStringIndex()));
4794 return; // No dex cache slow path.
4795 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4796 DCHECK(!kEmitCompilerReadBarrier);
4797 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4798 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004799 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004800 if (isR6) {
4801 __ Bind(&info->high_label);
4802 __ Bind(&info->pc_rel_label);
4803 // Add a 32-bit offset to PC.
4804 __ Auipc(out, /* placeholder */ 0x1234);
4805 __ Addiu(out, out, /* placeholder */ 0x5678);
4806 } else {
4807 __ Bind(&info->high_label);
4808 __ Lui(out, /* placeholder */ 0x1234);
4809 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4810 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4811 __ Ori(out, out, /* placeholder */ 0x5678);
4812 // Add a 32-bit offset to PC.
4813 __ Addu(out, out, base_or_current_method_reg);
4814 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004815 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004816 return; // No dex cache slow path.
4817 }
4818 case HLoadString::LoadKind::kBootImageAddress: {
4819 DCHECK(!kEmitCompilerReadBarrier);
4820 DCHECK_NE(load->GetAddress(), 0u);
4821 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4822 __ LoadLiteral(out,
4823 base_or_current_method_reg,
4824 codegen_->DeduplicateBootImageAddressLiteral(address));
4825 return; // No dex cache slow path.
4826 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004827 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004828 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004829 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004830
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004831 // TODO: Re-add the compiler code to do string dex cache lookup again.
4832 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4833 codegen_->AddSlowPath(slow_path);
4834 __ B(slow_path->GetEntryLabel());
4835 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004836}
4837
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004838void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4839 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4840 locations->SetOut(Location::ConstantLocation(constant));
4841}
4842
4843void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4844 // Will be generated at use site.
4845}
4846
4847void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4848 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004849 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004850 InvokeRuntimeCallingConvention calling_convention;
4851 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4852}
4853
4854void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4855 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01004856 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004857 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4858 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004859 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004860 }
4861 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4862}
4863
4864void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4865 LocationSummary* locations =
4866 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4867 switch (mul->GetResultType()) {
4868 case Primitive::kPrimInt:
4869 case Primitive::kPrimLong:
4870 locations->SetInAt(0, Location::RequiresRegister());
4871 locations->SetInAt(1, Location::RequiresRegister());
4872 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4873 break;
4874
4875 case Primitive::kPrimFloat:
4876 case Primitive::kPrimDouble:
4877 locations->SetInAt(0, Location::RequiresFpuRegister());
4878 locations->SetInAt(1, Location::RequiresFpuRegister());
4879 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4880 break;
4881
4882 default:
4883 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4884 }
4885}
4886
4887void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4888 Primitive::Type type = instruction->GetType();
4889 LocationSummary* locations = instruction->GetLocations();
4890 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4891
4892 switch (type) {
4893 case Primitive::kPrimInt: {
4894 Register dst = locations->Out().AsRegister<Register>();
4895 Register lhs = locations->InAt(0).AsRegister<Register>();
4896 Register rhs = locations->InAt(1).AsRegister<Register>();
4897
4898 if (isR6) {
4899 __ MulR6(dst, lhs, rhs);
4900 } else {
4901 __ MulR2(dst, lhs, rhs);
4902 }
4903 break;
4904 }
4905 case Primitive::kPrimLong: {
4906 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4907 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4908 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4909 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4910 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4911 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4912
4913 // Extra checks to protect caused by the existance of A1_A2.
4914 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4915 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4916 DCHECK_NE(dst_high, lhs_low);
4917 DCHECK_NE(dst_high, rhs_low);
4918
4919 // A_B * C_D
4920 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4921 // dst_lo: [ low(B*D) ]
4922 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4923
4924 if (isR6) {
4925 __ MulR6(TMP, lhs_high, rhs_low);
4926 __ MulR6(dst_high, lhs_low, rhs_high);
4927 __ Addu(dst_high, dst_high, TMP);
4928 __ MuhuR6(TMP, lhs_low, rhs_low);
4929 __ Addu(dst_high, dst_high, TMP);
4930 __ MulR6(dst_low, lhs_low, rhs_low);
4931 } else {
4932 __ MulR2(TMP, lhs_high, rhs_low);
4933 __ MulR2(dst_high, lhs_low, rhs_high);
4934 __ Addu(dst_high, dst_high, TMP);
4935 __ MultuR2(lhs_low, rhs_low);
4936 __ Mfhi(TMP);
4937 __ Addu(dst_high, dst_high, TMP);
4938 __ Mflo(dst_low);
4939 }
4940 break;
4941 }
4942 case Primitive::kPrimFloat:
4943 case Primitive::kPrimDouble: {
4944 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4945 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4946 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4947 if (type == Primitive::kPrimFloat) {
4948 __ MulS(dst, lhs, rhs);
4949 } else {
4950 __ MulD(dst, lhs, rhs);
4951 }
4952 break;
4953 }
4954 default:
4955 LOG(FATAL) << "Unexpected mul type " << type;
4956 }
4957}
4958
4959void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4960 LocationSummary* locations =
4961 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4962 switch (neg->GetResultType()) {
4963 case Primitive::kPrimInt:
4964 case Primitive::kPrimLong:
4965 locations->SetInAt(0, Location::RequiresRegister());
4966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4967 break;
4968
4969 case Primitive::kPrimFloat:
4970 case Primitive::kPrimDouble:
4971 locations->SetInAt(0, Location::RequiresFpuRegister());
4972 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4973 break;
4974
4975 default:
4976 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4977 }
4978}
4979
4980void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4981 Primitive::Type type = instruction->GetType();
4982 LocationSummary* locations = instruction->GetLocations();
4983
4984 switch (type) {
4985 case Primitive::kPrimInt: {
4986 Register dst = locations->Out().AsRegister<Register>();
4987 Register src = locations->InAt(0).AsRegister<Register>();
4988 __ Subu(dst, ZERO, src);
4989 break;
4990 }
4991 case Primitive::kPrimLong: {
4992 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4993 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4994 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4995 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4996 __ Subu(dst_low, ZERO, src_low);
4997 __ Sltu(TMP, ZERO, dst_low);
4998 __ Subu(dst_high, ZERO, src_high);
4999 __ Subu(dst_high, dst_high, TMP);
5000 break;
5001 }
5002 case Primitive::kPrimFloat:
5003 case Primitive::kPrimDouble: {
5004 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5005 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5006 if (type == Primitive::kPrimFloat) {
5007 __ NegS(dst, src);
5008 } else {
5009 __ NegD(dst, src);
5010 }
5011 break;
5012 }
5013 default:
5014 LOG(FATAL) << "Unexpected neg type " << type;
5015 }
5016}
5017
5018void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5019 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005021 InvokeRuntimeCallingConvention calling_convention;
5022 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5023 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5024 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5025 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5026}
5027
5028void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5029 InvokeRuntimeCallingConvention calling_convention;
5030 Register current_method_register = calling_convention.GetRegisterAt(2);
5031 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5032 // Move an uint16_t value to a register.
5033 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005034 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005035 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5036 void*, uint32_t, int32_t, ArtMethod*>();
5037}
5038
5039void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5040 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005041 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005042 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005043 if (instruction->IsStringAlloc()) {
5044 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5045 } else {
5046 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5047 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5048 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005049 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5050}
5051
5052void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005053 if (instruction->IsStringAlloc()) {
5054 // String is allocated through StringFactory. Call NewEmptyString entry point.
5055 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005056 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005057 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5058 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5059 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005060 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005061 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5062 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005063 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00005064 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
5065 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005066}
5067
5068void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5069 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5070 locations->SetInAt(0, Location::RequiresRegister());
5071 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5072}
5073
5074void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5075 Primitive::Type type = instruction->GetType();
5076 LocationSummary* locations = instruction->GetLocations();
5077
5078 switch (type) {
5079 case Primitive::kPrimInt: {
5080 Register dst = locations->Out().AsRegister<Register>();
5081 Register src = locations->InAt(0).AsRegister<Register>();
5082 __ Nor(dst, src, ZERO);
5083 break;
5084 }
5085
5086 case Primitive::kPrimLong: {
5087 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5088 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5089 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5090 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5091 __ Nor(dst_high, src_high, ZERO);
5092 __ Nor(dst_low, src_low, ZERO);
5093 break;
5094 }
5095
5096 default:
5097 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5098 }
5099}
5100
5101void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5102 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5103 locations->SetInAt(0, Location::RequiresRegister());
5104 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5105}
5106
5107void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5108 LocationSummary* locations = instruction->GetLocations();
5109 __ Xori(locations->Out().AsRegister<Register>(),
5110 locations->InAt(0).AsRegister<Register>(),
5111 1);
5112}
5113
5114void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko3b7537b2016-09-13 11:56:01 +00005115 codegen_->CreateNullCheckLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005116}
5117
Calin Juravle2ae48182016-03-16 14:05:09 +00005118void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5119 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005120 return;
5121 }
5122 Location obj = instruction->GetLocations()->InAt(0);
5123
5124 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005125 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005126}
5127
Calin Juravle2ae48182016-03-16 14:05:09 +00005128void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005129 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005130 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005131
5132 Location obj = instruction->GetLocations()->InAt(0);
5133
5134 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5135}
5136
5137void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005138 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005139}
5140
5141void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5142 HandleBinaryOp(instruction);
5143}
5144
5145void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5146 HandleBinaryOp(instruction);
5147}
5148
5149void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5150 LOG(FATAL) << "Unreachable";
5151}
5152
5153void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
5154 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5155}
5156
5157void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
5158 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5159 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5160 if (location.IsStackSlot()) {
5161 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5162 } else if (location.IsDoubleStackSlot()) {
5163 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5164 }
5165 locations->SetOut(location);
5166}
5167
5168void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
5169 ATTRIBUTE_UNUSED) {
5170 // Nothing to do, the parameter is already at its location.
5171}
5172
5173void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5174 LocationSummary* locations =
5175 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5176 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5177}
5178
5179void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5180 ATTRIBUTE_UNUSED) {
5181 // Nothing to do, the method is already at its location.
5182}
5183
5184void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5185 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005186 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005187 locations->SetInAt(i, Location::Any());
5188 }
5189 locations->SetOut(Location::Any());
5190}
5191
5192void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5193 LOG(FATAL) << "Unreachable";
5194}
5195
5196void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5197 Primitive::Type type = rem->GetResultType();
5198 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005199 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005200 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5201
5202 switch (type) {
5203 case Primitive::kPrimInt:
5204 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005205 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005206 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5207 break;
5208
5209 case Primitive::kPrimLong: {
5210 InvokeRuntimeCallingConvention calling_convention;
5211 locations->SetInAt(0, Location::RegisterPairLocation(
5212 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5213 locations->SetInAt(1, Location::RegisterPairLocation(
5214 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5215 locations->SetOut(calling_convention.GetReturnLocation(type));
5216 break;
5217 }
5218
5219 case Primitive::kPrimFloat:
5220 case Primitive::kPrimDouble: {
5221 InvokeRuntimeCallingConvention calling_convention;
5222 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5223 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5224 locations->SetOut(calling_convention.GetReturnLocation(type));
5225 break;
5226 }
5227
5228 default:
5229 LOG(FATAL) << "Unexpected rem type " << type;
5230 }
5231}
5232
5233void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5234 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005235
5236 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005237 case Primitive::kPrimInt:
5238 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005239 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005240 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005241 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005242 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5243 break;
5244 }
5245 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005246 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005247 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005248 break;
5249 }
5250 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005251 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005252 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005253 break;
5254 }
5255 default:
5256 LOG(FATAL) << "Unexpected rem type " << type;
5257 }
5258}
5259
5260void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5261 memory_barrier->SetLocations(nullptr);
5262}
5263
5264void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5265 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5266}
5267
5268void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5270 Primitive::Type return_type = ret->InputAt(0)->GetType();
5271 locations->SetInAt(0, MipsReturnLocation(return_type));
5272}
5273
5274void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5275 codegen_->GenerateFrameExit();
5276}
5277
5278void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5279 ret->SetLocations(nullptr);
5280}
5281
5282void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5283 codegen_->GenerateFrameExit();
5284}
5285
Alexey Frunze92d90602015-12-18 18:16:36 -08005286void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5287 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005288}
5289
Alexey Frunze92d90602015-12-18 18:16:36 -08005290void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5291 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005292}
5293
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005294void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5295 HandleShift(shl);
5296}
5297
5298void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5299 HandleShift(shl);
5300}
5301
5302void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5303 HandleShift(shr);
5304}
5305
5306void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5307 HandleShift(shr);
5308}
5309
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005310void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5311 HandleBinaryOp(instruction);
5312}
5313
5314void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5315 HandleBinaryOp(instruction);
5316}
5317
5318void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5319 HandleFieldGet(instruction, instruction->GetFieldInfo());
5320}
5321
5322void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5323 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5324}
5325
5326void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5327 HandleFieldSet(instruction, instruction->GetFieldInfo());
5328}
5329
5330void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5331 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5332}
5333
5334void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5335 HUnresolvedInstanceFieldGet* instruction) {
5336 FieldAccessCallingConventionMIPS calling_convention;
5337 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5338 instruction->GetFieldType(),
5339 calling_convention);
5340}
5341
5342void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5343 HUnresolvedInstanceFieldGet* instruction) {
5344 FieldAccessCallingConventionMIPS calling_convention;
5345 codegen_->GenerateUnresolvedFieldAccess(instruction,
5346 instruction->GetFieldType(),
5347 instruction->GetFieldIndex(),
5348 instruction->GetDexPc(),
5349 calling_convention);
5350}
5351
5352void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5353 HUnresolvedInstanceFieldSet* instruction) {
5354 FieldAccessCallingConventionMIPS calling_convention;
5355 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5356 instruction->GetFieldType(),
5357 calling_convention);
5358}
5359
5360void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5361 HUnresolvedInstanceFieldSet* instruction) {
5362 FieldAccessCallingConventionMIPS calling_convention;
5363 codegen_->GenerateUnresolvedFieldAccess(instruction,
5364 instruction->GetFieldType(),
5365 instruction->GetFieldIndex(),
5366 instruction->GetDexPc(),
5367 calling_convention);
5368}
5369
5370void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5371 HUnresolvedStaticFieldGet* instruction) {
5372 FieldAccessCallingConventionMIPS calling_convention;
5373 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5374 instruction->GetFieldType(),
5375 calling_convention);
5376}
5377
5378void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5379 HUnresolvedStaticFieldGet* instruction) {
5380 FieldAccessCallingConventionMIPS calling_convention;
5381 codegen_->GenerateUnresolvedFieldAccess(instruction,
5382 instruction->GetFieldType(),
5383 instruction->GetFieldIndex(),
5384 instruction->GetDexPc(),
5385 calling_convention);
5386}
5387
5388void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5389 HUnresolvedStaticFieldSet* instruction) {
5390 FieldAccessCallingConventionMIPS calling_convention;
5391 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5392 instruction->GetFieldType(),
5393 calling_convention);
5394}
5395
5396void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5397 HUnresolvedStaticFieldSet* instruction) {
5398 FieldAccessCallingConventionMIPS calling_convention;
5399 codegen_->GenerateUnresolvedFieldAccess(instruction,
5400 instruction->GetFieldType(),
5401 instruction->GetFieldIndex(),
5402 instruction->GetDexPc(),
5403 calling_convention);
5404}
5405
5406void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005407 LocationSummary* locations =
5408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5409 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005410}
5411
5412void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5413 HBasicBlock* block = instruction->GetBlock();
5414 if (block->GetLoopInformation() != nullptr) {
5415 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5416 // The back edge will generate the suspend check.
5417 return;
5418 }
5419 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5420 // The goto will generate the suspend check.
5421 return;
5422 }
5423 GenerateSuspendCheck(instruction, nullptr);
5424}
5425
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005426void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5427 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005428 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005429 InvokeRuntimeCallingConvention calling_convention;
5430 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5431}
5432
5433void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005434 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005435 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5436}
5437
5438void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5439 Primitive::Type input_type = conversion->GetInputType();
5440 Primitive::Type result_type = conversion->GetResultType();
5441 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005442 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005443
5444 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5445 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5446 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5447 }
5448
5449 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005450 if (!isR6 &&
5451 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5452 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005453 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005454 }
5455
5456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5457
5458 if (call_kind == LocationSummary::kNoCall) {
5459 if (Primitive::IsFloatingPointType(input_type)) {
5460 locations->SetInAt(0, Location::RequiresFpuRegister());
5461 } else {
5462 locations->SetInAt(0, Location::RequiresRegister());
5463 }
5464
5465 if (Primitive::IsFloatingPointType(result_type)) {
5466 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5467 } else {
5468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5469 }
5470 } else {
5471 InvokeRuntimeCallingConvention calling_convention;
5472
5473 if (Primitive::IsFloatingPointType(input_type)) {
5474 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5475 } else {
5476 DCHECK_EQ(input_type, Primitive::kPrimLong);
5477 locations->SetInAt(0, Location::RegisterPairLocation(
5478 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5479 }
5480
5481 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5482 }
5483}
5484
5485void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5486 LocationSummary* locations = conversion->GetLocations();
5487 Primitive::Type result_type = conversion->GetResultType();
5488 Primitive::Type input_type = conversion->GetInputType();
5489 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005490 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005491
5492 DCHECK_NE(input_type, result_type);
5493
5494 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5495 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5496 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5497 Register src = locations->InAt(0).AsRegister<Register>();
5498
Alexey Frunzea871ef12016-06-27 15:20:11 -07005499 if (dst_low != src) {
5500 __ Move(dst_low, src);
5501 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005502 __ Sra(dst_high, src, 31);
5503 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5504 Register dst = locations->Out().AsRegister<Register>();
5505 Register src = (input_type == Primitive::kPrimLong)
5506 ? locations->InAt(0).AsRegisterPairLow<Register>()
5507 : locations->InAt(0).AsRegister<Register>();
5508
5509 switch (result_type) {
5510 case Primitive::kPrimChar:
5511 __ Andi(dst, src, 0xFFFF);
5512 break;
5513 case Primitive::kPrimByte:
5514 if (has_sign_extension) {
5515 __ Seb(dst, src);
5516 } else {
5517 __ Sll(dst, src, 24);
5518 __ Sra(dst, dst, 24);
5519 }
5520 break;
5521 case Primitive::kPrimShort:
5522 if (has_sign_extension) {
5523 __ Seh(dst, src);
5524 } else {
5525 __ Sll(dst, src, 16);
5526 __ Sra(dst, dst, 16);
5527 }
5528 break;
5529 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005530 if (dst != src) {
5531 __ Move(dst, src);
5532 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005533 break;
5534
5535 default:
5536 LOG(FATAL) << "Unexpected type conversion from " << input_type
5537 << " to " << result_type;
5538 }
5539 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005540 if (input_type == Primitive::kPrimLong) {
5541 if (isR6) {
5542 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5543 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5544 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5545 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5546 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5547 __ Mtc1(src_low, FTMP);
5548 __ Mthc1(src_high, FTMP);
5549 if (result_type == Primitive::kPrimFloat) {
5550 __ Cvtsl(dst, FTMP);
5551 } else {
5552 __ Cvtdl(dst, FTMP);
5553 }
5554 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005555 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
5556 : kQuickL2d;
5557 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005558 if (result_type == Primitive::kPrimFloat) {
5559 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5560 } else {
5561 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5562 }
5563 }
5564 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005565 Register src = locations->InAt(0).AsRegister<Register>();
5566 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5567 __ Mtc1(src, FTMP);
5568 if (result_type == Primitive::kPrimFloat) {
5569 __ Cvtsw(dst, FTMP);
5570 } else {
5571 __ Cvtdw(dst, FTMP);
5572 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005573 }
5574 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5575 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005576 if (result_type == Primitive::kPrimLong) {
5577 if (isR6) {
5578 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5579 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5580 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5581 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5582 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5583 MipsLabel truncate;
5584 MipsLabel done;
5585
5586 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5587 // value when the input is either a NaN or is outside of the range of the output type
5588 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5589 // the same result.
5590 //
5591 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5592 // value of the output type if the input is outside of the range after the truncation or
5593 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5594 // results. This matches the desired float/double-to-int/long conversion exactly.
5595 //
5596 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5597 //
5598 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5599 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5600 // even though it must be NAN2008=1 on R6.
5601 //
5602 // The code takes care of the different behaviors by first comparing the input to the
5603 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5604 // If the input is greater than or equal to the minimum, it procedes to the truncate
5605 // instruction, which will handle such an input the same way irrespective of NAN2008.
5606 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5607 // in order to return either zero or the minimum value.
5608 //
5609 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5610 // truncate instruction for MIPS64R6.
5611 if (input_type == Primitive::kPrimFloat) {
5612 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5613 __ LoadConst32(TMP, min_val);
5614 __ Mtc1(TMP, FTMP);
5615 __ CmpLeS(FTMP, FTMP, src);
5616 } else {
5617 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5618 __ LoadConst32(TMP, High32Bits(min_val));
5619 __ Mtc1(ZERO, FTMP);
5620 __ Mthc1(TMP, FTMP);
5621 __ CmpLeD(FTMP, FTMP, src);
5622 }
5623
5624 __ Bc1nez(FTMP, &truncate);
5625
5626 if (input_type == Primitive::kPrimFloat) {
5627 __ CmpEqS(FTMP, src, src);
5628 } else {
5629 __ CmpEqD(FTMP, src, src);
5630 }
5631 __ Move(dst_low, ZERO);
5632 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5633 __ Mfc1(TMP, FTMP);
5634 __ And(dst_high, dst_high, TMP);
5635
5636 __ B(&done);
5637
5638 __ Bind(&truncate);
5639
5640 if (input_type == Primitive::kPrimFloat) {
5641 __ TruncLS(FTMP, src);
5642 } else {
5643 __ TruncLD(FTMP, src);
5644 }
5645 __ Mfc1(dst_low, FTMP);
5646 __ Mfhc1(dst_high, FTMP);
5647
5648 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005649 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005650 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
5651 : kQuickD2l;
5652 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005653 if (input_type == Primitive::kPrimFloat) {
5654 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5655 } else {
5656 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5657 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005658 }
5659 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005660 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5661 Register dst = locations->Out().AsRegister<Register>();
5662 MipsLabel truncate;
5663 MipsLabel done;
5664
5665 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5666 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5667 // even though it must be NAN2008=1 on R6.
5668 //
5669 // For details see the large comment above for the truncation of float/double to long on R6.
5670 //
5671 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5672 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005673 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005674 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5675 __ LoadConst32(TMP, min_val);
5676 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005677 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005678 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5679 __ LoadConst32(TMP, High32Bits(min_val));
5680 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005681 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005682 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005683
5684 if (isR6) {
5685 if (input_type == Primitive::kPrimFloat) {
5686 __ CmpLeS(FTMP, FTMP, src);
5687 } else {
5688 __ CmpLeD(FTMP, FTMP, src);
5689 }
5690 __ Bc1nez(FTMP, &truncate);
5691
5692 if (input_type == Primitive::kPrimFloat) {
5693 __ CmpEqS(FTMP, src, src);
5694 } else {
5695 __ CmpEqD(FTMP, src, src);
5696 }
5697 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5698 __ Mfc1(TMP, FTMP);
5699 __ And(dst, dst, TMP);
5700 } else {
5701 if (input_type == Primitive::kPrimFloat) {
5702 __ ColeS(0, FTMP, src);
5703 } else {
5704 __ ColeD(0, FTMP, src);
5705 }
5706 __ Bc1t(0, &truncate);
5707
5708 if (input_type == Primitive::kPrimFloat) {
5709 __ CeqS(0, src, src);
5710 } else {
5711 __ CeqD(0, src, src);
5712 }
5713 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5714 __ Movf(dst, ZERO, 0);
5715 }
5716
5717 __ B(&done);
5718
5719 __ Bind(&truncate);
5720
5721 if (input_type == Primitive::kPrimFloat) {
5722 __ TruncWS(FTMP, src);
5723 } else {
5724 __ TruncWD(FTMP, src);
5725 }
5726 __ Mfc1(dst, FTMP);
5727
5728 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005729 }
5730 } else if (Primitive::IsFloatingPointType(result_type) &&
5731 Primitive::IsFloatingPointType(input_type)) {
5732 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5733 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5734 if (result_type == Primitive::kPrimFloat) {
5735 __ Cvtsd(dst, src);
5736 } else {
5737 __ Cvtds(dst, src);
5738 }
5739 } else {
5740 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5741 << " to " << result_type;
5742 }
5743}
5744
5745void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5746 HandleShift(ushr);
5747}
5748
5749void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5750 HandleShift(ushr);
5751}
5752
5753void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5754 HandleBinaryOp(instruction);
5755}
5756
5757void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5758 HandleBinaryOp(instruction);
5759}
5760
5761void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5762 // Nothing to do, this should be removed during prepare for register allocator.
5763 LOG(FATAL) << "Unreachable";
5764}
5765
5766void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5767 // Nothing to do, this should be removed during prepare for register allocator.
5768 LOG(FATAL) << "Unreachable";
5769}
5770
5771void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005772 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005773}
5774
5775void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005776 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005777}
5778
5779void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005780 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005781}
5782
5783void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005784 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005785}
5786
5787void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005788 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005789}
5790
5791void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005792 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005793}
5794
5795void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005796 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005797}
5798
5799void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005800 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005801}
5802
5803void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005804 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005805}
5806
5807void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005808 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005809}
5810
5811void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005812 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005813}
5814
5815void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005816 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005817}
5818
5819void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005820 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005821}
5822
5823void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005824 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005825}
5826
5827void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005828 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005829}
5830
5831void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005832 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005833}
5834
5835void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005836 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005837}
5838
5839void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005840 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005841}
5842
5843void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005844 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005845}
5846
5847void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005848 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005849}
5850
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005851void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5852 LocationSummary* locations =
5853 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5854 locations->SetInAt(0, Location::RequiresRegister());
5855}
5856
Alexey Frunze96b66822016-09-10 02:32:44 -07005857void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
5858 int32_t lower_bound,
5859 uint32_t num_entries,
5860 HBasicBlock* switch_block,
5861 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005862 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005863 Register temp_reg = TMP;
5864 __ Addiu32(temp_reg, value_reg, -lower_bound);
5865 // Jump to default if index is negative
5866 // Note: We don't check the case that index is positive while value < lower_bound, because in
5867 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5868 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5869
Alexey Frunze96b66822016-09-10 02:32:44 -07005870 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005871 // Jump to successors[0] if value == lower_bound.
5872 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5873 int32_t last_index = 0;
5874 for (; num_entries - last_index > 2; last_index += 2) {
5875 __ Addiu(temp_reg, temp_reg, -2);
5876 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5877 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5878 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5879 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5880 }
5881 if (num_entries - last_index == 2) {
5882 // The last missing case_value.
5883 __ Addiu(temp_reg, temp_reg, -1);
5884 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005885 }
5886
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005887 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07005888 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005889 __ B(codegen_->GetLabelOf(default_block));
5890 }
5891}
5892
Alexey Frunze96b66822016-09-10 02:32:44 -07005893void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
5894 Register constant_area,
5895 int32_t lower_bound,
5896 uint32_t num_entries,
5897 HBasicBlock* switch_block,
5898 HBasicBlock* default_block) {
5899 // Create a jump table.
5900 std::vector<MipsLabel*> labels(num_entries);
5901 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
5902 for (uint32_t i = 0; i < num_entries; i++) {
5903 labels[i] = codegen_->GetLabelOf(successors[i]);
5904 }
5905 JumpTable* table = __ CreateJumpTable(std::move(labels));
5906
5907 // Is the value in range?
5908 __ Addiu32(TMP, value_reg, -lower_bound);
5909 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
5910 __ Sltiu(AT, TMP, num_entries);
5911 __ Beqz(AT, codegen_->GetLabelOf(default_block));
5912 } else {
5913 __ LoadConst32(AT, num_entries);
5914 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
5915 }
5916
5917 // We are in the range of the table.
5918 // Load the target address from the jump table, indexing by the value.
5919 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
5920 __ Sll(TMP, TMP, 2);
5921 __ Addu(TMP, TMP, AT);
5922 __ Lw(TMP, TMP, 0);
5923 // Compute the absolute target address by adding the table start address
5924 // (the table contains offsets to targets relative to its start).
5925 __ Addu(TMP, TMP, AT);
5926 // And jump.
5927 __ Jr(TMP);
5928 __ NopIfNoReordering();
5929}
5930
5931void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5932 int32_t lower_bound = switch_instr->GetStartValue();
5933 uint32_t num_entries = switch_instr->GetNumEntries();
5934 LocationSummary* locations = switch_instr->GetLocations();
5935 Register value_reg = locations->InAt(0).AsRegister<Register>();
5936 HBasicBlock* switch_block = switch_instr->GetBlock();
5937 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5938
5939 if (codegen_->GetInstructionSetFeatures().IsR6() &&
5940 num_entries > kPackedSwitchJumpTableThreshold) {
5941 // R6 uses PC-relative addressing to access the jump table.
5942 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
5943 // the jump table and it is implemented by changing HPackedSwitch to
5944 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
5945 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
5946 GenTableBasedPackedSwitch(value_reg,
5947 ZERO,
5948 lower_bound,
5949 num_entries,
5950 switch_block,
5951 default_block);
5952 } else {
5953 GenPackedSwitchWithCompares(value_reg,
5954 lower_bound,
5955 num_entries,
5956 switch_block,
5957 default_block);
5958 }
5959}
5960
5961void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5962 LocationSummary* locations =
5963 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5964 locations->SetInAt(0, Location::RequiresRegister());
5965 // Constant area pointer (HMipsComputeBaseMethodAddress).
5966 locations->SetInAt(1, Location::RequiresRegister());
5967}
5968
5969void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5970 int32_t lower_bound = switch_instr->GetStartValue();
5971 uint32_t num_entries = switch_instr->GetNumEntries();
5972 LocationSummary* locations = switch_instr->GetLocations();
5973 Register value_reg = locations->InAt(0).AsRegister<Register>();
5974 Register constant_area = locations->InAt(1).AsRegister<Register>();
5975 HBasicBlock* switch_block = switch_instr->GetBlock();
5976 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5977
5978 // This is an R2-only path. HPackedSwitch has been changed to
5979 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
5980 // required to address the jump table relative to PC.
5981 GenTableBasedPackedSwitch(value_reg,
5982 constant_area,
5983 lower_bound,
5984 num_entries,
5985 switch_block,
5986 default_block);
5987}
5988
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005989void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5990 HMipsComputeBaseMethodAddress* insn) {
5991 LocationSummary* locations =
5992 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5993 locations->SetOut(Location::RequiresRegister());
5994}
5995
5996void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5997 HMipsComputeBaseMethodAddress* insn) {
5998 LocationSummary* locations = insn->GetLocations();
5999 Register reg = locations->Out().AsRegister<Register>();
6000
6001 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6002
6003 // Generate a dummy PC-relative call to obtain PC.
6004 __ Nal();
6005 // Grab the return address off RA.
6006 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006007 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006008
6009 // Remember this offset (the obtained PC value) for later use with constant area.
6010 __ BindPcRelBaseLabel();
6011}
6012
6013void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6015 locations->SetOut(Location::RequiresRegister());
6016}
6017
6018void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6019 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6020 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6021 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006022 bool reordering = __ SetReorder(false);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006023 if (codegen_->GetInstructionSetFeatures().IsR6()) {
6024 __ Bind(&info->high_label);
6025 __ Bind(&info->pc_rel_label);
6026 // Add a 32-bit offset to PC.
6027 __ Auipc(reg, /* placeholder */ 0x1234);
6028 __ Addiu(reg, reg, /* placeholder */ 0x5678);
6029 } else {
6030 // Generate a dummy PC-relative call to obtain PC.
6031 __ Nal();
6032 __ Bind(&info->high_label);
6033 __ Lui(reg, /* placeholder */ 0x1234);
6034 __ Bind(&info->pc_rel_label);
6035 __ Ori(reg, reg, /* placeholder */ 0x5678);
6036 // Add a 32-bit offset to PC.
6037 __ Addu(reg, reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006038 // TODO: Can we share this code with that of VisitMipsComputeBaseMethodAddress()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006039 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006040 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006041}
6042
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006043void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6044 // The trampoline uses the same calling convention as dex calling conventions,
6045 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6046 // the method_idx.
6047 HandleInvoke(invoke);
6048}
6049
6050void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6051 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6052}
6053
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006054void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6055 LocationSummary* locations =
6056 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6057 locations->SetInAt(0, Location::RequiresRegister());
6058 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006059}
6060
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006061void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6062 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006063 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006064 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006065 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006066 __ LoadFromOffset(kLoadWord,
6067 locations->Out().AsRegister<Register>(),
6068 locations->InAt(0).AsRegister<Register>(),
6069 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006070 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006071 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006072 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006073 __ LoadFromOffset(kLoadWord,
6074 locations->Out().AsRegister<Register>(),
6075 locations->InAt(0).AsRegister<Register>(),
6076 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006077 __ LoadFromOffset(kLoadWord,
6078 locations->Out().AsRegister<Register>(),
6079 locations->Out().AsRegister<Register>(),
6080 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006081 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006082}
6083
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006084#undef __
6085#undef QUICK_ENTRY_POINT
6086
6087} // namespace mips
6088} // namespace art