blob: 119084e026fa6dcdf7faec3b7352ae2b068b7613 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
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_mips64.h"
18
Alexey Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070021#include "entrypoints/quick/quick_entrypoints.h"
22#include "entrypoints/quick/quick_entrypoints_enum.h"
23#include "gc/accounting/card_table.h"
24#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070025#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070026#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "offsets.h"
29#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070030#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "utils/stack_checks.h"
33
34namespace art {
35namespace mips64 {
36
37static constexpr int kCurrentMethodStackOffset = 0;
38static constexpr GpuRegister kMethodRegisterArgument = A0;
39
40// We need extra temporary/scratch registers (in addition to AT) in some cases.
Alexey Frunze4dda3372015-06-01 18:31:49 -070041static constexpr FpuRegister FTMP = F8;
42
Alexey Frunze4dda3372015-06-01 18:31:49 -070043Location Mips64ReturnLocation(Primitive::Type return_type) {
44 switch (return_type) {
45 case Primitive::kPrimBoolean:
46 case Primitive::kPrimByte:
47 case Primitive::kPrimChar:
48 case Primitive::kPrimShort:
49 case Primitive::kPrimInt:
50 case Primitive::kPrimNot:
51 case Primitive::kPrimLong:
52 return Location::RegisterLocation(V0);
53
54 case Primitive::kPrimFloat:
55 case Primitive::kPrimDouble:
56 return Location::FpuRegisterLocation(F0);
57
58 case Primitive::kPrimVoid:
59 return Location();
60 }
61 UNREACHABLE();
62}
63
64Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
65 return Mips64ReturnLocation(type);
66}
67
68Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
69 return Location::RegisterLocation(kMethodRegisterArgument);
70}
71
72Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
73 Location next_location;
74 if (type == Primitive::kPrimVoid) {
75 LOG(FATAL) << "Unexpected parameter type " << type;
76 }
77
78 if (Primitive::IsFloatingPointType(type) &&
79 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
80 next_location = Location::FpuRegisterLocation(
81 calling_convention.GetFpuRegisterAt(float_index_++));
82 gp_index_++;
83 } else if (!Primitive::IsFloatingPointType(type) &&
84 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
85 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
86 float_index_++;
87 } else {
88 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
89 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
90 : Location::StackSlot(stack_offset);
91 }
92
93 // Space on the stack is reserved for all arguments.
94 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
95
96 // TODO: review
97
98 // TODO: shouldn't we use a whole machine word per argument on the stack?
99 // Implicit 4-byte method pointer (and such) will cause misalignment.
100
101 return next_location;
102}
103
104Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
105 return Mips64ReturnLocation(type);
106}
107
108#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
Lazar Trsicd9672662015-09-03 17:33:01 +0200109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700110
111class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
112 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100113 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114
115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100116 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
118 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000119 if (instruction_->CanThrowIntoCatchBlock()) {
120 // Live registers will be restored in the catch block if caught.
121 SaveLiveRegisters(codegen, instruction_->GetLocations());
122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700123 // We're moving two locations to locations that could overlap, so we need a parallel
124 // move resolver.
125 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
128 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100129 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700130 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
131 Primitive::kPrimInt);
132 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
133 instruction_,
134 instruction_->GetDexPc(),
135 this);
136 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
137 }
138
Alexandre Rames8158f282015-08-07 10:26:17 +0100139 bool IsFatal() const OVERRIDE { return true; }
140
Roland Levillain46648892015-06-19 16:07:18 +0100141 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
142
Alexey Frunze4dda3372015-06-01 18:31:49 -0700143 private:
144 HBoundsCheck* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145
146 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
147};
148
149class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
150 public:
151 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : instruction_(instruction) {}
152
153 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
154 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
155 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000156 if (instruction_->CanThrowIntoCatchBlock()) {
157 // Live registers will be restored in the catch block if caught.
158 SaveLiveRegisters(codegen, instruction_->GetLocations());
159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
161 instruction_,
162 instruction_->GetDexPc(),
163 this);
164 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
165 }
166
Alexandre Rames8158f282015-08-07 10:26:17 +0100167 bool IsFatal() const OVERRIDE { return true; }
168
Roland Levillain46648892015-06-19 16:07:18 +0100169 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
170
Alexey Frunze4dda3372015-06-01 18:31:49 -0700171 private:
172 HDivZeroCheck* const instruction_;
173 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
174};
175
176class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
177 public:
178 LoadClassSlowPathMIPS64(HLoadClass* cls,
179 HInstruction* at,
180 uint32_t dex_pc,
181 bool do_clinit)
182 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
185
186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
187 LocationSummary* locations = at_->GetLocations();
188 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
189
190 __ Bind(GetEntryLabel());
191 SaveLiveRegisters(codegen, locations);
192
193 InvokeRuntimeCallingConvention calling_convention;
194 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
195 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
196 : QUICK_ENTRY_POINT(pInitializeType);
197 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
198 if (do_clinit_) {
199 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
200 } else {
201 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
202 }
203
204 // Move the class to the desired location.
205 Location out = locations->Out();
206 if (out.IsValid()) {
207 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
208 Primitive::Type type = at_->GetType();
209 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
210 }
211
212 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700213 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700214 }
215
Roland Levillain46648892015-06-19 16:07:18 +0100216 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
217
Alexey Frunze4dda3372015-06-01 18:31:49 -0700218 private:
219 // The class this slow path will load.
220 HLoadClass* const cls_;
221
222 // The instruction where this slow path is happening.
223 // (Might be the load class or an initialization check).
224 HInstruction* const at_;
225
226 // The dex PC of `at_`.
227 const uint32_t dex_pc_;
228
229 // Whether to initialize the class.
230 const bool do_clinit_;
231
232 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
233};
234
235class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
236 public:
237 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : instruction_(instruction) {}
238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
240 LocationSummary* locations = instruction_->GetLocations();
241 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
242 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
243
244 __ Bind(GetEntryLabel());
245 SaveLiveRegisters(codegen, locations);
246
247 InvokeRuntimeCallingConvention calling_convention;
248 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
249 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
250 instruction_,
251 instruction_->GetDexPc(),
252 this);
253 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
254 Primitive::Type type = instruction_->GetType();
255 mips64_codegen->MoveLocation(locations->Out(),
256 calling_convention.GetReturnLocation(type),
257 type);
258
259 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700260 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 }
262
Roland Levillain46648892015-06-19 16:07:18 +0100263 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
264
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 private:
266 HLoadString* const instruction_;
267
268 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
269};
270
271class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
272 public:
273 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : instruction_(instr) {}
274
275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
276 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
277 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000278 if (instruction_->CanThrowIntoCatchBlock()) {
279 // Live registers will be restored in the catch block if caught.
280 SaveLiveRegisters(codegen, instruction_->GetLocations());
281 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
283 instruction_,
284 instruction_->GetDexPc(),
285 this);
286 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
287 }
288
Alexandre Rames8158f282015-08-07 10:26:17 +0100289 bool IsFatal() const OVERRIDE { return true; }
290
Roland Levillain46648892015-06-19 16:07:18 +0100291 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
292
Alexey Frunze4dda3372015-06-01 18:31:49 -0700293 private:
294 HNullCheck* const instruction_;
295
296 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
297};
298
299class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
300 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100301 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700302 : instruction_(instruction), successor_(successor) {}
303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
307 SaveLiveRegisters(codegen, instruction_->GetLocations());
308 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
309 instruction_,
310 instruction_->GetDexPc(),
311 this);
312 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
313 RestoreLiveRegisters(codegen, instruction_->GetLocations());
314 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700317 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700318 }
319 }
320
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700321 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322 DCHECK(successor_ == nullptr);
323 return &return_label_;
324 }
325
Roland Levillain46648892015-06-19 16:07:18 +0100326 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
327
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328 private:
329 HSuspendCheck* const instruction_;
330 // If not null, the block to branch to after the suspend check.
331 HBasicBlock* const successor_;
332
333 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700334 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
337};
338
339class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
340 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700347 DCHECK(instruction_->IsCheckCast()
348 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
349 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
350
351 __ Bind(GetEntryLabel());
352 SaveLiveRegisters(codegen, locations);
353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100357 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
359 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100360 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
363
364 if (instruction_->IsInstanceOf()) {
365 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
366 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100367 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700368 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000369 CheckEntrypointTypes<
370 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700371 Primitive::Type ret_type = instruction_->GetType();
372 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
373 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700374 } else {
375 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100376 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
378 }
379
380 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700381 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 }
383
Roland Levillain46648892015-06-19 16:07:18 +0100384 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
385
Alexey Frunze4dda3372015-06-01 18:31:49 -0700386 private:
387 HInstruction* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388
389 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
390};
391
392class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
393 public:
Aart Bik42249c32016-01-07 15:33:50 -0800394 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395 : instruction_(instruction) {}
396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800398 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 __ Bind(GetEntryLabel());
400 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800401 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
402 instruction_,
403 instruction_->GetDexPc(),
404 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000405 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 }
407
Roland Levillain46648892015-06-19 16:07:18 +0100408 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
409
Alexey Frunze4dda3372015-06-01 18:31:49 -0700410 private:
Aart Bik42249c32016-01-07 15:33:50 -0800411 HDeoptimize* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700412 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
413};
414
415CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
416 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100417 const CompilerOptions& compiler_options,
418 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700419 : CodeGenerator(graph,
420 kNumberOfGpuRegisters,
421 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000422 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700423 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
424 arraysize(kCoreCalleeSaves)),
425 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
426 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100427 compiler_options,
428 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100429 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700430 location_builder_(graph, this),
431 instruction_visitor_(graph, this),
432 move_resolver_(graph->GetArena(), this),
433 isa_features_(isa_features) {
434 // Save RA (containing the return address) to mimic Quick.
435 AddAllocatedRegister(Location::RegisterLocation(RA));
436}
437
438#undef __
439#define __ down_cast<Mips64Assembler*>(GetAssembler())->
Lazar Trsicd9672662015-09-03 17:33:01 +0200440#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700441
442void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700443 // Ensure that we fix up branches.
444 __ FinalizeCode();
445
446 // Adjust native pc offsets in stack maps.
447 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
448 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
449 uint32_t new_position = __ GetAdjustedPosition(old_position);
450 DCHECK_GE(new_position, old_position);
451 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
452 }
453
454 // Adjust pc offsets for the disassembly information.
455 if (disasm_info_ != nullptr) {
456 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
457 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
458 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
459 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
460 it.second.start = __ GetAdjustedPosition(it.second.start);
461 it.second.end = __ GetAdjustedPosition(it.second.end);
462 }
463 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
464 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
465 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
466 }
467 }
468
Alexey Frunze4dda3372015-06-01 18:31:49 -0700469 CodeGenerator::Finalize(allocator);
470}
471
472Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
473 return codegen_->GetAssembler();
474}
475
476void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100477 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700478 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
479}
480
481void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100482 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700483 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
484}
485
486void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
487 // Pop reg
488 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200489 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700490}
491
492void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
493 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200494 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700495 __ Sd(GpuRegister(reg), SP, 0);
496}
497
498void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
499 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
500 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
501 // Allocate a scratch register other than TMP, if available.
502 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
503 // automatically unspilled when the scratch scope object is destroyed).
504 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
505 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200506 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700507 __ LoadFromOffset(load_type,
508 GpuRegister(ensure_scratch.GetRegister()),
509 SP,
510 index1 + stack_offset);
511 __ LoadFromOffset(load_type,
512 TMP,
513 SP,
514 index2 + stack_offset);
515 __ StoreToOffset(store_type,
516 GpuRegister(ensure_scratch.GetRegister()),
517 SP,
518 index2 + stack_offset);
519 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
520}
521
522static dwarf::Reg DWARFReg(GpuRegister reg) {
523 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
524}
525
David Srbeckyba702002016-02-01 18:15:29 +0000526static dwarf::Reg DWARFReg(FpuRegister reg) {
527 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
528}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700529
530void CodeGeneratorMIPS64::GenerateFrameEntry() {
531 __ Bind(&frame_entry_label_);
532
533 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
534
535 if (do_overflow_check) {
536 __ LoadFromOffset(kLoadWord,
537 ZERO,
538 SP,
539 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
540 RecordPcInfo(nullptr, 0);
541 }
542
543 // TODO: anything related to T9/GP/GOT/PIC/.so's?
544
545 if (HasEmptyFrame()) {
546 return;
547 }
548
549 // Make sure the frame size isn't unreasonably large. Per the various APIs
550 // it looks like it should always be less than 2GB in size, which allows
551 // us using 32-bit signed offsets from the stack pointer.
552 if (GetFrameSize() > 0x7FFFFFFF)
553 LOG(FATAL) << "Stack frame larger than 2GB";
554
555 // Spill callee-saved registers.
556 // Note that their cumulative size is small and they can be indexed using
557 // 16-bit offsets.
558
559 // TODO: increment/decrement SP in one step instead of two or remove this comment.
560
561 uint32_t ofs = FrameEntrySpillSize();
562 __ IncreaseFrameSize(ofs);
563
564 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
565 GpuRegister reg = kCoreCalleeSaves[i];
566 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200567 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700568 __ Sd(reg, SP, ofs);
569 __ cfi().RelOffset(DWARFReg(reg), ofs);
570 }
571 }
572
573 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
574 FpuRegister reg = kFpuCalleeSaves[i];
575 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200576 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700577 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000578 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700579 }
580 }
581
582 // Allocate the rest of the frame and store the current method pointer
583 // at its end.
584
585 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
586
587 static_assert(IsInt<16>(kCurrentMethodStackOffset),
588 "kCurrentMethodStackOffset must fit into int16_t");
589 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
590}
591
592void CodeGeneratorMIPS64::GenerateFrameExit() {
593 __ cfi().RememberState();
594
595 // TODO: anything related to T9/GP/GOT/PIC/.so's?
596
597 if (!HasEmptyFrame()) {
598 // Deallocate the rest of the frame.
599
600 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
601
602 // Restore callee-saved registers.
603 // Note that their cumulative size is small and they can be indexed using
604 // 16-bit offsets.
605
606 // TODO: increment/decrement SP in one step instead of two or remove this comment.
607
608 uint32_t ofs = 0;
609
610 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
611 FpuRegister reg = kFpuCalleeSaves[i];
612 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
613 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200614 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000615 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700616 }
617 }
618
619 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
620 GpuRegister reg = kCoreCalleeSaves[i];
621 if (allocated_registers_.ContainsCoreRegister(reg)) {
622 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200623 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700624 __ cfi().Restore(DWARFReg(reg));
625 }
626 }
627
628 DCHECK_EQ(ofs, FrameEntrySpillSize());
629 __ DecreaseFrameSize(ofs);
630 }
631
632 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700633 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700634
635 __ cfi().RestoreState();
636 __ cfi().DefCFAOffset(GetFrameSize());
637}
638
639void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
640 __ Bind(GetLabelOf(block));
641}
642
643void CodeGeneratorMIPS64::MoveLocation(Location destination,
644 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100645 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700646 if (source.Equals(destination)) {
647 return;
648 }
649
650 // A valid move can always be inferred from the destination and source
651 // locations. When moving from and to a register, the argument type can be
652 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100653 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700654 DCHECK_EQ(unspecified_type, false);
655
656 if (destination.IsRegister() || destination.IsFpuRegister()) {
657 if (unspecified_type) {
658 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
659 if (source.IsStackSlot() ||
660 (src_cst != nullptr && (src_cst->IsIntConstant()
661 || src_cst->IsFloatConstant()
662 || src_cst->IsNullConstant()))) {
663 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100664 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700665 } else {
666 // If the source is a double stack slot or a 64bit constant, a 64bit
667 // type is appropriate. Else the source is a register, and since the
668 // type has not been specified, we chose a 64bit type to force a 64bit
669 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100670 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700671 }
672 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100673 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
674 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700675 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
676 // Move to GPR/FPR from stack
677 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100678 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700679 __ LoadFpuFromOffset(load_type,
680 destination.AsFpuRegister<FpuRegister>(),
681 SP,
682 source.GetStackIndex());
683 } else {
684 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
685 __ LoadFromOffset(load_type,
686 destination.AsRegister<GpuRegister>(),
687 SP,
688 source.GetStackIndex());
689 }
690 } else if (source.IsConstant()) {
691 // Move to GPR/FPR from constant
692 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100693 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700694 gpr = destination.AsRegister<GpuRegister>();
695 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100696 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700697 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
698 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
699 gpr = ZERO;
700 } else {
701 __ LoadConst32(gpr, value);
702 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700703 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700704 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
705 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
706 gpr = ZERO;
707 } else {
708 __ LoadConst64(gpr, value);
709 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700710 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700712 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100713 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700714 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
715 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100716 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700717 if (destination.IsRegister()) {
718 // Move to GPR from GPR
719 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
720 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100721 DCHECK(destination.IsFpuRegister());
722 if (Primitive::Is64BitType(dst_type)) {
723 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
724 } else {
725 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
726 }
727 }
728 } else if (source.IsFpuRegister()) {
729 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100731 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700732 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
733 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100734 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700735 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
736 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100737 } else {
738 DCHECK(destination.IsRegister());
739 if (Primitive::Is64BitType(dst_type)) {
740 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
741 } else {
742 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
743 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700744 }
745 }
746 } else { // The destination is not a register. It must be a stack slot.
747 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
748 if (source.IsRegister() || source.IsFpuRegister()) {
749 if (unspecified_type) {
750 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100751 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100753 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700754 }
755 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100756 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
757 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700758 // Move to stack from GPR/FPR
759 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
760 if (source.IsRegister()) {
761 __ StoreToOffset(store_type,
762 source.AsRegister<GpuRegister>(),
763 SP,
764 destination.GetStackIndex());
765 } else {
766 __ StoreFpuToOffset(store_type,
767 source.AsFpuRegister<FpuRegister>(),
768 SP,
769 destination.GetStackIndex());
770 }
771 } else if (source.IsConstant()) {
772 // Move to stack from constant
773 HConstant* src_cst = source.GetConstant();
774 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700775 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700776 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700777 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
778 if (value != 0) {
779 gpr = TMP;
780 __ LoadConst32(gpr, value);
781 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700782 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700783 DCHECK(destination.IsDoubleStackSlot());
784 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
785 if (value != 0) {
786 gpr = TMP;
787 __ LoadConst64(gpr, value);
788 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700789 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700790 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700791 } else {
792 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
793 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
794 // Move to stack from stack
795 if (destination.IsStackSlot()) {
796 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
797 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
798 } else {
799 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
800 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
801 }
802 }
803 }
804}
805
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700806void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700807 DCHECK(!loc1.IsConstant());
808 DCHECK(!loc2.IsConstant());
809
810 if (loc1.Equals(loc2)) {
811 return;
812 }
813
814 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
815 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
816 bool is_fp_reg1 = loc1.IsFpuRegister();
817 bool is_fp_reg2 = loc2.IsFpuRegister();
818
819 if (loc2.IsRegister() && loc1.IsRegister()) {
820 // Swap 2 GPRs
821 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
822 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
823 __ Move(TMP, r2);
824 __ Move(r2, r1);
825 __ Move(r1, TMP);
826 } else if (is_fp_reg2 && is_fp_reg1) {
827 // Swap 2 FPRs
828 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
829 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700830 if (type == Primitive::kPrimFloat) {
831 __ MovS(FTMP, r1);
832 __ MovS(r1, r2);
833 __ MovS(r2, FTMP);
834 } else {
835 DCHECK_EQ(type, Primitive::kPrimDouble);
836 __ MovD(FTMP, r1);
837 __ MovD(r1, r2);
838 __ MovD(r2, FTMP);
839 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700840 } else if (is_slot1 != is_slot2) {
841 // Swap GPR/FPR and stack slot
842 Location reg_loc = is_slot1 ? loc2 : loc1;
843 Location mem_loc = is_slot1 ? loc1 : loc2;
844 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
845 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
846 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
847 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
848 if (reg_loc.IsFpuRegister()) {
849 __ StoreFpuToOffset(store_type,
850 reg_loc.AsFpuRegister<FpuRegister>(),
851 SP,
852 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700853 if (mem_loc.IsStackSlot()) {
854 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
855 } else {
856 DCHECK(mem_loc.IsDoubleStackSlot());
857 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
858 }
859 } else {
860 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
861 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
862 }
863 } else if (is_slot1 && is_slot2) {
864 move_resolver_.Exchange(loc1.GetStackIndex(),
865 loc2.GetStackIndex(),
866 loc1.IsDoubleStackSlot());
867 } else {
868 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
869 }
870}
871
Calin Juravle175dc732015-08-25 15:42:32 +0100872void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
873 DCHECK(location.IsRegister());
874 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
875}
876
Calin Juravlee460d1d2015-09-29 04:52:17 +0100877void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
878 if (location.IsRegister()) {
879 locations->AddTemp(location);
880 } else {
881 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
882 }
883}
884
Alexey Frunze4dda3372015-06-01 18:31:49 -0700885Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
886 Primitive::Type type = load->GetType();
887
888 switch (type) {
889 case Primitive::kPrimNot:
890 case Primitive::kPrimInt:
891 case Primitive::kPrimFloat:
892 return Location::StackSlot(GetStackSlot(load->GetLocal()));
893
894 case Primitive::kPrimLong:
895 case Primitive::kPrimDouble:
896 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
897
898 case Primitive::kPrimBoolean:
899 case Primitive::kPrimByte:
900 case Primitive::kPrimChar:
901 case Primitive::kPrimShort:
902 case Primitive::kPrimVoid:
903 LOG(FATAL) << "Unexpected type " << type;
904 }
905
906 LOG(FATAL) << "Unreachable";
907 return Location::NoLocation();
908}
909
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100910void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
911 GpuRegister value,
912 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700913 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700914 GpuRegister card = AT;
915 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100916 if (value_can_be_null) {
917 __ Beqzc(value, &done);
918 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700919 __ LoadFromOffset(kLoadDoubleword,
920 card,
921 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200922 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700923 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
924 __ Daddu(temp, card, temp);
925 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100926 if (value_can_be_null) {
927 __ Bind(&done);
928 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700929}
930
David Brazdil58282f42016-01-14 12:45:10 +0000931void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700932 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
933 blocked_core_registers_[ZERO] = true;
934 blocked_core_registers_[K0] = true;
935 blocked_core_registers_[K1] = true;
936 blocked_core_registers_[GP] = true;
937 blocked_core_registers_[SP] = true;
938 blocked_core_registers_[RA] = true;
939
Lazar Trsicd9672662015-09-03 17:33:01 +0200940 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
941 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700942 blocked_core_registers_[AT] = true;
943 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200944 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700945 blocked_fpu_registers_[FTMP] = true;
946
947 // Reserve suspend and thread registers.
948 blocked_core_registers_[S0] = true;
949 blocked_core_registers_[TR] = true;
950
951 // Reserve T9 for function calls
952 blocked_core_registers_[T9] = true;
953
954 // TODO: review; anything else?
955
David Brazdil58282f42016-01-14 12:45:10 +0000956 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700957 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
958 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
959 }
960
961 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
962 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
963 }
964}
965
Alexey Frunze4dda3372015-06-01 18:31:49 -0700966size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
967 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200968 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700969}
970
971size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
972 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200973 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700974}
975
976size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
977 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200978 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700979}
980
981size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
982 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200983 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700984}
985
986void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100987 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700988}
989
990void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100991 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700992}
993
Calin Juravle175dc732015-08-25 15:42:32 +0100994void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
995 HInstruction* instruction,
996 uint32_t dex_pc,
997 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200998 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100999 instruction,
1000 dex_pc,
1001 slow_path);
1002}
1003
Alexey Frunze4dda3372015-06-01 18:31:49 -07001004void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
1005 HInstruction* instruction,
1006 uint32_t dex_pc,
1007 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001008 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001009 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1010 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1011 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001012 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001013 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001014}
1015
1016void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1017 GpuRegister class_reg) {
1018 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1019 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1020 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1021 // TODO: barrier needed?
1022 __ Bind(slow_path->GetExitLabel());
1023}
1024
1025void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1026 __ Sync(0); // only stype 0 is supported
1027}
1028
1029void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1030 HBasicBlock* successor) {
1031 SuspendCheckSlowPathMIPS64* slow_path =
1032 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1033 codegen_->AddSlowPath(slow_path);
1034
1035 __ LoadFromOffset(kLoadUnsignedHalfword,
1036 TMP,
1037 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001038 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001039 if (successor == nullptr) {
1040 __ Bnezc(TMP, slow_path->GetEntryLabel());
1041 __ Bind(slow_path->GetReturnLabel());
1042 } else {
1043 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001044 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001045 // slow_path will return to GetLabelOf(successor).
1046 }
1047}
1048
1049InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1050 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001051 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001052 assembler_(codegen->GetAssembler()),
1053 codegen_(codegen) {}
1054
1055void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1056 DCHECK_EQ(instruction->InputCount(), 2U);
1057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1058 Primitive::Type type = instruction->GetResultType();
1059 switch (type) {
1060 case Primitive::kPrimInt:
1061 case Primitive::kPrimLong: {
1062 locations->SetInAt(0, Location::RequiresRegister());
1063 HInstruction* right = instruction->InputAt(1);
1064 bool can_use_imm = false;
1065 if (right->IsConstant()) {
1066 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1067 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1068 can_use_imm = IsUint<16>(imm);
1069 } else if (instruction->IsAdd()) {
1070 can_use_imm = IsInt<16>(imm);
1071 } else {
1072 DCHECK(instruction->IsSub());
1073 can_use_imm = IsInt<16>(-imm);
1074 }
1075 }
1076 if (can_use_imm)
1077 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1078 else
1079 locations->SetInAt(1, Location::RequiresRegister());
1080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1081 }
1082 break;
1083
1084 case Primitive::kPrimFloat:
1085 case Primitive::kPrimDouble:
1086 locations->SetInAt(0, Location::RequiresFpuRegister());
1087 locations->SetInAt(1, Location::RequiresFpuRegister());
1088 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1089 break;
1090
1091 default:
1092 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1093 }
1094}
1095
1096void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1097 Primitive::Type type = instruction->GetType();
1098 LocationSummary* locations = instruction->GetLocations();
1099
1100 switch (type) {
1101 case Primitive::kPrimInt:
1102 case Primitive::kPrimLong: {
1103 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1104 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1105 Location rhs_location = locations->InAt(1);
1106
1107 GpuRegister rhs_reg = ZERO;
1108 int64_t rhs_imm = 0;
1109 bool use_imm = rhs_location.IsConstant();
1110 if (use_imm) {
1111 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1112 } else {
1113 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1114 }
1115
1116 if (instruction->IsAnd()) {
1117 if (use_imm)
1118 __ Andi(dst, lhs, rhs_imm);
1119 else
1120 __ And(dst, lhs, rhs_reg);
1121 } else if (instruction->IsOr()) {
1122 if (use_imm)
1123 __ Ori(dst, lhs, rhs_imm);
1124 else
1125 __ Or(dst, lhs, rhs_reg);
1126 } else if (instruction->IsXor()) {
1127 if (use_imm)
1128 __ Xori(dst, lhs, rhs_imm);
1129 else
1130 __ Xor(dst, lhs, rhs_reg);
1131 } else if (instruction->IsAdd()) {
1132 if (type == Primitive::kPrimInt) {
1133 if (use_imm)
1134 __ Addiu(dst, lhs, rhs_imm);
1135 else
1136 __ Addu(dst, lhs, rhs_reg);
1137 } else {
1138 if (use_imm)
1139 __ Daddiu(dst, lhs, rhs_imm);
1140 else
1141 __ Daddu(dst, lhs, rhs_reg);
1142 }
1143 } else {
1144 DCHECK(instruction->IsSub());
1145 if (type == Primitive::kPrimInt) {
1146 if (use_imm)
1147 __ Addiu(dst, lhs, -rhs_imm);
1148 else
1149 __ Subu(dst, lhs, rhs_reg);
1150 } else {
1151 if (use_imm)
1152 __ Daddiu(dst, lhs, -rhs_imm);
1153 else
1154 __ Dsubu(dst, lhs, rhs_reg);
1155 }
1156 }
1157 break;
1158 }
1159 case Primitive::kPrimFloat:
1160 case Primitive::kPrimDouble: {
1161 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1162 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1163 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1164 if (instruction->IsAdd()) {
1165 if (type == Primitive::kPrimFloat)
1166 __ AddS(dst, lhs, rhs);
1167 else
1168 __ AddD(dst, lhs, rhs);
1169 } else if (instruction->IsSub()) {
1170 if (type == Primitive::kPrimFloat)
1171 __ SubS(dst, lhs, rhs);
1172 else
1173 __ SubD(dst, lhs, rhs);
1174 } else {
1175 LOG(FATAL) << "Unexpected floating-point binary operation";
1176 }
1177 break;
1178 }
1179 default:
1180 LOG(FATAL) << "Unexpected binary operation type " << type;
1181 }
1182}
1183
1184void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001185 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001186
1187 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1188 Primitive::Type type = instr->GetResultType();
1189 switch (type) {
1190 case Primitive::kPrimInt:
1191 case Primitive::kPrimLong: {
1192 locations->SetInAt(0, Location::RequiresRegister());
1193 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001194 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001195 break;
1196 }
1197 default:
1198 LOG(FATAL) << "Unexpected shift type " << type;
1199 }
1200}
1201
1202void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001203 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001204 LocationSummary* locations = instr->GetLocations();
1205 Primitive::Type type = instr->GetType();
1206
1207 switch (type) {
1208 case Primitive::kPrimInt:
1209 case Primitive::kPrimLong: {
1210 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1211 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1212 Location rhs_location = locations->InAt(1);
1213
1214 GpuRegister rhs_reg = ZERO;
1215 int64_t rhs_imm = 0;
1216 bool use_imm = rhs_location.IsConstant();
1217 if (use_imm) {
1218 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1219 } else {
1220 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1221 }
1222
1223 if (use_imm) {
1224 uint32_t shift_value = (type == Primitive::kPrimInt)
1225 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1226 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1227
Alexey Frunze92d90602015-12-18 18:16:36 -08001228 if (shift_value == 0) {
1229 if (dst != lhs) {
1230 __ Move(dst, lhs);
1231 }
1232 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001233 if (instr->IsShl()) {
1234 __ Sll(dst, lhs, shift_value);
1235 } else if (instr->IsShr()) {
1236 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001237 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001238 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001239 } else {
1240 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001241 }
1242 } else {
1243 if (shift_value < 32) {
1244 if (instr->IsShl()) {
1245 __ Dsll(dst, lhs, shift_value);
1246 } else if (instr->IsShr()) {
1247 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001248 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001249 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001250 } else {
1251 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001252 }
1253 } else {
1254 shift_value -= 32;
1255 if (instr->IsShl()) {
1256 __ Dsll32(dst, lhs, shift_value);
1257 } else if (instr->IsShr()) {
1258 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001259 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001260 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001261 } else {
1262 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001263 }
1264 }
1265 }
1266 } else {
1267 if (type == Primitive::kPrimInt) {
1268 if (instr->IsShl()) {
1269 __ Sllv(dst, lhs, rhs_reg);
1270 } else if (instr->IsShr()) {
1271 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001272 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001273 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001274 } else {
1275 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001276 }
1277 } else {
1278 if (instr->IsShl()) {
1279 __ Dsllv(dst, lhs, rhs_reg);
1280 } else if (instr->IsShr()) {
1281 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001282 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001283 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001284 } else {
1285 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001286 }
1287 }
1288 }
1289 break;
1290 }
1291 default:
1292 LOG(FATAL) << "Unexpected shift operation type " << type;
1293 }
1294}
1295
1296void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1297 HandleBinaryOp(instruction);
1298}
1299
1300void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1301 HandleBinaryOp(instruction);
1302}
1303
1304void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1305 HandleBinaryOp(instruction);
1306}
1307
1308void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1309 HandleBinaryOp(instruction);
1310}
1311
1312void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1313 LocationSummary* locations =
1314 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1315 locations->SetInAt(0, Location::RequiresRegister());
1316 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1317 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1318 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1319 } else {
1320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1321 }
1322}
1323
1324void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1325 LocationSummary* locations = instruction->GetLocations();
1326 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1327 Location index = locations->InAt(1);
1328 Primitive::Type type = instruction->GetType();
1329
1330 switch (type) {
1331 case Primitive::kPrimBoolean: {
1332 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1333 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1334 if (index.IsConstant()) {
1335 size_t offset =
1336 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1337 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1338 } else {
1339 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1340 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1341 }
1342 break;
1343 }
1344
1345 case Primitive::kPrimByte: {
1346 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1347 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1348 if (index.IsConstant()) {
1349 size_t offset =
1350 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1351 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1352 } else {
1353 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1354 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1355 }
1356 break;
1357 }
1358
1359 case Primitive::kPrimShort: {
1360 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1361 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1362 if (index.IsConstant()) {
1363 size_t offset =
1364 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1365 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1366 } else {
1367 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1368 __ Daddu(TMP, obj, TMP);
1369 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1370 }
1371 break;
1372 }
1373
1374 case Primitive::kPrimChar: {
1375 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1376 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1377 if (index.IsConstant()) {
1378 size_t offset =
1379 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1380 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1381 } else {
1382 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1383 __ Daddu(TMP, obj, TMP);
1384 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1385 }
1386 break;
1387 }
1388
1389 case Primitive::kPrimInt:
1390 case Primitive::kPrimNot: {
1391 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1392 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1393 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1394 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1395 if (index.IsConstant()) {
1396 size_t offset =
1397 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1398 __ LoadFromOffset(load_type, out, obj, offset);
1399 } else {
1400 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1401 __ Daddu(TMP, obj, TMP);
1402 __ LoadFromOffset(load_type, out, TMP, data_offset);
1403 }
1404 break;
1405 }
1406
1407 case Primitive::kPrimLong: {
1408 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1409 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1410 if (index.IsConstant()) {
1411 size_t offset =
1412 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1413 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1414 } else {
1415 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1416 __ Daddu(TMP, obj, TMP);
1417 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1418 }
1419 break;
1420 }
1421
1422 case Primitive::kPrimFloat: {
1423 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1424 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1425 if (index.IsConstant()) {
1426 size_t offset =
1427 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1428 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1429 } else {
1430 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1431 __ Daddu(TMP, obj, TMP);
1432 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1433 }
1434 break;
1435 }
1436
1437 case Primitive::kPrimDouble: {
1438 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1439 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1440 if (index.IsConstant()) {
1441 size_t offset =
1442 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1443 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1444 } else {
1445 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1446 __ Daddu(TMP, obj, TMP);
1447 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1448 }
1449 break;
1450 }
1451
1452 case Primitive::kPrimVoid:
1453 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1454 UNREACHABLE();
1455 }
1456 codegen_->MaybeRecordImplicitNullCheck(instruction);
1457}
1458
1459void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1461 locations->SetInAt(0, Location::RequiresRegister());
1462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1463}
1464
1465void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1466 LocationSummary* locations = instruction->GetLocations();
1467 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1468 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1469 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1470 __ LoadFromOffset(kLoadWord, out, obj, offset);
1471 codegen_->MaybeRecordImplicitNullCheck(instruction);
1472}
1473
1474void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001475 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1477 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001478 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1479 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001480 InvokeRuntimeCallingConvention calling_convention;
1481 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1482 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1483 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1484 } else {
1485 locations->SetInAt(0, Location::RequiresRegister());
1486 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1487 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1488 locations->SetInAt(2, Location::RequiresFpuRegister());
1489 } else {
1490 locations->SetInAt(2, Location::RequiresRegister());
1491 }
1492 }
1493}
1494
1495void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1496 LocationSummary* locations = instruction->GetLocations();
1497 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1498 Location index = locations->InAt(1);
1499 Primitive::Type value_type = instruction->GetComponentType();
1500 bool needs_runtime_call = locations->WillCall();
1501 bool needs_write_barrier =
1502 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1503
1504 switch (value_type) {
1505 case Primitive::kPrimBoolean:
1506 case Primitive::kPrimByte: {
1507 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1508 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1509 if (index.IsConstant()) {
1510 size_t offset =
1511 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1512 __ StoreToOffset(kStoreByte, value, obj, offset);
1513 } else {
1514 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1515 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1516 }
1517 break;
1518 }
1519
1520 case Primitive::kPrimShort:
1521 case Primitive::kPrimChar: {
1522 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1523 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1524 if (index.IsConstant()) {
1525 size_t offset =
1526 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1527 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1528 } else {
1529 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1530 __ Daddu(TMP, obj, TMP);
1531 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1532 }
1533 break;
1534 }
1535
1536 case Primitive::kPrimInt:
1537 case Primitive::kPrimNot: {
1538 if (!needs_runtime_call) {
1539 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1540 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1541 if (index.IsConstant()) {
1542 size_t offset =
1543 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1544 __ StoreToOffset(kStoreWord, value, obj, offset);
1545 } else {
1546 DCHECK(index.IsRegister()) << index;
1547 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1548 __ Daddu(TMP, obj, TMP);
1549 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1550 }
1551 codegen_->MaybeRecordImplicitNullCheck(instruction);
1552 if (needs_write_barrier) {
1553 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001554 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001555 }
1556 } else {
1557 DCHECK_EQ(value_type, Primitive::kPrimNot);
1558 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1559 instruction,
1560 instruction->GetDexPc(),
1561 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001562 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001563 }
1564 break;
1565 }
1566
1567 case Primitive::kPrimLong: {
1568 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1569 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1570 if (index.IsConstant()) {
1571 size_t offset =
1572 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1573 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1574 } else {
1575 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1576 __ Daddu(TMP, obj, TMP);
1577 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1578 }
1579 break;
1580 }
1581
1582 case Primitive::kPrimFloat: {
1583 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1584 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1585 DCHECK(locations->InAt(2).IsFpuRegister());
1586 if (index.IsConstant()) {
1587 size_t offset =
1588 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1589 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1590 } else {
1591 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1592 __ Daddu(TMP, obj, TMP);
1593 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1594 }
1595 break;
1596 }
1597
1598 case Primitive::kPrimDouble: {
1599 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1600 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1601 DCHECK(locations->InAt(2).IsFpuRegister());
1602 if (index.IsConstant()) {
1603 size_t offset =
1604 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1605 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1606 } else {
1607 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1608 __ Daddu(TMP, obj, TMP);
1609 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1610 }
1611 break;
1612 }
1613
1614 case Primitive::kPrimVoid:
1615 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1616 UNREACHABLE();
1617 }
1618
1619 // Ints and objects are handled in the switch.
1620 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1621 codegen_->MaybeRecordImplicitNullCheck(instruction);
1622 }
1623}
1624
1625void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001626 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1627 ? LocationSummary::kCallOnSlowPath
1628 : LocationSummary::kNoCall;
1629 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001630 locations->SetInAt(0, Location::RequiresRegister());
1631 locations->SetInAt(1, Location::RequiresRegister());
1632 if (instruction->HasUses()) {
1633 locations->SetOut(Location::SameAsFirstInput());
1634 }
1635}
1636
1637void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1638 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001639 BoundsCheckSlowPathMIPS64* slow_path =
1640 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001641 codegen_->AddSlowPath(slow_path);
1642
1643 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1644 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1645
1646 // length is limited by the maximum positive signed 32-bit integer.
1647 // Unsigned comparison of length and index checks for index < 0
1648 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001649 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001650}
1651
1652void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1653 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1654 instruction,
1655 LocationSummary::kCallOnSlowPath);
1656 locations->SetInAt(0, Location::RequiresRegister());
1657 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001658 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001659 locations->AddTemp(Location::RequiresRegister());
1660}
1661
1662void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1663 LocationSummary* locations = instruction->GetLocations();
1664 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1665 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1666 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1667
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001668 SlowPathCodeMIPS64* slow_path =
1669 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001670 codegen_->AddSlowPath(slow_path);
1671
1672 // TODO: avoid this check if we know obj is not null.
1673 __ Beqzc(obj, slow_path->GetExitLabel());
1674 // Compare the class of `obj` with `cls`.
1675 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1676 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1677 __ Bind(slow_path->GetExitLabel());
1678}
1679
1680void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1681 LocationSummary* locations =
1682 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1683 locations->SetInAt(0, Location::RequiresRegister());
1684 if (check->HasUses()) {
1685 locations->SetOut(Location::SameAsFirstInput());
1686 }
1687}
1688
1689void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1690 // We assume the class is not null.
1691 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1692 check->GetLoadClass(),
1693 check,
1694 check->GetDexPc(),
1695 true);
1696 codegen_->AddSlowPath(slow_path);
1697 GenerateClassInitializationCheck(slow_path,
1698 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1699}
1700
1701void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1702 Primitive::Type in_type = compare->InputAt(0)->GetType();
1703
Alexey Frunze299a9392015-12-08 16:08:02 -08001704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001705
1706 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08001707 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001708 case Primitive::kPrimLong:
1709 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001710 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1712 break;
1713
1714 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001715 case Primitive::kPrimDouble:
1716 locations->SetInAt(0, Location::RequiresFpuRegister());
1717 locations->SetInAt(1, Location::RequiresFpuRegister());
1718 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001719 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001720
1721 default:
1722 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1723 }
1724}
1725
1726void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1727 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001728 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001729 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08001730 bool gt_bias = instruction->IsGtBias();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001731
1732 // 0 if: left == right
1733 // 1 if: left > right
1734 // -1 if: left < right
1735 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08001736 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001737 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001738 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001739 Location rhs_location = locations->InAt(1);
1740 bool use_imm = rhs_location.IsConstant();
1741 GpuRegister rhs = ZERO;
1742 if (use_imm) {
Aart Bika19616e2016-02-01 18:57:58 -08001743 if (in_type == Primitive::kPrimInt) {
1744 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1745 if (value != 0) {
1746 rhs = AT;
1747 __ LoadConst32(rhs, value);
1748 }
1749 } else {
1750 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1751 if (value != 0) {
1752 rhs = AT;
1753 __ LoadConst64(rhs, value);
1754 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001755 }
1756 } else {
1757 rhs = rhs_location.AsRegister<GpuRegister>();
1758 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001759 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001760 __ Slt(res, rhs, lhs);
1761 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001762 break;
1763 }
1764
Alexey Frunze299a9392015-12-08 16:08:02 -08001765 case Primitive::kPrimFloat: {
1766 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1767 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1768 Mips64Label done;
1769 __ CmpEqS(FTMP, lhs, rhs);
1770 __ LoadConst32(res, 0);
1771 __ Bc1nez(FTMP, &done);
1772 if (gt_bias) {
1773 __ CmpLtS(FTMP, lhs, rhs);
1774 __ LoadConst32(res, -1);
1775 __ Bc1nez(FTMP, &done);
1776 __ LoadConst32(res, 1);
1777 } else {
1778 __ CmpLtS(FTMP, rhs, lhs);
1779 __ LoadConst32(res, 1);
1780 __ Bc1nez(FTMP, &done);
1781 __ LoadConst32(res, -1);
1782 }
1783 __ Bind(&done);
1784 break;
1785 }
1786
Alexey Frunze4dda3372015-06-01 18:31:49 -07001787 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001788 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1789 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1790 Mips64Label done;
1791 __ CmpEqD(FTMP, lhs, rhs);
1792 __ LoadConst32(res, 0);
1793 __ Bc1nez(FTMP, &done);
1794 if (gt_bias) {
1795 __ CmpLtD(FTMP, lhs, rhs);
1796 __ LoadConst32(res, -1);
1797 __ Bc1nez(FTMP, &done);
1798 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001799 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001800 __ CmpLtD(FTMP, rhs, lhs);
1801 __ LoadConst32(res, 1);
1802 __ Bc1nez(FTMP, &done);
1803 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001804 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001805 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001806 break;
1807 }
1808
1809 default:
1810 LOG(FATAL) << "Unimplemented compare type " << in_type;
1811 }
1812}
1813
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001816 switch (instruction->InputAt(0)->GetType()) {
1817 default:
1818 case Primitive::kPrimLong:
1819 locations->SetInAt(0, Location::RequiresRegister());
1820 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1821 break;
1822
1823 case Primitive::kPrimFloat:
1824 case Primitive::kPrimDouble:
1825 locations->SetInAt(0, Location::RequiresFpuRegister());
1826 locations->SetInAt(1, Location::RequiresFpuRegister());
1827 break;
1828 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001829 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001830 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1831 }
1832}
1833
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001834void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001835 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001836 return;
1837 }
1838
Alexey Frunze299a9392015-12-08 16:08:02 -08001839 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001840 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001841 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001842 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001843
Alexey Frunze299a9392015-12-08 16:08:02 -08001844 switch (type) {
1845 default:
1846 // Integer case.
1847 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1848 return;
1849 case Primitive::kPrimLong:
1850 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1851 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001852
Alexey Frunze299a9392015-12-08 16:08:02 -08001853 case Primitive::kPrimFloat:
1854 case Primitive::kPrimDouble:
1855 // TODO: don't use branches.
1856 GenerateFpCompareAndBranch(instruction->GetCondition(),
1857 instruction->IsGtBias(),
1858 type,
1859 locations,
1860 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001861 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001862 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001863
1864 // Convert the branches into the result.
1865 Mips64Label done;
1866
1867 // False case: result = 0.
1868 __ LoadConst32(dst, 0);
1869 __ Bc(&done);
1870
1871 // True case: result = 1.
1872 __ Bind(&true_label);
1873 __ LoadConst32(dst, 1);
1874 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001875}
1876
Alexey Frunzec857c742015-09-23 15:12:39 -07001877void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1878 DCHECK(instruction->IsDiv() || instruction->IsRem());
1879 Primitive::Type type = instruction->GetResultType();
1880
1881 LocationSummary* locations = instruction->GetLocations();
1882 Location second = locations->InAt(1);
1883 DCHECK(second.IsConstant());
1884
1885 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1886 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1887 int64_t imm = Int64FromConstant(second.GetConstant());
1888 DCHECK(imm == 1 || imm == -1);
1889
1890 if (instruction->IsRem()) {
1891 __ Move(out, ZERO);
1892 } else {
1893 if (imm == -1) {
1894 if (type == Primitive::kPrimInt) {
1895 __ Subu(out, ZERO, dividend);
1896 } else {
1897 DCHECK_EQ(type, Primitive::kPrimLong);
1898 __ Dsubu(out, ZERO, dividend);
1899 }
1900 } else if (out != dividend) {
1901 __ Move(out, dividend);
1902 }
1903 }
1904}
1905
1906void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1907 DCHECK(instruction->IsDiv() || instruction->IsRem());
1908 Primitive::Type type = instruction->GetResultType();
1909
1910 LocationSummary* locations = instruction->GetLocations();
1911 Location second = locations->InAt(1);
1912 DCHECK(second.IsConstant());
1913
1914 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1915 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1916 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001917 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001918 int ctz_imm = CTZ(abs_imm);
1919
1920 if (instruction->IsDiv()) {
1921 if (type == Primitive::kPrimInt) {
1922 if (ctz_imm == 1) {
1923 // Fast path for division by +/-2, which is very common.
1924 __ Srl(TMP, dividend, 31);
1925 } else {
1926 __ Sra(TMP, dividend, 31);
1927 __ Srl(TMP, TMP, 32 - ctz_imm);
1928 }
1929 __ Addu(out, dividend, TMP);
1930 __ Sra(out, out, ctz_imm);
1931 if (imm < 0) {
1932 __ Subu(out, ZERO, out);
1933 }
1934 } else {
1935 DCHECK_EQ(type, Primitive::kPrimLong);
1936 if (ctz_imm == 1) {
1937 // Fast path for division by +/-2, which is very common.
1938 __ Dsrl32(TMP, dividend, 31);
1939 } else {
1940 __ Dsra32(TMP, dividend, 31);
1941 if (ctz_imm > 32) {
1942 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1943 } else {
1944 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1945 }
1946 }
1947 __ Daddu(out, dividend, TMP);
1948 if (ctz_imm < 32) {
1949 __ Dsra(out, out, ctz_imm);
1950 } else {
1951 __ Dsra32(out, out, ctz_imm - 32);
1952 }
1953 if (imm < 0) {
1954 __ Dsubu(out, ZERO, out);
1955 }
1956 }
1957 } else {
1958 if (type == Primitive::kPrimInt) {
1959 if (ctz_imm == 1) {
1960 // Fast path for modulo +/-2, which is very common.
1961 __ Sra(TMP, dividend, 31);
1962 __ Subu(out, dividend, TMP);
1963 __ Andi(out, out, 1);
1964 __ Addu(out, out, TMP);
1965 } else {
1966 __ Sra(TMP, dividend, 31);
1967 __ Srl(TMP, TMP, 32 - ctz_imm);
1968 __ Addu(out, dividend, TMP);
1969 if (IsUint<16>(abs_imm - 1)) {
1970 __ Andi(out, out, abs_imm - 1);
1971 } else {
1972 __ Sll(out, out, 32 - ctz_imm);
1973 __ Srl(out, out, 32 - ctz_imm);
1974 }
1975 __ Subu(out, out, TMP);
1976 }
1977 } else {
1978 DCHECK_EQ(type, Primitive::kPrimLong);
1979 if (ctz_imm == 1) {
1980 // Fast path for modulo +/-2, which is very common.
1981 __ Dsra32(TMP, dividend, 31);
1982 __ Dsubu(out, dividend, TMP);
1983 __ Andi(out, out, 1);
1984 __ Daddu(out, out, TMP);
1985 } else {
1986 __ Dsra32(TMP, dividend, 31);
1987 if (ctz_imm > 32) {
1988 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1989 } else {
1990 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1991 }
1992 __ Daddu(out, dividend, TMP);
1993 if (IsUint<16>(abs_imm - 1)) {
1994 __ Andi(out, out, abs_imm - 1);
1995 } else {
1996 if (ctz_imm > 32) {
1997 __ Dsll(out, out, 64 - ctz_imm);
1998 __ Dsrl(out, out, 64 - ctz_imm);
1999 } else {
2000 __ Dsll32(out, out, 32 - ctz_imm);
2001 __ Dsrl32(out, out, 32 - ctz_imm);
2002 }
2003 }
2004 __ Dsubu(out, out, TMP);
2005 }
2006 }
2007 }
2008}
2009
2010void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2011 DCHECK(instruction->IsDiv() || instruction->IsRem());
2012
2013 LocationSummary* locations = instruction->GetLocations();
2014 Location second = locations->InAt(1);
2015 DCHECK(second.IsConstant());
2016
2017 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2018 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2019 int64_t imm = Int64FromConstant(second.GetConstant());
2020
2021 Primitive::Type type = instruction->GetResultType();
2022 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2023
2024 int64_t magic;
2025 int shift;
2026 CalculateMagicAndShiftForDivRem(imm,
2027 (type == Primitive::kPrimLong),
2028 &magic,
2029 &shift);
2030
2031 if (type == Primitive::kPrimInt) {
2032 __ LoadConst32(TMP, magic);
2033 __ MuhR6(TMP, dividend, TMP);
2034
2035 if (imm > 0 && magic < 0) {
2036 __ Addu(TMP, TMP, dividend);
2037 } else if (imm < 0 && magic > 0) {
2038 __ Subu(TMP, TMP, dividend);
2039 }
2040
2041 if (shift != 0) {
2042 __ Sra(TMP, TMP, shift);
2043 }
2044
2045 if (instruction->IsDiv()) {
2046 __ Sra(out, TMP, 31);
2047 __ Subu(out, TMP, out);
2048 } else {
2049 __ Sra(AT, TMP, 31);
2050 __ Subu(AT, TMP, AT);
2051 __ LoadConst32(TMP, imm);
2052 __ MulR6(TMP, AT, TMP);
2053 __ Subu(out, dividend, TMP);
2054 }
2055 } else {
2056 __ LoadConst64(TMP, magic);
2057 __ Dmuh(TMP, dividend, TMP);
2058
2059 if (imm > 0 && magic < 0) {
2060 __ Daddu(TMP, TMP, dividend);
2061 } else if (imm < 0 && magic > 0) {
2062 __ Dsubu(TMP, TMP, dividend);
2063 }
2064
2065 if (shift >= 32) {
2066 __ Dsra32(TMP, TMP, shift - 32);
2067 } else if (shift > 0) {
2068 __ Dsra(TMP, TMP, shift);
2069 }
2070
2071 if (instruction->IsDiv()) {
2072 __ Dsra32(out, TMP, 31);
2073 __ Dsubu(out, TMP, out);
2074 } else {
2075 __ Dsra32(AT, TMP, 31);
2076 __ Dsubu(AT, TMP, AT);
2077 __ LoadConst64(TMP, imm);
2078 __ Dmul(TMP, AT, TMP);
2079 __ Dsubu(out, dividend, TMP);
2080 }
2081 }
2082}
2083
2084void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2085 DCHECK(instruction->IsDiv() || instruction->IsRem());
2086 Primitive::Type type = instruction->GetResultType();
2087 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2088
2089 LocationSummary* locations = instruction->GetLocations();
2090 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2091 Location second = locations->InAt(1);
2092
2093 if (second.IsConstant()) {
2094 int64_t imm = Int64FromConstant(second.GetConstant());
2095 if (imm == 0) {
2096 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2097 } else if (imm == 1 || imm == -1) {
2098 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002099 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002100 DivRemByPowerOfTwo(instruction);
2101 } else {
2102 DCHECK(imm <= -2 || imm >= 2);
2103 GenerateDivRemWithAnyConstant(instruction);
2104 }
2105 } else {
2106 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2107 GpuRegister divisor = second.AsRegister<GpuRegister>();
2108 if (instruction->IsDiv()) {
2109 if (type == Primitive::kPrimInt)
2110 __ DivR6(out, dividend, divisor);
2111 else
2112 __ Ddiv(out, dividend, divisor);
2113 } else {
2114 if (type == Primitive::kPrimInt)
2115 __ ModR6(out, dividend, divisor);
2116 else
2117 __ Dmod(out, dividend, divisor);
2118 }
2119 }
2120}
2121
Alexey Frunze4dda3372015-06-01 18:31:49 -07002122void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2123 LocationSummary* locations =
2124 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2125 switch (div->GetResultType()) {
2126 case Primitive::kPrimInt:
2127 case Primitive::kPrimLong:
2128 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002129 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002130 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2131 break;
2132
2133 case Primitive::kPrimFloat:
2134 case Primitive::kPrimDouble:
2135 locations->SetInAt(0, Location::RequiresFpuRegister());
2136 locations->SetInAt(1, Location::RequiresFpuRegister());
2137 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2138 break;
2139
2140 default:
2141 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2142 }
2143}
2144
2145void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2146 Primitive::Type type = instruction->GetType();
2147 LocationSummary* locations = instruction->GetLocations();
2148
2149 switch (type) {
2150 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002151 case Primitive::kPrimLong:
2152 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002153 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002154 case Primitive::kPrimFloat:
2155 case Primitive::kPrimDouble: {
2156 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2157 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2158 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2159 if (type == Primitive::kPrimFloat)
2160 __ DivS(dst, lhs, rhs);
2161 else
2162 __ DivD(dst, lhs, rhs);
2163 break;
2164 }
2165 default:
2166 LOG(FATAL) << "Unexpected div type " << type;
2167 }
2168}
2169
2170void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002171 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2172 ? LocationSummary::kCallOnSlowPath
2173 : LocationSummary::kNoCall;
2174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002175 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2176 if (instruction->HasUses()) {
2177 locations->SetOut(Location::SameAsFirstInput());
2178 }
2179}
2180
2181void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2182 SlowPathCodeMIPS64* slow_path =
2183 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2184 codegen_->AddSlowPath(slow_path);
2185 Location value = instruction->GetLocations()->InAt(0);
2186
2187 Primitive::Type type = instruction->GetType();
2188
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002189 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002190 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002191 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002192 }
2193
2194 if (value.IsConstant()) {
2195 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2196 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002197 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002198 } else {
2199 // A division by a non-null constant is valid. We don't need to perform
2200 // any check, so simply fall through.
2201 }
2202 } else {
2203 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2204 }
2205}
2206
2207void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2208 LocationSummary* locations =
2209 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2210 locations->SetOut(Location::ConstantLocation(constant));
2211}
2212
2213void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2214 // Will be generated at use site.
2215}
2216
2217void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2218 exit->SetLocations(nullptr);
2219}
2220
2221void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2222}
2223
2224void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2225 LocationSummary* locations =
2226 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2227 locations->SetOut(Location::ConstantLocation(constant));
2228}
2229
2230void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2231 // Will be generated at use site.
2232}
2233
David Brazdilfc6a86a2015-06-26 10:33:45 +00002234void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002235 DCHECK(!successor->IsExitBlock());
2236 HBasicBlock* block = got->GetBlock();
2237 HInstruction* previous = got->GetPrevious();
2238 HLoopInformation* info = block->GetLoopInformation();
2239
2240 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2241 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2242 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2243 return;
2244 }
2245 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2246 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2247 }
2248 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002249 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002250 }
2251}
2252
David Brazdilfc6a86a2015-06-26 10:33:45 +00002253void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2254 got->SetLocations(nullptr);
2255}
2256
2257void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2258 HandleGoto(got, got->GetSuccessor());
2259}
2260
2261void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2262 try_boundary->SetLocations(nullptr);
2263}
2264
2265void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2266 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2267 if (!successor->IsExitBlock()) {
2268 HandleGoto(try_boundary, successor);
2269 }
2270}
2271
Alexey Frunze299a9392015-12-08 16:08:02 -08002272void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2273 bool is64bit,
2274 LocationSummary* locations) {
2275 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2276 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2277 Location rhs_location = locations->InAt(1);
2278 GpuRegister rhs_reg = ZERO;
2279 int64_t rhs_imm = 0;
2280 bool use_imm = rhs_location.IsConstant();
2281 if (use_imm) {
2282 if (is64bit) {
2283 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2284 } else {
2285 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2286 }
2287 } else {
2288 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2289 }
2290 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2291
2292 switch (cond) {
2293 case kCondEQ:
2294 case kCondNE:
2295 if (use_imm && IsUint<16>(rhs_imm)) {
2296 __ Xori(dst, lhs, rhs_imm);
2297 } else {
2298 if (use_imm) {
2299 rhs_reg = TMP;
2300 __ LoadConst64(rhs_reg, rhs_imm);
2301 }
2302 __ Xor(dst, lhs, rhs_reg);
2303 }
2304 if (cond == kCondEQ) {
2305 __ Sltiu(dst, dst, 1);
2306 } else {
2307 __ Sltu(dst, ZERO, dst);
2308 }
2309 break;
2310
2311 case kCondLT:
2312 case kCondGE:
2313 if (use_imm && IsInt<16>(rhs_imm)) {
2314 __ Slti(dst, lhs, rhs_imm);
2315 } else {
2316 if (use_imm) {
2317 rhs_reg = TMP;
2318 __ LoadConst64(rhs_reg, rhs_imm);
2319 }
2320 __ Slt(dst, lhs, rhs_reg);
2321 }
2322 if (cond == kCondGE) {
2323 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2324 // only the slt instruction but no sge.
2325 __ Xori(dst, dst, 1);
2326 }
2327 break;
2328
2329 case kCondLE:
2330 case kCondGT:
2331 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2332 // Simulate lhs <= rhs via lhs < rhs + 1.
2333 __ Slti(dst, lhs, rhs_imm_plus_one);
2334 if (cond == kCondGT) {
2335 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2336 // only the slti instruction but no sgti.
2337 __ Xori(dst, dst, 1);
2338 }
2339 } else {
2340 if (use_imm) {
2341 rhs_reg = TMP;
2342 __ LoadConst64(rhs_reg, rhs_imm);
2343 }
2344 __ Slt(dst, rhs_reg, lhs);
2345 if (cond == kCondLE) {
2346 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2347 // only the slt instruction but no sle.
2348 __ Xori(dst, dst, 1);
2349 }
2350 }
2351 break;
2352
2353 case kCondB:
2354 case kCondAE:
2355 if (use_imm && IsInt<16>(rhs_imm)) {
2356 // Sltiu sign-extends its 16-bit immediate operand before
2357 // the comparison and thus lets us compare directly with
2358 // unsigned values in the ranges [0, 0x7fff] and
2359 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2360 __ Sltiu(dst, lhs, rhs_imm);
2361 } else {
2362 if (use_imm) {
2363 rhs_reg = TMP;
2364 __ LoadConst64(rhs_reg, rhs_imm);
2365 }
2366 __ Sltu(dst, lhs, rhs_reg);
2367 }
2368 if (cond == kCondAE) {
2369 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2370 // only the sltu instruction but no sgeu.
2371 __ Xori(dst, dst, 1);
2372 }
2373 break;
2374
2375 case kCondBE:
2376 case kCondA:
2377 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2378 // Simulate lhs <= rhs via lhs < rhs + 1.
2379 // Note that this only works if rhs + 1 does not overflow
2380 // to 0, hence the check above.
2381 // Sltiu sign-extends its 16-bit immediate operand before
2382 // the comparison and thus lets us compare directly with
2383 // unsigned values in the ranges [0, 0x7fff] and
2384 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2385 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2386 if (cond == kCondA) {
2387 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2388 // only the sltiu instruction but no sgtiu.
2389 __ Xori(dst, dst, 1);
2390 }
2391 } else {
2392 if (use_imm) {
2393 rhs_reg = TMP;
2394 __ LoadConst64(rhs_reg, rhs_imm);
2395 }
2396 __ Sltu(dst, rhs_reg, lhs);
2397 if (cond == kCondBE) {
2398 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2399 // only the sltu instruction but no sleu.
2400 __ Xori(dst, dst, 1);
2401 }
2402 }
2403 break;
2404 }
2405}
2406
2407void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2408 bool is64bit,
2409 LocationSummary* locations,
2410 Mips64Label* label) {
2411 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2412 Location rhs_location = locations->InAt(1);
2413 GpuRegister rhs_reg = ZERO;
2414 int64_t rhs_imm = 0;
2415 bool use_imm = rhs_location.IsConstant();
2416 if (use_imm) {
2417 if (is64bit) {
2418 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2419 } else {
2420 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2421 }
2422 } else {
2423 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2424 }
2425
2426 if (use_imm && rhs_imm == 0) {
2427 switch (cond) {
2428 case kCondEQ:
2429 case kCondBE: // <= 0 if zero
2430 __ Beqzc(lhs, label);
2431 break;
2432 case kCondNE:
2433 case kCondA: // > 0 if non-zero
2434 __ Bnezc(lhs, label);
2435 break;
2436 case kCondLT:
2437 __ Bltzc(lhs, label);
2438 break;
2439 case kCondGE:
2440 __ Bgezc(lhs, label);
2441 break;
2442 case kCondLE:
2443 __ Blezc(lhs, label);
2444 break;
2445 case kCondGT:
2446 __ Bgtzc(lhs, label);
2447 break;
2448 case kCondB: // always false
2449 break;
2450 case kCondAE: // always true
2451 __ Bc(label);
2452 break;
2453 }
2454 } else {
2455 if (use_imm) {
2456 rhs_reg = TMP;
2457 __ LoadConst64(rhs_reg, rhs_imm);
2458 }
2459 switch (cond) {
2460 case kCondEQ:
2461 __ Beqc(lhs, rhs_reg, label);
2462 break;
2463 case kCondNE:
2464 __ Bnec(lhs, rhs_reg, label);
2465 break;
2466 case kCondLT:
2467 __ Bltc(lhs, rhs_reg, label);
2468 break;
2469 case kCondGE:
2470 __ Bgec(lhs, rhs_reg, label);
2471 break;
2472 case kCondLE:
2473 __ Bgec(rhs_reg, lhs, label);
2474 break;
2475 case kCondGT:
2476 __ Bltc(rhs_reg, lhs, label);
2477 break;
2478 case kCondB:
2479 __ Bltuc(lhs, rhs_reg, label);
2480 break;
2481 case kCondAE:
2482 __ Bgeuc(lhs, rhs_reg, label);
2483 break;
2484 case kCondBE:
2485 __ Bgeuc(rhs_reg, lhs, label);
2486 break;
2487 case kCondA:
2488 __ Bltuc(rhs_reg, lhs, label);
2489 break;
2490 }
2491 }
2492}
2493
2494void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2495 bool gt_bias,
2496 Primitive::Type type,
2497 LocationSummary* locations,
2498 Mips64Label* label) {
2499 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2500 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2501 if (type == Primitive::kPrimFloat) {
2502 switch (cond) {
2503 case kCondEQ:
2504 __ CmpEqS(FTMP, lhs, rhs);
2505 __ Bc1nez(FTMP, label);
2506 break;
2507 case kCondNE:
2508 __ CmpEqS(FTMP, lhs, rhs);
2509 __ Bc1eqz(FTMP, label);
2510 break;
2511 case kCondLT:
2512 if (gt_bias) {
2513 __ CmpLtS(FTMP, lhs, rhs);
2514 } else {
2515 __ CmpUltS(FTMP, lhs, rhs);
2516 }
2517 __ Bc1nez(FTMP, label);
2518 break;
2519 case kCondLE:
2520 if (gt_bias) {
2521 __ CmpLeS(FTMP, lhs, rhs);
2522 } else {
2523 __ CmpUleS(FTMP, lhs, rhs);
2524 }
2525 __ Bc1nez(FTMP, label);
2526 break;
2527 case kCondGT:
2528 if (gt_bias) {
2529 __ CmpUltS(FTMP, rhs, lhs);
2530 } else {
2531 __ CmpLtS(FTMP, rhs, lhs);
2532 }
2533 __ Bc1nez(FTMP, label);
2534 break;
2535 case kCondGE:
2536 if (gt_bias) {
2537 __ CmpUleS(FTMP, rhs, lhs);
2538 } else {
2539 __ CmpLeS(FTMP, rhs, lhs);
2540 }
2541 __ Bc1nez(FTMP, label);
2542 break;
2543 default:
2544 LOG(FATAL) << "Unexpected non-floating-point condition";
2545 }
2546 } else {
2547 DCHECK_EQ(type, Primitive::kPrimDouble);
2548 switch (cond) {
2549 case kCondEQ:
2550 __ CmpEqD(FTMP, lhs, rhs);
2551 __ Bc1nez(FTMP, label);
2552 break;
2553 case kCondNE:
2554 __ CmpEqD(FTMP, lhs, rhs);
2555 __ Bc1eqz(FTMP, label);
2556 break;
2557 case kCondLT:
2558 if (gt_bias) {
2559 __ CmpLtD(FTMP, lhs, rhs);
2560 } else {
2561 __ CmpUltD(FTMP, lhs, rhs);
2562 }
2563 __ Bc1nez(FTMP, label);
2564 break;
2565 case kCondLE:
2566 if (gt_bias) {
2567 __ CmpLeD(FTMP, lhs, rhs);
2568 } else {
2569 __ CmpUleD(FTMP, lhs, rhs);
2570 }
2571 __ Bc1nez(FTMP, label);
2572 break;
2573 case kCondGT:
2574 if (gt_bias) {
2575 __ CmpUltD(FTMP, rhs, lhs);
2576 } else {
2577 __ CmpLtD(FTMP, rhs, lhs);
2578 }
2579 __ Bc1nez(FTMP, label);
2580 break;
2581 case kCondGE:
2582 if (gt_bias) {
2583 __ CmpUleD(FTMP, rhs, lhs);
2584 } else {
2585 __ CmpLeD(FTMP, rhs, lhs);
2586 }
2587 __ Bc1nez(FTMP, label);
2588 break;
2589 default:
2590 LOG(FATAL) << "Unexpected non-floating-point condition";
2591 }
2592 }
2593}
2594
Alexey Frunze4dda3372015-06-01 18:31:49 -07002595void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002596 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002597 Mips64Label* true_target,
2598 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002599 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002600
David Brazdil0debae72015-11-12 18:37:00 +00002601 if (true_target == nullptr && false_target == nullptr) {
2602 // Nothing to do. The code always falls through.
2603 return;
2604 } else if (cond->IsIntConstant()) {
2605 // Constant condition, statically compared against 1.
2606 if (cond->AsIntConstant()->IsOne()) {
2607 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002608 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002609 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002610 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002611 DCHECK(cond->AsIntConstant()->IsZero());
2612 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002613 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002614 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002615 }
David Brazdil0debae72015-11-12 18:37:00 +00002616 return;
2617 }
2618
2619 // The following code generates these patterns:
2620 // (1) true_target == nullptr && false_target != nullptr
2621 // - opposite condition true => branch to false_target
2622 // (2) true_target != nullptr && false_target == nullptr
2623 // - condition true => branch to true_target
2624 // (3) true_target != nullptr && false_target != nullptr
2625 // - condition true => branch to true_target
2626 // - branch to false_target
2627 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002628 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002629 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002630 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002631 if (true_target == nullptr) {
2632 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2633 } else {
2634 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2635 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002636 } else {
2637 // The condition instruction has not been materialized, use its inputs as
2638 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002639 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002640 Primitive::Type type = condition->InputAt(0)->GetType();
2641 LocationSummary* locations = cond->GetLocations();
2642 IfCondition if_cond = condition->GetCondition();
2643 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002644
David Brazdil0debae72015-11-12 18:37:00 +00002645 if (true_target == nullptr) {
2646 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002647 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002648 }
2649
Alexey Frunze299a9392015-12-08 16:08:02 -08002650 switch (type) {
2651 default:
2652 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2653 break;
2654 case Primitive::kPrimLong:
2655 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2656 break;
2657 case Primitive::kPrimFloat:
2658 case Primitive::kPrimDouble:
2659 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2660 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002661 }
2662 }
David Brazdil0debae72015-11-12 18:37:00 +00002663
2664 // If neither branch falls through (case 3), the conditional branch to `true_target`
2665 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2666 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002667 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002668 }
2669}
2670
2671void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002673 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002674 locations->SetInAt(0, Location::RequiresRegister());
2675 }
2676}
2677
2678void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002679 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2680 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002681 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002682 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002683 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002684 nullptr : codegen_->GetLabelOf(false_successor);
2685 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002686}
2687
2688void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2689 LocationSummary* locations = new (GetGraph()->GetArena())
2690 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002691 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002692 locations->SetInAt(0, Location::RequiresRegister());
2693 }
2694}
2695
2696void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002697 SlowPathCodeMIPS64* slow_path =
2698 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002699 GenerateTestAndBranch(deoptimize,
2700 /* condition_input_index */ 0,
2701 slow_path->GetEntryLabel(),
2702 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002703}
2704
David Brazdil74eb1b22015-12-14 11:44:01 +00002705void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2706 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2707 if (Primitive::IsFloatingPointType(select->GetType())) {
2708 locations->SetInAt(0, Location::RequiresFpuRegister());
2709 locations->SetInAt(1, Location::RequiresFpuRegister());
2710 } else {
2711 locations->SetInAt(0, Location::RequiresRegister());
2712 locations->SetInAt(1, Location::RequiresRegister());
2713 }
2714 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2715 locations->SetInAt(2, Location::RequiresRegister());
2716 }
2717 locations->SetOut(Location::SameAsFirstInput());
2718}
2719
2720void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2721 LocationSummary* locations = select->GetLocations();
2722 Mips64Label false_target;
2723 GenerateTestAndBranch(select,
2724 /* condition_input_index */ 2,
2725 /* true_target */ nullptr,
2726 &false_target);
2727 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2728 __ Bind(&false_target);
2729}
2730
David Srbecky0cf44932015-12-09 14:09:59 +00002731void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2732 new (GetGraph()->GetArena()) LocationSummary(info);
2733}
2734
2735void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002736 if (codegen_->HasStackMapAtCurrentPc()) {
2737 // Ensure that we do not collide with the stack map of the previous instruction.
2738 __ Nop();
2739 }
David Srbecky0cf44932015-12-09 14:09:59 +00002740 codegen_->RecordPcInfo(info, info->GetDexPc());
2741}
2742
Alexey Frunze4dda3372015-06-01 18:31:49 -07002743void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2744 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2745 LocationSummary* locations =
2746 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2747 locations->SetInAt(0, Location::RequiresRegister());
2748 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2749 locations->SetOut(Location::RequiresFpuRegister());
2750 } else {
2751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2752 }
2753}
2754
2755void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2756 const FieldInfo& field_info) {
2757 Primitive::Type type = field_info.GetFieldType();
2758 LocationSummary* locations = instruction->GetLocations();
2759 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2760 LoadOperandType load_type = kLoadUnsignedByte;
2761 switch (type) {
2762 case Primitive::kPrimBoolean:
2763 load_type = kLoadUnsignedByte;
2764 break;
2765 case Primitive::kPrimByte:
2766 load_type = kLoadSignedByte;
2767 break;
2768 case Primitive::kPrimShort:
2769 load_type = kLoadSignedHalfword;
2770 break;
2771 case Primitive::kPrimChar:
2772 load_type = kLoadUnsignedHalfword;
2773 break;
2774 case Primitive::kPrimInt:
2775 case Primitive::kPrimFloat:
2776 load_type = kLoadWord;
2777 break;
2778 case Primitive::kPrimLong:
2779 case Primitive::kPrimDouble:
2780 load_type = kLoadDoubleword;
2781 break;
2782 case Primitive::kPrimNot:
2783 load_type = kLoadUnsignedWord;
2784 break;
2785 case Primitive::kPrimVoid:
2786 LOG(FATAL) << "Unreachable type " << type;
2787 UNREACHABLE();
2788 }
2789 if (!Primitive::IsFloatingPointType(type)) {
2790 DCHECK(locations->Out().IsRegister());
2791 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2792 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2793 } else {
2794 DCHECK(locations->Out().IsFpuRegister());
2795 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2796 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2797 }
2798
2799 codegen_->MaybeRecordImplicitNullCheck(instruction);
2800 // TODO: memory barrier?
2801}
2802
2803void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2804 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2805 LocationSummary* locations =
2806 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2807 locations->SetInAt(0, Location::RequiresRegister());
2808 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2809 locations->SetInAt(1, Location::RequiresFpuRegister());
2810 } else {
2811 locations->SetInAt(1, Location::RequiresRegister());
2812 }
2813}
2814
2815void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002816 const FieldInfo& field_info,
2817 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002818 Primitive::Type type = field_info.GetFieldType();
2819 LocationSummary* locations = instruction->GetLocations();
2820 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2821 StoreOperandType store_type = kStoreByte;
2822 switch (type) {
2823 case Primitive::kPrimBoolean:
2824 case Primitive::kPrimByte:
2825 store_type = kStoreByte;
2826 break;
2827 case Primitive::kPrimShort:
2828 case Primitive::kPrimChar:
2829 store_type = kStoreHalfword;
2830 break;
2831 case Primitive::kPrimInt:
2832 case Primitive::kPrimFloat:
2833 case Primitive::kPrimNot:
2834 store_type = kStoreWord;
2835 break;
2836 case Primitive::kPrimLong:
2837 case Primitive::kPrimDouble:
2838 store_type = kStoreDoubleword;
2839 break;
2840 case Primitive::kPrimVoid:
2841 LOG(FATAL) << "Unreachable type " << type;
2842 UNREACHABLE();
2843 }
2844 if (!Primitive::IsFloatingPointType(type)) {
2845 DCHECK(locations->InAt(1).IsRegister());
2846 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2847 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2848 } else {
2849 DCHECK(locations->InAt(1).IsFpuRegister());
2850 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2851 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2852 }
2853
2854 codegen_->MaybeRecordImplicitNullCheck(instruction);
2855 // TODO: memory barriers?
2856 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2857 DCHECK(locations->InAt(1).IsRegister());
2858 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002859 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002860 }
2861}
2862
2863void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2864 HandleFieldGet(instruction, instruction->GetFieldInfo());
2865}
2866
2867void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2868 HandleFieldGet(instruction, instruction->GetFieldInfo());
2869}
2870
2871void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2872 HandleFieldSet(instruction, instruction->GetFieldInfo());
2873}
2874
2875void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002876 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002877}
2878
2879void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2880 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002881 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2883 locations->SetInAt(0, Location::RequiresRegister());
2884 locations->SetInAt(1, Location::RequiresRegister());
2885 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002886 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002887 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2888}
2889
2890void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2891 LocationSummary* locations = instruction->GetLocations();
2892 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2893 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2894 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2895
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002896 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002897
2898 // Return 0 if `obj` is null.
2899 // TODO: Avoid this check if we know `obj` is not null.
2900 __ Move(out, ZERO);
2901 __ Beqzc(obj, &done);
2902
2903 // Compare the class of `obj` with `cls`.
2904 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002905 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002906 // Classes must be equal for the instanceof to succeed.
2907 __ Xor(out, out, cls);
2908 __ Sltiu(out, out, 1);
2909 } else {
2910 // If the classes are not equal, we go into a slow path.
2911 DCHECK(locations->OnlyCallsOnSlowPath());
2912 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002913 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002914 codegen_->AddSlowPath(slow_path);
2915 __ Bnec(out, cls, slow_path->GetEntryLabel());
2916 __ LoadConst32(out, 1);
2917 __ Bind(slow_path->GetExitLabel());
2918 }
2919
2920 __ Bind(&done);
2921}
2922
2923void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2924 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2925 locations->SetOut(Location::ConstantLocation(constant));
2926}
2927
2928void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2929 // Will be generated at use site.
2930}
2931
2932void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2933 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2934 locations->SetOut(Location::ConstantLocation(constant));
2935}
2936
2937void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2938 // Will be generated at use site.
2939}
2940
Calin Juravle175dc732015-08-25 15:42:32 +01002941void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2942 // The trampoline uses the same calling convention as dex calling conventions,
2943 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2944 // the method_idx.
2945 HandleInvoke(invoke);
2946}
2947
2948void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2949 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2950}
2951
Alexey Frunze4dda3372015-06-01 18:31:49 -07002952void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2953 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2954 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2955}
2956
2957void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2958 HandleInvoke(invoke);
2959 // The register T0 is required to be used for the hidden argument in
2960 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2961 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2962}
2963
2964void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2965 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2966 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2967 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2968 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2969 Location receiver = invoke->GetLocations()->InAt(0);
2970 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02002971 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002972
2973 // Set the hidden argument.
2974 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2975 invoke->GetDexMethodIndex());
2976
2977 // temp = object->GetClass();
2978 if (receiver.IsStackSlot()) {
2979 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2980 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2981 } else {
2982 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2983 }
2984 codegen_->MaybeRecordImplicitNullCheck(invoke);
2985 // temp = temp->GetImtEntryAt(method_offset);
2986 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2987 // T9 = temp->GetEntryPoint();
2988 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2989 // T9();
2990 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002991 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002992 DCHECK(!codegen_->IsLeafMethod());
2993 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2994}
2995
2996void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002997 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2998 if (intrinsic.TryDispatch(invoke)) {
2999 return;
3000 }
3001
Alexey Frunze4dda3372015-06-01 18:31:49 -07003002 HandleInvoke(invoke);
3003}
3004
3005void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003006 // Explicit clinit checks triggered by static invokes must have been pruned by
3007 // art::PrepareForRegisterAllocation.
3008 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003009
Chris Larsen3039e382015-08-26 07:54:08 -07003010 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3011 if (intrinsic.TryDispatch(invoke)) {
3012 return;
3013 }
3014
Alexey Frunze4dda3372015-06-01 18:31:49 -07003015 HandleInvoke(invoke);
3016
3017 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
3018 // clobbering somewhere else, reduce further register pressure by avoiding
3019 // allocation of a register for the current method pointer like on x86 baseline.
3020 // TODO: remove this once all the issues with register saving/restoring are
3021 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003022 if (invoke->HasCurrentMethodInput()) {
3023 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00003024 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003025 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003026 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003027 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003028 }
3029}
3030
Chris Larsen3039e382015-08-26 07:54:08 -07003031static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003032 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003033 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3034 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003035 return true;
3036 }
3037 return false;
3038}
3039
Vladimir Markodc151b22015-10-15 18:02:30 +01003040HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3041 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3042 MethodReference target_method ATTRIBUTE_UNUSED) {
3043 switch (desired_dispatch_info.method_load_kind) {
3044 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3045 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3046 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3047 return HInvokeStaticOrDirect::DispatchInfo {
3048 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3049 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3050 0u,
3051 0u
3052 };
3053 default:
3054 break;
3055 }
3056 switch (desired_dispatch_info.code_ptr_location) {
3057 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3058 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3059 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3060 return HInvokeStaticOrDirect::DispatchInfo {
3061 desired_dispatch_info.method_load_kind,
3062 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3063 desired_dispatch_info.method_load_data,
3064 0u
3065 };
3066 default:
3067 return desired_dispatch_info;
3068 }
3069}
3070
Alexey Frunze4dda3372015-06-01 18:31:49 -07003071void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3072 // All registers are assumed to be correctly set up per the calling convention.
3073
Vladimir Marko58155012015-08-19 12:49:41 +00003074 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3075 switch (invoke->GetMethodLoadKind()) {
3076 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3077 // temp = thread->string_init_entrypoint
3078 __ LoadFromOffset(kLoadDoubleword,
3079 temp.AsRegister<GpuRegister>(),
3080 TR,
3081 invoke->GetStringInitOffset());
3082 break;
3083 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003084 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003085 break;
3086 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3087 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3088 break;
3089 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003090 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003091 // TODO: Implement these types.
3092 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3093 LOG(FATAL) << "Unsupported";
3094 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003095 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003096 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003097 GpuRegister reg = temp.AsRegister<GpuRegister>();
3098 GpuRegister method_reg;
3099 if (current_method.IsRegister()) {
3100 method_reg = current_method.AsRegister<GpuRegister>();
3101 } else {
3102 // TODO: use the appropriate DCHECK() here if possible.
3103 // DCHECK(invoke->GetLocations()->Intrinsified());
3104 DCHECK(!current_method.IsValid());
3105 method_reg = reg;
3106 __ Ld(reg, SP, kCurrentMethodStackOffset);
3107 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003108
Vladimir Marko58155012015-08-19 12:49:41 +00003109 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003110 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003111 reg,
3112 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003113 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003114 // temp = temp[index_in_cache]
3115 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3116 __ LoadFromOffset(kLoadDoubleword,
3117 reg,
3118 reg,
3119 CodeGenerator::GetCachePointerOffset(index_in_cache));
3120 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003121 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003122 }
3123
Vladimir Marko58155012015-08-19 12:49:41 +00003124 switch (invoke->GetCodePtrLocation()) {
3125 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003126 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003127 break;
3128 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3129 // LR = invoke->GetDirectCodePtr();
3130 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3131 // LR()
3132 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003133 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003134 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003135 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003136 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3137 // TODO: Implement these types.
3138 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3139 LOG(FATAL) << "Unsupported";
3140 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003141 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3142 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3143 __ LoadFromOffset(kLoadDoubleword,
3144 T9,
3145 callee_method.AsRegister<GpuRegister>(),
3146 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003147 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003148 // T9()
3149 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003150 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003151 break;
3152 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003153 DCHECK(!IsLeafMethod());
3154}
3155
3156void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003157 // Explicit clinit checks triggered by static invokes must have been pruned by
3158 // art::PrepareForRegisterAllocation.
3159 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003160
3161 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3162 return;
3163 }
3164
3165 LocationSummary* locations = invoke->GetLocations();
3166 codegen_->GenerateStaticOrDirectCall(invoke,
3167 locations->HasTemps()
3168 ? locations->GetTemp(0)
3169 : Location::NoLocation());
3170 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3171}
3172
Alexey Frunze53afca12015-11-05 16:34:23 -08003173void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003174 // Use the calling convention instead of the location of the receiver, as
3175 // intrinsics may have put the receiver in a different register. In the intrinsics
3176 // slow path, the arguments have been moved to the right place, so here we are
3177 // guaranteed that the receiver is the first register of the calling convention.
3178 InvokeDexCallingConvention calling_convention;
3179 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3180
Alexey Frunze53afca12015-11-05 16:34:23 -08003181 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003182 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3183 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3184 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003185 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003186
3187 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003188 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003189 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003190 // temp = temp->GetMethodAt(method_offset);
3191 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3192 // T9 = temp->GetEntryPoint();
3193 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3194 // T9();
3195 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003196 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003197}
3198
3199void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3200 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3201 return;
3202 }
3203
3204 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003205 DCHECK(!codegen_->IsLeafMethod());
3206 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3207}
3208
3209void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003210 InvokeRuntimeCallingConvention calling_convention;
3211 CodeGenerator::CreateLoadClassLocationSummary(
3212 cls,
3213 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003214 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003215}
3216
3217void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3218 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003219 if (cls->NeedsAccessCheck()) {
3220 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3221 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3222 cls,
3223 cls->GetDexPc(),
3224 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003225 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003226 return;
3227 }
3228
3229 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3230 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3231 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003232 DCHECK(!cls->CanCallRuntime());
3233 DCHECK(!cls->MustGenerateClinitCheck());
3234 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3235 ArtMethod::DeclaringClassOffset().Int32Value());
3236 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003237 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3238 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003239 __ LoadFromOffset(
3240 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003241 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003242 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3243 DCHECK(cls->CanCallRuntime());
3244 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3245 cls,
3246 cls,
3247 cls->GetDexPc(),
3248 cls->MustGenerateClinitCheck());
3249 codegen_->AddSlowPath(slow_path);
3250 if (!cls->IsInDexCache()) {
3251 __ Beqzc(out, slow_path->GetEntryLabel());
3252 }
3253 if (cls->MustGenerateClinitCheck()) {
3254 GenerateClassInitializationCheck(slow_path, out);
3255 } else {
3256 __ Bind(slow_path->GetExitLabel());
3257 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003258 }
3259 }
3260}
3261
David Brazdilcb1c0552015-08-04 16:22:25 +01003262static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003263 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003264}
3265
Alexey Frunze4dda3372015-06-01 18:31:49 -07003266void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3267 LocationSummary* locations =
3268 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3269 locations->SetOut(Location::RequiresRegister());
3270}
3271
3272void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3273 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003274 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3275}
3276
3277void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3278 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3279}
3280
3281void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3282 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003283}
3284
3285void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3286 load->SetLocations(nullptr);
3287}
3288
3289void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3290 // Nothing to do, this is driven by the code generator.
3291}
3292
3293void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00003294 LocationSummary::CallKind call_kind = load->IsInDexCache()
3295 ? LocationSummary::kNoCall
3296 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003298 locations->SetInAt(0, Location::RequiresRegister());
3299 locations->SetOut(Location::RequiresRegister());
3300}
3301
3302void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003303 LocationSummary* locations = load->GetLocations();
3304 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3305 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3306 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3307 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003308 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003309 __ LoadFromOffset(
3310 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003311 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003312
3313 if (!load->IsInDexCache()) {
3314 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3315 codegen_->AddSlowPath(slow_path);
3316 __ Beqzc(out, slow_path->GetEntryLabel());
3317 __ Bind(slow_path->GetExitLabel());
3318 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003319}
3320
3321void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3322 local->SetLocations(nullptr);
3323}
3324
3325void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3326 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3327}
3328
3329void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3330 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3331 locations->SetOut(Location::ConstantLocation(constant));
3332}
3333
3334void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3335 // Will be generated at use site.
3336}
3337
3338void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3339 LocationSummary* locations =
3340 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3341 InvokeRuntimeCallingConvention calling_convention;
3342 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3343}
3344
3345void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3346 codegen_->InvokeRuntime(instruction->IsEnter()
3347 ? QUICK_ENTRY_POINT(pLockObject)
3348 : QUICK_ENTRY_POINT(pUnlockObject),
3349 instruction,
3350 instruction->GetDexPc(),
3351 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003352 if (instruction->IsEnter()) {
3353 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3354 } else {
3355 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3356 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003357}
3358
3359void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3360 LocationSummary* locations =
3361 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3362 switch (mul->GetResultType()) {
3363 case Primitive::kPrimInt:
3364 case Primitive::kPrimLong:
3365 locations->SetInAt(0, Location::RequiresRegister());
3366 locations->SetInAt(1, Location::RequiresRegister());
3367 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3368 break;
3369
3370 case Primitive::kPrimFloat:
3371 case Primitive::kPrimDouble:
3372 locations->SetInAt(0, Location::RequiresFpuRegister());
3373 locations->SetInAt(1, Location::RequiresFpuRegister());
3374 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3375 break;
3376
3377 default:
3378 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3379 }
3380}
3381
3382void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3383 Primitive::Type type = instruction->GetType();
3384 LocationSummary* locations = instruction->GetLocations();
3385
3386 switch (type) {
3387 case Primitive::kPrimInt:
3388 case Primitive::kPrimLong: {
3389 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3390 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3391 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3392 if (type == Primitive::kPrimInt)
3393 __ MulR6(dst, lhs, rhs);
3394 else
3395 __ Dmul(dst, lhs, rhs);
3396 break;
3397 }
3398 case Primitive::kPrimFloat:
3399 case Primitive::kPrimDouble: {
3400 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3401 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3402 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3403 if (type == Primitive::kPrimFloat)
3404 __ MulS(dst, lhs, rhs);
3405 else
3406 __ MulD(dst, lhs, rhs);
3407 break;
3408 }
3409 default:
3410 LOG(FATAL) << "Unexpected mul type " << type;
3411 }
3412}
3413
3414void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3415 LocationSummary* locations =
3416 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3417 switch (neg->GetResultType()) {
3418 case Primitive::kPrimInt:
3419 case Primitive::kPrimLong:
3420 locations->SetInAt(0, Location::RequiresRegister());
3421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3422 break;
3423
3424 case Primitive::kPrimFloat:
3425 case Primitive::kPrimDouble:
3426 locations->SetInAt(0, Location::RequiresFpuRegister());
3427 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3428 break;
3429
3430 default:
3431 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3432 }
3433}
3434
3435void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3436 Primitive::Type type = instruction->GetType();
3437 LocationSummary* locations = instruction->GetLocations();
3438
3439 switch (type) {
3440 case Primitive::kPrimInt:
3441 case Primitive::kPrimLong: {
3442 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3443 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3444 if (type == Primitive::kPrimInt)
3445 __ Subu(dst, ZERO, src);
3446 else
3447 __ Dsubu(dst, ZERO, src);
3448 break;
3449 }
3450 case Primitive::kPrimFloat:
3451 case Primitive::kPrimDouble: {
3452 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3453 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3454 if (type == Primitive::kPrimFloat)
3455 __ NegS(dst, src);
3456 else
3457 __ NegD(dst, src);
3458 break;
3459 }
3460 default:
3461 LOG(FATAL) << "Unexpected neg type " << type;
3462 }
3463}
3464
3465void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3466 LocationSummary* locations =
3467 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3468 InvokeRuntimeCallingConvention calling_convention;
3469 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3470 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3471 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3472 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3473}
3474
3475void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3476 LocationSummary* locations = instruction->GetLocations();
3477 // Move an uint16_t value to a register.
3478 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003479 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3480 instruction,
3481 instruction->GetDexPc(),
3482 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003483 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3484}
3485
3486void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3487 LocationSummary* locations =
3488 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3489 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003490 if (instruction->IsStringAlloc()) {
3491 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3492 } else {
3493 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3494 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3495 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003496 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3497}
3498
3499void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003500 if (instruction->IsStringAlloc()) {
3501 // String is allocated through StringFactory. Call NewEmptyString entry point.
3502 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003503 MemberOffset code_offset =
3504 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003505 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3506 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3507 __ Jalr(T9);
3508 __ Nop();
3509 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3510 } else {
3511 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3512 instruction,
3513 instruction->GetDexPc(),
3514 nullptr);
3515 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3516 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003517}
3518
3519void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3521 locations->SetInAt(0, Location::RequiresRegister());
3522 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3523}
3524
3525void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3526 Primitive::Type type = instruction->GetType();
3527 LocationSummary* locations = instruction->GetLocations();
3528
3529 switch (type) {
3530 case Primitive::kPrimInt:
3531 case Primitive::kPrimLong: {
3532 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3533 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3534 __ Nor(dst, src, ZERO);
3535 break;
3536 }
3537
3538 default:
3539 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3540 }
3541}
3542
3543void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3544 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3545 locations->SetInAt(0, Location::RequiresRegister());
3546 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3547}
3548
3549void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3550 LocationSummary* locations = instruction->GetLocations();
3551 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3552 locations->InAt(0).AsRegister<GpuRegister>(),
3553 1);
3554}
3555
3556void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003557 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3558 ? LocationSummary::kCallOnSlowPath
3559 : LocationSummary::kNoCall;
3560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003561 locations->SetInAt(0, Location::RequiresRegister());
3562 if (instruction->HasUses()) {
3563 locations->SetOut(Location::SameAsFirstInput());
3564 }
3565}
3566
3567void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3568 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3569 return;
3570 }
3571 Location obj = instruction->GetLocations()->InAt(0);
3572
3573 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3574 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3575}
3576
3577void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3578 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3579 codegen_->AddSlowPath(slow_path);
3580
3581 Location obj = instruction->GetLocations()->InAt(0);
3582
3583 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3584}
3585
3586void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003587 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003588 GenerateImplicitNullCheck(instruction);
3589 } else {
3590 GenerateExplicitNullCheck(instruction);
3591 }
3592}
3593
3594void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3595 HandleBinaryOp(instruction);
3596}
3597
3598void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3599 HandleBinaryOp(instruction);
3600}
3601
3602void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3603 LOG(FATAL) << "Unreachable";
3604}
3605
3606void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3607 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3608}
3609
3610void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3612 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3613 if (location.IsStackSlot()) {
3614 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3615 } else if (location.IsDoubleStackSlot()) {
3616 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3617 }
3618 locations->SetOut(location);
3619}
3620
3621void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3622 ATTRIBUTE_UNUSED) {
3623 // Nothing to do, the parameter is already at its location.
3624}
3625
3626void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3627 LocationSummary* locations =
3628 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3629 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3630}
3631
3632void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3633 ATTRIBUTE_UNUSED) {
3634 // Nothing to do, the method is already at its location.
3635}
3636
3637void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3639 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3640 locations->SetInAt(i, Location::Any());
3641 }
3642 locations->SetOut(Location::Any());
3643}
3644
3645void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3646 LOG(FATAL) << "Unreachable";
3647}
3648
3649void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3650 Primitive::Type type = rem->GetResultType();
3651 LocationSummary::CallKind call_kind =
3652 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3653 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3654
3655 switch (type) {
3656 case Primitive::kPrimInt:
3657 case Primitive::kPrimLong:
3658 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003659 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3661 break;
3662
3663 case Primitive::kPrimFloat:
3664 case Primitive::kPrimDouble: {
3665 InvokeRuntimeCallingConvention calling_convention;
3666 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3667 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3668 locations->SetOut(calling_convention.GetReturnLocation(type));
3669 break;
3670 }
3671
3672 default:
3673 LOG(FATAL) << "Unexpected rem type " << type;
3674 }
3675}
3676
3677void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3678 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003679
3680 switch (type) {
3681 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003682 case Primitive::kPrimLong:
3683 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003684 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003685
3686 case Primitive::kPrimFloat:
3687 case Primitive::kPrimDouble: {
3688 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3689 : QUICK_ENTRY_POINT(pFmod);
3690 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003691 if (type == Primitive::kPrimFloat) {
3692 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3693 } else {
3694 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3695 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003696 break;
3697 }
3698 default:
3699 LOG(FATAL) << "Unexpected rem type " << type;
3700 }
3701}
3702
3703void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3704 memory_barrier->SetLocations(nullptr);
3705}
3706
3707void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3708 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3709}
3710
3711void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3712 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3713 Primitive::Type return_type = ret->InputAt(0)->GetType();
3714 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3715}
3716
3717void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3718 codegen_->GenerateFrameExit();
3719}
3720
3721void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3722 ret->SetLocations(nullptr);
3723}
3724
3725void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3726 codegen_->GenerateFrameExit();
3727}
3728
Alexey Frunze92d90602015-12-18 18:16:36 -08003729void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3730 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003731}
3732
Alexey Frunze92d90602015-12-18 18:16:36 -08003733void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3734 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003735}
3736
Alexey Frunze4dda3372015-06-01 18:31:49 -07003737void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3738 HandleShift(shl);
3739}
3740
3741void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3742 HandleShift(shl);
3743}
3744
3745void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3746 HandleShift(shr);
3747}
3748
3749void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3750 HandleShift(shr);
3751}
3752
3753void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3754 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3755 Primitive::Type field_type = store->InputAt(1)->GetType();
3756 switch (field_type) {
3757 case Primitive::kPrimNot:
3758 case Primitive::kPrimBoolean:
3759 case Primitive::kPrimByte:
3760 case Primitive::kPrimChar:
3761 case Primitive::kPrimShort:
3762 case Primitive::kPrimInt:
3763 case Primitive::kPrimFloat:
3764 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3765 break;
3766
3767 case Primitive::kPrimLong:
3768 case Primitive::kPrimDouble:
3769 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3770 break;
3771
3772 default:
3773 LOG(FATAL) << "Unimplemented local type " << field_type;
3774 }
3775}
3776
3777void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3778}
3779
3780void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3781 HandleBinaryOp(instruction);
3782}
3783
3784void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3785 HandleBinaryOp(instruction);
3786}
3787
3788void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3789 HandleFieldGet(instruction, instruction->GetFieldInfo());
3790}
3791
3792void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3793 HandleFieldGet(instruction, instruction->GetFieldInfo());
3794}
3795
3796void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3797 HandleFieldSet(instruction, instruction->GetFieldInfo());
3798}
3799
3800void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003801 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003802}
3803
Calin Juravlee460d1d2015-09-29 04:52:17 +01003804void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3805 HUnresolvedInstanceFieldGet* instruction) {
3806 FieldAccessCallingConventionMIPS64 calling_convention;
3807 codegen_->CreateUnresolvedFieldLocationSummary(
3808 instruction, instruction->GetFieldType(), calling_convention);
3809}
3810
3811void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3812 HUnresolvedInstanceFieldGet* instruction) {
3813 FieldAccessCallingConventionMIPS64 calling_convention;
3814 codegen_->GenerateUnresolvedFieldAccess(instruction,
3815 instruction->GetFieldType(),
3816 instruction->GetFieldIndex(),
3817 instruction->GetDexPc(),
3818 calling_convention);
3819}
3820
3821void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3822 HUnresolvedInstanceFieldSet* instruction) {
3823 FieldAccessCallingConventionMIPS64 calling_convention;
3824 codegen_->CreateUnresolvedFieldLocationSummary(
3825 instruction, instruction->GetFieldType(), calling_convention);
3826}
3827
3828void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3829 HUnresolvedInstanceFieldSet* instruction) {
3830 FieldAccessCallingConventionMIPS64 calling_convention;
3831 codegen_->GenerateUnresolvedFieldAccess(instruction,
3832 instruction->GetFieldType(),
3833 instruction->GetFieldIndex(),
3834 instruction->GetDexPc(),
3835 calling_convention);
3836}
3837
3838void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3839 HUnresolvedStaticFieldGet* instruction) {
3840 FieldAccessCallingConventionMIPS64 calling_convention;
3841 codegen_->CreateUnresolvedFieldLocationSummary(
3842 instruction, instruction->GetFieldType(), calling_convention);
3843}
3844
3845void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3846 HUnresolvedStaticFieldGet* instruction) {
3847 FieldAccessCallingConventionMIPS64 calling_convention;
3848 codegen_->GenerateUnresolvedFieldAccess(instruction,
3849 instruction->GetFieldType(),
3850 instruction->GetFieldIndex(),
3851 instruction->GetDexPc(),
3852 calling_convention);
3853}
3854
3855void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3856 HUnresolvedStaticFieldSet* instruction) {
3857 FieldAccessCallingConventionMIPS64 calling_convention;
3858 codegen_->CreateUnresolvedFieldLocationSummary(
3859 instruction, instruction->GetFieldType(), calling_convention);
3860}
3861
3862void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3863 HUnresolvedStaticFieldSet* instruction) {
3864 FieldAccessCallingConventionMIPS64 calling_convention;
3865 codegen_->GenerateUnresolvedFieldAccess(instruction,
3866 instruction->GetFieldType(),
3867 instruction->GetFieldIndex(),
3868 instruction->GetDexPc(),
3869 calling_convention);
3870}
3871
Alexey Frunze4dda3372015-06-01 18:31:49 -07003872void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3873 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3874}
3875
3876void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3877 HBasicBlock* block = instruction->GetBlock();
3878 if (block->GetLoopInformation() != nullptr) {
3879 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3880 // The back edge will generate the suspend check.
3881 return;
3882 }
3883 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3884 // The goto will generate the suspend check.
3885 return;
3886 }
3887 GenerateSuspendCheck(instruction, nullptr);
3888}
3889
Alexey Frunze4dda3372015-06-01 18:31:49 -07003890void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3891 LocationSummary* locations =
3892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3893 InvokeRuntimeCallingConvention calling_convention;
3894 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3895}
3896
3897void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3898 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3899 instruction,
3900 instruction->GetDexPc(),
3901 nullptr);
3902 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3903}
3904
3905void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3906 Primitive::Type input_type = conversion->GetInputType();
3907 Primitive::Type result_type = conversion->GetResultType();
3908 DCHECK_NE(input_type, result_type);
3909
3910 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3911 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3912 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3913 }
3914
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003915 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3916
3917 if (Primitive::IsFloatingPointType(input_type)) {
3918 locations->SetInAt(0, Location::RequiresFpuRegister());
3919 } else {
3920 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003921 }
3922
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003923 if (Primitive::IsFloatingPointType(result_type)) {
3924 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003925 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003927 }
3928}
3929
3930void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3931 LocationSummary* locations = conversion->GetLocations();
3932 Primitive::Type result_type = conversion->GetResultType();
3933 Primitive::Type input_type = conversion->GetInputType();
3934
3935 DCHECK_NE(input_type, result_type);
3936
3937 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3938 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3939 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3940
3941 switch (result_type) {
3942 case Primitive::kPrimChar:
3943 __ Andi(dst, src, 0xFFFF);
3944 break;
3945 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003946 if (input_type == Primitive::kPrimLong) {
3947 // Type conversion from long to types narrower than int is a result of code
3948 // transformations. To avoid unpredictable results for SEB and SEH, we first
3949 // need to sign-extend the low 32-bit value into bits 32 through 63.
3950 __ Sll(dst, src, 0);
3951 __ Seb(dst, dst);
3952 } else {
3953 __ Seb(dst, src);
3954 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003955 break;
3956 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003957 if (input_type == Primitive::kPrimLong) {
3958 // Type conversion from long to types narrower than int is a result of code
3959 // transformations. To avoid unpredictable results for SEB and SEH, we first
3960 // need to sign-extend the low 32-bit value into bits 32 through 63.
3961 __ Sll(dst, src, 0);
3962 __ Seh(dst, dst);
3963 } else {
3964 __ Seh(dst, src);
3965 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003966 break;
3967 case Primitive::kPrimInt:
3968 case Primitive::kPrimLong:
3969 // Sign-extend 32-bit int into bits 32 through 63 for
3970 // int-to-long and long-to-int conversions
3971 __ Sll(dst, src, 0);
3972 break;
3973
3974 default:
3975 LOG(FATAL) << "Unexpected type conversion from " << input_type
3976 << " to " << result_type;
3977 }
3978 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003979 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3980 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3981 if (input_type == Primitive::kPrimLong) {
3982 __ Dmtc1(src, FTMP);
3983 if (result_type == Primitive::kPrimFloat) {
3984 __ Cvtsl(dst, FTMP);
3985 } else {
3986 __ Cvtdl(dst, FTMP);
3987 }
3988 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003989 __ Mtc1(src, FTMP);
3990 if (result_type == Primitive::kPrimFloat) {
3991 __ Cvtsw(dst, FTMP);
3992 } else {
3993 __ Cvtdw(dst, FTMP);
3994 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003995 }
3996 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3997 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003998 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3999 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4000 Mips64Label truncate;
4001 Mips64Label done;
4002
4003 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4004 // value when the input is either a NaN or is outside of the range of the output type
4005 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4006 // the same result.
4007 //
4008 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4009 // value of the output type if the input is outside of the range after the truncation or
4010 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4011 // results. This matches the desired float/double-to-int/long conversion exactly.
4012 //
4013 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4014 //
4015 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4016 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4017 // even though it must be NAN2008=1 on R6.
4018 //
4019 // The code takes care of the different behaviors by first comparing the input to the
4020 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4021 // If the input is greater than or equal to the minimum, it procedes to the truncate
4022 // instruction, which will handle such an input the same way irrespective of NAN2008.
4023 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4024 // in order to return either zero or the minimum value.
4025 //
4026 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4027 // truncate instruction for MIPS64R6.
4028 if (input_type == Primitive::kPrimFloat) {
4029 uint32_t min_val = (result_type == Primitive::kPrimLong)
4030 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4031 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4032 __ LoadConst32(TMP, min_val);
4033 __ Mtc1(TMP, FTMP);
4034 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004035 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004036 uint64_t min_val = (result_type == Primitive::kPrimLong)
4037 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4038 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4039 __ LoadConst64(TMP, min_val);
4040 __ Dmtc1(TMP, FTMP);
4041 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004042 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004043
4044 __ Bc1nez(FTMP, &truncate);
4045
4046 if (input_type == Primitive::kPrimFloat) {
4047 __ CmpEqS(FTMP, src, src);
4048 } else {
4049 __ CmpEqD(FTMP, src, src);
4050 }
4051 if (result_type == Primitive::kPrimLong) {
4052 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4053 } else {
4054 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4055 }
4056 __ Mfc1(TMP, FTMP);
4057 __ And(dst, dst, TMP);
4058
4059 __ Bc(&done);
4060
4061 __ Bind(&truncate);
4062
4063 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004064 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004065 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004066 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004067 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004068 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004069 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004070 } else {
4071 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004072 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004073 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004074 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004075 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004076 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004077 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004078
4079 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004080 } else if (Primitive::IsFloatingPointType(result_type) &&
4081 Primitive::IsFloatingPointType(input_type)) {
4082 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4083 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4084 if (result_type == Primitive::kPrimFloat) {
4085 __ Cvtsd(dst, src);
4086 } else {
4087 __ Cvtds(dst, src);
4088 }
4089 } else {
4090 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4091 << " to " << result_type;
4092 }
4093}
4094
4095void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4096 HandleShift(ushr);
4097}
4098
4099void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4100 HandleShift(ushr);
4101}
4102
4103void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4104 HandleBinaryOp(instruction);
4105}
4106
4107void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4108 HandleBinaryOp(instruction);
4109}
4110
4111void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4112 // Nothing to do, this should be removed during prepare for register allocator.
4113 LOG(FATAL) << "Unreachable";
4114}
4115
4116void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4117 // Nothing to do, this should be removed during prepare for register allocator.
4118 LOG(FATAL) << "Unreachable";
4119}
4120
4121void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004122 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004123}
4124
4125void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004126 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004127}
4128
4129void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004130 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004131}
4132
4133void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004134 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004135}
4136
4137void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004138 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004139}
4140
4141void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004142 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004143}
4144
4145void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004146 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004147}
4148
4149void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004150 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004151}
4152
4153void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004154 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004155}
4156
4157void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004158 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004159}
4160
4161void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004162 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004163}
4164
4165void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004166 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004167}
4168
Aart Bike9f37602015-10-09 11:15:55 -07004169void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004170 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004171}
4172
4173void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004174 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004175}
4176
4177void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004178 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004179}
4180
4181void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004182 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004183}
4184
4185void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004186 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004187}
4188
4189void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004190 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004191}
4192
4193void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004194 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004195}
4196
4197void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004198 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004199}
4200
Mark Mendellfe57faa2015-09-18 09:26:15 -04004201// Simple implementation of packed switch - generate cascaded compare/jumps.
4202void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4203 LocationSummary* locations =
4204 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4205 locations->SetInAt(0, Location::RequiresRegister());
4206}
4207
4208void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4209 int32_t lower_bound = switch_instr->GetStartValue();
4210 int32_t num_entries = switch_instr->GetNumEntries();
4211 LocationSummary* locations = switch_instr->GetLocations();
4212 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4213 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4214
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004215 // Create a set of compare/jumps.
4216 GpuRegister temp_reg = TMP;
4217 if (IsInt<16>(-lower_bound)) {
4218 __ Addiu(temp_reg, value_reg, -lower_bound);
4219 } else {
4220 __ LoadConst32(AT, -lower_bound);
4221 __ Addu(temp_reg, value_reg, AT);
4222 }
4223 // Jump to default if index is negative
4224 // Note: We don't check the case that index is positive while value < lower_bound, because in
4225 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4226 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4227
Mark Mendellfe57faa2015-09-18 09:26:15 -04004228 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004229 // Jump to successors[0] if value == lower_bound.
4230 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4231 int32_t last_index = 0;
4232 for (; num_entries - last_index > 2; last_index += 2) {
4233 __ Addiu(temp_reg, temp_reg, -2);
4234 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4235 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4236 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4237 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4238 }
4239 if (num_entries - last_index == 2) {
4240 // The last missing case_value.
4241 __ Addiu(temp_reg, temp_reg, -1);
4242 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004243 }
4244
4245 // And the default for any other value.
4246 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004247 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004248 }
4249}
4250
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004251void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4252 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4253}
4254
4255void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4256 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4257}
4258
Alexey Frunze4dda3372015-06-01 18:31:49 -07004259} // namespace mips64
4260} // namespace art
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004261