blob: 3e1563c66b770869363ed9db90e04fda98b2a645 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070021#include "entrypoints/quick/quick_entrypoints.h"
22#include "entrypoints/quick/quick_entrypoints_enum.h"
23#include "gc/accounting/card_table.h"
24#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070025#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070026#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "offsets.h"
29#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070030#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "utils/stack_checks.h"
33
34namespace art {
35namespace mips64 {
36
37static constexpr int kCurrentMethodStackOffset = 0;
38static constexpr GpuRegister kMethodRegisterArgument = A0;
39
40// We need extra temporary/scratch registers (in addition to AT) in some cases.
Alexey Frunze4dda3372015-06-01 18:31:49 -070041static constexpr FpuRegister FTMP = F8;
42
Alexey Frunze4dda3372015-06-01 18:31:49 -070043Location Mips64ReturnLocation(Primitive::Type return_type) {
44 switch (return_type) {
45 case Primitive::kPrimBoolean:
46 case Primitive::kPrimByte:
47 case Primitive::kPrimChar:
48 case Primitive::kPrimShort:
49 case Primitive::kPrimInt:
50 case Primitive::kPrimNot:
51 case Primitive::kPrimLong:
52 return Location::RegisterLocation(V0);
53
54 case Primitive::kPrimFloat:
55 case Primitive::kPrimDouble:
56 return Location::FpuRegisterLocation(F0);
57
58 case Primitive::kPrimVoid:
59 return Location();
60 }
61 UNREACHABLE();
62}
63
64Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
65 return Mips64ReturnLocation(type);
66}
67
68Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
69 return Location::RegisterLocation(kMethodRegisterArgument);
70}
71
72Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
73 Location next_location;
74 if (type == Primitive::kPrimVoid) {
75 LOG(FATAL) << "Unexpected parameter type " << type;
76 }
77
78 if (Primitive::IsFloatingPointType(type) &&
79 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
80 next_location = Location::FpuRegisterLocation(
81 calling_convention.GetFpuRegisterAt(float_index_++));
82 gp_index_++;
83 } else if (!Primitive::IsFloatingPointType(type) &&
84 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
85 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
86 float_index_++;
87 } else {
88 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
89 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
90 : Location::StackSlot(stack_offset);
91 }
92
93 // Space on the stack is reserved for all arguments.
94 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
95
96 // TODO: review
97
98 // TODO: shouldn't we use a whole machine word per argument on the stack?
99 // Implicit 4-byte method pointer (and such) will cause misalignment.
100
101 return next_location;
102}
103
104Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
105 return Mips64ReturnLocation(type);
106}
107
108#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
Lazar Trsicd9672662015-09-03 17:33:01 +0200109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700110
111class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
112 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100113 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114
115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100116 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
118 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000119 if (instruction_->CanThrowIntoCatchBlock()) {
120 // Live registers will be restored in the catch block if caught.
121 SaveLiveRegisters(codegen, instruction_->GetLocations());
122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700123 // We're moving two locations to locations that could overlap, so we need a parallel
124 // move resolver.
125 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
128 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100129 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700130 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
131 Primitive::kPrimInt);
132 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
133 instruction_,
134 instruction_->GetDexPc(),
135 this);
136 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
137 }
138
Alexandre Rames8158f282015-08-07 10:26:17 +0100139 bool IsFatal() const OVERRIDE { return true; }
140
Roland Levillain46648892015-06-19 16:07:18 +0100141 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
142
Alexey Frunze4dda3372015-06-01 18:31:49 -0700143 private:
144 HBoundsCheck* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145
146 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
147};
148
149class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
150 public:
151 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : instruction_(instruction) {}
152
153 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
154 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
155 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000156 if (instruction_->CanThrowIntoCatchBlock()) {
157 // Live registers will be restored in the catch block if caught.
158 SaveLiveRegisters(codegen, instruction_->GetLocations());
159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
161 instruction_,
162 instruction_->GetDexPc(),
163 this);
164 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
165 }
166
Alexandre Rames8158f282015-08-07 10:26:17 +0100167 bool IsFatal() const OVERRIDE { return true; }
168
Roland Levillain46648892015-06-19 16:07:18 +0100169 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
170
Alexey Frunze4dda3372015-06-01 18:31:49 -0700171 private:
172 HDivZeroCheck* const instruction_;
173 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
174};
175
176class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
177 public:
178 LoadClassSlowPathMIPS64(HLoadClass* cls,
179 HInstruction* at,
180 uint32_t dex_pc,
181 bool do_clinit)
182 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
185
186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
187 LocationSummary* locations = at_->GetLocations();
188 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
189
190 __ Bind(GetEntryLabel());
191 SaveLiveRegisters(codegen, locations);
192
193 InvokeRuntimeCallingConvention calling_convention;
194 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
195 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
196 : QUICK_ENTRY_POINT(pInitializeType);
197 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
198 if (do_clinit_) {
199 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
200 } else {
201 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
202 }
203
204 // Move the class to the desired location.
205 Location out = locations->Out();
206 if (out.IsValid()) {
207 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
208 Primitive::Type type = at_->GetType();
209 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
210 }
211
212 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700213 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700214 }
215
Roland Levillain46648892015-06-19 16:07:18 +0100216 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
217
Alexey Frunze4dda3372015-06-01 18:31:49 -0700218 private:
219 // The class this slow path will load.
220 HLoadClass* const cls_;
221
222 // The instruction where this slow path is happening.
223 // (Might be the load class or an initialization check).
224 HInstruction* const at_;
225
226 // The dex PC of `at_`.
227 const uint32_t dex_pc_;
228
229 // Whether to initialize the class.
230 const bool do_clinit_;
231
232 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
233};
234
235class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
236 public:
237 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : instruction_(instruction) {}
238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
240 LocationSummary* locations = instruction_->GetLocations();
241 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
242 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
243
244 __ Bind(GetEntryLabel());
245 SaveLiveRegisters(codegen, locations);
246
247 InvokeRuntimeCallingConvention calling_convention;
248 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
249 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
250 instruction_,
251 instruction_->GetDexPc(),
252 this);
253 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
254 Primitive::Type type = instruction_->GetType();
255 mips64_codegen->MoveLocation(locations->Out(),
256 calling_convention.GetReturnLocation(type),
257 type);
258
259 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700260 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 }
262
Roland Levillain46648892015-06-19 16:07:18 +0100263 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
264
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 private:
266 HLoadString* const instruction_;
267
268 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
269};
270
271class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
272 public:
273 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : instruction_(instr) {}
274
275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
276 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
277 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000278 if (instruction_->CanThrowIntoCatchBlock()) {
279 // Live registers will be restored in the catch block if caught.
280 SaveLiveRegisters(codegen, instruction_->GetLocations());
281 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
283 instruction_,
284 instruction_->GetDexPc(),
285 this);
286 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
287 }
288
Alexandre Rames8158f282015-08-07 10:26:17 +0100289 bool IsFatal() const OVERRIDE { return true; }
290
Roland Levillain46648892015-06-19 16:07:18 +0100291 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
292
Alexey Frunze4dda3372015-06-01 18:31:49 -0700293 private:
294 HNullCheck* const instruction_;
295
296 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
297};
298
299class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
300 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100301 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700302 : instruction_(instruction), successor_(successor) {}
303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
307 SaveLiveRegisters(codegen, instruction_->GetLocations());
308 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
309 instruction_,
310 instruction_->GetDexPc(),
311 this);
312 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
313 RestoreLiveRegisters(codegen, instruction_->GetLocations());
314 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700317 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700318 }
319 }
320
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700321 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322 DCHECK(successor_ == nullptr);
323 return &return_label_;
324 }
325
Roland Levillain46648892015-06-19 16:07:18 +0100326 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
327
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328 private:
329 HSuspendCheck* const instruction_;
330 // If not null, the block to branch to after the suspend check.
331 HBasicBlock* const successor_;
332
333 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700334 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
337};
338
339class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
340 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700347 DCHECK(instruction_->IsCheckCast()
348 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
349 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
350
351 __ Bind(GetEntryLabel());
352 SaveLiveRegisters(codegen, locations);
353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100357 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
359 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100360 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
363
364 if (instruction_->IsInstanceOf()) {
365 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
366 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100367 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700368 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000369 CheckEntrypointTypes<
370 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700371 Primitive::Type ret_type = instruction_->GetType();
372 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
373 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700374 } else {
375 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100376 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
378 }
379
380 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700381 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 }
383
Roland Levillain46648892015-06-19 16:07:18 +0100384 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
385
Alexey Frunze4dda3372015-06-01 18:31:49 -0700386 private:
387 HInstruction* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388
389 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
390};
391
392class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
393 public:
Aart Bik42249c32016-01-07 15:33:50 -0800394 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395 : instruction_(instruction) {}
396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800398 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 __ Bind(GetEntryLabel());
400 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800401 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
402 instruction_,
403 instruction_->GetDexPc(),
404 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000405 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 }
407
Roland Levillain46648892015-06-19 16:07:18 +0100408 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
409
Alexey Frunze4dda3372015-06-01 18:31:49 -0700410 private:
Aart Bik42249c32016-01-07 15:33:50 -0800411 HDeoptimize* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700412 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
413};
414
415CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
416 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100417 const CompilerOptions& compiler_options,
418 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700419 : CodeGenerator(graph,
420 kNumberOfGpuRegisters,
421 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000422 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700423 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
424 arraysize(kCoreCalleeSaves)),
425 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
426 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100427 compiler_options,
428 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100429 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700430 location_builder_(graph, this),
431 instruction_visitor_(graph, this),
432 move_resolver_(graph->GetArena(), this),
433 isa_features_(isa_features) {
434 // Save RA (containing the return address) to mimic Quick.
435 AddAllocatedRegister(Location::RegisterLocation(RA));
436}
437
438#undef __
439#define __ down_cast<Mips64Assembler*>(GetAssembler())->
Lazar Trsicd9672662015-09-03 17:33:01 +0200440#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700441
442void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700443 // Ensure that we fix up branches.
444 __ FinalizeCode();
445
446 // Adjust native pc offsets in stack maps.
447 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
448 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
449 uint32_t new_position = __ GetAdjustedPosition(old_position);
450 DCHECK_GE(new_position, old_position);
451 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
452 }
453
454 // Adjust pc offsets for the disassembly information.
455 if (disasm_info_ != nullptr) {
456 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
457 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
458 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
459 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
460 it.second.start = __ GetAdjustedPosition(it.second.start);
461 it.second.end = __ GetAdjustedPosition(it.second.end);
462 }
463 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
464 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
465 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
466 }
467 }
468
Alexey Frunze4dda3372015-06-01 18:31:49 -0700469 CodeGenerator::Finalize(allocator);
470}
471
472Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
473 return codegen_->GetAssembler();
474}
475
476void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100477 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700478 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
479}
480
481void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100482 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700483 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
484}
485
486void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
487 // Pop reg
488 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200489 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700490}
491
492void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
493 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200494 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700495 __ Sd(GpuRegister(reg), SP, 0);
496}
497
498void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
499 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
500 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
501 // Allocate a scratch register other than TMP, if available.
502 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
503 // automatically unspilled when the scratch scope object is destroyed).
504 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
505 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200506 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700507 __ LoadFromOffset(load_type,
508 GpuRegister(ensure_scratch.GetRegister()),
509 SP,
510 index1 + stack_offset);
511 __ LoadFromOffset(load_type,
512 TMP,
513 SP,
514 index2 + stack_offset);
515 __ StoreToOffset(store_type,
516 GpuRegister(ensure_scratch.GetRegister()),
517 SP,
518 index2 + stack_offset);
519 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
520}
521
522static dwarf::Reg DWARFReg(GpuRegister reg) {
523 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
524}
525
David Srbeckyba702002016-02-01 18:15:29 +0000526static dwarf::Reg DWARFReg(FpuRegister reg) {
527 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
528}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700529
530void CodeGeneratorMIPS64::GenerateFrameEntry() {
531 __ Bind(&frame_entry_label_);
532
533 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
534
535 if (do_overflow_check) {
536 __ LoadFromOffset(kLoadWord,
537 ZERO,
538 SP,
539 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
540 RecordPcInfo(nullptr, 0);
541 }
542
543 // TODO: anything related to T9/GP/GOT/PIC/.so's?
544
545 if (HasEmptyFrame()) {
546 return;
547 }
548
549 // Make sure the frame size isn't unreasonably large. Per the various APIs
550 // it looks like it should always be less than 2GB in size, which allows
551 // us using 32-bit signed offsets from the stack pointer.
552 if (GetFrameSize() > 0x7FFFFFFF)
553 LOG(FATAL) << "Stack frame larger than 2GB";
554
555 // Spill callee-saved registers.
556 // Note that their cumulative size is small and they can be indexed using
557 // 16-bit offsets.
558
559 // TODO: increment/decrement SP in one step instead of two or remove this comment.
560
561 uint32_t ofs = FrameEntrySpillSize();
562 __ IncreaseFrameSize(ofs);
563
564 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
565 GpuRegister reg = kCoreCalleeSaves[i];
566 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200567 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700568 __ Sd(reg, SP, ofs);
569 __ cfi().RelOffset(DWARFReg(reg), ofs);
570 }
571 }
572
573 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
574 FpuRegister reg = kFpuCalleeSaves[i];
575 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200576 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700577 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000578 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700579 }
580 }
581
582 // Allocate the rest of the frame and store the current method pointer
583 // at its end.
584
585 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
586
587 static_assert(IsInt<16>(kCurrentMethodStackOffset),
588 "kCurrentMethodStackOffset must fit into int16_t");
589 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
590}
591
592void CodeGeneratorMIPS64::GenerateFrameExit() {
593 __ cfi().RememberState();
594
595 // TODO: anything related to T9/GP/GOT/PIC/.so's?
596
597 if (!HasEmptyFrame()) {
598 // Deallocate the rest of the frame.
599
600 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
601
602 // Restore callee-saved registers.
603 // Note that their cumulative size is small and they can be indexed using
604 // 16-bit offsets.
605
606 // TODO: increment/decrement SP in one step instead of two or remove this comment.
607
608 uint32_t ofs = 0;
609
610 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
611 FpuRegister reg = kFpuCalleeSaves[i];
612 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
613 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200614 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000615 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700616 }
617 }
618
619 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
620 GpuRegister reg = kCoreCalleeSaves[i];
621 if (allocated_registers_.ContainsCoreRegister(reg)) {
622 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200623 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700624 __ cfi().Restore(DWARFReg(reg));
625 }
626 }
627
628 DCHECK_EQ(ofs, FrameEntrySpillSize());
629 __ DecreaseFrameSize(ofs);
630 }
631
632 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700633 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700634
635 __ cfi().RestoreState();
636 __ cfi().DefCFAOffset(GetFrameSize());
637}
638
639void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
640 __ Bind(GetLabelOf(block));
641}
642
643void CodeGeneratorMIPS64::MoveLocation(Location destination,
644 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100645 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700646 if (source.Equals(destination)) {
647 return;
648 }
649
650 // A valid move can always be inferred from the destination and source
651 // locations. When moving from and to a register, the argument type can be
652 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100653 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700654 DCHECK_EQ(unspecified_type, false);
655
656 if (destination.IsRegister() || destination.IsFpuRegister()) {
657 if (unspecified_type) {
658 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
659 if (source.IsStackSlot() ||
660 (src_cst != nullptr && (src_cst->IsIntConstant()
661 || src_cst->IsFloatConstant()
662 || src_cst->IsNullConstant()))) {
663 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100664 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700665 } else {
666 // If the source is a double stack slot or a 64bit constant, a 64bit
667 // type is appropriate. Else the source is a register, and since the
668 // type has not been specified, we chose a 64bit type to force a 64bit
669 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100670 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700671 }
672 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100673 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
674 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700675 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
676 // Move to GPR/FPR from stack
677 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100678 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700679 __ LoadFpuFromOffset(load_type,
680 destination.AsFpuRegister<FpuRegister>(),
681 SP,
682 source.GetStackIndex());
683 } else {
684 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
685 __ LoadFromOffset(load_type,
686 destination.AsRegister<GpuRegister>(),
687 SP,
688 source.GetStackIndex());
689 }
690 } else if (source.IsConstant()) {
691 // Move to GPR/FPR from constant
692 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100693 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700694 gpr = destination.AsRegister<GpuRegister>();
695 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100696 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700697 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
698 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
699 gpr = ZERO;
700 } else {
701 __ LoadConst32(gpr, value);
702 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700703 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700704 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
705 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
706 gpr = ZERO;
707 } else {
708 __ LoadConst64(gpr, value);
709 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700710 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700712 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100713 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700714 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
715 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100716 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700717 if (destination.IsRegister()) {
718 // Move to GPR from GPR
719 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
720 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100721 DCHECK(destination.IsFpuRegister());
722 if (Primitive::Is64BitType(dst_type)) {
723 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
724 } else {
725 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
726 }
727 }
728 } else if (source.IsFpuRegister()) {
729 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100731 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700732 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
733 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100734 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700735 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
736 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100737 } else {
738 DCHECK(destination.IsRegister());
739 if (Primitive::Is64BitType(dst_type)) {
740 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
741 } else {
742 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
743 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700744 }
745 }
746 } else { // The destination is not a register. It must be a stack slot.
747 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
748 if (source.IsRegister() || source.IsFpuRegister()) {
749 if (unspecified_type) {
750 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100751 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100753 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700754 }
755 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100756 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
757 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700758 // Move to stack from GPR/FPR
759 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
760 if (source.IsRegister()) {
761 __ StoreToOffset(store_type,
762 source.AsRegister<GpuRegister>(),
763 SP,
764 destination.GetStackIndex());
765 } else {
766 __ StoreFpuToOffset(store_type,
767 source.AsFpuRegister<FpuRegister>(),
768 SP,
769 destination.GetStackIndex());
770 }
771 } else if (source.IsConstant()) {
772 // Move to stack from constant
773 HConstant* src_cst = source.GetConstant();
774 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700775 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700776 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700777 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
778 if (value != 0) {
779 gpr = TMP;
780 __ LoadConst32(gpr, value);
781 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700782 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700783 DCHECK(destination.IsDoubleStackSlot());
784 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
785 if (value != 0) {
786 gpr = TMP;
787 __ LoadConst64(gpr, value);
788 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700789 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700790 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700791 } else {
792 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
793 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
794 // Move to stack from stack
795 if (destination.IsStackSlot()) {
796 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
797 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
798 } else {
799 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
800 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
801 }
802 }
803 }
804}
805
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700806void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700807 DCHECK(!loc1.IsConstant());
808 DCHECK(!loc2.IsConstant());
809
810 if (loc1.Equals(loc2)) {
811 return;
812 }
813
814 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
815 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
816 bool is_fp_reg1 = loc1.IsFpuRegister();
817 bool is_fp_reg2 = loc2.IsFpuRegister();
818
819 if (loc2.IsRegister() && loc1.IsRegister()) {
820 // Swap 2 GPRs
821 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
822 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
823 __ Move(TMP, r2);
824 __ Move(r2, r1);
825 __ Move(r1, TMP);
826 } else if (is_fp_reg2 && is_fp_reg1) {
827 // Swap 2 FPRs
828 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
829 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700830 if (type == Primitive::kPrimFloat) {
831 __ MovS(FTMP, r1);
832 __ MovS(r1, r2);
833 __ MovS(r2, FTMP);
834 } else {
835 DCHECK_EQ(type, Primitive::kPrimDouble);
836 __ MovD(FTMP, r1);
837 __ MovD(r1, r2);
838 __ MovD(r2, FTMP);
839 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700840 } else if (is_slot1 != is_slot2) {
841 // Swap GPR/FPR and stack slot
842 Location reg_loc = is_slot1 ? loc2 : loc1;
843 Location mem_loc = is_slot1 ? loc1 : loc2;
844 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
845 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
846 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
847 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
848 if (reg_loc.IsFpuRegister()) {
849 __ StoreFpuToOffset(store_type,
850 reg_loc.AsFpuRegister<FpuRegister>(),
851 SP,
852 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700853 if (mem_loc.IsStackSlot()) {
854 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
855 } else {
856 DCHECK(mem_loc.IsDoubleStackSlot());
857 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
858 }
859 } else {
860 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
861 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
862 }
863 } else if (is_slot1 && is_slot2) {
864 move_resolver_.Exchange(loc1.GetStackIndex(),
865 loc2.GetStackIndex(),
866 loc1.IsDoubleStackSlot());
867 } else {
868 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
869 }
870}
871
872void CodeGeneratorMIPS64::Move(HInstruction* instruction,
873 Location location,
874 HInstruction* move_for) {
875 LocationSummary* locations = instruction->GetLocations();
876 Primitive::Type type = instruction->GetType();
877 DCHECK_NE(type, Primitive::kPrimVoid);
878
879 if (instruction->IsCurrentMethod()) {
880 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset), type);
881 } else if (locations != nullptr && locations->Out().Equals(location)) {
882 return;
883 } else if (instruction->IsIntConstant()
884 || instruction->IsLongConstant()
885 || instruction->IsNullConstant()) {
886 if (location.IsRegister()) {
887 // Move to GPR from constant
888 GpuRegister dst = location.AsRegister<GpuRegister>();
889 if (instruction->IsNullConstant() || instruction->IsIntConstant()) {
890 __ LoadConst32(dst, GetInt32ValueOf(instruction->AsConstant()));
891 } else {
892 __ LoadConst64(dst, instruction->AsLongConstant()->GetValue());
893 }
894 } else {
895 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
896 // Move to stack from constant
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700897 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700898 if (location.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700899 int32_t value = GetInt32ValueOf(instruction->AsConstant());
900 if (value != 0) {
901 gpr = TMP;
902 __ LoadConst32(gpr, value);
903 }
904 __ StoreToOffset(kStoreWord, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700905 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700906 DCHECK(location.IsDoubleStackSlot());
907 int64_t value = instruction->AsLongConstant()->GetValue();
908 if (value != 0) {
909 gpr = TMP;
910 __ LoadConst64(gpr, value);
911 }
912 __ StoreToOffset(kStoreDoubleword, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700913 }
914 }
915 } else if (instruction->IsTemporary()) {
916 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
917 MoveLocation(location, temp_location, type);
918 } else if (instruction->IsLoadLocal()) {
919 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
920 if (Primitive::Is64BitType(type)) {
921 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
922 } else {
923 MoveLocation(location, Location::StackSlot(stack_slot), type);
924 }
925 } else {
926 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
927 MoveLocation(location, locations->Out(), type);
928 }
929}
930
Calin Juravle175dc732015-08-25 15:42:32 +0100931void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
932 DCHECK(location.IsRegister());
933 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
934}
935
Calin Juravlee460d1d2015-09-29 04:52:17 +0100936void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
937 if (location.IsRegister()) {
938 locations->AddTemp(location);
939 } else {
940 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
941 }
942}
943
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
945 Primitive::Type type = load->GetType();
946
947 switch (type) {
948 case Primitive::kPrimNot:
949 case Primitive::kPrimInt:
950 case Primitive::kPrimFloat:
951 return Location::StackSlot(GetStackSlot(load->GetLocal()));
952
953 case Primitive::kPrimLong:
954 case Primitive::kPrimDouble:
955 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
956
957 case Primitive::kPrimBoolean:
958 case Primitive::kPrimByte:
959 case Primitive::kPrimChar:
960 case Primitive::kPrimShort:
961 case Primitive::kPrimVoid:
962 LOG(FATAL) << "Unexpected type " << type;
963 }
964
965 LOG(FATAL) << "Unreachable";
966 return Location::NoLocation();
967}
968
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100969void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
970 GpuRegister value,
971 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700972 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700973 GpuRegister card = AT;
974 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100975 if (value_can_be_null) {
976 __ Beqzc(value, &done);
977 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700978 __ LoadFromOffset(kLoadDoubleword,
979 card,
980 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200981 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700982 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
983 __ Daddu(temp, card, temp);
984 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100985 if (value_can_be_null) {
986 __ Bind(&done);
987 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700988}
989
David Brazdil58282f42016-01-14 12:45:10 +0000990void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700991 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
992 blocked_core_registers_[ZERO] = true;
993 blocked_core_registers_[K0] = true;
994 blocked_core_registers_[K1] = true;
995 blocked_core_registers_[GP] = true;
996 blocked_core_registers_[SP] = true;
997 blocked_core_registers_[RA] = true;
998
Lazar Trsicd9672662015-09-03 17:33:01 +0200999 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1000 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001001 blocked_core_registers_[AT] = true;
1002 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001003 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001004 blocked_fpu_registers_[FTMP] = true;
1005
1006 // Reserve suspend and thread registers.
1007 blocked_core_registers_[S0] = true;
1008 blocked_core_registers_[TR] = true;
1009
1010 // Reserve T9 for function calls
1011 blocked_core_registers_[T9] = true;
1012
1013 // TODO: review; anything else?
1014
David Brazdil58282f42016-01-14 12:45:10 +00001015 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001016 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1017 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1018 }
1019
1020 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1021 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1022 }
1023}
1024
Alexey Frunze4dda3372015-06-01 18:31:49 -07001025size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1026 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001027 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001028}
1029
1030size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1031 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001032 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001033}
1034
1035size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1036 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001037 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001038}
1039
1040size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1041 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001042 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001043}
1044
1045void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001046 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001047}
1048
1049void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001050 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001051}
1052
Calin Juravle175dc732015-08-25 15:42:32 +01001053void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1054 HInstruction* instruction,
1055 uint32_t dex_pc,
1056 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001057 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001058 instruction,
1059 dex_pc,
1060 slow_path);
1061}
1062
Alexey Frunze4dda3372015-06-01 18:31:49 -07001063void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
1064 HInstruction* instruction,
1065 uint32_t dex_pc,
1066 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001067 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001068 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1069 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1070 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001071 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001072 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001073}
1074
1075void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1076 GpuRegister class_reg) {
1077 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1078 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1079 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1080 // TODO: barrier needed?
1081 __ Bind(slow_path->GetExitLabel());
1082}
1083
1084void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1085 __ Sync(0); // only stype 0 is supported
1086}
1087
1088void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1089 HBasicBlock* successor) {
1090 SuspendCheckSlowPathMIPS64* slow_path =
1091 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1092 codegen_->AddSlowPath(slow_path);
1093
1094 __ LoadFromOffset(kLoadUnsignedHalfword,
1095 TMP,
1096 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001097 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098 if (successor == nullptr) {
1099 __ Bnezc(TMP, slow_path->GetEntryLabel());
1100 __ Bind(slow_path->GetReturnLabel());
1101 } else {
1102 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001103 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001104 // slow_path will return to GetLabelOf(successor).
1105 }
1106}
1107
1108InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1109 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001110 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001111 assembler_(codegen->GetAssembler()),
1112 codegen_(codegen) {}
1113
1114void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1115 DCHECK_EQ(instruction->InputCount(), 2U);
1116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1117 Primitive::Type type = instruction->GetResultType();
1118 switch (type) {
1119 case Primitive::kPrimInt:
1120 case Primitive::kPrimLong: {
1121 locations->SetInAt(0, Location::RequiresRegister());
1122 HInstruction* right = instruction->InputAt(1);
1123 bool can_use_imm = false;
1124 if (right->IsConstant()) {
1125 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1126 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1127 can_use_imm = IsUint<16>(imm);
1128 } else if (instruction->IsAdd()) {
1129 can_use_imm = IsInt<16>(imm);
1130 } else {
1131 DCHECK(instruction->IsSub());
1132 can_use_imm = IsInt<16>(-imm);
1133 }
1134 }
1135 if (can_use_imm)
1136 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1137 else
1138 locations->SetInAt(1, Location::RequiresRegister());
1139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1140 }
1141 break;
1142
1143 case Primitive::kPrimFloat:
1144 case Primitive::kPrimDouble:
1145 locations->SetInAt(0, Location::RequiresFpuRegister());
1146 locations->SetInAt(1, Location::RequiresFpuRegister());
1147 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1148 break;
1149
1150 default:
1151 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1152 }
1153}
1154
1155void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1156 Primitive::Type type = instruction->GetType();
1157 LocationSummary* locations = instruction->GetLocations();
1158
1159 switch (type) {
1160 case Primitive::kPrimInt:
1161 case Primitive::kPrimLong: {
1162 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1163 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1164 Location rhs_location = locations->InAt(1);
1165
1166 GpuRegister rhs_reg = ZERO;
1167 int64_t rhs_imm = 0;
1168 bool use_imm = rhs_location.IsConstant();
1169 if (use_imm) {
1170 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1171 } else {
1172 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1173 }
1174
1175 if (instruction->IsAnd()) {
1176 if (use_imm)
1177 __ Andi(dst, lhs, rhs_imm);
1178 else
1179 __ And(dst, lhs, rhs_reg);
1180 } else if (instruction->IsOr()) {
1181 if (use_imm)
1182 __ Ori(dst, lhs, rhs_imm);
1183 else
1184 __ Or(dst, lhs, rhs_reg);
1185 } else if (instruction->IsXor()) {
1186 if (use_imm)
1187 __ Xori(dst, lhs, rhs_imm);
1188 else
1189 __ Xor(dst, lhs, rhs_reg);
1190 } else if (instruction->IsAdd()) {
1191 if (type == Primitive::kPrimInt) {
1192 if (use_imm)
1193 __ Addiu(dst, lhs, rhs_imm);
1194 else
1195 __ Addu(dst, lhs, rhs_reg);
1196 } else {
1197 if (use_imm)
1198 __ Daddiu(dst, lhs, rhs_imm);
1199 else
1200 __ Daddu(dst, lhs, rhs_reg);
1201 }
1202 } else {
1203 DCHECK(instruction->IsSub());
1204 if (type == Primitive::kPrimInt) {
1205 if (use_imm)
1206 __ Addiu(dst, lhs, -rhs_imm);
1207 else
1208 __ Subu(dst, lhs, rhs_reg);
1209 } else {
1210 if (use_imm)
1211 __ Daddiu(dst, lhs, -rhs_imm);
1212 else
1213 __ Dsubu(dst, lhs, rhs_reg);
1214 }
1215 }
1216 break;
1217 }
1218 case Primitive::kPrimFloat:
1219 case Primitive::kPrimDouble: {
1220 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1221 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1222 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1223 if (instruction->IsAdd()) {
1224 if (type == Primitive::kPrimFloat)
1225 __ AddS(dst, lhs, rhs);
1226 else
1227 __ AddD(dst, lhs, rhs);
1228 } else if (instruction->IsSub()) {
1229 if (type == Primitive::kPrimFloat)
1230 __ SubS(dst, lhs, rhs);
1231 else
1232 __ SubD(dst, lhs, rhs);
1233 } else {
1234 LOG(FATAL) << "Unexpected floating-point binary operation";
1235 }
1236 break;
1237 }
1238 default:
1239 LOG(FATAL) << "Unexpected binary operation type " << type;
1240 }
1241}
1242
1243void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001244 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001245
1246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1247 Primitive::Type type = instr->GetResultType();
1248 switch (type) {
1249 case Primitive::kPrimInt:
1250 case Primitive::kPrimLong: {
1251 locations->SetInAt(0, Location::RequiresRegister());
1252 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001254 break;
1255 }
1256 default:
1257 LOG(FATAL) << "Unexpected shift type " << type;
1258 }
1259}
1260
1261void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001262 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001263 LocationSummary* locations = instr->GetLocations();
1264 Primitive::Type type = instr->GetType();
1265
1266 switch (type) {
1267 case Primitive::kPrimInt:
1268 case Primitive::kPrimLong: {
1269 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1270 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1271 Location rhs_location = locations->InAt(1);
1272
1273 GpuRegister rhs_reg = ZERO;
1274 int64_t rhs_imm = 0;
1275 bool use_imm = rhs_location.IsConstant();
1276 if (use_imm) {
1277 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1278 } else {
1279 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1280 }
1281
1282 if (use_imm) {
1283 uint32_t shift_value = (type == Primitive::kPrimInt)
1284 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1285 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1286
Alexey Frunze92d90602015-12-18 18:16:36 -08001287 if (shift_value == 0) {
1288 if (dst != lhs) {
1289 __ Move(dst, lhs);
1290 }
1291 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001292 if (instr->IsShl()) {
1293 __ Sll(dst, lhs, shift_value);
1294 } else if (instr->IsShr()) {
1295 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001296 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001298 } else {
1299 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001300 }
1301 } else {
1302 if (shift_value < 32) {
1303 if (instr->IsShl()) {
1304 __ Dsll(dst, lhs, shift_value);
1305 } else if (instr->IsShr()) {
1306 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001307 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001308 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001309 } else {
1310 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001311 }
1312 } else {
1313 shift_value -= 32;
1314 if (instr->IsShl()) {
1315 __ Dsll32(dst, lhs, shift_value);
1316 } else if (instr->IsShr()) {
1317 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001318 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001319 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001320 } else {
1321 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001322 }
1323 }
1324 }
1325 } else {
1326 if (type == Primitive::kPrimInt) {
1327 if (instr->IsShl()) {
1328 __ Sllv(dst, lhs, rhs_reg);
1329 } else if (instr->IsShr()) {
1330 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001331 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001332 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001333 } else {
1334 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001335 }
1336 } else {
1337 if (instr->IsShl()) {
1338 __ Dsllv(dst, lhs, rhs_reg);
1339 } else if (instr->IsShr()) {
1340 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001341 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001342 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001343 } else {
1344 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001345 }
1346 }
1347 }
1348 break;
1349 }
1350 default:
1351 LOG(FATAL) << "Unexpected shift operation type " << type;
1352 }
1353}
1354
1355void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1356 HandleBinaryOp(instruction);
1357}
1358
1359void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1360 HandleBinaryOp(instruction);
1361}
1362
1363void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1364 HandleBinaryOp(instruction);
1365}
1366
1367void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1368 HandleBinaryOp(instruction);
1369}
1370
1371void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1372 LocationSummary* locations =
1373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1374 locations->SetInAt(0, Location::RequiresRegister());
1375 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1376 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1377 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1378 } else {
1379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1380 }
1381}
1382
1383void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1384 LocationSummary* locations = instruction->GetLocations();
1385 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1386 Location index = locations->InAt(1);
1387 Primitive::Type type = instruction->GetType();
1388
1389 switch (type) {
1390 case Primitive::kPrimBoolean: {
1391 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1392 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1393 if (index.IsConstant()) {
1394 size_t offset =
1395 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1396 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1397 } else {
1398 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1399 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1400 }
1401 break;
1402 }
1403
1404 case Primitive::kPrimByte: {
1405 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1406 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1407 if (index.IsConstant()) {
1408 size_t offset =
1409 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1410 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1411 } else {
1412 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1413 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1414 }
1415 break;
1416 }
1417
1418 case Primitive::kPrimShort: {
1419 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1420 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1421 if (index.IsConstant()) {
1422 size_t offset =
1423 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1424 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1425 } else {
1426 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1427 __ Daddu(TMP, obj, TMP);
1428 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1429 }
1430 break;
1431 }
1432
1433 case Primitive::kPrimChar: {
1434 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1435 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1436 if (index.IsConstant()) {
1437 size_t offset =
1438 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1439 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1440 } else {
1441 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1442 __ Daddu(TMP, obj, TMP);
1443 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1444 }
1445 break;
1446 }
1447
1448 case Primitive::kPrimInt:
1449 case Primitive::kPrimNot: {
1450 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1451 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1452 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1453 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1454 if (index.IsConstant()) {
1455 size_t offset =
1456 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1457 __ LoadFromOffset(load_type, out, obj, offset);
1458 } else {
1459 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1460 __ Daddu(TMP, obj, TMP);
1461 __ LoadFromOffset(load_type, out, TMP, data_offset);
1462 }
1463 break;
1464 }
1465
1466 case Primitive::kPrimLong: {
1467 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1468 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1469 if (index.IsConstant()) {
1470 size_t offset =
1471 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1472 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1473 } else {
1474 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1475 __ Daddu(TMP, obj, TMP);
1476 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1477 }
1478 break;
1479 }
1480
1481 case Primitive::kPrimFloat: {
1482 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1483 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1484 if (index.IsConstant()) {
1485 size_t offset =
1486 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1487 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1488 } else {
1489 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1490 __ Daddu(TMP, obj, TMP);
1491 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1492 }
1493 break;
1494 }
1495
1496 case Primitive::kPrimDouble: {
1497 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1498 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1499 if (index.IsConstant()) {
1500 size_t offset =
1501 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1502 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1503 } else {
1504 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1505 __ Daddu(TMP, obj, TMP);
1506 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1507 }
1508 break;
1509 }
1510
1511 case Primitive::kPrimVoid:
1512 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1513 UNREACHABLE();
1514 }
1515 codegen_->MaybeRecordImplicitNullCheck(instruction);
1516}
1517
1518void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1520 locations->SetInAt(0, Location::RequiresRegister());
1521 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1522}
1523
1524void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1525 LocationSummary* locations = instruction->GetLocations();
1526 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1527 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1528 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1529 __ LoadFromOffset(kLoadWord, out, obj, offset);
1530 codegen_->MaybeRecordImplicitNullCheck(instruction);
1531}
1532
1533void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001534 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1536 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001537 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1538 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001539 InvokeRuntimeCallingConvention calling_convention;
1540 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1541 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1542 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1543 } else {
1544 locations->SetInAt(0, Location::RequiresRegister());
1545 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1546 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1547 locations->SetInAt(2, Location::RequiresFpuRegister());
1548 } else {
1549 locations->SetInAt(2, Location::RequiresRegister());
1550 }
1551 }
1552}
1553
1554void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1555 LocationSummary* locations = instruction->GetLocations();
1556 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1557 Location index = locations->InAt(1);
1558 Primitive::Type value_type = instruction->GetComponentType();
1559 bool needs_runtime_call = locations->WillCall();
1560 bool needs_write_barrier =
1561 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1562
1563 switch (value_type) {
1564 case Primitive::kPrimBoolean:
1565 case Primitive::kPrimByte: {
1566 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1567 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1568 if (index.IsConstant()) {
1569 size_t offset =
1570 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1571 __ StoreToOffset(kStoreByte, value, obj, offset);
1572 } else {
1573 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1574 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1575 }
1576 break;
1577 }
1578
1579 case Primitive::kPrimShort:
1580 case Primitive::kPrimChar: {
1581 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1582 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1583 if (index.IsConstant()) {
1584 size_t offset =
1585 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1586 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1587 } else {
1588 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1589 __ Daddu(TMP, obj, TMP);
1590 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1591 }
1592 break;
1593 }
1594
1595 case Primitive::kPrimInt:
1596 case Primitive::kPrimNot: {
1597 if (!needs_runtime_call) {
1598 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1599 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1600 if (index.IsConstant()) {
1601 size_t offset =
1602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1603 __ StoreToOffset(kStoreWord, value, obj, offset);
1604 } else {
1605 DCHECK(index.IsRegister()) << index;
1606 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1607 __ Daddu(TMP, obj, TMP);
1608 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1609 }
1610 codegen_->MaybeRecordImplicitNullCheck(instruction);
1611 if (needs_write_barrier) {
1612 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001613 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001614 }
1615 } else {
1616 DCHECK_EQ(value_type, Primitive::kPrimNot);
1617 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1618 instruction,
1619 instruction->GetDexPc(),
1620 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001621 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001622 }
1623 break;
1624 }
1625
1626 case Primitive::kPrimLong: {
1627 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1628 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1629 if (index.IsConstant()) {
1630 size_t offset =
1631 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1632 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1633 } else {
1634 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1635 __ Daddu(TMP, obj, TMP);
1636 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1637 }
1638 break;
1639 }
1640
1641 case Primitive::kPrimFloat: {
1642 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1643 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1644 DCHECK(locations->InAt(2).IsFpuRegister());
1645 if (index.IsConstant()) {
1646 size_t offset =
1647 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1648 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1649 } else {
1650 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1651 __ Daddu(TMP, obj, TMP);
1652 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1653 }
1654 break;
1655 }
1656
1657 case Primitive::kPrimDouble: {
1658 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1659 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1660 DCHECK(locations->InAt(2).IsFpuRegister());
1661 if (index.IsConstant()) {
1662 size_t offset =
1663 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1664 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1665 } else {
1666 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1667 __ Daddu(TMP, obj, TMP);
1668 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1669 }
1670 break;
1671 }
1672
1673 case Primitive::kPrimVoid:
1674 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1675 UNREACHABLE();
1676 }
1677
1678 // Ints and objects are handled in the switch.
1679 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1680 codegen_->MaybeRecordImplicitNullCheck(instruction);
1681 }
1682}
1683
1684void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001685 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1686 ? LocationSummary::kCallOnSlowPath
1687 : LocationSummary::kNoCall;
1688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001689 locations->SetInAt(0, Location::RequiresRegister());
1690 locations->SetInAt(1, Location::RequiresRegister());
1691 if (instruction->HasUses()) {
1692 locations->SetOut(Location::SameAsFirstInput());
1693 }
1694}
1695
1696void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1697 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001698 BoundsCheckSlowPathMIPS64* slow_path =
1699 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001700 codegen_->AddSlowPath(slow_path);
1701
1702 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1703 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1704
1705 // length is limited by the maximum positive signed 32-bit integer.
1706 // Unsigned comparison of length and index checks for index < 0
1707 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001708 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001709}
1710
1711void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1712 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1713 instruction,
1714 LocationSummary::kCallOnSlowPath);
1715 locations->SetInAt(0, Location::RequiresRegister());
1716 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001717 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001718 locations->AddTemp(Location::RequiresRegister());
1719}
1720
1721void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1722 LocationSummary* locations = instruction->GetLocations();
1723 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1724 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1725 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1726
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001727 SlowPathCodeMIPS64* slow_path =
1728 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001729 codegen_->AddSlowPath(slow_path);
1730
1731 // TODO: avoid this check if we know obj is not null.
1732 __ Beqzc(obj, slow_path->GetExitLabel());
1733 // Compare the class of `obj` with `cls`.
1734 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1735 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1736 __ Bind(slow_path->GetExitLabel());
1737}
1738
1739void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1740 LocationSummary* locations =
1741 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1742 locations->SetInAt(0, Location::RequiresRegister());
1743 if (check->HasUses()) {
1744 locations->SetOut(Location::SameAsFirstInput());
1745 }
1746}
1747
1748void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1749 // We assume the class is not null.
1750 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1751 check->GetLoadClass(),
1752 check,
1753 check->GetDexPc(),
1754 true);
1755 codegen_->AddSlowPath(slow_path);
1756 GenerateClassInitializationCheck(slow_path,
1757 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1758}
1759
1760void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1761 Primitive::Type in_type = compare->InputAt(0)->GetType();
1762
Alexey Frunze299a9392015-12-08 16:08:02 -08001763 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001764
1765 switch (in_type) {
1766 case Primitive::kPrimLong:
1767 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001768 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001769 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1770 break;
1771
1772 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001773 case Primitive::kPrimDouble:
1774 locations->SetInAt(0, Location::RequiresFpuRegister());
1775 locations->SetInAt(1, Location::RequiresFpuRegister());
1776 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001777 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001778
1779 default:
1780 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1781 }
1782}
1783
1784void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1785 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001786 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001787 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08001788 bool gt_bias = instruction->IsGtBias();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001789
1790 // 0 if: left == right
1791 // 1 if: left > right
1792 // -1 if: left < right
1793 switch (in_type) {
1794 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001795 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001796 Location rhs_location = locations->InAt(1);
1797 bool use_imm = rhs_location.IsConstant();
1798 GpuRegister rhs = ZERO;
1799 if (use_imm) {
1800 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1801 if (value != 0) {
1802 rhs = AT;
1803 __ LoadConst64(rhs, value);
1804 }
1805 } else {
1806 rhs = rhs_location.AsRegister<GpuRegister>();
1807 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001808 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001809 __ Slt(res, rhs, lhs);
1810 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001811 break;
1812 }
1813
Alexey Frunze299a9392015-12-08 16:08:02 -08001814 case Primitive::kPrimFloat: {
1815 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1816 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1817 Mips64Label done;
1818 __ CmpEqS(FTMP, lhs, rhs);
1819 __ LoadConst32(res, 0);
1820 __ Bc1nez(FTMP, &done);
1821 if (gt_bias) {
1822 __ CmpLtS(FTMP, lhs, rhs);
1823 __ LoadConst32(res, -1);
1824 __ Bc1nez(FTMP, &done);
1825 __ LoadConst32(res, 1);
1826 } else {
1827 __ CmpLtS(FTMP, rhs, lhs);
1828 __ LoadConst32(res, 1);
1829 __ Bc1nez(FTMP, &done);
1830 __ LoadConst32(res, -1);
1831 }
1832 __ Bind(&done);
1833 break;
1834 }
1835
Alexey Frunze4dda3372015-06-01 18:31:49 -07001836 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001837 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1838 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1839 Mips64Label done;
1840 __ CmpEqD(FTMP, lhs, rhs);
1841 __ LoadConst32(res, 0);
1842 __ Bc1nez(FTMP, &done);
1843 if (gt_bias) {
1844 __ CmpLtD(FTMP, lhs, rhs);
1845 __ LoadConst32(res, -1);
1846 __ Bc1nez(FTMP, &done);
1847 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001848 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001849 __ CmpLtD(FTMP, rhs, lhs);
1850 __ LoadConst32(res, 1);
1851 __ Bc1nez(FTMP, &done);
1852 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001853 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001854 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001855 break;
1856 }
1857
1858 default:
1859 LOG(FATAL) << "Unimplemented compare type " << in_type;
1860 }
1861}
1862
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001863void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001864 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001865 switch (instruction->InputAt(0)->GetType()) {
1866 default:
1867 case Primitive::kPrimLong:
1868 locations->SetInAt(0, Location::RequiresRegister());
1869 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1870 break;
1871
1872 case Primitive::kPrimFloat:
1873 case Primitive::kPrimDouble:
1874 locations->SetInAt(0, Location::RequiresFpuRegister());
1875 locations->SetInAt(1, Location::RequiresFpuRegister());
1876 break;
1877 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001878 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001879 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1880 }
1881}
1882
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001883void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001884 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001885 return;
1886 }
1887
Alexey Frunze299a9392015-12-08 16:08:02 -08001888 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001889 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001890 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001891 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001892
Alexey Frunze299a9392015-12-08 16:08:02 -08001893 switch (type) {
1894 default:
1895 // Integer case.
1896 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1897 return;
1898 case Primitive::kPrimLong:
1899 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1900 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001901
Alexey Frunze299a9392015-12-08 16:08:02 -08001902 case Primitive::kPrimFloat:
1903 case Primitive::kPrimDouble:
1904 // TODO: don't use branches.
1905 GenerateFpCompareAndBranch(instruction->GetCondition(),
1906 instruction->IsGtBias(),
1907 type,
1908 locations,
1909 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001910 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001911 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001912
1913 // Convert the branches into the result.
1914 Mips64Label done;
1915
1916 // False case: result = 0.
1917 __ LoadConst32(dst, 0);
1918 __ Bc(&done);
1919
1920 // True case: result = 1.
1921 __ Bind(&true_label);
1922 __ LoadConst32(dst, 1);
1923 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001924}
1925
Alexey Frunzec857c742015-09-23 15:12:39 -07001926void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1927 DCHECK(instruction->IsDiv() || instruction->IsRem());
1928 Primitive::Type type = instruction->GetResultType();
1929
1930 LocationSummary* locations = instruction->GetLocations();
1931 Location second = locations->InAt(1);
1932 DCHECK(second.IsConstant());
1933
1934 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1935 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1936 int64_t imm = Int64FromConstant(second.GetConstant());
1937 DCHECK(imm == 1 || imm == -1);
1938
1939 if (instruction->IsRem()) {
1940 __ Move(out, ZERO);
1941 } else {
1942 if (imm == -1) {
1943 if (type == Primitive::kPrimInt) {
1944 __ Subu(out, ZERO, dividend);
1945 } else {
1946 DCHECK_EQ(type, Primitive::kPrimLong);
1947 __ Dsubu(out, ZERO, dividend);
1948 }
1949 } else if (out != dividend) {
1950 __ Move(out, dividend);
1951 }
1952 }
1953}
1954
1955void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1956 DCHECK(instruction->IsDiv() || instruction->IsRem());
1957 Primitive::Type type = instruction->GetResultType();
1958
1959 LocationSummary* locations = instruction->GetLocations();
1960 Location second = locations->InAt(1);
1961 DCHECK(second.IsConstant());
1962
1963 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1964 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1965 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001966 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001967 int ctz_imm = CTZ(abs_imm);
1968
1969 if (instruction->IsDiv()) {
1970 if (type == Primitive::kPrimInt) {
1971 if (ctz_imm == 1) {
1972 // Fast path for division by +/-2, which is very common.
1973 __ Srl(TMP, dividend, 31);
1974 } else {
1975 __ Sra(TMP, dividend, 31);
1976 __ Srl(TMP, TMP, 32 - ctz_imm);
1977 }
1978 __ Addu(out, dividend, TMP);
1979 __ Sra(out, out, ctz_imm);
1980 if (imm < 0) {
1981 __ Subu(out, ZERO, out);
1982 }
1983 } else {
1984 DCHECK_EQ(type, Primitive::kPrimLong);
1985 if (ctz_imm == 1) {
1986 // Fast path for division by +/-2, which is very common.
1987 __ Dsrl32(TMP, dividend, 31);
1988 } else {
1989 __ Dsra32(TMP, dividend, 31);
1990 if (ctz_imm > 32) {
1991 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1992 } else {
1993 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1994 }
1995 }
1996 __ Daddu(out, dividend, TMP);
1997 if (ctz_imm < 32) {
1998 __ Dsra(out, out, ctz_imm);
1999 } else {
2000 __ Dsra32(out, out, ctz_imm - 32);
2001 }
2002 if (imm < 0) {
2003 __ Dsubu(out, ZERO, out);
2004 }
2005 }
2006 } else {
2007 if (type == Primitive::kPrimInt) {
2008 if (ctz_imm == 1) {
2009 // Fast path for modulo +/-2, which is very common.
2010 __ Sra(TMP, dividend, 31);
2011 __ Subu(out, dividend, TMP);
2012 __ Andi(out, out, 1);
2013 __ Addu(out, out, TMP);
2014 } else {
2015 __ Sra(TMP, dividend, 31);
2016 __ Srl(TMP, TMP, 32 - ctz_imm);
2017 __ Addu(out, dividend, TMP);
2018 if (IsUint<16>(abs_imm - 1)) {
2019 __ Andi(out, out, abs_imm - 1);
2020 } else {
2021 __ Sll(out, out, 32 - ctz_imm);
2022 __ Srl(out, out, 32 - ctz_imm);
2023 }
2024 __ Subu(out, out, TMP);
2025 }
2026 } else {
2027 DCHECK_EQ(type, Primitive::kPrimLong);
2028 if (ctz_imm == 1) {
2029 // Fast path for modulo +/-2, which is very common.
2030 __ Dsra32(TMP, dividend, 31);
2031 __ Dsubu(out, dividend, TMP);
2032 __ Andi(out, out, 1);
2033 __ Daddu(out, out, TMP);
2034 } else {
2035 __ Dsra32(TMP, dividend, 31);
2036 if (ctz_imm > 32) {
2037 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2038 } else {
2039 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2040 }
2041 __ Daddu(out, dividend, TMP);
2042 if (IsUint<16>(abs_imm - 1)) {
2043 __ Andi(out, out, abs_imm - 1);
2044 } else {
2045 if (ctz_imm > 32) {
2046 __ Dsll(out, out, 64 - ctz_imm);
2047 __ Dsrl(out, out, 64 - ctz_imm);
2048 } else {
2049 __ Dsll32(out, out, 32 - ctz_imm);
2050 __ Dsrl32(out, out, 32 - ctz_imm);
2051 }
2052 }
2053 __ Dsubu(out, out, TMP);
2054 }
2055 }
2056 }
2057}
2058
2059void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2060 DCHECK(instruction->IsDiv() || instruction->IsRem());
2061
2062 LocationSummary* locations = instruction->GetLocations();
2063 Location second = locations->InAt(1);
2064 DCHECK(second.IsConstant());
2065
2066 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2067 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2068 int64_t imm = Int64FromConstant(second.GetConstant());
2069
2070 Primitive::Type type = instruction->GetResultType();
2071 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2072
2073 int64_t magic;
2074 int shift;
2075 CalculateMagicAndShiftForDivRem(imm,
2076 (type == Primitive::kPrimLong),
2077 &magic,
2078 &shift);
2079
2080 if (type == Primitive::kPrimInt) {
2081 __ LoadConst32(TMP, magic);
2082 __ MuhR6(TMP, dividend, TMP);
2083
2084 if (imm > 0 && magic < 0) {
2085 __ Addu(TMP, TMP, dividend);
2086 } else if (imm < 0 && magic > 0) {
2087 __ Subu(TMP, TMP, dividend);
2088 }
2089
2090 if (shift != 0) {
2091 __ Sra(TMP, TMP, shift);
2092 }
2093
2094 if (instruction->IsDiv()) {
2095 __ Sra(out, TMP, 31);
2096 __ Subu(out, TMP, out);
2097 } else {
2098 __ Sra(AT, TMP, 31);
2099 __ Subu(AT, TMP, AT);
2100 __ LoadConst32(TMP, imm);
2101 __ MulR6(TMP, AT, TMP);
2102 __ Subu(out, dividend, TMP);
2103 }
2104 } else {
2105 __ LoadConst64(TMP, magic);
2106 __ Dmuh(TMP, dividend, TMP);
2107
2108 if (imm > 0 && magic < 0) {
2109 __ Daddu(TMP, TMP, dividend);
2110 } else if (imm < 0 && magic > 0) {
2111 __ Dsubu(TMP, TMP, dividend);
2112 }
2113
2114 if (shift >= 32) {
2115 __ Dsra32(TMP, TMP, shift - 32);
2116 } else if (shift > 0) {
2117 __ Dsra(TMP, TMP, shift);
2118 }
2119
2120 if (instruction->IsDiv()) {
2121 __ Dsra32(out, TMP, 31);
2122 __ Dsubu(out, TMP, out);
2123 } else {
2124 __ Dsra32(AT, TMP, 31);
2125 __ Dsubu(AT, TMP, AT);
2126 __ LoadConst64(TMP, imm);
2127 __ Dmul(TMP, AT, TMP);
2128 __ Dsubu(out, dividend, TMP);
2129 }
2130 }
2131}
2132
2133void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2134 DCHECK(instruction->IsDiv() || instruction->IsRem());
2135 Primitive::Type type = instruction->GetResultType();
2136 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2137
2138 LocationSummary* locations = instruction->GetLocations();
2139 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2140 Location second = locations->InAt(1);
2141
2142 if (second.IsConstant()) {
2143 int64_t imm = Int64FromConstant(second.GetConstant());
2144 if (imm == 0) {
2145 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2146 } else if (imm == 1 || imm == -1) {
2147 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002148 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002149 DivRemByPowerOfTwo(instruction);
2150 } else {
2151 DCHECK(imm <= -2 || imm >= 2);
2152 GenerateDivRemWithAnyConstant(instruction);
2153 }
2154 } else {
2155 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2156 GpuRegister divisor = second.AsRegister<GpuRegister>();
2157 if (instruction->IsDiv()) {
2158 if (type == Primitive::kPrimInt)
2159 __ DivR6(out, dividend, divisor);
2160 else
2161 __ Ddiv(out, dividend, divisor);
2162 } else {
2163 if (type == Primitive::kPrimInt)
2164 __ ModR6(out, dividend, divisor);
2165 else
2166 __ Dmod(out, dividend, divisor);
2167 }
2168 }
2169}
2170
Alexey Frunze4dda3372015-06-01 18:31:49 -07002171void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2172 LocationSummary* locations =
2173 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2174 switch (div->GetResultType()) {
2175 case Primitive::kPrimInt:
2176 case Primitive::kPrimLong:
2177 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002178 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2180 break;
2181
2182 case Primitive::kPrimFloat:
2183 case Primitive::kPrimDouble:
2184 locations->SetInAt(0, Location::RequiresFpuRegister());
2185 locations->SetInAt(1, Location::RequiresFpuRegister());
2186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2187 break;
2188
2189 default:
2190 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2191 }
2192}
2193
2194void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2195 Primitive::Type type = instruction->GetType();
2196 LocationSummary* locations = instruction->GetLocations();
2197
2198 switch (type) {
2199 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002200 case Primitive::kPrimLong:
2201 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002202 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002203 case Primitive::kPrimFloat:
2204 case Primitive::kPrimDouble: {
2205 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2206 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2207 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2208 if (type == Primitive::kPrimFloat)
2209 __ DivS(dst, lhs, rhs);
2210 else
2211 __ DivD(dst, lhs, rhs);
2212 break;
2213 }
2214 default:
2215 LOG(FATAL) << "Unexpected div type " << type;
2216 }
2217}
2218
2219void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002220 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2221 ? LocationSummary::kCallOnSlowPath
2222 : LocationSummary::kNoCall;
2223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002224 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2225 if (instruction->HasUses()) {
2226 locations->SetOut(Location::SameAsFirstInput());
2227 }
2228}
2229
2230void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2231 SlowPathCodeMIPS64* slow_path =
2232 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2233 codegen_->AddSlowPath(slow_path);
2234 Location value = instruction->GetLocations()->InAt(0);
2235
2236 Primitive::Type type = instruction->GetType();
2237
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002238 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002239 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002240 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002241 }
2242
2243 if (value.IsConstant()) {
2244 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2245 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002246 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002247 } else {
2248 // A division by a non-null constant is valid. We don't need to perform
2249 // any check, so simply fall through.
2250 }
2251 } else {
2252 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2253 }
2254}
2255
2256void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2257 LocationSummary* locations =
2258 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2259 locations->SetOut(Location::ConstantLocation(constant));
2260}
2261
2262void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2263 // Will be generated at use site.
2264}
2265
2266void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2267 exit->SetLocations(nullptr);
2268}
2269
2270void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2271}
2272
2273void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2274 LocationSummary* locations =
2275 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2276 locations->SetOut(Location::ConstantLocation(constant));
2277}
2278
2279void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2280 // Will be generated at use site.
2281}
2282
David Brazdilfc6a86a2015-06-26 10:33:45 +00002283void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002284 DCHECK(!successor->IsExitBlock());
2285 HBasicBlock* block = got->GetBlock();
2286 HInstruction* previous = got->GetPrevious();
2287 HLoopInformation* info = block->GetLoopInformation();
2288
2289 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2290 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2291 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2292 return;
2293 }
2294 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2295 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2296 }
2297 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002298 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002299 }
2300}
2301
David Brazdilfc6a86a2015-06-26 10:33:45 +00002302void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2303 got->SetLocations(nullptr);
2304}
2305
2306void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2307 HandleGoto(got, got->GetSuccessor());
2308}
2309
2310void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2311 try_boundary->SetLocations(nullptr);
2312}
2313
2314void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2315 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2316 if (!successor->IsExitBlock()) {
2317 HandleGoto(try_boundary, successor);
2318 }
2319}
2320
Alexey Frunze299a9392015-12-08 16:08:02 -08002321void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2322 bool is64bit,
2323 LocationSummary* locations) {
2324 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2325 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2326 Location rhs_location = locations->InAt(1);
2327 GpuRegister rhs_reg = ZERO;
2328 int64_t rhs_imm = 0;
2329 bool use_imm = rhs_location.IsConstant();
2330 if (use_imm) {
2331 if (is64bit) {
2332 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2333 } else {
2334 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2335 }
2336 } else {
2337 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2338 }
2339 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2340
2341 switch (cond) {
2342 case kCondEQ:
2343 case kCondNE:
2344 if (use_imm && IsUint<16>(rhs_imm)) {
2345 __ Xori(dst, lhs, rhs_imm);
2346 } else {
2347 if (use_imm) {
2348 rhs_reg = TMP;
2349 __ LoadConst64(rhs_reg, rhs_imm);
2350 }
2351 __ Xor(dst, lhs, rhs_reg);
2352 }
2353 if (cond == kCondEQ) {
2354 __ Sltiu(dst, dst, 1);
2355 } else {
2356 __ Sltu(dst, ZERO, dst);
2357 }
2358 break;
2359
2360 case kCondLT:
2361 case kCondGE:
2362 if (use_imm && IsInt<16>(rhs_imm)) {
2363 __ Slti(dst, lhs, rhs_imm);
2364 } else {
2365 if (use_imm) {
2366 rhs_reg = TMP;
2367 __ LoadConst64(rhs_reg, rhs_imm);
2368 }
2369 __ Slt(dst, lhs, rhs_reg);
2370 }
2371 if (cond == kCondGE) {
2372 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2373 // only the slt instruction but no sge.
2374 __ Xori(dst, dst, 1);
2375 }
2376 break;
2377
2378 case kCondLE:
2379 case kCondGT:
2380 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2381 // Simulate lhs <= rhs via lhs < rhs + 1.
2382 __ Slti(dst, lhs, rhs_imm_plus_one);
2383 if (cond == kCondGT) {
2384 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2385 // only the slti instruction but no sgti.
2386 __ Xori(dst, dst, 1);
2387 }
2388 } else {
2389 if (use_imm) {
2390 rhs_reg = TMP;
2391 __ LoadConst64(rhs_reg, rhs_imm);
2392 }
2393 __ Slt(dst, rhs_reg, lhs);
2394 if (cond == kCondLE) {
2395 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2396 // only the slt instruction but no sle.
2397 __ Xori(dst, dst, 1);
2398 }
2399 }
2400 break;
2401
2402 case kCondB:
2403 case kCondAE:
2404 if (use_imm && IsInt<16>(rhs_imm)) {
2405 // Sltiu sign-extends its 16-bit immediate operand before
2406 // the comparison and thus lets us compare directly with
2407 // unsigned values in the ranges [0, 0x7fff] and
2408 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2409 __ Sltiu(dst, lhs, rhs_imm);
2410 } else {
2411 if (use_imm) {
2412 rhs_reg = TMP;
2413 __ LoadConst64(rhs_reg, rhs_imm);
2414 }
2415 __ Sltu(dst, lhs, rhs_reg);
2416 }
2417 if (cond == kCondAE) {
2418 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2419 // only the sltu instruction but no sgeu.
2420 __ Xori(dst, dst, 1);
2421 }
2422 break;
2423
2424 case kCondBE:
2425 case kCondA:
2426 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2427 // Simulate lhs <= rhs via lhs < rhs + 1.
2428 // Note that this only works if rhs + 1 does not overflow
2429 // to 0, hence the check above.
2430 // Sltiu sign-extends its 16-bit immediate operand before
2431 // the comparison and thus lets us compare directly with
2432 // unsigned values in the ranges [0, 0x7fff] and
2433 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2434 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2435 if (cond == kCondA) {
2436 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2437 // only the sltiu instruction but no sgtiu.
2438 __ Xori(dst, dst, 1);
2439 }
2440 } else {
2441 if (use_imm) {
2442 rhs_reg = TMP;
2443 __ LoadConst64(rhs_reg, rhs_imm);
2444 }
2445 __ Sltu(dst, rhs_reg, lhs);
2446 if (cond == kCondBE) {
2447 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2448 // only the sltu instruction but no sleu.
2449 __ Xori(dst, dst, 1);
2450 }
2451 }
2452 break;
2453 }
2454}
2455
2456void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2457 bool is64bit,
2458 LocationSummary* locations,
2459 Mips64Label* label) {
2460 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2461 Location rhs_location = locations->InAt(1);
2462 GpuRegister rhs_reg = ZERO;
2463 int64_t rhs_imm = 0;
2464 bool use_imm = rhs_location.IsConstant();
2465 if (use_imm) {
2466 if (is64bit) {
2467 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2468 } else {
2469 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2470 }
2471 } else {
2472 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2473 }
2474
2475 if (use_imm && rhs_imm == 0) {
2476 switch (cond) {
2477 case kCondEQ:
2478 case kCondBE: // <= 0 if zero
2479 __ Beqzc(lhs, label);
2480 break;
2481 case kCondNE:
2482 case kCondA: // > 0 if non-zero
2483 __ Bnezc(lhs, label);
2484 break;
2485 case kCondLT:
2486 __ Bltzc(lhs, label);
2487 break;
2488 case kCondGE:
2489 __ Bgezc(lhs, label);
2490 break;
2491 case kCondLE:
2492 __ Blezc(lhs, label);
2493 break;
2494 case kCondGT:
2495 __ Bgtzc(lhs, label);
2496 break;
2497 case kCondB: // always false
2498 break;
2499 case kCondAE: // always true
2500 __ Bc(label);
2501 break;
2502 }
2503 } else {
2504 if (use_imm) {
2505 rhs_reg = TMP;
2506 __ LoadConst64(rhs_reg, rhs_imm);
2507 }
2508 switch (cond) {
2509 case kCondEQ:
2510 __ Beqc(lhs, rhs_reg, label);
2511 break;
2512 case kCondNE:
2513 __ Bnec(lhs, rhs_reg, label);
2514 break;
2515 case kCondLT:
2516 __ Bltc(lhs, rhs_reg, label);
2517 break;
2518 case kCondGE:
2519 __ Bgec(lhs, rhs_reg, label);
2520 break;
2521 case kCondLE:
2522 __ Bgec(rhs_reg, lhs, label);
2523 break;
2524 case kCondGT:
2525 __ Bltc(rhs_reg, lhs, label);
2526 break;
2527 case kCondB:
2528 __ Bltuc(lhs, rhs_reg, label);
2529 break;
2530 case kCondAE:
2531 __ Bgeuc(lhs, rhs_reg, label);
2532 break;
2533 case kCondBE:
2534 __ Bgeuc(rhs_reg, lhs, label);
2535 break;
2536 case kCondA:
2537 __ Bltuc(rhs_reg, lhs, label);
2538 break;
2539 }
2540 }
2541}
2542
2543void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2544 bool gt_bias,
2545 Primitive::Type type,
2546 LocationSummary* locations,
2547 Mips64Label* label) {
2548 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2549 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2550 if (type == Primitive::kPrimFloat) {
2551 switch (cond) {
2552 case kCondEQ:
2553 __ CmpEqS(FTMP, lhs, rhs);
2554 __ Bc1nez(FTMP, label);
2555 break;
2556 case kCondNE:
2557 __ CmpEqS(FTMP, lhs, rhs);
2558 __ Bc1eqz(FTMP, label);
2559 break;
2560 case kCondLT:
2561 if (gt_bias) {
2562 __ CmpLtS(FTMP, lhs, rhs);
2563 } else {
2564 __ CmpUltS(FTMP, lhs, rhs);
2565 }
2566 __ Bc1nez(FTMP, label);
2567 break;
2568 case kCondLE:
2569 if (gt_bias) {
2570 __ CmpLeS(FTMP, lhs, rhs);
2571 } else {
2572 __ CmpUleS(FTMP, lhs, rhs);
2573 }
2574 __ Bc1nez(FTMP, label);
2575 break;
2576 case kCondGT:
2577 if (gt_bias) {
2578 __ CmpUltS(FTMP, rhs, lhs);
2579 } else {
2580 __ CmpLtS(FTMP, rhs, lhs);
2581 }
2582 __ Bc1nez(FTMP, label);
2583 break;
2584 case kCondGE:
2585 if (gt_bias) {
2586 __ CmpUleS(FTMP, rhs, lhs);
2587 } else {
2588 __ CmpLeS(FTMP, rhs, lhs);
2589 }
2590 __ Bc1nez(FTMP, label);
2591 break;
2592 default:
2593 LOG(FATAL) << "Unexpected non-floating-point condition";
2594 }
2595 } else {
2596 DCHECK_EQ(type, Primitive::kPrimDouble);
2597 switch (cond) {
2598 case kCondEQ:
2599 __ CmpEqD(FTMP, lhs, rhs);
2600 __ Bc1nez(FTMP, label);
2601 break;
2602 case kCondNE:
2603 __ CmpEqD(FTMP, lhs, rhs);
2604 __ Bc1eqz(FTMP, label);
2605 break;
2606 case kCondLT:
2607 if (gt_bias) {
2608 __ CmpLtD(FTMP, lhs, rhs);
2609 } else {
2610 __ CmpUltD(FTMP, lhs, rhs);
2611 }
2612 __ Bc1nez(FTMP, label);
2613 break;
2614 case kCondLE:
2615 if (gt_bias) {
2616 __ CmpLeD(FTMP, lhs, rhs);
2617 } else {
2618 __ CmpUleD(FTMP, lhs, rhs);
2619 }
2620 __ Bc1nez(FTMP, label);
2621 break;
2622 case kCondGT:
2623 if (gt_bias) {
2624 __ CmpUltD(FTMP, rhs, lhs);
2625 } else {
2626 __ CmpLtD(FTMP, rhs, lhs);
2627 }
2628 __ Bc1nez(FTMP, label);
2629 break;
2630 case kCondGE:
2631 if (gt_bias) {
2632 __ CmpUleD(FTMP, rhs, lhs);
2633 } else {
2634 __ CmpLeD(FTMP, rhs, lhs);
2635 }
2636 __ Bc1nez(FTMP, label);
2637 break;
2638 default:
2639 LOG(FATAL) << "Unexpected non-floating-point condition";
2640 }
2641 }
2642}
2643
Alexey Frunze4dda3372015-06-01 18:31:49 -07002644void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002645 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002646 Mips64Label* true_target,
2647 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002648 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002649
David Brazdil0debae72015-11-12 18:37:00 +00002650 if (true_target == nullptr && false_target == nullptr) {
2651 // Nothing to do. The code always falls through.
2652 return;
2653 } else if (cond->IsIntConstant()) {
2654 // Constant condition, statically compared against 1.
2655 if (cond->AsIntConstant()->IsOne()) {
2656 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002657 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002658 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002659 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002660 DCHECK(cond->AsIntConstant()->IsZero());
2661 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002662 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002663 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002664 }
David Brazdil0debae72015-11-12 18:37:00 +00002665 return;
2666 }
2667
2668 // The following code generates these patterns:
2669 // (1) true_target == nullptr && false_target != nullptr
2670 // - opposite condition true => branch to false_target
2671 // (2) true_target != nullptr && false_target == nullptr
2672 // - condition true => branch to true_target
2673 // (3) true_target != nullptr && false_target != nullptr
2674 // - condition true => branch to true_target
2675 // - branch to false_target
2676 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002677 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002678 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002679 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002680 if (true_target == nullptr) {
2681 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2682 } else {
2683 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2684 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002685 } else {
2686 // The condition instruction has not been materialized, use its inputs as
2687 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002688 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002689 Primitive::Type type = condition->InputAt(0)->GetType();
2690 LocationSummary* locations = cond->GetLocations();
2691 IfCondition if_cond = condition->GetCondition();
2692 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002693
David Brazdil0debae72015-11-12 18:37:00 +00002694 if (true_target == nullptr) {
2695 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002696 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002697 }
2698
Alexey Frunze299a9392015-12-08 16:08:02 -08002699 switch (type) {
2700 default:
2701 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2702 break;
2703 case Primitive::kPrimLong:
2704 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2705 break;
2706 case Primitive::kPrimFloat:
2707 case Primitive::kPrimDouble:
2708 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2709 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002710 }
2711 }
David Brazdil0debae72015-11-12 18:37:00 +00002712
2713 // If neither branch falls through (case 3), the conditional branch to `true_target`
2714 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2715 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002716 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002717 }
2718}
2719
2720void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2721 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002722 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002723 locations->SetInAt(0, Location::RequiresRegister());
2724 }
2725}
2726
2727void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002728 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2729 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002730 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002731 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002732 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002733 nullptr : codegen_->GetLabelOf(false_successor);
2734 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002735}
2736
2737void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2738 LocationSummary* locations = new (GetGraph()->GetArena())
2739 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002740 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002741 locations->SetInAt(0, Location::RequiresRegister());
2742 }
2743}
2744
2745void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002746 SlowPathCodeMIPS64* slow_path =
2747 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002748 GenerateTestAndBranch(deoptimize,
2749 /* condition_input_index */ 0,
2750 slow_path->GetEntryLabel(),
2751 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002752}
2753
David Brazdil74eb1b22015-12-14 11:44:01 +00002754void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2755 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2756 if (Primitive::IsFloatingPointType(select->GetType())) {
2757 locations->SetInAt(0, Location::RequiresFpuRegister());
2758 locations->SetInAt(1, Location::RequiresFpuRegister());
2759 } else {
2760 locations->SetInAt(0, Location::RequiresRegister());
2761 locations->SetInAt(1, Location::RequiresRegister());
2762 }
2763 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2764 locations->SetInAt(2, Location::RequiresRegister());
2765 }
2766 locations->SetOut(Location::SameAsFirstInput());
2767}
2768
2769void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2770 LocationSummary* locations = select->GetLocations();
2771 Mips64Label false_target;
2772 GenerateTestAndBranch(select,
2773 /* condition_input_index */ 2,
2774 /* true_target */ nullptr,
2775 &false_target);
2776 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2777 __ Bind(&false_target);
2778}
2779
David Srbecky0cf44932015-12-09 14:09:59 +00002780void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2781 new (GetGraph()->GetArena()) LocationSummary(info);
2782}
2783
2784void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002785 if (codegen_->HasStackMapAtCurrentPc()) {
2786 // Ensure that we do not collide with the stack map of the previous instruction.
2787 __ Nop();
2788 }
David Srbecky0cf44932015-12-09 14:09:59 +00002789 codegen_->RecordPcInfo(info, info->GetDexPc());
2790}
2791
Alexey Frunze4dda3372015-06-01 18:31:49 -07002792void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2793 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2794 LocationSummary* locations =
2795 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2796 locations->SetInAt(0, Location::RequiresRegister());
2797 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2798 locations->SetOut(Location::RequiresFpuRegister());
2799 } else {
2800 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2801 }
2802}
2803
2804void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2805 const FieldInfo& field_info) {
2806 Primitive::Type type = field_info.GetFieldType();
2807 LocationSummary* locations = instruction->GetLocations();
2808 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2809 LoadOperandType load_type = kLoadUnsignedByte;
2810 switch (type) {
2811 case Primitive::kPrimBoolean:
2812 load_type = kLoadUnsignedByte;
2813 break;
2814 case Primitive::kPrimByte:
2815 load_type = kLoadSignedByte;
2816 break;
2817 case Primitive::kPrimShort:
2818 load_type = kLoadSignedHalfword;
2819 break;
2820 case Primitive::kPrimChar:
2821 load_type = kLoadUnsignedHalfword;
2822 break;
2823 case Primitive::kPrimInt:
2824 case Primitive::kPrimFloat:
2825 load_type = kLoadWord;
2826 break;
2827 case Primitive::kPrimLong:
2828 case Primitive::kPrimDouble:
2829 load_type = kLoadDoubleword;
2830 break;
2831 case Primitive::kPrimNot:
2832 load_type = kLoadUnsignedWord;
2833 break;
2834 case Primitive::kPrimVoid:
2835 LOG(FATAL) << "Unreachable type " << type;
2836 UNREACHABLE();
2837 }
2838 if (!Primitive::IsFloatingPointType(type)) {
2839 DCHECK(locations->Out().IsRegister());
2840 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2841 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2842 } else {
2843 DCHECK(locations->Out().IsFpuRegister());
2844 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2845 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2846 }
2847
2848 codegen_->MaybeRecordImplicitNullCheck(instruction);
2849 // TODO: memory barrier?
2850}
2851
2852void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2853 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2854 LocationSummary* locations =
2855 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2856 locations->SetInAt(0, Location::RequiresRegister());
2857 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2858 locations->SetInAt(1, Location::RequiresFpuRegister());
2859 } else {
2860 locations->SetInAt(1, Location::RequiresRegister());
2861 }
2862}
2863
2864void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002865 const FieldInfo& field_info,
2866 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002867 Primitive::Type type = field_info.GetFieldType();
2868 LocationSummary* locations = instruction->GetLocations();
2869 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2870 StoreOperandType store_type = kStoreByte;
2871 switch (type) {
2872 case Primitive::kPrimBoolean:
2873 case Primitive::kPrimByte:
2874 store_type = kStoreByte;
2875 break;
2876 case Primitive::kPrimShort:
2877 case Primitive::kPrimChar:
2878 store_type = kStoreHalfword;
2879 break;
2880 case Primitive::kPrimInt:
2881 case Primitive::kPrimFloat:
2882 case Primitive::kPrimNot:
2883 store_type = kStoreWord;
2884 break;
2885 case Primitive::kPrimLong:
2886 case Primitive::kPrimDouble:
2887 store_type = kStoreDoubleword;
2888 break;
2889 case Primitive::kPrimVoid:
2890 LOG(FATAL) << "Unreachable type " << type;
2891 UNREACHABLE();
2892 }
2893 if (!Primitive::IsFloatingPointType(type)) {
2894 DCHECK(locations->InAt(1).IsRegister());
2895 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2896 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2897 } else {
2898 DCHECK(locations->InAt(1).IsFpuRegister());
2899 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2900 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2901 }
2902
2903 codegen_->MaybeRecordImplicitNullCheck(instruction);
2904 // TODO: memory barriers?
2905 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2906 DCHECK(locations->InAt(1).IsRegister());
2907 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002908 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002909 }
2910}
2911
2912void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2913 HandleFieldGet(instruction, instruction->GetFieldInfo());
2914}
2915
2916void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2917 HandleFieldGet(instruction, instruction->GetFieldInfo());
2918}
2919
2920void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2921 HandleFieldSet(instruction, instruction->GetFieldInfo());
2922}
2923
2924void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002925 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002926}
2927
2928void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2929 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002930 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2932 locations->SetInAt(0, Location::RequiresRegister());
2933 locations->SetInAt(1, Location::RequiresRegister());
2934 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002935 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002936 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2937}
2938
2939void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2940 LocationSummary* locations = instruction->GetLocations();
2941 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2942 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2943 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2944
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002945 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002946
2947 // Return 0 if `obj` is null.
2948 // TODO: Avoid this check if we know `obj` is not null.
2949 __ Move(out, ZERO);
2950 __ Beqzc(obj, &done);
2951
2952 // Compare the class of `obj` with `cls`.
2953 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002954 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002955 // Classes must be equal for the instanceof to succeed.
2956 __ Xor(out, out, cls);
2957 __ Sltiu(out, out, 1);
2958 } else {
2959 // If the classes are not equal, we go into a slow path.
2960 DCHECK(locations->OnlyCallsOnSlowPath());
2961 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002962 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002963 codegen_->AddSlowPath(slow_path);
2964 __ Bnec(out, cls, slow_path->GetEntryLabel());
2965 __ LoadConst32(out, 1);
2966 __ Bind(slow_path->GetExitLabel());
2967 }
2968
2969 __ Bind(&done);
2970}
2971
2972void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2973 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2974 locations->SetOut(Location::ConstantLocation(constant));
2975}
2976
2977void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2978 // Will be generated at use site.
2979}
2980
2981void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2982 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2983 locations->SetOut(Location::ConstantLocation(constant));
2984}
2985
2986void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2987 // Will be generated at use site.
2988}
2989
Calin Juravle175dc732015-08-25 15:42:32 +01002990void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2991 // The trampoline uses the same calling convention as dex calling conventions,
2992 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2993 // the method_idx.
2994 HandleInvoke(invoke);
2995}
2996
2997void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2998 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2999}
3000
Alexey Frunze4dda3372015-06-01 18:31:49 -07003001void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3002 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3003 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3004}
3005
3006void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3007 HandleInvoke(invoke);
3008 // The register T0 is required to be used for the hidden argument in
3009 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3010 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3011}
3012
3013void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3014 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3015 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
3016 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3017 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
3018 Location receiver = invoke->GetLocations()->InAt(0);
3019 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003020 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003021
3022 // Set the hidden argument.
3023 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3024 invoke->GetDexMethodIndex());
3025
3026 // temp = object->GetClass();
3027 if (receiver.IsStackSlot()) {
3028 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3029 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3030 } else {
3031 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3032 }
3033 codegen_->MaybeRecordImplicitNullCheck(invoke);
3034 // temp = temp->GetImtEntryAt(method_offset);
3035 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3036 // T9 = temp->GetEntryPoint();
3037 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3038 // T9();
3039 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003040 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003041 DCHECK(!codegen_->IsLeafMethod());
3042 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3043}
3044
3045void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003046 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3047 if (intrinsic.TryDispatch(invoke)) {
3048 return;
3049 }
3050
Alexey Frunze4dda3372015-06-01 18:31:49 -07003051 HandleInvoke(invoke);
3052}
3053
3054void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003055 // Explicit clinit checks triggered by static invokes must have been pruned by
3056 // art::PrepareForRegisterAllocation.
3057 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003058
Chris Larsen3039e382015-08-26 07:54:08 -07003059 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3060 if (intrinsic.TryDispatch(invoke)) {
3061 return;
3062 }
3063
Alexey Frunze4dda3372015-06-01 18:31:49 -07003064 HandleInvoke(invoke);
3065
3066 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
3067 // clobbering somewhere else, reduce further register pressure by avoiding
3068 // allocation of a register for the current method pointer like on x86 baseline.
3069 // TODO: remove this once all the issues with register saving/restoring are
3070 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003071 if (invoke->HasCurrentMethodInput()) {
3072 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00003073 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003074 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003075 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003076 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003077 }
3078}
3079
Chris Larsen3039e382015-08-26 07:54:08 -07003080static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003081 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003082 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3083 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003084 return true;
3085 }
3086 return false;
3087}
3088
Vladimir Markodc151b22015-10-15 18:02:30 +01003089HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3090 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3091 MethodReference target_method ATTRIBUTE_UNUSED) {
3092 switch (desired_dispatch_info.method_load_kind) {
3093 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3094 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3095 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3096 return HInvokeStaticOrDirect::DispatchInfo {
3097 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3098 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3099 0u,
3100 0u
3101 };
3102 default:
3103 break;
3104 }
3105 switch (desired_dispatch_info.code_ptr_location) {
3106 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3107 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3108 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3109 return HInvokeStaticOrDirect::DispatchInfo {
3110 desired_dispatch_info.method_load_kind,
3111 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3112 desired_dispatch_info.method_load_data,
3113 0u
3114 };
3115 default:
3116 return desired_dispatch_info;
3117 }
3118}
3119
Alexey Frunze4dda3372015-06-01 18:31:49 -07003120void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3121 // All registers are assumed to be correctly set up per the calling convention.
3122
Vladimir Marko58155012015-08-19 12:49:41 +00003123 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3124 switch (invoke->GetMethodLoadKind()) {
3125 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3126 // temp = thread->string_init_entrypoint
3127 __ LoadFromOffset(kLoadDoubleword,
3128 temp.AsRegister<GpuRegister>(),
3129 TR,
3130 invoke->GetStringInitOffset());
3131 break;
3132 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003133 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003134 break;
3135 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3136 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3137 break;
3138 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003139 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003140 // TODO: Implement these types.
3141 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3142 LOG(FATAL) << "Unsupported";
3143 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003144 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003145 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003146 GpuRegister reg = temp.AsRegister<GpuRegister>();
3147 GpuRegister method_reg;
3148 if (current_method.IsRegister()) {
3149 method_reg = current_method.AsRegister<GpuRegister>();
3150 } else {
3151 // TODO: use the appropriate DCHECK() here if possible.
3152 // DCHECK(invoke->GetLocations()->Intrinsified());
3153 DCHECK(!current_method.IsValid());
3154 method_reg = reg;
3155 __ Ld(reg, SP, kCurrentMethodStackOffset);
3156 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003157
Vladimir Marko58155012015-08-19 12:49:41 +00003158 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003159 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003160 reg,
3161 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003162 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003163 // temp = temp[index_in_cache]
3164 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3165 __ LoadFromOffset(kLoadDoubleword,
3166 reg,
3167 reg,
3168 CodeGenerator::GetCachePointerOffset(index_in_cache));
3169 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003170 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003171 }
3172
Vladimir Marko58155012015-08-19 12:49:41 +00003173 switch (invoke->GetCodePtrLocation()) {
3174 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003175 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003176 break;
3177 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3178 // LR = invoke->GetDirectCodePtr();
3179 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3180 // LR()
3181 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003182 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003183 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003184 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003185 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3186 // TODO: Implement these types.
3187 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3188 LOG(FATAL) << "Unsupported";
3189 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003190 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3191 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3192 __ LoadFromOffset(kLoadDoubleword,
3193 T9,
3194 callee_method.AsRegister<GpuRegister>(),
3195 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003196 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003197 // T9()
3198 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003199 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003200 break;
3201 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003202 DCHECK(!IsLeafMethod());
3203}
3204
3205void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003206 // Explicit clinit checks triggered by static invokes must have been pruned by
3207 // art::PrepareForRegisterAllocation.
3208 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003209
3210 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3211 return;
3212 }
3213
3214 LocationSummary* locations = invoke->GetLocations();
3215 codegen_->GenerateStaticOrDirectCall(invoke,
3216 locations->HasTemps()
3217 ? locations->GetTemp(0)
3218 : Location::NoLocation());
3219 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3220}
3221
Alexey Frunze53afca12015-11-05 16:34:23 -08003222void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003223 // Use the calling convention instead of the location of the receiver, as
3224 // intrinsics may have put the receiver in a different register. In the intrinsics
3225 // slow path, the arguments have been moved to the right place, so here we are
3226 // guaranteed that the receiver is the first register of the calling convention.
3227 InvokeDexCallingConvention calling_convention;
3228 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3229
Alexey Frunze53afca12015-11-05 16:34:23 -08003230 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003231 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3232 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3233 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003234 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003235
3236 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003237 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003238 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003239 // temp = temp->GetMethodAt(method_offset);
3240 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3241 // T9 = temp->GetEntryPoint();
3242 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3243 // T9();
3244 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003245 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003246}
3247
3248void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3249 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3250 return;
3251 }
3252
3253 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003254 DCHECK(!codegen_->IsLeafMethod());
3255 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3256}
3257
3258void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003259 InvokeRuntimeCallingConvention calling_convention;
3260 CodeGenerator::CreateLoadClassLocationSummary(
3261 cls,
3262 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003263 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003264}
3265
3266void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3267 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003268 if (cls->NeedsAccessCheck()) {
3269 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3270 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3271 cls,
3272 cls->GetDexPc(),
3273 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003274 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003275 return;
3276 }
3277
3278 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3279 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3280 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003281 DCHECK(!cls->CanCallRuntime());
3282 DCHECK(!cls->MustGenerateClinitCheck());
3283 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3284 ArtMethod::DeclaringClassOffset().Int32Value());
3285 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003286 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3287 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003288 __ LoadFromOffset(
3289 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003290 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003291 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3292 DCHECK(cls->CanCallRuntime());
3293 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3294 cls,
3295 cls,
3296 cls->GetDexPc(),
3297 cls->MustGenerateClinitCheck());
3298 codegen_->AddSlowPath(slow_path);
3299 if (!cls->IsInDexCache()) {
3300 __ Beqzc(out, slow_path->GetEntryLabel());
3301 }
3302 if (cls->MustGenerateClinitCheck()) {
3303 GenerateClassInitializationCheck(slow_path, out);
3304 } else {
3305 __ Bind(slow_path->GetExitLabel());
3306 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003307 }
3308 }
3309}
3310
David Brazdilcb1c0552015-08-04 16:22:25 +01003311static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003312 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003313}
3314
Alexey Frunze4dda3372015-06-01 18:31:49 -07003315void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3316 LocationSummary* locations =
3317 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3318 locations->SetOut(Location::RequiresRegister());
3319}
3320
3321void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3322 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003323 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3324}
3325
3326void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3327 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3328}
3329
3330void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3331 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003332}
3333
3334void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3335 load->SetLocations(nullptr);
3336}
3337
3338void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3339 // Nothing to do, this is driven by the code generator.
3340}
3341
3342void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00003343 LocationSummary::CallKind call_kind = load->IsInDexCache()
3344 ? LocationSummary::kNoCall
3345 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003347 locations->SetInAt(0, Location::RequiresRegister());
3348 locations->SetOut(Location::RequiresRegister());
3349}
3350
3351void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003352 LocationSummary* locations = load->GetLocations();
3353 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3354 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3355 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3356 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003357 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003358 __ LoadFromOffset(
3359 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003360 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003361
3362 if (!load->IsInDexCache()) {
3363 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3364 codegen_->AddSlowPath(slow_path);
3365 __ Beqzc(out, slow_path->GetEntryLabel());
3366 __ Bind(slow_path->GetExitLabel());
3367 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003368}
3369
3370void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3371 local->SetLocations(nullptr);
3372}
3373
3374void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3375 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3376}
3377
3378void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3379 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3380 locations->SetOut(Location::ConstantLocation(constant));
3381}
3382
3383void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3384 // Will be generated at use site.
3385}
3386
3387void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3388 LocationSummary* locations =
3389 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3390 InvokeRuntimeCallingConvention calling_convention;
3391 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3392}
3393
3394void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3395 codegen_->InvokeRuntime(instruction->IsEnter()
3396 ? QUICK_ENTRY_POINT(pLockObject)
3397 : QUICK_ENTRY_POINT(pUnlockObject),
3398 instruction,
3399 instruction->GetDexPc(),
3400 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003401 if (instruction->IsEnter()) {
3402 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3403 } else {
3404 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3405 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003406}
3407
3408void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3409 LocationSummary* locations =
3410 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3411 switch (mul->GetResultType()) {
3412 case Primitive::kPrimInt:
3413 case Primitive::kPrimLong:
3414 locations->SetInAt(0, Location::RequiresRegister());
3415 locations->SetInAt(1, Location::RequiresRegister());
3416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3417 break;
3418
3419 case Primitive::kPrimFloat:
3420 case Primitive::kPrimDouble:
3421 locations->SetInAt(0, Location::RequiresFpuRegister());
3422 locations->SetInAt(1, Location::RequiresFpuRegister());
3423 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3424 break;
3425
3426 default:
3427 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3428 }
3429}
3430
3431void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3432 Primitive::Type type = instruction->GetType();
3433 LocationSummary* locations = instruction->GetLocations();
3434
3435 switch (type) {
3436 case Primitive::kPrimInt:
3437 case Primitive::kPrimLong: {
3438 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3439 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3440 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3441 if (type == Primitive::kPrimInt)
3442 __ MulR6(dst, lhs, rhs);
3443 else
3444 __ Dmul(dst, lhs, rhs);
3445 break;
3446 }
3447 case Primitive::kPrimFloat:
3448 case Primitive::kPrimDouble: {
3449 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3450 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3451 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3452 if (type == Primitive::kPrimFloat)
3453 __ MulS(dst, lhs, rhs);
3454 else
3455 __ MulD(dst, lhs, rhs);
3456 break;
3457 }
3458 default:
3459 LOG(FATAL) << "Unexpected mul type " << type;
3460 }
3461}
3462
3463void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3464 LocationSummary* locations =
3465 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3466 switch (neg->GetResultType()) {
3467 case Primitive::kPrimInt:
3468 case Primitive::kPrimLong:
3469 locations->SetInAt(0, Location::RequiresRegister());
3470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3471 break;
3472
3473 case Primitive::kPrimFloat:
3474 case Primitive::kPrimDouble:
3475 locations->SetInAt(0, Location::RequiresFpuRegister());
3476 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3477 break;
3478
3479 default:
3480 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3481 }
3482}
3483
3484void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3485 Primitive::Type type = instruction->GetType();
3486 LocationSummary* locations = instruction->GetLocations();
3487
3488 switch (type) {
3489 case Primitive::kPrimInt:
3490 case Primitive::kPrimLong: {
3491 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3492 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3493 if (type == Primitive::kPrimInt)
3494 __ Subu(dst, ZERO, src);
3495 else
3496 __ Dsubu(dst, ZERO, src);
3497 break;
3498 }
3499 case Primitive::kPrimFloat:
3500 case Primitive::kPrimDouble: {
3501 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3502 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3503 if (type == Primitive::kPrimFloat)
3504 __ NegS(dst, src);
3505 else
3506 __ NegD(dst, src);
3507 break;
3508 }
3509 default:
3510 LOG(FATAL) << "Unexpected neg type " << type;
3511 }
3512}
3513
3514void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3515 LocationSummary* locations =
3516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3517 InvokeRuntimeCallingConvention calling_convention;
3518 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3519 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3520 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3521 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3522}
3523
3524void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3525 LocationSummary* locations = instruction->GetLocations();
3526 // Move an uint16_t value to a register.
3527 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003528 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3529 instruction,
3530 instruction->GetDexPc(),
3531 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003532 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3533}
3534
3535void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3536 LocationSummary* locations =
3537 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3538 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003539 if (instruction->IsStringAlloc()) {
3540 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3541 } else {
3542 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3543 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3544 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003545 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3546}
3547
3548void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003549 if (instruction->IsStringAlloc()) {
3550 // String is allocated through StringFactory. Call NewEmptyString entry point.
3551 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003552 MemberOffset code_offset =
3553 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003554 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3555 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3556 __ Jalr(T9);
3557 __ Nop();
3558 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3559 } else {
3560 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3561 instruction,
3562 instruction->GetDexPc(),
3563 nullptr);
3564 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3565 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003566}
3567
3568void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3570 locations->SetInAt(0, Location::RequiresRegister());
3571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3572}
3573
3574void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3575 Primitive::Type type = instruction->GetType();
3576 LocationSummary* locations = instruction->GetLocations();
3577
3578 switch (type) {
3579 case Primitive::kPrimInt:
3580 case Primitive::kPrimLong: {
3581 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3582 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3583 __ Nor(dst, src, ZERO);
3584 break;
3585 }
3586
3587 default:
3588 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3589 }
3590}
3591
3592void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3593 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3594 locations->SetInAt(0, Location::RequiresRegister());
3595 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3596}
3597
3598void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3599 LocationSummary* locations = instruction->GetLocations();
3600 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3601 locations->InAt(0).AsRegister<GpuRegister>(),
3602 1);
3603}
3604
3605void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003606 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3607 ? LocationSummary::kCallOnSlowPath
3608 : LocationSummary::kNoCall;
3609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003610 locations->SetInAt(0, Location::RequiresRegister());
3611 if (instruction->HasUses()) {
3612 locations->SetOut(Location::SameAsFirstInput());
3613 }
3614}
3615
3616void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3617 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3618 return;
3619 }
3620 Location obj = instruction->GetLocations()->InAt(0);
3621
3622 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3623 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3624}
3625
3626void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3627 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3628 codegen_->AddSlowPath(slow_path);
3629
3630 Location obj = instruction->GetLocations()->InAt(0);
3631
3632 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3633}
3634
3635void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003636 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003637 GenerateImplicitNullCheck(instruction);
3638 } else {
3639 GenerateExplicitNullCheck(instruction);
3640 }
3641}
3642
3643void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3644 HandleBinaryOp(instruction);
3645}
3646
3647void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3648 HandleBinaryOp(instruction);
3649}
3650
3651void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3652 LOG(FATAL) << "Unreachable";
3653}
3654
3655void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3656 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3657}
3658
3659void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3660 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3661 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3662 if (location.IsStackSlot()) {
3663 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3664 } else if (location.IsDoubleStackSlot()) {
3665 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3666 }
3667 locations->SetOut(location);
3668}
3669
3670void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3671 ATTRIBUTE_UNUSED) {
3672 // Nothing to do, the parameter is already at its location.
3673}
3674
3675void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3676 LocationSummary* locations =
3677 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3678 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3679}
3680
3681void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3682 ATTRIBUTE_UNUSED) {
3683 // Nothing to do, the method is already at its location.
3684}
3685
3686void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3687 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3688 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3689 locations->SetInAt(i, Location::Any());
3690 }
3691 locations->SetOut(Location::Any());
3692}
3693
3694void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3695 LOG(FATAL) << "Unreachable";
3696}
3697
3698void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3699 Primitive::Type type = rem->GetResultType();
3700 LocationSummary::CallKind call_kind =
3701 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3702 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3703
3704 switch (type) {
3705 case Primitive::kPrimInt:
3706 case Primitive::kPrimLong:
3707 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003708 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3710 break;
3711
3712 case Primitive::kPrimFloat:
3713 case Primitive::kPrimDouble: {
3714 InvokeRuntimeCallingConvention calling_convention;
3715 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3716 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3717 locations->SetOut(calling_convention.GetReturnLocation(type));
3718 break;
3719 }
3720
3721 default:
3722 LOG(FATAL) << "Unexpected rem type " << type;
3723 }
3724}
3725
3726void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3727 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003728
3729 switch (type) {
3730 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003731 case Primitive::kPrimLong:
3732 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003733 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003734
3735 case Primitive::kPrimFloat:
3736 case Primitive::kPrimDouble: {
3737 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3738 : QUICK_ENTRY_POINT(pFmod);
3739 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003740 if (type == Primitive::kPrimFloat) {
3741 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3742 } else {
3743 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3744 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003745 break;
3746 }
3747 default:
3748 LOG(FATAL) << "Unexpected rem type " << type;
3749 }
3750}
3751
3752void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3753 memory_barrier->SetLocations(nullptr);
3754}
3755
3756void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3757 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3758}
3759
3760void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3761 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3762 Primitive::Type return_type = ret->InputAt(0)->GetType();
3763 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3764}
3765
3766void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3767 codegen_->GenerateFrameExit();
3768}
3769
3770void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3771 ret->SetLocations(nullptr);
3772}
3773
3774void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3775 codegen_->GenerateFrameExit();
3776}
3777
Alexey Frunze92d90602015-12-18 18:16:36 -08003778void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3779 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003780}
3781
Alexey Frunze92d90602015-12-18 18:16:36 -08003782void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3783 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003784}
3785
Alexey Frunze4dda3372015-06-01 18:31:49 -07003786void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3787 HandleShift(shl);
3788}
3789
3790void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3791 HandleShift(shl);
3792}
3793
3794void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3795 HandleShift(shr);
3796}
3797
3798void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3799 HandleShift(shr);
3800}
3801
3802void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3803 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3804 Primitive::Type field_type = store->InputAt(1)->GetType();
3805 switch (field_type) {
3806 case Primitive::kPrimNot:
3807 case Primitive::kPrimBoolean:
3808 case Primitive::kPrimByte:
3809 case Primitive::kPrimChar:
3810 case Primitive::kPrimShort:
3811 case Primitive::kPrimInt:
3812 case Primitive::kPrimFloat:
3813 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3814 break;
3815
3816 case Primitive::kPrimLong:
3817 case Primitive::kPrimDouble:
3818 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3819 break;
3820
3821 default:
3822 LOG(FATAL) << "Unimplemented local type " << field_type;
3823 }
3824}
3825
3826void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3827}
3828
3829void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3830 HandleBinaryOp(instruction);
3831}
3832
3833void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3834 HandleBinaryOp(instruction);
3835}
3836
3837void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3838 HandleFieldGet(instruction, instruction->GetFieldInfo());
3839}
3840
3841void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3842 HandleFieldGet(instruction, instruction->GetFieldInfo());
3843}
3844
3845void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3846 HandleFieldSet(instruction, instruction->GetFieldInfo());
3847}
3848
3849void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003850 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003851}
3852
Calin Juravlee460d1d2015-09-29 04:52:17 +01003853void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3854 HUnresolvedInstanceFieldGet* instruction) {
3855 FieldAccessCallingConventionMIPS64 calling_convention;
3856 codegen_->CreateUnresolvedFieldLocationSummary(
3857 instruction, instruction->GetFieldType(), calling_convention);
3858}
3859
3860void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3861 HUnresolvedInstanceFieldGet* instruction) {
3862 FieldAccessCallingConventionMIPS64 calling_convention;
3863 codegen_->GenerateUnresolvedFieldAccess(instruction,
3864 instruction->GetFieldType(),
3865 instruction->GetFieldIndex(),
3866 instruction->GetDexPc(),
3867 calling_convention);
3868}
3869
3870void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3871 HUnresolvedInstanceFieldSet* instruction) {
3872 FieldAccessCallingConventionMIPS64 calling_convention;
3873 codegen_->CreateUnresolvedFieldLocationSummary(
3874 instruction, instruction->GetFieldType(), calling_convention);
3875}
3876
3877void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3878 HUnresolvedInstanceFieldSet* instruction) {
3879 FieldAccessCallingConventionMIPS64 calling_convention;
3880 codegen_->GenerateUnresolvedFieldAccess(instruction,
3881 instruction->GetFieldType(),
3882 instruction->GetFieldIndex(),
3883 instruction->GetDexPc(),
3884 calling_convention);
3885}
3886
3887void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3888 HUnresolvedStaticFieldGet* instruction) {
3889 FieldAccessCallingConventionMIPS64 calling_convention;
3890 codegen_->CreateUnresolvedFieldLocationSummary(
3891 instruction, instruction->GetFieldType(), calling_convention);
3892}
3893
3894void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3895 HUnresolvedStaticFieldGet* instruction) {
3896 FieldAccessCallingConventionMIPS64 calling_convention;
3897 codegen_->GenerateUnresolvedFieldAccess(instruction,
3898 instruction->GetFieldType(),
3899 instruction->GetFieldIndex(),
3900 instruction->GetDexPc(),
3901 calling_convention);
3902}
3903
3904void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3905 HUnresolvedStaticFieldSet* instruction) {
3906 FieldAccessCallingConventionMIPS64 calling_convention;
3907 codegen_->CreateUnresolvedFieldLocationSummary(
3908 instruction, instruction->GetFieldType(), calling_convention);
3909}
3910
3911void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3912 HUnresolvedStaticFieldSet* instruction) {
3913 FieldAccessCallingConventionMIPS64 calling_convention;
3914 codegen_->GenerateUnresolvedFieldAccess(instruction,
3915 instruction->GetFieldType(),
3916 instruction->GetFieldIndex(),
3917 instruction->GetDexPc(),
3918 calling_convention);
3919}
3920
Alexey Frunze4dda3372015-06-01 18:31:49 -07003921void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3922 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3923}
3924
3925void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3926 HBasicBlock* block = instruction->GetBlock();
3927 if (block->GetLoopInformation() != nullptr) {
3928 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3929 // The back edge will generate the suspend check.
3930 return;
3931 }
3932 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3933 // The goto will generate the suspend check.
3934 return;
3935 }
3936 GenerateSuspendCheck(instruction, nullptr);
3937}
3938
3939void LocationsBuilderMIPS64::VisitTemporary(HTemporary* temp) {
3940 temp->SetLocations(nullptr);
3941}
3942
3943void InstructionCodeGeneratorMIPS64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3944 // Nothing to do, this is driven by the code generator.
3945}
3946
3947void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3948 LocationSummary* locations =
3949 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3950 InvokeRuntimeCallingConvention calling_convention;
3951 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3952}
3953
3954void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3955 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3956 instruction,
3957 instruction->GetDexPc(),
3958 nullptr);
3959 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3960}
3961
3962void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3963 Primitive::Type input_type = conversion->GetInputType();
3964 Primitive::Type result_type = conversion->GetResultType();
3965 DCHECK_NE(input_type, result_type);
3966
3967 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3968 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3969 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3970 }
3971
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003972 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3973
3974 if (Primitive::IsFloatingPointType(input_type)) {
3975 locations->SetInAt(0, Location::RequiresFpuRegister());
3976 } else {
3977 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003978 }
3979
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003980 if (Primitive::IsFloatingPointType(result_type)) {
3981 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003982 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003984 }
3985}
3986
3987void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3988 LocationSummary* locations = conversion->GetLocations();
3989 Primitive::Type result_type = conversion->GetResultType();
3990 Primitive::Type input_type = conversion->GetInputType();
3991
3992 DCHECK_NE(input_type, result_type);
3993
3994 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3995 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3996 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3997
3998 switch (result_type) {
3999 case Primitive::kPrimChar:
4000 __ Andi(dst, src, 0xFFFF);
4001 break;
4002 case Primitive::kPrimByte:
4003 // long is never converted into types narrower than int directly,
4004 // so SEB and SEH can be used without ever causing unpredictable results
4005 // on 64-bit inputs
4006 DCHECK(input_type != Primitive::kPrimLong);
4007 __ Seb(dst, src);
4008 break;
4009 case Primitive::kPrimShort:
4010 // long is never converted into types narrower than int directly,
4011 // so SEB and SEH can be used without ever causing unpredictable results
4012 // on 64-bit inputs
4013 DCHECK(input_type != Primitive::kPrimLong);
4014 __ Seh(dst, src);
4015 break;
4016 case Primitive::kPrimInt:
4017 case Primitive::kPrimLong:
4018 // Sign-extend 32-bit int into bits 32 through 63 for
4019 // int-to-long and long-to-int conversions
4020 __ Sll(dst, src, 0);
4021 break;
4022
4023 default:
4024 LOG(FATAL) << "Unexpected type conversion from " << input_type
4025 << " to " << result_type;
4026 }
4027 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004028 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4029 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4030 if (input_type == Primitive::kPrimLong) {
4031 __ Dmtc1(src, FTMP);
4032 if (result_type == Primitive::kPrimFloat) {
4033 __ Cvtsl(dst, FTMP);
4034 } else {
4035 __ Cvtdl(dst, FTMP);
4036 }
4037 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004038 __ Mtc1(src, FTMP);
4039 if (result_type == Primitive::kPrimFloat) {
4040 __ Cvtsw(dst, FTMP);
4041 } else {
4042 __ Cvtdw(dst, FTMP);
4043 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004044 }
4045 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4046 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004047 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4048 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4049 Mips64Label truncate;
4050 Mips64Label done;
4051
4052 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4053 // value when the input is either a NaN or is outside of the range of the output type
4054 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4055 // the same result.
4056 //
4057 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4058 // value of the output type if the input is outside of the range after the truncation or
4059 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4060 // results. This matches the desired float/double-to-int/long conversion exactly.
4061 //
4062 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4063 //
4064 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4065 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4066 // even though it must be NAN2008=1 on R6.
4067 //
4068 // The code takes care of the different behaviors by first comparing the input to the
4069 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4070 // If the input is greater than or equal to the minimum, it procedes to the truncate
4071 // instruction, which will handle such an input the same way irrespective of NAN2008.
4072 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4073 // in order to return either zero or the minimum value.
4074 //
4075 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4076 // truncate instruction for MIPS64R6.
4077 if (input_type == Primitive::kPrimFloat) {
4078 uint32_t min_val = (result_type == Primitive::kPrimLong)
4079 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4080 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4081 __ LoadConst32(TMP, min_val);
4082 __ Mtc1(TMP, FTMP);
4083 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004084 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004085 uint64_t min_val = (result_type == Primitive::kPrimLong)
4086 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4087 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4088 __ LoadConst64(TMP, min_val);
4089 __ Dmtc1(TMP, FTMP);
4090 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004091 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004092
4093 __ Bc1nez(FTMP, &truncate);
4094
4095 if (input_type == Primitive::kPrimFloat) {
4096 __ CmpEqS(FTMP, src, src);
4097 } else {
4098 __ CmpEqD(FTMP, src, src);
4099 }
4100 if (result_type == Primitive::kPrimLong) {
4101 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4102 } else {
4103 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4104 }
4105 __ Mfc1(TMP, FTMP);
4106 __ And(dst, dst, TMP);
4107
4108 __ Bc(&done);
4109
4110 __ Bind(&truncate);
4111
4112 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004113 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004114 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004115 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004116 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004117 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004118 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004119 } else {
4120 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004121 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004122 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004123 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004124 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004125 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004126 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004127
4128 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004129 } else if (Primitive::IsFloatingPointType(result_type) &&
4130 Primitive::IsFloatingPointType(input_type)) {
4131 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4132 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4133 if (result_type == Primitive::kPrimFloat) {
4134 __ Cvtsd(dst, src);
4135 } else {
4136 __ Cvtds(dst, src);
4137 }
4138 } else {
4139 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4140 << " to " << result_type;
4141 }
4142}
4143
4144void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4145 HandleShift(ushr);
4146}
4147
4148void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4149 HandleShift(ushr);
4150}
4151
4152void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4153 HandleBinaryOp(instruction);
4154}
4155
4156void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4157 HandleBinaryOp(instruction);
4158}
4159
4160void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4161 // Nothing to do, this should be removed during prepare for register allocator.
4162 LOG(FATAL) << "Unreachable";
4163}
4164
4165void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4166 // Nothing to do, this should be removed during prepare for register allocator.
4167 LOG(FATAL) << "Unreachable";
4168}
4169
4170void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004171 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004172}
4173
4174void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004175 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004176}
4177
4178void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004179 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004180}
4181
4182void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004183 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004184}
4185
4186void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004187 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004188}
4189
4190void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004191 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004192}
4193
4194void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004195 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004196}
4197
4198void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004199 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004200}
4201
4202void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004203 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004204}
4205
4206void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004207 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004208}
4209
4210void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004211 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004212}
4213
4214void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004215 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004216}
4217
Aart Bike9f37602015-10-09 11:15:55 -07004218void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004219 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004220}
4221
4222void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004223 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004224}
4225
4226void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004227 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004228}
4229
4230void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004231 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004232}
4233
4234void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004235 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004236}
4237
4238void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004239 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004240}
4241
4242void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004243 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004244}
4245
4246void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004247 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004248}
4249
Mark Mendellfe57faa2015-09-18 09:26:15 -04004250// Simple implementation of packed switch - generate cascaded compare/jumps.
4251void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4252 LocationSummary* locations =
4253 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4254 locations->SetInAt(0, Location::RequiresRegister());
4255}
4256
4257void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4258 int32_t lower_bound = switch_instr->GetStartValue();
4259 int32_t num_entries = switch_instr->GetNumEntries();
4260 LocationSummary* locations = switch_instr->GetLocations();
4261 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4262 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4263
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004264 // Create a set of compare/jumps.
4265 GpuRegister temp_reg = TMP;
4266 if (IsInt<16>(-lower_bound)) {
4267 __ Addiu(temp_reg, value_reg, -lower_bound);
4268 } else {
4269 __ LoadConst32(AT, -lower_bound);
4270 __ Addu(temp_reg, value_reg, AT);
4271 }
4272 // Jump to default if index is negative
4273 // Note: We don't check the case that index is positive while value < lower_bound, because in
4274 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4275 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4276
Mark Mendellfe57faa2015-09-18 09:26:15 -04004277 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004278 // Jump to successors[0] if value == lower_bound.
4279 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4280 int32_t last_index = 0;
4281 for (; num_entries - last_index > 2; last_index += 2) {
4282 __ Addiu(temp_reg, temp_reg, -2);
4283 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4284 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4285 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4286 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4287 }
4288 if (num_entries - last_index == 2) {
4289 // The last missing case_value.
4290 __ Addiu(temp_reg, temp_reg, -1);
4291 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004292 }
4293
4294 // And the default for any other value.
4295 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004296 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004297 }
4298}
4299
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004300void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4301 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4302}
4303
4304void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4305 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4306}
4307
Alexey Frunze4dda3372015-06-01 18:31:49 -07004308} // namespace mips64
4309} // namespace art
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004310