blob: cec3ca11033e11ffc5fe68876073223634a321af [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020023#include "entrypoints/quick/quick_entrypoints.h"
24#include "entrypoints/quick/quick_entrypoints_enum.h"
25#include "gc/accounting/card_table.h"
26#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070027#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020028#include "mirror/array-inl.h"
29#include "mirror/class-inl.h"
30#include "offsets.h"
31#include "thread.h"
32#include "utils/assembler.h"
33#include "utils/mips/assembler_mips.h"
34#include "utils/stack_checks.h"
35
36namespace art {
37namespace mips {
38
39static constexpr int kCurrentMethodStackOffset = 0;
40static constexpr Register kMethodRegisterArgument = A0;
41
Alexey Frunzee3fb2452016-05-10 16:08:05 -070042// We'll maximize the range of a single load instruction for dex cache array accesses
43// by aligning offset -32768 with the offset of the first used element.
44static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
45
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020046Location MipsReturnLocation(Primitive::Type return_type) {
47 switch (return_type) {
48 case Primitive::kPrimBoolean:
49 case Primitive::kPrimByte:
50 case Primitive::kPrimChar:
51 case Primitive::kPrimShort:
52 case Primitive::kPrimInt:
53 case Primitive::kPrimNot:
54 return Location::RegisterLocation(V0);
55
56 case Primitive::kPrimLong:
57 return Location::RegisterPairLocation(V0, V1);
58
59 case Primitive::kPrimFloat:
60 case Primitive::kPrimDouble:
61 return Location::FpuRegisterLocation(F0);
62
63 case Primitive::kPrimVoid:
64 return Location();
65 }
66 UNREACHABLE();
67}
68
69Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
70 return MipsReturnLocation(type);
71}
72
73Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
74 return Location::RegisterLocation(kMethodRegisterArgument);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
78 Location next_location;
79
80 switch (type) {
81 case Primitive::kPrimBoolean:
82 case Primitive::kPrimByte:
83 case Primitive::kPrimChar:
84 case Primitive::kPrimShort:
85 case Primitive::kPrimInt:
86 case Primitive::kPrimNot: {
87 uint32_t gp_index = gp_index_++;
88 if (gp_index < calling_convention.GetNumberOfRegisters()) {
89 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
90 } else {
91 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
92 next_location = Location::StackSlot(stack_offset);
93 }
94 break;
95 }
96
97 case Primitive::kPrimLong: {
98 uint32_t gp_index = gp_index_;
99 gp_index_ += 2;
100 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
101 if (calling_convention.GetRegisterAt(gp_index) == A1) {
102 gp_index_++; // Skip A1, and use A2_A3 instead.
103 gp_index++;
104 }
105 Register low_even = calling_convention.GetRegisterAt(gp_index);
106 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
107 DCHECK_EQ(low_even + 1, high_odd);
108 next_location = Location::RegisterPairLocation(low_even, high_odd);
109 } else {
110 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
111 next_location = Location::DoubleStackSlot(stack_offset);
112 }
113 break;
114 }
115
116 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
117 // will take up the even/odd pair, while floats are stored in even regs only.
118 // On 64 bit FPU, both double and float are stored in even registers only.
119 case Primitive::kPrimFloat:
120 case Primitive::kPrimDouble: {
121 uint32_t float_index = float_index_++;
122 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
123 next_location = Location::FpuRegisterLocation(
124 calling_convention.GetFpuRegisterAt(float_index));
125 } else {
126 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
127 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
128 : Location::StackSlot(stack_offset);
129 }
130 break;
131 }
132
133 case Primitive::kPrimVoid:
134 LOG(FATAL) << "Unexpected parameter type " << type;
135 break;
136 }
137
138 // Space on the stack is reserved for all arguments.
139 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
140
141 return next_location;
142}
143
144Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
145 return MipsReturnLocation(type);
146}
147
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100148// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
149#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700150#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200151
152class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
153 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000154 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155
156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
157 LocationSummary* locations = instruction_->GetLocations();
158 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
159 __ Bind(GetEntryLabel());
160 if (instruction_->CanThrowIntoCatchBlock()) {
161 // Live registers will be restored in the catch block if caught.
162 SaveLiveRegisters(codegen, instruction_->GetLocations());
163 }
164 // We're moving two locations to locations that could overlap, so we need a parallel
165 // move resolver.
166 InvokeRuntimeCallingConvention calling_convention;
167 codegen->EmitParallelMoves(locations->InAt(0),
168 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
169 Primitive::kPrimInt,
170 locations->InAt(1),
171 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
172 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100173 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
174 ? kQuickThrowStringBounds
175 : kQuickThrowArrayBounds;
176 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100177 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200178 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
179 }
180
181 bool IsFatal() const OVERRIDE { return true; }
182
183 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
184
185 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
187};
188
189class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
190 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000191 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200192
193 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
194 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
195 __ Bind(GetEntryLabel());
196 if (instruction_->CanThrowIntoCatchBlock()) {
197 // Live registers will be restored in the catch block if caught.
198 SaveLiveRegisters(codegen, instruction_->GetLocations());
199 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100200 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200201 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
202 }
203
204 bool IsFatal() const OVERRIDE { return true; }
205
206 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
207
208 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200209 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
210};
211
212class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
213 public:
214 LoadClassSlowPathMIPS(HLoadClass* cls,
215 HInstruction* at,
216 uint32_t dex_pc,
217 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000218 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200219 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
220 }
221
222 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
223 LocationSummary* locations = at_->GetLocations();
224 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
225
226 __ Bind(GetEntryLabel());
227 SaveLiveRegisters(codegen, locations);
228
229 InvokeRuntimeCallingConvention calling_convention;
230 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
231
Serban Constantinescufca16662016-07-14 09:21:59 +0100232 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
233 : kQuickInitializeType;
234 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200235 if (do_clinit_) {
236 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
237 } else {
238 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
239 }
240
241 // Move the class to the desired location.
242 Location out = locations->Out();
243 if (out.IsValid()) {
244 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
245 Primitive::Type type = at_->GetType();
246 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
247 }
248
249 RestoreLiveRegisters(codegen, locations);
250 __ B(GetExitLabel());
251 }
252
253 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
254
255 private:
256 // The class this slow path will load.
257 HLoadClass* const cls_;
258
259 // The instruction where this slow path is happening.
260 // (Might be the load class or an initialization check).
261 HInstruction* const at_;
262
263 // The dex PC of `at_`.
264 const uint32_t dex_pc_;
265
266 // Whether to initialize the class.
267 const bool do_clinit_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
270};
271
272class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
273 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000274 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200275
276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
277 LocationSummary* locations = instruction_->GetLocations();
278 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
279 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
280
281 __ Bind(GetEntryLabel());
282 SaveLiveRegisters(codegen, locations);
283
284 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
286 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufca16662016-07-14 09:21:59 +0100287 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200288 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
289 Primitive::Type type = instruction_->GetType();
290 mips_codegen->MoveLocation(locations->Out(),
291 calling_convention.GetReturnLocation(type),
292 type);
293
294 RestoreLiveRegisters(codegen, locations);
295 __ B(GetExitLabel());
296 }
297
298 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
299
300 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200301 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
302};
303
304class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
305 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000306 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307
308 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
309 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
310 __ Bind(GetEntryLabel());
311 if (instruction_->CanThrowIntoCatchBlock()) {
312 // Live registers will be restored in the catch block if caught.
313 SaveLiveRegisters(codegen, instruction_->GetLocations());
314 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100315 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 instruction_,
317 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100318 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200319 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
320 }
321
322 bool IsFatal() const OVERRIDE { return true; }
323
324 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
325
326 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200327 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
328};
329
330class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
331 public:
332 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000333 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200334
335 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
336 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
337 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100338 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200339 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200340 if (successor_ == nullptr) {
341 __ B(GetReturnLabel());
342 } else {
343 __ B(mips_codegen->GetLabelOf(successor_));
344 }
345 }
346
347 MipsLabel* GetReturnLabel() {
348 DCHECK(successor_ == nullptr);
349 return &return_label_;
350 }
351
352 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
353
354 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200355 // If not null, the block to branch to after the suspend check.
356 HBasicBlock* const successor_;
357
358 // If `successor_` is null, the label to branch to after the suspend check.
359 MipsLabel return_label_;
360
361 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
362};
363
364class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
365 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000366 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200367
368 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
369 LocationSummary* locations = instruction_->GetLocations();
370 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
371 uint32_t dex_pc = instruction_->GetDexPc();
372 DCHECK(instruction_->IsCheckCast()
373 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
374 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
375
376 __ Bind(GetEntryLabel());
377 SaveLiveRegisters(codegen, locations);
378
379 // We're moving two locations to locations that could overlap, so we need a parallel
380 // move resolver.
381 InvokeRuntimeCallingConvention calling_convention;
382 codegen->EmitParallelMoves(locations->InAt(1),
383 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
384 Primitive::kPrimNot,
385 object_class,
386 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
387 Primitive::kPrimNot);
388
389 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100390 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000391 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700392 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200393 Primitive::Type ret_type = instruction_->GetType();
394 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
395 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200396 } else {
397 DCHECK(instruction_->IsCheckCast());
Serban Constantinescufca16662016-07-14 09:21:59 +0100398 mips_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200399 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
400 }
401
402 RestoreLiveRegisters(codegen, locations);
403 __ B(GetExitLabel());
404 }
405
406 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
407
408 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
410};
411
412class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
413 public:
Aart Bik42249c32016-01-07 15:33:50 -0800414 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000415 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200416
417 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800418 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200419 __ Bind(GetEntryLabel());
420 SaveLiveRegisters(codegen, instruction_->GetLocations());
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 Frunze06a46c42016-07-19 15:00:40 -0700668 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
669 // (this can happen in leaf methods), artificially spill the ZERO register in order to
670 // force explicit saving and restoring of RA. RA isn't saved/restored when it's the only
671 // spilled register.
672 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
673 // saved in an unused temporary register) and saving of RA and the current method pointer
674 // in the frame.
675 if (clobbered_ra_ && core_spill_mask_ == (1u << RA) && fpu_spill_mask_ == 0) {
676 core_spill_mask_ |= (1 << ZERO);
677 }
Alexey Frunze73296a72016-06-03 22:51:46 -0700678}
679
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200680static dwarf::Reg DWARFReg(Register reg) {
681 return dwarf::Reg::MipsCore(static_cast<int>(reg));
682}
683
684// TODO: mapping of floating-point registers to DWARF.
685
686void CodeGeneratorMIPS::GenerateFrameEntry() {
687 __ Bind(&frame_entry_label_);
688
689 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
690
691 if (do_overflow_check) {
692 __ LoadFromOffset(kLoadWord,
693 ZERO,
694 SP,
695 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
696 RecordPcInfo(nullptr, 0);
697 }
698
699 if (HasEmptyFrame()) {
700 return;
701 }
702
703 // Make sure the frame size isn't unreasonably large.
704 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
705 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
706 }
707
708 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200709
Alexey Frunze73296a72016-06-03 22:51:46 -0700710 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200711 __ IncreaseFrameSize(ofs);
712
Alexey Frunze73296a72016-06-03 22:51:46 -0700713 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
714 Register reg = static_cast<Register>(MostSignificantBit(mask));
715 mask ^= 1u << reg;
716 ofs -= kMipsWordSize;
717 // The ZERO register is only included for alignment.
718 if (reg != ZERO) {
719 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200720 __ cfi().RelOffset(DWARFReg(reg), ofs);
721 }
722 }
723
Alexey Frunze73296a72016-06-03 22:51:46 -0700724 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
725 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
726 mask ^= 1u << reg;
727 ofs -= kMipsDoublewordSize;
728 __ StoreDToOffset(reg, SP, ofs);
729 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200730 }
731
Alexey Frunze73296a72016-06-03 22:51:46 -0700732 // Store the current method pointer.
733 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200734}
735
736void CodeGeneratorMIPS::GenerateFrameExit() {
737 __ cfi().RememberState();
738
739 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200740 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200741
Alexey Frunze73296a72016-06-03 22:51:46 -0700742 // For better instruction scheduling restore RA before other registers.
743 uint32_t ofs = GetFrameSize();
744 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
745 Register reg = static_cast<Register>(MostSignificantBit(mask));
746 mask ^= 1u << reg;
747 ofs -= kMipsWordSize;
748 // The ZERO register is only included for alignment.
749 if (reg != ZERO) {
750 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200751 __ cfi().Restore(DWARFReg(reg));
752 }
753 }
754
Alexey Frunze73296a72016-06-03 22:51:46 -0700755 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
756 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
757 mask ^= 1u << reg;
758 ofs -= kMipsDoublewordSize;
759 __ LoadDFromOffset(reg, SP, ofs);
760 // TODO: __ cfi().Restore(DWARFReg(reg));
761 }
762
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700763 size_t frame_size = GetFrameSize();
764 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
765 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
766 bool reordering = __ SetReorder(false);
767 if (exchange) {
768 __ Jr(RA);
769 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
770 } else {
771 __ DecreaseFrameSize(frame_size);
772 __ Jr(RA);
773 __ Nop(); // In delay slot.
774 }
775 __ SetReorder(reordering);
776 } else {
777 __ Jr(RA);
778 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200779 }
780
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200781 __ cfi().RestoreState();
782 __ cfi().DefCFAOffset(GetFrameSize());
783}
784
785void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
786 __ Bind(GetLabelOf(block));
787}
788
789void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
790 if (src.Equals(dst)) {
791 return;
792 }
793
794 if (src.IsConstant()) {
795 MoveConstant(dst, src.GetConstant());
796 } else {
797 if (Primitive::Is64BitType(dst_type)) {
798 Move64(dst, src);
799 } else {
800 Move32(dst, src);
801 }
802 }
803}
804
805void CodeGeneratorMIPS::Move32(Location destination, Location source) {
806 if (source.Equals(destination)) {
807 return;
808 }
809
810 if (destination.IsRegister()) {
811 if (source.IsRegister()) {
812 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
813 } else if (source.IsFpuRegister()) {
814 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
815 } else {
816 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
817 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
818 }
819 } else if (destination.IsFpuRegister()) {
820 if (source.IsRegister()) {
821 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
822 } else if (source.IsFpuRegister()) {
823 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
824 } else {
825 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
826 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
827 }
828 } else {
829 DCHECK(destination.IsStackSlot()) << destination;
830 if (source.IsRegister()) {
831 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
832 } else if (source.IsFpuRegister()) {
833 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
834 } else {
835 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
836 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
837 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
838 }
839 }
840}
841
842void CodeGeneratorMIPS::Move64(Location destination, Location source) {
843 if (source.Equals(destination)) {
844 return;
845 }
846
847 if (destination.IsRegisterPair()) {
848 if (source.IsRegisterPair()) {
849 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
850 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
851 } else if (source.IsFpuRegister()) {
852 Register dst_high = destination.AsRegisterPairHigh<Register>();
853 Register dst_low = destination.AsRegisterPairLow<Register>();
854 FRegister src = source.AsFpuRegister<FRegister>();
855 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800856 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200857 } else {
858 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
859 int32_t off = source.GetStackIndex();
860 Register r = destination.AsRegisterPairLow<Register>();
861 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
862 }
863 } else if (destination.IsFpuRegister()) {
864 if (source.IsRegisterPair()) {
865 FRegister dst = destination.AsFpuRegister<FRegister>();
866 Register src_high = source.AsRegisterPairHigh<Register>();
867 Register src_low = source.AsRegisterPairLow<Register>();
868 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800869 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200870 } else if (source.IsFpuRegister()) {
871 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
872 } else {
873 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
874 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
875 }
876 } else {
877 DCHECK(destination.IsDoubleStackSlot()) << destination;
878 int32_t off = destination.GetStackIndex();
879 if (source.IsRegisterPair()) {
880 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
881 } else if (source.IsFpuRegister()) {
882 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
883 } else {
884 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
885 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
886 __ StoreToOffset(kStoreWord, TMP, SP, off);
887 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
888 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
889 }
890 }
891}
892
893void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
894 if (c->IsIntConstant() || c->IsNullConstant()) {
895 // Move 32 bit constant.
896 int32_t value = GetInt32ValueOf(c);
897 if (destination.IsRegister()) {
898 Register dst = destination.AsRegister<Register>();
899 __ LoadConst32(dst, value);
900 } else {
901 DCHECK(destination.IsStackSlot())
902 << "Cannot move " << c->DebugName() << " to " << destination;
903 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
904 }
905 } else if (c->IsLongConstant()) {
906 // Move 64 bit constant.
907 int64_t value = GetInt64ValueOf(c);
908 if (destination.IsRegisterPair()) {
909 Register r_h = destination.AsRegisterPairHigh<Register>();
910 Register r_l = destination.AsRegisterPairLow<Register>();
911 __ LoadConst64(r_h, r_l, value);
912 } else {
913 DCHECK(destination.IsDoubleStackSlot())
914 << "Cannot move " << c->DebugName() << " to " << destination;
915 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
916 }
917 } else if (c->IsFloatConstant()) {
918 // Move 32 bit float constant.
919 int32_t value = GetInt32ValueOf(c);
920 if (destination.IsFpuRegister()) {
921 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
922 } else {
923 DCHECK(destination.IsStackSlot())
924 << "Cannot move " << c->DebugName() << " to " << destination;
925 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
926 }
927 } else {
928 // Move 64 bit double constant.
929 DCHECK(c->IsDoubleConstant()) << c->DebugName();
930 int64_t value = GetInt64ValueOf(c);
931 if (destination.IsFpuRegister()) {
932 FRegister fd = destination.AsFpuRegister<FRegister>();
933 __ LoadDConst64(fd, value, TMP);
934 } else {
935 DCHECK(destination.IsDoubleStackSlot())
936 << "Cannot move " << c->DebugName() << " to " << destination;
937 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
938 }
939 }
940}
941
942void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
943 DCHECK(destination.IsRegister());
944 Register dst = destination.AsRegister<Register>();
945 __ LoadConst32(dst, value);
946}
947
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200948void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
949 if (location.IsRegister()) {
950 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700951 } else if (location.IsRegisterPair()) {
952 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
953 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200954 } else {
955 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
956 }
957}
958
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700959void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
960 DCHECK(linker_patches->empty());
961 size_t size =
962 method_patches_.size() +
963 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700964 pc_relative_dex_cache_patches_.size() +
965 pc_relative_string_patches_.size() +
966 pc_relative_type_patches_.size() +
967 boot_image_string_patches_.size() +
968 boot_image_type_patches_.size() +
969 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700970 linker_patches->reserve(size);
971 for (const auto& entry : method_patches_) {
972 const MethodReference& target_method = entry.first;
973 Literal* literal = entry.second;
974 DCHECK(literal->GetLabel()->IsBound());
975 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
976 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
977 target_method.dex_file,
978 target_method.dex_method_index));
979 }
980 for (const auto& entry : call_patches_) {
981 const MethodReference& target_method = entry.first;
982 Literal* literal = entry.second;
983 DCHECK(literal->GetLabel()->IsBound());
984 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
985 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
986 target_method.dex_file,
987 target_method.dex_method_index));
988 }
989 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
990 const DexFile& dex_file = info.target_dex_file;
991 size_t base_element_offset = info.offset_or_index;
992 DCHECK(info.high_label.IsBound());
993 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
994 DCHECK(info.pc_rel_label.IsBound());
995 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
996 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
997 &dex_file,
998 pc_rel_offset,
999 base_element_offset));
1000 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001001 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
1002 const DexFile& dex_file = info.target_dex_file;
1003 size_t string_index = info.offset_or_index;
1004 DCHECK(info.high_label.IsBound());
1005 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1006 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1007 // the assembler's base label used for PC-relative literals.
1008 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1009 ? __ GetLabelLocation(&info.pc_rel_label)
1010 : __ GetPcRelBaseLabelLocation();
1011 linker_patches->push_back(LinkerPatch::RelativeStringPatch(high_offset,
1012 &dex_file,
1013 pc_rel_offset,
1014 string_index));
1015 }
1016 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
1017 const DexFile& dex_file = info.target_dex_file;
1018 size_t type_index = info.offset_or_index;
1019 DCHECK(info.high_label.IsBound());
1020 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1021 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1022 // the assembler's base label used for PC-relative literals.
1023 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1024 ? __ GetLabelLocation(&info.pc_rel_label)
1025 : __ GetPcRelBaseLabelLocation();
1026 linker_patches->push_back(LinkerPatch::RelativeTypePatch(high_offset,
1027 &dex_file,
1028 pc_rel_offset,
1029 type_index));
1030 }
1031 for (const auto& entry : boot_image_string_patches_) {
1032 const StringReference& target_string = entry.first;
1033 Literal* literal = entry.second;
1034 DCHECK(literal->GetLabel()->IsBound());
1035 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1036 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1037 target_string.dex_file,
1038 target_string.string_index));
1039 }
1040 for (const auto& entry : boot_image_type_patches_) {
1041 const TypeReference& target_type = entry.first;
1042 Literal* literal = entry.second;
1043 DCHECK(literal->GetLabel()->IsBound());
1044 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1045 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1046 target_type.dex_file,
1047 target_type.type_index));
1048 }
1049 for (const auto& entry : boot_image_address_patches_) {
1050 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1051 Literal* literal = entry.second;
1052 DCHECK(literal->GetLabel()->IsBound());
1053 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1054 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1055 }
1056}
1057
1058CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1059 const DexFile& dex_file, uint32_t string_index) {
1060 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1061}
1062
1063CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1064 const DexFile& dex_file, uint32_t type_index) {
1065 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001066}
1067
1068CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1069 const DexFile& dex_file, uint32_t element_offset) {
1070 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1071}
1072
1073CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1074 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1075 patches->emplace_back(dex_file, offset_or_index);
1076 return &patches->back();
1077}
1078
Alexey Frunze06a46c42016-07-19 15:00:40 -07001079Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1080 return map->GetOrCreate(
1081 value,
1082 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1083}
1084
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001085Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1086 MethodToLiteralMap* map) {
1087 return map->GetOrCreate(
1088 target_method,
1089 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1090}
1091
1092Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1093 return DeduplicateMethodLiteral(target_method, &method_patches_);
1094}
1095
1096Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1097 return DeduplicateMethodLiteral(target_method, &call_patches_);
1098}
1099
Alexey Frunze06a46c42016-07-19 15:00:40 -07001100Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1101 uint32_t string_index) {
1102 return boot_image_string_patches_.GetOrCreate(
1103 StringReference(&dex_file, string_index),
1104 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1105}
1106
1107Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1108 uint32_t type_index) {
1109 return boot_image_type_patches_.GetOrCreate(
1110 TypeReference(&dex_file, type_index),
1111 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1112}
1113
1114Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1115 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1116 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1117 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1118}
1119
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001120void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1121 MipsLabel done;
1122 Register card = AT;
1123 Register temp = TMP;
1124 __ Beqz(value, &done);
1125 __ LoadFromOffset(kLoadWord,
1126 card,
1127 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001128 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001129 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1130 __ Addu(temp, card, temp);
1131 __ Sb(card, temp, 0);
1132 __ Bind(&done);
1133}
1134
David Brazdil58282f42016-01-14 12:45:10 +00001135void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001136 // Don't allocate the dalvik style register pair passing.
1137 blocked_register_pairs_[A1_A2] = true;
1138
1139 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1140 blocked_core_registers_[ZERO] = true;
1141 blocked_core_registers_[K0] = true;
1142 blocked_core_registers_[K1] = true;
1143 blocked_core_registers_[GP] = true;
1144 blocked_core_registers_[SP] = true;
1145 blocked_core_registers_[RA] = true;
1146
1147 // AT and TMP(T8) are used as temporary/scratch registers
1148 // (similar to how AT is used by MIPS assemblers).
1149 blocked_core_registers_[AT] = true;
1150 blocked_core_registers_[TMP] = true;
1151 blocked_fpu_registers_[FTMP] = true;
1152
1153 // Reserve suspend and thread registers.
1154 blocked_core_registers_[S0] = true;
1155 blocked_core_registers_[TR] = true;
1156
1157 // Reserve T9 for function calls
1158 blocked_core_registers_[T9] = true;
1159
1160 // Reserve odd-numbered FPU registers.
1161 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1162 blocked_fpu_registers_[i] = true;
1163 }
1164
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001165 if (GetGraph()->IsDebuggable()) {
1166 // Stubs do not save callee-save floating point registers. If the graph
1167 // is debuggable, we need to deal with these registers differently. For
1168 // now, just block them.
1169 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1170 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1171 }
1172 }
1173
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001174 UpdateBlockedPairRegisters();
1175}
1176
1177void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1178 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1179 MipsManagedRegister current =
1180 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1181 if (blocked_core_registers_[current.AsRegisterPairLow()]
1182 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1183 blocked_register_pairs_[i] = true;
1184 }
1185 }
1186}
1187
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001188size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1189 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1190 return kMipsWordSize;
1191}
1192
1193size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1194 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1195 return kMipsWordSize;
1196}
1197
1198size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1199 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1200 return kMipsDoublewordSize;
1201}
1202
1203size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1204 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1205 return kMipsDoublewordSize;
1206}
1207
1208void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001209 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210}
1211
1212void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001213 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001214}
1215
Serban Constantinescufca16662016-07-14 09:21:59 +01001216constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1217
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001218void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1219 HInstruction* instruction,
1220 uint32_t dex_pc,
1221 SlowPathCode* slow_path) {
Serban Constantinescufca16662016-07-14 09:21:59 +01001222 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001223 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001224 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001225 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001226 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001227 // Reserve argument space on stack (for $a0-$a3) for
1228 // entrypoints that directly reference native implementations.
1229 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001230 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001231 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001232 } else {
1233 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001234 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001235 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001236 if (EntrypointRequiresStackMap(entrypoint)) {
1237 RecordPcInfo(instruction, dex_pc, slow_path);
1238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001239}
1240
1241void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1242 Register class_reg) {
1243 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1244 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1245 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1246 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1247 __ Sync(0);
1248 __ Bind(slow_path->GetExitLabel());
1249}
1250
1251void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1252 __ Sync(0); // Only stype 0 is supported.
1253}
1254
1255void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1256 HBasicBlock* successor) {
1257 SuspendCheckSlowPathMIPS* slow_path =
1258 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1259 codegen_->AddSlowPath(slow_path);
1260
1261 __ LoadFromOffset(kLoadUnsignedHalfword,
1262 TMP,
1263 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001264 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001265 if (successor == nullptr) {
1266 __ Bnez(TMP, slow_path->GetEntryLabel());
1267 __ Bind(slow_path->GetReturnLabel());
1268 } else {
1269 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1270 __ B(slow_path->GetEntryLabel());
1271 // slow_path will return to GetLabelOf(successor).
1272 }
1273}
1274
1275InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1276 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001277 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001278 assembler_(codegen->GetAssembler()),
1279 codegen_(codegen) {}
1280
1281void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1282 DCHECK_EQ(instruction->InputCount(), 2U);
1283 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1284 Primitive::Type type = instruction->GetResultType();
1285 switch (type) {
1286 case Primitive::kPrimInt: {
1287 locations->SetInAt(0, Location::RequiresRegister());
1288 HInstruction* right = instruction->InputAt(1);
1289 bool can_use_imm = false;
1290 if (right->IsConstant()) {
1291 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1292 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1293 can_use_imm = IsUint<16>(imm);
1294 } else if (instruction->IsAdd()) {
1295 can_use_imm = IsInt<16>(imm);
1296 } else {
1297 DCHECK(instruction->IsSub());
1298 can_use_imm = IsInt<16>(-imm);
1299 }
1300 }
1301 if (can_use_imm)
1302 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1303 else
1304 locations->SetInAt(1, Location::RequiresRegister());
1305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1306 break;
1307 }
1308
1309 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001311 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1312 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001313 break;
1314 }
1315
1316 case Primitive::kPrimFloat:
1317 case Primitive::kPrimDouble:
1318 DCHECK(instruction->IsAdd() || instruction->IsSub());
1319 locations->SetInAt(0, Location::RequiresFpuRegister());
1320 locations->SetInAt(1, Location::RequiresFpuRegister());
1321 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1322 break;
1323
1324 default:
1325 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1326 }
1327}
1328
1329void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1330 Primitive::Type type = instruction->GetType();
1331 LocationSummary* locations = instruction->GetLocations();
1332
1333 switch (type) {
1334 case Primitive::kPrimInt: {
1335 Register dst = locations->Out().AsRegister<Register>();
1336 Register lhs = locations->InAt(0).AsRegister<Register>();
1337 Location rhs_location = locations->InAt(1);
1338
1339 Register rhs_reg = ZERO;
1340 int32_t rhs_imm = 0;
1341 bool use_imm = rhs_location.IsConstant();
1342 if (use_imm) {
1343 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1344 } else {
1345 rhs_reg = rhs_location.AsRegister<Register>();
1346 }
1347
1348 if (instruction->IsAnd()) {
1349 if (use_imm)
1350 __ Andi(dst, lhs, rhs_imm);
1351 else
1352 __ And(dst, lhs, rhs_reg);
1353 } else if (instruction->IsOr()) {
1354 if (use_imm)
1355 __ Ori(dst, lhs, rhs_imm);
1356 else
1357 __ Or(dst, lhs, rhs_reg);
1358 } else if (instruction->IsXor()) {
1359 if (use_imm)
1360 __ Xori(dst, lhs, rhs_imm);
1361 else
1362 __ Xor(dst, lhs, rhs_reg);
1363 } else if (instruction->IsAdd()) {
1364 if (use_imm)
1365 __ Addiu(dst, lhs, rhs_imm);
1366 else
1367 __ Addu(dst, lhs, rhs_reg);
1368 } else {
1369 DCHECK(instruction->IsSub());
1370 if (use_imm)
1371 __ Addiu(dst, lhs, -rhs_imm);
1372 else
1373 __ Subu(dst, lhs, rhs_reg);
1374 }
1375 break;
1376 }
1377
1378 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001379 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1380 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1381 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1382 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001383 Location rhs_location = locations->InAt(1);
1384 bool use_imm = rhs_location.IsConstant();
1385 if (!use_imm) {
1386 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1387 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1388 if (instruction->IsAnd()) {
1389 __ And(dst_low, lhs_low, rhs_low);
1390 __ And(dst_high, lhs_high, rhs_high);
1391 } else if (instruction->IsOr()) {
1392 __ Or(dst_low, lhs_low, rhs_low);
1393 __ Or(dst_high, lhs_high, rhs_high);
1394 } else if (instruction->IsXor()) {
1395 __ Xor(dst_low, lhs_low, rhs_low);
1396 __ Xor(dst_high, lhs_high, rhs_high);
1397 } else if (instruction->IsAdd()) {
1398 if (lhs_low == rhs_low) {
1399 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1400 __ Slt(TMP, lhs_low, ZERO);
1401 __ Addu(dst_low, lhs_low, rhs_low);
1402 } else {
1403 __ Addu(dst_low, lhs_low, rhs_low);
1404 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1405 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1406 }
1407 __ Addu(dst_high, lhs_high, rhs_high);
1408 __ Addu(dst_high, dst_high, TMP);
1409 } else {
1410 DCHECK(instruction->IsSub());
1411 __ Sltu(TMP, lhs_low, rhs_low);
1412 __ Subu(dst_low, lhs_low, rhs_low);
1413 __ Subu(dst_high, lhs_high, rhs_high);
1414 __ Subu(dst_high, dst_high, TMP);
1415 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001416 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001417 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1418 if (instruction->IsOr()) {
1419 uint32_t low = Low32Bits(value);
1420 uint32_t high = High32Bits(value);
1421 if (IsUint<16>(low)) {
1422 if (dst_low != lhs_low || low != 0) {
1423 __ Ori(dst_low, lhs_low, low);
1424 }
1425 } else {
1426 __ LoadConst32(TMP, low);
1427 __ Or(dst_low, lhs_low, TMP);
1428 }
1429 if (IsUint<16>(high)) {
1430 if (dst_high != lhs_high || high != 0) {
1431 __ Ori(dst_high, lhs_high, high);
1432 }
1433 } else {
1434 if (high != low) {
1435 __ LoadConst32(TMP, high);
1436 }
1437 __ Or(dst_high, lhs_high, TMP);
1438 }
1439 } else if (instruction->IsXor()) {
1440 uint32_t low = Low32Bits(value);
1441 uint32_t high = High32Bits(value);
1442 if (IsUint<16>(low)) {
1443 if (dst_low != lhs_low || low != 0) {
1444 __ Xori(dst_low, lhs_low, low);
1445 }
1446 } else {
1447 __ LoadConst32(TMP, low);
1448 __ Xor(dst_low, lhs_low, TMP);
1449 }
1450 if (IsUint<16>(high)) {
1451 if (dst_high != lhs_high || high != 0) {
1452 __ Xori(dst_high, lhs_high, high);
1453 }
1454 } else {
1455 if (high != low) {
1456 __ LoadConst32(TMP, high);
1457 }
1458 __ Xor(dst_high, lhs_high, TMP);
1459 }
1460 } else if (instruction->IsAnd()) {
1461 uint32_t low = Low32Bits(value);
1462 uint32_t high = High32Bits(value);
1463 if (IsUint<16>(low)) {
1464 __ Andi(dst_low, lhs_low, low);
1465 } else if (low != 0xFFFFFFFF) {
1466 __ LoadConst32(TMP, low);
1467 __ And(dst_low, lhs_low, TMP);
1468 } else if (dst_low != lhs_low) {
1469 __ Move(dst_low, lhs_low);
1470 }
1471 if (IsUint<16>(high)) {
1472 __ Andi(dst_high, lhs_high, high);
1473 } else if (high != 0xFFFFFFFF) {
1474 if (high != low) {
1475 __ LoadConst32(TMP, high);
1476 }
1477 __ And(dst_high, lhs_high, TMP);
1478 } else if (dst_high != lhs_high) {
1479 __ Move(dst_high, lhs_high);
1480 }
1481 } else {
1482 if (instruction->IsSub()) {
1483 value = -value;
1484 } else {
1485 DCHECK(instruction->IsAdd());
1486 }
1487 int32_t low = Low32Bits(value);
1488 int32_t high = High32Bits(value);
1489 if (IsInt<16>(low)) {
1490 if (dst_low != lhs_low || low != 0) {
1491 __ Addiu(dst_low, lhs_low, low);
1492 }
1493 if (low != 0) {
1494 __ Sltiu(AT, dst_low, low);
1495 }
1496 } else {
1497 __ LoadConst32(TMP, low);
1498 __ Addu(dst_low, lhs_low, TMP);
1499 __ Sltu(AT, dst_low, TMP);
1500 }
1501 if (IsInt<16>(high)) {
1502 if (dst_high != lhs_high || high != 0) {
1503 __ Addiu(dst_high, lhs_high, high);
1504 }
1505 } else {
1506 if (high != low) {
1507 __ LoadConst32(TMP, high);
1508 }
1509 __ Addu(dst_high, lhs_high, TMP);
1510 }
1511 if (low != 0) {
1512 __ Addu(dst_high, dst_high, AT);
1513 }
1514 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001515 }
1516 break;
1517 }
1518
1519 case Primitive::kPrimFloat:
1520 case Primitive::kPrimDouble: {
1521 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1522 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1523 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1524 if (instruction->IsAdd()) {
1525 if (type == Primitive::kPrimFloat) {
1526 __ AddS(dst, lhs, rhs);
1527 } else {
1528 __ AddD(dst, lhs, rhs);
1529 }
1530 } else {
1531 DCHECK(instruction->IsSub());
1532 if (type == Primitive::kPrimFloat) {
1533 __ SubS(dst, lhs, rhs);
1534 } else {
1535 __ SubD(dst, lhs, rhs);
1536 }
1537 }
1538 break;
1539 }
1540
1541 default:
1542 LOG(FATAL) << "Unexpected binary operation type " << type;
1543 }
1544}
1545
1546void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001547 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548
1549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1550 Primitive::Type type = instr->GetResultType();
1551 switch (type) {
1552 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001553 locations->SetInAt(0, Location::RequiresRegister());
1554 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1556 break;
1557 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1560 locations->SetOut(Location::RequiresRegister());
1561 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001562 default:
1563 LOG(FATAL) << "Unexpected shift type " << type;
1564 }
1565}
1566
1567static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1568
1569void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001570 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001571 LocationSummary* locations = instr->GetLocations();
1572 Primitive::Type type = instr->GetType();
1573
1574 Location rhs_location = locations->InAt(1);
1575 bool use_imm = rhs_location.IsConstant();
1576 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1577 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001578 const uint32_t shift_mask =
1579 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001580 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001581 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1582 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001583
1584 switch (type) {
1585 case Primitive::kPrimInt: {
1586 Register dst = locations->Out().AsRegister<Register>();
1587 Register lhs = locations->InAt(0).AsRegister<Register>();
1588 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001589 if (shift_value == 0) {
1590 if (dst != lhs) {
1591 __ Move(dst, lhs);
1592 }
1593 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001594 __ Sll(dst, lhs, shift_value);
1595 } else if (instr->IsShr()) {
1596 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001597 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001598 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001599 } else {
1600 if (has_ins_rotr) {
1601 __ Rotr(dst, lhs, shift_value);
1602 } else {
1603 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1604 __ Srl(dst, lhs, shift_value);
1605 __ Or(dst, dst, TMP);
1606 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001607 }
1608 } else {
1609 if (instr->IsShl()) {
1610 __ Sllv(dst, lhs, rhs_reg);
1611 } else if (instr->IsShr()) {
1612 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001613 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001614 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001615 } else {
1616 if (has_ins_rotr) {
1617 __ Rotrv(dst, lhs, rhs_reg);
1618 } else {
1619 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001620 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1621 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1622 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1623 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1624 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001625 __ Sllv(TMP, lhs, TMP);
1626 __ Srlv(dst, lhs, rhs_reg);
1627 __ Or(dst, dst, TMP);
1628 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001629 }
1630 }
1631 break;
1632 }
1633
1634 case Primitive::kPrimLong: {
1635 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1636 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1637 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1638 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1639 if (use_imm) {
1640 if (shift_value == 0) {
1641 codegen_->Move64(locations->Out(), locations->InAt(0));
1642 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001643 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001644 if (instr->IsShl()) {
1645 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1646 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1647 __ Sll(dst_low, lhs_low, shift_value);
1648 } else if (instr->IsShr()) {
1649 __ Srl(dst_low, lhs_low, shift_value);
1650 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1651 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001652 } else if (instr->IsUShr()) {
1653 __ Srl(dst_low, lhs_low, shift_value);
1654 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1655 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001656 } else {
1657 __ Srl(dst_low, lhs_low, shift_value);
1658 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1659 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001660 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001661 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001662 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001663 if (instr->IsShl()) {
1664 __ Sll(dst_low, lhs_low, shift_value);
1665 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1666 __ Sll(dst_high, lhs_high, shift_value);
1667 __ Or(dst_high, dst_high, TMP);
1668 } else if (instr->IsShr()) {
1669 __ Sra(dst_high, lhs_high, shift_value);
1670 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1671 __ Srl(dst_low, lhs_low, shift_value);
1672 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001673 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001674 __ Srl(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 {
1679 __ Srl(TMP, lhs_low, shift_value);
1680 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1681 __ Or(dst_low, dst_low, TMP);
1682 __ Srl(TMP, lhs_high, shift_value);
1683 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1684 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001685 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001686 }
1687 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001688 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001689 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001690 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001691 __ Move(dst_low, ZERO);
1692 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001693 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001694 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001695 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001696 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001697 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001698 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001699 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001700 // 64-bit rotation by 32 is just a swap.
1701 __ Move(dst_low, lhs_high);
1702 __ Move(dst_high, lhs_low);
1703 } else {
1704 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001705 __ Srl(dst_low, lhs_high, shift_value_high);
1706 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1707 __ Srl(dst_high, lhs_low, shift_value_high);
1708 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001709 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001710 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1711 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001712 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001713 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1714 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001715 __ Or(dst_high, dst_high, TMP);
1716 }
1717 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 }
1719 }
1720 } else {
1721 MipsLabel done;
1722 if (instr->IsShl()) {
1723 __ Sllv(dst_low, lhs_low, rhs_reg);
1724 __ Nor(AT, ZERO, rhs_reg);
1725 __ Srl(TMP, lhs_low, 1);
1726 __ Srlv(TMP, TMP, AT);
1727 __ Sllv(dst_high, lhs_high, rhs_reg);
1728 __ Or(dst_high, dst_high, TMP);
1729 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1730 __ Beqz(TMP, &done);
1731 __ Move(dst_high, dst_low);
1732 __ Move(dst_low, ZERO);
1733 } else if (instr->IsShr()) {
1734 __ Srav(dst_high, lhs_high, rhs_reg);
1735 __ Nor(AT, ZERO, rhs_reg);
1736 __ Sll(TMP, lhs_high, 1);
1737 __ Sllv(TMP, TMP, AT);
1738 __ Srlv(dst_low, lhs_low, rhs_reg);
1739 __ Or(dst_low, dst_low, TMP);
1740 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1741 __ Beqz(TMP, &done);
1742 __ Move(dst_low, dst_high);
1743 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001744 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001745 __ Srlv(dst_high, lhs_high, rhs_reg);
1746 __ Nor(AT, ZERO, rhs_reg);
1747 __ Sll(TMP, lhs_high, 1);
1748 __ Sllv(TMP, TMP, AT);
1749 __ Srlv(dst_low, lhs_low, rhs_reg);
1750 __ Or(dst_low, dst_low, TMP);
1751 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1752 __ Beqz(TMP, &done);
1753 __ Move(dst_low, dst_high);
1754 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001755 } else {
1756 __ Nor(AT, ZERO, rhs_reg);
1757 __ Srlv(TMP, lhs_low, rhs_reg);
1758 __ Sll(dst_low, lhs_high, 1);
1759 __ Sllv(dst_low, dst_low, AT);
1760 __ Or(dst_low, dst_low, TMP);
1761 __ Srlv(TMP, lhs_high, rhs_reg);
1762 __ Sll(dst_high, lhs_low, 1);
1763 __ Sllv(dst_high, dst_high, AT);
1764 __ Or(dst_high, dst_high, TMP);
1765 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1766 __ Beqz(TMP, &done);
1767 __ Move(TMP, dst_high);
1768 __ Move(dst_high, dst_low);
1769 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001770 }
1771 __ Bind(&done);
1772 }
1773 break;
1774 }
1775
1776 default:
1777 LOG(FATAL) << "Unexpected shift operation type " << type;
1778 }
1779}
1780
1781void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1782 HandleBinaryOp(instruction);
1783}
1784
1785void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1786 HandleBinaryOp(instruction);
1787}
1788
1789void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1790 HandleBinaryOp(instruction);
1791}
1792
1793void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1794 HandleBinaryOp(instruction);
1795}
1796
1797void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1800 locations->SetInAt(0, Location::RequiresRegister());
1801 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1802 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1803 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1804 } else {
1805 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1806 }
1807}
1808
Alexey Frunze2923db72016-08-20 01:55:47 -07001809auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1810 auto null_checker = [this, instruction]() {
1811 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1812 };
1813 return null_checker;
1814}
1815
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001816void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1817 LocationSummary* locations = instruction->GetLocations();
1818 Register obj = locations->InAt(0).AsRegister<Register>();
1819 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001820 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001821 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001822
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001823 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001824 switch (type) {
1825 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001826 Register out = locations->Out().AsRegister<Register>();
1827 if (index.IsConstant()) {
1828 size_t offset =
1829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001830 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001831 } else {
1832 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001833 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001834 }
1835 break;
1836 }
1837
1838 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001839 Register out = locations->Out().AsRegister<Register>();
1840 if (index.IsConstant()) {
1841 size_t offset =
1842 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001843 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001844 } else {
1845 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001846 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001847 }
1848 break;
1849 }
1850
1851 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852 Register out = locations->Out().AsRegister<Register>();
1853 if (index.IsConstant()) {
1854 size_t offset =
1855 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001856 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 } else {
1858 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1859 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001860 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 }
1862 break;
1863 }
1864
1865 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 Register out = locations->Out().AsRegister<Register>();
1867 if (index.IsConstant()) {
1868 size_t offset =
1869 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001870 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 } else {
1872 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1873 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001874 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001875 }
1876 break;
1877 }
1878
1879 case Primitive::kPrimInt:
1880 case Primitive::kPrimNot: {
1881 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001882 Register out = locations->Out().AsRegister<Register>();
1883 if (index.IsConstant()) {
1884 size_t offset =
1885 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001886 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887 } else {
1888 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1889 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001890 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001891 }
1892 break;
1893 }
1894
1895 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001896 Register out = locations->Out().AsRegisterPairLow<Register>();
1897 if (index.IsConstant()) {
1898 size_t offset =
1899 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001900 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901 } else {
1902 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1903 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001904 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001905 }
1906 break;
1907 }
1908
1909 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1911 if (index.IsConstant()) {
1912 size_t offset =
1913 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001914 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 } else {
1916 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1917 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001918 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 }
1920 break;
1921 }
1922
1923 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001924 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1925 if (index.IsConstant()) {
1926 size_t offset =
1927 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001928 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 } else {
1930 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1931 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001932 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933 }
1934 break;
1935 }
1936
1937 case Primitive::kPrimVoid:
1938 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1939 UNREACHABLE();
1940 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001941}
1942
1943void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1945 locations->SetInAt(0, Location::RequiresRegister());
1946 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1947}
1948
1949void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1950 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001951 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 Register obj = locations->InAt(0).AsRegister<Register>();
1953 Register out = locations->Out().AsRegister<Register>();
1954 __ LoadFromOffset(kLoadWord, out, obj, offset);
1955 codegen_->MaybeRecordImplicitNullCheck(instruction);
1956}
1957
1958void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001959 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001960 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1961 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001962 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001963 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001964 InvokeRuntimeCallingConvention calling_convention;
1965 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1966 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1967 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1968 } else {
1969 locations->SetInAt(0, Location::RequiresRegister());
1970 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1971 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1972 locations->SetInAt(2, Location::RequiresFpuRegister());
1973 } else {
1974 locations->SetInAt(2, Location::RequiresRegister());
1975 }
1976 }
1977}
1978
1979void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1980 LocationSummary* locations = instruction->GetLocations();
1981 Register obj = locations->InAt(0).AsRegister<Register>();
1982 Location index = locations->InAt(1);
1983 Primitive::Type value_type = instruction->GetComponentType();
1984 bool needs_runtime_call = locations->WillCall();
1985 bool needs_write_barrier =
1986 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07001987 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001988
1989 switch (value_type) {
1990 case Primitive::kPrimBoolean:
1991 case Primitive::kPrimByte: {
1992 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1993 Register value = locations->InAt(2).AsRegister<Register>();
1994 if (index.IsConstant()) {
1995 size_t offset =
1996 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001997 __ StoreToOffset(kStoreByte, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001998 } else {
1999 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002000 __ StoreToOffset(kStoreByte, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002001 }
2002 break;
2003 }
2004
2005 case Primitive::kPrimShort:
2006 case Primitive::kPrimChar: {
2007 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
2008 Register value = locations->InAt(2).AsRegister<Register>();
2009 if (index.IsConstant()) {
2010 size_t offset =
2011 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002012 __ StoreToOffset(kStoreHalfword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002013 } else {
2014 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
2015 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002016 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002017 }
2018 break;
2019 }
2020
2021 case Primitive::kPrimInt:
2022 case Primitive::kPrimNot: {
2023 if (!needs_runtime_call) {
2024 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2025 Register value = locations->InAt(2).AsRegister<Register>();
2026 if (index.IsConstant()) {
2027 size_t offset =
2028 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002029 __ StoreToOffset(kStoreWord, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002030 } else {
2031 DCHECK(index.IsRegister()) << index;
2032 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2033 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002034 __ StoreToOffset(kStoreWord, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002036 if (needs_write_barrier) {
2037 DCHECK_EQ(value_type, Primitive::kPrimNot);
2038 codegen_->MarkGCCard(obj, value);
2039 }
2040 } else {
2041 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002042 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002043 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2044 }
2045 break;
2046 }
2047
2048 case Primitive::kPrimLong: {
2049 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2050 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2051 if (index.IsConstant()) {
2052 size_t offset =
2053 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002054 __ StoreToOffset(kStoreDoubleword, value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002055 } else {
2056 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2057 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002058 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059 }
2060 break;
2061 }
2062
2063 case Primitive::kPrimFloat: {
2064 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2065 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2066 DCHECK(locations->InAt(2).IsFpuRegister());
2067 if (index.IsConstant()) {
2068 size_t offset =
2069 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002070 __ StoreSToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002071 } else {
2072 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2073 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002074 __ StoreSToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002075 }
2076 break;
2077 }
2078
2079 case Primitive::kPrimDouble: {
2080 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2081 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2082 DCHECK(locations->InAt(2).IsFpuRegister());
2083 if (index.IsConstant()) {
2084 size_t offset =
2085 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002086 __ StoreDToOffset(value, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 } else {
2088 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2089 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002090 __ StoreDToOffset(value, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002091 }
2092 break;
2093 }
2094
2095 case Primitive::kPrimVoid:
2096 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2097 UNREACHABLE();
2098 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002099}
2100
2101void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2102 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2103 ? LocationSummary::kCallOnSlowPath
2104 : LocationSummary::kNoCall;
2105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2106 locations->SetInAt(0, Location::RequiresRegister());
2107 locations->SetInAt(1, Location::RequiresRegister());
2108 if (instruction->HasUses()) {
2109 locations->SetOut(Location::SameAsFirstInput());
2110 }
2111}
2112
2113void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2114 LocationSummary* locations = instruction->GetLocations();
2115 BoundsCheckSlowPathMIPS* slow_path =
2116 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2117 codegen_->AddSlowPath(slow_path);
2118
2119 Register index = locations->InAt(0).AsRegister<Register>();
2120 Register length = locations->InAt(1).AsRegister<Register>();
2121
2122 // length is limited by the maximum positive signed 32-bit integer.
2123 // Unsigned comparison of length and index checks for index < 0
2124 // and for length <= index simultaneously.
2125 __ Bgeu(index, length, slow_path->GetEntryLabel());
2126}
2127
2128void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2129 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2130 instruction,
2131 LocationSummary::kCallOnSlowPath);
2132 locations->SetInAt(0, Location::RequiresRegister());
2133 locations->SetInAt(1, Location::RequiresRegister());
2134 // Note that TypeCheckSlowPathMIPS uses this register too.
2135 locations->AddTemp(Location::RequiresRegister());
2136}
2137
2138void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2139 LocationSummary* locations = instruction->GetLocations();
2140 Register obj = locations->InAt(0).AsRegister<Register>();
2141 Register cls = locations->InAt(1).AsRegister<Register>();
2142 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2143
2144 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2145 codegen_->AddSlowPath(slow_path);
2146
2147 // TODO: avoid this check if we know obj is not null.
2148 __ Beqz(obj, slow_path->GetExitLabel());
2149 // Compare the class of `obj` with `cls`.
2150 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2151 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2152 __ Bind(slow_path->GetExitLabel());
2153}
2154
2155void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2156 LocationSummary* locations =
2157 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2158 locations->SetInAt(0, Location::RequiresRegister());
2159 if (check->HasUses()) {
2160 locations->SetOut(Location::SameAsFirstInput());
2161 }
2162}
2163
2164void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2165 // We assume the class is not null.
2166 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2167 check->GetLoadClass(),
2168 check,
2169 check->GetDexPc(),
2170 true);
2171 codegen_->AddSlowPath(slow_path);
2172 GenerateClassInitializationCheck(slow_path,
2173 check->GetLocations()->InAt(0).AsRegister<Register>());
2174}
2175
2176void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2177 Primitive::Type in_type = compare->InputAt(0)->GetType();
2178
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002179 LocationSummary* locations =
2180 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002181
2182 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002183 case Primitive::kPrimBoolean:
2184 case Primitive::kPrimByte:
2185 case Primitive::kPrimShort:
2186 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002187 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002188 case Primitive::kPrimLong:
2189 locations->SetInAt(0, Location::RequiresRegister());
2190 locations->SetInAt(1, Location::RequiresRegister());
2191 // Output overlaps because it is written before doing the low comparison.
2192 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2193 break;
2194
2195 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002196 case Primitive::kPrimDouble:
2197 locations->SetInAt(0, Location::RequiresFpuRegister());
2198 locations->SetInAt(1, Location::RequiresFpuRegister());
2199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002200 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002201
2202 default:
2203 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2204 }
2205}
2206
2207void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2208 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002209 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002210 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002211 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002212
2213 // 0 if: left == right
2214 // 1 if: left > right
2215 // -1 if: left < right
2216 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002217 case Primitive::kPrimBoolean:
2218 case Primitive::kPrimByte:
2219 case Primitive::kPrimShort:
2220 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002221 case Primitive::kPrimInt: {
2222 Register lhs = locations->InAt(0).AsRegister<Register>();
2223 Register rhs = locations->InAt(1).AsRegister<Register>();
2224 __ Slt(TMP, lhs, rhs);
2225 __ Slt(res, rhs, lhs);
2226 __ Subu(res, res, TMP);
2227 break;
2228 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002229 case Primitive::kPrimLong: {
2230 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002231 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2232 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2233 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2234 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2235 // TODO: more efficient (direct) comparison with a constant.
2236 __ Slt(TMP, lhs_high, rhs_high);
2237 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2238 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2239 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2240 __ Sltu(TMP, lhs_low, rhs_low);
2241 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2242 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2243 __ Bind(&done);
2244 break;
2245 }
2246
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002247 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002248 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002249 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2250 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2251 MipsLabel done;
2252 if (isR6) {
2253 __ CmpEqS(FTMP, lhs, rhs);
2254 __ LoadConst32(res, 0);
2255 __ Bc1nez(FTMP, &done);
2256 if (gt_bias) {
2257 __ CmpLtS(FTMP, lhs, rhs);
2258 __ LoadConst32(res, -1);
2259 __ Bc1nez(FTMP, &done);
2260 __ LoadConst32(res, 1);
2261 } else {
2262 __ CmpLtS(FTMP, rhs, lhs);
2263 __ LoadConst32(res, 1);
2264 __ Bc1nez(FTMP, &done);
2265 __ LoadConst32(res, -1);
2266 }
2267 } else {
2268 if (gt_bias) {
2269 __ ColtS(0, lhs, rhs);
2270 __ LoadConst32(res, -1);
2271 __ Bc1t(0, &done);
2272 __ CeqS(0, lhs, rhs);
2273 __ LoadConst32(res, 1);
2274 __ Movt(res, ZERO, 0);
2275 } else {
2276 __ ColtS(0, rhs, lhs);
2277 __ LoadConst32(res, 1);
2278 __ Bc1t(0, &done);
2279 __ CeqS(0, lhs, rhs);
2280 __ LoadConst32(res, -1);
2281 __ Movt(res, ZERO, 0);
2282 }
2283 }
2284 __ Bind(&done);
2285 break;
2286 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002287 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002288 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002289 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2290 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2291 MipsLabel done;
2292 if (isR6) {
2293 __ CmpEqD(FTMP, lhs, rhs);
2294 __ LoadConst32(res, 0);
2295 __ Bc1nez(FTMP, &done);
2296 if (gt_bias) {
2297 __ CmpLtD(FTMP, lhs, rhs);
2298 __ LoadConst32(res, -1);
2299 __ Bc1nez(FTMP, &done);
2300 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002302 __ CmpLtD(FTMP, rhs, lhs);
2303 __ LoadConst32(res, 1);
2304 __ Bc1nez(FTMP, &done);
2305 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002306 }
2307 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002308 if (gt_bias) {
2309 __ ColtD(0, lhs, rhs);
2310 __ LoadConst32(res, -1);
2311 __ Bc1t(0, &done);
2312 __ CeqD(0, lhs, rhs);
2313 __ LoadConst32(res, 1);
2314 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002315 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002316 __ ColtD(0, rhs, lhs);
2317 __ LoadConst32(res, 1);
2318 __ Bc1t(0, &done);
2319 __ CeqD(0, lhs, rhs);
2320 __ LoadConst32(res, -1);
2321 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002322 }
2323 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002324 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002325 break;
2326 }
2327
2328 default:
2329 LOG(FATAL) << "Unimplemented compare type " << in_type;
2330 }
2331}
2332
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002333void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002335 switch (instruction->InputAt(0)->GetType()) {
2336 default:
2337 case Primitive::kPrimLong:
2338 locations->SetInAt(0, Location::RequiresRegister());
2339 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2340 break;
2341
2342 case Primitive::kPrimFloat:
2343 case Primitive::kPrimDouble:
2344 locations->SetInAt(0, Location::RequiresFpuRegister());
2345 locations->SetInAt(1, Location::RequiresFpuRegister());
2346 break;
2347 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002348 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2350 }
2351}
2352
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002353void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002354 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002355 return;
2356 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002357
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002358 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002359 LocationSummary* locations = instruction->GetLocations();
2360 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002361 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002362
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002363 switch (type) {
2364 default:
2365 // Integer case.
2366 GenerateIntCompare(instruction->GetCondition(), locations);
2367 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002369 case Primitive::kPrimLong:
2370 // TODO: don't use branches.
2371 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002372 break;
2373
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002374 case Primitive::kPrimFloat:
2375 case Primitive::kPrimDouble:
2376 // TODO: don't use branches.
2377 GenerateFpCompareAndBranch(instruction->GetCondition(),
2378 instruction->IsGtBias(),
2379 type,
2380 locations,
2381 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002382 break;
2383 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002384
2385 // Convert the branches into the result.
2386 MipsLabel done;
2387
2388 // False case: result = 0.
2389 __ LoadConst32(dst, 0);
2390 __ B(&done);
2391
2392 // True case: result = 1.
2393 __ Bind(&true_label);
2394 __ LoadConst32(dst, 1);
2395 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002396}
2397
Alexey Frunze7e99e052015-11-24 19:28:01 -08002398void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2399 DCHECK(instruction->IsDiv() || instruction->IsRem());
2400 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2401
2402 LocationSummary* locations = instruction->GetLocations();
2403 Location second = locations->InAt(1);
2404 DCHECK(second.IsConstant());
2405
2406 Register out = locations->Out().AsRegister<Register>();
2407 Register dividend = locations->InAt(0).AsRegister<Register>();
2408 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2409 DCHECK(imm == 1 || imm == -1);
2410
2411 if (instruction->IsRem()) {
2412 __ Move(out, ZERO);
2413 } else {
2414 if (imm == -1) {
2415 __ Subu(out, ZERO, dividend);
2416 } else if (out != dividend) {
2417 __ Move(out, dividend);
2418 }
2419 }
2420}
2421
2422void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2423 DCHECK(instruction->IsDiv() || instruction->IsRem());
2424 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2425
2426 LocationSummary* locations = instruction->GetLocations();
2427 Location second = locations->InAt(1);
2428 DCHECK(second.IsConstant());
2429
2430 Register out = locations->Out().AsRegister<Register>();
2431 Register dividend = locations->InAt(0).AsRegister<Register>();
2432 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002433 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002434 int ctz_imm = CTZ(abs_imm);
2435
2436 if (instruction->IsDiv()) {
2437 if (ctz_imm == 1) {
2438 // Fast path for division by +/-2, which is very common.
2439 __ Srl(TMP, dividend, 31);
2440 } else {
2441 __ Sra(TMP, dividend, 31);
2442 __ Srl(TMP, TMP, 32 - ctz_imm);
2443 }
2444 __ Addu(out, dividend, TMP);
2445 __ Sra(out, out, ctz_imm);
2446 if (imm < 0) {
2447 __ Subu(out, ZERO, out);
2448 }
2449 } else {
2450 if (ctz_imm == 1) {
2451 // Fast path for modulo +/-2, which is very common.
2452 __ Sra(TMP, dividend, 31);
2453 __ Subu(out, dividend, TMP);
2454 __ Andi(out, out, 1);
2455 __ Addu(out, out, TMP);
2456 } else {
2457 __ Sra(TMP, dividend, 31);
2458 __ Srl(TMP, TMP, 32 - ctz_imm);
2459 __ Addu(out, dividend, TMP);
2460 if (IsUint<16>(abs_imm - 1)) {
2461 __ Andi(out, out, abs_imm - 1);
2462 } else {
2463 __ Sll(out, out, 32 - ctz_imm);
2464 __ Srl(out, out, 32 - ctz_imm);
2465 }
2466 __ Subu(out, out, TMP);
2467 }
2468 }
2469}
2470
2471void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2472 DCHECK(instruction->IsDiv() || instruction->IsRem());
2473 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2474
2475 LocationSummary* locations = instruction->GetLocations();
2476 Location second = locations->InAt(1);
2477 DCHECK(second.IsConstant());
2478
2479 Register out = locations->Out().AsRegister<Register>();
2480 Register dividend = locations->InAt(0).AsRegister<Register>();
2481 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2482
2483 int64_t magic;
2484 int shift;
2485 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2486
2487 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2488
2489 __ LoadConst32(TMP, magic);
2490 if (isR6) {
2491 __ MuhR6(TMP, dividend, TMP);
2492 } else {
2493 __ MultR2(dividend, TMP);
2494 __ Mfhi(TMP);
2495 }
2496 if (imm > 0 && magic < 0) {
2497 __ Addu(TMP, TMP, dividend);
2498 } else if (imm < 0 && magic > 0) {
2499 __ Subu(TMP, TMP, dividend);
2500 }
2501
2502 if (shift != 0) {
2503 __ Sra(TMP, TMP, shift);
2504 }
2505
2506 if (instruction->IsDiv()) {
2507 __ Sra(out, TMP, 31);
2508 __ Subu(out, TMP, out);
2509 } else {
2510 __ Sra(AT, TMP, 31);
2511 __ Subu(AT, TMP, AT);
2512 __ LoadConst32(TMP, imm);
2513 if (isR6) {
2514 __ MulR6(TMP, AT, TMP);
2515 } else {
2516 __ MulR2(TMP, AT, TMP);
2517 }
2518 __ Subu(out, dividend, TMP);
2519 }
2520}
2521
2522void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2523 DCHECK(instruction->IsDiv() || instruction->IsRem());
2524 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2525
2526 LocationSummary* locations = instruction->GetLocations();
2527 Register out = locations->Out().AsRegister<Register>();
2528 Location second = locations->InAt(1);
2529
2530 if (second.IsConstant()) {
2531 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2532 if (imm == 0) {
2533 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2534 } else if (imm == 1 || imm == -1) {
2535 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002536 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002537 DivRemByPowerOfTwo(instruction);
2538 } else {
2539 DCHECK(imm <= -2 || imm >= 2);
2540 GenerateDivRemWithAnyConstant(instruction);
2541 }
2542 } else {
2543 Register dividend = locations->InAt(0).AsRegister<Register>();
2544 Register divisor = second.AsRegister<Register>();
2545 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2546 if (instruction->IsDiv()) {
2547 if (isR6) {
2548 __ DivR6(out, dividend, divisor);
2549 } else {
2550 __ DivR2(out, dividend, divisor);
2551 }
2552 } else {
2553 if (isR6) {
2554 __ ModR6(out, dividend, divisor);
2555 } else {
2556 __ ModR2(out, dividend, divisor);
2557 }
2558 }
2559 }
2560}
2561
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002562void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2563 Primitive::Type type = div->GetResultType();
2564 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002565 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002566 : LocationSummary::kNoCall;
2567
2568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2569
2570 switch (type) {
2571 case Primitive::kPrimInt:
2572 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002573 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002574 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2575 break;
2576
2577 case Primitive::kPrimLong: {
2578 InvokeRuntimeCallingConvention calling_convention;
2579 locations->SetInAt(0, Location::RegisterPairLocation(
2580 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2581 locations->SetInAt(1, Location::RegisterPairLocation(
2582 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2583 locations->SetOut(calling_convention.GetReturnLocation(type));
2584 break;
2585 }
2586
2587 case Primitive::kPrimFloat:
2588 case Primitive::kPrimDouble:
2589 locations->SetInAt(0, Location::RequiresFpuRegister());
2590 locations->SetInAt(1, Location::RequiresFpuRegister());
2591 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2592 break;
2593
2594 default:
2595 LOG(FATAL) << "Unexpected div type " << type;
2596 }
2597}
2598
2599void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2600 Primitive::Type type = instruction->GetType();
2601 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002602
2603 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002604 case Primitive::kPrimInt:
2605 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002606 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002608 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002609 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2610 break;
2611 }
2612 case Primitive::kPrimFloat:
2613 case Primitive::kPrimDouble: {
2614 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2615 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2616 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2617 if (type == Primitive::kPrimFloat) {
2618 __ DivS(dst, lhs, rhs);
2619 } else {
2620 __ DivD(dst, lhs, rhs);
2621 }
2622 break;
2623 }
2624 default:
2625 LOG(FATAL) << "Unexpected div type " << type;
2626 }
2627}
2628
2629void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2630 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2631 ? LocationSummary::kCallOnSlowPath
2632 : LocationSummary::kNoCall;
2633 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2634 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2635 if (instruction->HasUses()) {
2636 locations->SetOut(Location::SameAsFirstInput());
2637 }
2638}
2639
2640void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2641 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2642 codegen_->AddSlowPath(slow_path);
2643 Location value = instruction->GetLocations()->InAt(0);
2644 Primitive::Type type = instruction->GetType();
2645
2646 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002647 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002648 case Primitive::kPrimByte:
2649 case Primitive::kPrimChar:
2650 case Primitive::kPrimShort:
2651 case Primitive::kPrimInt: {
2652 if (value.IsConstant()) {
2653 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2654 __ B(slow_path->GetEntryLabel());
2655 } else {
2656 // A division by a non-null constant is valid. We don't need to perform
2657 // any check, so simply fall through.
2658 }
2659 } else {
2660 DCHECK(value.IsRegister()) << value;
2661 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2662 }
2663 break;
2664 }
2665 case Primitive::kPrimLong: {
2666 if (value.IsConstant()) {
2667 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2668 __ B(slow_path->GetEntryLabel());
2669 } else {
2670 // A division by a non-null constant is valid. We don't need to perform
2671 // any check, so simply fall through.
2672 }
2673 } else {
2674 DCHECK(value.IsRegisterPair()) << value;
2675 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2676 __ Beqz(TMP, slow_path->GetEntryLabel());
2677 }
2678 break;
2679 }
2680 default:
2681 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2682 }
2683}
2684
2685void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2686 LocationSummary* locations =
2687 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2688 locations->SetOut(Location::ConstantLocation(constant));
2689}
2690
2691void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2692 // Will be generated at use site.
2693}
2694
2695void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2696 exit->SetLocations(nullptr);
2697}
2698
2699void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2700}
2701
2702void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2703 LocationSummary* locations =
2704 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2705 locations->SetOut(Location::ConstantLocation(constant));
2706}
2707
2708void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2709 // Will be generated at use site.
2710}
2711
2712void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2713 got->SetLocations(nullptr);
2714}
2715
2716void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2717 DCHECK(!successor->IsExitBlock());
2718 HBasicBlock* block = got->GetBlock();
2719 HInstruction* previous = got->GetPrevious();
2720 HLoopInformation* info = block->GetLoopInformation();
2721
2722 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2723 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2724 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2725 return;
2726 }
2727 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2728 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2729 }
2730 if (!codegen_->GoesToNextBlock(block, successor)) {
2731 __ B(codegen_->GetLabelOf(successor));
2732 }
2733}
2734
2735void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2736 HandleGoto(got, got->GetSuccessor());
2737}
2738
2739void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2740 try_boundary->SetLocations(nullptr);
2741}
2742
2743void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2744 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2745 if (!successor->IsExitBlock()) {
2746 HandleGoto(try_boundary, successor);
2747 }
2748}
2749
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002750void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2751 LocationSummary* locations) {
2752 Register dst = locations->Out().AsRegister<Register>();
2753 Register lhs = locations->InAt(0).AsRegister<Register>();
2754 Location rhs_location = locations->InAt(1);
2755 Register rhs_reg = ZERO;
2756 int64_t rhs_imm = 0;
2757 bool use_imm = rhs_location.IsConstant();
2758 if (use_imm) {
2759 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2760 } else {
2761 rhs_reg = rhs_location.AsRegister<Register>();
2762 }
2763
2764 switch (cond) {
2765 case kCondEQ:
2766 case kCondNE:
2767 if (use_imm && IsUint<16>(rhs_imm)) {
2768 __ Xori(dst, lhs, rhs_imm);
2769 } else {
2770 if (use_imm) {
2771 rhs_reg = TMP;
2772 __ LoadConst32(rhs_reg, rhs_imm);
2773 }
2774 __ Xor(dst, lhs, rhs_reg);
2775 }
2776 if (cond == kCondEQ) {
2777 __ Sltiu(dst, dst, 1);
2778 } else {
2779 __ Sltu(dst, ZERO, dst);
2780 }
2781 break;
2782
2783 case kCondLT:
2784 case kCondGE:
2785 if (use_imm && IsInt<16>(rhs_imm)) {
2786 __ Slti(dst, lhs, rhs_imm);
2787 } else {
2788 if (use_imm) {
2789 rhs_reg = TMP;
2790 __ LoadConst32(rhs_reg, rhs_imm);
2791 }
2792 __ Slt(dst, lhs, rhs_reg);
2793 }
2794 if (cond == kCondGE) {
2795 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2796 // only the slt instruction but no sge.
2797 __ Xori(dst, dst, 1);
2798 }
2799 break;
2800
2801 case kCondLE:
2802 case kCondGT:
2803 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2804 // Simulate lhs <= rhs via lhs < rhs + 1.
2805 __ Slti(dst, lhs, rhs_imm + 1);
2806 if (cond == kCondGT) {
2807 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2808 // only the slti instruction but no sgti.
2809 __ Xori(dst, dst, 1);
2810 }
2811 } else {
2812 if (use_imm) {
2813 rhs_reg = TMP;
2814 __ LoadConst32(rhs_reg, rhs_imm);
2815 }
2816 __ Slt(dst, rhs_reg, lhs);
2817 if (cond == kCondLE) {
2818 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2819 // only the slt instruction but no sle.
2820 __ Xori(dst, dst, 1);
2821 }
2822 }
2823 break;
2824
2825 case kCondB:
2826 case kCondAE:
2827 if (use_imm && IsInt<16>(rhs_imm)) {
2828 // Sltiu sign-extends its 16-bit immediate operand before
2829 // the comparison and thus lets us compare directly with
2830 // unsigned values in the ranges [0, 0x7fff] and
2831 // [0xffff8000, 0xffffffff].
2832 __ Sltiu(dst, lhs, rhs_imm);
2833 } else {
2834 if (use_imm) {
2835 rhs_reg = TMP;
2836 __ LoadConst32(rhs_reg, rhs_imm);
2837 }
2838 __ Sltu(dst, lhs, rhs_reg);
2839 }
2840 if (cond == kCondAE) {
2841 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2842 // only the sltu instruction but no sgeu.
2843 __ Xori(dst, dst, 1);
2844 }
2845 break;
2846
2847 case kCondBE:
2848 case kCondA:
2849 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2850 // Simulate lhs <= rhs via lhs < rhs + 1.
2851 // Note that this only works if rhs + 1 does not overflow
2852 // to 0, hence the check above.
2853 // Sltiu sign-extends its 16-bit immediate operand before
2854 // the comparison and thus lets us compare directly with
2855 // unsigned values in the ranges [0, 0x7fff] and
2856 // [0xffff8000, 0xffffffff].
2857 __ Sltiu(dst, lhs, rhs_imm + 1);
2858 if (cond == kCondA) {
2859 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2860 // only the sltiu instruction but no sgtiu.
2861 __ Xori(dst, dst, 1);
2862 }
2863 } else {
2864 if (use_imm) {
2865 rhs_reg = TMP;
2866 __ LoadConst32(rhs_reg, rhs_imm);
2867 }
2868 __ Sltu(dst, rhs_reg, lhs);
2869 if (cond == kCondBE) {
2870 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2871 // only the sltu instruction but no sleu.
2872 __ Xori(dst, dst, 1);
2873 }
2874 }
2875 break;
2876 }
2877}
2878
2879void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2880 LocationSummary* locations,
2881 MipsLabel* label) {
2882 Register lhs = locations->InAt(0).AsRegister<Register>();
2883 Location rhs_location = locations->InAt(1);
2884 Register rhs_reg = ZERO;
2885 int32_t rhs_imm = 0;
2886 bool use_imm = rhs_location.IsConstant();
2887 if (use_imm) {
2888 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2889 } else {
2890 rhs_reg = rhs_location.AsRegister<Register>();
2891 }
2892
2893 if (use_imm && rhs_imm == 0) {
2894 switch (cond) {
2895 case kCondEQ:
2896 case kCondBE: // <= 0 if zero
2897 __ Beqz(lhs, label);
2898 break;
2899 case kCondNE:
2900 case kCondA: // > 0 if non-zero
2901 __ Bnez(lhs, label);
2902 break;
2903 case kCondLT:
2904 __ Bltz(lhs, label);
2905 break;
2906 case kCondGE:
2907 __ Bgez(lhs, label);
2908 break;
2909 case kCondLE:
2910 __ Blez(lhs, label);
2911 break;
2912 case kCondGT:
2913 __ Bgtz(lhs, label);
2914 break;
2915 case kCondB: // always false
2916 break;
2917 case kCondAE: // always true
2918 __ B(label);
2919 break;
2920 }
2921 } else {
2922 if (use_imm) {
2923 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2924 rhs_reg = TMP;
2925 __ LoadConst32(rhs_reg, rhs_imm);
2926 }
2927 switch (cond) {
2928 case kCondEQ:
2929 __ Beq(lhs, rhs_reg, label);
2930 break;
2931 case kCondNE:
2932 __ Bne(lhs, rhs_reg, label);
2933 break;
2934 case kCondLT:
2935 __ Blt(lhs, rhs_reg, label);
2936 break;
2937 case kCondGE:
2938 __ Bge(lhs, rhs_reg, label);
2939 break;
2940 case kCondLE:
2941 __ Bge(rhs_reg, lhs, label);
2942 break;
2943 case kCondGT:
2944 __ Blt(rhs_reg, lhs, label);
2945 break;
2946 case kCondB:
2947 __ Bltu(lhs, rhs_reg, label);
2948 break;
2949 case kCondAE:
2950 __ Bgeu(lhs, rhs_reg, label);
2951 break;
2952 case kCondBE:
2953 __ Bgeu(rhs_reg, lhs, label);
2954 break;
2955 case kCondA:
2956 __ Bltu(rhs_reg, lhs, label);
2957 break;
2958 }
2959 }
2960}
2961
2962void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2963 LocationSummary* locations,
2964 MipsLabel* label) {
2965 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2966 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2967 Location rhs_location = locations->InAt(1);
2968 Register rhs_high = ZERO;
2969 Register rhs_low = ZERO;
2970 int64_t imm = 0;
2971 uint32_t imm_high = 0;
2972 uint32_t imm_low = 0;
2973 bool use_imm = rhs_location.IsConstant();
2974 if (use_imm) {
2975 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2976 imm_high = High32Bits(imm);
2977 imm_low = Low32Bits(imm);
2978 } else {
2979 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2980 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2981 }
2982
2983 if (use_imm && imm == 0) {
2984 switch (cond) {
2985 case kCondEQ:
2986 case kCondBE: // <= 0 if zero
2987 __ Or(TMP, lhs_high, lhs_low);
2988 __ Beqz(TMP, label);
2989 break;
2990 case kCondNE:
2991 case kCondA: // > 0 if non-zero
2992 __ Or(TMP, lhs_high, lhs_low);
2993 __ Bnez(TMP, label);
2994 break;
2995 case kCondLT:
2996 __ Bltz(lhs_high, label);
2997 break;
2998 case kCondGE:
2999 __ Bgez(lhs_high, label);
3000 break;
3001 case kCondLE:
3002 __ Or(TMP, lhs_high, lhs_low);
3003 __ Sra(AT, lhs_high, 31);
3004 __ Bgeu(AT, TMP, label);
3005 break;
3006 case kCondGT:
3007 __ Or(TMP, lhs_high, lhs_low);
3008 __ Sra(AT, lhs_high, 31);
3009 __ Bltu(AT, TMP, label);
3010 break;
3011 case kCondB: // always false
3012 break;
3013 case kCondAE: // always true
3014 __ B(label);
3015 break;
3016 }
3017 } else if (use_imm) {
3018 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3019 switch (cond) {
3020 case kCondEQ:
3021 __ LoadConst32(TMP, imm_high);
3022 __ Xor(TMP, TMP, lhs_high);
3023 __ LoadConst32(AT, imm_low);
3024 __ Xor(AT, AT, lhs_low);
3025 __ Or(TMP, TMP, AT);
3026 __ Beqz(TMP, label);
3027 break;
3028 case kCondNE:
3029 __ LoadConst32(TMP, imm_high);
3030 __ Xor(TMP, TMP, lhs_high);
3031 __ LoadConst32(AT, imm_low);
3032 __ Xor(AT, AT, lhs_low);
3033 __ Or(TMP, TMP, AT);
3034 __ Bnez(TMP, label);
3035 break;
3036 case kCondLT:
3037 __ LoadConst32(TMP, imm_high);
3038 __ Blt(lhs_high, TMP, label);
3039 __ Slt(TMP, TMP, lhs_high);
3040 __ LoadConst32(AT, imm_low);
3041 __ Sltu(AT, lhs_low, AT);
3042 __ Blt(TMP, AT, label);
3043 break;
3044 case kCondGE:
3045 __ LoadConst32(TMP, imm_high);
3046 __ Blt(TMP, lhs_high, label);
3047 __ Slt(TMP, lhs_high, TMP);
3048 __ LoadConst32(AT, imm_low);
3049 __ Sltu(AT, lhs_low, AT);
3050 __ Or(TMP, TMP, AT);
3051 __ Beqz(TMP, label);
3052 break;
3053 case kCondLE:
3054 __ LoadConst32(TMP, imm_high);
3055 __ Blt(lhs_high, TMP, label);
3056 __ Slt(TMP, TMP, lhs_high);
3057 __ LoadConst32(AT, imm_low);
3058 __ Sltu(AT, AT, lhs_low);
3059 __ Or(TMP, TMP, AT);
3060 __ Beqz(TMP, label);
3061 break;
3062 case kCondGT:
3063 __ LoadConst32(TMP, imm_high);
3064 __ Blt(TMP, lhs_high, label);
3065 __ Slt(TMP, lhs_high, TMP);
3066 __ LoadConst32(AT, imm_low);
3067 __ Sltu(AT, AT, lhs_low);
3068 __ Blt(TMP, AT, label);
3069 break;
3070 case kCondB:
3071 __ LoadConst32(TMP, imm_high);
3072 __ Bltu(lhs_high, TMP, label);
3073 __ Sltu(TMP, TMP, lhs_high);
3074 __ LoadConst32(AT, imm_low);
3075 __ Sltu(AT, lhs_low, AT);
3076 __ Blt(TMP, AT, label);
3077 break;
3078 case kCondAE:
3079 __ LoadConst32(TMP, imm_high);
3080 __ Bltu(TMP, lhs_high, label);
3081 __ Sltu(TMP, lhs_high, TMP);
3082 __ LoadConst32(AT, imm_low);
3083 __ Sltu(AT, lhs_low, AT);
3084 __ Or(TMP, TMP, AT);
3085 __ Beqz(TMP, label);
3086 break;
3087 case kCondBE:
3088 __ LoadConst32(TMP, imm_high);
3089 __ Bltu(lhs_high, TMP, label);
3090 __ Sltu(TMP, TMP, lhs_high);
3091 __ LoadConst32(AT, imm_low);
3092 __ Sltu(AT, AT, lhs_low);
3093 __ Or(TMP, TMP, AT);
3094 __ Beqz(TMP, label);
3095 break;
3096 case kCondA:
3097 __ LoadConst32(TMP, imm_high);
3098 __ Bltu(TMP, lhs_high, label);
3099 __ Sltu(TMP, lhs_high, TMP);
3100 __ LoadConst32(AT, imm_low);
3101 __ Sltu(AT, AT, lhs_low);
3102 __ Blt(TMP, AT, label);
3103 break;
3104 }
3105 } else {
3106 switch (cond) {
3107 case kCondEQ:
3108 __ Xor(TMP, lhs_high, rhs_high);
3109 __ Xor(AT, lhs_low, rhs_low);
3110 __ Or(TMP, TMP, AT);
3111 __ Beqz(TMP, label);
3112 break;
3113 case kCondNE:
3114 __ Xor(TMP, lhs_high, rhs_high);
3115 __ Xor(AT, lhs_low, rhs_low);
3116 __ Or(TMP, TMP, AT);
3117 __ Bnez(TMP, label);
3118 break;
3119 case kCondLT:
3120 __ Blt(lhs_high, rhs_high, label);
3121 __ Slt(TMP, rhs_high, lhs_high);
3122 __ Sltu(AT, lhs_low, rhs_low);
3123 __ Blt(TMP, AT, label);
3124 break;
3125 case kCondGE:
3126 __ Blt(rhs_high, lhs_high, label);
3127 __ Slt(TMP, lhs_high, rhs_high);
3128 __ Sltu(AT, lhs_low, rhs_low);
3129 __ Or(TMP, TMP, AT);
3130 __ Beqz(TMP, label);
3131 break;
3132 case kCondLE:
3133 __ Blt(lhs_high, rhs_high, label);
3134 __ Slt(TMP, rhs_high, lhs_high);
3135 __ Sltu(AT, rhs_low, lhs_low);
3136 __ Or(TMP, TMP, AT);
3137 __ Beqz(TMP, label);
3138 break;
3139 case kCondGT:
3140 __ Blt(rhs_high, lhs_high, label);
3141 __ Slt(TMP, lhs_high, rhs_high);
3142 __ Sltu(AT, rhs_low, lhs_low);
3143 __ Blt(TMP, AT, label);
3144 break;
3145 case kCondB:
3146 __ Bltu(lhs_high, rhs_high, label);
3147 __ Sltu(TMP, rhs_high, lhs_high);
3148 __ Sltu(AT, lhs_low, rhs_low);
3149 __ Blt(TMP, AT, label);
3150 break;
3151 case kCondAE:
3152 __ Bltu(rhs_high, lhs_high, label);
3153 __ Sltu(TMP, lhs_high, rhs_high);
3154 __ Sltu(AT, lhs_low, rhs_low);
3155 __ Or(TMP, TMP, AT);
3156 __ Beqz(TMP, label);
3157 break;
3158 case kCondBE:
3159 __ Bltu(lhs_high, rhs_high, label);
3160 __ Sltu(TMP, rhs_high, lhs_high);
3161 __ Sltu(AT, rhs_low, lhs_low);
3162 __ Or(TMP, TMP, AT);
3163 __ Beqz(TMP, label);
3164 break;
3165 case kCondA:
3166 __ Bltu(rhs_high, lhs_high, label);
3167 __ Sltu(TMP, lhs_high, rhs_high);
3168 __ Sltu(AT, rhs_low, lhs_low);
3169 __ Blt(TMP, AT, label);
3170 break;
3171 }
3172 }
3173}
3174
3175void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3176 bool gt_bias,
3177 Primitive::Type type,
3178 LocationSummary* locations,
3179 MipsLabel* label) {
3180 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3181 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3182 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3183 if (type == Primitive::kPrimFloat) {
3184 if (isR6) {
3185 switch (cond) {
3186 case kCondEQ:
3187 __ CmpEqS(FTMP, lhs, rhs);
3188 __ Bc1nez(FTMP, label);
3189 break;
3190 case kCondNE:
3191 __ CmpEqS(FTMP, lhs, rhs);
3192 __ Bc1eqz(FTMP, label);
3193 break;
3194 case kCondLT:
3195 if (gt_bias) {
3196 __ CmpLtS(FTMP, lhs, rhs);
3197 } else {
3198 __ CmpUltS(FTMP, lhs, rhs);
3199 }
3200 __ Bc1nez(FTMP, label);
3201 break;
3202 case kCondLE:
3203 if (gt_bias) {
3204 __ CmpLeS(FTMP, lhs, rhs);
3205 } else {
3206 __ CmpUleS(FTMP, lhs, rhs);
3207 }
3208 __ Bc1nez(FTMP, label);
3209 break;
3210 case kCondGT:
3211 if (gt_bias) {
3212 __ CmpUltS(FTMP, rhs, lhs);
3213 } else {
3214 __ CmpLtS(FTMP, rhs, lhs);
3215 }
3216 __ Bc1nez(FTMP, label);
3217 break;
3218 case kCondGE:
3219 if (gt_bias) {
3220 __ CmpUleS(FTMP, rhs, lhs);
3221 } else {
3222 __ CmpLeS(FTMP, rhs, lhs);
3223 }
3224 __ Bc1nez(FTMP, label);
3225 break;
3226 default:
3227 LOG(FATAL) << "Unexpected non-floating-point condition";
3228 }
3229 } else {
3230 switch (cond) {
3231 case kCondEQ:
3232 __ CeqS(0, lhs, rhs);
3233 __ Bc1t(0, label);
3234 break;
3235 case kCondNE:
3236 __ CeqS(0, lhs, rhs);
3237 __ Bc1f(0, label);
3238 break;
3239 case kCondLT:
3240 if (gt_bias) {
3241 __ ColtS(0, lhs, rhs);
3242 } else {
3243 __ CultS(0, lhs, rhs);
3244 }
3245 __ Bc1t(0, label);
3246 break;
3247 case kCondLE:
3248 if (gt_bias) {
3249 __ ColeS(0, lhs, rhs);
3250 } else {
3251 __ CuleS(0, lhs, rhs);
3252 }
3253 __ Bc1t(0, label);
3254 break;
3255 case kCondGT:
3256 if (gt_bias) {
3257 __ CultS(0, rhs, lhs);
3258 } else {
3259 __ ColtS(0, rhs, lhs);
3260 }
3261 __ Bc1t(0, label);
3262 break;
3263 case kCondGE:
3264 if (gt_bias) {
3265 __ CuleS(0, rhs, lhs);
3266 } else {
3267 __ ColeS(0, rhs, lhs);
3268 }
3269 __ Bc1t(0, label);
3270 break;
3271 default:
3272 LOG(FATAL) << "Unexpected non-floating-point condition";
3273 }
3274 }
3275 } else {
3276 DCHECK_EQ(type, Primitive::kPrimDouble);
3277 if (isR6) {
3278 switch (cond) {
3279 case kCondEQ:
3280 __ CmpEqD(FTMP, lhs, rhs);
3281 __ Bc1nez(FTMP, label);
3282 break;
3283 case kCondNE:
3284 __ CmpEqD(FTMP, lhs, rhs);
3285 __ Bc1eqz(FTMP, label);
3286 break;
3287 case kCondLT:
3288 if (gt_bias) {
3289 __ CmpLtD(FTMP, lhs, rhs);
3290 } else {
3291 __ CmpUltD(FTMP, lhs, rhs);
3292 }
3293 __ Bc1nez(FTMP, label);
3294 break;
3295 case kCondLE:
3296 if (gt_bias) {
3297 __ CmpLeD(FTMP, lhs, rhs);
3298 } else {
3299 __ CmpUleD(FTMP, lhs, rhs);
3300 }
3301 __ Bc1nez(FTMP, label);
3302 break;
3303 case kCondGT:
3304 if (gt_bias) {
3305 __ CmpUltD(FTMP, rhs, lhs);
3306 } else {
3307 __ CmpLtD(FTMP, rhs, lhs);
3308 }
3309 __ Bc1nez(FTMP, label);
3310 break;
3311 case kCondGE:
3312 if (gt_bias) {
3313 __ CmpUleD(FTMP, rhs, lhs);
3314 } else {
3315 __ CmpLeD(FTMP, rhs, lhs);
3316 }
3317 __ Bc1nez(FTMP, label);
3318 break;
3319 default:
3320 LOG(FATAL) << "Unexpected non-floating-point condition";
3321 }
3322 } else {
3323 switch (cond) {
3324 case kCondEQ:
3325 __ CeqD(0, lhs, rhs);
3326 __ Bc1t(0, label);
3327 break;
3328 case kCondNE:
3329 __ CeqD(0, lhs, rhs);
3330 __ Bc1f(0, label);
3331 break;
3332 case kCondLT:
3333 if (gt_bias) {
3334 __ ColtD(0, lhs, rhs);
3335 } else {
3336 __ CultD(0, lhs, rhs);
3337 }
3338 __ Bc1t(0, label);
3339 break;
3340 case kCondLE:
3341 if (gt_bias) {
3342 __ ColeD(0, lhs, rhs);
3343 } else {
3344 __ CuleD(0, lhs, rhs);
3345 }
3346 __ Bc1t(0, label);
3347 break;
3348 case kCondGT:
3349 if (gt_bias) {
3350 __ CultD(0, rhs, lhs);
3351 } else {
3352 __ ColtD(0, rhs, lhs);
3353 }
3354 __ Bc1t(0, label);
3355 break;
3356 case kCondGE:
3357 if (gt_bias) {
3358 __ CuleD(0, rhs, lhs);
3359 } else {
3360 __ ColeD(0, rhs, lhs);
3361 }
3362 __ Bc1t(0, label);
3363 break;
3364 default:
3365 LOG(FATAL) << "Unexpected non-floating-point condition";
3366 }
3367 }
3368 }
3369}
3370
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003371void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003372 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003373 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003374 MipsLabel* false_target) {
3375 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003376
David Brazdil0debae72015-11-12 18:37:00 +00003377 if (true_target == nullptr && false_target == nullptr) {
3378 // Nothing to do. The code always falls through.
3379 return;
3380 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003381 // Constant condition, statically compared against "true" (integer value 1).
3382 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003383 if (true_target != nullptr) {
3384 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003385 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003386 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003387 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003388 if (false_target != nullptr) {
3389 __ B(false_target);
3390 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003391 }
David Brazdil0debae72015-11-12 18:37:00 +00003392 return;
3393 }
3394
3395 // The following code generates these patterns:
3396 // (1) true_target == nullptr && false_target != nullptr
3397 // - opposite condition true => branch to false_target
3398 // (2) true_target != nullptr && false_target == nullptr
3399 // - condition true => branch to true_target
3400 // (3) true_target != nullptr && false_target != nullptr
3401 // - condition true => branch to true_target
3402 // - branch to false_target
3403 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003404 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003405 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003406 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003407 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003408 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3409 } else {
3410 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3411 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003412 } else {
3413 // The condition instruction has not been materialized, use its inputs as
3414 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003415 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003416 Primitive::Type type = condition->InputAt(0)->GetType();
3417 LocationSummary* locations = cond->GetLocations();
3418 IfCondition if_cond = condition->GetCondition();
3419 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003420
David Brazdil0debae72015-11-12 18:37:00 +00003421 if (true_target == nullptr) {
3422 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003423 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003424 }
3425
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003426 switch (type) {
3427 default:
3428 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3429 break;
3430 case Primitive::kPrimLong:
3431 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3432 break;
3433 case Primitive::kPrimFloat:
3434 case Primitive::kPrimDouble:
3435 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3436 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003437 }
3438 }
David Brazdil0debae72015-11-12 18:37:00 +00003439
3440 // If neither branch falls through (case 3), the conditional branch to `true_target`
3441 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3442 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003443 __ B(false_target);
3444 }
3445}
3446
3447void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3448 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003449 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003450 locations->SetInAt(0, Location::RequiresRegister());
3451 }
3452}
3453
3454void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003455 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3456 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3457 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3458 nullptr : codegen_->GetLabelOf(true_successor);
3459 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3460 nullptr : codegen_->GetLabelOf(false_successor);
3461 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003462}
3463
3464void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3465 LocationSummary* locations = new (GetGraph()->GetArena())
3466 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003467 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003468 locations->SetInAt(0, Location::RequiresRegister());
3469 }
3470}
3471
3472void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003473 SlowPathCodeMIPS* slow_path =
3474 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003475 GenerateTestAndBranch(deoptimize,
3476 /* condition_input_index */ 0,
3477 slow_path->GetEntryLabel(),
3478 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003479}
3480
David Brazdil74eb1b22015-12-14 11:44:01 +00003481void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3483 if (Primitive::IsFloatingPointType(select->GetType())) {
3484 locations->SetInAt(0, Location::RequiresFpuRegister());
3485 locations->SetInAt(1, Location::RequiresFpuRegister());
3486 } else {
3487 locations->SetInAt(0, Location::RequiresRegister());
3488 locations->SetInAt(1, Location::RequiresRegister());
3489 }
3490 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3491 locations->SetInAt(2, Location::RequiresRegister());
3492 }
3493 locations->SetOut(Location::SameAsFirstInput());
3494}
3495
3496void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3497 LocationSummary* locations = select->GetLocations();
3498 MipsLabel false_target;
3499 GenerateTestAndBranch(select,
3500 /* condition_input_index */ 2,
3501 /* true_target */ nullptr,
3502 &false_target);
3503 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3504 __ Bind(&false_target);
3505}
3506
David Srbecky0cf44932015-12-09 14:09:59 +00003507void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3508 new (GetGraph()->GetArena()) LocationSummary(info);
3509}
3510
David Srbeckyd28f4a02016-03-14 17:14:24 +00003511void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3512 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003513}
3514
3515void CodeGeneratorMIPS::GenerateNop() {
3516 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003517}
3518
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003519void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3520 Primitive::Type field_type = field_info.GetFieldType();
3521 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3522 bool generate_volatile = field_info.IsVolatile() && is_wide;
3523 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003524 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003525
3526 locations->SetInAt(0, Location::RequiresRegister());
3527 if (generate_volatile) {
3528 InvokeRuntimeCallingConvention calling_convention;
3529 // need A0 to hold base + offset
3530 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3531 if (field_type == Primitive::kPrimLong) {
3532 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3533 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003534 // Use Location::Any() to prevent situations when running out of available fp registers.
3535 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003536 // Need some temp core regs since FP results are returned in core registers
3537 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3538 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3539 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3540 }
3541 } else {
3542 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3543 locations->SetOut(Location::RequiresFpuRegister());
3544 } else {
3545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3546 }
3547 }
3548}
3549
3550void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3551 const FieldInfo& field_info,
3552 uint32_t dex_pc) {
3553 Primitive::Type type = field_info.GetFieldType();
3554 LocationSummary* locations = instruction->GetLocations();
3555 Register obj = locations->InAt(0).AsRegister<Register>();
3556 LoadOperandType load_type = kLoadUnsignedByte;
3557 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003558 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003559 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003560
3561 switch (type) {
3562 case Primitive::kPrimBoolean:
3563 load_type = kLoadUnsignedByte;
3564 break;
3565 case Primitive::kPrimByte:
3566 load_type = kLoadSignedByte;
3567 break;
3568 case Primitive::kPrimShort:
3569 load_type = kLoadSignedHalfword;
3570 break;
3571 case Primitive::kPrimChar:
3572 load_type = kLoadUnsignedHalfword;
3573 break;
3574 case Primitive::kPrimInt:
3575 case Primitive::kPrimFloat:
3576 case Primitive::kPrimNot:
3577 load_type = kLoadWord;
3578 break;
3579 case Primitive::kPrimLong:
3580 case Primitive::kPrimDouble:
3581 load_type = kLoadDoubleword;
3582 break;
3583 case Primitive::kPrimVoid:
3584 LOG(FATAL) << "Unreachable type " << type;
3585 UNREACHABLE();
3586 }
3587
3588 if (is_volatile && load_type == kLoadDoubleword) {
3589 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003590 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003591 // Do implicit Null check
3592 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3593 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01003594 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3596 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003597 // FP results are returned in core registers. Need to move them.
3598 Location out = locations->Out();
3599 if (out.IsFpuRegister()) {
3600 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3601 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3602 out.AsFpuRegister<FRegister>());
3603 } else {
3604 DCHECK(out.IsDoubleStackSlot());
3605 __ StoreToOffset(kStoreWord,
3606 locations->GetTemp(1).AsRegister<Register>(),
3607 SP,
3608 out.GetStackIndex());
3609 __ StoreToOffset(kStoreWord,
3610 locations->GetTemp(2).AsRegister<Register>(),
3611 SP,
3612 out.GetStackIndex() + 4);
3613 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003614 }
3615 } else {
3616 if (!Primitive::IsFloatingPointType(type)) {
3617 Register dst;
3618 if (type == Primitive::kPrimLong) {
3619 DCHECK(locations->Out().IsRegisterPair());
3620 dst = locations->Out().AsRegisterPairLow<Register>();
3621 } else {
3622 DCHECK(locations->Out().IsRegister());
3623 dst = locations->Out().AsRegister<Register>();
3624 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003625 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003626 } else {
3627 DCHECK(locations->Out().IsFpuRegister());
3628 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3629 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003630 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003631 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003632 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003633 }
3634 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003635 }
3636
3637 if (is_volatile) {
3638 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3639 }
3640}
3641
3642void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3643 Primitive::Type field_type = field_info.GetFieldType();
3644 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3645 bool generate_volatile = field_info.IsVolatile() && is_wide;
3646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003647 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003648
3649 locations->SetInAt(0, Location::RequiresRegister());
3650 if (generate_volatile) {
3651 InvokeRuntimeCallingConvention calling_convention;
3652 // need A0 to hold base + offset
3653 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3654 if (field_type == Primitive::kPrimLong) {
3655 locations->SetInAt(1, Location::RegisterPairLocation(
3656 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3657 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003658 // Use Location::Any() to prevent situations when running out of available fp registers.
3659 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003660 // Pass FP parameters in core registers.
3661 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3662 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3663 }
3664 } else {
3665 if (Primitive::IsFloatingPointType(field_type)) {
3666 locations->SetInAt(1, Location::RequiresFpuRegister());
3667 } else {
3668 locations->SetInAt(1, Location::RequiresRegister());
3669 }
3670 }
3671}
3672
3673void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3674 const FieldInfo& field_info,
3675 uint32_t dex_pc) {
3676 Primitive::Type type = field_info.GetFieldType();
3677 LocationSummary* locations = instruction->GetLocations();
3678 Register obj = locations->InAt(0).AsRegister<Register>();
3679 StoreOperandType store_type = kStoreByte;
3680 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003681 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003682 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003683
3684 switch (type) {
3685 case Primitive::kPrimBoolean:
3686 case Primitive::kPrimByte:
3687 store_type = kStoreByte;
3688 break;
3689 case Primitive::kPrimShort:
3690 case Primitive::kPrimChar:
3691 store_type = kStoreHalfword;
3692 break;
3693 case Primitive::kPrimInt:
3694 case Primitive::kPrimFloat:
3695 case Primitive::kPrimNot:
3696 store_type = kStoreWord;
3697 break;
3698 case Primitive::kPrimLong:
3699 case Primitive::kPrimDouble:
3700 store_type = kStoreDoubleword;
3701 break;
3702 case Primitive::kPrimVoid:
3703 LOG(FATAL) << "Unreachable type " << type;
3704 UNREACHABLE();
3705 }
3706
3707 if (is_volatile) {
3708 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3709 }
3710
3711 if (is_volatile && store_type == kStoreDoubleword) {
3712 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003713 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003714 // Do implicit Null check.
3715 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3716 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3717 if (type == Primitive::kPrimDouble) {
3718 // Pass FP parameters in core registers.
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003719 Location in = locations->InAt(1);
3720 if (in.IsFpuRegister()) {
3721 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(), in.AsFpuRegister<FRegister>());
3722 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3723 in.AsFpuRegister<FRegister>());
3724 } else if (in.IsDoubleStackSlot()) {
3725 __ LoadFromOffset(kLoadWord,
3726 locations->GetTemp(1).AsRegister<Register>(),
3727 SP,
3728 in.GetStackIndex());
3729 __ LoadFromOffset(kLoadWord,
3730 locations->GetTemp(2).AsRegister<Register>(),
3731 SP,
3732 in.GetStackIndex() + 4);
3733 } else {
3734 DCHECK(in.IsConstant());
3735 DCHECK(in.GetConstant()->IsDoubleConstant());
3736 int64_t value = bit_cast<int64_t, double>(in.GetConstant()->AsDoubleConstant()->GetValue());
3737 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
3738 locations->GetTemp(1).AsRegister<Register>(),
3739 value);
3740 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003741 }
Serban Constantinescufca16662016-07-14 09:21:59 +01003742 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003743 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3744 } else {
3745 if (!Primitive::IsFloatingPointType(type)) {
3746 Register src;
3747 if (type == Primitive::kPrimLong) {
3748 DCHECK(locations->InAt(1).IsRegisterPair());
3749 src = locations->InAt(1).AsRegisterPairLow<Register>();
3750 } else {
3751 DCHECK(locations->InAt(1).IsRegister());
3752 src = locations->InAt(1).AsRegister<Register>();
3753 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003754 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003755 } else {
3756 DCHECK(locations->InAt(1).IsFpuRegister());
3757 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3758 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003759 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003760 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003761 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003762 }
3763 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003764 }
3765
3766 // TODO: memory barriers?
3767 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3768 DCHECK(locations->InAt(1).IsRegister());
3769 Register src = locations->InAt(1).AsRegister<Register>();
3770 codegen_->MarkGCCard(obj, src);
3771 }
3772
3773 if (is_volatile) {
3774 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3775 }
3776}
3777
3778void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3779 HandleFieldGet(instruction, instruction->GetFieldInfo());
3780}
3781
3782void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3783 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3784}
3785
3786void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3787 HandleFieldSet(instruction, instruction->GetFieldInfo());
3788}
3789
3790void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3791 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3792}
3793
Alexey Frunze06a46c42016-07-19 15:00:40 -07003794void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
3795 HInstruction* instruction ATTRIBUTE_UNUSED,
3796 Location root,
3797 Register obj,
3798 uint32_t offset) {
3799 Register root_reg = root.AsRegister<Register>();
3800 if (kEmitCompilerReadBarrier) {
3801 UNIMPLEMENTED(FATAL) << "for read barrier";
3802 } else {
3803 // Plain GC root load with no read barrier.
3804 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3805 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
3806 // Note that GC roots are not affected by heap poisoning, thus we
3807 // do not have to unpoison `root_reg` here.
3808 }
3809}
3810
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003811void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3812 LocationSummary::CallKind call_kind =
3813 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3815 locations->SetInAt(0, Location::RequiresRegister());
3816 locations->SetInAt(1, Location::RequiresRegister());
3817 // The output does overlap inputs.
3818 // Note that TypeCheckSlowPathMIPS uses this register too.
3819 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3820}
3821
3822void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3823 LocationSummary* locations = instruction->GetLocations();
3824 Register obj = locations->InAt(0).AsRegister<Register>();
3825 Register cls = locations->InAt(1).AsRegister<Register>();
3826 Register out = locations->Out().AsRegister<Register>();
3827
3828 MipsLabel done;
3829
3830 // Return 0 if `obj` is null.
3831 // TODO: Avoid this check if we know `obj` is not null.
3832 __ Move(out, ZERO);
3833 __ Beqz(obj, &done);
3834
3835 // Compare the class of `obj` with `cls`.
3836 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3837 if (instruction->IsExactCheck()) {
3838 // Classes must be equal for the instanceof to succeed.
3839 __ Xor(out, out, cls);
3840 __ Sltiu(out, out, 1);
3841 } else {
3842 // If the classes are not equal, we go into a slow path.
3843 DCHECK(locations->OnlyCallsOnSlowPath());
3844 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3845 codegen_->AddSlowPath(slow_path);
3846 __ Bne(out, cls, slow_path->GetEntryLabel());
3847 __ LoadConst32(out, 1);
3848 __ Bind(slow_path->GetExitLabel());
3849 }
3850
3851 __ Bind(&done);
3852}
3853
3854void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3855 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3856 locations->SetOut(Location::ConstantLocation(constant));
3857}
3858
3859void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3860 // Will be generated at use site.
3861}
3862
3863void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3864 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3865 locations->SetOut(Location::ConstantLocation(constant));
3866}
3867
3868void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3869 // Will be generated at use site.
3870}
3871
3872void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3873 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3874 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3875}
3876
3877void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3878 HandleInvoke(invoke);
3879 // The register T0 is required to be used for the hidden argument in
3880 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3881 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3882}
3883
3884void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3885 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3886 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003887 Location receiver = invoke->GetLocations()->InAt(0);
3888 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003889 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003890
3891 // Set the hidden argument.
3892 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3893 invoke->GetDexMethodIndex());
3894
3895 // temp = object->GetClass();
3896 if (receiver.IsStackSlot()) {
3897 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3898 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3899 } else {
3900 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3901 }
3902 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003903 __ LoadFromOffset(kLoadWord, temp, temp,
3904 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3905 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003906 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003907 // temp = temp->GetImtEntryAt(method_offset);
3908 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3909 // T9 = temp->GetEntryPoint();
3910 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3911 // T9();
3912 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003913 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003914 DCHECK(!codegen_->IsLeafMethod());
3915 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3916}
3917
3918void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003919 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3920 if (intrinsic.TryDispatch(invoke)) {
3921 return;
3922 }
3923
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003924 HandleInvoke(invoke);
3925}
3926
3927void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003928 // Explicit clinit checks triggered by static invokes must have been pruned by
3929 // art::PrepareForRegisterAllocation.
3930 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003931
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003932 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3933 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3934 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3935
3936 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
3937 // R6 has PC-relative addressing.
3938 bool has_extra_input = !isR6 &&
3939 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3940 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
3941
3942 if (invoke->HasPcRelativeDexCache()) {
3943 // kDexCachePcRelative is mutually exclusive with
3944 // kDirectAddressWithFixup/kCallDirectWithFixup.
3945 CHECK(!has_extra_input);
3946 has_extra_input = true;
3947 }
3948
Chris Larsen701566a2015-10-27 15:29:13 -07003949 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3950 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003951 if (invoke->GetLocations()->CanCall() && has_extra_input) {
3952 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3953 }
Chris Larsen701566a2015-10-27 15:29:13 -07003954 return;
3955 }
3956
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003957 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003958
3959 // Add the extra input register if either the dex cache array base register
3960 // or the PC-relative base register for accessing literals is needed.
3961 if (has_extra_input) {
3962 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3963 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003964}
3965
Chris Larsen701566a2015-10-27 15:29:13 -07003966static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003967 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003968 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3969 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003970 return true;
3971 }
3972 return false;
3973}
3974
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003975HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07003976 HLoadString::LoadKind desired_string_load_kind) {
3977 if (kEmitCompilerReadBarrier) {
3978 UNIMPLEMENTED(FATAL) << "for read barrier";
3979 }
3980 // We disable PC-relative load when there is an irreducible loop, as the optimization
3981 // is incompatible with it.
3982 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
3983 bool fallback_load = has_irreducible_loops;
3984 switch (desired_string_load_kind) {
3985 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3986 DCHECK(!GetCompilerOptions().GetCompilePic());
3987 break;
3988 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3989 DCHECK(GetCompilerOptions().GetCompilePic());
3990 break;
3991 case HLoadString::LoadKind::kBootImageAddress:
3992 break;
3993 case HLoadString::LoadKind::kDexCacheAddress:
3994 DCHECK(Runtime::Current()->UseJitCompilation());
3995 fallback_load = false;
3996 break;
3997 case HLoadString::LoadKind::kDexCachePcRelative:
3998 DCHECK(!Runtime::Current()->UseJitCompilation());
3999 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4000 // with irreducible loops.
4001 break;
4002 case HLoadString::LoadKind::kDexCacheViaMethod:
4003 fallback_load = false;
4004 break;
4005 }
4006 if (fallback_load) {
4007 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4008 }
4009 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004010}
4011
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004012HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4013 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004014 if (kEmitCompilerReadBarrier) {
4015 UNIMPLEMENTED(FATAL) << "for read barrier";
4016 }
4017 // We disable pc-relative load when there is an irreducible loop, as the optimization
4018 // is incompatible with it.
4019 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4020 bool fallback_load = has_irreducible_loops;
4021 switch (desired_class_load_kind) {
4022 case HLoadClass::LoadKind::kReferrersClass:
4023 fallback_load = false;
4024 break;
4025 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4026 DCHECK(!GetCompilerOptions().GetCompilePic());
4027 break;
4028 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4029 DCHECK(GetCompilerOptions().GetCompilePic());
4030 break;
4031 case HLoadClass::LoadKind::kBootImageAddress:
4032 break;
4033 case HLoadClass::LoadKind::kDexCacheAddress:
4034 DCHECK(Runtime::Current()->UseJitCompilation());
4035 fallback_load = false;
4036 break;
4037 case HLoadClass::LoadKind::kDexCachePcRelative:
4038 DCHECK(!Runtime::Current()->UseJitCompilation());
4039 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4040 // with irreducible loops.
4041 break;
4042 case HLoadClass::LoadKind::kDexCacheViaMethod:
4043 fallback_load = false;
4044 break;
4045 }
4046 if (fallback_load) {
4047 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4048 }
4049 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004050}
4051
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004052Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4053 Register temp) {
4054 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4055 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4056 if (!invoke->GetLocations()->Intrinsified()) {
4057 return location.AsRegister<Register>();
4058 }
4059 // For intrinsics we allow any location, so it may be on the stack.
4060 if (!location.IsRegister()) {
4061 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4062 return temp;
4063 }
4064 // For register locations, check if the register was saved. If so, get it from the stack.
4065 // Note: There is a chance that the register was saved but not overwritten, so we could
4066 // save one load. However, since this is just an intrinsic slow path we prefer this
4067 // simple and more robust approach rather that trying to determine if that's the case.
4068 SlowPathCode* slow_path = GetCurrentSlowPath();
4069 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4070 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4071 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4072 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4073 return temp;
4074 }
4075 return location.AsRegister<Register>();
4076}
4077
Vladimir Markodc151b22015-10-15 18:02:30 +01004078HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4079 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4080 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004081 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4082 // We disable PC-relative load when there is an irreducible loop, as the optimization
4083 // is incompatible with it.
4084 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4085 bool fallback_load = true;
4086 bool fallback_call = true;
4087 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004088 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4089 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004090 fallback_load = has_irreducible_loops;
4091 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004092 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004093 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004094 break;
4095 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004096 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004097 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004098 fallback_call = has_irreducible_loops;
4099 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004100 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004101 // TODO: Implement this type.
4102 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004103 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004104 fallback_call = false;
4105 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004106 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004107 if (fallback_load) {
4108 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4109 dispatch_info.method_load_data = 0;
4110 }
4111 if (fallback_call) {
4112 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4113 dispatch_info.direct_code_ptr = 0;
4114 }
4115 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004116}
4117
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004118void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4119 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004120 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004121 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4122 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4123 bool isR6 = isa_features_.IsR6();
4124 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4125 // R6 has PC-relative addressing.
4126 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4127 (!isR6 &&
4128 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4129 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4130 Register base_reg = has_extra_input
4131 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4132 : ZERO;
4133
4134 // For better instruction scheduling we load the direct code pointer before the method pointer.
4135 switch (code_ptr_location) {
4136 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4137 // T9 = invoke->GetDirectCodePtr();
4138 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4139 break;
4140 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4141 // T9 = code address from literal pool with link-time patch.
4142 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4143 break;
4144 default:
4145 break;
4146 }
4147
4148 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004149 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4150 // temp = thread->string_init_entrypoint
4151 __ LoadFromOffset(kLoadWord,
4152 temp.AsRegister<Register>(),
4153 TR,
4154 invoke->GetStringInitOffset());
4155 break;
4156 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004157 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004158 break;
4159 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4160 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4161 break;
4162 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004163 __ LoadLiteral(temp.AsRegister<Register>(),
4164 base_reg,
4165 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4166 break;
4167 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4168 HMipsDexCacheArraysBase* base =
4169 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4170 int32_t offset =
4171 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4172 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4173 break;
4174 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004175 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004176 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004177 Register reg = temp.AsRegister<Register>();
4178 Register method_reg;
4179 if (current_method.IsRegister()) {
4180 method_reg = current_method.AsRegister<Register>();
4181 } else {
4182 // TODO: use the appropriate DCHECK() here if possible.
4183 // DCHECK(invoke->GetLocations()->Intrinsified());
4184 DCHECK(!current_method.IsValid());
4185 method_reg = reg;
4186 __ Lw(reg, SP, kCurrentMethodStackOffset);
4187 }
4188
4189 // temp = temp->dex_cache_resolved_methods_;
4190 __ LoadFromOffset(kLoadWord,
4191 reg,
4192 method_reg,
4193 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004194 // temp = temp[index_in_cache];
4195 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4196 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004197 __ LoadFromOffset(kLoadWord,
4198 reg,
4199 reg,
4200 CodeGenerator::GetCachePointerOffset(index_in_cache));
4201 break;
4202 }
4203 }
4204
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004205 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004206 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004207 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004208 break;
4209 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004210 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4211 // T9 prepared above for better instruction scheduling.
4212 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004213 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004214 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004215 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004216 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004217 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004218 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4219 LOG(FATAL) << "Unsupported";
4220 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004221 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4222 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004223 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004224 T9,
4225 callee_method.AsRegister<Register>(),
4226 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004227 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004228 // T9()
4229 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004230 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004231 break;
4232 }
4233 DCHECK(!IsLeafMethod());
4234}
4235
4236void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004237 // Explicit clinit checks triggered by static invokes must have been pruned by
4238 // art::PrepareForRegisterAllocation.
4239 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004240
4241 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4242 return;
4243 }
4244
4245 LocationSummary* locations = invoke->GetLocations();
4246 codegen_->GenerateStaticOrDirectCall(invoke,
4247 locations->HasTemps()
4248 ? locations->GetTemp(0)
4249 : Location::NoLocation());
4250 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4251}
4252
Chris Larsen3acee732015-11-18 13:31:08 -08004253void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004254 LocationSummary* locations = invoke->GetLocations();
4255 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004256 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004257 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4258 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4259 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004260 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004261
4262 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004263 DCHECK(receiver.IsRegister());
4264 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4265 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004266 // temp = temp->GetMethodAt(method_offset);
4267 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4268 // T9 = temp->GetEntryPoint();
4269 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4270 // T9();
4271 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004272 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004273}
4274
4275void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4276 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4277 return;
4278 }
4279
4280 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004281 DCHECK(!codegen_->IsLeafMethod());
4282 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4283}
4284
4285void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004286 if (cls->NeedsAccessCheck()) {
4287 InvokeRuntimeCallingConvention calling_convention;
4288 CodeGenerator::CreateLoadClassLocationSummary(
4289 cls,
4290 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4291 Location::RegisterLocation(V0),
4292 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4293 return;
4294 }
4295
4296 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4297 ? LocationSummary::kCallOnSlowPath
4298 : LocationSummary::kNoCall;
4299 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4300 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4301 switch (load_kind) {
4302 // We need an extra register for PC-relative literals on R2.
4303 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4304 case HLoadClass::LoadKind::kBootImageAddress:
4305 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4306 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4307 break;
4308 }
4309 FALLTHROUGH_INTENDED;
4310 // We need an extra register for PC-relative dex cache accesses.
4311 case HLoadClass::LoadKind::kDexCachePcRelative:
4312 case HLoadClass::LoadKind::kReferrersClass:
4313 case HLoadClass::LoadKind::kDexCacheViaMethod:
4314 locations->SetInAt(0, Location::RequiresRegister());
4315 break;
4316 default:
4317 break;
4318 }
4319 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004320}
4321
4322void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4323 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004324 if (cls->NeedsAccessCheck()) {
4325 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004326 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004327 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004328 return;
4329 }
4330
Alexey Frunze06a46c42016-07-19 15:00:40 -07004331 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4332 Location out_loc = locations->Out();
4333 Register out = out_loc.AsRegister<Register>();
4334 Register base_or_current_method_reg;
4335 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4336 switch (load_kind) {
4337 // We need an extra register for PC-relative literals on R2.
4338 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4339 case HLoadClass::LoadKind::kBootImageAddress:
4340 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4341 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4342 break;
4343 // We need an extra register for PC-relative dex cache accesses.
4344 case HLoadClass::LoadKind::kDexCachePcRelative:
4345 case HLoadClass::LoadKind::kReferrersClass:
4346 case HLoadClass::LoadKind::kDexCacheViaMethod:
4347 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4348 break;
4349 default:
4350 base_or_current_method_reg = ZERO;
4351 break;
4352 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004353
Alexey Frunze06a46c42016-07-19 15:00:40 -07004354 bool generate_null_check = false;
4355 switch (load_kind) {
4356 case HLoadClass::LoadKind::kReferrersClass: {
4357 DCHECK(!cls->CanCallRuntime());
4358 DCHECK(!cls->MustGenerateClinitCheck());
4359 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4360 GenerateGcRootFieldLoad(cls,
4361 out_loc,
4362 base_or_current_method_reg,
4363 ArtMethod::DeclaringClassOffset().Int32Value());
4364 break;
4365 }
4366 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4367 DCHECK(!kEmitCompilerReadBarrier);
4368 __ LoadLiteral(out,
4369 base_or_current_method_reg,
4370 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4371 cls->GetTypeIndex()));
4372 break;
4373 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4374 DCHECK(!kEmitCompilerReadBarrier);
4375 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4376 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004377 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004378 if (isR6) {
4379 __ Bind(&info->high_label);
4380 __ Bind(&info->pc_rel_label);
4381 // Add a 32-bit offset to PC.
4382 __ Auipc(out, /* placeholder */ 0x1234);
4383 __ Addiu(out, out, /* placeholder */ 0x5678);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004384 } else {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004385 __ Bind(&info->high_label);
4386 __ Lui(out, /* placeholder */ 0x1234);
4387 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4388 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4389 __ Ori(out, out, /* placeholder */ 0x5678);
4390 // Add a 32-bit offset to PC.
4391 __ Addu(out, out, base_or_current_method_reg);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004392 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004393 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004394 break;
4395 }
4396 case HLoadClass::LoadKind::kBootImageAddress: {
4397 DCHECK(!kEmitCompilerReadBarrier);
4398 DCHECK_NE(cls->GetAddress(), 0u);
4399 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4400 __ LoadLiteral(out,
4401 base_or_current_method_reg,
4402 codegen_->DeduplicateBootImageAddressLiteral(address));
4403 break;
4404 }
4405 case HLoadClass::LoadKind::kDexCacheAddress: {
4406 DCHECK_NE(cls->GetAddress(), 0u);
4407 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4408 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4409 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4410 int16_t offset = Low16Bits(address);
4411 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4412 __ Lui(out, High16Bits(base_address));
4413 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4414 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4415 generate_null_check = !cls->IsInDexCache();
4416 break;
4417 }
4418 case HLoadClass::LoadKind::kDexCachePcRelative: {
4419 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4420 int32_t offset =
4421 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4422 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4423 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4424 generate_null_check = !cls->IsInDexCache();
4425 break;
4426 }
4427 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4428 // /* GcRoot<mirror::Class>[] */ out =
4429 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4430 __ LoadFromOffset(kLoadWord,
4431 out,
4432 base_or_current_method_reg,
4433 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4434 // /* GcRoot<mirror::Class> */ out = out[type_index]
4435 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4436 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4437 generate_null_check = !cls->IsInDexCache();
4438 }
4439 }
4440
4441 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4442 DCHECK(cls->CanCallRuntime());
4443 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4444 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4445 codegen_->AddSlowPath(slow_path);
4446 if (generate_null_check) {
4447 __ Beqz(out, slow_path->GetEntryLabel());
4448 }
4449 if (cls->MustGenerateClinitCheck()) {
4450 GenerateClassInitializationCheck(slow_path, out);
4451 } else {
4452 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004453 }
4454 }
4455}
4456
4457static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004458 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004459}
4460
4461void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4462 LocationSummary* locations =
4463 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4464 locations->SetOut(Location::RequiresRegister());
4465}
4466
4467void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4468 Register out = load->GetLocations()->Out().AsRegister<Register>();
4469 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4470}
4471
4472void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4473 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4474}
4475
4476void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4477 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4478}
4479
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004480void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004481 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004482 ? LocationSummary::kCallOnSlowPath
4483 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004484 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004485 HLoadString::LoadKind load_kind = load->GetLoadKind();
4486 switch (load_kind) {
4487 // We need an extra register for PC-relative literals on R2.
4488 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4489 case HLoadString::LoadKind::kBootImageAddress:
4490 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4491 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4492 break;
4493 }
4494 FALLTHROUGH_INTENDED;
4495 // We need an extra register for PC-relative dex cache accesses.
4496 case HLoadString::LoadKind::kDexCachePcRelative:
4497 case HLoadString::LoadKind::kDexCacheViaMethod:
4498 locations->SetInAt(0, Location::RequiresRegister());
4499 break;
4500 default:
4501 break;
4502 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004503 locations->SetOut(Location::RequiresRegister());
4504}
4505
4506void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004507 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004508 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004509 Location out_loc = locations->Out();
4510 Register out = out_loc.AsRegister<Register>();
4511 Register base_or_current_method_reg;
4512 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4513 switch (load_kind) {
4514 // We need an extra register for PC-relative literals on R2.
4515 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4516 case HLoadString::LoadKind::kBootImageAddress:
4517 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4518 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4519 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004520 default:
4521 base_or_current_method_reg = ZERO;
4522 break;
4523 }
4524
4525 switch (load_kind) {
4526 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4527 DCHECK(!kEmitCompilerReadBarrier);
4528 __ LoadLiteral(out,
4529 base_or_current_method_reg,
4530 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4531 load->GetStringIndex()));
4532 return; // No dex cache slow path.
4533 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4534 DCHECK(!kEmitCompilerReadBarrier);
4535 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4536 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004537 bool reordering = __ SetReorder(false);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004538 if (isR6) {
4539 __ Bind(&info->high_label);
4540 __ Bind(&info->pc_rel_label);
4541 // Add a 32-bit offset to PC.
4542 __ Auipc(out, /* placeholder */ 0x1234);
4543 __ Addiu(out, out, /* placeholder */ 0x5678);
4544 } else {
4545 __ Bind(&info->high_label);
4546 __ Lui(out, /* placeholder */ 0x1234);
4547 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4548 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4549 __ Ori(out, out, /* placeholder */ 0x5678);
4550 // Add a 32-bit offset to PC.
4551 __ Addu(out, out, base_or_current_method_reg);
4552 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004553 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004554 return; // No dex cache slow path.
4555 }
4556 case HLoadString::LoadKind::kBootImageAddress: {
4557 DCHECK(!kEmitCompilerReadBarrier);
4558 DCHECK_NE(load->GetAddress(), 0u);
4559 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4560 __ LoadLiteral(out,
4561 base_or_current_method_reg,
4562 codegen_->DeduplicateBootImageAddressLiteral(address));
4563 return; // No dex cache slow path.
4564 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004565 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004566 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004567 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004568
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004569 // TODO: Re-add the compiler code to do string dex cache lookup again.
4570 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4571 codegen_->AddSlowPath(slow_path);
4572 __ B(slow_path->GetEntryLabel());
4573 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004574}
4575
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004576void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4578 locations->SetOut(Location::ConstantLocation(constant));
4579}
4580
4581void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4582 // Will be generated at use site.
4583}
4584
4585void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4586 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004588 InvokeRuntimeCallingConvention calling_convention;
4589 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4590}
4591
4592void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4593 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01004594 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004595 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4596 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004597 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004598 }
4599 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4600}
4601
4602void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4603 LocationSummary* locations =
4604 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4605 switch (mul->GetResultType()) {
4606 case Primitive::kPrimInt:
4607 case Primitive::kPrimLong:
4608 locations->SetInAt(0, Location::RequiresRegister());
4609 locations->SetInAt(1, Location::RequiresRegister());
4610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4611 break;
4612
4613 case Primitive::kPrimFloat:
4614 case Primitive::kPrimDouble:
4615 locations->SetInAt(0, Location::RequiresFpuRegister());
4616 locations->SetInAt(1, Location::RequiresFpuRegister());
4617 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4618 break;
4619
4620 default:
4621 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4622 }
4623}
4624
4625void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4626 Primitive::Type type = instruction->GetType();
4627 LocationSummary* locations = instruction->GetLocations();
4628 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4629
4630 switch (type) {
4631 case Primitive::kPrimInt: {
4632 Register dst = locations->Out().AsRegister<Register>();
4633 Register lhs = locations->InAt(0).AsRegister<Register>();
4634 Register rhs = locations->InAt(1).AsRegister<Register>();
4635
4636 if (isR6) {
4637 __ MulR6(dst, lhs, rhs);
4638 } else {
4639 __ MulR2(dst, lhs, rhs);
4640 }
4641 break;
4642 }
4643 case Primitive::kPrimLong: {
4644 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4645 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4646 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4647 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4648 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4649 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4650
4651 // Extra checks to protect caused by the existance of A1_A2.
4652 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4653 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4654 DCHECK_NE(dst_high, lhs_low);
4655 DCHECK_NE(dst_high, rhs_low);
4656
4657 // A_B * C_D
4658 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4659 // dst_lo: [ low(B*D) ]
4660 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4661
4662 if (isR6) {
4663 __ MulR6(TMP, lhs_high, rhs_low);
4664 __ MulR6(dst_high, lhs_low, rhs_high);
4665 __ Addu(dst_high, dst_high, TMP);
4666 __ MuhuR6(TMP, lhs_low, rhs_low);
4667 __ Addu(dst_high, dst_high, TMP);
4668 __ MulR6(dst_low, lhs_low, rhs_low);
4669 } else {
4670 __ MulR2(TMP, lhs_high, rhs_low);
4671 __ MulR2(dst_high, lhs_low, rhs_high);
4672 __ Addu(dst_high, dst_high, TMP);
4673 __ MultuR2(lhs_low, rhs_low);
4674 __ Mfhi(TMP);
4675 __ Addu(dst_high, dst_high, TMP);
4676 __ Mflo(dst_low);
4677 }
4678 break;
4679 }
4680 case Primitive::kPrimFloat:
4681 case Primitive::kPrimDouble: {
4682 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4683 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4684 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4685 if (type == Primitive::kPrimFloat) {
4686 __ MulS(dst, lhs, rhs);
4687 } else {
4688 __ MulD(dst, lhs, rhs);
4689 }
4690 break;
4691 }
4692 default:
4693 LOG(FATAL) << "Unexpected mul type " << type;
4694 }
4695}
4696
4697void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4698 LocationSummary* locations =
4699 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4700 switch (neg->GetResultType()) {
4701 case Primitive::kPrimInt:
4702 case Primitive::kPrimLong:
4703 locations->SetInAt(0, Location::RequiresRegister());
4704 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4705 break;
4706
4707 case Primitive::kPrimFloat:
4708 case Primitive::kPrimDouble:
4709 locations->SetInAt(0, Location::RequiresFpuRegister());
4710 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4711 break;
4712
4713 default:
4714 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4715 }
4716}
4717
4718void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4719 Primitive::Type type = instruction->GetType();
4720 LocationSummary* locations = instruction->GetLocations();
4721
4722 switch (type) {
4723 case Primitive::kPrimInt: {
4724 Register dst = locations->Out().AsRegister<Register>();
4725 Register src = locations->InAt(0).AsRegister<Register>();
4726 __ Subu(dst, ZERO, src);
4727 break;
4728 }
4729 case Primitive::kPrimLong: {
4730 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4731 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4732 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4733 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4734 __ Subu(dst_low, ZERO, src_low);
4735 __ Sltu(TMP, ZERO, dst_low);
4736 __ Subu(dst_high, ZERO, src_high);
4737 __ Subu(dst_high, dst_high, TMP);
4738 break;
4739 }
4740 case Primitive::kPrimFloat:
4741 case Primitive::kPrimDouble: {
4742 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4743 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4744 if (type == Primitive::kPrimFloat) {
4745 __ NegS(dst, src);
4746 } else {
4747 __ NegD(dst, src);
4748 }
4749 break;
4750 }
4751 default:
4752 LOG(FATAL) << "Unexpected neg type " << type;
4753 }
4754}
4755
4756void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4757 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004759 InvokeRuntimeCallingConvention calling_convention;
4760 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4761 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4762 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4763 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4764}
4765
4766void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4767 InvokeRuntimeCallingConvention calling_convention;
4768 Register current_method_register = calling_convention.GetRegisterAt(2);
4769 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4770 // Move an uint16_t value to a register.
4771 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004772 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004773 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4774 void*, uint32_t, int32_t, ArtMethod*>();
4775}
4776
4777void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4778 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004779 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004780 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004781 if (instruction->IsStringAlloc()) {
4782 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4783 } else {
4784 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4785 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4786 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004787 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4788}
4789
4790void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004791 if (instruction->IsStringAlloc()) {
4792 // String is allocated through StringFactory. Call NewEmptyString entry point.
4793 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004794 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004795 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4796 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4797 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004798 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00004799 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4800 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004801 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004802 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4803 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004804}
4805
4806void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4807 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4808 locations->SetInAt(0, Location::RequiresRegister());
4809 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4810}
4811
4812void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4813 Primitive::Type type = instruction->GetType();
4814 LocationSummary* locations = instruction->GetLocations();
4815
4816 switch (type) {
4817 case Primitive::kPrimInt: {
4818 Register dst = locations->Out().AsRegister<Register>();
4819 Register src = locations->InAt(0).AsRegister<Register>();
4820 __ Nor(dst, src, ZERO);
4821 break;
4822 }
4823
4824 case Primitive::kPrimLong: {
4825 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4826 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4827 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4828 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4829 __ Nor(dst_high, src_high, ZERO);
4830 __ Nor(dst_low, src_low, ZERO);
4831 break;
4832 }
4833
4834 default:
4835 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4836 }
4837}
4838
4839void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4840 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4841 locations->SetInAt(0, Location::RequiresRegister());
4842 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4843}
4844
4845void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4846 LocationSummary* locations = instruction->GetLocations();
4847 __ Xori(locations->Out().AsRegister<Register>(),
4848 locations->InAt(0).AsRegister<Register>(),
4849 1);
4850}
4851
4852void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4853 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4854 ? LocationSummary::kCallOnSlowPath
4855 : LocationSummary::kNoCall;
4856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4857 locations->SetInAt(0, Location::RequiresRegister());
4858 if (instruction->HasUses()) {
4859 locations->SetOut(Location::SameAsFirstInput());
4860 }
4861}
4862
Calin Juravle2ae48182016-03-16 14:05:09 +00004863void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4864 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004865 return;
4866 }
4867 Location obj = instruction->GetLocations()->InAt(0);
4868
4869 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004870 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004871}
4872
Calin Juravle2ae48182016-03-16 14:05:09 +00004873void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004874 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004875 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004876
4877 Location obj = instruction->GetLocations()->InAt(0);
4878
4879 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4880}
4881
4882void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004883 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004884}
4885
4886void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4887 HandleBinaryOp(instruction);
4888}
4889
4890void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4891 HandleBinaryOp(instruction);
4892}
4893
4894void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4895 LOG(FATAL) << "Unreachable";
4896}
4897
4898void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4899 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4900}
4901
4902void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4903 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4904 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4905 if (location.IsStackSlot()) {
4906 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4907 } else if (location.IsDoubleStackSlot()) {
4908 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4909 }
4910 locations->SetOut(location);
4911}
4912
4913void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4914 ATTRIBUTE_UNUSED) {
4915 // Nothing to do, the parameter is already at its location.
4916}
4917
4918void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4919 LocationSummary* locations =
4920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4921 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4922}
4923
4924void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4925 ATTRIBUTE_UNUSED) {
4926 // Nothing to do, the method is already at its location.
4927}
4928
4929void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4930 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004931 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004932 locations->SetInAt(i, Location::Any());
4933 }
4934 locations->SetOut(Location::Any());
4935}
4936
4937void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4938 LOG(FATAL) << "Unreachable";
4939}
4940
4941void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4942 Primitive::Type type = rem->GetResultType();
4943 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004944 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004945 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4946
4947 switch (type) {
4948 case Primitive::kPrimInt:
4949 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004950 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4952 break;
4953
4954 case Primitive::kPrimLong: {
4955 InvokeRuntimeCallingConvention calling_convention;
4956 locations->SetInAt(0, Location::RegisterPairLocation(
4957 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4958 locations->SetInAt(1, Location::RegisterPairLocation(
4959 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4960 locations->SetOut(calling_convention.GetReturnLocation(type));
4961 break;
4962 }
4963
4964 case Primitive::kPrimFloat:
4965 case Primitive::kPrimDouble: {
4966 InvokeRuntimeCallingConvention calling_convention;
4967 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4968 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4969 locations->SetOut(calling_convention.GetReturnLocation(type));
4970 break;
4971 }
4972
4973 default:
4974 LOG(FATAL) << "Unexpected rem type " << type;
4975 }
4976}
4977
4978void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4979 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004980
4981 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004982 case Primitive::kPrimInt:
4983 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004984 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004985 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01004986 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004987 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4988 break;
4989 }
4990 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01004991 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004992 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004993 break;
4994 }
4995 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01004996 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004997 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004998 break;
4999 }
5000 default:
5001 LOG(FATAL) << "Unexpected rem type " << type;
5002 }
5003}
5004
5005void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5006 memory_barrier->SetLocations(nullptr);
5007}
5008
5009void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5010 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5011}
5012
5013void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5015 Primitive::Type return_type = ret->InputAt(0)->GetType();
5016 locations->SetInAt(0, MipsReturnLocation(return_type));
5017}
5018
5019void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5020 codegen_->GenerateFrameExit();
5021}
5022
5023void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5024 ret->SetLocations(nullptr);
5025}
5026
5027void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5028 codegen_->GenerateFrameExit();
5029}
5030
Alexey Frunze92d90602015-12-18 18:16:36 -08005031void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5032 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005033}
5034
Alexey Frunze92d90602015-12-18 18:16:36 -08005035void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5036 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005037}
5038
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005039void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5040 HandleShift(shl);
5041}
5042
5043void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5044 HandleShift(shl);
5045}
5046
5047void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5048 HandleShift(shr);
5049}
5050
5051void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5052 HandleShift(shr);
5053}
5054
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005055void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5056 HandleBinaryOp(instruction);
5057}
5058
5059void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5060 HandleBinaryOp(instruction);
5061}
5062
5063void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5064 HandleFieldGet(instruction, instruction->GetFieldInfo());
5065}
5066
5067void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5068 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5069}
5070
5071void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5072 HandleFieldSet(instruction, instruction->GetFieldInfo());
5073}
5074
5075void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5076 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5077}
5078
5079void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5080 HUnresolvedInstanceFieldGet* instruction) {
5081 FieldAccessCallingConventionMIPS calling_convention;
5082 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5083 instruction->GetFieldType(),
5084 calling_convention);
5085}
5086
5087void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5088 HUnresolvedInstanceFieldGet* instruction) {
5089 FieldAccessCallingConventionMIPS calling_convention;
5090 codegen_->GenerateUnresolvedFieldAccess(instruction,
5091 instruction->GetFieldType(),
5092 instruction->GetFieldIndex(),
5093 instruction->GetDexPc(),
5094 calling_convention);
5095}
5096
5097void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5098 HUnresolvedInstanceFieldSet* instruction) {
5099 FieldAccessCallingConventionMIPS calling_convention;
5100 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5101 instruction->GetFieldType(),
5102 calling_convention);
5103}
5104
5105void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5106 HUnresolvedInstanceFieldSet* instruction) {
5107 FieldAccessCallingConventionMIPS calling_convention;
5108 codegen_->GenerateUnresolvedFieldAccess(instruction,
5109 instruction->GetFieldType(),
5110 instruction->GetFieldIndex(),
5111 instruction->GetDexPc(),
5112 calling_convention);
5113}
5114
5115void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5116 HUnresolvedStaticFieldGet* instruction) {
5117 FieldAccessCallingConventionMIPS calling_convention;
5118 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5119 instruction->GetFieldType(),
5120 calling_convention);
5121}
5122
5123void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5124 HUnresolvedStaticFieldGet* instruction) {
5125 FieldAccessCallingConventionMIPS calling_convention;
5126 codegen_->GenerateUnresolvedFieldAccess(instruction,
5127 instruction->GetFieldType(),
5128 instruction->GetFieldIndex(),
5129 instruction->GetDexPc(),
5130 calling_convention);
5131}
5132
5133void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5134 HUnresolvedStaticFieldSet* instruction) {
5135 FieldAccessCallingConventionMIPS calling_convention;
5136 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5137 instruction->GetFieldType(),
5138 calling_convention);
5139}
5140
5141void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5142 HUnresolvedStaticFieldSet* instruction) {
5143 FieldAccessCallingConventionMIPS calling_convention;
5144 codegen_->GenerateUnresolvedFieldAccess(instruction,
5145 instruction->GetFieldType(),
5146 instruction->GetFieldIndex(),
5147 instruction->GetDexPc(),
5148 calling_convention);
5149}
5150
5151void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5152 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5153}
5154
5155void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5156 HBasicBlock* block = instruction->GetBlock();
5157 if (block->GetLoopInformation() != nullptr) {
5158 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5159 // The back edge will generate the suspend check.
5160 return;
5161 }
5162 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5163 // The goto will generate the suspend check.
5164 return;
5165 }
5166 GenerateSuspendCheck(instruction, nullptr);
5167}
5168
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005169void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5170 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005171 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005172 InvokeRuntimeCallingConvention calling_convention;
5173 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5174}
5175
5176void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005177 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005178 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5179}
5180
5181void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5182 Primitive::Type input_type = conversion->GetInputType();
5183 Primitive::Type result_type = conversion->GetResultType();
5184 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005185 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005186
5187 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5188 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5189 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5190 }
5191
5192 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005193 if (!isR6 &&
5194 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5195 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005196 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005197 }
5198
5199 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5200
5201 if (call_kind == LocationSummary::kNoCall) {
5202 if (Primitive::IsFloatingPointType(input_type)) {
5203 locations->SetInAt(0, Location::RequiresFpuRegister());
5204 } else {
5205 locations->SetInAt(0, Location::RequiresRegister());
5206 }
5207
5208 if (Primitive::IsFloatingPointType(result_type)) {
5209 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5210 } else {
5211 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5212 }
5213 } else {
5214 InvokeRuntimeCallingConvention calling_convention;
5215
5216 if (Primitive::IsFloatingPointType(input_type)) {
5217 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5218 } else {
5219 DCHECK_EQ(input_type, Primitive::kPrimLong);
5220 locations->SetInAt(0, Location::RegisterPairLocation(
5221 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5222 }
5223
5224 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5225 }
5226}
5227
5228void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5229 LocationSummary* locations = conversion->GetLocations();
5230 Primitive::Type result_type = conversion->GetResultType();
5231 Primitive::Type input_type = conversion->GetInputType();
5232 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005233 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005234
5235 DCHECK_NE(input_type, result_type);
5236
5237 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5238 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5239 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5240 Register src = locations->InAt(0).AsRegister<Register>();
5241
Alexey Frunzea871ef12016-06-27 15:20:11 -07005242 if (dst_low != src) {
5243 __ Move(dst_low, src);
5244 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005245 __ Sra(dst_high, src, 31);
5246 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5247 Register dst = locations->Out().AsRegister<Register>();
5248 Register src = (input_type == Primitive::kPrimLong)
5249 ? locations->InAt(0).AsRegisterPairLow<Register>()
5250 : locations->InAt(0).AsRegister<Register>();
5251
5252 switch (result_type) {
5253 case Primitive::kPrimChar:
5254 __ Andi(dst, src, 0xFFFF);
5255 break;
5256 case Primitive::kPrimByte:
5257 if (has_sign_extension) {
5258 __ Seb(dst, src);
5259 } else {
5260 __ Sll(dst, src, 24);
5261 __ Sra(dst, dst, 24);
5262 }
5263 break;
5264 case Primitive::kPrimShort:
5265 if (has_sign_extension) {
5266 __ Seh(dst, src);
5267 } else {
5268 __ Sll(dst, src, 16);
5269 __ Sra(dst, dst, 16);
5270 }
5271 break;
5272 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005273 if (dst != src) {
5274 __ Move(dst, src);
5275 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005276 break;
5277
5278 default:
5279 LOG(FATAL) << "Unexpected type conversion from " << input_type
5280 << " to " << result_type;
5281 }
5282 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005283 if (input_type == Primitive::kPrimLong) {
5284 if (isR6) {
5285 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5286 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5287 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5288 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5289 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5290 __ Mtc1(src_low, FTMP);
5291 __ Mthc1(src_high, FTMP);
5292 if (result_type == Primitive::kPrimFloat) {
5293 __ Cvtsl(dst, FTMP);
5294 } else {
5295 __ Cvtdl(dst, FTMP);
5296 }
5297 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005298 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
5299 : kQuickL2d;
5300 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005301 if (result_type == Primitive::kPrimFloat) {
5302 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5303 } else {
5304 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5305 }
5306 }
5307 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005308 Register src = locations->InAt(0).AsRegister<Register>();
5309 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5310 __ Mtc1(src, FTMP);
5311 if (result_type == Primitive::kPrimFloat) {
5312 __ Cvtsw(dst, FTMP);
5313 } else {
5314 __ Cvtdw(dst, FTMP);
5315 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005316 }
5317 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5318 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005319 if (result_type == Primitive::kPrimLong) {
5320 if (isR6) {
5321 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5322 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5323 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5324 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5325 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5326 MipsLabel truncate;
5327 MipsLabel done;
5328
5329 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5330 // value when the input is either a NaN or is outside of the range of the output type
5331 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5332 // the same result.
5333 //
5334 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5335 // value of the output type if the input is outside of the range after the truncation or
5336 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5337 // results. This matches the desired float/double-to-int/long conversion exactly.
5338 //
5339 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5340 //
5341 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5342 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5343 // even though it must be NAN2008=1 on R6.
5344 //
5345 // The code takes care of the different behaviors by first comparing the input to the
5346 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5347 // If the input is greater than or equal to the minimum, it procedes to the truncate
5348 // instruction, which will handle such an input the same way irrespective of NAN2008.
5349 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5350 // in order to return either zero or the minimum value.
5351 //
5352 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5353 // truncate instruction for MIPS64R6.
5354 if (input_type == Primitive::kPrimFloat) {
5355 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5356 __ LoadConst32(TMP, min_val);
5357 __ Mtc1(TMP, FTMP);
5358 __ CmpLeS(FTMP, FTMP, src);
5359 } else {
5360 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5361 __ LoadConst32(TMP, High32Bits(min_val));
5362 __ Mtc1(ZERO, FTMP);
5363 __ Mthc1(TMP, FTMP);
5364 __ CmpLeD(FTMP, FTMP, src);
5365 }
5366
5367 __ Bc1nez(FTMP, &truncate);
5368
5369 if (input_type == Primitive::kPrimFloat) {
5370 __ CmpEqS(FTMP, src, src);
5371 } else {
5372 __ CmpEqD(FTMP, src, src);
5373 }
5374 __ Move(dst_low, ZERO);
5375 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5376 __ Mfc1(TMP, FTMP);
5377 __ And(dst_high, dst_high, TMP);
5378
5379 __ B(&done);
5380
5381 __ Bind(&truncate);
5382
5383 if (input_type == Primitive::kPrimFloat) {
5384 __ TruncLS(FTMP, src);
5385 } else {
5386 __ TruncLD(FTMP, src);
5387 }
5388 __ Mfc1(dst_low, FTMP);
5389 __ Mfhc1(dst_high, FTMP);
5390
5391 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005392 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005393 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
5394 : kQuickD2l;
5395 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005396 if (input_type == Primitive::kPrimFloat) {
5397 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5398 } else {
5399 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5400 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005401 }
5402 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005403 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5404 Register dst = locations->Out().AsRegister<Register>();
5405 MipsLabel truncate;
5406 MipsLabel done;
5407
5408 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5409 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5410 // even though it must be NAN2008=1 on R6.
5411 //
5412 // For details see the large comment above for the truncation of float/double to long on R6.
5413 //
5414 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5415 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005416 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005417 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5418 __ LoadConst32(TMP, min_val);
5419 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005420 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005421 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5422 __ LoadConst32(TMP, High32Bits(min_val));
5423 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005424 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005425 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005426
5427 if (isR6) {
5428 if (input_type == Primitive::kPrimFloat) {
5429 __ CmpLeS(FTMP, FTMP, src);
5430 } else {
5431 __ CmpLeD(FTMP, FTMP, src);
5432 }
5433 __ Bc1nez(FTMP, &truncate);
5434
5435 if (input_type == Primitive::kPrimFloat) {
5436 __ CmpEqS(FTMP, src, src);
5437 } else {
5438 __ CmpEqD(FTMP, src, src);
5439 }
5440 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5441 __ Mfc1(TMP, FTMP);
5442 __ And(dst, dst, TMP);
5443 } else {
5444 if (input_type == Primitive::kPrimFloat) {
5445 __ ColeS(0, FTMP, src);
5446 } else {
5447 __ ColeD(0, FTMP, src);
5448 }
5449 __ Bc1t(0, &truncate);
5450
5451 if (input_type == Primitive::kPrimFloat) {
5452 __ CeqS(0, src, src);
5453 } else {
5454 __ CeqD(0, src, src);
5455 }
5456 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5457 __ Movf(dst, ZERO, 0);
5458 }
5459
5460 __ B(&done);
5461
5462 __ Bind(&truncate);
5463
5464 if (input_type == Primitive::kPrimFloat) {
5465 __ TruncWS(FTMP, src);
5466 } else {
5467 __ TruncWD(FTMP, src);
5468 }
5469 __ Mfc1(dst, FTMP);
5470
5471 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005472 }
5473 } else if (Primitive::IsFloatingPointType(result_type) &&
5474 Primitive::IsFloatingPointType(input_type)) {
5475 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5476 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5477 if (result_type == Primitive::kPrimFloat) {
5478 __ Cvtsd(dst, src);
5479 } else {
5480 __ Cvtds(dst, src);
5481 }
5482 } else {
5483 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5484 << " to " << result_type;
5485 }
5486}
5487
5488void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5489 HandleShift(ushr);
5490}
5491
5492void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5493 HandleShift(ushr);
5494}
5495
5496void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5497 HandleBinaryOp(instruction);
5498}
5499
5500void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5501 HandleBinaryOp(instruction);
5502}
5503
5504void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5505 // Nothing to do, this should be removed during prepare for register allocator.
5506 LOG(FATAL) << "Unreachable";
5507}
5508
5509void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5510 // Nothing to do, this should be removed during prepare for register allocator.
5511 LOG(FATAL) << "Unreachable";
5512}
5513
5514void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005515 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005516}
5517
5518void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005519 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005520}
5521
5522void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005523 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005524}
5525
5526void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005527 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005528}
5529
5530void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005531 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005532}
5533
5534void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005535 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005536}
5537
5538void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005539 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005540}
5541
5542void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005543 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005544}
5545
5546void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005547 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005548}
5549
5550void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005551 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005552}
5553
5554void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005555 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005556}
5557
5558void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005559 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005560}
5561
5562void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005563 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005564}
5565
5566void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005567 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005568}
5569
5570void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005571 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005572}
5573
5574void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005575 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005576}
5577
5578void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005579 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005580}
5581
5582void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005583 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005584}
5585
5586void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005587 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005588}
5589
5590void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005591 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005592}
5593
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005594void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5595 LocationSummary* locations =
5596 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5597 locations->SetInAt(0, Location::RequiresRegister());
5598}
5599
5600void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5601 int32_t lower_bound = switch_instr->GetStartValue();
5602 int32_t num_entries = switch_instr->GetNumEntries();
5603 LocationSummary* locations = switch_instr->GetLocations();
5604 Register value_reg = locations->InAt(0).AsRegister<Register>();
5605 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5606
5607 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005608 Register temp_reg = TMP;
5609 __ Addiu32(temp_reg, value_reg, -lower_bound);
5610 // Jump to default if index is negative
5611 // Note: We don't check the case that index is positive while value < lower_bound, because in
5612 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5613 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5614
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005615 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005616 // Jump to successors[0] if value == lower_bound.
5617 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5618 int32_t last_index = 0;
5619 for (; num_entries - last_index > 2; last_index += 2) {
5620 __ Addiu(temp_reg, temp_reg, -2);
5621 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5622 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5623 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5624 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5625 }
5626 if (num_entries - last_index == 2) {
5627 // The last missing case_value.
5628 __ Addiu(temp_reg, temp_reg, -1);
5629 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005630 }
5631
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005632 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005633 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5634 __ B(codegen_->GetLabelOf(default_block));
5635 }
5636}
5637
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005638void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5639 HMipsComputeBaseMethodAddress* insn) {
5640 LocationSummary* locations =
5641 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5642 locations->SetOut(Location::RequiresRegister());
5643}
5644
5645void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5646 HMipsComputeBaseMethodAddress* insn) {
5647 LocationSummary* locations = insn->GetLocations();
5648 Register reg = locations->Out().AsRegister<Register>();
5649
5650 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5651
5652 // Generate a dummy PC-relative call to obtain PC.
5653 __ Nal();
5654 // Grab the return address off RA.
5655 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005656 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005657
5658 // Remember this offset (the obtained PC value) for later use with constant area.
5659 __ BindPcRelBaseLabel();
5660}
5661
5662void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5663 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5664 locations->SetOut(Location::RequiresRegister());
5665}
5666
5667void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5668 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5669 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5670 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005671 bool reordering = __ SetReorder(false);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005672 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5673 __ Bind(&info->high_label);
5674 __ Bind(&info->pc_rel_label);
5675 // Add a 32-bit offset to PC.
5676 __ Auipc(reg, /* placeholder */ 0x1234);
5677 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5678 } else {
5679 // Generate a dummy PC-relative call to obtain PC.
5680 __ Nal();
5681 __ Bind(&info->high_label);
5682 __ Lui(reg, /* placeholder */ 0x1234);
5683 __ Bind(&info->pc_rel_label);
5684 __ Ori(reg, reg, /* placeholder */ 0x5678);
5685 // Add a 32-bit offset to PC.
5686 __ Addu(reg, reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005687 // TODO: Can we share this code with that of VisitMipsComputeBaseMethodAddress()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005688 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005689 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005690}
5691
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005692void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5693 // The trampoline uses the same calling convention as dex calling conventions,
5694 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5695 // the method_idx.
5696 HandleInvoke(invoke);
5697}
5698
5699void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5700 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5701}
5702
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005703void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5704 LocationSummary* locations =
5705 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5706 locations->SetInAt(0, Location::RequiresRegister());
5707 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005708}
5709
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005710void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5711 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005713 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005714 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005715 __ LoadFromOffset(kLoadWord,
5716 locations->Out().AsRegister<Register>(),
5717 locations->InAt(0).AsRegister<Register>(),
5718 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005719 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005720 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005721 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005722 __ LoadFromOffset(kLoadWord,
5723 locations->Out().AsRegister<Register>(),
5724 locations->InAt(0).AsRegister<Register>(),
5725 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005726 __ LoadFromOffset(kLoadWord,
5727 locations->Out().AsRegister<Register>(),
5728 locations->Out().AsRegister<Register>(),
5729 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005730 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005731}
5732
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005733#undef __
5734#undef QUICK_ENTRY_POINT
5735
5736} // namespace mips
5737} // namespace art