blob: cefcb95c1eeaa7b86bb276c06b18600a2095ca08 [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
526// TODO: mapping of floating-point registers to DWARF
527
528void CodeGeneratorMIPS64::GenerateFrameEntry() {
529 __ Bind(&frame_entry_label_);
530
531 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
532
533 if (do_overflow_check) {
534 __ LoadFromOffset(kLoadWord,
535 ZERO,
536 SP,
537 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
538 RecordPcInfo(nullptr, 0);
539 }
540
541 // TODO: anything related to T9/GP/GOT/PIC/.so's?
542
543 if (HasEmptyFrame()) {
544 return;
545 }
546
547 // Make sure the frame size isn't unreasonably large. Per the various APIs
548 // it looks like it should always be less than 2GB in size, which allows
549 // us using 32-bit signed offsets from the stack pointer.
550 if (GetFrameSize() > 0x7FFFFFFF)
551 LOG(FATAL) << "Stack frame larger than 2GB";
552
553 // Spill callee-saved registers.
554 // Note that their cumulative size is small and they can be indexed using
555 // 16-bit offsets.
556
557 // TODO: increment/decrement SP in one step instead of two or remove this comment.
558
559 uint32_t ofs = FrameEntrySpillSize();
560 __ IncreaseFrameSize(ofs);
561
562 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
563 GpuRegister reg = kCoreCalleeSaves[i];
564 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200565 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700566 __ Sd(reg, SP, ofs);
567 __ cfi().RelOffset(DWARFReg(reg), ofs);
568 }
569 }
570
571 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
572 FpuRegister reg = kFpuCalleeSaves[i];
573 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200574 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700575 __ Sdc1(reg, SP, ofs);
576 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
577 }
578 }
579
580 // Allocate the rest of the frame and store the current method pointer
581 // at its end.
582
583 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
584
585 static_assert(IsInt<16>(kCurrentMethodStackOffset),
586 "kCurrentMethodStackOffset must fit into int16_t");
587 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
588}
589
590void CodeGeneratorMIPS64::GenerateFrameExit() {
591 __ cfi().RememberState();
592
593 // TODO: anything related to T9/GP/GOT/PIC/.so's?
594
595 if (!HasEmptyFrame()) {
596 // Deallocate the rest of the frame.
597
598 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
599
600 // Restore callee-saved registers.
601 // Note that their cumulative size is small and they can be indexed using
602 // 16-bit offsets.
603
604 // TODO: increment/decrement SP in one step instead of two or remove this comment.
605
606 uint32_t ofs = 0;
607
608 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
609 FpuRegister reg = kFpuCalleeSaves[i];
610 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
611 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200612 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700613 // TODO: __ cfi().Restore(DWARFReg(reg));
614 }
615 }
616
617 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
618 GpuRegister reg = kCoreCalleeSaves[i];
619 if (allocated_registers_.ContainsCoreRegister(reg)) {
620 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200621 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700622 __ cfi().Restore(DWARFReg(reg));
623 }
624 }
625
626 DCHECK_EQ(ofs, FrameEntrySpillSize());
627 __ DecreaseFrameSize(ofs);
628 }
629
630 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700631 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700632
633 __ cfi().RestoreState();
634 __ cfi().DefCFAOffset(GetFrameSize());
635}
636
637void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
638 __ Bind(GetLabelOf(block));
639}
640
641void CodeGeneratorMIPS64::MoveLocation(Location destination,
642 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100643 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700644 if (source.Equals(destination)) {
645 return;
646 }
647
648 // A valid move can always be inferred from the destination and source
649 // locations. When moving from and to a register, the argument type can be
650 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100651 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700652 DCHECK_EQ(unspecified_type, false);
653
654 if (destination.IsRegister() || destination.IsFpuRegister()) {
655 if (unspecified_type) {
656 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
657 if (source.IsStackSlot() ||
658 (src_cst != nullptr && (src_cst->IsIntConstant()
659 || src_cst->IsFloatConstant()
660 || src_cst->IsNullConstant()))) {
661 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100662 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700663 } else {
664 // If the source is a double stack slot or a 64bit constant, a 64bit
665 // type is appropriate. Else the source is a register, and since the
666 // type has not been specified, we chose a 64bit type to force a 64bit
667 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100668 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700669 }
670 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100671 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
672 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700673 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
674 // Move to GPR/FPR from stack
675 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100676 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700677 __ LoadFpuFromOffset(load_type,
678 destination.AsFpuRegister<FpuRegister>(),
679 SP,
680 source.GetStackIndex());
681 } else {
682 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
683 __ LoadFromOffset(load_type,
684 destination.AsRegister<GpuRegister>(),
685 SP,
686 source.GetStackIndex());
687 }
688 } else if (source.IsConstant()) {
689 // Move to GPR/FPR from constant
690 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100691 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700692 gpr = destination.AsRegister<GpuRegister>();
693 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100694 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700695 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
696 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
697 gpr = ZERO;
698 } else {
699 __ LoadConst32(gpr, value);
700 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700701 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700702 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
703 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
704 gpr = ZERO;
705 } else {
706 __ LoadConst64(gpr, value);
707 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700708 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100709 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700710 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700712 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
713 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100714 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700715 if (destination.IsRegister()) {
716 // Move to GPR from GPR
717 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
718 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100719 DCHECK(destination.IsFpuRegister());
720 if (Primitive::Is64BitType(dst_type)) {
721 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
722 } else {
723 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
724 }
725 }
726 } else if (source.IsFpuRegister()) {
727 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700728 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100729 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
731 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100732 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700733 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
734 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100735 } else {
736 DCHECK(destination.IsRegister());
737 if (Primitive::Is64BitType(dst_type)) {
738 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
739 } else {
740 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
741 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700742 }
743 }
744 } else { // The destination is not a register. It must be a stack slot.
745 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
746 if (source.IsRegister() || source.IsFpuRegister()) {
747 if (unspecified_type) {
748 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100749 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700750 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100751 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 }
753 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100754 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
755 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700756 // Move to stack from GPR/FPR
757 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
758 if (source.IsRegister()) {
759 __ StoreToOffset(store_type,
760 source.AsRegister<GpuRegister>(),
761 SP,
762 destination.GetStackIndex());
763 } else {
764 __ StoreFpuToOffset(store_type,
765 source.AsFpuRegister<FpuRegister>(),
766 SP,
767 destination.GetStackIndex());
768 }
769 } else if (source.IsConstant()) {
770 // Move to stack from constant
771 HConstant* src_cst = source.GetConstant();
772 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700773 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700774 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700775 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
776 if (value != 0) {
777 gpr = TMP;
778 __ LoadConst32(gpr, value);
779 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700780 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700781 DCHECK(destination.IsDoubleStackSlot());
782 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
783 if (value != 0) {
784 gpr = TMP;
785 __ LoadConst64(gpr, value);
786 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700787 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700788 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700789 } else {
790 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
791 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
792 // Move to stack from stack
793 if (destination.IsStackSlot()) {
794 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
795 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
796 } else {
797 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
798 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
799 }
800 }
801 }
802}
803
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700804void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700805 DCHECK(!loc1.IsConstant());
806 DCHECK(!loc2.IsConstant());
807
808 if (loc1.Equals(loc2)) {
809 return;
810 }
811
812 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
813 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
814 bool is_fp_reg1 = loc1.IsFpuRegister();
815 bool is_fp_reg2 = loc2.IsFpuRegister();
816
817 if (loc2.IsRegister() && loc1.IsRegister()) {
818 // Swap 2 GPRs
819 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
820 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
821 __ Move(TMP, r2);
822 __ Move(r2, r1);
823 __ Move(r1, TMP);
824 } else if (is_fp_reg2 && is_fp_reg1) {
825 // Swap 2 FPRs
826 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
827 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700828 if (type == Primitive::kPrimFloat) {
829 __ MovS(FTMP, r1);
830 __ MovS(r1, r2);
831 __ MovS(r2, FTMP);
832 } else {
833 DCHECK_EQ(type, Primitive::kPrimDouble);
834 __ MovD(FTMP, r1);
835 __ MovD(r1, r2);
836 __ MovD(r2, FTMP);
837 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700838 } else if (is_slot1 != is_slot2) {
839 // Swap GPR/FPR and stack slot
840 Location reg_loc = is_slot1 ? loc2 : loc1;
841 Location mem_loc = is_slot1 ? loc1 : loc2;
842 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
843 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
844 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
845 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
846 if (reg_loc.IsFpuRegister()) {
847 __ StoreFpuToOffset(store_type,
848 reg_loc.AsFpuRegister<FpuRegister>(),
849 SP,
850 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700851 if (mem_loc.IsStackSlot()) {
852 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
853 } else {
854 DCHECK(mem_loc.IsDoubleStackSlot());
855 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
856 }
857 } else {
858 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
859 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
860 }
861 } else if (is_slot1 && is_slot2) {
862 move_resolver_.Exchange(loc1.GetStackIndex(),
863 loc2.GetStackIndex(),
864 loc1.IsDoubleStackSlot());
865 } else {
866 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
867 }
868}
869
870void CodeGeneratorMIPS64::Move(HInstruction* instruction,
871 Location location,
872 HInstruction* move_for) {
873 LocationSummary* locations = instruction->GetLocations();
874 Primitive::Type type = instruction->GetType();
875 DCHECK_NE(type, Primitive::kPrimVoid);
876
877 if (instruction->IsCurrentMethod()) {
878 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset), type);
879 } else if (locations != nullptr && locations->Out().Equals(location)) {
880 return;
881 } else if (instruction->IsIntConstant()
882 || instruction->IsLongConstant()
883 || instruction->IsNullConstant()) {
884 if (location.IsRegister()) {
885 // Move to GPR from constant
886 GpuRegister dst = location.AsRegister<GpuRegister>();
887 if (instruction->IsNullConstant() || instruction->IsIntConstant()) {
888 __ LoadConst32(dst, GetInt32ValueOf(instruction->AsConstant()));
889 } else {
890 __ LoadConst64(dst, instruction->AsLongConstant()->GetValue());
891 }
892 } else {
893 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
894 // Move to stack from constant
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700895 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700896 if (location.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700897 int32_t value = GetInt32ValueOf(instruction->AsConstant());
898 if (value != 0) {
899 gpr = TMP;
900 __ LoadConst32(gpr, value);
901 }
902 __ StoreToOffset(kStoreWord, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700903 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700904 DCHECK(location.IsDoubleStackSlot());
905 int64_t value = instruction->AsLongConstant()->GetValue();
906 if (value != 0) {
907 gpr = TMP;
908 __ LoadConst64(gpr, value);
909 }
910 __ StoreToOffset(kStoreDoubleword, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700911 }
912 }
913 } else if (instruction->IsTemporary()) {
914 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
915 MoveLocation(location, temp_location, type);
916 } else if (instruction->IsLoadLocal()) {
917 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
918 if (Primitive::Is64BitType(type)) {
919 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
920 } else {
921 MoveLocation(location, Location::StackSlot(stack_slot), type);
922 }
923 } else {
924 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
925 MoveLocation(location, locations->Out(), type);
926 }
927}
928
Calin Juravle175dc732015-08-25 15:42:32 +0100929void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
930 DCHECK(location.IsRegister());
931 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
932}
933
Calin Juravlee460d1d2015-09-29 04:52:17 +0100934void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
935 if (location.IsRegister()) {
936 locations->AddTemp(location);
937 } else {
938 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
939 }
940}
941
Alexey Frunze4dda3372015-06-01 18:31:49 -0700942Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
943 Primitive::Type type = load->GetType();
944
945 switch (type) {
946 case Primitive::kPrimNot:
947 case Primitive::kPrimInt:
948 case Primitive::kPrimFloat:
949 return Location::StackSlot(GetStackSlot(load->GetLocal()));
950
951 case Primitive::kPrimLong:
952 case Primitive::kPrimDouble:
953 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
954
955 case Primitive::kPrimBoolean:
956 case Primitive::kPrimByte:
957 case Primitive::kPrimChar:
958 case Primitive::kPrimShort:
959 case Primitive::kPrimVoid:
960 LOG(FATAL) << "Unexpected type " << type;
961 }
962
963 LOG(FATAL) << "Unreachable";
964 return Location::NoLocation();
965}
966
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100967void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
968 GpuRegister value,
969 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700970 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700971 GpuRegister card = AT;
972 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100973 if (value_can_be_null) {
974 __ Beqzc(value, &done);
975 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700976 __ LoadFromOffset(kLoadDoubleword,
977 card,
978 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200979 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700980 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
981 __ Daddu(temp, card, temp);
982 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100983 if (value_can_be_null) {
984 __ Bind(&done);
985 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700986}
987
David Brazdil58282f42016-01-14 12:45:10 +0000988void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700989 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
990 blocked_core_registers_[ZERO] = true;
991 blocked_core_registers_[K0] = true;
992 blocked_core_registers_[K1] = true;
993 blocked_core_registers_[GP] = true;
994 blocked_core_registers_[SP] = true;
995 blocked_core_registers_[RA] = true;
996
Lazar Trsicd9672662015-09-03 17:33:01 +0200997 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
998 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700999 blocked_core_registers_[AT] = true;
1000 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001001 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001002 blocked_fpu_registers_[FTMP] = true;
1003
1004 // Reserve suspend and thread registers.
1005 blocked_core_registers_[S0] = true;
1006 blocked_core_registers_[TR] = true;
1007
1008 // Reserve T9 for function calls
1009 blocked_core_registers_[T9] = true;
1010
1011 // TODO: review; anything else?
1012
David Brazdil58282f42016-01-14 12:45:10 +00001013 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001014 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1015 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1016 }
1017
1018 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1019 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1020 }
1021}
1022
Alexey Frunze4dda3372015-06-01 18:31:49 -07001023size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1024 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001025 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001026}
1027
1028size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1029 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001030 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001031}
1032
1033size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1034 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001035 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001036}
1037
1038size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1039 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001040 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001041}
1042
1043void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001044 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001045}
1046
1047void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001048 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001049}
1050
Calin Juravle175dc732015-08-25 15:42:32 +01001051void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1052 HInstruction* instruction,
1053 uint32_t dex_pc,
1054 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001055 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001056 instruction,
1057 dex_pc,
1058 slow_path);
1059}
1060
Alexey Frunze4dda3372015-06-01 18:31:49 -07001061void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
1062 HInstruction* instruction,
1063 uint32_t dex_pc,
1064 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001065 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001066 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1067 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1068 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001069 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001070 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001071}
1072
1073void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1074 GpuRegister class_reg) {
1075 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1076 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1077 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1078 // TODO: barrier needed?
1079 __ Bind(slow_path->GetExitLabel());
1080}
1081
1082void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1083 __ Sync(0); // only stype 0 is supported
1084}
1085
1086void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1087 HBasicBlock* successor) {
1088 SuspendCheckSlowPathMIPS64* slow_path =
1089 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1090 codegen_->AddSlowPath(slow_path);
1091
1092 __ LoadFromOffset(kLoadUnsignedHalfword,
1093 TMP,
1094 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001095 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001096 if (successor == nullptr) {
1097 __ Bnezc(TMP, slow_path->GetEntryLabel());
1098 __ Bind(slow_path->GetReturnLabel());
1099 } else {
1100 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001101 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001102 // slow_path will return to GetLabelOf(successor).
1103 }
1104}
1105
1106InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1107 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001108 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001109 assembler_(codegen->GetAssembler()),
1110 codegen_(codegen) {}
1111
1112void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1113 DCHECK_EQ(instruction->InputCount(), 2U);
1114 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1115 Primitive::Type type = instruction->GetResultType();
1116 switch (type) {
1117 case Primitive::kPrimInt:
1118 case Primitive::kPrimLong: {
1119 locations->SetInAt(0, Location::RequiresRegister());
1120 HInstruction* right = instruction->InputAt(1);
1121 bool can_use_imm = false;
1122 if (right->IsConstant()) {
1123 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1124 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1125 can_use_imm = IsUint<16>(imm);
1126 } else if (instruction->IsAdd()) {
1127 can_use_imm = IsInt<16>(imm);
1128 } else {
1129 DCHECK(instruction->IsSub());
1130 can_use_imm = IsInt<16>(-imm);
1131 }
1132 }
1133 if (can_use_imm)
1134 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1135 else
1136 locations->SetInAt(1, Location::RequiresRegister());
1137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1138 }
1139 break;
1140
1141 case Primitive::kPrimFloat:
1142 case Primitive::kPrimDouble:
1143 locations->SetInAt(0, Location::RequiresFpuRegister());
1144 locations->SetInAt(1, Location::RequiresFpuRegister());
1145 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1146 break;
1147
1148 default:
1149 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1150 }
1151}
1152
1153void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1154 Primitive::Type type = instruction->GetType();
1155 LocationSummary* locations = instruction->GetLocations();
1156
1157 switch (type) {
1158 case Primitive::kPrimInt:
1159 case Primitive::kPrimLong: {
1160 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1161 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1162 Location rhs_location = locations->InAt(1);
1163
1164 GpuRegister rhs_reg = ZERO;
1165 int64_t rhs_imm = 0;
1166 bool use_imm = rhs_location.IsConstant();
1167 if (use_imm) {
1168 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1169 } else {
1170 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1171 }
1172
1173 if (instruction->IsAnd()) {
1174 if (use_imm)
1175 __ Andi(dst, lhs, rhs_imm);
1176 else
1177 __ And(dst, lhs, rhs_reg);
1178 } else if (instruction->IsOr()) {
1179 if (use_imm)
1180 __ Ori(dst, lhs, rhs_imm);
1181 else
1182 __ Or(dst, lhs, rhs_reg);
1183 } else if (instruction->IsXor()) {
1184 if (use_imm)
1185 __ Xori(dst, lhs, rhs_imm);
1186 else
1187 __ Xor(dst, lhs, rhs_reg);
1188 } else if (instruction->IsAdd()) {
1189 if (type == Primitive::kPrimInt) {
1190 if (use_imm)
1191 __ Addiu(dst, lhs, rhs_imm);
1192 else
1193 __ Addu(dst, lhs, rhs_reg);
1194 } else {
1195 if (use_imm)
1196 __ Daddiu(dst, lhs, rhs_imm);
1197 else
1198 __ Daddu(dst, lhs, rhs_reg);
1199 }
1200 } else {
1201 DCHECK(instruction->IsSub());
1202 if (type == Primitive::kPrimInt) {
1203 if (use_imm)
1204 __ Addiu(dst, lhs, -rhs_imm);
1205 else
1206 __ Subu(dst, lhs, rhs_reg);
1207 } else {
1208 if (use_imm)
1209 __ Daddiu(dst, lhs, -rhs_imm);
1210 else
1211 __ Dsubu(dst, lhs, rhs_reg);
1212 }
1213 }
1214 break;
1215 }
1216 case Primitive::kPrimFloat:
1217 case Primitive::kPrimDouble: {
1218 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1219 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1220 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1221 if (instruction->IsAdd()) {
1222 if (type == Primitive::kPrimFloat)
1223 __ AddS(dst, lhs, rhs);
1224 else
1225 __ AddD(dst, lhs, rhs);
1226 } else if (instruction->IsSub()) {
1227 if (type == Primitive::kPrimFloat)
1228 __ SubS(dst, lhs, rhs);
1229 else
1230 __ SubD(dst, lhs, rhs);
1231 } else {
1232 LOG(FATAL) << "Unexpected floating-point binary operation";
1233 }
1234 break;
1235 }
1236 default:
1237 LOG(FATAL) << "Unexpected binary operation type " << type;
1238 }
1239}
1240
1241void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001242 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001243
1244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1245 Primitive::Type type = instr->GetResultType();
1246 switch (type) {
1247 case Primitive::kPrimInt:
1248 case Primitive::kPrimLong: {
1249 locations->SetInAt(0, Location::RequiresRegister());
1250 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001252 break;
1253 }
1254 default:
1255 LOG(FATAL) << "Unexpected shift type " << type;
1256 }
1257}
1258
1259void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001260 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001261 LocationSummary* locations = instr->GetLocations();
1262 Primitive::Type type = instr->GetType();
1263
1264 switch (type) {
1265 case Primitive::kPrimInt:
1266 case Primitive::kPrimLong: {
1267 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1268 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1269 Location rhs_location = locations->InAt(1);
1270
1271 GpuRegister rhs_reg = ZERO;
1272 int64_t rhs_imm = 0;
1273 bool use_imm = rhs_location.IsConstant();
1274 if (use_imm) {
1275 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1276 } else {
1277 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1278 }
1279
1280 if (use_imm) {
1281 uint32_t shift_value = (type == Primitive::kPrimInt)
1282 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1283 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1284
Alexey Frunze92d90602015-12-18 18:16:36 -08001285 if (shift_value == 0) {
1286 if (dst != lhs) {
1287 __ Move(dst, lhs);
1288 }
1289 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001290 if (instr->IsShl()) {
1291 __ Sll(dst, lhs, shift_value);
1292 } else if (instr->IsShr()) {
1293 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001294 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001295 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001296 } else {
1297 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001298 }
1299 } else {
1300 if (shift_value < 32) {
1301 if (instr->IsShl()) {
1302 __ Dsll(dst, lhs, shift_value);
1303 } else if (instr->IsShr()) {
1304 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001305 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001306 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001307 } else {
1308 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001309 }
1310 } else {
1311 shift_value -= 32;
1312 if (instr->IsShl()) {
1313 __ Dsll32(dst, lhs, shift_value);
1314 } else if (instr->IsShr()) {
1315 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001316 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001317 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001318 } else {
1319 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001320 }
1321 }
1322 }
1323 } else {
1324 if (type == Primitive::kPrimInt) {
1325 if (instr->IsShl()) {
1326 __ Sllv(dst, lhs, rhs_reg);
1327 } else if (instr->IsShr()) {
1328 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001329 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001330 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001331 } else {
1332 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001333 }
1334 } else {
1335 if (instr->IsShl()) {
1336 __ Dsllv(dst, lhs, rhs_reg);
1337 } else if (instr->IsShr()) {
1338 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001339 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001340 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001341 } else {
1342 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001343 }
1344 }
1345 }
1346 break;
1347 }
1348 default:
1349 LOG(FATAL) << "Unexpected shift operation type " << type;
1350 }
1351}
1352
1353void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1354 HandleBinaryOp(instruction);
1355}
1356
1357void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1358 HandleBinaryOp(instruction);
1359}
1360
1361void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1362 HandleBinaryOp(instruction);
1363}
1364
1365void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1366 HandleBinaryOp(instruction);
1367}
1368
1369void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1370 LocationSummary* locations =
1371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1372 locations->SetInAt(0, Location::RequiresRegister());
1373 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1374 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1375 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1376 } else {
1377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1378 }
1379}
1380
1381void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1382 LocationSummary* locations = instruction->GetLocations();
1383 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1384 Location index = locations->InAt(1);
1385 Primitive::Type type = instruction->GetType();
1386
1387 switch (type) {
1388 case Primitive::kPrimBoolean: {
1389 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1390 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1391 if (index.IsConstant()) {
1392 size_t offset =
1393 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1394 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1395 } else {
1396 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1397 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1398 }
1399 break;
1400 }
1401
1402 case Primitive::kPrimByte: {
1403 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1404 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1405 if (index.IsConstant()) {
1406 size_t offset =
1407 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1408 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1409 } else {
1410 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1411 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1412 }
1413 break;
1414 }
1415
1416 case Primitive::kPrimShort: {
1417 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1418 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1419 if (index.IsConstant()) {
1420 size_t offset =
1421 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1422 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1423 } else {
1424 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1425 __ Daddu(TMP, obj, TMP);
1426 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1427 }
1428 break;
1429 }
1430
1431 case Primitive::kPrimChar: {
1432 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1433 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1434 if (index.IsConstant()) {
1435 size_t offset =
1436 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1437 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1438 } else {
1439 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1440 __ Daddu(TMP, obj, TMP);
1441 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1442 }
1443 break;
1444 }
1445
1446 case Primitive::kPrimInt:
1447 case Primitive::kPrimNot: {
1448 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1449 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1450 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1451 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1452 if (index.IsConstant()) {
1453 size_t offset =
1454 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1455 __ LoadFromOffset(load_type, out, obj, offset);
1456 } else {
1457 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1458 __ Daddu(TMP, obj, TMP);
1459 __ LoadFromOffset(load_type, out, TMP, data_offset);
1460 }
1461 break;
1462 }
1463
1464 case Primitive::kPrimLong: {
1465 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1466 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1467 if (index.IsConstant()) {
1468 size_t offset =
1469 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1470 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1471 } else {
1472 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1473 __ Daddu(TMP, obj, TMP);
1474 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1475 }
1476 break;
1477 }
1478
1479 case Primitive::kPrimFloat: {
1480 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1481 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1482 if (index.IsConstant()) {
1483 size_t offset =
1484 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1485 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1486 } else {
1487 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1488 __ Daddu(TMP, obj, TMP);
1489 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1490 }
1491 break;
1492 }
1493
1494 case Primitive::kPrimDouble: {
1495 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1496 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1497 if (index.IsConstant()) {
1498 size_t offset =
1499 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1500 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1501 } else {
1502 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1503 __ Daddu(TMP, obj, TMP);
1504 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1505 }
1506 break;
1507 }
1508
1509 case Primitive::kPrimVoid:
1510 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1511 UNREACHABLE();
1512 }
1513 codegen_->MaybeRecordImplicitNullCheck(instruction);
1514}
1515
1516void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1518 locations->SetInAt(0, Location::RequiresRegister());
1519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1520}
1521
1522void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1523 LocationSummary* locations = instruction->GetLocations();
1524 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1525 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1526 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1527 __ LoadFromOffset(kLoadWord, out, obj, offset);
1528 codegen_->MaybeRecordImplicitNullCheck(instruction);
1529}
1530
1531void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001532 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1534 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001535 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1536 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001537 InvokeRuntimeCallingConvention calling_convention;
1538 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1539 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1540 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1541 } else {
1542 locations->SetInAt(0, Location::RequiresRegister());
1543 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1544 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1545 locations->SetInAt(2, Location::RequiresFpuRegister());
1546 } else {
1547 locations->SetInAt(2, Location::RequiresRegister());
1548 }
1549 }
1550}
1551
1552void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1553 LocationSummary* locations = instruction->GetLocations();
1554 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1555 Location index = locations->InAt(1);
1556 Primitive::Type value_type = instruction->GetComponentType();
1557 bool needs_runtime_call = locations->WillCall();
1558 bool needs_write_barrier =
1559 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1560
1561 switch (value_type) {
1562 case Primitive::kPrimBoolean:
1563 case Primitive::kPrimByte: {
1564 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1565 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1566 if (index.IsConstant()) {
1567 size_t offset =
1568 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1569 __ StoreToOffset(kStoreByte, value, obj, offset);
1570 } else {
1571 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1572 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1573 }
1574 break;
1575 }
1576
1577 case Primitive::kPrimShort:
1578 case Primitive::kPrimChar: {
1579 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1580 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1581 if (index.IsConstant()) {
1582 size_t offset =
1583 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1584 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1585 } else {
1586 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1587 __ Daddu(TMP, obj, TMP);
1588 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1589 }
1590 break;
1591 }
1592
1593 case Primitive::kPrimInt:
1594 case Primitive::kPrimNot: {
1595 if (!needs_runtime_call) {
1596 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1597 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1598 if (index.IsConstant()) {
1599 size_t offset =
1600 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1601 __ StoreToOffset(kStoreWord, value, obj, offset);
1602 } else {
1603 DCHECK(index.IsRegister()) << index;
1604 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1605 __ Daddu(TMP, obj, TMP);
1606 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1607 }
1608 codegen_->MaybeRecordImplicitNullCheck(instruction);
1609 if (needs_write_barrier) {
1610 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001611 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001612 }
1613 } else {
1614 DCHECK_EQ(value_type, Primitive::kPrimNot);
1615 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1616 instruction,
1617 instruction->GetDexPc(),
1618 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001619 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001620 }
1621 break;
1622 }
1623
1624 case Primitive::kPrimLong: {
1625 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1626 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1627 if (index.IsConstant()) {
1628 size_t offset =
1629 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1630 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1631 } else {
1632 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1633 __ Daddu(TMP, obj, TMP);
1634 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1635 }
1636 break;
1637 }
1638
1639 case Primitive::kPrimFloat: {
1640 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1641 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1642 DCHECK(locations->InAt(2).IsFpuRegister());
1643 if (index.IsConstant()) {
1644 size_t offset =
1645 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1646 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1647 } else {
1648 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1649 __ Daddu(TMP, obj, TMP);
1650 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1651 }
1652 break;
1653 }
1654
1655 case Primitive::kPrimDouble: {
1656 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1657 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1658 DCHECK(locations->InAt(2).IsFpuRegister());
1659 if (index.IsConstant()) {
1660 size_t offset =
1661 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1662 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1663 } else {
1664 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1665 __ Daddu(TMP, obj, TMP);
1666 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1667 }
1668 break;
1669 }
1670
1671 case Primitive::kPrimVoid:
1672 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1673 UNREACHABLE();
1674 }
1675
1676 // Ints and objects are handled in the switch.
1677 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1678 codegen_->MaybeRecordImplicitNullCheck(instruction);
1679 }
1680}
1681
1682void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001683 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1684 ? LocationSummary::kCallOnSlowPath
1685 : LocationSummary::kNoCall;
1686 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001687 locations->SetInAt(0, Location::RequiresRegister());
1688 locations->SetInAt(1, Location::RequiresRegister());
1689 if (instruction->HasUses()) {
1690 locations->SetOut(Location::SameAsFirstInput());
1691 }
1692}
1693
1694void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1695 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001696 BoundsCheckSlowPathMIPS64* slow_path =
1697 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001698 codegen_->AddSlowPath(slow_path);
1699
1700 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1701 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1702
1703 // length is limited by the maximum positive signed 32-bit integer.
1704 // Unsigned comparison of length and index checks for index < 0
1705 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001706 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001707}
1708
1709void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1710 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1711 instruction,
1712 LocationSummary::kCallOnSlowPath);
1713 locations->SetInAt(0, Location::RequiresRegister());
1714 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001715 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001716 locations->AddTemp(Location::RequiresRegister());
1717}
1718
1719void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1720 LocationSummary* locations = instruction->GetLocations();
1721 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1722 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1723 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1724
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001725 SlowPathCodeMIPS64* slow_path =
1726 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001727 codegen_->AddSlowPath(slow_path);
1728
1729 // TODO: avoid this check if we know obj is not null.
1730 __ Beqzc(obj, slow_path->GetExitLabel());
1731 // Compare the class of `obj` with `cls`.
1732 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1733 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1734 __ Bind(slow_path->GetExitLabel());
1735}
1736
1737void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1738 LocationSummary* locations =
1739 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1740 locations->SetInAt(0, Location::RequiresRegister());
1741 if (check->HasUses()) {
1742 locations->SetOut(Location::SameAsFirstInput());
1743 }
1744}
1745
1746void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1747 // We assume the class is not null.
1748 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1749 check->GetLoadClass(),
1750 check,
1751 check->GetDexPc(),
1752 true);
1753 codegen_->AddSlowPath(slow_path);
1754 GenerateClassInitializationCheck(slow_path,
1755 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1756}
1757
1758void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1759 Primitive::Type in_type = compare->InputAt(0)->GetType();
1760
Alexey Frunze299a9392015-12-08 16:08:02 -08001761 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001762
1763 switch (in_type) {
1764 case Primitive::kPrimLong:
1765 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001766 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001767 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1768 break;
1769
1770 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001771 case Primitive::kPrimDouble:
1772 locations->SetInAt(0, Location::RequiresFpuRegister());
1773 locations->SetInAt(1, Location::RequiresFpuRegister());
1774 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001775 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001776
1777 default:
1778 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1779 }
1780}
1781
1782void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1783 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001784 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001785 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08001786 bool gt_bias = instruction->IsGtBias();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001787
1788 // 0 if: left == right
1789 // 1 if: left > right
1790 // -1 if: left < right
1791 switch (in_type) {
1792 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001793 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001794 Location rhs_location = locations->InAt(1);
1795 bool use_imm = rhs_location.IsConstant();
1796 GpuRegister rhs = ZERO;
1797 if (use_imm) {
1798 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1799 if (value != 0) {
1800 rhs = AT;
1801 __ LoadConst64(rhs, value);
1802 }
1803 } else {
1804 rhs = rhs_location.AsRegister<GpuRegister>();
1805 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001806 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001807 __ Slt(res, rhs, lhs);
1808 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001809 break;
1810 }
1811
Alexey Frunze299a9392015-12-08 16:08:02 -08001812 case Primitive::kPrimFloat: {
1813 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1814 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1815 Mips64Label done;
1816 __ CmpEqS(FTMP, lhs, rhs);
1817 __ LoadConst32(res, 0);
1818 __ Bc1nez(FTMP, &done);
1819 if (gt_bias) {
1820 __ CmpLtS(FTMP, lhs, rhs);
1821 __ LoadConst32(res, -1);
1822 __ Bc1nez(FTMP, &done);
1823 __ LoadConst32(res, 1);
1824 } else {
1825 __ CmpLtS(FTMP, rhs, lhs);
1826 __ LoadConst32(res, 1);
1827 __ Bc1nez(FTMP, &done);
1828 __ LoadConst32(res, -1);
1829 }
1830 __ Bind(&done);
1831 break;
1832 }
1833
Alexey Frunze4dda3372015-06-01 18:31:49 -07001834 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001835 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1836 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1837 Mips64Label done;
1838 __ CmpEqD(FTMP, lhs, rhs);
1839 __ LoadConst32(res, 0);
1840 __ Bc1nez(FTMP, &done);
1841 if (gt_bias) {
1842 __ CmpLtD(FTMP, lhs, rhs);
1843 __ LoadConst32(res, -1);
1844 __ Bc1nez(FTMP, &done);
1845 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001846 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001847 __ CmpLtD(FTMP, rhs, lhs);
1848 __ LoadConst32(res, 1);
1849 __ Bc1nez(FTMP, &done);
1850 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001851 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001852 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001853 break;
1854 }
1855
1856 default:
1857 LOG(FATAL) << "Unimplemented compare type " << in_type;
1858 }
1859}
1860
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001861void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001862 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001863 switch (instruction->InputAt(0)->GetType()) {
1864 default:
1865 case Primitive::kPrimLong:
1866 locations->SetInAt(0, Location::RequiresRegister());
1867 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1868 break;
1869
1870 case Primitive::kPrimFloat:
1871 case Primitive::kPrimDouble:
1872 locations->SetInAt(0, Location::RequiresFpuRegister());
1873 locations->SetInAt(1, Location::RequiresFpuRegister());
1874 break;
1875 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001876 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001877 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1878 }
1879}
1880
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001881void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001882 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001883 return;
1884 }
1885
Alexey Frunze299a9392015-12-08 16:08:02 -08001886 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001887 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001888 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001889 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001890
Alexey Frunze299a9392015-12-08 16:08:02 -08001891 switch (type) {
1892 default:
1893 // Integer case.
1894 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1895 return;
1896 case Primitive::kPrimLong:
1897 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1898 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001899
Alexey Frunze299a9392015-12-08 16:08:02 -08001900 case Primitive::kPrimFloat:
1901 case Primitive::kPrimDouble:
1902 // TODO: don't use branches.
1903 GenerateFpCompareAndBranch(instruction->GetCondition(),
1904 instruction->IsGtBias(),
1905 type,
1906 locations,
1907 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001908 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001909 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001910
1911 // Convert the branches into the result.
1912 Mips64Label done;
1913
1914 // False case: result = 0.
1915 __ LoadConst32(dst, 0);
1916 __ Bc(&done);
1917
1918 // True case: result = 1.
1919 __ Bind(&true_label);
1920 __ LoadConst32(dst, 1);
1921 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001922}
1923
Alexey Frunzec857c742015-09-23 15:12:39 -07001924void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1925 DCHECK(instruction->IsDiv() || instruction->IsRem());
1926 Primitive::Type type = instruction->GetResultType();
1927
1928 LocationSummary* locations = instruction->GetLocations();
1929 Location second = locations->InAt(1);
1930 DCHECK(second.IsConstant());
1931
1932 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1933 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1934 int64_t imm = Int64FromConstant(second.GetConstant());
1935 DCHECK(imm == 1 || imm == -1);
1936
1937 if (instruction->IsRem()) {
1938 __ Move(out, ZERO);
1939 } else {
1940 if (imm == -1) {
1941 if (type == Primitive::kPrimInt) {
1942 __ Subu(out, ZERO, dividend);
1943 } else {
1944 DCHECK_EQ(type, Primitive::kPrimLong);
1945 __ Dsubu(out, ZERO, dividend);
1946 }
1947 } else if (out != dividend) {
1948 __ Move(out, dividend);
1949 }
1950 }
1951}
1952
1953void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1954 DCHECK(instruction->IsDiv() || instruction->IsRem());
1955 Primitive::Type type = instruction->GetResultType();
1956
1957 LocationSummary* locations = instruction->GetLocations();
1958 Location second = locations->InAt(1);
1959 DCHECK(second.IsConstant());
1960
1961 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1962 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1963 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001964 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001965 int ctz_imm = CTZ(abs_imm);
1966
1967 if (instruction->IsDiv()) {
1968 if (type == Primitive::kPrimInt) {
1969 if (ctz_imm == 1) {
1970 // Fast path for division by +/-2, which is very common.
1971 __ Srl(TMP, dividend, 31);
1972 } else {
1973 __ Sra(TMP, dividend, 31);
1974 __ Srl(TMP, TMP, 32 - ctz_imm);
1975 }
1976 __ Addu(out, dividend, TMP);
1977 __ Sra(out, out, ctz_imm);
1978 if (imm < 0) {
1979 __ Subu(out, ZERO, out);
1980 }
1981 } else {
1982 DCHECK_EQ(type, Primitive::kPrimLong);
1983 if (ctz_imm == 1) {
1984 // Fast path for division by +/-2, which is very common.
1985 __ Dsrl32(TMP, dividend, 31);
1986 } else {
1987 __ Dsra32(TMP, dividend, 31);
1988 if (ctz_imm > 32) {
1989 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1990 } else {
1991 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1992 }
1993 }
1994 __ Daddu(out, dividend, TMP);
1995 if (ctz_imm < 32) {
1996 __ Dsra(out, out, ctz_imm);
1997 } else {
1998 __ Dsra32(out, out, ctz_imm - 32);
1999 }
2000 if (imm < 0) {
2001 __ Dsubu(out, ZERO, out);
2002 }
2003 }
2004 } else {
2005 if (type == Primitive::kPrimInt) {
2006 if (ctz_imm == 1) {
2007 // Fast path for modulo +/-2, which is very common.
2008 __ Sra(TMP, dividend, 31);
2009 __ Subu(out, dividend, TMP);
2010 __ Andi(out, out, 1);
2011 __ Addu(out, out, TMP);
2012 } else {
2013 __ Sra(TMP, dividend, 31);
2014 __ Srl(TMP, TMP, 32 - ctz_imm);
2015 __ Addu(out, dividend, TMP);
2016 if (IsUint<16>(abs_imm - 1)) {
2017 __ Andi(out, out, abs_imm - 1);
2018 } else {
2019 __ Sll(out, out, 32 - ctz_imm);
2020 __ Srl(out, out, 32 - ctz_imm);
2021 }
2022 __ Subu(out, out, TMP);
2023 }
2024 } else {
2025 DCHECK_EQ(type, Primitive::kPrimLong);
2026 if (ctz_imm == 1) {
2027 // Fast path for modulo +/-2, which is very common.
2028 __ Dsra32(TMP, dividend, 31);
2029 __ Dsubu(out, dividend, TMP);
2030 __ Andi(out, out, 1);
2031 __ Daddu(out, out, TMP);
2032 } else {
2033 __ Dsra32(TMP, dividend, 31);
2034 if (ctz_imm > 32) {
2035 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2036 } else {
2037 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2038 }
2039 __ Daddu(out, dividend, TMP);
2040 if (IsUint<16>(abs_imm - 1)) {
2041 __ Andi(out, out, abs_imm - 1);
2042 } else {
2043 if (ctz_imm > 32) {
2044 __ Dsll(out, out, 64 - ctz_imm);
2045 __ Dsrl(out, out, 64 - ctz_imm);
2046 } else {
2047 __ Dsll32(out, out, 32 - ctz_imm);
2048 __ Dsrl32(out, out, 32 - ctz_imm);
2049 }
2050 }
2051 __ Dsubu(out, out, TMP);
2052 }
2053 }
2054 }
2055}
2056
2057void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2058 DCHECK(instruction->IsDiv() || instruction->IsRem());
2059
2060 LocationSummary* locations = instruction->GetLocations();
2061 Location second = locations->InAt(1);
2062 DCHECK(second.IsConstant());
2063
2064 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2065 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2066 int64_t imm = Int64FromConstant(second.GetConstant());
2067
2068 Primitive::Type type = instruction->GetResultType();
2069 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2070
2071 int64_t magic;
2072 int shift;
2073 CalculateMagicAndShiftForDivRem(imm,
2074 (type == Primitive::kPrimLong),
2075 &magic,
2076 &shift);
2077
2078 if (type == Primitive::kPrimInt) {
2079 __ LoadConst32(TMP, magic);
2080 __ MuhR6(TMP, dividend, TMP);
2081
2082 if (imm > 0 && magic < 0) {
2083 __ Addu(TMP, TMP, dividend);
2084 } else if (imm < 0 && magic > 0) {
2085 __ Subu(TMP, TMP, dividend);
2086 }
2087
2088 if (shift != 0) {
2089 __ Sra(TMP, TMP, shift);
2090 }
2091
2092 if (instruction->IsDiv()) {
2093 __ Sra(out, TMP, 31);
2094 __ Subu(out, TMP, out);
2095 } else {
2096 __ Sra(AT, TMP, 31);
2097 __ Subu(AT, TMP, AT);
2098 __ LoadConst32(TMP, imm);
2099 __ MulR6(TMP, AT, TMP);
2100 __ Subu(out, dividend, TMP);
2101 }
2102 } else {
2103 __ LoadConst64(TMP, magic);
2104 __ Dmuh(TMP, dividend, TMP);
2105
2106 if (imm > 0 && magic < 0) {
2107 __ Daddu(TMP, TMP, dividend);
2108 } else if (imm < 0 && magic > 0) {
2109 __ Dsubu(TMP, TMP, dividend);
2110 }
2111
2112 if (shift >= 32) {
2113 __ Dsra32(TMP, TMP, shift - 32);
2114 } else if (shift > 0) {
2115 __ Dsra(TMP, TMP, shift);
2116 }
2117
2118 if (instruction->IsDiv()) {
2119 __ Dsra32(out, TMP, 31);
2120 __ Dsubu(out, TMP, out);
2121 } else {
2122 __ Dsra32(AT, TMP, 31);
2123 __ Dsubu(AT, TMP, AT);
2124 __ LoadConst64(TMP, imm);
2125 __ Dmul(TMP, AT, TMP);
2126 __ Dsubu(out, dividend, TMP);
2127 }
2128 }
2129}
2130
2131void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2132 DCHECK(instruction->IsDiv() || instruction->IsRem());
2133 Primitive::Type type = instruction->GetResultType();
2134 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2135
2136 LocationSummary* locations = instruction->GetLocations();
2137 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2138 Location second = locations->InAt(1);
2139
2140 if (second.IsConstant()) {
2141 int64_t imm = Int64FromConstant(second.GetConstant());
2142 if (imm == 0) {
2143 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2144 } else if (imm == 1 || imm == -1) {
2145 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002146 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002147 DivRemByPowerOfTwo(instruction);
2148 } else {
2149 DCHECK(imm <= -2 || imm >= 2);
2150 GenerateDivRemWithAnyConstant(instruction);
2151 }
2152 } else {
2153 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2154 GpuRegister divisor = second.AsRegister<GpuRegister>();
2155 if (instruction->IsDiv()) {
2156 if (type == Primitive::kPrimInt)
2157 __ DivR6(out, dividend, divisor);
2158 else
2159 __ Ddiv(out, dividend, divisor);
2160 } else {
2161 if (type == Primitive::kPrimInt)
2162 __ ModR6(out, dividend, divisor);
2163 else
2164 __ Dmod(out, dividend, divisor);
2165 }
2166 }
2167}
2168
Alexey Frunze4dda3372015-06-01 18:31:49 -07002169void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2170 LocationSummary* locations =
2171 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2172 switch (div->GetResultType()) {
2173 case Primitive::kPrimInt:
2174 case Primitive::kPrimLong:
2175 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002176 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2178 break;
2179
2180 case Primitive::kPrimFloat:
2181 case Primitive::kPrimDouble:
2182 locations->SetInAt(0, Location::RequiresFpuRegister());
2183 locations->SetInAt(1, Location::RequiresFpuRegister());
2184 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2185 break;
2186
2187 default:
2188 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2189 }
2190}
2191
2192void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2193 Primitive::Type type = instruction->GetType();
2194 LocationSummary* locations = instruction->GetLocations();
2195
2196 switch (type) {
2197 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002198 case Primitive::kPrimLong:
2199 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002200 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002201 case Primitive::kPrimFloat:
2202 case Primitive::kPrimDouble: {
2203 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2204 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2205 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2206 if (type == Primitive::kPrimFloat)
2207 __ DivS(dst, lhs, rhs);
2208 else
2209 __ DivD(dst, lhs, rhs);
2210 break;
2211 }
2212 default:
2213 LOG(FATAL) << "Unexpected div type " << type;
2214 }
2215}
2216
2217void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002218 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2219 ? LocationSummary::kCallOnSlowPath
2220 : LocationSummary::kNoCall;
2221 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002222 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2223 if (instruction->HasUses()) {
2224 locations->SetOut(Location::SameAsFirstInput());
2225 }
2226}
2227
2228void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2229 SlowPathCodeMIPS64* slow_path =
2230 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2231 codegen_->AddSlowPath(slow_path);
2232 Location value = instruction->GetLocations()->InAt(0);
2233
2234 Primitive::Type type = instruction->GetType();
2235
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002236 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002237 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002238 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002239 }
2240
2241 if (value.IsConstant()) {
2242 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2243 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002244 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002245 } else {
2246 // A division by a non-null constant is valid. We don't need to perform
2247 // any check, so simply fall through.
2248 }
2249 } else {
2250 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2251 }
2252}
2253
2254void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2255 LocationSummary* locations =
2256 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2257 locations->SetOut(Location::ConstantLocation(constant));
2258}
2259
2260void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2261 // Will be generated at use site.
2262}
2263
2264void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2265 exit->SetLocations(nullptr);
2266}
2267
2268void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2269}
2270
2271void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2272 LocationSummary* locations =
2273 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2274 locations->SetOut(Location::ConstantLocation(constant));
2275}
2276
2277void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2278 // Will be generated at use site.
2279}
2280
David Brazdilfc6a86a2015-06-26 10:33:45 +00002281void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002282 DCHECK(!successor->IsExitBlock());
2283 HBasicBlock* block = got->GetBlock();
2284 HInstruction* previous = got->GetPrevious();
2285 HLoopInformation* info = block->GetLoopInformation();
2286
2287 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2288 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2289 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2290 return;
2291 }
2292 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2293 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2294 }
2295 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002296 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002297 }
2298}
2299
David Brazdilfc6a86a2015-06-26 10:33:45 +00002300void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2301 got->SetLocations(nullptr);
2302}
2303
2304void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2305 HandleGoto(got, got->GetSuccessor());
2306}
2307
2308void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2309 try_boundary->SetLocations(nullptr);
2310}
2311
2312void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2313 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2314 if (!successor->IsExitBlock()) {
2315 HandleGoto(try_boundary, successor);
2316 }
2317}
2318
Alexey Frunze299a9392015-12-08 16:08:02 -08002319void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2320 bool is64bit,
2321 LocationSummary* locations) {
2322 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2323 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2324 Location rhs_location = locations->InAt(1);
2325 GpuRegister rhs_reg = ZERO;
2326 int64_t rhs_imm = 0;
2327 bool use_imm = rhs_location.IsConstant();
2328 if (use_imm) {
2329 if (is64bit) {
2330 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2331 } else {
2332 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2333 }
2334 } else {
2335 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2336 }
2337 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2338
2339 switch (cond) {
2340 case kCondEQ:
2341 case kCondNE:
2342 if (use_imm && IsUint<16>(rhs_imm)) {
2343 __ Xori(dst, lhs, rhs_imm);
2344 } else {
2345 if (use_imm) {
2346 rhs_reg = TMP;
2347 __ LoadConst64(rhs_reg, rhs_imm);
2348 }
2349 __ Xor(dst, lhs, rhs_reg);
2350 }
2351 if (cond == kCondEQ) {
2352 __ Sltiu(dst, dst, 1);
2353 } else {
2354 __ Sltu(dst, ZERO, dst);
2355 }
2356 break;
2357
2358 case kCondLT:
2359 case kCondGE:
2360 if (use_imm && IsInt<16>(rhs_imm)) {
2361 __ Slti(dst, lhs, rhs_imm);
2362 } else {
2363 if (use_imm) {
2364 rhs_reg = TMP;
2365 __ LoadConst64(rhs_reg, rhs_imm);
2366 }
2367 __ Slt(dst, lhs, rhs_reg);
2368 }
2369 if (cond == kCondGE) {
2370 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2371 // only the slt instruction but no sge.
2372 __ Xori(dst, dst, 1);
2373 }
2374 break;
2375
2376 case kCondLE:
2377 case kCondGT:
2378 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2379 // Simulate lhs <= rhs via lhs < rhs + 1.
2380 __ Slti(dst, lhs, rhs_imm_plus_one);
2381 if (cond == kCondGT) {
2382 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2383 // only the slti instruction but no sgti.
2384 __ Xori(dst, dst, 1);
2385 }
2386 } else {
2387 if (use_imm) {
2388 rhs_reg = TMP;
2389 __ LoadConst64(rhs_reg, rhs_imm);
2390 }
2391 __ Slt(dst, rhs_reg, lhs);
2392 if (cond == kCondLE) {
2393 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2394 // only the slt instruction but no sle.
2395 __ Xori(dst, dst, 1);
2396 }
2397 }
2398 break;
2399
2400 case kCondB:
2401 case kCondAE:
2402 if (use_imm && IsInt<16>(rhs_imm)) {
2403 // Sltiu sign-extends its 16-bit immediate operand before
2404 // the comparison and thus lets us compare directly with
2405 // unsigned values in the ranges [0, 0x7fff] and
2406 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2407 __ Sltiu(dst, lhs, rhs_imm);
2408 } else {
2409 if (use_imm) {
2410 rhs_reg = TMP;
2411 __ LoadConst64(rhs_reg, rhs_imm);
2412 }
2413 __ Sltu(dst, lhs, rhs_reg);
2414 }
2415 if (cond == kCondAE) {
2416 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2417 // only the sltu instruction but no sgeu.
2418 __ Xori(dst, dst, 1);
2419 }
2420 break;
2421
2422 case kCondBE:
2423 case kCondA:
2424 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2425 // Simulate lhs <= rhs via lhs < rhs + 1.
2426 // Note that this only works if rhs + 1 does not overflow
2427 // to 0, hence the check above.
2428 // Sltiu sign-extends its 16-bit immediate operand before
2429 // the comparison and thus lets us compare directly with
2430 // unsigned values in the ranges [0, 0x7fff] and
2431 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2432 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2433 if (cond == kCondA) {
2434 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2435 // only the sltiu instruction but no sgtiu.
2436 __ Xori(dst, dst, 1);
2437 }
2438 } else {
2439 if (use_imm) {
2440 rhs_reg = TMP;
2441 __ LoadConst64(rhs_reg, rhs_imm);
2442 }
2443 __ Sltu(dst, rhs_reg, lhs);
2444 if (cond == kCondBE) {
2445 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2446 // only the sltu instruction but no sleu.
2447 __ Xori(dst, dst, 1);
2448 }
2449 }
2450 break;
2451 }
2452}
2453
2454void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2455 bool is64bit,
2456 LocationSummary* locations,
2457 Mips64Label* label) {
2458 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2459 Location rhs_location = locations->InAt(1);
2460 GpuRegister rhs_reg = ZERO;
2461 int64_t rhs_imm = 0;
2462 bool use_imm = rhs_location.IsConstant();
2463 if (use_imm) {
2464 if (is64bit) {
2465 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2466 } else {
2467 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2468 }
2469 } else {
2470 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2471 }
2472
2473 if (use_imm && rhs_imm == 0) {
2474 switch (cond) {
2475 case kCondEQ:
2476 case kCondBE: // <= 0 if zero
2477 __ Beqzc(lhs, label);
2478 break;
2479 case kCondNE:
2480 case kCondA: // > 0 if non-zero
2481 __ Bnezc(lhs, label);
2482 break;
2483 case kCondLT:
2484 __ Bltzc(lhs, label);
2485 break;
2486 case kCondGE:
2487 __ Bgezc(lhs, label);
2488 break;
2489 case kCondLE:
2490 __ Blezc(lhs, label);
2491 break;
2492 case kCondGT:
2493 __ Bgtzc(lhs, label);
2494 break;
2495 case kCondB: // always false
2496 break;
2497 case kCondAE: // always true
2498 __ Bc(label);
2499 break;
2500 }
2501 } else {
2502 if (use_imm) {
2503 rhs_reg = TMP;
2504 __ LoadConst64(rhs_reg, rhs_imm);
2505 }
2506 switch (cond) {
2507 case kCondEQ:
2508 __ Beqc(lhs, rhs_reg, label);
2509 break;
2510 case kCondNE:
2511 __ Bnec(lhs, rhs_reg, label);
2512 break;
2513 case kCondLT:
2514 __ Bltc(lhs, rhs_reg, label);
2515 break;
2516 case kCondGE:
2517 __ Bgec(lhs, rhs_reg, label);
2518 break;
2519 case kCondLE:
2520 __ Bgec(rhs_reg, lhs, label);
2521 break;
2522 case kCondGT:
2523 __ Bltc(rhs_reg, lhs, label);
2524 break;
2525 case kCondB:
2526 __ Bltuc(lhs, rhs_reg, label);
2527 break;
2528 case kCondAE:
2529 __ Bgeuc(lhs, rhs_reg, label);
2530 break;
2531 case kCondBE:
2532 __ Bgeuc(rhs_reg, lhs, label);
2533 break;
2534 case kCondA:
2535 __ Bltuc(rhs_reg, lhs, label);
2536 break;
2537 }
2538 }
2539}
2540
2541void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2542 bool gt_bias,
2543 Primitive::Type type,
2544 LocationSummary* locations,
2545 Mips64Label* label) {
2546 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2547 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2548 if (type == Primitive::kPrimFloat) {
2549 switch (cond) {
2550 case kCondEQ:
2551 __ CmpEqS(FTMP, lhs, rhs);
2552 __ Bc1nez(FTMP, label);
2553 break;
2554 case kCondNE:
2555 __ CmpEqS(FTMP, lhs, rhs);
2556 __ Bc1eqz(FTMP, label);
2557 break;
2558 case kCondLT:
2559 if (gt_bias) {
2560 __ CmpLtS(FTMP, lhs, rhs);
2561 } else {
2562 __ CmpUltS(FTMP, lhs, rhs);
2563 }
2564 __ Bc1nez(FTMP, label);
2565 break;
2566 case kCondLE:
2567 if (gt_bias) {
2568 __ CmpLeS(FTMP, lhs, rhs);
2569 } else {
2570 __ CmpUleS(FTMP, lhs, rhs);
2571 }
2572 __ Bc1nez(FTMP, label);
2573 break;
2574 case kCondGT:
2575 if (gt_bias) {
2576 __ CmpUltS(FTMP, rhs, lhs);
2577 } else {
2578 __ CmpLtS(FTMP, rhs, lhs);
2579 }
2580 __ Bc1nez(FTMP, label);
2581 break;
2582 case kCondGE:
2583 if (gt_bias) {
2584 __ CmpUleS(FTMP, rhs, lhs);
2585 } else {
2586 __ CmpLeS(FTMP, rhs, lhs);
2587 }
2588 __ Bc1nez(FTMP, label);
2589 break;
2590 default:
2591 LOG(FATAL) << "Unexpected non-floating-point condition";
2592 }
2593 } else {
2594 DCHECK_EQ(type, Primitive::kPrimDouble);
2595 switch (cond) {
2596 case kCondEQ:
2597 __ CmpEqD(FTMP, lhs, rhs);
2598 __ Bc1nez(FTMP, label);
2599 break;
2600 case kCondNE:
2601 __ CmpEqD(FTMP, lhs, rhs);
2602 __ Bc1eqz(FTMP, label);
2603 break;
2604 case kCondLT:
2605 if (gt_bias) {
2606 __ CmpLtD(FTMP, lhs, rhs);
2607 } else {
2608 __ CmpUltD(FTMP, lhs, rhs);
2609 }
2610 __ Bc1nez(FTMP, label);
2611 break;
2612 case kCondLE:
2613 if (gt_bias) {
2614 __ CmpLeD(FTMP, lhs, rhs);
2615 } else {
2616 __ CmpUleD(FTMP, lhs, rhs);
2617 }
2618 __ Bc1nez(FTMP, label);
2619 break;
2620 case kCondGT:
2621 if (gt_bias) {
2622 __ CmpUltD(FTMP, rhs, lhs);
2623 } else {
2624 __ CmpLtD(FTMP, rhs, lhs);
2625 }
2626 __ Bc1nez(FTMP, label);
2627 break;
2628 case kCondGE:
2629 if (gt_bias) {
2630 __ CmpUleD(FTMP, rhs, lhs);
2631 } else {
2632 __ CmpLeD(FTMP, rhs, lhs);
2633 }
2634 __ Bc1nez(FTMP, label);
2635 break;
2636 default:
2637 LOG(FATAL) << "Unexpected non-floating-point condition";
2638 }
2639 }
2640}
2641
Alexey Frunze4dda3372015-06-01 18:31:49 -07002642void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002643 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002644 Mips64Label* true_target,
2645 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002646 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002647
David Brazdil0debae72015-11-12 18:37:00 +00002648 if (true_target == nullptr && false_target == nullptr) {
2649 // Nothing to do. The code always falls through.
2650 return;
2651 } else if (cond->IsIntConstant()) {
2652 // Constant condition, statically compared against 1.
2653 if (cond->AsIntConstant()->IsOne()) {
2654 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002655 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002656 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002657 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002658 DCHECK(cond->AsIntConstant()->IsZero());
2659 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002660 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002661 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002662 }
David Brazdil0debae72015-11-12 18:37:00 +00002663 return;
2664 }
2665
2666 // The following code generates these patterns:
2667 // (1) true_target == nullptr && false_target != nullptr
2668 // - opposite condition true => branch to false_target
2669 // (2) true_target != nullptr && false_target == nullptr
2670 // - condition true => branch to true_target
2671 // (3) true_target != nullptr && false_target != nullptr
2672 // - condition true => branch to true_target
2673 // - branch to false_target
2674 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002675 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002676 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002677 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002678 if (true_target == nullptr) {
2679 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2680 } else {
2681 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2682 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002683 } else {
2684 // The condition instruction has not been materialized, use its inputs as
2685 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002686 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002687 Primitive::Type type = condition->InputAt(0)->GetType();
2688 LocationSummary* locations = cond->GetLocations();
2689 IfCondition if_cond = condition->GetCondition();
2690 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002691
David Brazdil0debae72015-11-12 18:37:00 +00002692 if (true_target == nullptr) {
2693 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002694 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002695 }
2696
Alexey Frunze299a9392015-12-08 16:08:02 -08002697 switch (type) {
2698 default:
2699 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2700 break;
2701 case Primitive::kPrimLong:
2702 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2703 break;
2704 case Primitive::kPrimFloat:
2705 case Primitive::kPrimDouble:
2706 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2707 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002708 }
2709 }
David Brazdil0debae72015-11-12 18:37:00 +00002710
2711 // If neither branch falls through (case 3), the conditional branch to `true_target`
2712 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2713 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002714 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002715 }
2716}
2717
2718void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002720 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002721 locations->SetInAt(0, Location::RequiresRegister());
2722 }
2723}
2724
2725void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002726 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2727 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002728 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002729 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002730 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002731 nullptr : codegen_->GetLabelOf(false_successor);
2732 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002733}
2734
2735void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2736 LocationSummary* locations = new (GetGraph()->GetArena())
2737 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002738 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002739 locations->SetInAt(0, Location::RequiresRegister());
2740 }
2741}
2742
2743void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002744 SlowPathCodeMIPS64* slow_path =
2745 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002746 GenerateTestAndBranch(deoptimize,
2747 /* condition_input_index */ 0,
2748 slow_path->GetEntryLabel(),
2749 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002750}
2751
David Brazdil74eb1b22015-12-14 11:44:01 +00002752void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2753 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2754 if (Primitive::IsFloatingPointType(select->GetType())) {
2755 locations->SetInAt(0, Location::RequiresFpuRegister());
2756 locations->SetInAt(1, Location::RequiresFpuRegister());
2757 } else {
2758 locations->SetInAt(0, Location::RequiresRegister());
2759 locations->SetInAt(1, Location::RequiresRegister());
2760 }
2761 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2762 locations->SetInAt(2, Location::RequiresRegister());
2763 }
2764 locations->SetOut(Location::SameAsFirstInput());
2765}
2766
2767void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2768 LocationSummary* locations = select->GetLocations();
2769 Mips64Label false_target;
2770 GenerateTestAndBranch(select,
2771 /* condition_input_index */ 2,
2772 /* true_target */ nullptr,
2773 &false_target);
2774 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2775 __ Bind(&false_target);
2776}
2777
David Srbecky0cf44932015-12-09 14:09:59 +00002778void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2779 new (GetGraph()->GetArena()) LocationSummary(info);
2780}
2781
2782void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002783 if (codegen_->HasStackMapAtCurrentPc()) {
2784 // Ensure that we do not collide with the stack map of the previous instruction.
2785 __ Nop();
2786 }
David Srbecky0cf44932015-12-09 14:09:59 +00002787 codegen_->RecordPcInfo(info, info->GetDexPc());
2788}
2789
Alexey Frunze4dda3372015-06-01 18:31:49 -07002790void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2791 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2792 LocationSummary* locations =
2793 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2794 locations->SetInAt(0, Location::RequiresRegister());
2795 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2796 locations->SetOut(Location::RequiresFpuRegister());
2797 } else {
2798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2799 }
2800}
2801
2802void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2803 const FieldInfo& field_info) {
2804 Primitive::Type type = field_info.GetFieldType();
2805 LocationSummary* locations = instruction->GetLocations();
2806 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2807 LoadOperandType load_type = kLoadUnsignedByte;
2808 switch (type) {
2809 case Primitive::kPrimBoolean:
2810 load_type = kLoadUnsignedByte;
2811 break;
2812 case Primitive::kPrimByte:
2813 load_type = kLoadSignedByte;
2814 break;
2815 case Primitive::kPrimShort:
2816 load_type = kLoadSignedHalfword;
2817 break;
2818 case Primitive::kPrimChar:
2819 load_type = kLoadUnsignedHalfword;
2820 break;
2821 case Primitive::kPrimInt:
2822 case Primitive::kPrimFloat:
2823 load_type = kLoadWord;
2824 break;
2825 case Primitive::kPrimLong:
2826 case Primitive::kPrimDouble:
2827 load_type = kLoadDoubleword;
2828 break;
2829 case Primitive::kPrimNot:
2830 load_type = kLoadUnsignedWord;
2831 break;
2832 case Primitive::kPrimVoid:
2833 LOG(FATAL) << "Unreachable type " << type;
2834 UNREACHABLE();
2835 }
2836 if (!Primitive::IsFloatingPointType(type)) {
2837 DCHECK(locations->Out().IsRegister());
2838 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2839 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2840 } else {
2841 DCHECK(locations->Out().IsFpuRegister());
2842 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2843 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2844 }
2845
2846 codegen_->MaybeRecordImplicitNullCheck(instruction);
2847 // TODO: memory barrier?
2848}
2849
2850void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2851 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2852 LocationSummary* locations =
2853 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2854 locations->SetInAt(0, Location::RequiresRegister());
2855 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2856 locations->SetInAt(1, Location::RequiresFpuRegister());
2857 } else {
2858 locations->SetInAt(1, Location::RequiresRegister());
2859 }
2860}
2861
2862void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002863 const FieldInfo& field_info,
2864 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002865 Primitive::Type type = field_info.GetFieldType();
2866 LocationSummary* locations = instruction->GetLocations();
2867 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2868 StoreOperandType store_type = kStoreByte;
2869 switch (type) {
2870 case Primitive::kPrimBoolean:
2871 case Primitive::kPrimByte:
2872 store_type = kStoreByte;
2873 break;
2874 case Primitive::kPrimShort:
2875 case Primitive::kPrimChar:
2876 store_type = kStoreHalfword;
2877 break;
2878 case Primitive::kPrimInt:
2879 case Primitive::kPrimFloat:
2880 case Primitive::kPrimNot:
2881 store_type = kStoreWord;
2882 break;
2883 case Primitive::kPrimLong:
2884 case Primitive::kPrimDouble:
2885 store_type = kStoreDoubleword;
2886 break;
2887 case Primitive::kPrimVoid:
2888 LOG(FATAL) << "Unreachable type " << type;
2889 UNREACHABLE();
2890 }
2891 if (!Primitive::IsFloatingPointType(type)) {
2892 DCHECK(locations->InAt(1).IsRegister());
2893 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2894 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2895 } else {
2896 DCHECK(locations->InAt(1).IsFpuRegister());
2897 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2898 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2899 }
2900
2901 codegen_->MaybeRecordImplicitNullCheck(instruction);
2902 // TODO: memory barriers?
2903 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2904 DCHECK(locations->InAt(1).IsRegister());
2905 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002906 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002907 }
2908}
2909
2910void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2911 HandleFieldGet(instruction, instruction->GetFieldInfo());
2912}
2913
2914void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2915 HandleFieldGet(instruction, instruction->GetFieldInfo());
2916}
2917
2918void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2919 HandleFieldSet(instruction, instruction->GetFieldInfo());
2920}
2921
2922void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002923 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002924}
2925
2926void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2927 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002928 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002929 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2930 locations->SetInAt(0, Location::RequiresRegister());
2931 locations->SetInAt(1, Location::RequiresRegister());
2932 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002933 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002934 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2935}
2936
2937void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2938 LocationSummary* locations = instruction->GetLocations();
2939 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2940 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2941 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2942
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002943 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002944
2945 // Return 0 if `obj` is null.
2946 // TODO: Avoid this check if we know `obj` is not null.
2947 __ Move(out, ZERO);
2948 __ Beqzc(obj, &done);
2949
2950 // Compare the class of `obj` with `cls`.
2951 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002952 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002953 // Classes must be equal for the instanceof to succeed.
2954 __ Xor(out, out, cls);
2955 __ Sltiu(out, out, 1);
2956 } else {
2957 // If the classes are not equal, we go into a slow path.
2958 DCHECK(locations->OnlyCallsOnSlowPath());
2959 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002960 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002961 codegen_->AddSlowPath(slow_path);
2962 __ Bnec(out, cls, slow_path->GetEntryLabel());
2963 __ LoadConst32(out, 1);
2964 __ Bind(slow_path->GetExitLabel());
2965 }
2966
2967 __ Bind(&done);
2968}
2969
2970void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2972 locations->SetOut(Location::ConstantLocation(constant));
2973}
2974
2975void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2976 // Will be generated at use site.
2977}
2978
2979void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2980 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2981 locations->SetOut(Location::ConstantLocation(constant));
2982}
2983
2984void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2985 // Will be generated at use site.
2986}
2987
Calin Juravle175dc732015-08-25 15:42:32 +01002988void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2989 // The trampoline uses the same calling convention as dex calling conventions,
2990 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2991 // the method_idx.
2992 HandleInvoke(invoke);
2993}
2994
2995void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2996 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2997}
2998
Alexey Frunze4dda3372015-06-01 18:31:49 -07002999void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3000 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3001 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3002}
3003
3004void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3005 HandleInvoke(invoke);
3006 // The register T0 is required to be used for the hidden argument in
3007 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3008 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3009}
3010
3011void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3012 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3013 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
3014 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3015 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
3016 Location receiver = invoke->GetLocations()->InAt(0);
3017 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003018 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003019
3020 // Set the hidden argument.
3021 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3022 invoke->GetDexMethodIndex());
3023
3024 // temp = object->GetClass();
3025 if (receiver.IsStackSlot()) {
3026 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3027 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3028 } else {
3029 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3030 }
3031 codegen_->MaybeRecordImplicitNullCheck(invoke);
3032 // temp = temp->GetImtEntryAt(method_offset);
3033 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3034 // T9 = temp->GetEntryPoint();
3035 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3036 // T9();
3037 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003038 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003039 DCHECK(!codegen_->IsLeafMethod());
3040 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3041}
3042
3043void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003044 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3045 if (intrinsic.TryDispatch(invoke)) {
3046 return;
3047 }
3048
Alexey Frunze4dda3372015-06-01 18:31:49 -07003049 HandleInvoke(invoke);
3050}
3051
3052void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003053 // Explicit clinit checks triggered by static invokes must have been pruned by
3054 // art::PrepareForRegisterAllocation.
3055 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003056
Chris Larsen3039e382015-08-26 07:54:08 -07003057 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3058 if (intrinsic.TryDispatch(invoke)) {
3059 return;
3060 }
3061
Alexey Frunze4dda3372015-06-01 18:31:49 -07003062 HandleInvoke(invoke);
3063
3064 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
3065 // clobbering somewhere else, reduce further register pressure by avoiding
3066 // allocation of a register for the current method pointer like on x86 baseline.
3067 // TODO: remove this once all the issues with register saving/restoring are
3068 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003069 if (invoke->HasCurrentMethodInput()) {
3070 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00003071 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003072 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003073 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003074 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003075 }
3076}
3077
Chris Larsen3039e382015-08-26 07:54:08 -07003078static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003079 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003080 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3081 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003082 return true;
3083 }
3084 return false;
3085}
3086
Vladimir Markodc151b22015-10-15 18:02:30 +01003087HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3088 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3089 MethodReference target_method ATTRIBUTE_UNUSED) {
3090 switch (desired_dispatch_info.method_load_kind) {
3091 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3092 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3093 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3094 return HInvokeStaticOrDirect::DispatchInfo {
3095 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3096 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3097 0u,
3098 0u
3099 };
3100 default:
3101 break;
3102 }
3103 switch (desired_dispatch_info.code_ptr_location) {
3104 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3105 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3106 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3107 return HInvokeStaticOrDirect::DispatchInfo {
3108 desired_dispatch_info.method_load_kind,
3109 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3110 desired_dispatch_info.method_load_data,
3111 0u
3112 };
3113 default:
3114 return desired_dispatch_info;
3115 }
3116}
3117
Alexey Frunze4dda3372015-06-01 18:31:49 -07003118void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3119 // All registers are assumed to be correctly set up per the calling convention.
3120
Vladimir Marko58155012015-08-19 12:49:41 +00003121 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3122 switch (invoke->GetMethodLoadKind()) {
3123 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3124 // temp = thread->string_init_entrypoint
3125 __ LoadFromOffset(kLoadDoubleword,
3126 temp.AsRegister<GpuRegister>(),
3127 TR,
3128 invoke->GetStringInitOffset());
3129 break;
3130 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003131 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003132 break;
3133 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3134 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3135 break;
3136 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003137 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003138 // TODO: Implement these types.
3139 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3140 LOG(FATAL) << "Unsupported";
3141 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003142 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003143 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003144 GpuRegister reg = temp.AsRegister<GpuRegister>();
3145 GpuRegister method_reg;
3146 if (current_method.IsRegister()) {
3147 method_reg = current_method.AsRegister<GpuRegister>();
3148 } else {
3149 // TODO: use the appropriate DCHECK() here if possible.
3150 // DCHECK(invoke->GetLocations()->Intrinsified());
3151 DCHECK(!current_method.IsValid());
3152 method_reg = reg;
3153 __ Ld(reg, SP, kCurrentMethodStackOffset);
3154 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003155
Vladimir Marko58155012015-08-19 12:49:41 +00003156 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003157 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003158 reg,
3159 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003160 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003161 // temp = temp[index_in_cache]
3162 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3163 __ LoadFromOffset(kLoadDoubleword,
3164 reg,
3165 reg,
3166 CodeGenerator::GetCachePointerOffset(index_in_cache));
3167 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003168 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003169 }
3170
Vladimir Marko58155012015-08-19 12:49:41 +00003171 switch (invoke->GetCodePtrLocation()) {
3172 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003173 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003174 break;
3175 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3176 // LR = invoke->GetDirectCodePtr();
3177 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3178 // LR()
3179 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003180 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003181 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003182 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003183 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3184 // TODO: Implement these types.
3185 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3186 LOG(FATAL) << "Unsupported";
3187 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003188 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3189 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3190 __ LoadFromOffset(kLoadDoubleword,
3191 T9,
3192 callee_method.AsRegister<GpuRegister>(),
3193 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003194 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003195 // T9()
3196 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003197 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003198 break;
3199 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003200 DCHECK(!IsLeafMethod());
3201}
3202
3203void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003204 // Explicit clinit checks triggered by static invokes must have been pruned by
3205 // art::PrepareForRegisterAllocation.
3206 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003207
3208 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3209 return;
3210 }
3211
3212 LocationSummary* locations = invoke->GetLocations();
3213 codegen_->GenerateStaticOrDirectCall(invoke,
3214 locations->HasTemps()
3215 ? locations->GetTemp(0)
3216 : Location::NoLocation());
3217 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3218}
3219
Alexey Frunze53afca12015-11-05 16:34:23 -08003220void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003221 // Use the calling convention instead of the location of the receiver, as
3222 // intrinsics may have put the receiver in a different register. In the intrinsics
3223 // slow path, the arguments have been moved to the right place, so here we are
3224 // guaranteed that the receiver is the first register of the calling convention.
3225 InvokeDexCallingConvention calling_convention;
3226 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3227
Alexey Frunze53afca12015-11-05 16:34:23 -08003228 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003229 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3230 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3231 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003232 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003233
3234 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003235 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003236 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003237 // temp = temp->GetMethodAt(method_offset);
3238 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3239 // T9 = temp->GetEntryPoint();
3240 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3241 // T9();
3242 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003243 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003244}
3245
3246void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3247 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3248 return;
3249 }
3250
3251 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003252 DCHECK(!codegen_->IsLeafMethod());
3253 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3254}
3255
3256void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003257 InvokeRuntimeCallingConvention calling_convention;
3258 CodeGenerator::CreateLoadClassLocationSummary(
3259 cls,
3260 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003261 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003262}
3263
3264void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3265 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003266 if (cls->NeedsAccessCheck()) {
3267 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3268 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3269 cls,
3270 cls->GetDexPc(),
3271 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003272 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003273 return;
3274 }
3275
3276 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3277 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3278 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003279 DCHECK(!cls->CanCallRuntime());
3280 DCHECK(!cls->MustGenerateClinitCheck());
3281 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3282 ArtMethod::DeclaringClassOffset().Int32Value());
3283 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003284 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3285 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003286 __ LoadFromOffset(
3287 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003288 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003289 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3290 DCHECK(cls->CanCallRuntime());
3291 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3292 cls,
3293 cls,
3294 cls->GetDexPc(),
3295 cls->MustGenerateClinitCheck());
3296 codegen_->AddSlowPath(slow_path);
3297 if (!cls->IsInDexCache()) {
3298 __ Beqzc(out, slow_path->GetEntryLabel());
3299 }
3300 if (cls->MustGenerateClinitCheck()) {
3301 GenerateClassInitializationCheck(slow_path, out);
3302 } else {
3303 __ Bind(slow_path->GetExitLabel());
3304 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003305 }
3306 }
3307}
3308
David Brazdilcb1c0552015-08-04 16:22:25 +01003309static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003310 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003311}
3312
Alexey Frunze4dda3372015-06-01 18:31:49 -07003313void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3314 LocationSummary* locations =
3315 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3316 locations->SetOut(Location::RequiresRegister());
3317}
3318
3319void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3320 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003321 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3322}
3323
3324void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3325 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3326}
3327
3328void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3329 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003330}
3331
3332void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3333 load->SetLocations(nullptr);
3334}
3335
3336void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3337 // Nothing to do, this is driven by the code generator.
3338}
3339
3340void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00003341 LocationSummary::CallKind call_kind = load->IsInDexCache()
3342 ? LocationSummary::kNoCall
3343 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003344 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003345 locations->SetInAt(0, Location::RequiresRegister());
3346 locations->SetOut(Location::RequiresRegister());
3347}
3348
3349void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003350 LocationSummary* locations = load->GetLocations();
3351 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3352 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3353 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3354 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003355 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003356 __ LoadFromOffset(
3357 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003358 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003359
3360 if (!load->IsInDexCache()) {
3361 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3362 codegen_->AddSlowPath(slow_path);
3363 __ Beqzc(out, slow_path->GetEntryLabel());
3364 __ Bind(slow_path->GetExitLabel());
3365 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003366}
3367
3368void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3369 local->SetLocations(nullptr);
3370}
3371
3372void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3373 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3374}
3375
3376void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3378 locations->SetOut(Location::ConstantLocation(constant));
3379}
3380
3381void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3382 // Will be generated at use site.
3383}
3384
3385void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3386 LocationSummary* locations =
3387 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3388 InvokeRuntimeCallingConvention calling_convention;
3389 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3390}
3391
3392void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3393 codegen_->InvokeRuntime(instruction->IsEnter()
3394 ? QUICK_ENTRY_POINT(pLockObject)
3395 : QUICK_ENTRY_POINT(pUnlockObject),
3396 instruction,
3397 instruction->GetDexPc(),
3398 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003399 if (instruction->IsEnter()) {
3400 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3401 } else {
3402 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3403 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003404}
3405
3406void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3407 LocationSummary* locations =
3408 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3409 switch (mul->GetResultType()) {
3410 case Primitive::kPrimInt:
3411 case Primitive::kPrimLong:
3412 locations->SetInAt(0, Location::RequiresRegister());
3413 locations->SetInAt(1, Location::RequiresRegister());
3414 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3415 break;
3416
3417 case Primitive::kPrimFloat:
3418 case Primitive::kPrimDouble:
3419 locations->SetInAt(0, Location::RequiresFpuRegister());
3420 locations->SetInAt(1, Location::RequiresFpuRegister());
3421 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3422 break;
3423
3424 default:
3425 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3426 }
3427}
3428
3429void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3430 Primitive::Type type = instruction->GetType();
3431 LocationSummary* locations = instruction->GetLocations();
3432
3433 switch (type) {
3434 case Primitive::kPrimInt:
3435 case Primitive::kPrimLong: {
3436 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3437 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3438 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3439 if (type == Primitive::kPrimInt)
3440 __ MulR6(dst, lhs, rhs);
3441 else
3442 __ Dmul(dst, lhs, rhs);
3443 break;
3444 }
3445 case Primitive::kPrimFloat:
3446 case Primitive::kPrimDouble: {
3447 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3448 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3449 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3450 if (type == Primitive::kPrimFloat)
3451 __ MulS(dst, lhs, rhs);
3452 else
3453 __ MulD(dst, lhs, rhs);
3454 break;
3455 }
3456 default:
3457 LOG(FATAL) << "Unexpected mul type " << type;
3458 }
3459}
3460
3461void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3462 LocationSummary* locations =
3463 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3464 switch (neg->GetResultType()) {
3465 case Primitive::kPrimInt:
3466 case Primitive::kPrimLong:
3467 locations->SetInAt(0, Location::RequiresRegister());
3468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3469 break;
3470
3471 case Primitive::kPrimFloat:
3472 case Primitive::kPrimDouble:
3473 locations->SetInAt(0, Location::RequiresFpuRegister());
3474 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3475 break;
3476
3477 default:
3478 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3479 }
3480}
3481
3482void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3483 Primitive::Type type = instruction->GetType();
3484 LocationSummary* locations = instruction->GetLocations();
3485
3486 switch (type) {
3487 case Primitive::kPrimInt:
3488 case Primitive::kPrimLong: {
3489 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3490 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3491 if (type == Primitive::kPrimInt)
3492 __ Subu(dst, ZERO, src);
3493 else
3494 __ Dsubu(dst, ZERO, src);
3495 break;
3496 }
3497 case Primitive::kPrimFloat:
3498 case Primitive::kPrimDouble: {
3499 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3500 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3501 if (type == Primitive::kPrimFloat)
3502 __ NegS(dst, src);
3503 else
3504 __ NegD(dst, src);
3505 break;
3506 }
3507 default:
3508 LOG(FATAL) << "Unexpected neg type " << type;
3509 }
3510}
3511
3512void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3513 LocationSummary* locations =
3514 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3515 InvokeRuntimeCallingConvention calling_convention;
3516 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3517 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3518 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3519 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3520}
3521
3522void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3523 LocationSummary* locations = instruction->GetLocations();
3524 // Move an uint16_t value to a register.
3525 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003526 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3527 instruction,
3528 instruction->GetDexPc(),
3529 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003530 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3531}
3532
3533void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3534 LocationSummary* locations =
3535 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3536 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003537 if (instruction->IsStringAlloc()) {
3538 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3539 } else {
3540 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3541 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3542 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003543 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3544}
3545
3546void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003547 if (instruction->IsStringAlloc()) {
3548 // String is allocated through StringFactory. Call NewEmptyString entry point.
3549 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003550 MemberOffset code_offset =
3551 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003552 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3553 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3554 __ Jalr(T9);
3555 __ Nop();
3556 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3557 } else {
3558 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3559 instruction,
3560 instruction->GetDexPc(),
3561 nullptr);
3562 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3563 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003564}
3565
3566void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3567 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3568 locations->SetInAt(0, Location::RequiresRegister());
3569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3570}
3571
3572void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3573 Primitive::Type type = instruction->GetType();
3574 LocationSummary* locations = instruction->GetLocations();
3575
3576 switch (type) {
3577 case Primitive::kPrimInt:
3578 case Primitive::kPrimLong: {
3579 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3580 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3581 __ Nor(dst, src, ZERO);
3582 break;
3583 }
3584
3585 default:
3586 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3587 }
3588}
3589
3590void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3592 locations->SetInAt(0, Location::RequiresRegister());
3593 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3594}
3595
3596void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3597 LocationSummary* locations = instruction->GetLocations();
3598 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3599 locations->InAt(0).AsRegister<GpuRegister>(),
3600 1);
3601}
3602
3603void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003604 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3605 ? LocationSummary::kCallOnSlowPath
3606 : LocationSummary::kNoCall;
3607 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003608 locations->SetInAt(0, Location::RequiresRegister());
3609 if (instruction->HasUses()) {
3610 locations->SetOut(Location::SameAsFirstInput());
3611 }
3612}
3613
3614void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3615 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3616 return;
3617 }
3618 Location obj = instruction->GetLocations()->InAt(0);
3619
3620 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3621 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3622}
3623
3624void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3625 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3626 codegen_->AddSlowPath(slow_path);
3627
3628 Location obj = instruction->GetLocations()->InAt(0);
3629
3630 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3631}
3632
3633void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003634 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003635 GenerateImplicitNullCheck(instruction);
3636 } else {
3637 GenerateExplicitNullCheck(instruction);
3638 }
3639}
3640
3641void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3642 HandleBinaryOp(instruction);
3643}
3644
3645void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3646 HandleBinaryOp(instruction);
3647}
3648
3649void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3650 LOG(FATAL) << "Unreachable";
3651}
3652
3653void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3654 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3655}
3656
3657void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3658 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3659 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3660 if (location.IsStackSlot()) {
3661 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3662 } else if (location.IsDoubleStackSlot()) {
3663 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3664 }
3665 locations->SetOut(location);
3666}
3667
3668void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3669 ATTRIBUTE_UNUSED) {
3670 // Nothing to do, the parameter is already at its location.
3671}
3672
3673void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3674 LocationSummary* locations =
3675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3676 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3677}
3678
3679void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3680 ATTRIBUTE_UNUSED) {
3681 // Nothing to do, the method is already at its location.
3682}
3683
3684void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3686 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3687 locations->SetInAt(i, Location::Any());
3688 }
3689 locations->SetOut(Location::Any());
3690}
3691
3692void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3693 LOG(FATAL) << "Unreachable";
3694}
3695
3696void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3697 Primitive::Type type = rem->GetResultType();
3698 LocationSummary::CallKind call_kind =
3699 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3700 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3701
3702 switch (type) {
3703 case Primitive::kPrimInt:
3704 case Primitive::kPrimLong:
3705 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003706 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3708 break;
3709
3710 case Primitive::kPrimFloat:
3711 case Primitive::kPrimDouble: {
3712 InvokeRuntimeCallingConvention calling_convention;
3713 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3714 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3715 locations->SetOut(calling_convention.GetReturnLocation(type));
3716 break;
3717 }
3718
3719 default:
3720 LOG(FATAL) << "Unexpected rem type " << type;
3721 }
3722}
3723
3724void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3725 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003726
3727 switch (type) {
3728 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003729 case Primitive::kPrimLong:
3730 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003731 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003732
3733 case Primitive::kPrimFloat:
3734 case Primitive::kPrimDouble: {
3735 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3736 : QUICK_ENTRY_POINT(pFmod);
3737 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003738 if (type == Primitive::kPrimFloat) {
3739 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3740 } else {
3741 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3742 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003743 break;
3744 }
3745 default:
3746 LOG(FATAL) << "Unexpected rem type " << type;
3747 }
3748}
3749
3750void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3751 memory_barrier->SetLocations(nullptr);
3752}
3753
3754void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3755 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3756}
3757
3758void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3759 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3760 Primitive::Type return_type = ret->InputAt(0)->GetType();
3761 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3762}
3763
3764void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3765 codegen_->GenerateFrameExit();
3766}
3767
3768void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3769 ret->SetLocations(nullptr);
3770}
3771
3772void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3773 codegen_->GenerateFrameExit();
3774}
3775
Alexey Frunze92d90602015-12-18 18:16:36 -08003776void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3777 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003778}
3779
Alexey Frunze92d90602015-12-18 18:16:36 -08003780void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3781 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003782}
3783
Alexey Frunze4dda3372015-06-01 18:31:49 -07003784void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3785 HandleShift(shl);
3786}
3787
3788void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3789 HandleShift(shl);
3790}
3791
3792void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3793 HandleShift(shr);
3794}
3795
3796void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3797 HandleShift(shr);
3798}
3799
3800void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3802 Primitive::Type field_type = store->InputAt(1)->GetType();
3803 switch (field_type) {
3804 case Primitive::kPrimNot:
3805 case Primitive::kPrimBoolean:
3806 case Primitive::kPrimByte:
3807 case Primitive::kPrimChar:
3808 case Primitive::kPrimShort:
3809 case Primitive::kPrimInt:
3810 case Primitive::kPrimFloat:
3811 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3812 break;
3813
3814 case Primitive::kPrimLong:
3815 case Primitive::kPrimDouble:
3816 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3817 break;
3818
3819 default:
3820 LOG(FATAL) << "Unimplemented local type " << field_type;
3821 }
3822}
3823
3824void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3825}
3826
3827void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3828 HandleBinaryOp(instruction);
3829}
3830
3831void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3832 HandleBinaryOp(instruction);
3833}
3834
3835void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3836 HandleFieldGet(instruction, instruction->GetFieldInfo());
3837}
3838
3839void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3840 HandleFieldGet(instruction, instruction->GetFieldInfo());
3841}
3842
3843void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3844 HandleFieldSet(instruction, instruction->GetFieldInfo());
3845}
3846
3847void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003848 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003849}
3850
Calin Juravlee460d1d2015-09-29 04:52:17 +01003851void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3852 HUnresolvedInstanceFieldGet* instruction) {
3853 FieldAccessCallingConventionMIPS64 calling_convention;
3854 codegen_->CreateUnresolvedFieldLocationSummary(
3855 instruction, instruction->GetFieldType(), calling_convention);
3856}
3857
3858void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3859 HUnresolvedInstanceFieldGet* instruction) {
3860 FieldAccessCallingConventionMIPS64 calling_convention;
3861 codegen_->GenerateUnresolvedFieldAccess(instruction,
3862 instruction->GetFieldType(),
3863 instruction->GetFieldIndex(),
3864 instruction->GetDexPc(),
3865 calling_convention);
3866}
3867
3868void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3869 HUnresolvedInstanceFieldSet* instruction) {
3870 FieldAccessCallingConventionMIPS64 calling_convention;
3871 codegen_->CreateUnresolvedFieldLocationSummary(
3872 instruction, instruction->GetFieldType(), calling_convention);
3873}
3874
3875void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3876 HUnresolvedInstanceFieldSet* instruction) {
3877 FieldAccessCallingConventionMIPS64 calling_convention;
3878 codegen_->GenerateUnresolvedFieldAccess(instruction,
3879 instruction->GetFieldType(),
3880 instruction->GetFieldIndex(),
3881 instruction->GetDexPc(),
3882 calling_convention);
3883}
3884
3885void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3886 HUnresolvedStaticFieldGet* instruction) {
3887 FieldAccessCallingConventionMIPS64 calling_convention;
3888 codegen_->CreateUnresolvedFieldLocationSummary(
3889 instruction, instruction->GetFieldType(), calling_convention);
3890}
3891
3892void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3893 HUnresolvedStaticFieldGet* instruction) {
3894 FieldAccessCallingConventionMIPS64 calling_convention;
3895 codegen_->GenerateUnresolvedFieldAccess(instruction,
3896 instruction->GetFieldType(),
3897 instruction->GetFieldIndex(),
3898 instruction->GetDexPc(),
3899 calling_convention);
3900}
3901
3902void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3903 HUnresolvedStaticFieldSet* instruction) {
3904 FieldAccessCallingConventionMIPS64 calling_convention;
3905 codegen_->CreateUnresolvedFieldLocationSummary(
3906 instruction, instruction->GetFieldType(), calling_convention);
3907}
3908
3909void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3910 HUnresolvedStaticFieldSet* instruction) {
3911 FieldAccessCallingConventionMIPS64 calling_convention;
3912 codegen_->GenerateUnresolvedFieldAccess(instruction,
3913 instruction->GetFieldType(),
3914 instruction->GetFieldIndex(),
3915 instruction->GetDexPc(),
3916 calling_convention);
3917}
3918
Alexey Frunze4dda3372015-06-01 18:31:49 -07003919void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3921}
3922
3923void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3924 HBasicBlock* block = instruction->GetBlock();
3925 if (block->GetLoopInformation() != nullptr) {
3926 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3927 // The back edge will generate the suspend check.
3928 return;
3929 }
3930 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3931 // The goto will generate the suspend check.
3932 return;
3933 }
3934 GenerateSuspendCheck(instruction, nullptr);
3935}
3936
3937void LocationsBuilderMIPS64::VisitTemporary(HTemporary* temp) {
3938 temp->SetLocations(nullptr);
3939}
3940
3941void InstructionCodeGeneratorMIPS64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3942 // Nothing to do, this is driven by the code generator.
3943}
3944
3945void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3946 LocationSummary* locations =
3947 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3948 InvokeRuntimeCallingConvention calling_convention;
3949 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3950}
3951
3952void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3953 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3954 instruction,
3955 instruction->GetDexPc(),
3956 nullptr);
3957 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3958}
3959
3960void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3961 Primitive::Type input_type = conversion->GetInputType();
3962 Primitive::Type result_type = conversion->GetResultType();
3963 DCHECK_NE(input_type, result_type);
3964
3965 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3966 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3967 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3968 }
3969
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003970 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3971
3972 if (Primitive::IsFloatingPointType(input_type)) {
3973 locations->SetInAt(0, Location::RequiresFpuRegister());
3974 } else {
3975 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003976 }
3977
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003978 if (Primitive::IsFloatingPointType(result_type)) {
3979 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003980 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003981 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003982 }
3983}
3984
3985void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3986 LocationSummary* locations = conversion->GetLocations();
3987 Primitive::Type result_type = conversion->GetResultType();
3988 Primitive::Type input_type = conversion->GetInputType();
3989
3990 DCHECK_NE(input_type, result_type);
3991
3992 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3993 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3994 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3995
3996 switch (result_type) {
3997 case Primitive::kPrimChar:
3998 __ Andi(dst, src, 0xFFFF);
3999 break;
4000 case Primitive::kPrimByte:
4001 // long is never converted into types narrower than int directly,
4002 // so SEB and SEH can be used without ever causing unpredictable results
4003 // on 64-bit inputs
4004 DCHECK(input_type != Primitive::kPrimLong);
4005 __ Seb(dst, src);
4006 break;
4007 case Primitive::kPrimShort:
4008 // long is never converted into types narrower than int directly,
4009 // so SEB and SEH can be used without ever causing unpredictable results
4010 // on 64-bit inputs
4011 DCHECK(input_type != Primitive::kPrimLong);
4012 __ Seh(dst, src);
4013 break;
4014 case Primitive::kPrimInt:
4015 case Primitive::kPrimLong:
4016 // Sign-extend 32-bit int into bits 32 through 63 for
4017 // int-to-long and long-to-int conversions
4018 __ Sll(dst, src, 0);
4019 break;
4020
4021 default:
4022 LOG(FATAL) << "Unexpected type conversion from " << input_type
4023 << " to " << result_type;
4024 }
4025 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004026 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4027 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4028 if (input_type == Primitive::kPrimLong) {
4029 __ Dmtc1(src, FTMP);
4030 if (result_type == Primitive::kPrimFloat) {
4031 __ Cvtsl(dst, FTMP);
4032 } else {
4033 __ Cvtdl(dst, FTMP);
4034 }
4035 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004036 __ Mtc1(src, FTMP);
4037 if (result_type == Primitive::kPrimFloat) {
4038 __ Cvtsw(dst, FTMP);
4039 } else {
4040 __ Cvtdw(dst, FTMP);
4041 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004042 }
4043 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4044 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004045 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4046 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4047 Mips64Label truncate;
4048 Mips64Label done;
4049
4050 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4051 // value when the input is either a NaN or is outside of the range of the output type
4052 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4053 // the same result.
4054 //
4055 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4056 // value of the output type if the input is outside of the range after the truncation or
4057 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4058 // results. This matches the desired float/double-to-int/long conversion exactly.
4059 //
4060 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4061 //
4062 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4063 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4064 // even though it must be NAN2008=1 on R6.
4065 //
4066 // The code takes care of the different behaviors by first comparing the input to the
4067 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4068 // If the input is greater than or equal to the minimum, it procedes to the truncate
4069 // instruction, which will handle such an input the same way irrespective of NAN2008.
4070 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4071 // in order to return either zero or the minimum value.
4072 //
4073 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4074 // truncate instruction for MIPS64R6.
4075 if (input_type == Primitive::kPrimFloat) {
4076 uint32_t min_val = (result_type == Primitive::kPrimLong)
4077 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4078 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4079 __ LoadConst32(TMP, min_val);
4080 __ Mtc1(TMP, FTMP);
4081 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004082 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004083 uint64_t min_val = (result_type == Primitive::kPrimLong)
4084 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4085 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4086 __ LoadConst64(TMP, min_val);
4087 __ Dmtc1(TMP, FTMP);
4088 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004089 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004090
4091 __ Bc1nez(FTMP, &truncate);
4092
4093 if (input_type == Primitive::kPrimFloat) {
4094 __ CmpEqS(FTMP, src, src);
4095 } else {
4096 __ CmpEqD(FTMP, src, src);
4097 }
4098 if (result_type == Primitive::kPrimLong) {
4099 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4100 } else {
4101 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4102 }
4103 __ Mfc1(TMP, FTMP);
4104 __ And(dst, dst, TMP);
4105
4106 __ Bc(&done);
4107
4108 __ Bind(&truncate);
4109
4110 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004111 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004112 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004113 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004114 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004115 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004116 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004117 } else {
4118 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004119 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004120 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004121 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004122 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004123 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004124 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004125
4126 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004127 } else if (Primitive::IsFloatingPointType(result_type) &&
4128 Primitive::IsFloatingPointType(input_type)) {
4129 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4130 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4131 if (result_type == Primitive::kPrimFloat) {
4132 __ Cvtsd(dst, src);
4133 } else {
4134 __ Cvtds(dst, src);
4135 }
4136 } else {
4137 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4138 << " to " << result_type;
4139 }
4140}
4141
4142void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4143 HandleShift(ushr);
4144}
4145
4146void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4147 HandleShift(ushr);
4148}
4149
4150void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4151 HandleBinaryOp(instruction);
4152}
4153
4154void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4155 HandleBinaryOp(instruction);
4156}
4157
4158void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4159 // Nothing to do, this should be removed during prepare for register allocator.
4160 LOG(FATAL) << "Unreachable";
4161}
4162
4163void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4164 // Nothing to do, this should be removed during prepare for register allocator.
4165 LOG(FATAL) << "Unreachable";
4166}
4167
4168void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004169 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004170}
4171
4172void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004173 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004174}
4175
4176void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004177 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004178}
4179
4180void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004181 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004182}
4183
4184void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004185 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004186}
4187
4188void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004189 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004190}
4191
4192void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004193 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004194}
4195
4196void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004197 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004198}
4199
4200void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004201 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004202}
4203
4204void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004205 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004206}
4207
4208void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004209 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004210}
4211
4212void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004213 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004214}
4215
Aart Bike9f37602015-10-09 11:15:55 -07004216void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004217 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004218}
4219
4220void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004221 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004222}
4223
4224void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004225 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004226}
4227
4228void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004229 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004230}
4231
4232void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004233 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004234}
4235
4236void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004237 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004238}
4239
4240void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004241 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004242}
4243
4244void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004245 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004246}
4247
Mark Mendellfe57faa2015-09-18 09:26:15 -04004248// Simple implementation of packed switch - generate cascaded compare/jumps.
4249void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4250 LocationSummary* locations =
4251 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4252 locations->SetInAt(0, Location::RequiresRegister());
4253}
4254
4255void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4256 int32_t lower_bound = switch_instr->GetStartValue();
4257 int32_t num_entries = switch_instr->GetNumEntries();
4258 LocationSummary* locations = switch_instr->GetLocations();
4259 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4260 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4261
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004262 // Create a set of compare/jumps.
4263 GpuRegister temp_reg = TMP;
4264 if (IsInt<16>(-lower_bound)) {
4265 __ Addiu(temp_reg, value_reg, -lower_bound);
4266 } else {
4267 __ LoadConst32(AT, -lower_bound);
4268 __ Addu(temp_reg, value_reg, AT);
4269 }
4270 // Jump to default if index is negative
4271 // Note: We don't check the case that index is positive while value < lower_bound, because in
4272 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4273 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4274
Mark Mendellfe57faa2015-09-18 09:26:15 -04004275 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004276 // Jump to successors[0] if value == lower_bound.
4277 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4278 int32_t last_index = 0;
4279 for (; num_entries - last_index > 2; last_index += 2) {
4280 __ Addiu(temp_reg, temp_reg, -2);
4281 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4282 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4283 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4284 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4285 }
4286 if (num_entries - last_index == 2) {
4287 // The last missing case_value.
4288 __ Addiu(temp_reg, temp_reg, -1);
4289 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004290 }
4291
4292 // And the default for any other value.
4293 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004294 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004295 }
4296}
4297
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004298void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4299 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4300}
4301
4302void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4303 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4304}
4305
Alexey Frunze4dda3372015-06-01 18:31:49 -07004306} // namespace mips64
4307} // namespace art
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004308