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