blob: adae3f708d2607598218c08b397c76645f15df07 [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
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700148// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
149#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
151
152class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
153 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000154 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155
156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
157 LocationSummary* locations = instruction_->GetLocations();
158 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
159 __ Bind(GetEntryLabel());
160 if (instruction_->CanThrowIntoCatchBlock()) {
161 // Live registers will be restored in the catch block if caught.
162 SaveLiveRegisters(codegen, instruction_->GetLocations());
163 }
164 // We're moving two locations to locations that could overlap, so we need a parallel
165 // move resolver.
166 InvokeRuntimeCallingConvention calling_convention;
167 codegen->EmitParallelMoves(locations->InAt(0),
168 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
169 Primitive::kPrimInt,
170 locations->InAt(1),
171 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
172 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100173 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
174 ? QUICK_ENTRY_POINT(pThrowStringBounds)
175 : QUICK_ENTRY_POINT(pThrowArrayBounds);
176 mips_codegen->InvokeRuntime(entry_point_offset,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200177 instruction_,
178 instruction_->GetDexPc(),
179 this,
180 IsDirectEntrypoint(kQuickThrowArrayBounds));
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100181 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200182 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
183 }
184
185 bool IsFatal() const OVERRIDE { return true; }
186
187 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
188
189 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200190 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
191};
192
193class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
194 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000195 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196
197 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
198 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
199 __ Bind(GetEntryLabel());
200 if (instruction_->CanThrowIntoCatchBlock()) {
201 // Live registers will be restored in the catch block if caught.
202 SaveLiveRegisters(codegen, instruction_->GetLocations());
203 }
204 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
205 instruction_,
206 instruction_->GetDexPc(),
207 this,
208 IsDirectEntrypoint(kQuickThrowDivZero));
209 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
210 }
211
212 bool IsFatal() const OVERRIDE { return true; }
213
214 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
215
216 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
218};
219
220class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
221 public:
222 LoadClassSlowPathMIPS(HLoadClass* cls,
223 HInstruction* at,
224 uint32_t dex_pc,
225 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000226 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 LocationSummary* locations = at_->GetLocations();
232 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
233
234 __ Bind(GetEntryLabel());
235 SaveLiveRegisters(codegen, locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
238 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
239
240 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
241 : QUICK_ENTRY_POINT(pInitializeType);
242 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
243 : IsDirectEntrypoint(kQuickInitializeType);
244
245 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
246 if (do_clinit_) {
247 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
248 } else {
249 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
250 }
251
252 // Move the class to the desired location.
253 Location out = locations->Out();
254 if (out.IsValid()) {
255 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
256 Primitive::Type type = at_->GetType();
257 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
258 }
259
260 RestoreLiveRegisters(codegen, locations);
261 __ B(GetExitLabel());
262 }
263
264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
265
266 private:
267 // The class this slow path will load.
268 HLoadClass* const cls_;
269
270 // The instruction where this slow path is happening.
271 // (Might be the load class or an initialization check).
272 HInstruction* const at_;
273
274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
281};
282
283class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
284 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286
287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
288 LocationSummary* locations = instruction_->GetLocations();
289 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
290 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
291
292 __ Bind(GetEntryLabel());
293 SaveLiveRegisters(codegen, locations);
294
295 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000296 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
297 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200298 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
299 instruction_,
300 instruction_->GetDexPc(),
301 this,
302 IsDirectEntrypoint(kQuickResolveString));
303 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
304 Primitive::Type type = instruction_->GetType();
305 mips_codegen->MoveLocation(locations->Out(),
306 calling_convention.GetReturnLocation(type),
307 type);
308
309 RestoreLiveRegisters(codegen, locations);
310 __ B(GetExitLabel());
311 }
312
313 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
314
315 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
317};
318
319class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
320 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
325 __ Bind(GetEntryLabel());
326 if (instruction_->CanThrowIntoCatchBlock()) {
327 // Live registers will be restored in the catch block if caught.
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 }
330 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
331 instruction_,
332 instruction_->GetDexPc(),
333 this,
334 IsDirectEntrypoint(kQuickThrowNullPointer));
335 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
336 }
337
338 bool IsFatal() const OVERRIDE { return true; }
339
340 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
341
342 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200343 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
344};
345
346class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
347 public:
348 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
355 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
356 instruction_,
357 instruction_->GetDexPc(),
358 this,
359 IsDirectEntrypoint(kQuickTestSuspend));
360 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
361 RestoreLiveRegisters(codegen, instruction_->GetLocations());
362 if (successor_ == nullptr) {
363 __ B(GetReturnLabel());
364 } else {
365 __ B(mips_codegen->GetLabelOf(successor_));
366 }
367 }
368
369 MipsLabel* GetReturnLabel() {
370 DCHECK(successor_ == nullptr);
371 return &return_label_;
372 }
373
374 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
375
376 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377 // If not null, the block to branch to after the suspend check.
378 HBasicBlock* const successor_;
379
380 // If `successor_` is null, the label to branch to after the suspend check.
381 MipsLabel return_label_;
382
383 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
384};
385
386class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
387 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
392 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
393 uint32_t dex_pc = instruction_->GetDexPc();
394 DCHECK(instruction_->IsCheckCast()
395 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
396 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
397
398 __ Bind(GetEntryLabel());
399 SaveLiveRegisters(codegen, locations);
400
401 // We're moving two locations to locations that could overlap, so we need a parallel
402 // move resolver.
403 InvokeRuntimeCallingConvention calling_convention;
404 codegen->EmitParallelMoves(locations->InAt(1),
405 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
406 Primitive::kPrimNot,
407 object_class,
408 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
409 Primitive::kPrimNot);
410
411 if (instruction_->IsInstanceOf()) {
412 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
413 instruction_,
414 dex_pc,
415 this,
416 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000417 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700418 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200419 Primitive::Type ret_type = instruction_->GetType();
420 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
421 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200422 } else {
423 DCHECK(instruction_->IsCheckCast());
424 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
425 instruction_,
426 dex_pc,
427 this,
428 IsDirectEntrypoint(kQuickCheckCast));
429 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
430 }
431
432 RestoreLiveRegisters(codegen, locations);
433 __ B(GetExitLabel());
434 }
435
436 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
437
438 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200439 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
440};
441
442class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
443 public:
Aart Bik42249c32016-01-07 15:33:50 -0800444 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000445 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446
447 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800448 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200449 __ Bind(GetEntryLabel());
450 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200451 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
452 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800453 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 this,
455 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000456 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200457 }
458
459 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
460
461 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200462 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
463};
464
465CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
466 const MipsInstructionSetFeatures& isa_features,
467 const CompilerOptions& compiler_options,
468 OptimizingCompilerStats* stats)
469 : CodeGenerator(graph,
470 kNumberOfCoreRegisters,
471 kNumberOfFRegisters,
472 kNumberOfRegisterPairs,
473 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
474 arraysize(kCoreCalleeSaves)),
475 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
476 arraysize(kFpuCalleeSaves)),
477 compiler_options,
478 stats),
479 block_labels_(nullptr),
480 location_builder_(graph, this),
481 instruction_visitor_(graph, this),
482 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100483 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700484 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700485 uint32_literals_(std::less<uint32_t>(),
486 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700487 method_patches_(MethodReferenceComparator(),
488 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
489 call_patches_(MethodReferenceComparator(),
490 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700491 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
492 boot_image_string_patches_(StringReferenceValueComparator(),
493 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
494 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
495 boot_image_type_patches_(TypeReferenceValueComparator(),
496 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
497 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
498 boot_image_address_patches_(std::less<uint32_t>(),
499 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
500 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200501 // Save RA (containing the return address) to mimic Quick.
502 AddAllocatedRegister(Location::RegisterLocation(RA));
503}
504
505#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700506// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
507#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200508#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
509
510void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
511 // Ensure that we fix up branches.
512 __ FinalizeCode();
513
514 // Adjust native pc offsets in stack maps.
515 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
516 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
517 uint32_t new_position = __ GetAdjustedPosition(old_position);
518 DCHECK_GE(new_position, old_position);
519 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
520 }
521
522 // Adjust pc offsets for the disassembly information.
523 if (disasm_info_ != nullptr) {
524 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
525 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
526 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
527 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
528 it.second.start = __ GetAdjustedPosition(it.second.start);
529 it.second.end = __ GetAdjustedPosition(it.second.end);
530 }
531 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
532 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
533 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
534 }
535 }
536
537 CodeGenerator::Finalize(allocator);
538}
539
540MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
541 return codegen_->GetAssembler();
542}
543
544void ParallelMoveResolverMIPS::EmitMove(size_t index) {
545 DCHECK_LT(index, moves_.size());
546 MoveOperands* move = moves_[index];
547 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
548}
549
550void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
551 DCHECK_LT(index, moves_.size());
552 MoveOperands* move = moves_[index];
553 Primitive::Type type = move->GetType();
554 Location loc1 = move->GetDestination();
555 Location loc2 = move->GetSource();
556
557 DCHECK(!loc1.IsConstant());
558 DCHECK(!loc2.IsConstant());
559
560 if (loc1.Equals(loc2)) {
561 return;
562 }
563
564 if (loc1.IsRegister() && loc2.IsRegister()) {
565 // Swap 2 GPRs.
566 Register r1 = loc1.AsRegister<Register>();
567 Register r2 = loc2.AsRegister<Register>();
568 __ Move(TMP, r2);
569 __ Move(r2, r1);
570 __ Move(r1, TMP);
571 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
572 FRegister f1 = loc1.AsFpuRegister<FRegister>();
573 FRegister f2 = loc2.AsFpuRegister<FRegister>();
574 if (type == Primitive::kPrimFloat) {
575 __ MovS(FTMP, f2);
576 __ MovS(f2, f1);
577 __ MovS(f1, FTMP);
578 } else {
579 DCHECK_EQ(type, Primitive::kPrimDouble);
580 __ MovD(FTMP, f2);
581 __ MovD(f2, f1);
582 __ MovD(f1, FTMP);
583 }
584 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
585 (loc1.IsFpuRegister() && loc2.IsRegister())) {
586 // Swap FPR and GPR.
587 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
588 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
589 : loc2.AsFpuRegister<FRegister>();
590 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
591 : loc2.AsRegister<Register>();
592 __ Move(TMP, r2);
593 __ Mfc1(r2, f1);
594 __ Mtc1(TMP, f1);
595 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
596 // Swap 2 GPR register pairs.
597 Register r1 = loc1.AsRegisterPairLow<Register>();
598 Register r2 = loc2.AsRegisterPairLow<Register>();
599 __ Move(TMP, r2);
600 __ Move(r2, r1);
601 __ Move(r1, TMP);
602 r1 = loc1.AsRegisterPairHigh<Register>();
603 r2 = loc2.AsRegisterPairHigh<Register>();
604 __ Move(TMP, r2);
605 __ Move(r2, r1);
606 __ Move(r1, TMP);
607 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
608 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
609 // Swap FPR and GPR register pair.
610 DCHECK_EQ(type, Primitive::kPrimDouble);
611 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
612 : loc2.AsFpuRegister<FRegister>();
613 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
614 : loc2.AsRegisterPairLow<Register>();
615 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
616 : loc2.AsRegisterPairHigh<Register>();
617 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
618 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
619 // unpredictable and the following mfch1 will fail.
620 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800621 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200622 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800623 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200624 __ Move(r2_l, TMP);
625 __ Move(r2_h, AT);
626 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
627 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
628 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
629 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000630 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
631 (loc1.IsStackSlot() && loc2.IsRegister())) {
632 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
633 : loc2.AsRegister<Register>();
634 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
635 : loc2.GetStackIndex();
636 __ Move(TMP, reg);
637 __ LoadFromOffset(kLoadWord, reg, SP, offset);
638 __ StoreToOffset(kStoreWord, TMP, SP, offset);
639 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
640 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
641 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
642 : loc2.AsRegisterPairLow<Register>();
643 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
644 : loc2.AsRegisterPairHigh<Register>();
645 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
646 : loc2.GetStackIndex();
647 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
648 : loc2.GetHighStackIndex(kMipsWordSize);
649 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000650 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000651 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000652 __ Move(TMP, reg_h);
653 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
654 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200655 } else {
656 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
657 }
658}
659
660void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
661 __ Pop(static_cast<Register>(reg));
662}
663
664void ParallelMoveResolverMIPS::SpillScratch(int reg) {
665 __ Push(static_cast<Register>(reg));
666}
667
668void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
669 // Allocate a scratch register other than TMP, if available.
670 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
671 // automatically unspilled when the scratch scope object is destroyed).
672 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
673 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
674 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
675 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
676 __ LoadFromOffset(kLoadWord,
677 Register(ensure_scratch.GetRegister()),
678 SP,
679 index1 + stack_offset);
680 __ LoadFromOffset(kLoadWord,
681 TMP,
682 SP,
683 index2 + stack_offset);
684 __ StoreToOffset(kStoreWord,
685 Register(ensure_scratch.GetRegister()),
686 SP,
687 index2 + stack_offset);
688 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
689 }
690}
691
Alexey Frunze73296a72016-06-03 22:51:46 -0700692void CodeGeneratorMIPS::ComputeSpillMask() {
693 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
694 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
695 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
696 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
697 // registers, include the ZERO register to force alignment of FPU callee-saved registers
698 // within the stack frame.
699 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
700 core_spill_mask_ |= (1 << ZERO);
701 }
Alexey Frunze06a46c42016-07-19 15:00:40 -0700702 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
703 // (this can happen in leaf methods), artificially spill the ZERO register in order to
704 // force explicit saving and restoring of RA. RA isn't saved/restored when it's the only
705 // spilled register.
706 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
707 // saved in an unused temporary register) and saving of RA and the current method pointer
708 // in the frame.
709 if (clobbered_ra_ && core_spill_mask_ == (1u << RA) && fpu_spill_mask_ == 0) {
710 core_spill_mask_ |= (1 << ZERO);
711 }
Alexey Frunze73296a72016-06-03 22:51:46 -0700712}
713
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200714static dwarf::Reg DWARFReg(Register reg) {
715 return dwarf::Reg::MipsCore(static_cast<int>(reg));
716}
717
718// TODO: mapping of floating-point registers to DWARF.
719
720void CodeGeneratorMIPS::GenerateFrameEntry() {
721 __ Bind(&frame_entry_label_);
722
723 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
724
725 if (do_overflow_check) {
726 __ LoadFromOffset(kLoadWord,
727 ZERO,
728 SP,
729 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
730 RecordPcInfo(nullptr, 0);
731 }
732
733 if (HasEmptyFrame()) {
734 return;
735 }
736
737 // Make sure the frame size isn't unreasonably large.
738 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
739 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
740 }
741
742 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200743
Alexey Frunze73296a72016-06-03 22:51:46 -0700744 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200745 __ IncreaseFrameSize(ofs);
746
Alexey Frunze73296a72016-06-03 22:51:46 -0700747 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
748 Register reg = static_cast<Register>(MostSignificantBit(mask));
749 mask ^= 1u << reg;
750 ofs -= kMipsWordSize;
751 // The ZERO register is only included for alignment.
752 if (reg != ZERO) {
753 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200754 __ cfi().RelOffset(DWARFReg(reg), ofs);
755 }
756 }
757
Alexey Frunze73296a72016-06-03 22:51:46 -0700758 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
759 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
760 mask ^= 1u << reg;
761 ofs -= kMipsDoublewordSize;
762 __ StoreDToOffset(reg, SP, ofs);
763 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200764 }
765
Alexey Frunze73296a72016-06-03 22:51:46 -0700766 // Store the current method pointer.
767 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200768}
769
770void CodeGeneratorMIPS::GenerateFrameExit() {
771 __ cfi().RememberState();
772
773 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200774 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200775
Alexey Frunze73296a72016-06-03 22:51:46 -0700776 // For better instruction scheduling restore RA before other registers.
777 uint32_t ofs = GetFrameSize();
778 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
779 Register reg = static_cast<Register>(MostSignificantBit(mask));
780 mask ^= 1u << reg;
781 ofs -= kMipsWordSize;
782 // The ZERO register is only included for alignment.
783 if (reg != ZERO) {
784 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200785 __ cfi().Restore(DWARFReg(reg));
786 }
787 }
788
Alexey Frunze73296a72016-06-03 22:51:46 -0700789 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
790 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
791 mask ^= 1u << reg;
792 ofs -= kMipsDoublewordSize;
793 __ LoadDFromOffset(reg, SP, ofs);
794 // TODO: __ cfi().Restore(DWARFReg(reg));
795 }
796
797 __ DecreaseFrameSize(GetFrameSize());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200798 }
799
800 __ Jr(RA);
801 __ Nop();
802
803 __ cfi().RestoreState();
804 __ cfi().DefCFAOffset(GetFrameSize());
805}
806
807void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
808 __ Bind(GetLabelOf(block));
809}
810
811void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
812 if (src.Equals(dst)) {
813 return;
814 }
815
816 if (src.IsConstant()) {
817 MoveConstant(dst, src.GetConstant());
818 } else {
819 if (Primitive::Is64BitType(dst_type)) {
820 Move64(dst, src);
821 } else {
822 Move32(dst, src);
823 }
824 }
825}
826
827void CodeGeneratorMIPS::Move32(Location destination, Location source) {
828 if (source.Equals(destination)) {
829 return;
830 }
831
832 if (destination.IsRegister()) {
833 if (source.IsRegister()) {
834 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
835 } else if (source.IsFpuRegister()) {
836 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
837 } else {
838 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
839 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
840 }
841 } else if (destination.IsFpuRegister()) {
842 if (source.IsRegister()) {
843 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
844 } else if (source.IsFpuRegister()) {
845 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
846 } else {
847 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
848 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
849 }
850 } else {
851 DCHECK(destination.IsStackSlot()) << destination;
852 if (source.IsRegister()) {
853 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
854 } else if (source.IsFpuRegister()) {
855 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
856 } else {
857 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
858 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
859 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
860 }
861 }
862}
863
864void CodeGeneratorMIPS::Move64(Location destination, Location source) {
865 if (source.Equals(destination)) {
866 return;
867 }
868
869 if (destination.IsRegisterPair()) {
870 if (source.IsRegisterPair()) {
871 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
872 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
873 } else if (source.IsFpuRegister()) {
874 Register dst_high = destination.AsRegisterPairHigh<Register>();
875 Register dst_low = destination.AsRegisterPairLow<Register>();
876 FRegister src = source.AsFpuRegister<FRegister>();
877 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800878 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200879 } else {
880 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
881 int32_t off = source.GetStackIndex();
882 Register r = destination.AsRegisterPairLow<Register>();
883 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
884 }
885 } else if (destination.IsFpuRegister()) {
886 if (source.IsRegisterPair()) {
887 FRegister dst = destination.AsFpuRegister<FRegister>();
888 Register src_high = source.AsRegisterPairHigh<Register>();
889 Register src_low = source.AsRegisterPairLow<Register>();
890 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800891 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200892 } else if (source.IsFpuRegister()) {
893 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
894 } else {
895 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
896 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
897 }
898 } else {
899 DCHECK(destination.IsDoubleStackSlot()) << destination;
900 int32_t off = destination.GetStackIndex();
901 if (source.IsRegisterPair()) {
902 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
903 } else if (source.IsFpuRegister()) {
904 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
905 } else {
906 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
907 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
908 __ StoreToOffset(kStoreWord, TMP, SP, off);
909 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
910 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
911 }
912 }
913}
914
915void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
916 if (c->IsIntConstant() || c->IsNullConstant()) {
917 // Move 32 bit constant.
918 int32_t value = GetInt32ValueOf(c);
919 if (destination.IsRegister()) {
920 Register dst = destination.AsRegister<Register>();
921 __ LoadConst32(dst, value);
922 } else {
923 DCHECK(destination.IsStackSlot())
924 << "Cannot move " << c->DebugName() << " to " << destination;
925 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
926 }
927 } else if (c->IsLongConstant()) {
928 // Move 64 bit constant.
929 int64_t value = GetInt64ValueOf(c);
930 if (destination.IsRegisterPair()) {
931 Register r_h = destination.AsRegisterPairHigh<Register>();
932 Register r_l = destination.AsRegisterPairLow<Register>();
933 __ LoadConst64(r_h, r_l, value);
934 } else {
935 DCHECK(destination.IsDoubleStackSlot())
936 << "Cannot move " << c->DebugName() << " to " << destination;
937 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
938 }
939 } else if (c->IsFloatConstant()) {
940 // Move 32 bit float constant.
941 int32_t value = GetInt32ValueOf(c);
942 if (destination.IsFpuRegister()) {
943 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
944 } else {
945 DCHECK(destination.IsStackSlot())
946 << "Cannot move " << c->DebugName() << " to " << destination;
947 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
948 }
949 } else {
950 // Move 64 bit double constant.
951 DCHECK(c->IsDoubleConstant()) << c->DebugName();
952 int64_t value = GetInt64ValueOf(c);
953 if (destination.IsFpuRegister()) {
954 FRegister fd = destination.AsFpuRegister<FRegister>();
955 __ LoadDConst64(fd, value, TMP);
956 } else {
957 DCHECK(destination.IsDoubleStackSlot())
958 << "Cannot move " << c->DebugName() << " to " << destination;
959 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
960 }
961 }
962}
963
964void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
965 DCHECK(destination.IsRegister());
966 Register dst = destination.AsRegister<Register>();
967 __ LoadConst32(dst, value);
968}
969
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200970void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
971 if (location.IsRegister()) {
972 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700973 } else if (location.IsRegisterPair()) {
974 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
975 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200976 } else {
977 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
978 }
979}
980
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700981void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
982 DCHECK(linker_patches->empty());
983 size_t size =
984 method_patches_.size() +
985 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700986 pc_relative_dex_cache_patches_.size() +
987 pc_relative_string_patches_.size() +
988 pc_relative_type_patches_.size() +
989 boot_image_string_patches_.size() +
990 boot_image_type_patches_.size() +
991 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700992 linker_patches->reserve(size);
993 for (const auto& entry : method_patches_) {
994 const MethodReference& target_method = entry.first;
995 Literal* literal = entry.second;
996 DCHECK(literal->GetLabel()->IsBound());
997 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
998 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
999 target_method.dex_file,
1000 target_method.dex_method_index));
1001 }
1002 for (const auto& entry : call_patches_) {
1003 const MethodReference& target_method = entry.first;
1004 Literal* literal = entry.second;
1005 DCHECK(literal->GetLabel()->IsBound());
1006 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1007 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1008 target_method.dex_file,
1009 target_method.dex_method_index));
1010 }
1011 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
1012 const DexFile& dex_file = info.target_dex_file;
1013 size_t base_element_offset = info.offset_or_index;
1014 DCHECK(info.high_label.IsBound());
1015 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1016 DCHECK(info.pc_rel_label.IsBound());
1017 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1018 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
1019 &dex_file,
1020 pc_rel_offset,
1021 base_element_offset));
1022 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001023 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
1024 const DexFile& dex_file = info.target_dex_file;
1025 size_t string_index = info.offset_or_index;
1026 DCHECK(info.high_label.IsBound());
1027 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1028 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1029 // the assembler's base label used for PC-relative literals.
1030 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1031 ? __ GetLabelLocation(&info.pc_rel_label)
1032 : __ GetPcRelBaseLabelLocation();
1033 linker_patches->push_back(LinkerPatch::RelativeStringPatch(high_offset,
1034 &dex_file,
1035 pc_rel_offset,
1036 string_index));
1037 }
1038 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
1039 const DexFile& dex_file = info.target_dex_file;
1040 size_t type_index = info.offset_or_index;
1041 DCHECK(info.high_label.IsBound());
1042 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1043 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1044 // the assembler's base label used for PC-relative literals.
1045 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1046 ? __ GetLabelLocation(&info.pc_rel_label)
1047 : __ GetPcRelBaseLabelLocation();
1048 linker_patches->push_back(LinkerPatch::RelativeTypePatch(high_offset,
1049 &dex_file,
1050 pc_rel_offset,
1051 type_index));
1052 }
1053 for (const auto& entry : boot_image_string_patches_) {
1054 const StringReference& target_string = entry.first;
1055 Literal* literal = entry.second;
1056 DCHECK(literal->GetLabel()->IsBound());
1057 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1058 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1059 target_string.dex_file,
1060 target_string.string_index));
1061 }
1062 for (const auto& entry : boot_image_type_patches_) {
1063 const TypeReference& target_type = entry.first;
1064 Literal* literal = entry.second;
1065 DCHECK(literal->GetLabel()->IsBound());
1066 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1067 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1068 target_type.dex_file,
1069 target_type.type_index));
1070 }
1071 for (const auto& entry : boot_image_address_patches_) {
1072 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1073 Literal* literal = entry.second;
1074 DCHECK(literal->GetLabel()->IsBound());
1075 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1076 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1077 }
1078}
1079
1080CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1081 const DexFile& dex_file, uint32_t string_index) {
1082 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1083}
1084
1085CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1086 const DexFile& dex_file, uint32_t type_index) {
1087 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001088}
1089
1090CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1091 const DexFile& dex_file, uint32_t element_offset) {
1092 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1093}
1094
1095CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1096 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1097 patches->emplace_back(dex_file, offset_or_index);
1098 return &patches->back();
1099}
1100
Alexey Frunze06a46c42016-07-19 15:00:40 -07001101Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1102 return map->GetOrCreate(
1103 value,
1104 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1105}
1106
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001107Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1108 MethodToLiteralMap* map) {
1109 return map->GetOrCreate(
1110 target_method,
1111 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1112}
1113
1114Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1115 return DeduplicateMethodLiteral(target_method, &method_patches_);
1116}
1117
1118Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1119 return DeduplicateMethodLiteral(target_method, &call_patches_);
1120}
1121
Alexey Frunze06a46c42016-07-19 15:00:40 -07001122Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1123 uint32_t string_index) {
1124 return boot_image_string_patches_.GetOrCreate(
1125 StringReference(&dex_file, string_index),
1126 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1127}
1128
1129Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1130 uint32_t type_index) {
1131 return boot_image_type_patches_.GetOrCreate(
1132 TypeReference(&dex_file, type_index),
1133 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1134}
1135
1136Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1137 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1138 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1139 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1140}
1141
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001142void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1143 MipsLabel done;
1144 Register card = AT;
1145 Register temp = TMP;
1146 __ Beqz(value, &done);
1147 __ LoadFromOffset(kLoadWord,
1148 card,
1149 TR,
1150 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1151 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1152 __ Addu(temp, card, temp);
1153 __ Sb(card, temp, 0);
1154 __ Bind(&done);
1155}
1156
David Brazdil58282f42016-01-14 12:45:10 +00001157void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001158 // Don't allocate the dalvik style register pair passing.
1159 blocked_register_pairs_[A1_A2] = true;
1160
1161 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1162 blocked_core_registers_[ZERO] = true;
1163 blocked_core_registers_[K0] = true;
1164 blocked_core_registers_[K1] = true;
1165 blocked_core_registers_[GP] = true;
1166 blocked_core_registers_[SP] = true;
1167 blocked_core_registers_[RA] = true;
1168
1169 // AT and TMP(T8) are used as temporary/scratch registers
1170 // (similar to how AT is used by MIPS assemblers).
1171 blocked_core_registers_[AT] = true;
1172 blocked_core_registers_[TMP] = true;
1173 blocked_fpu_registers_[FTMP] = true;
1174
1175 // Reserve suspend and thread registers.
1176 blocked_core_registers_[S0] = true;
1177 blocked_core_registers_[TR] = true;
1178
1179 // Reserve T9 for function calls
1180 blocked_core_registers_[T9] = true;
1181
1182 // Reserve odd-numbered FPU registers.
1183 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1184 blocked_fpu_registers_[i] = true;
1185 }
1186
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001187 UpdateBlockedPairRegisters();
1188}
1189
1190void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1191 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1192 MipsManagedRegister current =
1193 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1194 if (blocked_core_registers_[current.AsRegisterPairLow()]
1195 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1196 blocked_register_pairs_[i] = true;
1197 }
1198 }
1199}
1200
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001201size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1202 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1203 return kMipsWordSize;
1204}
1205
1206size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1207 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1208 return kMipsWordSize;
1209}
1210
1211size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1212 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1213 return kMipsDoublewordSize;
1214}
1215
1216size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1217 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1218 return kMipsDoublewordSize;
1219}
1220
1221void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001222 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001223}
1224
1225void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001226 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001227}
1228
1229void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1230 HInstruction* instruction,
1231 uint32_t dex_pc,
1232 SlowPathCode* slow_path) {
1233 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1234 instruction,
1235 dex_pc,
1236 slow_path,
1237 IsDirectEntrypoint(entrypoint));
1238}
1239
1240constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1241
1242void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1243 HInstruction* instruction,
1244 uint32_t dex_pc,
1245 SlowPathCode* slow_path,
1246 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001247 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1248 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001249 if (is_direct_entrypoint) {
1250 // Reserve argument space on stack (for $a0-$a3) for
1251 // entrypoints that directly reference native implementations.
1252 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001253 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001254 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001255 } else {
1256 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001257 }
1258 RecordPcInfo(instruction, dex_pc, slow_path);
1259}
1260
1261void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1262 Register class_reg) {
1263 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1264 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1265 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1266 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1267 __ Sync(0);
1268 __ Bind(slow_path->GetExitLabel());
1269}
1270
1271void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1272 __ Sync(0); // Only stype 0 is supported.
1273}
1274
1275void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1276 HBasicBlock* successor) {
1277 SuspendCheckSlowPathMIPS* slow_path =
1278 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1279 codegen_->AddSlowPath(slow_path);
1280
1281 __ LoadFromOffset(kLoadUnsignedHalfword,
1282 TMP,
1283 TR,
1284 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1285 if (successor == nullptr) {
1286 __ Bnez(TMP, slow_path->GetEntryLabel());
1287 __ Bind(slow_path->GetReturnLabel());
1288 } else {
1289 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1290 __ B(slow_path->GetEntryLabel());
1291 // slow_path will return to GetLabelOf(successor).
1292 }
1293}
1294
1295InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1296 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001297 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001298 assembler_(codegen->GetAssembler()),
1299 codegen_(codegen) {}
1300
1301void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1302 DCHECK_EQ(instruction->InputCount(), 2U);
1303 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1304 Primitive::Type type = instruction->GetResultType();
1305 switch (type) {
1306 case Primitive::kPrimInt: {
1307 locations->SetInAt(0, Location::RequiresRegister());
1308 HInstruction* right = instruction->InputAt(1);
1309 bool can_use_imm = false;
1310 if (right->IsConstant()) {
1311 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1312 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1313 can_use_imm = IsUint<16>(imm);
1314 } else if (instruction->IsAdd()) {
1315 can_use_imm = IsInt<16>(imm);
1316 } else {
1317 DCHECK(instruction->IsSub());
1318 can_use_imm = IsInt<16>(-imm);
1319 }
1320 }
1321 if (can_use_imm)
1322 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1323 else
1324 locations->SetInAt(1, Location::RequiresRegister());
1325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1326 break;
1327 }
1328
1329 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001330 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001331 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1332 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001333 break;
1334 }
1335
1336 case Primitive::kPrimFloat:
1337 case Primitive::kPrimDouble:
1338 DCHECK(instruction->IsAdd() || instruction->IsSub());
1339 locations->SetInAt(0, Location::RequiresFpuRegister());
1340 locations->SetInAt(1, Location::RequiresFpuRegister());
1341 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1342 break;
1343
1344 default:
1345 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1346 }
1347}
1348
1349void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1350 Primitive::Type type = instruction->GetType();
1351 LocationSummary* locations = instruction->GetLocations();
1352
1353 switch (type) {
1354 case Primitive::kPrimInt: {
1355 Register dst = locations->Out().AsRegister<Register>();
1356 Register lhs = locations->InAt(0).AsRegister<Register>();
1357 Location rhs_location = locations->InAt(1);
1358
1359 Register rhs_reg = ZERO;
1360 int32_t rhs_imm = 0;
1361 bool use_imm = rhs_location.IsConstant();
1362 if (use_imm) {
1363 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1364 } else {
1365 rhs_reg = rhs_location.AsRegister<Register>();
1366 }
1367
1368 if (instruction->IsAnd()) {
1369 if (use_imm)
1370 __ Andi(dst, lhs, rhs_imm);
1371 else
1372 __ And(dst, lhs, rhs_reg);
1373 } else if (instruction->IsOr()) {
1374 if (use_imm)
1375 __ Ori(dst, lhs, rhs_imm);
1376 else
1377 __ Or(dst, lhs, rhs_reg);
1378 } else if (instruction->IsXor()) {
1379 if (use_imm)
1380 __ Xori(dst, lhs, rhs_imm);
1381 else
1382 __ Xor(dst, lhs, rhs_reg);
1383 } else if (instruction->IsAdd()) {
1384 if (use_imm)
1385 __ Addiu(dst, lhs, rhs_imm);
1386 else
1387 __ Addu(dst, lhs, rhs_reg);
1388 } else {
1389 DCHECK(instruction->IsSub());
1390 if (use_imm)
1391 __ Addiu(dst, lhs, -rhs_imm);
1392 else
1393 __ Subu(dst, lhs, rhs_reg);
1394 }
1395 break;
1396 }
1397
1398 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001399 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1400 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1401 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1402 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001403 Location rhs_location = locations->InAt(1);
1404 bool use_imm = rhs_location.IsConstant();
1405 if (!use_imm) {
1406 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1407 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1408 if (instruction->IsAnd()) {
1409 __ And(dst_low, lhs_low, rhs_low);
1410 __ And(dst_high, lhs_high, rhs_high);
1411 } else if (instruction->IsOr()) {
1412 __ Or(dst_low, lhs_low, rhs_low);
1413 __ Or(dst_high, lhs_high, rhs_high);
1414 } else if (instruction->IsXor()) {
1415 __ Xor(dst_low, lhs_low, rhs_low);
1416 __ Xor(dst_high, lhs_high, rhs_high);
1417 } else if (instruction->IsAdd()) {
1418 if (lhs_low == rhs_low) {
1419 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1420 __ Slt(TMP, lhs_low, ZERO);
1421 __ Addu(dst_low, lhs_low, rhs_low);
1422 } else {
1423 __ Addu(dst_low, lhs_low, rhs_low);
1424 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1425 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1426 }
1427 __ Addu(dst_high, lhs_high, rhs_high);
1428 __ Addu(dst_high, dst_high, TMP);
1429 } else {
1430 DCHECK(instruction->IsSub());
1431 __ Sltu(TMP, lhs_low, rhs_low);
1432 __ Subu(dst_low, lhs_low, rhs_low);
1433 __ Subu(dst_high, lhs_high, rhs_high);
1434 __ Subu(dst_high, dst_high, TMP);
1435 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001436 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001437 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1438 if (instruction->IsOr()) {
1439 uint32_t low = Low32Bits(value);
1440 uint32_t high = High32Bits(value);
1441 if (IsUint<16>(low)) {
1442 if (dst_low != lhs_low || low != 0) {
1443 __ Ori(dst_low, lhs_low, low);
1444 }
1445 } else {
1446 __ LoadConst32(TMP, low);
1447 __ Or(dst_low, lhs_low, TMP);
1448 }
1449 if (IsUint<16>(high)) {
1450 if (dst_high != lhs_high || high != 0) {
1451 __ Ori(dst_high, lhs_high, high);
1452 }
1453 } else {
1454 if (high != low) {
1455 __ LoadConst32(TMP, high);
1456 }
1457 __ Or(dst_high, lhs_high, TMP);
1458 }
1459 } else if (instruction->IsXor()) {
1460 uint32_t low = Low32Bits(value);
1461 uint32_t high = High32Bits(value);
1462 if (IsUint<16>(low)) {
1463 if (dst_low != lhs_low || low != 0) {
1464 __ Xori(dst_low, lhs_low, low);
1465 }
1466 } else {
1467 __ LoadConst32(TMP, low);
1468 __ Xor(dst_low, lhs_low, TMP);
1469 }
1470 if (IsUint<16>(high)) {
1471 if (dst_high != lhs_high || high != 0) {
1472 __ Xori(dst_high, lhs_high, high);
1473 }
1474 } else {
1475 if (high != low) {
1476 __ LoadConst32(TMP, high);
1477 }
1478 __ Xor(dst_high, lhs_high, TMP);
1479 }
1480 } else if (instruction->IsAnd()) {
1481 uint32_t low = Low32Bits(value);
1482 uint32_t high = High32Bits(value);
1483 if (IsUint<16>(low)) {
1484 __ Andi(dst_low, lhs_low, low);
1485 } else if (low != 0xFFFFFFFF) {
1486 __ LoadConst32(TMP, low);
1487 __ And(dst_low, lhs_low, TMP);
1488 } else if (dst_low != lhs_low) {
1489 __ Move(dst_low, lhs_low);
1490 }
1491 if (IsUint<16>(high)) {
1492 __ Andi(dst_high, lhs_high, high);
1493 } else if (high != 0xFFFFFFFF) {
1494 if (high != low) {
1495 __ LoadConst32(TMP, high);
1496 }
1497 __ And(dst_high, lhs_high, TMP);
1498 } else if (dst_high != lhs_high) {
1499 __ Move(dst_high, lhs_high);
1500 }
1501 } else {
1502 if (instruction->IsSub()) {
1503 value = -value;
1504 } else {
1505 DCHECK(instruction->IsAdd());
1506 }
1507 int32_t low = Low32Bits(value);
1508 int32_t high = High32Bits(value);
1509 if (IsInt<16>(low)) {
1510 if (dst_low != lhs_low || low != 0) {
1511 __ Addiu(dst_low, lhs_low, low);
1512 }
1513 if (low != 0) {
1514 __ Sltiu(AT, dst_low, low);
1515 }
1516 } else {
1517 __ LoadConst32(TMP, low);
1518 __ Addu(dst_low, lhs_low, TMP);
1519 __ Sltu(AT, dst_low, TMP);
1520 }
1521 if (IsInt<16>(high)) {
1522 if (dst_high != lhs_high || high != 0) {
1523 __ Addiu(dst_high, lhs_high, high);
1524 }
1525 } else {
1526 if (high != low) {
1527 __ LoadConst32(TMP, high);
1528 }
1529 __ Addu(dst_high, lhs_high, TMP);
1530 }
1531 if (low != 0) {
1532 __ Addu(dst_high, dst_high, AT);
1533 }
1534 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001535 }
1536 break;
1537 }
1538
1539 case Primitive::kPrimFloat:
1540 case Primitive::kPrimDouble: {
1541 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1542 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1543 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1544 if (instruction->IsAdd()) {
1545 if (type == Primitive::kPrimFloat) {
1546 __ AddS(dst, lhs, rhs);
1547 } else {
1548 __ AddD(dst, lhs, rhs);
1549 }
1550 } else {
1551 DCHECK(instruction->IsSub());
1552 if (type == Primitive::kPrimFloat) {
1553 __ SubS(dst, lhs, rhs);
1554 } else {
1555 __ SubD(dst, lhs, rhs);
1556 }
1557 }
1558 break;
1559 }
1560
1561 default:
1562 LOG(FATAL) << "Unexpected binary operation type " << type;
1563 }
1564}
1565
1566void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001567 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001568
1569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1570 Primitive::Type type = instr->GetResultType();
1571 switch (type) {
1572 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001573 locations->SetInAt(0, Location::RequiresRegister());
1574 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1575 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1576 break;
1577 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001578 locations->SetInAt(0, Location::RequiresRegister());
1579 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1580 locations->SetOut(Location::RequiresRegister());
1581 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001582 default:
1583 LOG(FATAL) << "Unexpected shift type " << type;
1584 }
1585}
1586
1587static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1588
1589void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001590 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001591 LocationSummary* locations = instr->GetLocations();
1592 Primitive::Type type = instr->GetType();
1593
1594 Location rhs_location = locations->InAt(1);
1595 bool use_imm = rhs_location.IsConstant();
1596 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1597 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001598 const uint32_t shift_mask =
1599 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001600 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001601 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1602 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603
1604 switch (type) {
1605 case Primitive::kPrimInt: {
1606 Register dst = locations->Out().AsRegister<Register>();
1607 Register lhs = locations->InAt(0).AsRegister<Register>();
1608 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001609 if (shift_value == 0) {
1610 if (dst != lhs) {
1611 __ Move(dst, lhs);
1612 }
1613 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001614 __ Sll(dst, lhs, shift_value);
1615 } else if (instr->IsShr()) {
1616 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001617 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001618 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001619 } else {
1620 if (has_ins_rotr) {
1621 __ Rotr(dst, lhs, shift_value);
1622 } else {
1623 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1624 __ Srl(dst, lhs, shift_value);
1625 __ Or(dst, dst, TMP);
1626 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001627 }
1628 } else {
1629 if (instr->IsShl()) {
1630 __ Sllv(dst, lhs, rhs_reg);
1631 } else if (instr->IsShr()) {
1632 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001633 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001634 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001635 } else {
1636 if (has_ins_rotr) {
1637 __ Rotrv(dst, lhs, rhs_reg);
1638 } else {
1639 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001640 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1641 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1642 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1643 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1644 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001645 __ Sllv(TMP, lhs, TMP);
1646 __ Srlv(dst, lhs, rhs_reg);
1647 __ Or(dst, dst, TMP);
1648 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001649 }
1650 }
1651 break;
1652 }
1653
1654 case Primitive::kPrimLong: {
1655 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1656 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1657 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1658 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1659 if (use_imm) {
1660 if (shift_value == 0) {
1661 codegen_->Move64(locations->Out(), locations->InAt(0));
1662 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001663 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001664 if (instr->IsShl()) {
1665 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1666 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1667 __ Sll(dst_low, lhs_low, shift_value);
1668 } else if (instr->IsShr()) {
1669 __ Srl(dst_low, lhs_low, shift_value);
1670 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1671 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001672 } else if (instr->IsUShr()) {
1673 __ Srl(dst_low, lhs_low, shift_value);
1674 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1675 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001676 } else {
1677 __ Srl(dst_low, lhs_low, shift_value);
1678 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1679 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001680 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001681 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001682 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001683 if (instr->IsShl()) {
1684 __ Sll(dst_low, lhs_low, shift_value);
1685 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1686 __ Sll(dst_high, lhs_high, shift_value);
1687 __ Or(dst_high, dst_high, TMP);
1688 } else if (instr->IsShr()) {
1689 __ Sra(dst_high, lhs_high, shift_value);
1690 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1691 __ Srl(dst_low, lhs_low, shift_value);
1692 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001693 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001694 __ Srl(dst_high, lhs_high, shift_value);
1695 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1696 __ Srl(dst_low, lhs_low, shift_value);
1697 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001698 } else {
1699 __ Srl(TMP, lhs_low, shift_value);
1700 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1701 __ Or(dst_low, dst_low, TMP);
1702 __ Srl(TMP, lhs_high, shift_value);
1703 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1704 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001705 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001706 }
1707 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001708 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001709 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001710 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001711 __ Move(dst_low, ZERO);
1712 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001713 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001714 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001715 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001716 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001717 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001718 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001719 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001720 // 64-bit rotation by 32 is just a swap.
1721 __ Move(dst_low, lhs_high);
1722 __ Move(dst_high, lhs_low);
1723 } else {
1724 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001725 __ Srl(dst_low, lhs_high, shift_value_high);
1726 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1727 __ Srl(dst_high, lhs_low, shift_value_high);
1728 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001729 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001730 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1731 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001732 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001733 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1734 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001735 __ Or(dst_high, dst_high, TMP);
1736 }
1737 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001738 }
1739 }
1740 } else {
1741 MipsLabel done;
1742 if (instr->IsShl()) {
1743 __ Sllv(dst_low, lhs_low, rhs_reg);
1744 __ Nor(AT, ZERO, rhs_reg);
1745 __ Srl(TMP, lhs_low, 1);
1746 __ Srlv(TMP, TMP, AT);
1747 __ Sllv(dst_high, lhs_high, rhs_reg);
1748 __ Or(dst_high, dst_high, TMP);
1749 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1750 __ Beqz(TMP, &done);
1751 __ Move(dst_high, dst_low);
1752 __ Move(dst_low, ZERO);
1753 } else if (instr->IsShr()) {
1754 __ Srav(dst_high, lhs_high, rhs_reg);
1755 __ Nor(AT, ZERO, rhs_reg);
1756 __ Sll(TMP, lhs_high, 1);
1757 __ Sllv(TMP, TMP, AT);
1758 __ Srlv(dst_low, lhs_low, rhs_reg);
1759 __ Or(dst_low, dst_low, TMP);
1760 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1761 __ Beqz(TMP, &done);
1762 __ Move(dst_low, dst_high);
1763 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001764 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001765 __ Srlv(dst_high, lhs_high, rhs_reg);
1766 __ Nor(AT, ZERO, rhs_reg);
1767 __ Sll(TMP, lhs_high, 1);
1768 __ Sllv(TMP, TMP, AT);
1769 __ Srlv(dst_low, lhs_low, rhs_reg);
1770 __ Or(dst_low, dst_low, TMP);
1771 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1772 __ Beqz(TMP, &done);
1773 __ Move(dst_low, dst_high);
1774 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001775 } else {
1776 __ Nor(AT, ZERO, rhs_reg);
1777 __ Srlv(TMP, lhs_low, rhs_reg);
1778 __ Sll(dst_low, lhs_high, 1);
1779 __ Sllv(dst_low, dst_low, AT);
1780 __ Or(dst_low, dst_low, TMP);
1781 __ Srlv(TMP, lhs_high, rhs_reg);
1782 __ Sll(dst_high, lhs_low, 1);
1783 __ Sllv(dst_high, dst_high, AT);
1784 __ Or(dst_high, dst_high, TMP);
1785 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1786 __ Beqz(TMP, &done);
1787 __ Move(TMP, dst_high);
1788 __ Move(dst_high, dst_low);
1789 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001790 }
1791 __ Bind(&done);
1792 }
1793 break;
1794 }
1795
1796 default:
1797 LOG(FATAL) << "Unexpected shift operation type " << type;
1798 }
1799}
1800
1801void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1802 HandleBinaryOp(instruction);
1803}
1804
1805void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1806 HandleBinaryOp(instruction);
1807}
1808
1809void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1810 HandleBinaryOp(instruction);
1811}
1812
1813void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1814 HandleBinaryOp(instruction);
1815}
1816
1817void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1820 locations->SetInAt(0, Location::RequiresRegister());
1821 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1822 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1823 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1824 } else {
1825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1826 }
1827}
1828
1829void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1830 LocationSummary* locations = instruction->GetLocations();
1831 Register obj = locations->InAt(0).AsRegister<Register>();
1832 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001833 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001834
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001835 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001836 switch (type) {
1837 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001838 Register out = locations->Out().AsRegister<Register>();
1839 if (index.IsConstant()) {
1840 size_t offset =
1841 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1842 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1843 } else {
1844 __ Addu(TMP, obj, index.AsRegister<Register>());
1845 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1846 }
1847 break;
1848 }
1849
1850 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001851 Register out = locations->Out().AsRegister<Register>();
1852 if (index.IsConstant()) {
1853 size_t offset =
1854 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1855 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1856 } else {
1857 __ Addu(TMP, obj, index.AsRegister<Register>());
1858 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1859 }
1860 break;
1861 }
1862
1863 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001864 Register out = locations->Out().AsRegister<Register>();
1865 if (index.IsConstant()) {
1866 size_t offset =
1867 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1868 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1869 } else {
1870 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1871 __ Addu(TMP, obj, TMP);
1872 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1873 }
1874 break;
1875 }
1876
1877 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001878 Register out = locations->Out().AsRegister<Register>();
1879 if (index.IsConstant()) {
1880 size_t offset =
1881 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1882 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1883 } else {
1884 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1885 __ Addu(TMP, obj, TMP);
1886 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1887 }
1888 break;
1889 }
1890
1891 case Primitive::kPrimInt:
1892 case Primitive::kPrimNot: {
1893 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001894 Register out = locations->Out().AsRegister<Register>();
1895 if (index.IsConstant()) {
1896 size_t offset =
1897 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1898 __ LoadFromOffset(kLoadWord, out, obj, offset);
1899 } else {
1900 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1901 __ Addu(TMP, obj, TMP);
1902 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1903 }
1904 break;
1905 }
1906
1907 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001908 Register out = locations->Out().AsRegisterPairLow<Register>();
1909 if (index.IsConstant()) {
1910 size_t offset =
1911 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1912 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1913 } else {
1914 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1915 __ Addu(TMP, obj, TMP);
1916 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1917 }
1918 break;
1919 }
1920
1921 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001922 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1923 if (index.IsConstant()) {
1924 size_t offset =
1925 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1926 __ LoadSFromOffset(out, obj, offset);
1927 } else {
1928 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1929 __ Addu(TMP, obj, TMP);
1930 __ LoadSFromOffset(out, TMP, data_offset);
1931 }
1932 break;
1933 }
1934
1935 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001936 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1937 if (index.IsConstant()) {
1938 size_t offset =
1939 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1940 __ LoadDFromOffset(out, obj, offset);
1941 } else {
1942 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1943 __ Addu(TMP, obj, TMP);
1944 __ LoadDFromOffset(out, TMP, data_offset);
1945 }
1946 break;
1947 }
1948
1949 case Primitive::kPrimVoid:
1950 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1951 UNREACHABLE();
1952 }
1953 codegen_->MaybeRecordImplicitNullCheck(instruction);
1954}
1955
1956void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1957 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1958 locations->SetInAt(0, Location::RequiresRegister());
1959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1960}
1961
1962void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1963 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001964 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001965 Register obj = locations->InAt(0).AsRegister<Register>();
1966 Register out = locations->Out().AsRegister<Register>();
1967 __ LoadFromOffset(kLoadWord, out, obj, offset);
1968 codegen_->MaybeRecordImplicitNullCheck(instruction);
1969}
1970
1971void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001972 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1974 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001975 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001976 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977 InvokeRuntimeCallingConvention calling_convention;
1978 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1979 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1980 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1981 } else {
1982 locations->SetInAt(0, Location::RequiresRegister());
1983 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1984 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1985 locations->SetInAt(2, Location::RequiresFpuRegister());
1986 } else {
1987 locations->SetInAt(2, Location::RequiresRegister());
1988 }
1989 }
1990}
1991
1992void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1993 LocationSummary* locations = instruction->GetLocations();
1994 Register obj = locations->InAt(0).AsRegister<Register>();
1995 Location index = locations->InAt(1);
1996 Primitive::Type value_type = instruction->GetComponentType();
1997 bool needs_runtime_call = locations->WillCall();
1998 bool needs_write_barrier =
1999 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2000
2001 switch (value_type) {
2002 case Primitive::kPrimBoolean:
2003 case Primitive::kPrimByte: {
2004 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
2005 Register value = locations->InAt(2).AsRegister<Register>();
2006 if (index.IsConstant()) {
2007 size_t offset =
2008 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
2009 __ StoreToOffset(kStoreByte, value, obj, offset);
2010 } else {
2011 __ Addu(TMP, obj, index.AsRegister<Register>());
2012 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
2013 }
2014 break;
2015 }
2016
2017 case Primitive::kPrimShort:
2018 case Primitive::kPrimChar: {
2019 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
2020 Register value = locations->InAt(2).AsRegister<Register>();
2021 if (index.IsConstant()) {
2022 size_t offset =
2023 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2024 __ StoreToOffset(kStoreHalfword, value, obj, offset);
2025 } else {
2026 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
2027 __ Addu(TMP, obj, TMP);
2028 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
2029 }
2030 break;
2031 }
2032
2033 case Primitive::kPrimInt:
2034 case Primitive::kPrimNot: {
2035 if (!needs_runtime_call) {
2036 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2037 Register value = locations->InAt(2).AsRegister<Register>();
2038 if (index.IsConstant()) {
2039 size_t offset =
2040 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2041 __ StoreToOffset(kStoreWord, value, obj, offset);
2042 } else {
2043 DCHECK(index.IsRegister()) << index;
2044 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2045 __ Addu(TMP, obj, TMP);
2046 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
2047 }
2048 codegen_->MaybeRecordImplicitNullCheck(instruction);
2049 if (needs_write_barrier) {
2050 DCHECK_EQ(value_type, Primitive::kPrimNot);
2051 codegen_->MarkGCCard(obj, value);
2052 }
2053 } else {
2054 DCHECK_EQ(value_type, Primitive::kPrimNot);
2055 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
2056 instruction,
2057 instruction->GetDexPc(),
2058 nullptr,
2059 IsDirectEntrypoint(kQuickAputObject));
2060 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2061 }
2062 break;
2063 }
2064
2065 case Primitive::kPrimLong: {
2066 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2067 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2068 if (index.IsConstant()) {
2069 size_t offset =
2070 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2071 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
2072 } else {
2073 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2074 __ Addu(TMP, obj, TMP);
2075 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
2076 }
2077 break;
2078 }
2079
2080 case Primitive::kPrimFloat: {
2081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2082 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2083 DCHECK(locations->InAt(2).IsFpuRegister());
2084 if (index.IsConstant()) {
2085 size_t offset =
2086 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2087 __ StoreSToOffset(value, obj, offset);
2088 } else {
2089 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2090 __ Addu(TMP, obj, TMP);
2091 __ StoreSToOffset(value, TMP, data_offset);
2092 }
2093 break;
2094 }
2095
2096 case Primitive::kPrimDouble: {
2097 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2098 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2099 DCHECK(locations->InAt(2).IsFpuRegister());
2100 if (index.IsConstant()) {
2101 size_t offset =
2102 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2103 __ StoreDToOffset(value, obj, offset);
2104 } else {
2105 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2106 __ Addu(TMP, obj, TMP);
2107 __ StoreDToOffset(value, TMP, data_offset);
2108 }
2109 break;
2110 }
2111
2112 case Primitive::kPrimVoid:
2113 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2114 UNREACHABLE();
2115 }
2116
2117 // Ints and objects are handled in the switch.
2118 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
2119 codegen_->MaybeRecordImplicitNullCheck(instruction);
2120 }
2121}
2122
2123void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2124 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2125 ? LocationSummary::kCallOnSlowPath
2126 : LocationSummary::kNoCall;
2127 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2128 locations->SetInAt(0, Location::RequiresRegister());
2129 locations->SetInAt(1, Location::RequiresRegister());
2130 if (instruction->HasUses()) {
2131 locations->SetOut(Location::SameAsFirstInput());
2132 }
2133}
2134
2135void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2136 LocationSummary* locations = instruction->GetLocations();
2137 BoundsCheckSlowPathMIPS* slow_path =
2138 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2139 codegen_->AddSlowPath(slow_path);
2140
2141 Register index = locations->InAt(0).AsRegister<Register>();
2142 Register length = locations->InAt(1).AsRegister<Register>();
2143
2144 // length is limited by the maximum positive signed 32-bit integer.
2145 // Unsigned comparison of length and index checks for index < 0
2146 // and for length <= index simultaneously.
2147 __ Bgeu(index, length, slow_path->GetEntryLabel());
2148}
2149
2150void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2152 instruction,
2153 LocationSummary::kCallOnSlowPath);
2154 locations->SetInAt(0, Location::RequiresRegister());
2155 locations->SetInAt(1, Location::RequiresRegister());
2156 // Note that TypeCheckSlowPathMIPS uses this register too.
2157 locations->AddTemp(Location::RequiresRegister());
2158}
2159
2160void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2161 LocationSummary* locations = instruction->GetLocations();
2162 Register obj = locations->InAt(0).AsRegister<Register>();
2163 Register cls = locations->InAt(1).AsRegister<Register>();
2164 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2165
2166 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2167 codegen_->AddSlowPath(slow_path);
2168
2169 // TODO: avoid this check if we know obj is not null.
2170 __ Beqz(obj, slow_path->GetExitLabel());
2171 // Compare the class of `obj` with `cls`.
2172 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2173 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2174 __ Bind(slow_path->GetExitLabel());
2175}
2176
2177void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2178 LocationSummary* locations =
2179 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2180 locations->SetInAt(0, Location::RequiresRegister());
2181 if (check->HasUses()) {
2182 locations->SetOut(Location::SameAsFirstInput());
2183 }
2184}
2185
2186void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2187 // We assume the class is not null.
2188 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2189 check->GetLoadClass(),
2190 check,
2191 check->GetDexPc(),
2192 true);
2193 codegen_->AddSlowPath(slow_path);
2194 GenerateClassInitializationCheck(slow_path,
2195 check->GetLocations()->InAt(0).AsRegister<Register>());
2196}
2197
2198void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2199 Primitive::Type in_type = compare->InputAt(0)->GetType();
2200
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002201 LocationSummary* locations =
2202 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002203
2204 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002205 case Primitive::kPrimBoolean:
2206 case Primitive::kPrimByte:
2207 case Primitive::kPrimShort:
2208 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002209 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002210 case Primitive::kPrimLong:
2211 locations->SetInAt(0, Location::RequiresRegister());
2212 locations->SetInAt(1, Location::RequiresRegister());
2213 // Output overlaps because it is written before doing the low comparison.
2214 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2215 break;
2216
2217 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002218 case Primitive::kPrimDouble:
2219 locations->SetInAt(0, Location::RequiresFpuRegister());
2220 locations->SetInAt(1, Location::RequiresFpuRegister());
2221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002222 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002223
2224 default:
2225 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2226 }
2227}
2228
2229void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2230 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002231 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002232 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002233 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002234
2235 // 0 if: left == right
2236 // 1 if: left > right
2237 // -1 if: left < right
2238 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002239 case Primitive::kPrimBoolean:
2240 case Primitive::kPrimByte:
2241 case Primitive::kPrimShort:
2242 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002243 case Primitive::kPrimInt: {
2244 Register lhs = locations->InAt(0).AsRegister<Register>();
2245 Register rhs = locations->InAt(1).AsRegister<Register>();
2246 __ Slt(TMP, lhs, rhs);
2247 __ Slt(res, rhs, lhs);
2248 __ Subu(res, res, TMP);
2249 break;
2250 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002251 case Primitive::kPrimLong: {
2252 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002253 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2254 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2255 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2256 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2257 // TODO: more efficient (direct) comparison with a constant.
2258 __ Slt(TMP, lhs_high, rhs_high);
2259 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2260 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2261 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2262 __ Sltu(TMP, lhs_low, rhs_low);
2263 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2264 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2265 __ Bind(&done);
2266 break;
2267 }
2268
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002269 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002270 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002271 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2272 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2273 MipsLabel done;
2274 if (isR6) {
2275 __ CmpEqS(FTMP, lhs, rhs);
2276 __ LoadConst32(res, 0);
2277 __ Bc1nez(FTMP, &done);
2278 if (gt_bias) {
2279 __ CmpLtS(FTMP, lhs, rhs);
2280 __ LoadConst32(res, -1);
2281 __ Bc1nez(FTMP, &done);
2282 __ LoadConst32(res, 1);
2283 } else {
2284 __ CmpLtS(FTMP, rhs, lhs);
2285 __ LoadConst32(res, 1);
2286 __ Bc1nez(FTMP, &done);
2287 __ LoadConst32(res, -1);
2288 }
2289 } else {
2290 if (gt_bias) {
2291 __ ColtS(0, lhs, rhs);
2292 __ LoadConst32(res, -1);
2293 __ Bc1t(0, &done);
2294 __ CeqS(0, lhs, rhs);
2295 __ LoadConst32(res, 1);
2296 __ Movt(res, ZERO, 0);
2297 } else {
2298 __ ColtS(0, rhs, lhs);
2299 __ LoadConst32(res, 1);
2300 __ Bc1t(0, &done);
2301 __ CeqS(0, lhs, rhs);
2302 __ LoadConst32(res, -1);
2303 __ Movt(res, ZERO, 0);
2304 }
2305 }
2306 __ Bind(&done);
2307 break;
2308 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002309 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002310 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002311 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2312 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2313 MipsLabel done;
2314 if (isR6) {
2315 __ CmpEqD(FTMP, lhs, rhs);
2316 __ LoadConst32(res, 0);
2317 __ Bc1nez(FTMP, &done);
2318 if (gt_bias) {
2319 __ CmpLtD(FTMP, lhs, rhs);
2320 __ LoadConst32(res, -1);
2321 __ Bc1nez(FTMP, &done);
2322 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002323 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002324 __ CmpLtD(FTMP, rhs, lhs);
2325 __ LoadConst32(res, 1);
2326 __ Bc1nez(FTMP, &done);
2327 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002328 }
2329 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002330 if (gt_bias) {
2331 __ ColtD(0, lhs, rhs);
2332 __ LoadConst32(res, -1);
2333 __ Bc1t(0, &done);
2334 __ CeqD(0, lhs, rhs);
2335 __ LoadConst32(res, 1);
2336 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002338 __ ColtD(0, rhs, lhs);
2339 __ LoadConst32(res, 1);
2340 __ Bc1t(0, &done);
2341 __ CeqD(0, lhs, rhs);
2342 __ LoadConst32(res, -1);
2343 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002344 }
2345 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002346 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002347 break;
2348 }
2349
2350 default:
2351 LOG(FATAL) << "Unimplemented compare type " << in_type;
2352 }
2353}
2354
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002355void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002357 switch (instruction->InputAt(0)->GetType()) {
2358 default:
2359 case Primitive::kPrimLong:
2360 locations->SetInAt(0, Location::RequiresRegister());
2361 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2362 break;
2363
2364 case Primitive::kPrimFloat:
2365 case Primitive::kPrimDouble:
2366 locations->SetInAt(0, Location::RequiresFpuRegister());
2367 locations->SetInAt(1, Location::RequiresFpuRegister());
2368 break;
2369 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002370 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2372 }
2373}
2374
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002375void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002376 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002377 return;
2378 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002379
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002380 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002381 LocationSummary* locations = instruction->GetLocations();
2382 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002383 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002384
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002385 switch (type) {
2386 default:
2387 // Integer case.
2388 GenerateIntCompare(instruction->GetCondition(), locations);
2389 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002390
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002391 case Primitive::kPrimLong:
2392 // TODO: don't use branches.
2393 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002394 break;
2395
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002396 case Primitive::kPrimFloat:
2397 case Primitive::kPrimDouble:
2398 // TODO: don't use branches.
2399 GenerateFpCompareAndBranch(instruction->GetCondition(),
2400 instruction->IsGtBias(),
2401 type,
2402 locations,
2403 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002404 break;
2405 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002406
2407 // Convert the branches into the result.
2408 MipsLabel done;
2409
2410 // False case: result = 0.
2411 __ LoadConst32(dst, 0);
2412 __ B(&done);
2413
2414 // True case: result = 1.
2415 __ Bind(&true_label);
2416 __ LoadConst32(dst, 1);
2417 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418}
2419
Alexey Frunze7e99e052015-11-24 19:28:01 -08002420void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2421 DCHECK(instruction->IsDiv() || instruction->IsRem());
2422 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2423
2424 LocationSummary* locations = instruction->GetLocations();
2425 Location second = locations->InAt(1);
2426 DCHECK(second.IsConstant());
2427
2428 Register out = locations->Out().AsRegister<Register>();
2429 Register dividend = locations->InAt(0).AsRegister<Register>();
2430 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2431 DCHECK(imm == 1 || imm == -1);
2432
2433 if (instruction->IsRem()) {
2434 __ Move(out, ZERO);
2435 } else {
2436 if (imm == -1) {
2437 __ Subu(out, ZERO, dividend);
2438 } else if (out != dividend) {
2439 __ Move(out, dividend);
2440 }
2441 }
2442}
2443
2444void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2445 DCHECK(instruction->IsDiv() || instruction->IsRem());
2446 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2447
2448 LocationSummary* locations = instruction->GetLocations();
2449 Location second = locations->InAt(1);
2450 DCHECK(second.IsConstant());
2451
2452 Register out = locations->Out().AsRegister<Register>();
2453 Register dividend = locations->InAt(0).AsRegister<Register>();
2454 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002455 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002456 int ctz_imm = CTZ(abs_imm);
2457
2458 if (instruction->IsDiv()) {
2459 if (ctz_imm == 1) {
2460 // Fast path for division by +/-2, which is very common.
2461 __ Srl(TMP, dividend, 31);
2462 } else {
2463 __ Sra(TMP, dividend, 31);
2464 __ Srl(TMP, TMP, 32 - ctz_imm);
2465 }
2466 __ Addu(out, dividend, TMP);
2467 __ Sra(out, out, ctz_imm);
2468 if (imm < 0) {
2469 __ Subu(out, ZERO, out);
2470 }
2471 } else {
2472 if (ctz_imm == 1) {
2473 // Fast path for modulo +/-2, which is very common.
2474 __ Sra(TMP, dividend, 31);
2475 __ Subu(out, dividend, TMP);
2476 __ Andi(out, out, 1);
2477 __ Addu(out, out, TMP);
2478 } else {
2479 __ Sra(TMP, dividend, 31);
2480 __ Srl(TMP, TMP, 32 - ctz_imm);
2481 __ Addu(out, dividend, TMP);
2482 if (IsUint<16>(abs_imm - 1)) {
2483 __ Andi(out, out, abs_imm - 1);
2484 } else {
2485 __ Sll(out, out, 32 - ctz_imm);
2486 __ Srl(out, out, 32 - ctz_imm);
2487 }
2488 __ Subu(out, out, TMP);
2489 }
2490 }
2491}
2492
2493void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2494 DCHECK(instruction->IsDiv() || instruction->IsRem());
2495 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2496
2497 LocationSummary* locations = instruction->GetLocations();
2498 Location second = locations->InAt(1);
2499 DCHECK(second.IsConstant());
2500
2501 Register out = locations->Out().AsRegister<Register>();
2502 Register dividend = locations->InAt(0).AsRegister<Register>();
2503 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2504
2505 int64_t magic;
2506 int shift;
2507 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2508
2509 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2510
2511 __ LoadConst32(TMP, magic);
2512 if (isR6) {
2513 __ MuhR6(TMP, dividend, TMP);
2514 } else {
2515 __ MultR2(dividend, TMP);
2516 __ Mfhi(TMP);
2517 }
2518 if (imm > 0 && magic < 0) {
2519 __ Addu(TMP, TMP, dividend);
2520 } else if (imm < 0 && magic > 0) {
2521 __ Subu(TMP, TMP, dividend);
2522 }
2523
2524 if (shift != 0) {
2525 __ Sra(TMP, TMP, shift);
2526 }
2527
2528 if (instruction->IsDiv()) {
2529 __ Sra(out, TMP, 31);
2530 __ Subu(out, TMP, out);
2531 } else {
2532 __ Sra(AT, TMP, 31);
2533 __ Subu(AT, TMP, AT);
2534 __ LoadConst32(TMP, imm);
2535 if (isR6) {
2536 __ MulR6(TMP, AT, TMP);
2537 } else {
2538 __ MulR2(TMP, AT, TMP);
2539 }
2540 __ Subu(out, dividend, TMP);
2541 }
2542}
2543
2544void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2545 DCHECK(instruction->IsDiv() || instruction->IsRem());
2546 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2547
2548 LocationSummary* locations = instruction->GetLocations();
2549 Register out = locations->Out().AsRegister<Register>();
2550 Location second = locations->InAt(1);
2551
2552 if (second.IsConstant()) {
2553 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2554 if (imm == 0) {
2555 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2556 } else if (imm == 1 || imm == -1) {
2557 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002558 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002559 DivRemByPowerOfTwo(instruction);
2560 } else {
2561 DCHECK(imm <= -2 || imm >= 2);
2562 GenerateDivRemWithAnyConstant(instruction);
2563 }
2564 } else {
2565 Register dividend = locations->InAt(0).AsRegister<Register>();
2566 Register divisor = second.AsRegister<Register>();
2567 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2568 if (instruction->IsDiv()) {
2569 if (isR6) {
2570 __ DivR6(out, dividend, divisor);
2571 } else {
2572 __ DivR2(out, dividend, divisor);
2573 }
2574 } else {
2575 if (isR6) {
2576 __ ModR6(out, dividend, divisor);
2577 } else {
2578 __ ModR2(out, dividend, divisor);
2579 }
2580 }
2581 }
2582}
2583
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002584void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2585 Primitive::Type type = div->GetResultType();
2586 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002587 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002588 : LocationSummary::kNoCall;
2589
2590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2591
2592 switch (type) {
2593 case Primitive::kPrimInt:
2594 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002595 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2597 break;
2598
2599 case Primitive::kPrimLong: {
2600 InvokeRuntimeCallingConvention calling_convention;
2601 locations->SetInAt(0, Location::RegisterPairLocation(
2602 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2603 locations->SetInAt(1, Location::RegisterPairLocation(
2604 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2605 locations->SetOut(calling_convention.GetReturnLocation(type));
2606 break;
2607 }
2608
2609 case Primitive::kPrimFloat:
2610 case Primitive::kPrimDouble:
2611 locations->SetInAt(0, Location::RequiresFpuRegister());
2612 locations->SetInAt(1, Location::RequiresFpuRegister());
2613 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2614 break;
2615
2616 default:
2617 LOG(FATAL) << "Unexpected div type " << type;
2618 }
2619}
2620
2621void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2622 Primitive::Type type = instruction->GetType();
2623 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624
2625 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002626 case Primitive::kPrimInt:
2627 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002629 case Primitive::kPrimLong: {
2630 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2631 instruction,
2632 instruction->GetDexPc(),
2633 nullptr,
2634 IsDirectEntrypoint(kQuickLdiv));
2635 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2636 break;
2637 }
2638 case Primitive::kPrimFloat:
2639 case Primitive::kPrimDouble: {
2640 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2641 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2642 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2643 if (type == Primitive::kPrimFloat) {
2644 __ DivS(dst, lhs, rhs);
2645 } else {
2646 __ DivD(dst, lhs, rhs);
2647 }
2648 break;
2649 }
2650 default:
2651 LOG(FATAL) << "Unexpected div type " << type;
2652 }
2653}
2654
2655void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2656 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2657 ? LocationSummary::kCallOnSlowPath
2658 : LocationSummary::kNoCall;
2659 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2660 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2661 if (instruction->HasUses()) {
2662 locations->SetOut(Location::SameAsFirstInput());
2663 }
2664}
2665
2666void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2667 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2668 codegen_->AddSlowPath(slow_path);
2669 Location value = instruction->GetLocations()->InAt(0);
2670 Primitive::Type type = instruction->GetType();
2671
2672 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002673 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002674 case Primitive::kPrimByte:
2675 case Primitive::kPrimChar:
2676 case Primitive::kPrimShort:
2677 case Primitive::kPrimInt: {
2678 if (value.IsConstant()) {
2679 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2680 __ B(slow_path->GetEntryLabel());
2681 } else {
2682 // A division by a non-null constant is valid. We don't need to perform
2683 // any check, so simply fall through.
2684 }
2685 } else {
2686 DCHECK(value.IsRegister()) << value;
2687 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2688 }
2689 break;
2690 }
2691 case Primitive::kPrimLong: {
2692 if (value.IsConstant()) {
2693 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2694 __ B(slow_path->GetEntryLabel());
2695 } else {
2696 // A division by a non-null constant is valid. We don't need to perform
2697 // any check, so simply fall through.
2698 }
2699 } else {
2700 DCHECK(value.IsRegisterPair()) << value;
2701 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2702 __ Beqz(TMP, slow_path->GetEntryLabel());
2703 }
2704 break;
2705 }
2706 default:
2707 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2708 }
2709}
2710
2711void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2712 LocationSummary* locations =
2713 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2714 locations->SetOut(Location::ConstantLocation(constant));
2715}
2716
2717void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2718 // Will be generated at use site.
2719}
2720
2721void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2722 exit->SetLocations(nullptr);
2723}
2724
2725void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2726}
2727
2728void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2729 LocationSummary* locations =
2730 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2731 locations->SetOut(Location::ConstantLocation(constant));
2732}
2733
2734void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2735 // Will be generated at use site.
2736}
2737
2738void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2739 got->SetLocations(nullptr);
2740}
2741
2742void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2743 DCHECK(!successor->IsExitBlock());
2744 HBasicBlock* block = got->GetBlock();
2745 HInstruction* previous = got->GetPrevious();
2746 HLoopInformation* info = block->GetLoopInformation();
2747
2748 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2749 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2750 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2751 return;
2752 }
2753 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2754 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2755 }
2756 if (!codegen_->GoesToNextBlock(block, successor)) {
2757 __ B(codegen_->GetLabelOf(successor));
2758 }
2759}
2760
2761void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2762 HandleGoto(got, got->GetSuccessor());
2763}
2764
2765void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2766 try_boundary->SetLocations(nullptr);
2767}
2768
2769void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2770 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2771 if (!successor->IsExitBlock()) {
2772 HandleGoto(try_boundary, successor);
2773 }
2774}
2775
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002776void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2777 LocationSummary* locations) {
2778 Register dst = locations->Out().AsRegister<Register>();
2779 Register lhs = locations->InAt(0).AsRegister<Register>();
2780 Location rhs_location = locations->InAt(1);
2781 Register rhs_reg = ZERO;
2782 int64_t rhs_imm = 0;
2783 bool use_imm = rhs_location.IsConstant();
2784 if (use_imm) {
2785 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2786 } else {
2787 rhs_reg = rhs_location.AsRegister<Register>();
2788 }
2789
2790 switch (cond) {
2791 case kCondEQ:
2792 case kCondNE:
2793 if (use_imm && IsUint<16>(rhs_imm)) {
2794 __ Xori(dst, lhs, rhs_imm);
2795 } else {
2796 if (use_imm) {
2797 rhs_reg = TMP;
2798 __ LoadConst32(rhs_reg, rhs_imm);
2799 }
2800 __ Xor(dst, lhs, rhs_reg);
2801 }
2802 if (cond == kCondEQ) {
2803 __ Sltiu(dst, dst, 1);
2804 } else {
2805 __ Sltu(dst, ZERO, dst);
2806 }
2807 break;
2808
2809 case kCondLT:
2810 case kCondGE:
2811 if (use_imm && IsInt<16>(rhs_imm)) {
2812 __ Slti(dst, lhs, rhs_imm);
2813 } else {
2814 if (use_imm) {
2815 rhs_reg = TMP;
2816 __ LoadConst32(rhs_reg, rhs_imm);
2817 }
2818 __ Slt(dst, lhs, rhs_reg);
2819 }
2820 if (cond == kCondGE) {
2821 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2822 // only the slt instruction but no sge.
2823 __ Xori(dst, dst, 1);
2824 }
2825 break;
2826
2827 case kCondLE:
2828 case kCondGT:
2829 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2830 // Simulate lhs <= rhs via lhs < rhs + 1.
2831 __ Slti(dst, lhs, rhs_imm + 1);
2832 if (cond == kCondGT) {
2833 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2834 // only the slti instruction but no sgti.
2835 __ Xori(dst, dst, 1);
2836 }
2837 } else {
2838 if (use_imm) {
2839 rhs_reg = TMP;
2840 __ LoadConst32(rhs_reg, rhs_imm);
2841 }
2842 __ Slt(dst, rhs_reg, lhs);
2843 if (cond == kCondLE) {
2844 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2845 // only the slt instruction but no sle.
2846 __ Xori(dst, dst, 1);
2847 }
2848 }
2849 break;
2850
2851 case kCondB:
2852 case kCondAE:
2853 if (use_imm && IsInt<16>(rhs_imm)) {
2854 // Sltiu sign-extends its 16-bit immediate operand before
2855 // the comparison and thus lets us compare directly with
2856 // unsigned values in the ranges [0, 0x7fff] and
2857 // [0xffff8000, 0xffffffff].
2858 __ Sltiu(dst, lhs, rhs_imm);
2859 } else {
2860 if (use_imm) {
2861 rhs_reg = TMP;
2862 __ LoadConst32(rhs_reg, rhs_imm);
2863 }
2864 __ Sltu(dst, lhs, rhs_reg);
2865 }
2866 if (cond == kCondAE) {
2867 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2868 // only the sltu instruction but no sgeu.
2869 __ Xori(dst, dst, 1);
2870 }
2871 break;
2872
2873 case kCondBE:
2874 case kCondA:
2875 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2876 // Simulate lhs <= rhs via lhs < rhs + 1.
2877 // Note that this only works if rhs + 1 does not overflow
2878 // to 0, hence the check above.
2879 // Sltiu sign-extends its 16-bit immediate operand before
2880 // the comparison and thus lets us compare directly with
2881 // unsigned values in the ranges [0, 0x7fff] and
2882 // [0xffff8000, 0xffffffff].
2883 __ Sltiu(dst, lhs, rhs_imm + 1);
2884 if (cond == kCondA) {
2885 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2886 // only the sltiu instruction but no sgtiu.
2887 __ Xori(dst, dst, 1);
2888 }
2889 } else {
2890 if (use_imm) {
2891 rhs_reg = TMP;
2892 __ LoadConst32(rhs_reg, rhs_imm);
2893 }
2894 __ Sltu(dst, rhs_reg, lhs);
2895 if (cond == kCondBE) {
2896 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2897 // only the sltu instruction but no sleu.
2898 __ Xori(dst, dst, 1);
2899 }
2900 }
2901 break;
2902 }
2903}
2904
2905void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2906 LocationSummary* locations,
2907 MipsLabel* label) {
2908 Register lhs = locations->InAt(0).AsRegister<Register>();
2909 Location rhs_location = locations->InAt(1);
2910 Register rhs_reg = ZERO;
2911 int32_t rhs_imm = 0;
2912 bool use_imm = rhs_location.IsConstant();
2913 if (use_imm) {
2914 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2915 } else {
2916 rhs_reg = rhs_location.AsRegister<Register>();
2917 }
2918
2919 if (use_imm && rhs_imm == 0) {
2920 switch (cond) {
2921 case kCondEQ:
2922 case kCondBE: // <= 0 if zero
2923 __ Beqz(lhs, label);
2924 break;
2925 case kCondNE:
2926 case kCondA: // > 0 if non-zero
2927 __ Bnez(lhs, label);
2928 break;
2929 case kCondLT:
2930 __ Bltz(lhs, label);
2931 break;
2932 case kCondGE:
2933 __ Bgez(lhs, label);
2934 break;
2935 case kCondLE:
2936 __ Blez(lhs, label);
2937 break;
2938 case kCondGT:
2939 __ Bgtz(lhs, label);
2940 break;
2941 case kCondB: // always false
2942 break;
2943 case kCondAE: // always true
2944 __ B(label);
2945 break;
2946 }
2947 } else {
2948 if (use_imm) {
2949 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2950 rhs_reg = TMP;
2951 __ LoadConst32(rhs_reg, rhs_imm);
2952 }
2953 switch (cond) {
2954 case kCondEQ:
2955 __ Beq(lhs, rhs_reg, label);
2956 break;
2957 case kCondNE:
2958 __ Bne(lhs, rhs_reg, label);
2959 break;
2960 case kCondLT:
2961 __ Blt(lhs, rhs_reg, label);
2962 break;
2963 case kCondGE:
2964 __ Bge(lhs, rhs_reg, label);
2965 break;
2966 case kCondLE:
2967 __ Bge(rhs_reg, lhs, label);
2968 break;
2969 case kCondGT:
2970 __ Blt(rhs_reg, lhs, label);
2971 break;
2972 case kCondB:
2973 __ Bltu(lhs, rhs_reg, label);
2974 break;
2975 case kCondAE:
2976 __ Bgeu(lhs, rhs_reg, label);
2977 break;
2978 case kCondBE:
2979 __ Bgeu(rhs_reg, lhs, label);
2980 break;
2981 case kCondA:
2982 __ Bltu(rhs_reg, lhs, label);
2983 break;
2984 }
2985 }
2986}
2987
2988void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2989 LocationSummary* locations,
2990 MipsLabel* label) {
2991 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2992 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2993 Location rhs_location = locations->InAt(1);
2994 Register rhs_high = ZERO;
2995 Register rhs_low = ZERO;
2996 int64_t imm = 0;
2997 uint32_t imm_high = 0;
2998 uint32_t imm_low = 0;
2999 bool use_imm = rhs_location.IsConstant();
3000 if (use_imm) {
3001 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3002 imm_high = High32Bits(imm);
3003 imm_low = Low32Bits(imm);
3004 } else {
3005 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3006 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3007 }
3008
3009 if (use_imm && imm == 0) {
3010 switch (cond) {
3011 case kCondEQ:
3012 case kCondBE: // <= 0 if zero
3013 __ Or(TMP, lhs_high, lhs_low);
3014 __ Beqz(TMP, label);
3015 break;
3016 case kCondNE:
3017 case kCondA: // > 0 if non-zero
3018 __ Or(TMP, lhs_high, lhs_low);
3019 __ Bnez(TMP, label);
3020 break;
3021 case kCondLT:
3022 __ Bltz(lhs_high, label);
3023 break;
3024 case kCondGE:
3025 __ Bgez(lhs_high, label);
3026 break;
3027 case kCondLE:
3028 __ Or(TMP, lhs_high, lhs_low);
3029 __ Sra(AT, lhs_high, 31);
3030 __ Bgeu(AT, TMP, label);
3031 break;
3032 case kCondGT:
3033 __ Or(TMP, lhs_high, lhs_low);
3034 __ Sra(AT, lhs_high, 31);
3035 __ Bltu(AT, TMP, label);
3036 break;
3037 case kCondB: // always false
3038 break;
3039 case kCondAE: // always true
3040 __ B(label);
3041 break;
3042 }
3043 } else if (use_imm) {
3044 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3045 switch (cond) {
3046 case kCondEQ:
3047 __ LoadConst32(TMP, imm_high);
3048 __ Xor(TMP, TMP, lhs_high);
3049 __ LoadConst32(AT, imm_low);
3050 __ Xor(AT, AT, lhs_low);
3051 __ Or(TMP, TMP, AT);
3052 __ Beqz(TMP, label);
3053 break;
3054 case kCondNE:
3055 __ LoadConst32(TMP, imm_high);
3056 __ Xor(TMP, TMP, lhs_high);
3057 __ LoadConst32(AT, imm_low);
3058 __ Xor(AT, AT, lhs_low);
3059 __ Or(TMP, TMP, AT);
3060 __ Bnez(TMP, label);
3061 break;
3062 case kCondLT:
3063 __ LoadConst32(TMP, imm_high);
3064 __ Blt(lhs_high, TMP, label);
3065 __ Slt(TMP, TMP, lhs_high);
3066 __ LoadConst32(AT, imm_low);
3067 __ Sltu(AT, lhs_low, AT);
3068 __ Blt(TMP, AT, label);
3069 break;
3070 case kCondGE:
3071 __ LoadConst32(TMP, imm_high);
3072 __ Blt(TMP, lhs_high, label);
3073 __ Slt(TMP, lhs_high, TMP);
3074 __ LoadConst32(AT, imm_low);
3075 __ Sltu(AT, lhs_low, AT);
3076 __ Or(TMP, TMP, AT);
3077 __ Beqz(TMP, label);
3078 break;
3079 case kCondLE:
3080 __ LoadConst32(TMP, imm_high);
3081 __ Blt(lhs_high, TMP, label);
3082 __ Slt(TMP, TMP, lhs_high);
3083 __ LoadConst32(AT, imm_low);
3084 __ Sltu(AT, AT, lhs_low);
3085 __ Or(TMP, TMP, AT);
3086 __ Beqz(TMP, label);
3087 break;
3088 case kCondGT:
3089 __ LoadConst32(TMP, imm_high);
3090 __ Blt(TMP, lhs_high, label);
3091 __ Slt(TMP, lhs_high, TMP);
3092 __ LoadConst32(AT, imm_low);
3093 __ Sltu(AT, AT, lhs_low);
3094 __ Blt(TMP, AT, label);
3095 break;
3096 case kCondB:
3097 __ LoadConst32(TMP, imm_high);
3098 __ Bltu(lhs_high, TMP, label);
3099 __ Sltu(TMP, TMP, lhs_high);
3100 __ LoadConst32(AT, imm_low);
3101 __ Sltu(AT, lhs_low, AT);
3102 __ Blt(TMP, AT, label);
3103 break;
3104 case kCondAE:
3105 __ LoadConst32(TMP, imm_high);
3106 __ Bltu(TMP, lhs_high, label);
3107 __ Sltu(TMP, lhs_high, TMP);
3108 __ LoadConst32(AT, imm_low);
3109 __ Sltu(AT, lhs_low, AT);
3110 __ Or(TMP, TMP, AT);
3111 __ Beqz(TMP, label);
3112 break;
3113 case kCondBE:
3114 __ LoadConst32(TMP, imm_high);
3115 __ Bltu(lhs_high, TMP, label);
3116 __ Sltu(TMP, TMP, lhs_high);
3117 __ LoadConst32(AT, imm_low);
3118 __ Sltu(AT, AT, lhs_low);
3119 __ Or(TMP, TMP, AT);
3120 __ Beqz(TMP, label);
3121 break;
3122 case kCondA:
3123 __ LoadConst32(TMP, imm_high);
3124 __ Bltu(TMP, lhs_high, label);
3125 __ Sltu(TMP, lhs_high, TMP);
3126 __ LoadConst32(AT, imm_low);
3127 __ Sltu(AT, AT, lhs_low);
3128 __ Blt(TMP, AT, label);
3129 break;
3130 }
3131 } else {
3132 switch (cond) {
3133 case kCondEQ:
3134 __ Xor(TMP, lhs_high, rhs_high);
3135 __ Xor(AT, lhs_low, rhs_low);
3136 __ Or(TMP, TMP, AT);
3137 __ Beqz(TMP, label);
3138 break;
3139 case kCondNE:
3140 __ Xor(TMP, lhs_high, rhs_high);
3141 __ Xor(AT, lhs_low, rhs_low);
3142 __ Or(TMP, TMP, AT);
3143 __ Bnez(TMP, label);
3144 break;
3145 case kCondLT:
3146 __ Blt(lhs_high, rhs_high, label);
3147 __ Slt(TMP, rhs_high, lhs_high);
3148 __ Sltu(AT, lhs_low, rhs_low);
3149 __ Blt(TMP, AT, label);
3150 break;
3151 case kCondGE:
3152 __ Blt(rhs_high, lhs_high, label);
3153 __ Slt(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 kCondLE:
3159 __ Blt(lhs_high, rhs_high, label);
3160 __ Slt(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 kCondGT:
3166 __ Blt(rhs_high, lhs_high, label);
3167 __ Slt(TMP, lhs_high, rhs_high);
3168 __ Sltu(AT, rhs_low, lhs_low);
3169 __ Blt(TMP, AT, label);
3170 break;
3171 case kCondB:
3172 __ Bltu(lhs_high, rhs_high, label);
3173 __ Sltu(TMP, rhs_high, lhs_high);
3174 __ Sltu(AT, lhs_low, rhs_low);
3175 __ Blt(TMP, AT, label);
3176 break;
3177 case kCondAE:
3178 __ Bltu(rhs_high, lhs_high, label);
3179 __ Sltu(TMP, lhs_high, rhs_high);
3180 __ Sltu(AT, lhs_low, rhs_low);
3181 __ Or(TMP, TMP, AT);
3182 __ Beqz(TMP, label);
3183 break;
3184 case kCondBE:
3185 __ Bltu(lhs_high, rhs_high, label);
3186 __ Sltu(TMP, rhs_high, lhs_high);
3187 __ Sltu(AT, rhs_low, lhs_low);
3188 __ Or(TMP, TMP, AT);
3189 __ Beqz(TMP, label);
3190 break;
3191 case kCondA:
3192 __ Bltu(rhs_high, lhs_high, label);
3193 __ Sltu(TMP, lhs_high, rhs_high);
3194 __ Sltu(AT, rhs_low, lhs_low);
3195 __ Blt(TMP, AT, label);
3196 break;
3197 }
3198 }
3199}
3200
3201void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3202 bool gt_bias,
3203 Primitive::Type type,
3204 LocationSummary* locations,
3205 MipsLabel* label) {
3206 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3207 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3208 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3209 if (type == Primitive::kPrimFloat) {
3210 if (isR6) {
3211 switch (cond) {
3212 case kCondEQ:
3213 __ CmpEqS(FTMP, lhs, rhs);
3214 __ Bc1nez(FTMP, label);
3215 break;
3216 case kCondNE:
3217 __ CmpEqS(FTMP, lhs, rhs);
3218 __ Bc1eqz(FTMP, label);
3219 break;
3220 case kCondLT:
3221 if (gt_bias) {
3222 __ CmpLtS(FTMP, lhs, rhs);
3223 } else {
3224 __ CmpUltS(FTMP, lhs, rhs);
3225 }
3226 __ Bc1nez(FTMP, label);
3227 break;
3228 case kCondLE:
3229 if (gt_bias) {
3230 __ CmpLeS(FTMP, lhs, rhs);
3231 } else {
3232 __ CmpUleS(FTMP, lhs, rhs);
3233 }
3234 __ Bc1nez(FTMP, label);
3235 break;
3236 case kCondGT:
3237 if (gt_bias) {
3238 __ CmpUltS(FTMP, rhs, lhs);
3239 } else {
3240 __ CmpLtS(FTMP, rhs, lhs);
3241 }
3242 __ Bc1nez(FTMP, label);
3243 break;
3244 case kCondGE:
3245 if (gt_bias) {
3246 __ CmpUleS(FTMP, rhs, lhs);
3247 } else {
3248 __ CmpLeS(FTMP, rhs, lhs);
3249 }
3250 __ Bc1nez(FTMP, label);
3251 break;
3252 default:
3253 LOG(FATAL) << "Unexpected non-floating-point condition";
3254 }
3255 } else {
3256 switch (cond) {
3257 case kCondEQ:
3258 __ CeqS(0, lhs, rhs);
3259 __ Bc1t(0, label);
3260 break;
3261 case kCondNE:
3262 __ CeqS(0, lhs, rhs);
3263 __ Bc1f(0, label);
3264 break;
3265 case kCondLT:
3266 if (gt_bias) {
3267 __ ColtS(0, lhs, rhs);
3268 } else {
3269 __ CultS(0, lhs, rhs);
3270 }
3271 __ Bc1t(0, label);
3272 break;
3273 case kCondLE:
3274 if (gt_bias) {
3275 __ ColeS(0, lhs, rhs);
3276 } else {
3277 __ CuleS(0, lhs, rhs);
3278 }
3279 __ Bc1t(0, label);
3280 break;
3281 case kCondGT:
3282 if (gt_bias) {
3283 __ CultS(0, rhs, lhs);
3284 } else {
3285 __ ColtS(0, rhs, lhs);
3286 }
3287 __ Bc1t(0, label);
3288 break;
3289 case kCondGE:
3290 if (gt_bias) {
3291 __ CuleS(0, rhs, lhs);
3292 } else {
3293 __ ColeS(0, rhs, lhs);
3294 }
3295 __ Bc1t(0, label);
3296 break;
3297 default:
3298 LOG(FATAL) << "Unexpected non-floating-point condition";
3299 }
3300 }
3301 } else {
3302 DCHECK_EQ(type, Primitive::kPrimDouble);
3303 if (isR6) {
3304 switch (cond) {
3305 case kCondEQ:
3306 __ CmpEqD(FTMP, lhs, rhs);
3307 __ Bc1nez(FTMP, label);
3308 break;
3309 case kCondNE:
3310 __ CmpEqD(FTMP, lhs, rhs);
3311 __ Bc1eqz(FTMP, label);
3312 break;
3313 case kCondLT:
3314 if (gt_bias) {
3315 __ CmpLtD(FTMP, lhs, rhs);
3316 } else {
3317 __ CmpUltD(FTMP, lhs, rhs);
3318 }
3319 __ Bc1nez(FTMP, label);
3320 break;
3321 case kCondLE:
3322 if (gt_bias) {
3323 __ CmpLeD(FTMP, lhs, rhs);
3324 } else {
3325 __ CmpUleD(FTMP, lhs, rhs);
3326 }
3327 __ Bc1nez(FTMP, label);
3328 break;
3329 case kCondGT:
3330 if (gt_bias) {
3331 __ CmpUltD(FTMP, rhs, lhs);
3332 } else {
3333 __ CmpLtD(FTMP, rhs, lhs);
3334 }
3335 __ Bc1nez(FTMP, label);
3336 break;
3337 case kCondGE:
3338 if (gt_bias) {
3339 __ CmpUleD(FTMP, rhs, lhs);
3340 } else {
3341 __ CmpLeD(FTMP, rhs, lhs);
3342 }
3343 __ Bc1nez(FTMP, label);
3344 break;
3345 default:
3346 LOG(FATAL) << "Unexpected non-floating-point condition";
3347 }
3348 } else {
3349 switch (cond) {
3350 case kCondEQ:
3351 __ CeqD(0, lhs, rhs);
3352 __ Bc1t(0, label);
3353 break;
3354 case kCondNE:
3355 __ CeqD(0, lhs, rhs);
3356 __ Bc1f(0, label);
3357 break;
3358 case kCondLT:
3359 if (gt_bias) {
3360 __ ColtD(0, lhs, rhs);
3361 } else {
3362 __ CultD(0, lhs, rhs);
3363 }
3364 __ Bc1t(0, label);
3365 break;
3366 case kCondLE:
3367 if (gt_bias) {
3368 __ ColeD(0, lhs, rhs);
3369 } else {
3370 __ CuleD(0, lhs, rhs);
3371 }
3372 __ Bc1t(0, label);
3373 break;
3374 case kCondGT:
3375 if (gt_bias) {
3376 __ CultD(0, rhs, lhs);
3377 } else {
3378 __ ColtD(0, rhs, lhs);
3379 }
3380 __ Bc1t(0, label);
3381 break;
3382 case kCondGE:
3383 if (gt_bias) {
3384 __ CuleD(0, rhs, lhs);
3385 } else {
3386 __ ColeD(0, rhs, lhs);
3387 }
3388 __ Bc1t(0, label);
3389 break;
3390 default:
3391 LOG(FATAL) << "Unexpected non-floating-point condition";
3392 }
3393 }
3394 }
3395}
3396
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003397void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003398 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003399 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003400 MipsLabel* false_target) {
3401 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003402
David Brazdil0debae72015-11-12 18:37:00 +00003403 if (true_target == nullptr && false_target == nullptr) {
3404 // Nothing to do. The code always falls through.
3405 return;
3406 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003407 // Constant condition, statically compared against "true" (integer value 1).
3408 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003409 if (true_target != nullptr) {
3410 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003411 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003412 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003413 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003414 if (false_target != nullptr) {
3415 __ B(false_target);
3416 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003417 }
David Brazdil0debae72015-11-12 18:37:00 +00003418 return;
3419 }
3420
3421 // The following code generates these patterns:
3422 // (1) true_target == nullptr && false_target != nullptr
3423 // - opposite condition true => branch to false_target
3424 // (2) true_target != nullptr && false_target == nullptr
3425 // - condition true => branch to true_target
3426 // (3) true_target != nullptr && false_target != nullptr
3427 // - condition true => branch to true_target
3428 // - branch to false_target
3429 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003430 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003431 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003432 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003433 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003434 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3435 } else {
3436 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3437 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003438 } else {
3439 // The condition instruction has not been materialized, use its inputs as
3440 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003441 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003442 Primitive::Type type = condition->InputAt(0)->GetType();
3443 LocationSummary* locations = cond->GetLocations();
3444 IfCondition if_cond = condition->GetCondition();
3445 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003446
David Brazdil0debae72015-11-12 18:37:00 +00003447 if (true_target == nullptr) {
3448 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003449 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003450 }
3451
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003452 switch (type) {
3453 default:
3454 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3455 break;
3456 case Primitive::kPrimLong:
3457 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3458 break;
3459 case Primitive::kPrimFloat:
3460 case Primitive::kPrimDouble:
3461 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3462 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003463 }
3464 }
David Brazdil0debae72015-11-12 18:37:00 +00003465
3466 // If neither branch falls through (case 3), the conditional branch to `true_target`
3467 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3468 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003469 __ B(false_target);
3470 }
3471}
3472
3473void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3474 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003475 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003476 locations->SetInAt(0, Location::RequiresRegister());
3477 }
3478}
3479
3480void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003481 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3482 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3483 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3484 nullptr : codegen_->GetLabelOf(true_successor);
3485 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3486 nullptr : codegen_->GetLabelOf(false_successor);
3487 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003488}
3489
3490void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3491 LocationSummary* locations = new (GetGraph()->GetArena())
3492 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003493 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003494 locations->SetInAt(0, Location::RequiresRegister());
3495 }
3496}
3497
3498void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003499 SlowPathCodeMIPS* slow_path =
3500 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003501 GenerateTestAndBranch(deoptimize,
3502 /* condition_input_index */ 0,
3503 slow_path->GetEntryLabel(),
3504 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003505}
3506
David Brazdil74eb1b22015-12-14 11:44:01 +00003507void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3509 if (Primitive::IsFloatingPointType(select->GetType())) {
3510 locations->SetInAt(0, Location::RequiresFpuRegister());
3511 locations->SetInAt(1, Location::RequiresFpuRegister());
3512 } else {
3513 locations->SetInAt(0, Location::RequiresRegister());
3514 locations->SetInAt(1, Location::RequiresRegister());
3515 }
3516 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3517 locations->SetInAt(2, Location::RequiresRegister());
3518 }
3519 locations->SetOut(Location::SameAsFirstInput());
3520}
3521
3522void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3523 LocationSummary* locations = select->GetLocations();
3524 MipsLabel false_target;
3525 GenerateTestAndBranch(select,
3526 /* condition_input_index */ 2,
3527 /* true_target */ nullptr,
3528 &false_target);
3529 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3530 __ Bind(&false_target);
3531}
3532
David Srbecky0cf44932015-12-09 14:09:59 +00003533void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3534 new (GetGraph()->GetArena()) LocationSummary(info);
3535}
3536
David Srbeckyd28f4a02016-03-14 17:14:24 +00003537void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3538 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003539}
3540
3541void CodeGeneratorMIPS::GenerateNop() {
3542 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003543}
3544
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003545void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3546 Primitive::Type field_type = field_info.GetFieldType();
3547 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3548 bool generate_volatile = field_info.IsVolatile() && is_wide;
3549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003550 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003551
3552 locations->SetInAt(0, Location::RequiresRegister());
3553 if (generate_volatile) {
3554 InvokeRuntimeCallingConvention calling_convention;
3555 // need A0 to hold base + offset
3556 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3557 if (field_type == Primitive::kPrimLong) {
3558 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3559 } else {
3560 locations->SetOut(Location::RequiresFpuRegister());
3561 // Need some temp core regs since FP results are returned in core registers
3562 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3563 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3564 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3565 }
3566 } else {
3567 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3568 locations->SetOut(Location::RequiresFpuRegister());
3569 } else {
3570 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3571 }
3572 }
3573}
3574
3575void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3576 const FieldInfo& field_info,
3577 uint32_t dex_pc) {
3578 Primitive::Type type = field_info.GetFieldType();
3579 LocationSummary* locations = instruction->GetLocations();
3580 Register obj = locations->InAt(0).AsRegister<Register>();
3581 LoadOperandType load_type = kLoadUnsignedByte;
3582 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003583 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003584
3585 switch (type) {
3586 case Primitive::kPrimBoolean:
3587 load_type = kLoadUnsignedByte;
3588 break;
3589 case Primitive::kPrimByte:
3590 load_type = kLoadSignedByte;
3591 break;
3592 case Primitive::kPrimShort:
3593 load_type = kLoadSignedHalfword;
3594 break;
3595 case Primitive::kPrimChar:
3596 load_type = kLoadUnsignedHalfword;
3597 break;
3598 case Primitive::kPrimInt:
3599 case Primitive::kPrimFloat:
3600 case Primitive::kPrimNot:
3601 load_type = kLoadWord;
3602 break;
3603 case Primitive::kPrimLong:
3604 case Primitive::kPrimDouble:
3605 load_type = kLoadDoubleword;
3606 break;
3607 case Primitive::kPrimVoid:
3608 LOG(FATAL) << "Unreachable type " << type;
3609 UNREACHABLE();
3610 }
3611
3612 if (is_volatile && load_type == kLoadDoubleword) {
3613 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003614 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003615 // Do implicit Null check
3616 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3617 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3618 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3619 instruction,
3620 dex_pc,
3621 nullptr,
3622 IsDirectEntrypoint(kQuickA64Load));
3623 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3624 if (type == Primitive::kPrimDouble) {
3625 // Need to move to FP regs since FP results are returned in core registers.
3626 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3627 locations->Out().AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003628 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3629 locations->Out().AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003630 }
3631 } else {
3632 if (!Primitive::IsFloatingPointType(type)) {
3633 Register dst;
3634 if (type == Primitive::kPrimLong) {
3635 DCHECK(locations->Out().IsRegisterPair());
3636 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003637 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3638 if (obj == dst) {
3639 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3640 codegen_->MaybeRecordImplicitNullCheck(instruction);
3641 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3642 } else {
3643 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3644 codegen_->MaybeRecordImplicitNullCheck(instruction);
3645 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3646 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003647 } else {
3648 DCHECK(locations->Out().IsRegister());
3649 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003650 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003651 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003652 } else {
3653 DCHECK(locations->Out().IsFpuRegister());
3654 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3655 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003656 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003657 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003658 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003659 }
3660 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003661 // Longs are handled earlier.
3662 if (type != Primitive::kPrimLong) {
3663 codegen_->MaybeRecordImplicitNullCheck(instruction);
3664 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003665 }
3666
3667 if (is_volatile) {
3668 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3669 }
3670}
3671
3672void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3673 Primitive::Type field_type = field_info.GetFieldType();
3674 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3675 bool generate_volatile = field_info.IsVolatile() && is_wide;
3676 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003677 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003678
3679 locations->SetInAt(0, Location::RequiresRegister());
3680 if (generate_volatile) {
3681 InvokeRuntimeCallingConvention calling_convention;
3682 // need A0 to hold base + offset
3683 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3684 if (field_type == Primitive::kPrimLong) {
3685 locations->SetInAt(1, Location::RegisterPairLocation(
3686 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3687 } else {
3688 locations->SetInAt(1, Location::RequiresFpuRegister());
3689 // Pass FP parameters in core registers.
3690 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3691 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3692 }
3693 } else {
3694 if (Primitive::IsFloatingPointType(field_type)) {
3695 locations->SetInAt(1, Location::RequiresFpuRegister());
3696 } else {
3697 locations->SetInAt(1, Location::RequiresRegister());
3698 }
3699 }
3700}
3701
3702void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3703 const FieldInfo& field_info,
3704 uint32_t dex_pc) {
3705 Primitive::Type type = field_info.GetFieldType();
3706 LocationSummary* locations = instruction->GetLocations();
3707 Register obj = locations->InAt(0).AsRegister<Register>();
3708 StoreOperandType store_type = kStoreByte;
3709 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003710 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003711
3712 switch (type) {
3713 case Primitive::kPrimBoolean:
3714 case Primitive::kPrimByte:
3715 store_type = kStoreByte;
3716 break;
3717 case Primitive::kPrimShort:
3718 case Primitive::kPrimChar:
3719 store_type = kStoreHalfword;
3720 break;
3721 case Primitive::kPrimInt:
3722 case Primitive::kPrimFloat:
3723 case Primitive::kPrimNot:
3724 store_type = kStoreWord;
3725 break;
3726 case Primitive::kPrimLong:
3727 case Primitive::kPrimDouble:
3728 store_type = kStoreDoubleword;
3729 break;
3730 case Primitive::kPrimVoid:
3731 LOG(FATAL) << "Unreachable type " << type;
3732 UNREACHABLE();
3733 }
3734
3735 if (is_volatile) {
3736 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3737 }
3738
3739 if (is_volatile && store_type == kStoreDoubleword) {
3740 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003741 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003742 // Do implicit Null check.
3743 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3744 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3745 if (type == Primitive::kPrimDouble) {
3746 // Pass FP parameters in core registers.
3747 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3748 locations->InAt(1).AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003749 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3750 locations->InAt(1).AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003751 }
3752 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3753 instruction,
3754 dex_pc,
3755 nullptr,
3756 IsDirectEntrypoint(kQuickA64Store));
3757 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3758 } else {
3759 if (!Primitive::IsFloatingPointType(type)) {
3760 Register src;
3761 if (type == Primitive::kPrimLong) {
3762 DCHECK(locations->InAt(1).IsRegisterPair());
3763 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003764 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3765 __ StoreToOffset(kStoreWord, src, obj, offset);
3766 codegen_->MaybeRecordImplicitNullCheck(instruction);
3767 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003768 } else {
3769 DCHECK(locations->InAt(1).IsRegister());
3770 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003771 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003772 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003773 } else {
3774 DCHECK(locations->InAt(1).IsFpuRegister());
3775 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3776 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003777 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003778 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003779 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003780 }
3781 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003782 // Longs are handled earlier.
3783 if (type != Primitive::kPrimLong) {
3784 codegen_->MaybeRecordImplicitNullCheck(instruction);
3785 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003786 }
3787
3788 // TODO: memory barriers?
3789 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3790 DCHECK(locations->InAt(1).IsRegister());
3791 Register src = locations->InAt(1).AsRegister<Register>();
3792 codegen_->MarkGCCard(obj, src);
3793 }
3794
3795 if (is_volatile) {
3796 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3797 }
3798}
3799
3800void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3801 HandleFieldGet(instruction, instruction->GetFieldInfo());
3802}
3803
3804void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3805 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3806}
3807
3808void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3809 HandleFieldSet(instruction, instruction->GetFieldInfo());
3810}
3811
3812void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3813 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3814}
3815
Alexey Frunze06a46c42016-07-19 15:00:40 -07003816void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
3817 HInstruction* instruction ATTRIBUTE_UNUSED,
3818 Location root,
3819 Register obj,
3820 uint32_t offset) {
3821 Register root_reg = root.AsRegister<Register>();
3822 if (kEmitCompilerReadBarrier) {
3823 UNIMPLEMENTED(FATAL) << "for read barrier";
3824 } else {
3825 // Plain GC root load with no read barrier.
3826 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3827 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
3828 // Note that GC roots are not affected by heap poisoning, thus we
3829 // do not have to unpoison `root_reg` here.
3830 }
3831}
3832
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003833void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3834 LocationSummary::CallKind call_kind =
3835 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3836 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3837 locations->SetInAt(0, Location::RequiresRegister());
3838 locations->SetInAt(1, Location::RequiresRegister());
3839 // The output does overlap inputs.
3840 // Note that TypeCheckSlowPathMIPS uses this register too.
3841 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3842}
3843
3844void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3845 LocationSummary* locations = instruction->GetLocations();
3846 Register obj = locations->InAt(0).AsRegister<Register>();
3847 Register cls = locations->InAt(1).AsRegister<Register>();
3848 Register out = locations->Out().AsRegister<Register>();
3849
3850 MipsLabel done;
3851
3852 // Return 0 if `obj` is null.
3853 // TODO: Avoid this check if we know `obj` is not null.
3854 __ Move(out, ZERO);
3855 __ Beqz(obj, &done);
3856
3857 // Compare the class of `obj` with `cls`.
3858 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3859 if (instruction->IsExactCheck()) {
3860 // Classes must be equal for the instanceof to succeed.
3861 __ Xor(out, out, cls);
3862 __ Sltiu(out, out, 1);
3863 } else {
3864 // If the classes are not equal, we go into a slow path.
3865 DCHECK(locations->OnlyCallsOnSlowPath());
3866 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3867 codegen_->AddSlowPath(slow_path);
3868 __ Bne(out, cls, slow_path->GetEntryLabel());
3869 __ LoadConst32(out, 1);
3870 __ Bind(slow_path->GetExitLabel());
3871 }
3872
3873 __ Bind(&done);
3874}
3875
3876void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3877 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3878 locations->SetOut(Location::ConstantLocation(constant));
3879}
3880
3881void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3882 // Will be generated at use site.
3883}
3884
3885void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3887 locations->SetOut(Location::ConstantLocation(constant));
3888}
3889
3890void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3891 // Will be generated at use site.
3892}
3893
3894void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3895 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3896 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3897}
3898
3899void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3900 HandleInvoke(invoke);
3901 // The register T0 is required to be used for the hidden argument in
3902 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3903 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3904}
3905
3906void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3907 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3908 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003909 Location receiver = invoke->GetLocations()->InAt(0);
3910 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3911 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3912
3913 // Set the hidden argument.
3914 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3915 invoke->GetDexMethodIndex());
3916
3917 // temp = object->GetClass();
3918 if (receiver.IsStackSlot()) {
3919 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3920 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3921 } else {
3922 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3923 }
3924 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003925 __ LoadFromOffset(kLoadWord, temp, temp,
3926 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3927 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003928 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003929 // temp = temp->GetImtEntryAt(method_offset);
3930 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3931 // T9 = temp->GetEntryPoint();
3932 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3933 // T9();
3934 __ Jalr(T9);
3935 __ Nop();
3936 DCHECK(!codegen_->IsLeafMethod());
3937 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3938}
3939
3940void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003941 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3942 if (intrinsic.TryDispatch(invoke)) {
3943 return;
3944 }
3945
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003946 HandleInvoke(invoke);
3947}
3948
3949void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003950 // Explicit clinit checks triggered by static invokes must have been pruned by
3951 // art::PrepareForRegisterAllocation.
3952 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003953
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003954 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3955 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3956 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3957
3958 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
3959 // R6 has PC-relative addressing.
3960 bool has_extra_input = !isR6 &&
3961 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3962 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
3963
3964 if (invoke->HasPcRelativeDexCache()) {
3965 // kDexCachePcRelative is mutually exclusive with
3966 // kDirectAddressWithFixup/kCallDirectWithFixup.
3967 CHECK(!has_extra_input);
3968 has_extra_input = true;
3969 }
3970
Chris Larsen701566a2015-10-27 15:29:13 -07003971 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3972 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003973 if (invoke->GetLocations()->CanCall() && has_extra_input) {
3974 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3975 }
Chris Larsen701566a2015-10-27 15:29:13 -07003976 return;
3977 }
3978
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003979 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003980
3981 // Add the extra input register if either the dex cache array base register
3982 // or the PC-relative base register for accessing literals is needed.
3983 if (has_extra_input) {
3984 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3985 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003986}
3987
Chris Larsen701566a2015-10-27 15:29:13 -07003988static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003989 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003990 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3991 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003992 return true;
3993 }
3994 return false;
3995}
3996
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003997HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07003998 HLoadString::LoadKind desired_string_load_kind) {
3999 if (kEmitCompilerReadBarrier) {
4000 UNIMPLEMENTED(FATAL) << "for read barrier";
4001 }
4002 // We disable PC-relative load when there is an irreducible loop, as the optimization
4003 // is incompatible with it.
4004 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4005 bool fallback_load = has_irreducible_loops;
4006 switch (desired_string_load_kind) {
4007 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4008 DCHECK(!GetCompilerOptions().GetCompilePic());
4009 break;
4010 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4011 DCHECK(GetCompilerOptions().GetCompilePic());
4012 break;
4013 case HLoadString::LoadKind::kBootImageAddress:
4014 break;
4015 case HLoadString::LoadKind::kDexCacheAddress:
4016 DCHECK(Runtime::Current()->UseJitCompilation());
4017 fallback_load = false;
4018 break;
4019 case HLoadString::LoadKind::kDexCachePcRelative:
4020 DCHECK(!Runtime::Current()->UseJitCompilation());
4021 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4022 // with irreducible loops.
4023 break;
4024 case HLoadString::LoadKind::kDexCacheViaMethod:
4025 fallback_load = false;
4026 break;
4027 }
4028 if (fallback_load) {
4029 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4030 }
4031 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004032}
4033
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004034HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4035 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004036 if (kEmitCompilerReadBarrier) {
4037 UNIMPLEMENTED(FATAL) << "for read barrier";
4038 }
4039 // We disable pc-relative load when there is an irreducible loop, as the optimization
4040 // is incompatible with it.
4041 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4042 bool fallback_load = has_irreducible_loops;
4043 switch (desired_class_load_kind) {
4044 case HLoadClass::LoadKind::kReferrersClass:
4045 fallback_load = false;
4046 break;
4047 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4048 DCHECK(!GetCompilerOptions().GetCompilePic());
4049 break;
4050 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4051 DCHECK(GetCompilerOptions().GetCompilePic());
4052 break;
4053 case HLoadClass::LoadKind::kBootImageAddress:
4054 break;
4055 case HLoadClass::LoadKind::kDexCacheAddress:
4056 DCHECK(Runtime::Current()->UseJitCompilation());
4057 fallback_load = false;
4058 break;
4059 case HLoadClass::LoadKind::kDexCachePcRelative:
4060 DCHECK(!Runtime::Current()->UseJitCompilation());
4061 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4062 // with irreducible loops.
4063 break;
4064 case HLoadClass::LoadKind::kDexCacheViaMethod:
4065 fallback_load = false;
4066 break;
4067 }
4068 if (fallback_load) {
4069 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4070 }
4071 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004072}
4073
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004074Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4075 Register temp) {
4076 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4077 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4078 if (!invoke->GetLocations()->Intrinsified()) {
4079 return location.AsRegister<Register>();
4080 }
4081 // For intrinsics we allow any location, so it may be on the stack.
4082 if (!location.IsRegister()) {
4083 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4084 return temp;
4085 }
4086 // For register locations, check if the register was saved. If so, get it from the stack.
4087 // Note: There is a chance that the register was saved but not overwritten, so we could
4088 // save one load. However, since this is just an intrinsic slow path we prefer this
4089 // simple and more robust approach rather that trying to determine if that's the case.
4090 SlowPathCode* slow_path = GetCurrentSlowPath();
4091 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4092 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4093 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4094 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4095 return temp;
4096 }
4097 return location.AsRegister<Register>();
4098}
4099
Vladimir Markodc151b22015-10-15 18:02:30 +01004100HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4101 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4102 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004103 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4104 // We disable PC-relative load when there is an irreducible loop, as the optimization
4105 // is incompatible with it.
4106 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4107 bool fallback_load = true;
4108 bool fallback_call = true;
4109 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004110 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4111 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004112 fallback_load = has_irreducible_loops;
4113 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004114 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004115 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004116 break;
4117 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004118 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004119 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004120 fallback_call = has_irreducible_loops;
4121 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004122 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004123 // TODO: Implement this type.
4124 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004125 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004126 fallback_call = false;
4127 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004128 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004129 if (fallback_load) {
4130 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4131 dispatch_info.method_load_data = 0;
4132 }
4133 if (fallback_call) {
4134 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4135 dispatch_info.direct_code_ptr = 0;
4136 }
4137 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004138}
4139
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004140void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4141 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004142 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004143 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4144 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4145 bool isR6 = isa_features_.IsR6();
4146 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4147 // R6 has PC-relative addressing.
4148 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4149 (!isR6 &&
4150 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4151 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4152 Register base_reg = has_extra_input
4153 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4154 : ZERO;
4155
4156 // For better instruction scheduling we load the direct code pointer before the method pointer.
4157 switch (code_ptr_location) {
4158 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4159 // T9 = invoke->GetDirectCodePtr();
4160 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4161 break;
4162 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4163 // T9 = code address from literal pool with link-time patch.
4164 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4165 break;
4166 default:
4167 break;
4168 }
4169
4170 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004171 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4172 // temp = thread->string_init_entrypoint
4173 __ LoadFromOffset(kLoadWord,
4174 temp.AsRegister<Register>(),
4175 TR,
4176 invoke->GetStringInitOffset());
4177 break;
4178 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004179 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004180 break;
4181 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4182 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4183 break;
4184 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004185 __ LoadLiteral(temp.AsRegister<Register>(),
4186 base_reg,
4187 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4188 break;
4189 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4190 HMipsDexCacheArraysBase* base =
4191 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4192 int32_t offset =
4193 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4194 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4195 break;
4196 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004197 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004198 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004199 Register reg = temp.AsRegister<Register>();
4200 Register method_reg;
4201 if (current_method.IsRegister()) {
4202 method_reg = current_method.AsRegister<Register>();
4203 } else {
4204 // TODO: use the appropriate DCHECK() here if possible.
4205 // DCHECK(invoke->GetLocations()->Intrinsified());
4206 DCHECK(!current_method.IsValid());
4207 method_reg = reg;
4208 __ Lw(reg, SP, kCurrentMethodStackOffset);
4209 }
4210
4211 // temp = temp->dex_cache_resolved_methods_;
4212 __ LoadFromOffset(kLoadWord,
4213 reg,
4214 method_reg,
4215 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004216 // temp = temp[index_in_cache];
4217 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4218 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004219 __ LoadFromOffset(kLoadWord,
4220 reg,
4221 reg,
4222 CodeGenerator::GetCachePointerOffset(index_in_cache));
4223 break;
4224 }
4225 }
4226
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004227 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004228 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004229 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004230 break;
4231 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004232 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4233 // T9 prepared above for better instruction scheduling.
4234 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004235 __ Jalr(T9);
4236 __ Nop();
4237 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004238 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004239 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004240 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4241 LOG(FATAL) << "Unsupported";
4242 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004243 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4244 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004245 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004246 T9,
4247 callee_method.AsRegister<Register>(),
4248 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4249 kMipsWordSize).Int32Value());
4250 // T9()
4251 __ Jalr(T9);
4252 __ Nop();
4253 break;
4254 }
4255 DCHECK(!IsLeafMethod());
4256}
4257
4258void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004259 // Explicit clinit checks triggered by static invokes must have been pruned by
4260 // art::PrepareForRegisterAllocation.
4261 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004262
4263 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4264 return;
4265 }
4266
4267 LocationSummary* locations = invoke->GetLocations();
4268 codegen_->GenerateStaticOrDirectCall(invoke,
4269 locations->HasTemps()
4270 ? locations->GetTemp(0)
4271 : Location::NoLocation());
4272 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4273}
4274
Chris Larsen3acee732015-11-18 13:31:08 -08004275void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004276 LocationSummary* locations = invoke->GetLocations();
4277 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004278 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004279 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4280 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4281 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4282 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4283
4284 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004285 DCHECK(receiver.IsRegister());
4286 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4287 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004288 // temp = temp->GetMethodAt(method_offset);
4289 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4290 // T9 = temp->GetEntryPoint();
4291 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4292 // T9();
4293 __ Jalr(T9);
4294 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08004295}
4296
4297void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4298 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4299 return;
4300 }
4301
4302 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004303 DCHECK(!codegen_->IsLeafMethod());
4304 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4305}
4306
4307void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004308 if (cls->NeedsAccessCheck()) {
4309 InvokeRuntimeCallingConvention calling_convention;
4310 CodeGenerator::CreateLoadClassLocationSummary(
4311 cls,
4312 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4313 Location::RegisterLocation(V0),
4314 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4315 return;
4316 }
4317
4318 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4319 ? LocationSummary::kCallOnSlowPath
4320 : LocationSummary::kNoCall;
4321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4322 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4323 switch (load_kind) {
4324 // We need an extra register for PC-relative literals on R2.
4325 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4326 case HLoadClass::LoadKind::kBootImageAddress:
4327 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4328 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4329 break;
4330 }
4331 FALLTHROUGH_INTENDED;
4332 // We need an extra register for PC-relative dex cache accesses.
4333 case HLoadClass::LoadKind::kDexCachePcRelative:
4334 case HLoadClass::LoadKind::kReferrersClass:
4335 case HLoadClass::LoadKind::kDexCacheViaMethod:
4336 locations->SetInAt(0, Location::RequiresRegister());
4337 break;
4338 default:
4339 break;
4340 }
4341 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004342}
4343
4344void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4345 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004346 if (cls->NeedsAccessCheck()) {
4347 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4348 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4349 cls,
4350 cls->GetDexPc(),
4351 nullptr,
4352 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004353 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004354 return;
4355 }
4356
Alexey Frunze06a46c42016-07-19 15:00:40 -07004357 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4358 Location out_loc = locations->Out();
4359 Register out = out_loc.AsRegister<Register>();
4360 Register base_or_current_method_reg;
4361 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4362 switch (load_kind) {
4363 // We need an extra register for PC-relative literals on R2.
4364 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4365 case HLoadClass::LoadKind::kBootImageAddress:
4366 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4367 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4368 break;
4369 // We need an extra register for PC-relative dex cache accesses.
4370 case HLoadClass::LoadKind::kDexCachePcRelative:
4371 case HLoadClass::LoadKind::kReferrersClass:
4372 case HLoadClass::LoadKind::kDexCacheViaMethod:
4373 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4374 break;
4375 default:
4376 base_or_current_method_reg = ZERO;
4377 break;
4378 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004379
Alexey Frunze06a46c42016-07-19 15:00:40 -07004380 bool generate_null_check = false;
4381 switch (load_kind) {
4382 case HLoadClass::LoadKind::kReferrersClass: {
4383 DCHECK(!cls->CanCallRuntime());
4384 DCHECK(!cls->MustGenerateClinitCheck());
4385 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4386 GenerateGcRootFieldLoad(cls,
4387 out_loc,
4388 base_or_current_method_reg,
4389 ArtMethod::DeclaringClassOffset().Int32Value());
4390 break;
4391 }
4392 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4393 DCHECK(!kEmitCompilerReadBarrier);
4394 __ LoadLiteral(out,
4395 base_or_current_method_reg,
4396 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4397 cls->GetTypeIndex()));
4398 break;
4399 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4400 DCHECK(!kEmitCompilerReadBarrier);
4401 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4402 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
4403 if (isR6) {
4404 __ Bind(&info->high_label);
4405 __ Bind(&info->pc_rel_label);
4406 // Add a 32-bit offset to PC.
4407 __ Auipc(out, /* placeholder */ 0x1234);
4408 __ Addiu(out, out, /* placeholder */ 0x5678);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004409 } else {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004410 __ Bind(&info->high_label);
4411 __ Lui(out, /* placeholder */ 0x1234);
4412 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4413 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4414 __ Ori(out, out, /* placeholder */ 0x5678);
4415 // Add a 32-bit offset to PC.
4416 __ Addu(out, out, base_or_current_method_reg);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004417 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004418 break;
4419 }
4420 case HLoadClass::LoadKind::kBootImageAddress: {
4421 DCHECK(!kEmitCompilerReadBarrier);
4422 DCHECK_NE(cls->GetAddress(), 0u);
4423 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4424 __ LoadLiteral(out,
4425 base_or_current_method_reg,
4426 codegen_->DeduplicateBootImageAddressLiteral(address));
4427 break;
4428 }
4429 case HLoadClass::LoadKind::kDexCacheAddress: {
4430 DCHECK_NE(cls->GetAddress(), 0u);
4431 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4432 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4433 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4434 int16_t offset = Low16Bits(address);
4435 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4436 __ Lui(out, High16Bits(base_address));
4437 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4438 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4439 generate_null_check = !cls->IsInDexCache();
4440 break;
4441 }
4442 case HLoadClass::LoadKind::kDexCachePcRelative: {
4443 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4444 int32_t offset =
4445 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4446 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4447 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4448 generate_null_check = !cls->IsInDexCache();
4449 break;
4450 }
4451 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4452 // /* GcRoot<mirror::Class>[] */ out =
4453 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4454 __ LoadFromOffset(kLoadWord,
4455 out,
4456 base_or_current_method_reg,
4457 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4458 // /* GcRoot<mirror::Class> */ out = out[type_index]
4459 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4460 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4461 generate_null_check = !cls->IsInDexCache();
4462 }
4463 }
4464
4465 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4466 DCHECK(cls->CanCallRuntime());
4467 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4468 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4469 codegen_->AddSlowPath(slow_path);
4470 if (generate_null_check) {
4471 __ Beqz(out, slow_path->GetEntryLabel());
4472 }
4473 if (cls->MustGenerateClinitCheck()) {
4474 GenerateClassInitializationCheck(slow_path, out);
4475 } else {
4476 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004477 }
4478 }
4479}
4480
4481static int32_t GetExceptionTlsOffset() {
4482 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4483}
4484
4485void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4486 LocationSummary* locations =
4487 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4488 locations->SetOut(Location::RequiresRegister());
4489}
4490
4491void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4492 Register out = load->GetLocations()->Out().AsRegister<Register>();
4493 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4494}
4495
4496void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4497 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4498}
4499
4500void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4501 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4502}
4503
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004504void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004505 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004506 ? LocationSummary::kCallOnSlowPath
4507 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004509 HLoadString::LoadKind load_kind = load->GetLoadKind();
4510 switch (load_kind) {
4511 // We need an extra register for PC-relative literals on R2.
4512 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4513 case HLoadString::LoadKind::kBootImageAddress:
4514 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4515 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4516 break;
4517 }
4518 FALLTHROUGH_INTENDED;
4519 // We need an extra register for PC-relative dex cache accesses.
4520 case HLoadString::LoadKind::kDexCachePcRelative:
4521 case HLoadString::LoadKind::kDexCacheViaMethod:
4522 locations->SetInAt(0, Location::RequiresRegister());
4523 break;
4524 default:
4525 break;
4526 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004527 locations->SetOut(Location::RequiresRegister());
4528}
4529
4530void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004531 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004532 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004533 Location out_loc = locations->Out();
4534 Register out = out_loc.AsRegister<Register>();
4535 Register base_or_current_method_reg;
4536 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4537 switch (load_kind) {
4538 // We need an extra register for PC-relative literals on R2.
4539 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4540 case HLoadString::LoadKind::kBootImageAddress:
4541 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4542 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4543 break;
4544 // We need an extra register for PC-relative dex cache accesses.
4545 case HLoadString::LoadKind::kDexCachePcRelative:
4546 case HLoadString::LoadKind::kDexCacheViaMethod:
4547 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4548 break;
4549 default:
4550 base_or_current_method_reg = ZERO;
4551 break;
4552 }
4553
4554 switch (load_kind) {
4555 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4556 DCHECK(!kEmitCompilerReadBarrier);
4557 __ LoadLiteral(out,
4558 base_or_current_method_reg,
4559 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4560 load->GetStringIndex()));
4561 return; // No dex cache slow path.
4562 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4563 DCHECK(!kEmitCompilerReadBarrier);
4564 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4565 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
4566 if (isR6) {
4567 __ Bind(&info->high_label);
4568 __ Bind(&info->pc_rel_label);
4569 // Add a 32-bit offset to PC.
4570 __ Auipc(out, /* placeholder */ 0x1234);
4571 __ Addiu(out, out, /* placeholder */ 0x5678);
4572 } else {
4573 __ Bind(&info->high_label);
4574 __ Lui(out, /* placeholder */ 0x1234);
4575 // We do not bind info->pc_rel_label here, we'll use the assembler's label
4576 // for PC-relative literals and the base from HMipsComputeBaseMethodAddress.
4577 __ Ori(out, out, /* placeholder */ 0x5678);
4578 // Add a 32-bit offset to PC.
4579 __ Addu(out, out, base_or_current_method_reg);
4580 }
4581 return; // No dex cache slow path.
4582 }
4583 case HLoadString::LoadKind::kBootImageAddress: {
4584 DCHECK(!kEmitCompilerReadBarrier);
4585 DCHECK_NE(load->GetAddress(), 0u);
4586 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4587 __ LoadLiteral(out,
4588 base_or_current_method_reg,
4589 codegen_->DeduplicateBootImageAddressLiteral(address));
4590 return; // No dex cache slow path.
4591 }
4592 case HLoadString::LoadKind::kDexCacheAddress: {
4593 DCHECK_NE(load->GetAddress(), 0u);
4594 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4595 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4596 DCHECK_ALIGNED(load->GetAddress(), 4u);
4597 int16_t offset = Low16Bits(address);
4598 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4599 __ Lui(out, High16Bits(base_address));
4600 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
4601 GenerateGcRootFieldLoad(load, out_loc, out, offset);
4602 break;
4603 }
4604 case HLoadString::LoadKind::kDexCachePcRelative: {
4605 HMipsDexCacheArraysBase* base = load->InputAt(0)->AsMipsDexCacheArraysBase();
4606 int32_t offset =
4607 load->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4608 // /* GcRoot<mirror::String> */ out = *(dex_cache_arrays_base + offset)
4609 GenerateGcRootFieldLoad(load, out_loc, base_or_current_method_reg, offset);
4610 break;
4611 }
4612 case HLoadString::LoadKind::kDexCacheViaMethod: {
4613 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4614 GenerateGcRootFieldLoad(load,
4615 out_loc,
4616 base_or_current_method_reg,
4617 ArtMethod::DeclaringClassOffset().Int32Value());
4618 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4619 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4620 // /* GcRoot<mirror::String> */ out = out[string_index]
4621 GenerateGcRootFieldLoad(load,
4622 out_loc,
4623 out,
4624 CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4625 break;
4626 }
4627 default:
4628 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4629 UNREACHABLE();
4630 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004631
4632 if (!load->IsInDexCache()) {
4633 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4634 codegen_->AddSlowPath(slow_path);
4635 __ Beqz(out, slow_path->GetEntryLabel());
4636 __ Bind(slow_path->GetExitLabel());
4637 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004638}
4639
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004640void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4641 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4642 locations->SetOut(Location::ConstantLocation(constant));
4643}
4644
4645void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4646 // Will be generated at use site.
4647}
4648
4649void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4650 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004651 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004652 InvokeRuntimeCallingConvention calling_convention;
4653 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4654}
4655
4656void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4657 if (instruction->IsEnter()) {
4658 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4659 instruction,
4660 instruction->GetDexPc(),
4661 nullptr,
4662 IsDirectEntrypoint(kQuickLockObject));
4663 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4664 } else {
4665 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4666 instruction,
4667 instruction->GetDexPc(),
4668 nullptr,
4669 IsDirectEntrypoint(kQuickUnlockObject));
4670 }
4671 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4672}
4673
4674void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4675 LocationSummary* locations =
4676 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4677 switch (mul->GetResultType()) {
4678 case Primitive::kPrimInt:
4679 case Primitive::kPrimLong:
4680 locations->SetInAt(0, Location::RequiresRegister());
4681 locations->SetInAt(1, Location::RequiresRegister());
4682 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4683 break;
4684
4685 case Primitive::kPrimFloat:
4686 case Primitive::kPrimDouble:
4687 locations->SetInAt(0, Location::RequiresFpuRegister());
4688 locations->SetInAt(1, Location::RequiresFpuRegister());
4689 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4690 break;
4691
4692 default:
4693 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4694 }
4695}
4696
4697void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4698 Primitive::Type type = instruction->GetType();
4699 LocationSummary* locations = instruction->GetLocations();
4700 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4701
4702 switch (type) {
4703 case Primitive::kPrimInt: {
4704 Register dst = locations->Out().AsRegister<Register>();
4705 Register lhs = locations->InAt(0).AsRegister<Register>();
4706 Register rhs = locations->InAt(1).AsRegister<Register>();
4707
4708 if (isR6) {
4709 __ MulR6(dst, lhs, rhs);
4710 } else {
4711 __ MulR2(dst, lhs, rhs);
4712 }
4713 break;
4714 }
4715 case Primitive::kPrimLong: {
4716 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4717 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4718 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4719 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4720 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4721 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4722
4723 // Extra checks to protect caused by the existance of A1_A2.
4724 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4725 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4726 DCHECK_NE(dst_high, lhs_low);
4727 DCHECK_NE(dst_high, rhs_low);
4728
4729 // A_B * C_D
4730 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4731 // dst_lo: [ low(B*D) ]
4732 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4733
4734 if (isR6) {
4735 __ MulR6(TMP, lhs_high, rhs_low);
4736 __ MulR6(dst_high, lhs_low, rhs_high);
4737 __ Addu(dst_high, dst_high, TMP);
4738 __ MuhuR6(TMP, lhs_low, rhs_low);
4739 __ Addu(dst_high, dst_high, TMP);
4740 __ MulR6(dst_low, lhs_low, rhs_low);
4741 } else {
4742 __ MulR2(TMP, lhs_high, rhs_low);
4743 __ MulR2(dst_high, lhs_low, rhs_high);
4744 __ Addu(dst_high, dst_high, TMP);
4745 __ MultuR2(lhs_low, rhs_low);
4746 __ Mfhi(TMP);
4747 __ Addu(dst_high, dst_high, TMP);
4748 __ Mflo(dst_low);
4749 }
4750 break;
4751 }
4752 case Primitive::kPrimFloat:
4753 case Primitive::kPrimDouble: {
4754 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4755 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4756 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4757 if (type == Primitive::kPrimFloat) {
4758 __ MulS(dst, lhs, rhs);
4759 } else {
4760 __ MulD(dst, lhs, rhs);
4761 }
4762 break;
4763 }
4764 default:
4765 LOG(FATAL) << "Unexpected mul type " << type;
4766 }
4767}
4768
4769void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4770 LocationSummary* locations =
4771 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4772 switch (neg->GetResultType()) {
4773 case Primitive::kPrimInt:
4774 case Primitive::kPrimLong:
4775 locations->SetInAt(0, Location::RequiresRegister());
4776 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4777 break;
4778
4779 case Primitive::kPrimFloat:
4780 case Primitive::kPrimDouble:
4781 locations->SetInAt(0, Location::RequiresFpuRegister());
4782 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4783 break;
4784
4785 default:
4786 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4787 }
4788}
4789
4790void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4791 Primitive::Type type = instruction->GetType();
4792 LocationSummary* locations = instruction->GetLocations();
4793
4794 switch (type) {
4795 case Primitive::kPrimInt: {
4796 Register dst = locations->Out().AsRegister<Register>();
4797 Register src = locations->InAt(0).AsRegister<Register>();
4798 __ Subu(dst, ZERO, src);
4799 break;
4800 }
4801 case Primitive::kPrimLong: {
4802 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4803 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4804 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4805 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4806 __ Subu(dst_low, ZERO, src_low);
4807 __ Sltu(TMP, ZERO, dst_low);
4808 __ Subu(dst_high, ZERO, src_high);
4809 __ Subu(dst_high, dst_high, TMP);
4810 break;
4811 }
4812 case Primitive::kPrimFloat:
4813 case Primitive::kPrimDouble: {
4814 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4815 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4816 if (type == Primitive::kPrimFloat) {
4817 __ NegS(dst, src);
4818 } else {
4819 __ NegD(dst, src);
4820 }
4821 break;
4822 }
4823 default:
4824 LOG(FATAL) << "Unexpected neg type " << type;
4825 }
4826}
4827
4828void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4829 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004830 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004831 InvokeRuntimeCallingConvention calling_convention;
4832 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4833 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4834 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4835 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4836}
4837
4838void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4839 InvokeRuntimeCallingConvention calling_convention;
4840 Register current_method_register = calling_convention.GetRegisterAt(2);
4841 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4842 // Move an uint16_t value to a register.
4843 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4844 codegen_->InvokeRuntime(
4845 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4846 instruction,
4847 instruction->GetDexPc(),
4848 nullptr,
4849 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4850 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4851 void*, uint32_t, int32_t, ArtMethod*>();
4852}
4853
4854void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4855 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004856 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004857 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004858 if (instruction->IsStringAlloc()) {
4859 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4860 } else {
4861 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4862 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4863 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004864 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4865}
4866
4867void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004868 if (instruction->IsStringAlloc()) {
4869 // String is allocated through StringFactory. Call NewEmptyString entry point.
4870 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4871 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4872 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4873 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4874 __ Jalr(T9);
4875 __ Nop();
4876 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4877 } else {
4878 codegen_->InvokeRuntime(
4879 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4880 instruction,
4881 instruction->GetDexPc(),
4882 nullptr,
4883 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4884 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4885 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004886}
4887
4888void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4889 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4890 locations->SetInAt(0, Location::RequiresRegister());
4891 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4892}
4893
4894void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4895 Primitive::Type type = instruction->GetType();
4896 LocationSummary* locations = instruction->GetLocations();
4897
4898 switch (type) {
4899 case Primitive::kPrimInt: {
4900 Register dst = locations->Out().AsRegister<Register>();
4901 Register src = locations->InAt(0).AsRegister<Register>();
4902 __ Nor(dst, src, ZERO);
4903 break;
4904 }
4905
4906 case Primitive::kPrimLong: {
4907 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4908 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4909 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4910 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4911 __ Nor(dst_high, src_high, ZERO);
4912 __ Nor(dst_low, src_low, ZERO);
4913 break;
4914 }
4915
4916 default:
4917 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4918 }
4919}
4920
4921void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4922 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4923 locations->SetInAt(0, Location::RequiresRegister());
4924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4925}
4926
4927void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4928 LocationSummary* locations = instruction->GetLocations();
4929 __ Xori(locations->Out().AsRegister<Register>(),
4930 locations->InAt(0).AsRegister<Register>(),
4931 1);
4932}
4933
4934void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4935 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4936 ? LocationSummary::kCallOnSlowPath
4937 : LocationSummary::kNoCall;
4938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4939 locations->SetInAt(0, Location::RequiresRegister());
4940 if (instruction->HasUses()) {
4941 locations->SetOut(Location::SameAsFirstInput());
4942 }
4943}
4944
Calin Juravle2ae48182016-03-16 14:05:09 +00004945void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4946 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004947 return;
4948 }
4949 Location obj = instruction->GetLocations()->InAt(0);
4950
4951 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004952 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004953}
4954
Calin Juravle2ae48182016-03-16 14:05:09 +00004955void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004956 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004957 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004958
4959 Location obj = instruction->GetLocations()->InAt(0);
4960
4961 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4962}
4963
4964void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004965 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004966}
4967
4968void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4969 HandleBinaryOp(instruction);
4970}
4971
4972void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4973 HandleBinaryOp(instruction);
4974}
4975
4976void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4977 LOG(FATAL) << "Unreachable";
4978}
4979
4980void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4981 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4982}
4983
4984void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4986 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4987 if (location.IsStackSlot()) {
4988 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4989 } else if (location.IsDoubleStackSlot()) {
4990 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4991 }
4992 locations->SetOut(location);
4993}
4994
4995void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4996 ATTRIBUTE_UNUSED) {
4997 // Nothing to do, the parameter is already at its location.
4998}
4999
5000void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5001 LocationSummary* locations =
5002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5003 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5004}
5005
5006void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5007 ATTRIBUTE_UNUSED) {
5008 // Nothing to do, the method is already at its location.
5009}
5010
5011void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5012 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005013 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005014 locations->SetInAt(i, Location::Any());
5015 }
5016 locations->SetOut(Location::Any());
5017}
5018
5019void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5020 LOG(FATAL) << "Unreachable";
5021}
5022
5023void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5024 Primitive::Type type = rem->GetResultType();
5025 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005026 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5028
5029 switch (type) {
5030 case Primitive::kPrimInt:
5031 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005032 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5034 break;
5035
5036 case Primitive::kPrimLong: {
5037 InvokeRuntimeCallingConvention calling_convention;
5038 locations->SetInAt(0, Location::RegisterPairLocation(
5039 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5040 locations->SetInAt(1, Location::RegisterPairLocation(
5041 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5042 locations->SetOut(calling_convention.GetReturnLocation(type));
5043 break;
5044 }
5045
5046 case Primitive::kPrimFloat:
5047 case Primitive::kPrimDouble: {
5048 InvokeRuntimeCallingConvention calling_convention;
5049 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5050 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5051 locations->SetOut(calling_convention.GetReturnLocation(type));
5052 break;
5053 }
5054
5055 default:
5056 LOG(FATAL) << "Unexpected rem type " << type;
5057 }
5058}
5059
5060void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5061 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005062
5063 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005064 case Primitive::kPrimInt:
5065 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005066 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005067 case Primitive::kPrimLong: {
5068 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
5069 instruction,
5070 instruction->GetDexPc(),
5071 nullptr,
5072 IsDirectEntrypoint(kQuickLmod));
5073 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5074 break;
5075 }
5076 case Primitive::kPrimFloat: {
5077 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
5078 instruction, instruction->GetDexPc(),
5079 nullptr,
5080 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00005081 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005082 break;
5083 }
5084 case Primitive::kPrimDouble: {
5085 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
5086 instruction, instruction->GetDexPc(),
5087 nullptr,
5088 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00005089 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005090 break;
5091 }
5092 default:
5093 LOG(FATAL) << "Unexpected rem type " << type;
5094 }
5095}
5096
5097void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5098 memory_barrier->SetLocations(nullptr);
5099}
5100
5101void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5102 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5103}
5104
5105void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5107 Primitive::Type return_type = ret->InputAt(0)->GetType();
5108 locations->SetInAt(0, MipsReturnLocation(return_type));
5109}
5110
5111void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5112 codegen_->GenerateFrameExit();
5113}
5114
5115void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5116 ret->SetLocations(nullptr);
5117}
5118
5119void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5120 codegen_->GenerateFrameExit();
5121}
5122
Alexey Frunze92d90602015-12-18 18:16:36 -08005123void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5124 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005125}
5126
Alexey Frunze92d90602015-12-18 18:16:36 -08005127void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5128 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005129}
5130
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005131void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5132 HandleShift(shl);
5133}
5134
5135void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5136 HandleShift(shl);
5137}
5138
5139void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5140 HandleShift(shr);
5141}
5142
5143void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5144 HandleShift(shr);
5145}
5146
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005147void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5148 HandleBinaryOp(instruction);
5149}
5150
5151void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5152 HandleBinaryOp(instruction);
5153}
5154
5155void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5156 HandleFieldGet(instruction, instruction->GetFieldInfo());
5157}
5158
5159void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5160 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5161}
5162
5163void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5164 HandleFieldSet(instruction, instruction->GetFieldInfo());
5165}
5166
5167void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5168 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5169}
5170
5171void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5172 HUnresolvedInstanceFieldGet* instruction) {
5173 FieldAccessCallingConventionMIPS calling_convention;
5174 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5175 instruction->GetFieldType(),
5176 calling_convention);
5177}
5178
5179void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5180 HUnresolvedInstanceFieldGet* instruction) {
5181 FieldAccessCallingConventionMIPS calling_convention;
5182 codegen_->GenerateUnresolvedFieldAccess(instruction,
5183 instruction->GetFieldType(),
5184 instruction->GetFieldIndex(),
5185 instruction->GetDexPc(),
5186 calling_convention);
5187}
5188
5189void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5190 HUnresolvedInstanceFieldSet* instruction) {
5191 FieldAccessCallingConventionMIPS calling_convention;
5192 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5193 instruction->GetFieldType(),
5194 calling_convention);
5195}
5196
5197void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5198 HUnresolvedInstanceFieldSet* instruction) {
5199 FieldAccessCallingConventionMIPS calling_convention;
5200 codegen_->GenerateUnresolvedFieldAccess(instruction,
5201 instruction->GetFieldType(),
5202 instruction->GetFieldIndex(),
5203 instruction->GetDexPc(),
5204 calling_convention);
5205}
5206
5207void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5208 HUnresolvedStaticFieldGet* instruction) {
5209 FieldAccessCallingConventionMIPS calling_convention;
5210 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5211 instruction->GetFieldType(),
5212 calling_convention);
5213}
5214
5215void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5216 HUnresolvedStaticFieldGet* instruction) {
5217 FieldAccessCallingConventionMIPS calling_convention;
5218 codegen_->GenerateUnresolvedFieldAccess(instruction,
5219 instruction->GetFieldType(),
5220 instruction->GetFieldIndex(),
5221 instruction->GetDexPc(),
5222 calling_convention);
5223}
5224
5225void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5226 HUnresolvedStaticFieldSet* instruction) {
5227 FieldAccessCallingConventionMIPS calling_convention;
5228 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5229 instruction->GetFieldType(),
5230 calling_convention);
5231}
5232
5233void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5234 HUnresolvedStaticFieldSet* instruction) {
5235 FieldAccessCallingConventionMIPS calling_convention;
5236 codegen_->GenerateUnresolvedFieldAccess(instruction,
5237 instruction->GetFieldType(),
5238 instruction->GetFieldIndex(),
5239 instruction->GetDexPc(),
5240 calling_convention);
5241}
5242
5243void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5244 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5245}
5246
5247void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5248 HBasicBlock* block = instruction->GetBlock();
5249 if (block->GetLoopInformation() != nullptr) {
5250 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5251 // The back edge will generate the suspend check.
5252 return;
5253 }
5254 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5255 // The goto will generate the suspend check.
5256 return;
5257 }
5258 GenerateSuspendCheck(instruction, nullptr);
5259}
5260
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005261void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5262 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005264 InvokeRuntimeCallingConvention calling_convention;
5265 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5266}
5267
5268void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
5269 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5270 instruction,
5271 instruction->GetDexPc(),
5272 nullptr,
5273 IsDirectEntrypoint(kQuickDeliverException));
5274 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5275}
5276
5277void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5278 Primitive::Type input_type = conversion->GetInputType();
5279 Primitive::Type result_type = conversion->GetResultType();
5280 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005281 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005282
5283 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5284 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5285 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5286 }
5287
5288 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005289 if (!isR6 &&
5290 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5291 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005292 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005293 }
5294
5295 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5296
5297 if (call_kind == LocationSummary::kNoCall) {
5298 if (Primitive::IsFloatingPointType(input_type)) {
5299 locations->SetInAt(0, Location::RequiresFpuRegister());
5300 } else {
5301 locations->SetInAt(0, Location::RequiresRegister());
5302 }
5303
5304 if (Primitive::IsFloatingPointType(result_type)) {
5305 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5306 } else {
5307 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5308 }
5309 } else {
5310 InvokeRuntimeCallingConvention calling_convention;
5311
5312 if (Primitive::IsFloatingPointType(input_type)) {
5313 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5314 } else {
5315 DCHECK_EQ(input_type, Primitive::kPrimLong);
5316 locations->SetInAt(0, Location::RegisterPairLocation(
5317 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5318 }
5319
5320 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5321 }
5322}
5323
5324void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5325 LocationSummary* locations = conversion->GetLocations();
5326 Primitive::Type result_type = conversion->GetResultType();
5327 Primitive::Type input_type = conversion->GetInputType();
5328 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005329 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005330
5331 DCHECK_NE(input_type, result_type);
5332
5333 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5334 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5335 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5336 Register src = locations->InAt(0).AsRegister<Register>();
5337
Alexey Frunzea871ef12016-06-27 15:20:11 -07005338 if (dst_low != src) {
5339 __ Move(dst_low, src);
5340 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005341 __ Sra(dst_high, src, 31);
5342 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5343 Register dst = locations->Out().AsRegister<Register>();
5344 Register src = (input_type == Primitive::kPrimLong)
5345 ? locations->InAt(0).AsRegisterPairLow<Register>()
5346 : locations->InAt(0).AsRegister<Register>();
5347
5348 switch (result_type) {
5349 case Primitive::kPrimChar:
5350 __ Andi(dst, src, 0xFFFF);
5351 break;
5352 case Primitive::kPrimByte:
5353 if (has_sign_extension) {
5354 __ Seb(dst, src);
5355 } else {
5356 __ Sll(dst, src, 24);
5357 __ Sra(dst, dst, 24);
5358 }
5359 break;
5360 case Primitive::kPrimShort:
5361 if (has_sign_extension) {
5362 __ Seh(dst, src);
5363 } else {
5364 __ Sll(dst, src, 16);
5365 __ Sra(dst, dst, 16);
5366 }
5367 break;
5368 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005369 if (dst != src) {
5370 __ Move(dst, src);
5371 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005372 break;
5373
5374 default:
5375 LOG(FATAL) << "Unexpected type conversion from " << input_type
5376 << " to " << result_type;
5377 }
5378 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005379 if (input_type == Primitive::kPrimLong) {
5380 if (isR6) {
5381 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5382 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5383 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5384 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5385 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5386 __ Mtc1(src_low, FTMP);
5387 __ Mthc1(src_high, FTMP);
5388 if (result_type == Primitive::kPrimFloat) {
5389 __ Cvtsl(dst, FTMP);
5390 } else {
5391 __ Cvtdl(dst, FTMP);
5392 }
5393 } else {
5394 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
5395 : QUICK_ENTRY_POINT(pL2d);
5396 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
5397 : IsDirectEntrypoint(kQuickL2d);
5398 codegen_->InvokeRuntime(entry_offset,
5399 conversion,
5400 conversion->GetDexPc(),
5401 nullptr,
5402 direct);
5403 if (result_type == Primitive::kPrimFloat) {
5404 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5405 } else {
5406 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5407 }
5408 }
5409 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005410 Register src = locations->InAt(0).AsRegister<Register>();
5411 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5412 __ Mtc1(src, FTMP);
5413 if (result_type == Primitive::kPrimFloat) {
5414 __ Cvtsw(dst, FTMP);
5415 } else {
5416 __ Cvtdw(dst, FTMP);
5417 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005418 }
5419 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5420 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005421 if (result_type == Primitive::kPrimLong) {
5422 if (isR6) {
5423 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5424 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5425 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5426 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5427 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5428 MipsLabel truncate;
5429 MipsLabel done;
5430
5431 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5432 // value when the input is either a NaN or is outside of the range of the output type
5433 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5434 // the same result.
5435 //
5436 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5437 // value of the output type if the input is outside of the range after the truncation or
5438 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5439 // results. This matches the desired float/double-to-int/long conversion exactly.
5440 //
5441 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5442 //
5443 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5444 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5445 // even though it must be NAN2008=1 on R6.
5446 //
5447 // The code takes care of the different behaviors by first comparing the input to the
5448 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5449 // If the input is greater than or equal to the minimum, it procedes to the truncate
5450 // instruction, which will handle such an input the same way irrespective of NAN2008.
5451 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5452 // in order to return either zero or the minimum value.
5453 //
5454 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5455 // truncate instruction for MIPS64R6.
5456 if (input_type == Primitive::kPrimFloat) {
5457 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5458 __ LoadConst32(TMP, min_val);
5459 __ Mtc1(TMP, FTMP);
5460 __ CmpLeS(FTMP, FTMP, src);
5461 } else {
5462 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5463 __ LoadConst32(TMP, High32Bits(min_val));
5464 __ Mtc1(ZERO, FTMP);
5465 __ Mthc1(TMP, FTMP);
5466 __ CmpLeD(FTMP, FTMP, src);
5467 }
5468
5469 __ Bc1nez(FTMP, &truncate);
5470
5471 if (input_type == Primitive::kPrimFloat) {
5472 __ CmpEqS(FTMP, src, src);
5473 } else {
5474 __ CmpEqD(FTMP, src, src);
5475 }
5476 __ Move(dst_low, ZERO);
5477 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5478 __ Mfc1(TMP, FTMP);
5479 __ And(dst_high, dst_high, TMP);
5480
5481 __ B(&done);
5482
5483 __ Bind(&truncate);
5484
5485 if (input_type == Primitive::kPrimFloat) {
5486 __ TruncLS(FTMP, src);
5487 } else {
5488 __ TruncLD(FTMP, src);
5489 }
5490 __ Mfc1(dst_low, FTMP);
5491 __ Mfhc1(dst_high, FTMP);
5492
5493 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005494 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005495 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5496 : QUICK_ENTRY_POINT(pD2l);
5497 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5498 : IsDirectEntrypoint(kQuickD2l);
5499 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5500 if (input_type == Primitive::kPrimFloat) {
5501 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5502 } else {
5503 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5504 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005505 }
5506 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005507 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5508 Register dst = locations->Out().AsRegister<Register>();
5509 MipsLabel truncate;
5510 MipsLabel done;
5511
5512 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5513 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5514 // even though it must be NAN2008=1 on R6.
5515 //
5516 // For details see the large comment above for the truncation of float/double to long on R6.
5517 //
5518 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5519 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005520 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005521 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5522 __ LoadConst32(TMP, min_val);
5523 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005524 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005525 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5526 __ LoadConst32(TMP, High32Bits(min_val));
5527 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005528 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005529 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005530
5531 if (isR6) {
5532 if (input_type == Primitive::kPrimFloat) {
5533 __ CmpLeS(FTMP, FTMP, src);
5534 } else {
5535 __ CmpLeD(FTMP, FTMP, src);
5536 }
5537 __ Bc1nez(FTMP, &truncate);
5538
5539 if (input_type == Primitive::kPrimFloat) {
5540 __ CmpEqS(FTMP, src, src);
5541 } else {
5542 __ CmpEqD(FTMP, src, src);
5543 }
5544 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5545 __ Mfc1(TMP, FTMP);
5546 __ And(dst, dst, TMP);
5547 } else {
5548 if (input_type == Primitive::kPrimFloat) {
5549 __ ColeS(0, FTMP, src);
5550 } else {
5551 __ ColeD(0, FTMP, src);
5552 }
5553 __ Bc1t(0, &truncate);
5554
5555 if (input_type == Primitive::kPrimFloat) {
5556 __ CeqS(0, src, src);
5557 } else {
5558 __ CeqD(0, src, src);
5559 }
5560 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5561 __ Movf(dst, ZERO, 0);
5562 }
5563
5564 __ B(&done);
5565
5566 __ Bind(&truncate);
5567
5568 if (input_type == Primitive::kPrimFloat) {
5569 __ TruncWS(FTMP, src);
5570 } else {
5571 __ TruncWD(FTMP, src);
5572 }
5573 __ Mfc1(dst, FTMP);
5574
5575 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005576 }
5577 } else if (Primitive::IsFloatingPointType(result_type) &&
5578 Primitive::IsFloatingPointType(input_type)) {
5579 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5580 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5581 if (result_type == Primitive::kPrimFloat) {
5582 __ Cvtsd(dst, src);
5583 } else {
5584 __ Cvtds(dst, src);
5585 }
5586 } else {
5587 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5588 << " to " << result_type;
5589 }
5590}
5591
5592void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5593 HandleShift(ushr);
5594}
5595
5596void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5597 HandleShift(ushr);
5598}
5599
5600void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5601 HandleBinaryOp(instruction);
5602}
5603
5604void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5605 HandleBinaryOp(instruction);
5606}
5607
5608void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5609 // Nothing to do, this should be removed during prepare for register allocator.
5610 LOG(FATAL) << "Unreachable";
5611}
5612
5613void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5614 // Nothing to do, this should be removed during prepare for register allocator.
5615 LOG(FATAL) << "Unreachable";
5616}
5617
5618void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005619 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005620}
5621
5622void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005623 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005624}
5625
5626void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005627 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005628}
5629
5630void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005631 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005632}
5633
5634void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005635 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005636}
5637
5638void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005639 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005640}
5641
5642void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005643 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005644}
5645
5646void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005647 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005648}
5649
5650void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005651 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005652}
5653
5654void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005655 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005656}
5657
5658void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005659 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005660}
5661
5662void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005663 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005664}
5665
5666void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005667 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005668}
5669
5670void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005671 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005672}
5673
5674void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005675 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005676}
5677
5678void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005679 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005680}
5681
5682void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005683 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005684}
5685
5686void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005687 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005688}
5689
5690void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005691 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005692}
5693
5694void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005695 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005696}
5697
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005698void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5699 LocationSummary* locations =
5700 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5701 locations->SetInAt(0, Location::RequiresRegister());
5702}
5703
5704void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5705 int32_t lower_bound = switch_instr->GetStartValue();
5706 int32_t num_entries = switch_instr->GetNumEntries();
5707 LocationSummary* locations = switch_instr->GetLocations();
5708 Register value_reg = locations->InAt(0).AsRegister<Register>();
5709 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5710
5711 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005712 Register temp_reg = TMP;
5713 __ Addiu32(temp_reg, value_reg, -lower_bound);
5714 // Jump to default if index is negative
5715 // Note: We don't check the case that index is positive while value < lower_bound, because in
5716 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5717 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5718
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005719 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005720 // Jump to successors[0] if value == lower_bound.
5721 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5722 int32_t last_index = 0;
5723 for (; num_entries - last_index > 2; last_index += 2) {
5724 __ Addiu(temp_reg, temp_reg, -2);
5725 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5726 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5727 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5728 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5729 }
5730 if (num_entries - last_index == 2) {
5731 // The last missing case_value.
5732 __ Addiu(temp_reg, temp_reg, -1);
5733 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005734 }
5735
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005736 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005737 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5738 __ B(codegen_->GetLabelOf(default_block));
5739 }
5740}
5741
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005742void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5743 HMipsComputeBaseMethodAddress* insn) {
5744 LocationSummary* locations =
5745 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5746 locations->SetOut(Location::RequiresRegister());
5747}
5748
5749void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5750 HMipsComputeBaseMethodAddress* insn) {
5751 LocationSummary* locations = insn->GetLocations();
5752 Register reg = locations->Out().AsRegister<Register>();
5753
5754 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5755
5756 // Generate a dummy PC-relative call to obtain PC.
5757 __ Nal();
5758 // Grab the return address off RA.
5759 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005760 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005761
5762 // Remember this offset (the obtained PC value) for later use with constant area.
5763 __ BindPcRelBaseLabel();
5764}
5765
5766void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5767 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5768 locations->SetOut(Location::RequiresRegister());
5769}
5770
5771void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5772 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5773 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5774 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
5775
5776 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5777 __ Bind(&info->high_label);
5778 __ Bind(&info->pc_rel_label);
5779 // Add a 32-bit offset to PC.
5780 __ Auipc(reg, /* placeholder */ 0x1234);
5781 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5782 } else {
5783 // Generate a dummy PC-relative call to obtain PC.
5784 __ Nal();
5785 __ Bind(&info->high_label);
5786 __ Lui(reg, /* placeholder */ 0x1234);
5787 __ Bind(&info->pc_rel_label);
5788 __ Ori(reg, reg, /* placeholder */ 0x5678);
5789 // Add a 32-bit offset to PC.
5790 __ Addu(reg, reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005791 // TODO: Can we share this code with that of VisitMipsComputeBaseMethodAddress()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005792 }
5793}
5794
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005795void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5796 // The trampoline uses the same calling convention as dex calling conventions,
5797 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5798 // the method_idx.
5799 HandleInvoke(invoke);
5800}
5801
5802void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5803 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5804}
5805
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005806void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5807 LocationSummary* locations =
5808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5809 locations->SetInAt(0, Location::RequiresRegister());
5810 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005811}
5812
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005813void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5814 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005815 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005816 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005817 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005818 __ LoadFromOffset(kLoadWord,
5819 locations->Out().AsRegister<Register>(),
5820 locations->InAt(0).AsRegister<Register>(),
5821 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005822 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005823 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005824 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005825 __ LoadFromOffset(kLoadWord,
5826 locations->Out().AsRegister<Register>(),
5827 locations->InAt(0).AsRegister<Register>(),
5828 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005829 __ LoadFromOffset(kLoadWord,
5830 locations->Out().AsRegister<Register>(),
5831 locations->Out().AsRegister<Register>(),
5832 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005833 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005834}
5835
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005836#undef __
5837#undef QUICK_ENTRY_POINT
5838
5839} // namespace mips
5840} // namespace art