blob: da98a89f65388c04dafb071527c57622886d4edd [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
872void CodeGeneratorMIPS64::Move(HInstruction* instruction,
873 Location location,
874 HInstruction* move_for) {
875 LocationSummary* locations = instruction->GetLocations();
876 Primitive::Type type = instruction->GetType();
877 DCHECK_NE(type, Primitive::kPrimVoid);
878
879 if (instruction->IsCurrentMethod()) {
880 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset), type);
881 } else if (locations != nullptr && locations->Out().Equals(location)) {
882 return;
883 } else if (instruction->IsIntConstant()
884 || instruction->IsLongConstant()
885 || instruction->IsNullConstant()) {
886 if (location.IsRegister()) {
887 // Move to GPR from constant
888 GpuRegister dst = location.AsRegister<GpuRegister>();
889 if (instruction->IsNullConstant() || instruction->IsIntConstant()) {
890 __ LoadConst32(dst, GetInt32ValueOf(instruction->AsConstant()));
891 } else {
892 __ LoadConst64(dst, instruction->AsLongConstant()->GetValue());
893 }
894 } else {
895 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
896 // Move to stack from constant
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700897 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700898 if (location.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700899 int32_t value = GetInt32ValueOf(instruction->AsConstant());
900 if (value != 0) {
901 gpr = TMP;
902 __ LoadConst32(gpr, value);
903 }
904 __ StoreToOffset(kStoreWord, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700905 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700906 DCHECK(location.IsDoubleStackSlot());
907 int64_t value = instruction->AsLongConstant()->GetValue();
908 if (value != 0) {
909 gpr = TMP;
910 __ LoadConst64(gpr, value);
911 }
912 __ StoreToOffset(kStoreDoubleword, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700913 }
914 }
915 } else if (instruction->IsTemporary()) {
916 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
917 MoveLocation(location, temp_location, type);
918 } else if (instruction->IsLoadLocal()) {
919 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
920 if (Primitive::Is64BitType(type)) {
921 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
922 } else {
923 MoveLocation(location, Location::StackSlot(stack_slot), type);
924 }
925 } else {
926 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
927 MoveLocation(location, locations->Out(), type);
928 }
929}
930
Calin Juravle175dc732015-08-25 15:42:32 +0100931void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
932 DCHECK(location.IsRegister());
933 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
934}
935
Calin Juravlee460d1d2015-09-29 04:52:17 +0100936void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
937 if (location.IsRegister()) {
938 locations->AddTemp(location);
939 } else {
940 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
941 }
942}
943
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
945 Primitive::Type type = load->GetType();
946
947 switch (type) {
948 case Primitive::kPrimNot:
949 case Primitive::kPrimInt:
950 case Primitive::kPrimFloat:
951 return Location::StackSlot(GetStackSlot(load->GetLocal()));
952
953 case Primitive::kPrimLong:
954 case Primitive::kPrimDouble:
955 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
956
957 case Primitive::kPrimBoolean:
958 case Primitive::kPrimByte:
959 case Primitive::kPrimChar:
960 case Primitive::kPrimShort:
961 case Primitive::kPrimVoid:
962 LOG(FATAL) << "Unexpected type " << type;
963 }
964
965 LOG(FATAL) << "Unreachable";
966 return Location::NoLocation();
967}
968
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100969void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
970 GpuRegister value,
971 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700972 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700973 GpuRegister card = AT;
974 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100975 if (value_can_be_null) {
976 __ Beqzc(value, &done);
977 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700978 __ LoadFromOffset(kLoadDoubleword,
979 card,
980 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200981 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700982 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
983 __ Daddu(temp, card, temp);
984 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100985 if (value_can_be_null) {
986 __ Bind(&done);
987 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700988}
989
David Brazdil58282f42016-01-14 12:45:10 +0000990void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700991 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
992 blocked_core_registers_[ZERO] = true;
993 blocked_core_registers_[K0] = true;
994 blocked_core_registers_[K1] = true;
995 blocked_core_registers_[GP] = true;
996 blocked_core_registers_[SP] = true;
997 blocked_core_registers_[RA] = true;
998
Lazar Trsicd9672662015-09-03 17:33:01 +0200999 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1000 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001001 blocked_core_registers_[AT] = true;
1002 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001003 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001004 blocked_fpu_registers_[FTMP] = true;
1005
1006 // Reserve suspend and thread registers.
1007 blocked_core_registers_[S0] = true;
1008 blocked_core_registers_[TR] = true;
1009
1010 // Reserve T9 for function calls
1011 blocked_core_registers_[T9] = true;
1012
1013 // TODO: review; anything else?
1014
David Brazdil58282f42016-01-14 12:45:10 +00001015 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001016 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1017 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1018 }
1019
1020 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1021 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1022 }
1023}
1024
Alexey Frunze4dda3372015-06-01 18:31:49 -07001025size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1026 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001027 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001028}
1029
1030size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1031 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001032 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001033}
1034
1035size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1036 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001037 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001038}
1039
1040size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1041 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001042 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001043}
1044
1045void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001046 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001047}
1048
1049void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001050 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001051}
1052
Calin Juravle175dc732015-08-25 15:42:32 +01001053void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1054 HInstruction* instruction,
1055 uint32_t dex_pc,
1056 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001057 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001058 instruction,
1059 dex_pc,
1060 slow_path);
1061}
1062
Alexey Frunze4dda3372015-06-01 18:31:49 -07001063void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
1064 HInstruction* instruction,
1065 uint32_t dex_pc,
1066 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001067 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001068 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1069 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1070 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001071 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001072 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001073}
1074
1075void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1076 GpuRegister class_reg) {
1077 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1078 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1079 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1080 // TODO: barrier needed?
1081 __ Bind(slow_path->GetExitLabel());
1082}
1083
1084void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1085 __ Sync(0); // only stype 0 is supported
1086}
1087
1088void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1089 HBasicBlock* successor) {
1090 SuspendCheckSlowPathMIPS64* slow_path =
1091 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1092 codegen_->AddSlowPath(slow_path);
1093
1094 __ LoadFromOffset(kLoadUnsignedHalfword,
1095 TMP,
1096 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001097 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098 if (successor == nullptr) {
1099 __ Bnezc(TMP, slow_path->GetEntryLabel());
1100 __ Bind(slow_path->GetReturnLabel());
1101 } else {
1102 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001103 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001104 // slow_path will return to GetLabelOf(successor).
1105 }
1106}
1107
1108InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1109 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001110 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001111 assembler_(codegen->GetAssembler()),
1112 codegen_(codegen) {}
1113
1114void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1115 DCHECK_EQ(instruction->InputCount(), 2U);
1116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1117 Primitive::Type type = instruction->GetResultType();
1118 switch (type) {
1119 case Primitive::kPrimInt:
1120 case Primitive::kPrimLong: {
1121 locations->SetInAt(0, Location::RequiresRegister());
1122 HInstruction* right = instruction->InputAt(1);
1123 bool can_use_imm = false;
1124 if (right->IsConstant()) {
1125 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1126 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1127 can_use_imm = IsUint<16>(imm);
1128 } else if (instruction->IsAdd()) {
1129 can_use_imm = IsInt<16>(imm);
1130 } else {
1131 DCHECK(instruction->IsSub());
1132 can_use_imm = IsInt<16>(-imm);
1133 }
1134 }
1135 if (can_use_imm)
1136 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1137 else
1138 locations->SetInAt(1, Location::RequiresRegister());
1139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1140 }
1141 break;
1142
1143 case Primitive::kPrimFloat:
1144 case Primitive::kPrimDouble:
1145 locations->SetInAt(0, Location::RequiresFpuRegister());
1146 locations->SetInAt(1, Location::RequiresFpuRegister());
1147 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1148 break;
1149
1150 default:
1151 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1152 }
1153}
1154
1155void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1156 Primitive::Type type = instruction->GetType();
1157 LocationSummary* locations = instruction->GetLocations();
1158
1159 switch (type) {
1160 case Primitive::kPrimInt:
1161 case Primitive::kPrimLong: {
1162 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1163 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1164 Location rhs_location = locations->InAt(1);
1165
1166 GpuRegister rhs_reg = ZERO;
1167 int64_t rhs_imm = 0;
1168 bool use_imm = rhs_location.IsConstant();
1169 if (use_imm) {
1170 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1171 } else {
1172 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1173 }
1174
1175 if (instruction->IsAnd()) {
1176 if (use_imm)
1177 __ Andi(dst, lhs, rhs_imm);
1178 else
1179 __ And(dst, lhs, rhs_reg);
1180 } else if (instruction->IsOr()) {
1181 if (use_imm)
1182 __ Ori(dst, lhs, rhs_imm);
1183 else
1184 __ Or(dst, lhs, rhs_reg);
1185 } else if (instruction->IsXor()) {
1186 if (use_imm)
1187 __ Xori(dst, lhs, rhs_imm);
1188 else
1189 __ Xor(dst, lhs, rhs_reg);
1190 } else if (instruction->IsAdd()) {
1191 if (type == Primitive::kPrimInt) {
1192 if (use_imm)
1193 __ Addiu(dst, lhs, rhs_imm);
1194 else
1195 __ Addu(dst, lhs, rhs_reg);
1196 } else {
1197 if (use_imm)
1198 __ Daddiu(dst, lhs, rhs_imm);
1199 else
1200 __ Daddu(dst, lhs, rhs_reg);
1201 }
1202 } else {
1203 DCHECK(instruction->IsSub());
1204 if (type == Primitive::kPrimInt) {
1205 if (use_imm)
1206 __ Addiu(dst, lhs, -rhs_imm);
1207 else
1208 __ Subu(dst, lhs, rhs_reg);
1209 } else {
1210 if (use_imm)
1211 __ Daddiu(dst, lhs, -rhs_imm);
1212 else
1213 __ Dsubu(dst, lhs, rhs_reg);
1214 }
1215 }
1216 break;
1217 }
1218 case Primitive::kPrimFloat:
1219 case Primitive::kPrimDouble: {
1220 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1221 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1222 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1223 if (instruction->IsAdd()) {
1224 if (type == Primitive::kPrimFloat)
1225 __ AddS(dst, lhs, rhs);
1226 else
1227 __ AddD(dst, lhs, rhs);
1228 } else if (instruction->IsSub()) {
1229 if (type == Primitive::kPrimFloat)
1230 __ SubS(dst, lhs, rhs);
1231 else
1232 __ SubD(dst, lhs, rhs);
1233 } else {
1234 LOG(FATAL) << "Unexpected floating-point binary operation";
1235 }
1236 break;
1237 }
1238 default:
1239 LOG(FATAL) << "Unexpected binary operation type " << type;
1240 }
1241}
1242
1243void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001244 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001245
1246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1247 Primitive::Type type = instr->GetResultType();
1248 switch (type) {
1249 case Primitive::kPrimInt:
1250 case Primitive::kPrimLong: {
1251 locations->SetInAt(0, Location::RequiresRegister());
1252 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001254 break;
1255 }
1256 default:
1257 LOG(FATAL) << "Unexpected shift type " << type;
1258 }
1259}
1260
1261void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001262 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001263 LocationSummary* locations = instr->GetLocations();
1264 Primitive::Type type = instr->GetType();
1265
1266 switch (type) {
1267 case Primitive::kPrimInt:
1268 case Primitive::kPrimLong: {
1269 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1270 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1271 Location rhs_location = locations->InAt(1);
1272
1273 GpuRegister rhs_reg = ZERO;
1274 int64_t rhs_imm = 0;
1275 bool use_imm = rhs_location.IsConstant();
1276 if (use_imm) {
1277 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1278 } else {
1279 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1280 }
1281
1282 if (use_imm) {
1283 uint32_t shift_value = (type == Primitive::kPrimInt)
1284 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1285 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1286
Alexey Frunze92d90602015-12-18 18:16:36 -08001287 if (shift_value == 0) {
1288 if (dst != lhs) {
1289 __ Move(dst, lhs);
1290 }
1291 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001292 if (instr->IsShl()) {
1293 __ Sll(dst, lhs, shift_value);
1294 } else if (instr->IsShr()) {
1295 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001296 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001298 } else {
1299 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001300 }
1301 } else {
1302 if (shift_value < 32) {
1303 if (instr->IsShl()) {
1304 __ Dsll(dst, lhs, shift_value);
1305 } else if (instr->IsShr()) {
1306 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001307 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001308 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001309 } else {
1310 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001311 }
1312 } else {
1313 shift_value -= 32;
1314 if (instr->IsShl()) {
1315 __ Dsll32(dst, lhs, shift_value);
1316 } else if (instr->IsShr()) {
1317 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001318 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001319 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001320 } else {
1321 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001322 }
1323 }
1324 }
1325 } else {
1326 if (type == Primitive::kPrimInt) {
1327 if (instr->IsShl()) {
1328 __ Sllv(dst, lhs, rhs_reg);
1329 } else if (instr->IsShr()) {
1330 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001331 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001332 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001333 } else {
1334 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001335 }
1336 } else {
1337 if (instr->IsShl()) {
1338 __ Dsllv(dst, lhs, rhs_reg);
1339 } else if (instr->IsShr()) {
1340 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001341 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001342 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001343 } else {
1344 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001345 }
1346 }
1347 }
1348 break;
1349 }
1350 default:
1351 LOG(FATAL) << "Unexpected shift operation type " << type;
1352 }
1353}
1354
1355void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1356 HandleBinaryOp(instruction);
1357}
1358
1359void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1360 HandleBinaryOp(instruction);
1361}
1362
1363void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1364 HandleBinaryOp(instruction);
1365}
1366
1367void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1368 HandleBinaryOp(instruction);
1369}
1370
1371void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1372 LocationSummary* locations =
1373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1374 locations->SetInAt(0, Location::RequiresRegister());
1375 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1376 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1377 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1378 } else {
1379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1380 }
1381}
1382
1383void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1384 LocationSummary* locations = instruction->GetLocations();
1385 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1386 Location index = locations->InAt(1);
1387 Primitive::Type type = instruction->GetType();
1388
1389 switch (type) {
1390 case Primitive::kPrimBoolean: {
1391 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1392 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1393 if (index.IsConstant()) {
1394 size_t offset =
1395 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1396 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1397 } else {
1398 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1399 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1400 }
1401 break;
1402 }
1403
1404 case Primitive::kPrimByte: {
1405 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1406 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1407 if (index.IsConstant()) {
1408 size_t offset =
1409 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1410 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1411 } else {
1412 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1413 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1414 }
1415 break;
1416 }
1417
1418 case Primitive::kPrimShort: {
1419 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1420 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1421 if (index.IsConstant()) {
1422 size_t offset =
1423 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1424 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1425 } else {
1426 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1427 __ Daddu(TMP, obj, TMP);
1428 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1429 }
1430 break;
1431 }
1432
1433 case Primitive::kPrimChar: {
1434 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1435 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1436 if (index.IsConstant()) {
1437 size_t offset =
1438 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1439 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1440 } else {
1441 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1442 __ Daddu(TMP, obj, TMP);
1443 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1444 }
1445 break;
1446 }
1447
1448 case Primitive::kPrimInt:
1449 case Primitive::kPrimNot: {
1450 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1451 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1452 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1453 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1454 if (index.IsConstant()) {
1455 size_t offset =
1456 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1457 __ LoadFromOffset(load_type, out, obj, offset);
1458 } else {
1459 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1460 __ Daddu(TMP, obj, TMP);
1461 __ LoadFromOffset(load_type, out, TMP, data_offset);
1462 }
1463 break;
1464 }
1465
1466 case Primitive::kPrimLong: {
1467 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1468 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1469 if (index.IsConstant()) {
1470 size_t offset =
1471 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1472 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1473 } else {
1474 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1475 __ Daddu(TMP, obj, TMP);
1476 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1477 }
1478 break;
1479 }
1480
1481 case Primitive::kPrimFloat: {
1482 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1483 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1484 if (index.IsConstant()) {
1485 size_t offset =
1486 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1487 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1488 } else {
1489 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1490 __ Daddu(TMP, obj, TMP);
1491 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1492 }
1493 break;
1494 }
1495
1496 case Primitive::kPrimDouble: {
1497 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1498 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1499 if (index.IsConstant()) {
1500 size_t offset =
1501 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1502 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1503 } else {
1504 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1505 __ Daddu(TMP, obj, TMP);
1506 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1507 }
1508 break;
1509 }
1510
1511 case Primitive::kPrimVoid:
1512 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1513 UNREACHABLE();
1514 }
1515 codegen_->MaybeRecordImplicitNullCheck(instruction);
1516}
1517
1518void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1520 locations->SetInAt(0, Location::RequiresRegister());
1521 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1522}
1523
1524void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1525 LocationSummary* locations = instruction->GetLocations();
1526 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1527 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1528 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1529 __ LoadFromOffset(kLoadWord, out, obj, offset);
1530 codegen_->MaybeRecordImplicitNullCheck(instruction);
1531}
1532
1533void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001534 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1536 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001537 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1538 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001539 InvokeRuntimeCallingConvention calling_convention;
1540 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1541 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1542 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1543 } else {
1544 locations->SetInAt(0, Location::RequiresRegister());
1545 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1546 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1547 locations->SetInAt(2, Location::RequiresFpuRegister());
1548 } else {
1549 locations->SetInAt(2, Location::RequiresRegister());
1550 }
1551 }
1552}
1553
1554void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1555 LocationSummary* locations = instruction->GetLocations();
1556 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1557 Location index = locations->InAt(1);
1558 Primitive::Type value_type = instruction->GetComponentType();
1559 bool needs_runtime_call = locations->WillCall();
1560 bool needs_write_barrier =
1561 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1562
1563 switch (value_type) {
1564 case Primitive::kPrimBoolean:
1565 case Primitive::kPrimByte: {
1566 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1567 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1568 if (index.IsConstant()) {
1569 size_t offset =
1570 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1571 __ StoreToOffset(kStoreByte, value, obj, offset);
1572 } else {
1573 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1574 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1575 }
1576 break;
1577 }
1578
1579 case Primitive::kPrimShort:
1580 case Primitive::kPrimChar: {
1581 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1582 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1583 if (index.IsConstant()) {
1584 size_t offset =
1585 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1586 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1587 } else {
1588 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1589 __ Daddu(TMP, obj, TMP);
1590 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1591 }
1592 break;
1593 }
1594
1595 case Primitive::kPrimInt:
1596 case Primitive::kPrimNot: {
1597 if (!needs_runtime_call) {
1598 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1599 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1600 if (index.IsConstant()) {
1601 size_t offset =
1602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1603 __ StoreToOffset(kStoreWord, value, obj, offset);
1604 } else {
1605 DCHECK(index.IsRegister()) << index;
1606 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1607 __ Daddu(TMP, obj, TMP);
1608 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1609 }
1610 codegen_->MaybeRecordImplicitNullCheck(instruction);
1611 if (needs_write_barrier) {
1612 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001613 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001614 }
1615 } else {
1616 DCHECK_EQ(value_type, Primitive::kPrimNot);
1617 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1618 instruction,
1619 instruction->GetDexPc(),
1620 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001621 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001622 }
1623 break;
1624 }
1625
1626 case Primitive::kPrimLong: {
1627 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1628 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1629 if (index.IsConstant()) {
1630 size_t offset =
1631 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1632 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1633 } else {
1634 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1635 __ Daddu(TMP, obj, TMP);
1636 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1637 }
1638 break;
1639 }
1640
1641 case Primitive::kPrimFloat: {
1642 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1643 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1644 DCHECK(locations->InAt(2).IsFpuRegister());
1645 if (index.IsConstant()) {
1646 size_t offset =
1647 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1648 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1649 } else {
1650 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1651 __ Daddu(TMP, obj, TMP);
1652 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1653 }
1654 break;
1655 }
1656
1657 case Primitive::kPrimDouble: {
1658 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1659 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1660 DCHECK(locations->InAt(2).IsFpuRegister());
1661 if (index.IsConstant()) {
1662 size_t offset =
1663 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1664 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1665 } else {
1666 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1667 __ Daddu(TMP, obj, TMP);
1668 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1669 }
1670 break;
1671 }
1672
1673 case Primitive::kPrimVoid:
1674 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1675 UNREACHABLE();
1676 }
1677
1678 // Ints and objects are handled in the switch.
1679 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1680 codegen_->MaybeRecordImplicitNullCheck(instruction);
1681 }
1682}
1683
1684void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001685 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1686 ? LocationSummary::kCallOnSlowPath
1687 : LocationSummary::kNoCall;
1688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001689 locations->SetInAt(0, Location::RequiresRegister());
1690 locations->SetInAt(1, Location::RequiresRegister());
1691 if (instruction->HasUses()) {
1692 locations->SetOut(Location::SameAsFirstInput());
1693 }
1694}
1695
1696void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1697 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001698 BoundsCheckSlowPathMIPS64* slow_path =
1699 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001700 codegen_->AddSlowPath(slow_path);
1701
1702 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1703 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1704
1705 // length is limited by the maximum positive signed 32-bit integer.
1706 // Unsigned comparison of length and index checks for index < 0
1707 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001708 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001709}
1710
1711void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1712 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1713 instruction,
1714 LocationSummary::kCallOnSlowPath);
1715 locations->SetInAt(0, Location::RequiresRegister());
1716 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001717 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001718 locations->AddTemp(Location::RequiresRegister());
1719}
1720
1721void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1722 LocationSummary* locations = instruction->GetLocations();
1723 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1724 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1725 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1726
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001727 SlowPathCodeMIPS64* slow_path =
1728 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001729 codegen_->AddSlowPath(slow_path);
1730
1731 // TODO: avoid this check if we know obj is not null.
1732 __ Beqzc(obj, slow_path->GetExitLabel());
1733 // Compare the class of `obj` with `cls`.
1734 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1735 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1736 __ Bind(slow_path->GetExitLabel());
1737}
1738
1739void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1740 LocationSummary* locations =
1741 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1742 locations->SetInAt(0, Location::RequiresRegister());
1743 if (check->HasUses()) {
1744 locations->SetOut(Location::SameAsFirstInput());
1745 }
1746}
1747
1748void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1749 // We assume the class is not null.
1750 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1751 check->GetLoadClass(),
1752 check,
1753 check->GetDexPc(),
1754 true);
1755 codegen_->AddSlowPath(slow_path);
1756 GenerateClassInitializationCheck(slow_path,
1757 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1758}
1759
1760void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1761 Primitive::Type in_type = compare->InputAt(0)->GetType();
1762
Alexey Frunze299a9392015-12-08 16:08:02 -08001763 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001764
1765 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08001766 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001767 case Primitive::kPrimLong:
1768 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001769 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1771 break;
1772
1773 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001774 case Primitive::kPrimDouble:
1775 locations->SetInAt(0, Location::RequiresFpuRegister());
1776 locations->SetInAt(1, Location::RequiresFpuRegister());
1777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001778 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779
1780 default:
1781 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1782 }
1783}
1784
1785void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1786 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001787 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001788 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08001789 bool gt_bias = instruction->IsGtBias();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001790
1791 // 0 if: left == right
1792 // 1 if: left > right
1793 // -1 if: left < right
1794 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08001795 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001796 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001797 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001798 Location rhs_location = locations->InAt(1);
1799 bool use_imm = rhs_location.IsConstant();
1800 GpuRegister rhs = ZERO;
1801 if (use_imm) {
Aart Bika19616e2016-02-01 18:57:58 -08001802 if (in_type == Primitive::kPrimInt) {
1803 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1804 if (value != 0) {
1805 rhs = AT;
1806 __ LoadConst32(rhs, value);
1807 }
1808 } else {
1809 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1810 if (value != 0) {
1811 rhs = AT;
1812 __ LoadConst64(rhs, value);
1813 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001814 }
1815 } else {
1816 rhs = rhs_location.AsRegister<GpuRegister>();
1817 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001818 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001819 __ Slt(res, rhs, lhs);
1820 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001821 break;
1822 }
1823
Alexey Frunze299a9392015-12-08 16:08:02 -08001824 case Primitive::kPrimFloat: {
1825 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1826 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1827 Mips64Label done;
1828 __ CmpEqS(FTMP, lhs, rhs);
1829 __ LoadConst32(res, 0);
1830 __ Bc1nez(FTMP, &done);
1831 if (gt_bias) {
1832 __ CmpLtS(FTMP, lhs, rhs);
1833 __ LoadConst32(res, -1);
1834 __ Bc1nez(FTMP, &done);
1835 __ LoadConst32(res, 1);
1836 } else {
1837 __ CmpLtS(FTMP, rhs, lhs);
1838 __ LoadConst32(res, 1);
1839 __ Bc1nez(FTMP, &done);
1840 __ LoadConst32(res, -1);
1841 }
1842 __ Bind(&done);
1843 break;
1844 }
1845
Alexey Frunze4dda3372015-06-01 18:31:49 -07001846 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001847 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1848 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1849 Mips64Label done;
1850 __ CmpEqD(FTMP, lhs, rhs);
1851 __ LoadConst32(res, 0);
1852 __ Bc1nez(FTMP, &done);
1853 if (gt_bias) {
1854 __ CmpLtD(FTMP, lhs, rhs);
1855 __ LoadConst32(res, -1);
1856 __ Bc1nez(FTMP, &done);
1857 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001858 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001859 __ CmpLtD(FTMP, rhs, lhs);
1860 __ LoadConst32(res, 1);
1861 __ Bc1nez(FTMP, &done);
1862 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001863 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001864 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001865 break;
1866 }
1867
1868 default:
1869 LOG(FATAL) << "Unimplemented compare type " << in_type;
1870 }
1871}
1872
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001873void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001874 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001875 switch (instruction->InputAt(0)->GetType()) {
1876 default:
1877 case Primitive::kPrimLong:
1878 locations->SetInAt(0, Location::RequiresRegister());
1879 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1880 break;
1881
1882 case Primitive::kPrimFloat:
1883 case Primitive::kPrimDouble:
1884 locations->SetInAt(0, Location::RequiresFpuRegister());
1885 locations->SetInAt(1, Location::RequiresFpuRegister());
1886 break;
1887 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001888 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001889 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1890 }
1891}
1892
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001893void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001894 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001895 return;
1896 }
1897
Alexey Frunze299a9392015-12-08 16:08:02 -08001898 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001899 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001900 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001901 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001902
Alexey Frunze299a9392015-12-08 16:08:02 -08001903 switch (type) {
1904 default:
1905 // Integer case.
1906 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1907 return;
1908 case Primitive::kPrimLong:
1909 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1910 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001911
Alexey Frunze299a9392015-12-08 16:08:02 -08001912 case Primitive::kPrimFloat:
1913 case Primitive::kPrimDouble:
1914 // TODO: don't use branches.
1915 GenerateFpCompareAndBranch(instruction->GetCondition(),
1916 instruction->IsGtBias(),
1917 type,
1918 locations,
1919 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001920 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001921 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001922
1923 // Convert the branches into the result.
1924 Mips64Label done;
1925
1926 // False case: result = 0.
1927 __ LoadConst32(dst, 0);
1928 __ Bc(&done);
1929
1930 // True case: result = 1.
1931 __ Bind(&true_label);
1932 __ LoadConst32(dst, 1);
1933 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001934}
1935
Alexey Frunzec857c742015-09-23 15:12:39 -07001936void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1937 DCHECK(instruction->IsDiv() || instruction->IsRem());
1938 Primitive::Type type = instruction->GetResultType();
1939
1940 LocationSummary* locations = instruction->GetLocations();
1941 Location second = locations->InAt(1);
1942 DCHECK(second.IsConstant());
1943
1944 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1945 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1946 int64_t imm = Int64FromConstant(second.GetConstant());
1947 DCHECK(imm == 1 || imm == -1);
1948
1949 if (instruction->IsRem()) {
1950 __ Move(out, ZERO);
1951 } else {
1952 if (imm == -1) {
1953 if (type == Primitive::kPrimInt) {
1954 __ Subu(out, ZERO, dividend);
1955 } else {
1956 DCHECK_EQ(type, Primitive::kPrimLong);
1957 __ Dsubu(out, ZERO, dividend);
1958 }
1959 } else if (out != dividend) {
1960 __ Move(out, dividend);
1961 }
1962 }
1963}
1964
1965void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1966 DCHECK(instruction->IsDiv() || instruction->IsRem());
1967 Primitive::Type type = instruction->GetResultType();
1968
1969 LocationSummary* locations = instruction->GetLocations();
1970 Location second = locations->InAt(1);
1971 DCHECK(second.IsConstant());
1972
1973 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1974 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1975 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001976 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001977 int ctz_imm = CTZ(abs_imm);
1978
1979 if (instruction->IsDiv()) {
1980 if (type == Primitive::kPrimInt) {
1981 if (ctz_imm == 1) {
1982 // Fast path for division by +/-2, which is very common.
1983 __ Srl(TMP, dividend, 31);
1984 } else {
1985 __ Sra(TMP, dividend, 31);
1986 __ Srl(TMP, TMP, 32 - ctz_imm);
1987 }
1988 __ Addu(out, dividend, TMP);
1989 __ Sra(out, out, ctz_imm);
1990 if (imm < 0) {
1991 __ Subu(out, ZERO, out);
1992 }
1993 } else {
1994 DCHECK_EQ(type, Primitive::kPrimLong);
1995 if (ctz_imm == 1) {
1996 // Fast path for division by +/-2, which is very common.
1997 __ Dsrl32(TMP, dividend, 31);
1998 } else {
1999 __ Dsra32(TMP, dividend, 31);
2000 if (ctz_imm > 32) {
2001 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2002 } else {
2003 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2004 }
2005 }
2006 __ Daddu(out, dividend, TMP);
2007 if (ctz_imm < 32) {
2008 __ Dsra(out, out, ctz_imm);
2009 } else {
2010 __ Dsra32(out, out, ctz_imm - 32);
2011 }
2012 if (imm < 0) {
2013 __ Dsubu(out, ZERO, out);
2014 }
2015 }
2016 } else {
2017 if (type == Primitive::kPrimInt) {
2018 if (ctz_imm == 1) {
2019 // Fast path for modulo +/-2, which is very common.
2020 __ Sra(TMP, dividend, 31);
2021 __ Subu(out, dividend, TMP);
2022 __ Andi(out, out, 1);
2023 __ Addu(out, out, TMP);
2024 } else {
2025 __ Sra(TMP, dividend, 31);
2026 __ Srl(TMP, TMP, 32 - ctz_imm);
2027 __ Addu(out, dividend, TMP);
2028 if (IsUint<16>(abs_imm - 1)) {
2029 __ Andi(out, out, abs_imm - 1);
2030 } else {
2031 __ Sll(out, out, 32 - ctz_imm);
2032 __ Srl(out, out, 32 - ctz_imm);
2033 }
2034 __ Subu(out, out, TMP);
2035 }
2036 } else {
2037 DCHECK_EQ(type, Primitive::kPrimLong);
2038 if (ctz_imm == 1) {
2039 // Fast path for modulo +/-2, which is very common.
2040 __ Dsra32(TMP, dividend, 31);
2041 __ Dsubu(out, dividend, TMP);
2042 __ Andi(out, out, 1);
2043 __ Daddu(out, out, TMP);
2044 } else {
2045 __ Dsra32(TMP, dividend, 31);
2046 if (ctz_imm > 32) {
2047 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2048 } else {
2049 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2050 }
2051 __ Daddu(out, dividend, TMP);
2052 if (IsUint<16>(abs_imm - 1)) {
2053 __ Andi(out, out, abs_imm - 1);
2054 } else {
2055 if (ctz_imm > 32) {
2056 __ Dsll(out, out, 64 - ctz_imm);
2057 __ Dsrl(out, out, 64 - ctz_imm);
2058 } else {
2059 __ Dsll32(out, out, 32 - ctz_imm);
2060 __ Dsrl32(out, out, 32 - ctz_imm);
2061 }
2062 }
2063 __ Dsubu(out, out, TMP);
2064 }
2065 }
2066 }
2067}
2068
2069void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2070 DCHECK(instruction->IsDiv() || instruction->IsRem());
2071
2072 LocationSummary* locations = instruction->GetLocations();
2073 Location second = locations->InAt(1);
2074 DCHECK(second.IsConstant());
2075
2076 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2077 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2078 int64_t imm = Int64FromConstant(second.GetConstant());
2079
2080 Primitive::Type type = instruction->GetResultType();
2081 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2082
2083 int64_t magic;
2084 int shift;
2085 CalculateMagicAndShiftForDivRem(imm,
2086 (type == Primitive::kPrimLong),
2087 &magic,
2088 &shift);
2089
2090 if (type == Primitive::kPrimInt) {
2091 __ LoadConst32(TMP, magic);
2092 __ MuhR6(TMP, dividend, TMP);
2093
2094 if (imm > 0 && magic < 0) {
2095 __ Addu(TMP, TMP, dividend);
2096 } else if (imm < 0 && magic > 0) {
2097 __ Subu(TMP, TMP, dividend);
2098 }
2099
2100 if (shift != 0) {
2101 __ Sra(TMP, TMP, shift);
2102 }
2103
2104 if (instruction->IsDiv()) {
2105 __ Sra(out, TMP, 31);
2106 __ Subu(out, TMP, out);
2107 } else {
2108 __ Sra(AT, TMP, 31);
2109 __ Subu(AT, TMP, AT);
2110 __ LoadConst32(TMP, imm);
2111 __ MulR6(TMP, AT, TMP);
2112 __ Subu(out, dividend, TMP);
2113 }
2114 } else {
2115 __ LoadConst64(TMP, magic);
2116 __ Dmuh(TMP, dividend, TMP);
2117
2118 if (imm > 0 && magic < 0) {
2119 __ Daddu(TMP, TMP, dividend);
2120 } else if (imm < 0 && magic > 0) {
2121 __ Dsubu(TMP, TMP, dividend);
2122 }
2123
2124 if (shift >= 32) {
2125 __ Dsra32(TMP, TMP, shift - 32);
2126 } else if (shift > 0) {
2127 __ Dsra(TMP, TMP, shift);
2128 }
2129
2130 if (instruction->IsDiv()) {
2131 __ Dsra32(out, TMP, 31);
2132 __ Dsubu(out, TMP, out);
2133 } else {
2134 __ Dsra32(AT, TMP, 31);
2135 __ Dsubu(AT, TMP, AT);
2136 __ LoadConst64(TMP, imm);
2137 __ Dmul(TMP, AT, TMP);
2138 __ Dsubu(out, dividend, TMP);
2139 }
2140 }
2141}
2142
2143void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2144 DCHECK(instruction->IsDiv() || instruction->IsRem());
2145 Primitive::Type type = instruction->GetResultType();
2146 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2147
2148 LocationSummary* locations = instruction->GetLocations();
2149 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2150 Location second = locations->InAt(1);
2151
2152 if (second.IsConstant()) {
2153 int64_t imm = Int64FromConstant(second.GetConstant());
2154 if (imm == 0) {
2155 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2156 } else if (imm == 1 || imm == -1) {
2157 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002158 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002159 DivRemByPowerOfTwo(instruction);
2160 } else {
2161 DCHECK(imm <= -2 || imm >= 2);
2162 GenerateDivRemWithAnyConstant(instruction);
2163 }
2164 } else {
2165 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2166 GpuRegister divisor = second.AsRegister<GpuRegister>();
2167 if (instruction->IsDiv()) {
2168 if (type == Primitive::kPrimInt)
2169 __ DivR6(out, dividend, divisor);
2170 else
2171 __ Ddiv(out, dividend, divisor);
2172 } else {
2173 if (type == Primitive::kPrimInt)
2174 __ ModR6(out, dividend, divisor);
2175 else
2176 __ Dmod(out, dividend, divisor);
2177 }
2178 }
2179}
2180
Alexey Frunze4dda3372015-06-01 18:31:49 -07002181void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2182 LocationSummary* locations =
2183 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2184 switch (div->GetResultType()) {
2185 case Primitive::kPrimInt:
2186 case Primitive::kPrimLong:
2187 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002188 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2190 break;
2191
2192 case Primitive::kPrimFloat:
2193 case Primitive::kPrimDouble:
2194 locations->SetInAt(0, Location::RequiresFpuRegister());
2195 locations->SetInAt(1, Location::RequiresFpuRegister());
2196 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2197 break;
2198
2199 default:
2200 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2201 }
2202}
2203
2204void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2205 Primitive::Type type = instruction->GetType();
2206 LocationSummary* locations = instruction->GetLocations();
2207
2208 switch (type) {
2209 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002210 case Primitive::kPrimLong:
2211 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002212 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002213 case Primitive::kPrimFloat:
2214 case Primitive::kPrimDouble: {
2215 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2216 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2217 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2218 if (type == Primitive::kPrimFloat)
2219 __ DivS(dst, lhs, rhs);
2220 else
2221 __ DivD(dst, lhs, rhs);
2222 break;
2223 }
2224 default:
2225 LOG(FATAL) << "Unexpected div type " << type;
2226 }
2227}
2228
2229void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002230 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2231 ? LocationSummary::kCallOnSlowPath
2232 : LocationSummary::kNoCall;
2233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002234 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2235 if (instruction->HasUses()) {
2236 locations->SetOut(Location::SameAsFirstInput());
2237 }
2238}
2239
2240void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2241 SlowPathCodeMIPS64* slow_path =
2242 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2243 codegen_->AddSlowPath(slow_path);
2244 Location value = instruction->GetLocations()->InAt(0);
2245
2246 Primitive::Type type = instruction->GetType();
2247
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002248 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002249 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002250 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002251 }
2252
2253 if (value.IsConstant()) {
2254 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2255 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002256 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002257 } else {
2258 // A division by a non-null constant is valid. We don't need to perform
2259 // any check, so simply fall through.
2260 }
2261 } else {
2262 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2263 }
2264}
2265
2266void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2267 LocationSummary* locations =
2268 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2269 locations->SetOut(Location::ConstantLocation(constant));
2270}
2271
2272void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2273 // Will be generated at use site.
2274}
2275
2276void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2277 exit->SetLocations(nullptr);
2278}
2279
2280void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2281}
2282
2283void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2284 LocationSummary* locations =
2285 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2286 locations->SetOut(Location::ConstantLocation(constant));
2287}
2288
2289void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2290 // Will be generated at use site.
2291}
2292
David Brazdilfc6a86a2015-06-26 10:33:45 +00002293void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002294 DCHECK(!successor->IsExitBlock());
2295 HBasicBlock* block = got->GetBlock();
2296 HInstruction* previous = got->GetPrevious();
2297 HLoopInformation* info = block->GetLoopInformation();
2298
2299 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2300 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2301 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2302 return;
2303 }
2304 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2305 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2306 }
2307 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002308 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002309 }
2310}
2311
David Brazdilfc6a86a2015-06-26 10:33:45 +00002312void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2313 got->SetLocations(nullptr);
2314}
2315
2316void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2317 HandleGoto(got, got->GetSuccessor());
2318}
2319
2320void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2321 try_boundary->SetLocations(nullptr);
2322}
2323
2324void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2325 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2326 if (!successor->IsExitBlock()) {
2327 HandleGoto(try_boundary, successor);
2328 }
2329}
2330
Alexey Frunze299a9392015-12-08 16:08:02 -08002331void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2332 bool is64bit,
2333 LocationSummary* locations) {
2334 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2335 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2336 Location rhs_location = locations->InAt(1);
2337 GpuRegister rhs_reg = ZERO;
2338 int64_t rhs_imm = 0;
2339 bool use_imm = rhs_location.IsConstant();
2340 if (use_imm) {
2341 if (is64bit) {
2342 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2343 } else {
2344 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2345 }
2346 } else {
2347 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2348 }
2349 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2350
2351 switch (cond) {
2352 case kCondEQ:
2353 case kCondNE:
2354 if (use_imm && IsUint<16>(rhs_imm)) {
2355 __ Xori(dst, lhs, rhs_imm);
2356 } else {
2357 if (use_imm) {
2358 rhs_reg = TMP;
2359 __ LoadConst64(rhs_reg, rhs_imm);
2360 }
2361 __ Xor(dst, lhs, rhs_reg);
2362 }
2363 if (cond == kCondEQ) {
2364 __ Sltiu(dst, dst, 1);
2365 } else {
2366 __ Sltu(dst, ZERO, dst);
2367 }
2368 break;
2369
2370 case kCondLT:
2371 case kCondGE:
2372 if (use_imm && IsInt<16>(rhs_imm)) {
2373 __ Slti(dst, lhs, rhs_imm);
2374 } else {
2375 if (use_imm) {
2376 rhs_reg = TMP;
2377 __ LoadConst64(rhs_reg, rhs_imm);
2378 }
2379 __ Slt(dst, lhs, rhs_reg);
2380 }
2381 if (cond == kCondGE) {
2382 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2383 // only the slt instruction but no sge.
2384 __ Xori(dst, dst, 1);
2385 }
2386 break;
2387
2388 case kCondLE:
2389 case kCondGT:
2390 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2391 // Simulate lhs <= rhs via lhs < rhs + 1.
2392 __ Slti(dst, lhs, rhs_imm_plus_one);
2393 if (cond == kCondGT) {
2394 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2395 // only the slti instruction but no sgti.
2396 __ Xori(dst, dst, 1);
2397 }
2398 } else {
2399 if (use_imm) {
2400 rhs_reg = TMP;
2401 __ LoadConst64(rhs_reg, rhs_imm);
2402 }
2403 __ Slt(dst, rhs_reg, lhs);
2404 if (cond == kCondLE) {
2405 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2406 // only the slt instruction but no sle.
2407 __ Xori(dst, dst, 1);
2408 }
2409 }
2410 break;
2411
2412 case kCondB:
2413 case kCondAE:
2414 if (use_imm && IsInt<16>(rhs_imm)) {
2415 // Sltiu sign-extends its 16-bit immediate operand before
2416 // the comparison and thus lets us compare directly with
2417 // unsigned values in the ranges [0, 0x7fff] and
2418 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2419 __ Sltiu(dst, lhs, rhs_imm);
2420 } else {
2421 if (use_imm) {
2422 rhs_reg = TMP;
2423 __ LoadConst64(rhs_reg, rhs_imm);
2424 }
2425 __ Sltu(dst, lhs, rhs_reg);
2426 }
2427 if (cond == kCondAE) {
2428 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2429 // only the sltu instruction but no sgeu.
2430 __ Xori(dst, dst, 1);
2431 }
2432 break;
2433
2434 case kCondBE:
2435 case kCondA:
2436 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2437 // Simulate lhs <= rhs via lhs < rhs + 1.
2438 // Note that this only works if rhs + 1 does not overflow
2439 // to 0, hence the check above.
2440 // Sltiu sign-extends its 16-bit immediate operand before
2441 // the comparison and thus lets us compare directly with
2442 // unsigned values in the ranges [0, 0x7fff] and
2443 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2444 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2445 if (cond == kCondA) {
2446 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2447 // only the sltiu instruction but no sgtiu.
2448 __ Xori(dst, dst, 1);
2449 }
2450 } else {
2451 if (use_imm) {
2452 rhs_reg = TMP;
2453 __ LoadConst64(rhs_reg, rhs_imm);
2454 }
2455 __ Sltu(dst, rhs_reg, lhs);
2456 if (cond == kCondBE) {
2457 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2458 // only the sltu instruction but no sleu.
2459 __ Xori(dst, dst, 1);
2460 }
2461 }
2462 break;
2463 }
2464}
2465
2466void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2467 bool is64bit,
2468 LocationSummary* locations,
2469 Mips64Label* label) {
2470 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2471 Location rhs_location = locations->InAt(1);
2472 GpuRegister rhs_reg = ZERO;
2473 int64_t rhs_imm = 0;
2474 bool use_imm = rhs_location.IsConstant();
2475 if (use_imm) {
2476 if (is64bit) {
2477 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2478 } else {
2479 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2480 }
2481 } else {
2482 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2483 }
2484
2485 if (use_imm && rhs_imm == 0) {
2486 switch (cond) {
2487 case kCondEQ:
2488 case kCondBE: // <= 0 if zero
2489 __ Beqzc(lhs, label);
2490 break;
2491 case kCondNE:
2492 case kCondA: // > 0 if non-zero
2493 __ Bnezc(lhs, label);
2494 break;
2495 case kCondLT:
2496 __ Bltzc(lhs, label);
2497 break;
2498 case kCondGE:
2499 __ Bgezc(lhs, label);
2500 break;
2501 case kCondLE:
2502 __ Blezc(lhs, label);
2503 break;
2504 case kCondGT:
2505 __ Bgtzc(lhs, label);
2506 break;
2507 case kCondB: // always false
2508 break;
2509 case kCondAE: // always true
2510 __ Bc(label);
2511 break;
2512 }
2513 } else {
2514 if (use_imm) {
2515 rhs_reg = TMP;
2516 __ LoadConst64(rhs_reg, rhs_imm);
2517 }
2518 switch (cond) {
2519 case kCondEQ:
2520 __ Beqc(lhs, rhs_reg, label);
2521 break;
2522 case kCondNE:
2523 __ Bnec(lhs, rhs_reg, label);
2524 break;
2525 case kCondLT:
2526 __ Bltc(lhs, rhs_reg, label);
2527 break;
2528 case kCondGE:
2529 __ Bgec(lhs, rhs_reg, label);
2530 break;
2531 case kCondLE:
2532 __ Bgec(rhs_reg, lhs, label);
2533 break;
2534 case kCondGT:
2535 __ Bltc(rhs_reg, lhs, label);
2536 break;
2537 case kCondB:
2538 __ Bltuc(lhs, rhs_reg, label);
2539 break;
2540 case kCondAE:
2541 __ Bgeuc(lhs, rhs_reg, label);
2542 break;
2543 case kCondBE:
2544 __ Bgeuc(rhs_reg, lhs, label);
2545 break;
2546 case kCondA:
2547 __ Bltuc(rhs_reg, lhs, label);
2548 break;
2549 }
2550 }
2551}
2552
2553void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2554 bool gt_bias,
2555 Primitive::Type type,
2556 LocationSummary* locations,
2557 Mips64Label* label) {
2558 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2559 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2560 if (type == Primitive::kPrimFloat) {
2561 switch (cond) {
2562 case kCondEQ:
2563 __ CmpEqS(FTMP, lhs, rhs);
2564 __ Bc1nez(FTMP, label);
2565 break;
2566 case kCondNE:
2567 __ CmpEqS(FTMP, lhs, rhs);
2568 __ Bc1eqz(FTMP, label);
2569 break;
2570 case kCondLT:
2571 if (gt_bias) {
2572 __ CmpLtS(FTMP, lhs, rhs);
2573 } else {
2574 __ CmpUltS(FTMP, lhs, rhs);
2575 }
2576 __ Bc1nez(FTMP, label);
2577 break;
2578 case kCondLE:
2579 if (gt_bias) {
2580 __ CmpLeS(FTMP, lhs, rhs);
2581 } else {
2582 __ CmpUleS(FTMP, lhs, rhs);
2583 }
2584 __ Bc1nez(FTMP, label);
2585 break;
2586 case kCondGT:
2587 if (gt_bias) {
2588 __ CmpUltS(FTMP, rhs, lhs);
2589 } else {
2590 __ CmpLtS(FTMP, rhs, lhs);
2591 }
2592 __ Bc1nez(FTMP, label);
2593 break;
2594 case kCondGE:
2595 if (gt_bias) {
2596 __ CmpUleS(FTMP, rhs, lhs);
2597 } else {
2598 __ CmpLeS(FTMP, rhs, lhs);
2599 }
2600 __ Bc1nez(FTMP, label);
2601 break;
2602 default:
2603 LOG(FATAL) << "Unexpected non-floating-point condition";
2604 }
2605 } else {
2606 DCHECK_EQ(type, Primitive::kPrimDouble);
2607 switch (cond) {
2608 case kCondEQ:
2609 __ CmpEqD(FTMP, lhs, rhs);
2610 __ Bc1nez(FTMP, label);
2611 break;
2612 case kCondNE:
2613 __ CmpEqD(FTMP, lhs, rhs);
2614 __ Bc1eqz(FTMP, label);
2615 break;
2616 case kCondLT:
2617 if (gt_bias) {
2618 __ CmpLtD(FTMP, lhs, rhs);
2619 } else {
2620 __ CmpUltD(FTMP, lhs, rhs);
2621 }
2622 __ Bc1nez(FTMP, label);
2623 break;
2624 case kCondLE:
2625 if (gt_bias) {
2626 __ CmpLeD(FTMP, lhs, rhs);
2627 } else {
2628 __ CmpUleD(FTMP, lhs, rhs);
2629 }
2630 __ Bc1nez(FTMP, label);
2631 break;
2632 case kCondGT:
2633 if (gt_bias) {
2634 __ CmpUltD(FTMP, rhs, lhs);
2635 } else {
2636 __ CmpLtD(FTMP, rhs, lhs);
2637 }
2638 __ Bc1nez(FTMP, label);
2639 break;
2640 case kCondGE:
2641 if (gt_bias) {
2642 __ CmpUleD(FTMP, rhs, lhs);
2643 } else {
2644 __ CmpLeD(FTMP, rhs, lhs);
2645 }
2646 __ Bc1nez(FTMP, label);
2647 break;
2648 default:
2649 LOG(FATAL) << "Unexpected non-floating-point condition";
2650 }
2651 }
2652}
2653
Alexey Frunze4dda3372015-06-01 18:31:49 -07002654void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002655 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002656 Mips64Label* true_target,
2657 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002658 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002659
David Brazdil0debae72015-11-12 18:37:00 +00002660 if (true_target == nullptr && false_target == nullptr) {
2661 // Nothing to do. The code always falls through.
2662 return;
2663 } else if (cond->IsIntConstant()) {
2664 // Constant condition, statically compared against 1.
2665 if (cond->AsIntConstant()->IsOne()) {
2666 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002667 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002668 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002669 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002670 DCHECK(cond->AsIntConstant()->IsZero());
2671 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002672 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002673 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002674 }
David Brazdil0debae72015-11-12 18:37:00 +00002675 return;
2676 }
2677
2678 // The following code generates these patterns:
2679 // (1) true_target == nullptr && false_target != nullptr
2680 // - opposite condition true => branch to false_target
2681 // (2) true_target != nullptr && false_target == nullptr
2682 // - condition true => branch to true_target
2683 // (3) true_target != nullptr && false_target != nullptr
2684 // - condition true => branch to true_target
2685 // - branch to false_target
2686 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002687 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002688 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002689 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002690 if (true_target == nullptr) {
2691 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2692 } else {
2693 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2694 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002695 } else {
2696 // The condition instruction has not been materialized, use its inputs as
2697 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002698 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002699 Primitive::Type type = condition->InputAt(0)->GetType();
2700 LocationSummary* locations = cond->GetLocations();
2701 IfCondition if_cond = condition->GetCondition();
2702 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002703
David Brazdil0debae72015-11-12 18:37:00 +00002704 if (true_target == nullptr) {
2705 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002706 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002707 }
2708
Alexey Frunze299a9392015-12-08 16:08:02 -08002709 switch (type) {
2710 default:
2711 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2712 break;
2713 case Primitive::kPrimLong:
2714 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2715 break;
2716 case Primitive::kPrimFloat:
2717 case Primitive::kPrimDouble:
2718 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2719 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002720 }
2721 }
David Brazdil0debae72015-11-12 18:37:00 +00002722
2723 // If neither branch falls through (case 3), the conditional branch to `true_target`
2724 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2725 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002726 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002727 }
2728}
2729
2730void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002732 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002733 locations->SetInAt(0, Location::RequiresRegister());
2734 }
2735}
2736
2737void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002738 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2739 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002740 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002741 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002742 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002743 nullptr : codegen_->GetLabelOf(false_successor);
2744 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002745}
2746
2747void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2748 LocationSummary* locations = new (GetGraph()->GetArena())
2749 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002750 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002751 locations->SetInAt(0, Location::RequiresRegister());
2752 }
2753}
2754
2755void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002756 SlowPathCodeMIPS64* slow_path =
2757 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002758 GenerateTestAndBranch(deoptimize,
2759 /* condition_input_index */ 0,
2760 slow_path->GetEntryLabel(),
2761 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002762}
2763
David Brazdil74eb1b22015-12-14 11:44:01 +00002764void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2765 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2766 if (Primitive::IsFloatingPointType(select->GetType())) {
2767 locations->SetInAt(0, Location::RequiresFpuRegister());
2768 locations->SetInAt(1, Location::RequiresFpuRegister());
2769 } else {
2770 locations->SetInAt(0, Location::RequiresRegister());
2771 locations->SetInAt(1, Location::RequiresRegister());
2772 }
2773 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2774 locations->SetInAt(2, Location::RequiresRegister());
2775 }
2776 locations->SetOut(Location::SameAsFirstInput());
2777}
2778
2779void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2780 LocationSummary* locations = select->GetLocations();
2781 Mips64Label false_target;
2782 GenerateTestAndBranch(select,
2783 /* condition_input_index */ 2,
2784 /* true_target */ nullptr,
2785 &false_target);
2786 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2787 __ Bind(&false_target);
2788}
2789
David Srbecky0cf44932015-12-09 14:09:59 +00002790void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2791 new (GetGraph()->GetArena()) LocationSummary(info);
2792}
2793
2794void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002795 if (codegen_->HasStackMapAtCurrentPc()) {
2796 // Ensure that we do not collide with the stack map of the previous instruction.
2797 __ Nop();
2798 }
David Srbecky0cf44932015-12-09 14:09:59 +00002799 codegen_->RecordPcInfo(info, info->GetDexPc());
2800}
2801
Alexey Frunze4dda3372015-06-01 18:31:49 -07002802void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2803 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2804 LocationSummary* locations =
2805 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2806 locations->SetInAt(0, Location::RequiresRegister());
2807 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2808 locations->SetOut(Location::RequiresFpuRegister());
2809 } else {
2810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2811 }
2812}
2813
2814void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2815 const FieldInfo& field_info) {
2816 Primitive::Type type = field_info.GetFieldType();
2817 LocationSummary* locations = instruction->GetLocations();
2818 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2819 LoadOperandType load_type = kLoadUnsignedByte;
2820 switch (type) {
2821 case Primitive::kPrimBoolean:
2822 load_type = kLoadUnsignedByte;
2823 break;
2824 case Primitive::kPrimByte:
2825 load_type = kLoadSignedByte;
2826 break;
2827 case Primitive::kPrimShort:
2828 load_type = kLoadSignedHalfword;
2829 break;
2830 case Primitive::kPrimChar:
2831 load_type = kLoadUnsignedHalfword;
2832 break;
2833 case Primitive::kPrimInt:
2834 case Primitive::kPrimFloat:
2835 load_type = kLoadWord;
2836 break;
2837 case Primitive::kPrimLong:
2838 case Primitive::kPrimDouble:
2839 load_type = kLoadDoubleword;
2840 break;
2841 case Primitive::kPrimNot:
2842 load_type = kLoadUnsignedWord;
2843 break;
2844 case Primitive::kPrimVoid:
2845 LOG(FATAL) << "Unreachable type " << type;
2846 UNREACHABLE();
2847 }
2848 if (!Primitive::IsFloatingPointType(type)) {
2849 DCHECK(locations->Out().IsRegister());
2850 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2851 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2852 } else {
2853 DCHECK(locations->Out().IsFpuRegister());
2854 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2855 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2856 }
2857
2858 codegen_->MaybeRecordImplicitNullCheck(instruction);
2859 // TODO: memory barrier?
2860}
2861
2862void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2863 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2864 LocationSummary* locations =
2865 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2866 locations->SetInAt(0, Location::RequiresRegister());
2867 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2868 locations->SetInAt(1, Location::RequiresFpuRegister());
2869 } else {
2870 locations->SetInAt(1, Location::RequiresRegister());
2871 }
2872}
2873
2874void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002875 const FieldInfo& field_info,
2876 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002877 Primitive::Type type = field_info.GetFieldType();
2878 LocationSummary* locations = instruction->GetLocations();
2879 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2880 StoreOperandType store_type = kStoreByte;
2881 switch (type) {
2882 case Primitive::kPrimBoolean:
2883 case Primitive::kPrimByte:
2884 store_type = kStoreByte;
2885 break;
2886 case Primitive::kPrimShort:
2887 case Primitive::kPrimChar:
2888 store_type = kStoreHalfword;
2889 break;
2890 case Primitive::kPrimInt:
2891 case Primitive::kPrimFloat:
2892 case Primitive::kPrimNot:
2893 store_type = kStoreWord;
2894 break;
2895 case Primitive::kPrimLong:
2896 case Primitive::kPrimDouble:
2897 store_type = kStoreDoubleword;
2898 break;
2899 case Primitive::kPrimVoid:
2900 LOG(FATAL) << "Unreachable type " << type;
2901 UNREACHABLE();
2902 }
2903 if (!Primitive::IsFloatingPointType(type)) {
2904 DCHECK(locations->InAt(1).IsRegister());
2905 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2906 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2907 } else {
2908 DCHECK(locations->InAt(1).IsFpuRegister());
2909 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2910 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2911 }
2912
2913 codegen_->MaybeRecordImplicitNullCheck(instruction);
2914 // TODO: memory barriers?
2915 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2916 DCHECK(locations->InAt(1).IsRegister());
2917 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002918 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002919 }
2920}
2921
2922void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2923 HandleFieldGet(instruction, instruction->GetFieldInfo());
2924}
2925
2926void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2927 HandleFieldGet(instruction, instruction->GetFieldInfo());
2928}
2929
2930void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2931 HandleFieldSet(instruction, instruction->GetFieldInfo());
2932}
2933
2934void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002935 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002936}
2937
2938void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2939 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002940 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002941 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2942 locations->SetInAt(0, Location::RequiresRegister());
2943 locations->SetInAt(1, Location::RequiresRegister());
2944 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002945 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002946 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2947}
2948
2949void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2950 LocationSummary* locations = instruction->GetLocations();
2951 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2952 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2953 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2954
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002955 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002956
2957 // Return 0 if `obj` is null.
2958 // TODO: Avoid this check if we know `obj` is not null.
2959 __ Move(out, ZERO);
2960 __ Beqzc(obj, &done);
2961
2962 // Compare the class of `obj` with `cls`.
2963 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002964 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002965 // Classes must be equal for the instanceof to succeed.
2966 __ Xor(out, out, cls);
2967 __ Sltiu(out, out, 1);
2968 } else {
2969 // If the classes are not equal, we go into a slow path.
2970 DCHECK(locations->OnlyCallsOnSlowPath());
2971 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002972 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002973 codegen_->AddSlowPath(slow_path);
2974 __ Bnec(out, cls, slow_path->GetEntryLabel());
2975 __ LoadConst32(out, 1);
2976 __ Bind(slow_path->GetExitLabel());
2977 }
2978
2979 __ Bind(&done);
2980}
2981
2982void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2983 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2984 locations->SetOut(Location::ConstantLocation(constant));
2985}
2986
2987void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2988 // Will be generated at use site.
2989}
2990
2991void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2992 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2993 locations->SetOut(Location::ConstantLocation(constant));
2994}
2995
2996void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2997 // Will be generated at use site.
2998}
2999
Calin Juravle175dc732015-08-25 15:42:32 +01003000void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3001 // The trampoline uses the same calling convention as dex calling conventions,
3002 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3003 // the method_idx.
3004 HandleInvoke(invoke);
3005}
3006
3007void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3008 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3009}
3010
Alexey Frunze4dda3372015-06-01 18:31:49 -07003011void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3012 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3013 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3014}
3015
3016void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3017 HandleInvoke(invoke);
3018 // The register T0 is required to be used for the hidden argument in
3019 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3020 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3021}
3022
3023void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3024 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3025 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
3026 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3027 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
3028 Location receiver = invoke->GetLocations()->InAt(0);
3029 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003030 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003031
3032 // Set the hidden argument.
3033 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3034 invoke->GetDexMethodIndex());
3035
3036 // temp = object->GetClass();
3037 if (receiver.IsStackSlot()) {
3038 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3039 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3040 } else {
3041 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3042 }
3043 codegen_->MaybeRecordImplicitNullCheck(invoke);
3044 // temp = temp->GetImtEntryAt(method_offset);
3045 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3046 // T9 = temp->GetEntryPoint();
3047 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3048 // T9();
3049 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003050 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003051 DCHECK(!codegen_->IsLeafMethod());
3052 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3053}
3054
3055void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003056 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3057 if (intrinsic.TryDispatch(invoke)) {
3058 return;
3059 }
3060
Alexey Frunze4dda3372015-06-01 18:31:49 -07003061 HandleInvoke(invoke);
3062}
3063
3064void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003065 // Explicit clinit checks triggered by static invokes must have been pruned by
3066 // art::PrepareForRegisterAllocation.
3067 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003068
Chris Larsen3039e382015-08-26 07:54:08 -07003069 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3070 if (intrinsic.TryDispatch(invoke)) {
3071 return;
3072 }
3073
Alexey Frunze4dda3372015-06-01 18:31:49 -07003074 HandleInvoke(invoke);
3075
3076 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
3077 // clobbering somewhere else, reduce further register pressure by avoiding
3078 // allocation of a register for the current method pointer like on x86 baseline.
3079 // TODO: remove this once all the issues with register saving/restoring are
3080 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003081 if (invoke->HasCurrentMethodInput()) {
3082 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00003083 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003084 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003085 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003086 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003087 }
3088}
3089
Chris Larsen3039e382015-08-26 07:54:08 -07003090static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003091 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003092 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3093 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003094 return true;
3095 }
3096 return false;
3097}
3098
Vladimir Markodc151b22015-10-15 18:02:30 +01003099HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3100 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3101 MethodReference target_method ATTRIBUTE_UNUSED) {
3102 switch (desired_dispatch_info.method_load_kind) {
3103 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3104 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3105 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3106 return HInvokeStaticOrDirect::DispatchInfo {
3107 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3108 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3109 0u,
3110 0u
3111 };
3112 default:
3113 break;
3114 }
3115 switch (desired_dispatch_info.code_ptr_location) {
3116 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3117 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3118 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3119 return HInvokeStaticOrDirect::DispatchInfo {
3120 desired_dispatch_info.method_load_kind,
3121 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3122 desired_dispatch_info.method_load_data,
3123 0u
3124 };
3125 default:
3126 return desired_dispatch_info;
3127 }
3128}
3129
Alexey Frunze4dda3372015-06-01 18:31:49 -07003130void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3131 // All registers are assumed to be correctly set up per the calling convention.
3132
Vladimir Marko58155012015-08-19 12:49:41 +00003133 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3134 switch (invoke->GetMethodLoadKind()) {
3135 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3136 // temp = thread->string_init_entrypoint
3137 __ LoadFromOffset(kLoadDoubleword,
3138 temp.AsRegister<GpuRegister>(),
3139 TR,
3140 invoke->GetStringInitOffset());
3141 break;
3142 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003143 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003144 break;
3145 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3146 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3147 break;
3148 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003149 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003150 // TODO: Implement these types.
3151 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3152 LOG(FATAL) << "Unsupported";
3153 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003154 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003155 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003156 GpuRegister reg = temp.AsRegister<GpuRegister>();
3157 GpuRegister method_reg;
3158 if (current_method.IsRegister()) {
3159 method_reg = current_method.AsRegister<GpuRegister>();
3160 } else {
3161 // TODO: use the appropriate DCHECK() here if possible.
3162 // DCHECK(invoke->GetLocations()->Intrinsified());
3163 DCHECK(!current_method.IsValid());
3164 method_reg = reg;
3165 __ Ld(reg, SP, kCurrentMethodStackOffset);
3166 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003167
Vladimir Marko58155012015-08-19 12:49:41 +00003168 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003169 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003170 reg,
3171 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003172 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003173 // temp = temp[index_in_cache]
3174 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3175 __ LoadFromOffset(kLoadDoubleword,
3176 reg,
3177 reg,
3178 CodeGenerator::GetCachePointerOffset(index_in_cache));
3179 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003180 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003181 }
3182
Vladimir Marko58155012015-08-19 12:49:41 +00003183 switch (invoke->GetCodePtrLocation()) {
3184 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003185 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003186 break;
3187 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3188 // LR = invoke->GetDirectCodePtr();
3189 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3190 // LR()
3191 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003192 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003193 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003194 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003195 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3196 // TODO: Implement these types.
3197 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3198 LOG(FATAL) << "Unsupported";
3199 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003200 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3201 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3202 __ LoadFromOffset(kLoadDoubleword,
3203 T9,
3204 callee_method.AsRegister<GpuRegister>(),
3205 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003206 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003207 // T9()
3208 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003209 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003210 break;
3211 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003212 DCHECK(!IsLeafMethod());
3213}
3214
3215void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003216 // Explicit clinit checks triggered by static invokes must have been pruned by
3217 // art::PrepareForRegisterAllocation.
3218 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003219
3220 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3221 return;
3222 }
3223
3224 LocationSummary* locations = invoke->GetLocations();
3225 codegen_->GenerateStaticOrDirectCall(invoke,
3226 locations->HasTemps()
3227 ? locations->GetTemp(0)
3228 : Location::NoLocation());
3229 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3230}
3231
Alexey Frunze53afca12015-11-05 16:34:23 -08003232void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003233 // Use the calling convention instead of the location of the receiver, as
3234 // intrinsics may have put the receiver in a different register. In the intrinsics
3235 // slow path, the arguments have been moved to the right place, so here we are
3236 // guaranteed that the receiver is the first register of the calling convention.
3237 InvokeDexCallingConvention calling_convention;
3238 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3239
Alexey Frunze53afca12015-11-05 16:34:23 -08003240 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003241 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3242 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3243 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003244 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003245
3246 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003247 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003248 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003249 // temp = temp->GetMethodAt(method_offset);
3250 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3251 // T9 = temp->GetEntryPoint();
3252 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3253 // T9();
3254 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003255 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003256}
3257
3258void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3259 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3260 return;
3261 }
3262
3263 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003264 DCHECK(!codegen_->IsLeafMethod());
3265 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3266}
3267
3268void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003269 InvokeRuntimeCallingConvention calling_convention;
3270 CodeGenerator::CreateLoadClassLocationSummary(
3271 cls,
3272 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003273 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003274}
3275
3276void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3277 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003278 if (cls->NeedsAccessCheck()) {
3279 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3280 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3281 cls,
3282 cls->GetDexPc(),
3283 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003284 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003285 return;
3286 }
3287
3288 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3289 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3290 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003291 DCHECK(!cls->CanCallRuntime());
3292 DCHECK(!cls->MustGenerateClinitCheck());
3293 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3294 ArtMethod::DeclaringClassOffset().Int32Value());
3295 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003296 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3297 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003298 __ LoadFromOffset(
3299 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003300 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003301 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3302 DCHECK(cls->CanCallRuntime());
3303 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3304 cls,
3305 cls,
3306 cls->GetDexPc(),
3307 cls->MustGenerateClinitCheck());
3308 codegen_->AddSlowPath(slow_path);
3309 if (!cls->IsInDexCache()) {
3310 __ Beqzc(out, slow_path->GetEntryLabel());
3311 }
3312 if (cls->MustGenerateClinitCheck()) {
3313 GenerateClassInitializationCheck(slow_path, out);
3314 } else {
3315 __ Bind(slow_path->GetExitLabel());
3316 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003317 }
3318 }
3319}
3320
David Brazdilcb1c0552015-08-04 16:22:25 +01003321static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003322 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003323}
3324
Alexey Frunze4dda3372015-06-01 18:31:49 -07003325void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3326 LocationSummary* locations =
3327 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3328 locations->SetOut(Location::RequiresRegister());
3329}
3330
3331void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3332 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003333 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3334}
3335
3336void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3337 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3338}
3339
3340void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3341 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003342}
3343
3344void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3345 load->SetLocations(nullptr);
3346}
3347
3348void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3349 // Nothing to do, this is driven by the code generator.
3350}
3351
3352void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00003353 LocationSummary::CallKind call_kind = load->IsInDexCache()
3354 ? LocationSummary::kNoCall
3355 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003357 locations->SetInAt(0, Location::RequiresRegister());
3358 locations->SetOut(Location::RequiresRegister());
3359}
3360
3361void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003362 LocationSummary* locations = load->GetLocations();
3363 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3364 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3365 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3366 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003367 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003368 __ LoadFromOffset(
3369 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003370 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003371
3372 if (!load->IsInDexCache()) {
3373 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3374 codegen_->AddSlowPath(slow_path);
3375 __ Beqzc(out, slow_path->GetEntryLabel());
3376 __ Bind(slow_path->GetExitLabel());
3377 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003378}
3379
3380void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3381 local->SetLocations(nullptr);
3382}
3383
3384void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3385 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3386}
3387
3388void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3390 locations->SetOut(Location::ConstantLocation(constant));
3391}
3392
3393void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3394 // Will be generated at use site.
3395}
3396
3397void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3398 LocationSummary* locations =
3399 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3400 InvokeRuntimeCallingConvention calling_convention;
3401 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3402}
3403
3404void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3405 codegen_->InvokeRuntime(instruction->IsEnter()
3406 ? QUICK_ENTRY_POINT(pLockObject)
3407 : QUICK_ENTRY_POINT(pUnlockObject),
3408 instruction,
3409 instruction->GetDexPc(),
3410 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003411 if (instruction->IsEnter()) {
3412 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3413 } else {
3414 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3415 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003416}
3417
3418void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3419 LocationSummary* locations =
3420 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3421 switch (mul->GetResultType()) {
3422 case Primitive::kPrimInt:
3423 case Primitive::kPrimLong:
3424 locations->SetInAt(0, Location::RequiresRegister());
3425 locations->SetInAt(1, Location::RequiresRegister());
3426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3427 break;
3428
3429 case Primitive::kPrimFloat:
3430 case Primitive::kPrimDouble:
3431 locations->SetInAt(0, Location::RequiresFpuRegister());
3432 locations->SetInAt(1, Location::RequiresFpuRegister());
3433 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3434 break;
3435
3436 default:
3437 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3438 }
3439}
3440
3441void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3442 Primitive::Type type = instruction->GetType();
3443 LocationSummary* locations = instruction->GetLocations();
3444
3445 switch (type) {
3446 case Primitive::kPrimInt:
3447 case Primitive::kPrimLong: {
3448 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3449 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3450 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3451 if (type == Primitive::kPrimInt)
3452 __ MulR6(dst, lhs, rhs);
3453 else
3454 __ Dmul(dst, lhs, rhs);
3455 break;
3456 }
3457 case Primitive::kPrimFloat:
3458 case Primitive::kPrimDouble: {
3459 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3460 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3461 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3462 if (type == Primitive::kPrimFloat)
3463 __ MulS(dst, lhs, rhs);
3464 else
3465 __ MulD(dst, lhs, rhs);
3466 break;
3467 }
3468 default:
3469 LOG(FATAL) << "Unexpected mul type " << type;
3470 }
3471}
3472
3473void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3474 LocationSummary* locations =
3475 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3476 switch (neg->GetResultType()) {
3477 case Primitive::kPrimInt:
3478 case Primitive::kPrimLong:
3479 locations->SetInAt(0, Location::RequiresRegister());
3480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3481 break;
3482
3483 case Primitive::kPrimFloat:
3484 case Primitive::kPrimDouble:
3485 locations->SetInAt(0, Location::RequiresFpuRegister());
3486 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3487 break;
3488
3489 default:
3490 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3491 }
3492}
3493
3494void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3495 Primitive::Type type = instruction->GetType();
3496 LocationSummary* locations = instruction->GetLocations();
3497
3498 switch (type) {
3499 case Primitive::kPrimInt:
3500 case Primitive::kPrimLong: {
3501 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3502 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3503 if (type == Primitive::kPrimInt)
3504 __ Subu(dst, ZERO, src);
3505 else
3506 __ Dsubu(dst, ZERO, src);
3507 break;
3508 }
3509 case Primitive::kPrimFloat:
3510 case Primitive::kPrimDouble: {
3511 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3512 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3513 if (type == Primitive::kPrimFloat)
3514 __ NegS(dst, src);
3515 else
3516 __ NegD(dst, src);
3517 break;
3518 }
3519 default:
3520 LOG(FATAL) << "Unexpected neg type " << type;
3521 }
3522}
3523
3524void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3525 LocationSummary* locations =
3526 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3527 InvokeRuntimeCallingConvention calling_convention;
3528 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3529 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3530 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3531 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3532}
3533
3534void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3535 LocationSummary* locations = instruction->GetLocations();
3536 // Move an uint16_t value to a register.
3537 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003538 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3539 instruction,
3540 instruction->GetDexPc(),
3541 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003542 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3543}
3544
3545void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3546 LocationSummary* locations =
3547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3548 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003549 if (instruction->IsStringAlloc()) {
3550 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3551 } else {
3552 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3553 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3554 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003555 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3556}
3557
3558void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003559 if (instruction->IsStringAlloc()) {
3560 // String is allocated through StringFactory. Call NewEmptyString entry point.
3561 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003562 MemberOffset code_offset =
3563 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003564 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3565 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3566 __ Jalr(T9);
3567 __ Nop();
3568 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3569 } else {
3570 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3571 instruction,
3572 instruction->GetDexPc(),
3573 nullptr);
3574 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3575 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003576}
3577
3578void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3580 locations->SetInAt(0, Location::RequiresRegister());
3581 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3582}
3583
3584void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3585 Primitive::Type type = instruction->GetType();
3586 LocationSummary* locations = instruction->GetLocations();
3587
3588 switch (type) {
3589 case Primitive::kPrimInt:
3590 case Primitive::kPrimLong: {
3591 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3592 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3593 __ Nor(dst, src, ZERO);
3594 break;
3595 }
3596
3597 default:
3598 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3599 }
3600}
3601
3602void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3604 locations->SetInAt(0, Location::RequiresRegister());
3605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3606}
3607
3608void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3609 LocationSummary* locations = instruction->GetLocations();
3610 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3611 locations->InAt(0).AsRegister<GpuRegister>(),
3612 1);
3613}
3614
3615void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003616 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3617 ? LocationSummary::kCallOnSlowPath
3618 : LocationSummary::kNoCall;
3619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003620 locations->SetInAt(0, Location::RequiresRegister());
3621 if (instruction->HasUses()) {
3622 locations->SetOut(Location::SameAsFirstInput());
3623 }
3624}
3625
3626void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3627 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3628 return;
3629 }
3630 Location obj = instruction->GetLocations()->InAt(0);
3631
3632 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3633 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3634}
3635
3636void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3637 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3638 codegen_->AddSlowPath(slow_path);
3639
3640 Location obj = instruction->GetLocations()->InAt(0);
3641
3642 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3643}
3644
3645void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003646 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003647 GenerateImplicitNullCheck(instruction);
3648 } else {
3649 GenerateExplicitNullCheck(instruction);
3650 }
3651}
3652
3653void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3654 HandleBinaryOp(instruction);
3655}
3656
3657void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3658 HandleBinaryOp(instruction);
3659}
3660
3661void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3662 LOG(FATAL) << "Unreachable";
3663}
3664
3665void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3666 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3667}
3668
3669void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3670 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3671 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3672 if (location.IsStackSlot()) {
3673 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3674 } else if (location.IsDoubleStackSlot()) {
3675 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3676 }
3677 locations->SetOut(location);
3678}
3679
3680void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3681 ATTRIBUTE_UNUSED) {
3682 // Nothing to do, the parameter is already at its location.
3683}
3684
3685void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3686 LocationSummary* locations =
3687 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3688 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3689}
3690
3691void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3692 ATTRIBUTE_UNUSED) {
3693 // Nothing to do, the method is already at its location.
3694}
3695
3696void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3698 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3699 locations->SetInAt(i, Location::Any());
3700 }
3701 locations->SetOut(Location::Any());
3702}
3703
3704void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3705 LOG(FATAL) << "Unreachable";
3706}
3707
3708void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3709 Primitive::Type type = rem->GetResultType();
3710 LocationSummary::CallKind call_kind =
3711 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3712 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3713
3714 switch (type) {
3715 case Primitive::kPrimInt:
3716 case Primitive::kPrimLong:
3717 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003718 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003719 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3720 break;
3721
3722 case Primitive::kPrimFloat:
3723 case Primitive::kPrimDouble: {
3724 InvokeRuntimeCallingConvention calling_convention;
3725 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3726 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3727 locations->SetOut(calling_convention.GetReturnLocation(type));
3728 break;
3729 }
3730
3731 default:
3732 LOG(FATAL) << "Unexpected rem type " << type;
3733 }
3734}
3735
3736void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3737 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003738
3739 switch (type) {
3740 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003741 case Primitive::kPrimLong:
3742 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003743 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003744
3745 case Primitive::kPrimFloat:
3746 case Primitive::kPrimDouble: {
3747 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3748 : QUICK_ENTRY_POINT(pFmod);
3749 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003750 if (type == Primitive::kPrimFloat) {
3751 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3752 } else {
3753 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3754 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003755 break;
3756 }
3757 default:
3758 LOG(FATAL) << "Unexpected rem type " << type;
3759 }
3760}
3761
3762void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3763 memory_barrier->SetLocations(nullptr);
3764}
3765
3766void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3767 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3768}
3769
3770void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3771 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3772 Primitive::Type return_type = ret->InputAt(0)->GetType();
3773 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3774}
3775
3776void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3777 codegen_->GenerateFrameExit();
3778}
3779
3780void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3781 ret->SetLocations(nullptr);
3782}
3783
3784void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3785 codegen_->GenerateFrameExit();
3786}
3787
Alexey Frunze92d90602015-12-18 18:16:36 -08003788void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3789 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003790}
3791
Alexey Frunze92d90602015-12-18 18:16:36 -08003792void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3793 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003794}
3795
Alexey Frunze4dda3372015-06-01 18:31:49 -07003796void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3797 HandleShift(shl);
3798}
3799
3800void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3801 HandleShift(shl);
3802}
3803
3804void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3805 HandleShift(shr);
3806}
3807
3808void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3809 HandleShift(shr);
3810}
3811
3812void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3814 Primitive::Type field_type = store->InputAt(1)->GetType();
3815 switch (field_type) {
3816 case Primitive::kPrimNot:
3817 case Primitive::kPrimBoolean:
3818 case Primitive::kPrimByte:
3819 case Primitive::kPrimChar:
3820 case Primitive::kPrimShort:
3821 case Primitive::kPrimInt:
3822 case Primitive::kPrimFloat:
3823 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3824 break;
3825
3826 case Primitive::kPrimLong:
3827 case Primitive::kPrimDouble:
3828 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3829 break;
3830
3831 default:
3832 LOG(FATAL) << "Unimplemented local type " << field_type;
3833 }
3834}
3835
3836void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3837}
3838
3839void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3840 HandleBinaryOp(instruction);
3841}
3842
3843void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3844 HandleBinaryOp(instruction);
3845}
3846
3847void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3848 HandleFieldGet(instruction, instruction->GetFieldInfo());
3849}
3850
3851void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3852 HandleFieldGet(instruction, instruction->GetFieldInfo());
3853}
3854
3855void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3856 HandleFieldSet(instruction, instruction->GetFieldInfo());
3857}
3858
3859void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003860 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003861}
3862
Calin Juravlee460d1d2015-09-29 04:52:17 +01003863void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3864 HUnresolvedInstanceFieldGet* instruction) {
3865 FieldAccessCallingConventionMIPS64 calling_convention;
3866 codegen_->CreateUnresolvedFieldLocationSummary(
3867 instruction, instruction->GetFieldType(), calling_convention);
3868}
3869
3870void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3871 HUnresolvedInstanceFieldGet* instruction) {
3872 FieldAccessCallingConventionMIPS64 calling_convention;
3873 codegen_->GenerateUnresolvedFieldAccess(instruction,
3874 instruction->GetFieldType(),
3875 instruction->GetFieldIndex(),
3876 instruction->GetDexPc(),
3877 calling_convention);
3878}
3879
3880void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3881 HUnresolvedInstanceFieldSet* instruction) {
3882 FieldAccessCallingConventionMIPS64 calling_convention;
3883 codegen_->CreateUnresolvedFieldLocationSummary(
3884 instruction, instruction->GetFieldType(), calling_convention);
3885}
3886
3887void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3888 HUnresolvedInstanceFieldSet* instruction) {
3889 FieldAccessCallingConventionMIPS64 calling_convention;
3890 codegen_->GenerateUnresolvedFieldAccess(instruction,
3891 instruction->GetFieldType(),
3892 instruction->GetFieldIndex(),
3893 instruction->GetDexPc(),
3894 calling_convention);
3895}
3896
3897void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3898 HUnresolvedStaticFieldGet* instruction) {
3899 FieldAccessCallingConventionMIPS64 calling_convention;
3900 codegen_->CreateUnresolvedFieldLocationSummary(
3901 instruction, instruction->GetFieldType(), calling_convention);
3902}
3903
3904void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3905 HUnresolvedStaticFieldGet* instruction) {
3906 FieldAccessCallingConventionMIPS64 calling_convention;
3907 codegen_->GenerateUnresolvedFieldAccess(instruction,
3908 instruction->GetFieldType(),
3909 instruction->GetFieldIndex(),
3910 instruction->GetDexPc(),
3911 calling_convention);
3912}
3913
3914void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3915 HUnresolvedStaticFieldSet* instruction) {
3916 FieldAccessCallingConventionMIPS64 calling_convention;
3917 codegen_->CreateUnresolvedFieldLocationSummary(
3918 instruction, instruction->GetFieldType(), calling_convention);
3919}
3920
3921void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3922 HUnresolvedStaticFieldSet* instruction) {
3923 FieldAccessCallingConventionMIPS64 calling_convention;
3924 codegen_->GenerateUnresolvedFieldAccess(instruction,
3925 instruction->GetFieldType(),
3926 instruction->GetFieldIndex(),
3927 instruction->GetDexPc(),
3928 calling_convention);
3929}
3930
Alexey Frunze4dda3372015-06-01 18:31:49 -07003931void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3932 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3933}
3934
3935void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3936 HBasicBlock* block = instruction->GetBlock();
3937 if (block->GetLoopInformation() != nullptr) {
3938 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3939 // The back edge will generate the suspend check.
3940 return;
3941 }
3942 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3943 // The goto will generate the suspend check.
3944 return;
3945 }
3946 GenerateSuspendCheck(instruction, nullptr);
3947}
3948
3949void LocationsBuilderMIPS64::VisitTemporary(HTemporary* temp) {
3950 temp->SetLocations(nullptr);
3951}
3952
3953void InstructionCodeGeneratorMIPS64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3954 // Nothing to do, this is driven by the code generator.
3955}
3956
3957void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3958 LocationSummary* locations =
3959 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3960 InvokeRuntimeCallingConvention calling_convention;
3961 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3962}
3963
3964void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3965 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3966 instruction,
3967 instruction->GetDexPc(),
3968 nullptr);
3969 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3970}
3971
3972void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3973 Primitive::Type input_type = conversion->GetInputType();
3974 Primitive::Type result_type = conversion->GetResultType();
3975 DCHECK_NE(input_type, result_type);
3976
3977 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3978 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3979 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3980 }
3981
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003982 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3983
3984 if (Primitive::IsFloatingPointType(input_type)) {
3985 locations->SetInAt(0, Location::RequiresFpuRegister());
3986 } else {
3987 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003988 }
3989
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003990 if (Primitive::IsFloatingPointType(result_type)) {
3991 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003992 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003993 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003994 }
3995}
3996
3997void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3998 LocationSummary* locations = conversion->GetLocations();
3999 Primitive::Type result_type = conversion->GetResultType();
4000 Primitive::Type input_type = conversion->GetInputType();
4001
4002 DCHECK_NE(input_type, result_type);
4003
4004 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4005 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4006 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4007
4008 switch (result_type) {
4009 case Primitive::kPrimChar:
4010 __ Andi(dst, src, 0xFFFF);
4011 break;
4012 case Primitive::kPrimByte:
4013 // long is never converted into types narrower than int directly,
4014 // so SEB and SEH can be used without ever causing unpredictable results
4015 // on 64-bit inputs
4016 DCHECK(input_type != Primitive::kPrimLong);
4017 __ Seb(dst, src);
4018 break;
4019 case Primitive::kPrimShort:
4020 // long is never converted into types narrower than int directly,
4021 // so SEB and SEH can be used without ever causing unpredictable results
4022 // on 64-bit inputs
4023 DCHECK(input_type != Primitive::kPrimLong);
4024 __ Seh(dst, src);
4025 break;
4026 case Primitive::kPrimInt:
4027 case Primitive::kPrimLong:
4028 // Sign-extend 32-bit int into bits 32 through 63 for
4029 // int-to-long and long-to-int conversions
4030 __ Sll(dst, src, 0);
4031 break;
4032
4033 default:
4034 LOG(FATAL) << "Unexpected type conversion from " << input_type
4035 << " to " << result_type;
4036 }
4037 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004038 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4039 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4040 if (input_type == Primitive::kPrimLong) {
4041 __ Dmtc1(src, FTMP);
4042 if (result_type == Primitive::kPrimFloat) {
4043 __ Cvtsl(dst, FTMP);
4044 } else {
4045 __ Cvtdl(dst, FTMP);
4046 }
4047 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004048 __ Mtc1(src, FTMP);
4049 if (result_type == Primitive::kPrimFloat) {
4050 __ Cvtsw(dst, FTMP);
4051 } else {
4052 __ Cvtdw(dst, FTMP);
4053 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004054 }
4055 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4056 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004057 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4058 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4059 Mips64Label truncate;
4060 Mips64Label done;
4061
4062 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4063 // value when the input is either a NaN or is outside of the range of the output type
4064 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4065 // the same result.
4066 //
4067 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4068 // value of the output type if the input is outside of the range after the truncation or
4069 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4070 // results. This matches the desired float/double-to-int/long conversion exactly.
4071 //
4072 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4073 //
4074 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4075 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4076 // even though it must be NAN2008=1 on R6.
4077 //
4078 // The code takes care of the different behaviors by first comparing the input to the
4079 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4080 // If the input is greater than or equal to the minimum, it procedes to the truncate
4081 // instruction, which will handle such an input the same way irrespective of NAN2008.
4082 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4083 // in order to return either zero or the minimum value.
4084 //
4085 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4086 // truncate instruction for MIPS64R6.
4087 if (input_type == Primitive::kPrimFloat) {
4088 uint32_t min_val = (result_type == Primitive::kPrimLong)
4089 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4090 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4091 __ LoadConst32(TMP, min_val);
4092 __ Mtc1(TMP, FTMP);
4093 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004094 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004095 uint64_t min_val = (result_type == Primitive::kPrimLong)
4096 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4097 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4098 __ LoadConst64(TMP, min_val);
4099 __ Dmtc1(TMP, FTMP);
4100 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004101 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004102
4103 __ Bc1nez(FTMP, &truncate);
4104
4105 if (input_type == Primitive::kPrimFloat) {
4106 __ CmpEqS(FTMP, src, src);
4107 } else {
4108 __ CmpEqD(FTMP, src, src);
4109 }
4110 if (result_type == Primitive::kPrimLong) {
4111 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4112 } else {
4113 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4114 }
4115 __ Mfc1(TMP, FTMP);
4116 __ And(dst, dst, TMP);
4117
4118 __ Bc(&done);
4119
4120 __ Bind(&truncate);
4121
4122 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004123 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004124 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004125 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004126 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004127 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004128 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004129 } else {
4130 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004131 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004132 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004133 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004134 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004135 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004136 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004137
4138 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004139 } else if (Primitive::IsFloatingPointType(result_type) &&
4140 Primitive::IsFloatingPointType(input_type)) {
4141 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4142 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4143 if (result_type == Primitive::kPrimFloat) {
4144 __ Cvtsd(dst, src);
4145 } else {
4146 __ Cvtds(dst, src);
4147 }
4148 } else {
4149 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4150 << " to " << result_type;
4151 }
4152}
4153
4154void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4155 HandleShift(ushr);
4156}
4157
4158void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4159 HandleShift(ushr);
4160}
4161
4162void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4163 HandleBinaryOp(instruction);
4164}
4165
4166void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4167 HandleBinaryOp(instruction);
4168}
4169
4170void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4171 // Nothing to do, this should be removed during prepare for register allocator.
4172 LOG(FATAL) << "Unreachable";
4173}
4174
4175void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4176 // Nothing to do, this should be removed during prepare for register allocator.
4177 LOG(FATAL) << "Unreachable";
4178}
4179
4180void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004181 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004182}
4183
4184void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004185 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004186}
4187
4188void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004189 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004190}
4191
4192void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004193 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004194}
4195
4196void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004197 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004198}
4199
4200void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004201 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004202}
4203
4204void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004205 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004206}
4207
4208void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004209 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004210}
4211
4212void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004213 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004214}
4215
4216void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004217 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004218}
4219
4220void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004221 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004222}
4223
4224void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004225 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004226}
4227
Aart Bike9f37602015-10-09 11:15:55 -07004228void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004229 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004230}
4231
4232void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004233 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004234}
4235
4236void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004237 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004238}
4239
4240void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004241 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004242}
4243
4244void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004245 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004246}
4247
4248void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004249 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004250}
4251
4252void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004253 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004254}
4255
4256void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004257 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004258}
4259
Mark Mendellfe57faa2015-09-18 09:26:15 -04004260// Simple implementation of packed switch - generate cascaded compare/jumps.
4261void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4262 LocationSummary* locations =
4263 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4264 locations->SetInAt(0, Location::RequiresRegister());
4265}
4266
4267void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4268 int32_t lower_bound = switch_instr->GetStartValue();
4269 int32_t num_entries = switch_instr->GetNumEntries();
4270 LocationSummary* locations = switch_instr->GetLocations();
4271 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4272 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4273
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004274 // Create a set of compare/jumps.
4275 GpuRegister temp_reg = TMP;
4276 if (IsInt<16>(-lower_bound)) {
4277 __ Addiu(temp_reg, value_reg, -lower_bound);
4278 } else {
4279 __ LoadConst32(AT, -lower_bound);
4280 __ Addu(temp_reg, value_reg, AT);
4281 }
4282 // Jump to default if index is negative
4283 // Note: We don't check the case that index is positive while value < lower_bound, because in
4284 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4285 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4286
Mark Mendellfe57faa2015-09-18 09:26:15 -04004287 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004288 // Jump to successors[0] if value == lower_bound.
4289 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4290 int32_t last_index = 0;
4291 for (; num_entries - last_index > 2; last_index += 2) {
4292 __ Addiu(temp_reg, temp_reg, -2);
4293 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4294 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4295 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4296 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4297 }
4298 if (num_entries - last_index == 2) {
4299 // The last missing case_value.
4300 __ Addiu(temp_reg, temp_reg, -1);
4301 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004302 }
4303
4304 // And the default for any other value.
4305 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004306 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004307 }
4308}
4309
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004310void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4311 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4312}
4313
4314void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4315 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4316}
4317
Alexey Frunze4dda3372015-06-01 18:31:49 -07004318} // namespace mips64
4319} // namespace art
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004320