blob: 3aff4ecdb0085b926bee0c7cf8317d9725e345bb [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()->
109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
110
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())->
440#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
441
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);
489 __ DecreaseFrameSize(kMips64WordSize);
490}
491
492void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
493 // Push reg
494 __ IncreaseFrameSize(kMips64WordSize);
495 __ 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.
506 int stack_offset = ensure_scratch.IsSpilled() ? kMips64WordSize : 0;
507 __ LoadFromOffset(load_type,
508 GpuRegister(ensure_scratch.GetRegister()),
509 SP,
510 index1 + stack_offset);
511 __ LoadFromOffset(load_type,
512 TMP,
513 SP,
514 index2 + stack_offset);
515 __ StoreToOffset(store_type,
516 GpuRegister(ensure_scratch.GetRegister()),
517 SP,
518 index2 + stack_offset);
519 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
520}
521
522static dwarf::Reg DWARFReg(GpuRegister reg) {
523 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
524}
525
526// TODO: mapping of floating-point registers to DWARF
527
528void CodeGeneratorMIPS64::GenerateFrameEntry() {
529 __ Bind(&frame_entry_label_);
530
531 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
532
533 if (do_overflow_check) {
534 __ LoadFromOffset(kLoadWord,
535 ZERO,
536 SP,
537 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
538 RecordPcInfo(nullptr, 0);
539 }
540
541 // TODO: anything related to T9/GP/GOT/PIC/.so's?
542
543 if (HasEmptyFrame()) {
544 return;
545 }
546
547 // Make sure the frame size isn't unreasonably large. Per the various APIs
548 // it looks like it should always be less than 2GB in size, which allows
549 // us using 32-bit signed offsets from the stack pointer.
550 if (GetFrameSize() > 0x7FFFFFFF)
551 LOG(FATAL) << "Stack frame larger than 2GB";
552
553 // Spill callee-saved registers.
554 // Note that their cumulative size is small and they can be indexed using
555 // 16-bit offsets.
556
557 // TODO: increment/decrement SP in one step instead of two or remove this comment.
558
559 uint32_t ofs = FrameEntrySpillSize();
560 __ IncreaseFrameSize(ofs);
561
562 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
563 GpuRegister reg = kCoreCalleeSaves[i];
564 if (allocated_registers_.ContainsCoreRegister(reg)) {
565 ofs -= kMips64WordSize;
566 __ Sd(reg, SP, ofs);
567 __ cfi().RelOffset(DWARFReg(reg), ofs);
568 }
569 }
570
571 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
572 FpuRegister reg = kFpuCalleeSaves[i];
573 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
574 ofs -= kMips64WordSize;
575 __ Sdc1(reg, SP, ofs);
576 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
577 }
578 }
579
580 // Allocate the rest of the frame and store the current method pointer
581 // at its end.
582
583 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
584
585 static_assert(IsInt<16>(kCurrentMethodStackOffset),
586 "kCurrentMethodStackOffset must fit into int16_t");
587 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
588}
589
590void CodeGeneratorMIPS64::GenerateFrameExit() {
591 __ cfi().RememberState();
592
593 // TODO: anything related to T9/GP/GOT/PIC/.so's?
594
595 if (!HasEmptyFrame()) {
596 // Deallocate the rest of the frame.
597
598 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
599
600 // Restore callee-saved registers.
601 // Note that their cumulative size is small and they can be indexed using
602 // 16-bit offsets.
603
604 // TODO: increment/decrement SP in one step instead of two or remove this comment.
605
606 uint32_t ofs = 0;
607
608 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
609 FpuRegister reg = kFpuCalleeSaves[i];
610 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
611 __ Ldc1(reg, SP, ofs);
612 ofs += kMips64WordSize;
613 // TODO: __ cfi().Restore(DWARFReg(reg));
614 }
615 }
616
617 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
618 GpuRegister reg = kCoreCalleeSaves[i];
619 if (allocated_registers_.ContainsCoreRegister(reg)) {
620 __ Ld(reg, SP, ofs);
621 ofs += kMips64WordSize;
622 __ cfi().Restore(DWARFReg(reg));
623 }
624 }
625
626 DCHECK_EQ(ofs, FrameEntrySpillSize());
627 __ DecreaseFrameSize(ofs);
628 }
629
630 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700631 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700632
633 __ cfi().RestoreState();
634 __ cfi().DefCFAOffset(GetFrameSize());
635}
636
637void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
638 __ Bind(GetLabelOf(block));
639}
640
641void CodeGeneratorMIPS64::MoveLocation(Location destination,
642 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100643 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700644 if (source.Equals(destination)) {
645 return;
646 }
647
648 // A valid move can always be inferred from the destination and source
649 // locations. When moving from and to a register, the argument type can be
650 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100651 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700652 DCHECK_EQ(unspecified_type, false);
653
654 if (destination.IsRegister() || destination.IsFpuRegister()) {
655 if (unspecified_type) {
656 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
657 if (source.IsStackSlot() ||
658 (src_cst != nullptr && (src_cst->IsIntConstant()
659 || src_cst->IsFloatConstant()
660 || src_cst->IsNullConstant()))) {
661 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100662 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700663 } else {
664 // If the source is a double stack slot or a 64bit constant, a 64bit
665 // type is appropriate. Else the source is a register, and since the
666 // type has not been specified, we chose a 64bit type to force a 64bit
667 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100668 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700669 }
670 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100671 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
672 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700673 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
674 // Move to GPR/FPR from stack
675 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100676 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700677 __ LoadFpuFromOffset(load_type,
678 destination.AsFpuRegister<FpuRegister>(),
679 SP,
680 source.GetStackIndex());
681 } else {
682 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
683 __ LoadFromOffset(load_type,
684 destination.AsRegister<GpuRegister>(),
685 SP,
686 source.GetStackIndex());
687 }
688 } else if (source.IsConstant()) {
689 // Move to GPR/FPR from constant
690 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100691 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700692 gpr = destination.AsRegister<GpuRegister>();
693 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100694 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700695 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
696 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
697 gpr = ZERO;
698 } else {
699 __ LoadConst32(gpr, value);
700 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700701 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700702 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
703 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
704 gpr = ZERO;
705 } else {
706 __ LoadConst64(gpr, value);
707 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700708 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100709 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700710 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700712 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
713 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100714 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700715 if (destination.IsRegister()) {
716 // Move to GPR from GPR
717 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
718 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100719 DCHECK(destination.IsFpuRegister());
720 if (Primitive::Is64BitType(dst_type)) {
721 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
722 } else {
723 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
724 }
725 }
726 } else if (source.IsFpuRegister()) {
727 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700728 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100729 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
731 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100732 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700733 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
734 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100735 } else {
736 DCHECK(destination.IsRegister());
737 if (Primitive::Is64BitType(dst_type)) {
738 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
739 } else {
740 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
741 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700742 }
743 }
744 } else { // The destination is not a register. It must be a stack slot.
745 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
746 if (source.IsRegister() || source.IsFpuRegister()) {
747 if (unspecified_type) {
748 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100749 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700750 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100751 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 }
753 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100754 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
755 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700756 // Move to stack from GPR/FPR
757 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
758 if (source.IsRegister()) {
759 __ StoreToOffset(store_type,
760 source.AsRegister<GpuRegister>(),
761 SP,
762 destination.GetStackIndex());
763 } else {
764 __ StoreFpuToOffset(store_type,
765 source.AsFpuRegister<FpuRegister>(),
766 SP,
767 destination.GetStackIndex());
768 }
769 } else if (source.IsConstant()) {
770 // Move to stack from constant
771 HConstant* src_cst = source.GetConstant();
772 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700773 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700774 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700775 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
776 if (value != 0) {
777 gpr = TMP;
778 __ LoadConst32(gpr, value);
779 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700780 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700781 DCHECK(destination.IsDoubleStackSlot());
782 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
783 if (value != 0) {
784 gpr = TMP;
785 __ LoadConst64(gpr, value);
786 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700787 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700788 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700789 } else {
790 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
791 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
792 // Move to stack from stack
793 if (destination.IsStackSlot()) {
794 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
795 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
796 } else {
797 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
798 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
799 }
800 }
801 }
802}
803
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700804void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700805 DCHECK(!loc1.IsConstant());
806 DCHECK(!loc2.IsConstant());
807
808 if (loc1.Equals(loc2)) {
809 return;
810 }
811
812 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
813 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
814 bool is_fp_reg1 = loc1.IsFpuRegister();
815 bool is_fp_reg2 = loc2.IsFpuRegister();
816
817 if (loc2.IsRegister() && loc1.IsRegister()) {
818 // Swap 2 GPRs
819 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
820 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
821 __ Move(TMP, r2);
822 __ Move(r2, r1);
823 __ Move(r1, TMP);
824 } else if (is_fp_reg2 && is_fp_reg1) {
825 // Swap 2 FPRs
826 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
827 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700828 if (type == Primitive::kPrimFloat) {
829 __ MovS(FTMP, r1);
830 __ MovS(r1, r2);
831 __ MovS(r2, FTMP);
832 } else {
833 DCHECK_EQ(type, Primitive::kPrimDouble);
834 __ MovD(FTMP, r1);
835 __ MovD(r1, r2);
836 __ MovD(r2, FTMP);
837 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700838 } else if (is_slot1 != is_slot2) {
839 // Swap GPR/FPR and stack slot
840 Location reg_loc = is_slot1 ? loc2 : loc1;
841 Location mem_loc = is_slot1 ? loc1 : loc2;
842 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
843 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
844 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
845 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
846 if (reg_loc.IsFpuRegister()) {
847 __ StoreFpuToOffset(store_type,
848 reg_loc.AsFpuRegister<FpuRegister>(),
849 SP,
850 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700851 if (mem_loc.IsStackSlot()) {
852 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
853 } else {
854 DCHECK(mem_loc.IsDoubleStackSlot());
855 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
856 }
857 } else {
858 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
859 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
860 }
861 } else if (is_slot1 && is_slot2) {
862 move_resolver_.Exchange(loc1.GetStackIndex(),
863 loc2.GetStackIndex(),
864 loc1.IsDoubleStackSlot());
865 } else {
866 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
867 }
868}
869
870void CodeGeneratorMIPS64::Move(HInstruction* instruction,
871 Location location,
872 HInstruction* move_for) {
873 LocationSummary* locations = instruction->GetLocations();
874 Primitive::Type type = instruction->GetType();
875 DCHECK_NE(type, Primitive::kPrimVoid);
876
877 if (instruction->IsCurrentMethod()) {
878 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset), type);
879 } else if (locations != nullptr && locations->Out().Equals(location)) {
880 return;
881 } else if (instruction->IsIntConstant()
882 || instruction->IsLongConstant()
883 || instruction->IsNullConstant()) {
884 if (location.IsRegister()) {
885 // Move to GPR from constant
886 GpuRegister dst = location.AsRegister<GpuRegister>();
887 if (instruction->IsNullConstant() || instruction->IsIntConstant()) {
888 __ LoadConst32(dst, GetInt32ValueOf(instruction->AsConstant()));
889 } else {
890 __ LoadConst64(dst, instruction->AsLongConstant()->GetValue());
891 }
892 } else {
893 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
894 // Move to stack from constant
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700895 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700896 if (location.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700897 int32_t value = GetInt32ValueOf(instruction->AsConstant());
898 if (value != 0) {
899 gpr = TMP;
900 __ LoadConst32(gpr, value);
901 }
902 __ StoreToOffset(kStoreWord, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700903 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700904 DCHECK(location.IsDoubleStackSlot());
905 int64_t value = instruction->AsLongConstant()->GetValue();
906 if (value != 0) {
907 gpr = TMP;
908 __ LoadConst64(gpr, value);
909 }
910 __ StoreToOffset(kStoreDoubleword, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700911 }
912 }
913 } else if (instruction->IsTemporary()) {
914 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
915 MoveLocation(location, temp_location, type);
916 } else if (instruction->IsLoadLocal()) {
917 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
918 if (Primitive::Is64BitType(type)) {
919 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
920 } else {
921 MoveLocation(location, Location::StackSlot(stack_slot), type);
922 }
923 } else {
924 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
925 MoveLocation(location, locations->Out(), type);
926 }
927}
928
Calin Juravle175dc732015-08-25 15:42:32 +0100929void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
930 DCHECK(location.IsRegister());
931 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
932}
933
Calin Juravlee460d1d2015-09-29 04:52:17 +0100934void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
935 if (location.IsRegister()) {
936 locations->AddTemp(location);
937 } else {
938 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
939 }
940}
941
Alexey Frunze4dda3372015-06-01 18:31:49 -0700942Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
943 Primitive::Type type = load->GetType();
944
945 switch (type) {
946 case Primitive::kPrimNot:
947 case Primitive::kPrimInt:
948 case Primitive::kPrimFloat:
949 return Location::StackSlot(GetStackSlot(load->GetLocal()));
950
951 case Primitive::kPrimLong:
952 case Primitive::kPrimDouble:
953 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
954
955 case Primitive::kPrimBoolean:
956 case Primitive::kPrimByte:
957 case Primitive::kPrimChar:
958 case Primitive::kPrimShort:
959 case Primitive::kPrimVoid:
960 LOG(FATAL) << "Unexpected type " << type;
961 }
962
963 LOG(FATAL) << "Unreachable";
964 return Location::NoLocation();
965}
966
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100967void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
968 GpuRegister value,
969 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700970 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700971 GpuRegister card = AT;
972 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100973 if (value_can_be_null) {
974 __ Beqzc(value, &done);
975 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700976 __ LoadFromOffset(kLoadDoubleword,
977 card,
978 TR,
979 Thread::CardTableOffset<kMips64WordSize>().Int32Value());
980 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
981 __ Daddu(temp, card, temp);
982 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100983 if (value_can_be_null) {
984 __ Bind(&done);
985 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700986}
987
David Brazdil58282f42016-01-14 12:45:10 +0000988void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700989 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
990 blocked_core_registers_[ZERO] = true;
991 blocked_core_registers_[K0] = true;
992 blocked_core_registers_[K1] = true;
993 blocked_core_registers_[GP] = true;
994 blocked_core_registers_[SP] = true;
995 blocked_core_registers_[RA] = true;
996
997 // AT and TMP(T8) are used as temporary/scratch registers
998 // (similar to how AT is used by MIPS assemblers).
999 blocked_core_registers_[AT] = true;
1000 blocked_core_registers_[TMP] = true;
1001 blocked_fpu_registers_[FTMP] = true;
1002
1003 // Reserve suspend and thread registers.
1004 blocked_core_registers_[S0] = true;
1005 blocked_core_registers_[TR] = true;
1006
1007 // Reserve T9 for function calls
1008 blocked_core_registers_[T9] = true;
1009
1010 // TODO: review; anything else?
1011
David Brazdil58282f42016-01-14 12:45:10 +00001012 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001013 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1014 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1015 }
1016
1017 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1018 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1019 }
1020}
1021
Alexey Frunze4dda3372015-06-01 18:31:49 -07001022size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1023 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
1024 return kMips64WordSize;
1025}
1026
1027size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1028 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
1029 return kMips64WordSize;
1030}
1031
1032size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1033 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
1034 return kMips64WordSize;
1035}
1036
1037size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1038 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
1039 return kMips64WordSize;
1040}
1041
1042void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001043 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001044}
1045
1046void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001047 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001048}
1049
Calin Juravle175dc732015-08-25 15:42:32 +01001050void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1051 HInstruction* instruction,
1052 uint32_t dex_pc,
1053 SlowPathCode* slow_path) {
1054 InvokeRuntime(GetThreadOffset<kMips64WordSize>(entrypoint).Int32Value(),
1055 instruction,
1056 dex_pc,
1057 slow_path);
1058}
1059
Alexey Frunze4dda3372015-06-01 18:31:49 -07001060void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
1061 HInstruction* instruction,
1062 uint32_t dex_pc,
1063 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001064 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001065 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1066 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1067 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001068 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001069 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001070}
1071
1072void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1073 GpuRegister class_reg) {
1074 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1075 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1076 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1077 // TODO: barrier needed?
1078 __ Bind(slow_path->GetExitLabel());
1079}
1080
1081void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1082 __ Sync(0); // only stype 0 is supported
1083}
1084
1085void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1086 HBasicBlock* successor) {
1087 SuspendCheckSlowPathMIPS64* slow_path =
1088 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1089 codegen_->AddSlowPath(slow_path);
1090
1091 __ LoadFromOffset(kLoadUnsignedHalfword,
1092 TMP,
1093 TR,
1094 Thread::ThreadFlagsOffset<kMips64WordSize>().Int32Value());
1095 if (successor == nullptr) {
1096 __ Bnezc(TMP, slow_path->GetEntryLabel());
1097 __ Bind(slow_path->GetReturnLabel());
1098 } else {
1099 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001100 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001101 // slow_path will return to GetLabelOf(successor).
1102 }
1103}
1104
1105InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1106 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001107 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001108 assembler_(codegen->GetAssembler()),
1109 codegen_(codegen) {}
1110
1111void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1112 DCHECK_EQ(instruction->InputCount(), 2U);
1113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1114 Primitive::Type type = instruction->GetResultType();
1115 switch (type) {
1116 case Primitive::kPrimInt:
1117 case Primitive::kPrimLong: {
1118 locations->SetInAt(0, Location::RequiresRegister());
1119 HInstruction* right = instruction->InputAt(1);
1120 bool can_use_imm = false;
1121 if (right->IsConstant()) {
1122 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1123 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1124 can_use_imm = IsUint<16>(imm);
1125 } else if (instruction->IsAdd()) {
1126 can_use_imm = IsInt<16>(imm);
1127 } else {
1128 DCHECK(instruction->IsSub());
1129 can_use_imm = IsInt<16>(-imm);
1130 }
1131 }
1132 if (can_use_imm)
1133 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1134 else
1135 locations->SetInAt(1, Location::RequiresRegister());
1136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1137 }
1138 break;
1139
1140 case Primitive::kPrimFloat:
1141 case Primitive::kPrimDouble:
1142 locations->SetInAt(0, Location::RequiresFpuRegister());
1143 locations->SetInAt(1, Location::RequiresFpuRegister());
1144 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1145 break;
1146
1147 default:
1148 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1149 }
1150}
1151
1152void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1153 Primitive::Type type = instruction->GetType();
1154 LocationSummary* locations = instruction->GetLocations();
1155
1156 switch (type) {
1157 case Primitive::kPrimInt:
1158 case Primitive::kPrimLong: {
1159 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1160 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1161 Location rhs_location = locations->InAt(1);
1162
1163 GpuRegister rhs_reg = ZERO;
1164 int64_t rhs_imm = 0;
1165 bool use_imm = rhs_location.IsConstant();
1166 if (use_imm) {
1167 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1168 } else {
1169 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1170 }
1171
1172 if (instruction->IsAnd()) {
1173 if (use_imm)
1174 __ Andi(dst, lhs, rhs_imm);
1175 else
1176 __ And(dst, lhs, rhs_reg);
1177 } else if (instruction->IsOr()) {
1178 if (use_imm)
1179 __ Ori(dst, lhs, rhs_imm);
1180 else
1181 __ Or(dst, lhs, rhs_reg);
1182 } else if (instruction->IsXor()) {
1183 if (use_imm)
1184 __ Xori(dst, lhs, rhs_imm);
1185 else
1186 __ Xor(dst, lhs, rhs_reg);
1187 } else if (instruction->IsAdd()) {
1188 if (type == Primitive::kPrimInt) {
1189 if (use_imm)
1190 __ Addiu(dst, lhs, rhs_imm);
1191 else
1192 __ Addu(dst, lhs, rhs_reg);
1193 } else {
1194 if (use_imm)
1195 __ Daddiu(dst, lhs, rhs_imm);
1196 else
1197 __ Daddu(dst, lhs, rhs_reg);
1198 }
1199 } else {
1200 DCHECK(instruction->IsSub());
1201 if (type == Primitive::kPrimInt) {
1202 if (use_imm)
1203 __ Addiu(dst, lhs, -rhs_imm);
1204 else
1205 __ Subu(dst, lhs, rhs_reg);
1206 } else {
1207 if (use_imm)
1208 __ Daddiu(dst, lhs, -rhs_imm);
1209 else
1210 __ Dsubu(dst, lhs, rhs_reg);
1211 }
1212 }
1213 break;
1214 }
1215 case Primitive::kPrimFloat:
1216 case Primitive::kPrimDouble: {
1217 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1218 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1219 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1220 if (instruction->IsAdd()) {
1221 if (type == Primitive::kPrimFloat)
1222 __ AddS(dst, lhs, rhs);
1223 else
1224 __ AddD(dst, lhs, rhs);
1225 } else if (instruction->IsSub()) {
1226 if (type == Primitive::kPrimFloat)
1227 __ SubS(dst, lhs, rhs);
1228 else
1229 __ SubD(dst, lhs, rhs);
1230 } else {
1231 LOG(FATAL) << "Unexpected floating-point binary operation";
1232 }
1233 break;
1234 }
1235 default:
1236 LOG(FATAL) << "Unexpected binary operation type " << type;
1237 }
1238}
1239
1240void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001241 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001242
1243 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1244 Primitive::Type type = instr->GetResultType();
1245 switch (type) {
1246 case Primitive::kPrimInt:
1247 case Primitive::kPrimLong: {
1248 locations->SetInAt(0, Location::RequiresRegister());
1249 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001251 break;
1252 }
1253 default:
1254 LOG(FATAL) << "Unexpected shift type " << type;
1255 }
1256}
1257
1258void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001259 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001260 LocationSummary* locations = instr->GetLocations();
1261 Primitive::Type type = instr->GetType();
1262
1263 switch (type) {
1264 case Primitive::kPrimInt:
1265 case Primitive::kPrimLong: {
1266 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1267 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1268 Location rhs_location = locations->InAt(1);
1269
1270 GpuRegister rhs_reg = ZERO;
1271 int64_t rhs_imm = 0;
1272 bool use_imm = rhs_location.IsConstant();
1273 if (use_imm) {
1274 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1275 } else {
1276 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1277 }
1278
1279 if (use_imm) {
1280 uint32_t shift_value = (type == Primitive::kPrimInt)
1281 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1282 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1283
Alexey Frunze92d90602015-12-18 18:16:36 -08001284 if (shift_value == 0) {
1285 if (dst != lhs) {
1286 __ Move(dst, lhs);
1287 }
1288 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001289 if (instr->IsShl()) {
1290 __ Sll(dst, lhs, shift_value);
1291 } else if (instr->IsShr()) {
1292 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001293 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001294 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001295 } else {
1296 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297 }
1298 } else {
1299 if (shift_value < 32) {
1300 if (instr->IsShl()) {
1301 __ Dsll(dst, lhs, shift_value);
1302 } else if (instr->IsShr()) {
1303 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001304 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001305 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001306 } else {
1307 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001308 }
1309 } else {
1310 shift_value -= 32;
1311 if (instr->IsShl()) {
1312 __ Dsll32(dst, lhs, shift_value);
1313 } else if (instr->IsShr()) {
1314 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001315 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001316 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001317 } else {
1318 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001319 }
1320 }
1321 }
1322 } else {
1323 if (type == Primitive::kPrimInt) {
1324 if (instr->IsShl()) {
1325 __ Sllv(dst, lhs, rhs_reg);
1326 } else if (instr->IsShr()) {
1327 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001328 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001329 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001330 } else {
1331 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001332 }
1333 } else {
1334 if (instr->IsShl()) {
1335 __ Dsllv(dst, lhs, rhs_reg);
1336 } else if (instr->IsShr()) {
1337 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001338 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001339 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001340 } else {
1341 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001342 }
1343 }
1344 }
1345 break;
1346 }
1347 default:
1348 LOG(FATAL) << "Unexpected shift operation type " << type;
1349 }
1350}
1351
1352void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1353 HandleBinaryOp(instruction);
1354}
1355
1356void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1357 HandleBinaryOp(instruction);
1358}
1359
1360void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1361 HandleBinaryOp(instruction);
1362}
1363
1364void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1365 HandleBinaryOp(instruction);
1366}
1367
1368void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1369 LocationSummary* locations =
1370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1371 locations->SetInAt(0, Location::RequiresRegister());
1372 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1373 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1374 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1375 } else {
1376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1377 }
1378}
1379
1380void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1381 LocationSummary* locations = instruction->GetLocations();
1382 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1383 Location index = locations->InAt(1);
1384 Primitive::Type type = instruction->GetType();
1385
1386 switch (type) {
1387 case Primitive::kPrimBoolean: {
1388 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1389 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1390 if (index.IsConstant()) {
1391 size_t offset =
1392 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1393 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1394 } else {
1395 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1396 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1397 }
1398 break;
1399 }
1400
1401 case Primitive::kPrimByte: {
1402 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1403 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1404 if (index.IsConstant()) {
1405 size_t offset =
1406 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1407 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1408 } else {
1409 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1410 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1411 }
1412 break;
1413 }
1414
1415 case Primitive::kPrimShort: {
1416 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1417 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1418 if (index.IsConstant()) {
1419 size_t offset =
1420 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1421 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1422 } else {
1423 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1424 __ Daddu(TMP, obj, TMP);
1425 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1426 }
1427 break;
1428 }
1429
1430 case Primitive::kPrimChar: {
1431 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1432 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1433 if (index.IsConstant()) {
1434 size_t offset =
1435 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1436 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1437 } else {
1438 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1439 __ Daddu(TMP, obj, TMP);
1440 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1441 }
1442 break;
1443 }
1444
1445 case Primitive::kPrimInt:
1446 case Primitive::kPrimNot: {
1447 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1448 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1449 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1450 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1451 if (index.IsConstant()) {
1452 size_t offset =
1453 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1454 __ LoadFromOffset(load_type, out, obj, offset);
1455 } else {
1456 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1457 __ Daddu(TMP, obj, TMP);
1458 __ LoadFromOffset(load_type, out, TMP, data_offset);
1459 }
1460 break;
1461 }
1462
1463 case Primitive::kPrimLong: {
1464 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1465 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1466 if (index.IsConstant()) {
1467 size_t offset =
1468 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1469 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1470 } else {
1471 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1472 __ Daddu(TMP, obj, TMP);
1473 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1474 }
1475 break;
1476 }
1477
1478 case Primitive::kPrimFloat: {
1479 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1480 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1481 if (index.IsConstant()) {
1482 size_t offset =
1483 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1484 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1485 } else {
1486 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1487 __ Daddu(TMP, obj, TMP);
1488 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1489 }
1490 break;
1491 }
1492
1493 case Primitive::kPrimDouble: {
1494 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1495 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1496 if (index.IsConstant()) {
1497 size_t offset =
1498 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1499 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1500 } else {
1501 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1502 __ Daddu(TMP, obj, TMP);
1503 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1504 }
1505 break;
1506 }
1507
1508 case Primitive::kPrimVoid:
1509 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1510 UNREACHABLE();
1511 }
1512 codegen_->MaybeRecordImplicitNullCheck(instruction);
1513}
1514
1515void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1516 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1517 locations->SetInAt(0, Location::RequiresRegister());
1518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1519}
1520
1521void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1522 LocationSummary* locations = instruction->GetLocations();
1523 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1524 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1525 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1526 __ LoadFromOffset(kLoadWord, out, obj, offset);
1527 codegen_->MaybeRecordImplicitNullCheck(instruction);
1528}
1529
1530void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001531 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001532 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1533 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001534 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1535 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001536 InvokeRuntimeCallingConvention calling_convention;
1537 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1538 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1539 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1540 } else {
1541 locations->SetInAt(0, Location::RequiresRegister());
1542 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1543 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1544 locations->SetInAt(2, Location::RequiresFpuRegister());
1545 } else {
1546 locations->SetInAt(2, Location::RequiresRegister());
1547 }
1548 }
1549}
1550
1551void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1552 LocationSummary* locations = instruction->GetLocations();
1553 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1554 Location index = locations->InAt(1);
1555 Primitive::Type value_type = instruction->GetComponentType();
1556 bool needs_runtime_call = locations->WillCall();
1557 bool needs_write_barrier =
1558 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1559
1560 switch (value_type) {
1561 case Primitive::kPrimBoolean:
1562 case Primitive::kPrimByte: {
1563 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1564 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1565 if (index.IsConstant()) {
1566 size_t offset =
1567 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1568 __ StoreToOffset(kStoreByte, value, obj, offset);
1569 } else {
1570 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1571 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1572 }
1573 break;
1574 }
1575
1576 case Primitive::kPrimShort:
1577 case Primitive::kPrimChar: {
1578 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1579 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1580 if (index.IsConstant()) {
1581 size_t offset =
1582 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1583 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1584 } else {
1585 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1586 __ Daddu(TMP, obj, TMP);
1587 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1588 }
1589 break;
1590 }
1591
1592 case Primitive::kPrimInt:
1593 case Primitive::kPrimNot: {
1594 if (!needs_runtime_call) {
1595 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1596 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1597 if (index.IsConstant()) {
1598 size_t offset =
1599 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1600 __ StoreToOffset(kStoreWord, value, obj, offset);
1601 } else {
1602 DCHECK(index.IsRegister()) << index;
1603 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1604 __ Daddu(TMP, obj, TMP);
1605 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1606 }
1607 codegen_->MaybeRecordImplicitNullCheck(instruction);
1608 if (needs_write_barrier) {
1609 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001610 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001611 }
1612 } else {
1613 DCHECK_EQ(value_type, Primitive::kPrimNot);
1614 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1615 instruction,
1616 instruction->GetDexPc(),
1617 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001618 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001619 }
1620 break;
1621 }
1622
1623 case Primitive::kPrimLong: {
1624 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1625 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1626 if (index.IsConstant()) {
1627 size_t offset =
1628 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1629 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1630 } else {
1631 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1632 __ Daddu(TMP, obj, TMP);
1633 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1634 }
1635 break;
1636 }
1637
1638 case Primitive::kPrimFloat: {
1639 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1640 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1641 DCHECK(locations->InAt(2).IsFpuRegister());
1642 if (index.IsConstant()) {
1643 size_t offset =
1644 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1645 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1646 } else {
1647 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1648 __ Daddu(TMP, obj, TMP);
1649 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1650 }
1651 break;
1652 }
1653
1654 case Primitive::kPrimDouble: {
1655 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1656 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1657 DCHECK(locations->InAt(2).IsFpuRegister());
1658 if (index.IsConstant()) {
1659 size_t offset =
1660 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1661 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1662 } else {
1663 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1664 __ Daddu(TMP, obj, TMP);
1665 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1666 }
1667 break;
1668 }
1669
1670 case Primitive::kPrimVoid:
1671 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1672 UNREACHABLE();
1673 }
1674
1675 // Ints and objects are handled in the switch.
1676 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1677 codegen_->MaybeRecordImplicitNullCheck(instruction);
1678 }
1679}
1680
1681void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001682 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1683 ? LocationSummary::kCallOnSlowPath
1684 : LocationSummary::kNoCall;
1685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001686 locations->SetInAt(0, Location::RequiresRegister());
1687 locations->SetInAt(1, Location::RequiresRegister());
1688 if (instruction->HasUses()) {
1689 locations->SetOut(Location::SameAsFirstInput());
1690 }
1691}
1692
1693void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1694 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001695 BoundsCheckSlowPathMIPS64* slow_path =
1696 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001697 codegen_->AddSlowPath(slow_path);
1698
1699 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1700 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1701
1702 // length is limited by the maximum positive signed 32-bit integer.
1703 // Unsigned comparison of length and index checks for index < 0
1704 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001705 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001706}
1707
1708void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1710 instruction,
1711 LocationSummary::kCallOnSlowPath);
1712 locations->SetInAt(0, Location::RequiresRegister());
1713 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001714 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001715 locations->AddTemp(Location::RequiresRegister());
1716}
1717
1718void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1719 LocationSummary* locations = instruction->GetLocations();
1720 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1721 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1722 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1723
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001724 SlowPathCodeMIPS64* slow_path =
1725 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001726 codegen_->AddSlowPath(slow_path);
1727
1728 // TODO: avoid this check if we know obj is not null.
1729 __ Beqzc(obj, slow_path->GetExitLabel());
1730 // Compare the class of `obj` with `cls`.
1731 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1732 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1733 __ Bind(slow_path->GetExitLabel());
1734}
1735
1736void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1737 LocationSummary* locations =
1738 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1739 locations->SetInAt(0, Location::RequiresRegister());
1740 if (check->HasUses()) {
1741 locations->SetOut(Location::SameAsFirstInput());
1742 }
1743}
1744
1745void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1746 // We assume the class is not null.
1747 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1748 check->GetLoadClass(),
1749 check,
1750 check->GetDexPc(),
1751 true);
1752 codegen_->AddSlowPath(slow_path);
1753 GenerateClassInitializationCheck(slow_path,
1754 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1755}
1756
1757void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1758 Primitive::Type in_type = compare->InputAt(0)->GetType();
1759
Alexey Frunze299a9392015-12-08 16:08:02 -08001760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001761
1762 switch (in_type) {
1763 case Primitive::kPrimLong:
1764 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001765 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001766 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1767 break;
1768
1769 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001770 case Primitive::kPrimDouble:
1771 locations->SetInAt(0, Location::RequiresFpuRegister());
1772 locations->SetInAt(1, Location::RequiresFpuRegister());
1773 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001774 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001775
1776 default:
1777 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1778 }
1779}
1780
1781void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1782 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001783 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001784 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08001785 bool gt_bias = instruction->IsGtBias();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001786
1787 // 0 if: left == right
1788 // 1 if: left > right
1789 // -1 if: left < right
1790 switch (in_type) {
1791 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001792 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001793 Location rhs_location = locations->InAt(1);
1794 bool use_imm = rhs_location.IsConstant();
1795 GpuRegister rhs = ZERO;
1796 if (use_imm) {
1797 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1798 if (value != 0) {
1799 rhs = AT;
1800 __ LoadConst64(rhs, value);
1801 }
1802 } else {
1803 rhs = rhs_location.AsRegister<GpuRegister>();
1804 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001805 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001806 __ Slt(res, rhs, lhs);
1807 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001808 break;
1809 }
1810
Alexey Frunze299a9392015-12-08 16:08:02 -08001811 case Primitive::kPrimFloat: {
1812 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1813 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1814 Mips64Label done;
1815 __ CmpEqS(FTMP, lhs, rhs);
1816 __ LoadConst32(res, 0);
1817 __ Bc1nez(FTMP, &done);
1818 if (gt_bias) {
1819 __ CmpLtS(FTMP, lhs, rhs);
1820 __ LoadConst32(res, -1);
1821 __ Bc1nez(FTMP, &done);
1822 __ LoadConst32(res, 1);
1823 } else {
1824 __ CmpLtS(FTMP, rhs, lhs);
1825 __ LoadConst32(res, 1);
1826 __ Bc1nez(FTMP, &done);
1827 __ LoadConst32(res, -1);
1828 }
1829 __ Bind(&done);
1830 break;
1831 }
1832
Alexey Frunze4dda3372015-06-01 18:31:49 -07001833 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001834 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1835 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1836 Mips64Label done;
1837 __ CmpEqD(FTMP, lhs, rhs);
1838 __ LoadConst32(res, 0);
1839 __ Bc1nez(FTMP, &done);
1840 if (gt_bias) {
1841 __ CmpLtD(FTMP, lhs, rhs);
1842 __ LoadConst32(res, -1);
1843 __ Bc1nez(FTMP, &done);
1844 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001845 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001846 __ CmpLtD(FTMP, rhs, lhs);
1847 __ LoadConst32(res, 1);
1848 __ Bc1nez(FTMP, &done);
1849 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001850 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001851 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001852 break;
1853 }
1854
1855 default:
1856 LOG(FATAL) << "Unimplemented compare type " << in_type;
1857 }
1858}
1859
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001860void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001861 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001862 switch (instruction->InputAt(0)->GetType()) {
1863 default:
1864 case Primitive::kPrimLong:
1865 locations->SetInAt(0, Location::RequiresRegister());
1866 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1867 break;
1868
1869 case Primitive::kPrimFloat:
1870 case Primitive::kPrimDouble:
1871 locations->SetInAt(0, Location::RequiresFpuRegister());
1872 locations->SetInAt(1, Location::RequiresFpuRegister());
1873 break;
1874 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001875 if (instruction->NeedsMaterialization()) {
1876 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1877 }
1878}
1879
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001880void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001881 if (!instruction->NeedsMaterialization()) {
1882 return;
1883 }
1884
Alexey Frunze299a9392015-12-08 16:08:02 -08001885 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001886 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001887 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001888 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001889
Alexey Frunze299a9392015-12-08 16:08:02 -08001890 switch (type) {
1891 default:
1892 // Integer case.
1893 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1894 return;
1895 case Primitive::kPrimLong:
1896 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1897 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001898
Alexey Frunze299a9392015-12-08 16:08:02 -08001899 case Primitive::kPrimFloat:
1900 case Primitive::kPrimDouble:
1901 // TODO: don't use branches.
1902 GenerateFpCompareAndBranch(instruction->GetCondition(),
1903 instruction->IsGtBias(),
1904 type,
1905 locations,
1906 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001907 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001908 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001909
1910 // Convert the branches into the result.
1911 Mips64Label done;
1912
1913 // False case: result = 0.
1914 __ LoadConst32(dst, 0);
1915 __ Bc(&done);
1916
1917 // True case: result = 1.
1918 __ Bind(&true_label);
1919 __ LoadConst32(dst, 1);
1920 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001921}
1922
Alexey Frunzec857c742015-09-23 15:12:39 -07001923void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1924 DCHECK(instruction->IsDiv() || instruction->IsRem());
1925 Primitive::Type type = instruction->GetResultType();
1926
1927 LocationSummary* locations = instruction->GetLocations();
1928 Location second = locations->InAt(1);
1929 DCHECK(second.IsConstant());
1930
1931 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1932 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1933 int64_t imm = Int64FromConstant(second.GetConstant());
1934 DCHECK(imm == 1 || imm == -1);
1935
1936 if (instruction->IsRem()) {
1937 __ Move(out, ZERO);
1938 } else {
1939 if (imm == -1) {
1940 if (type == Primitive::kPrimInt) {
1941 __ Subu(out, ZERO, dividend);
1942 } else {
1943 DCHECK_EQ(type, Primitive::kPrimLong);
1944 __ Dsubu(out, ZERO, dividend);
1945 }
1946 } else if (out != dividend) {
1947 __ Move(out, dividend);
1948 }
1949 }
1950}
1951
1952void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1953 DCHECK(instruction->IsDiv() || instruction->IsRem());
1954 Primitive::Type type = instruction->GetResultType();
1955
1956 LocationSummary* locations = instruction->GetLocations();
1957 Location second = locations->InAt(1);
1958 DCHECK(second.IsConstant());
1959
1960 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1961 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1962 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001963 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001964 int ctz_imm = CTZ(abs_imm);
1965
1966 if (instruction->IsDiv()) {
1967 if (type == Primitive::kPrimInt) {
1968 if (ctz_imm == 1) {
1969 // Fast path for division by +/-2, which is very common.
1970 __ Srl(TMP, dividend, 31);
1971 } else {
1972 __ Sra(TMP, dividend, 31);
1973 __ Srl(TMP, TMP, 32 - ctz_imm);
1974 }
1975 __ Addu(out, dividend, TMP);
1976 __ Sra(out, out, ctz_imm);
1977 if (imm < 0) {
1978 __ Subu(out, ZERO, out);
1979 }
1980 } else {
1981 DCHECK_EQ(type, Primitive::kPrimLong);
1982 if (ctz_imm == 1) {
1983 // Fast path for division by +/-2, which is very common.
1984 __ Dsrl32(TMP, dividend, 31);
1985 } else {
1986 __ Dsra32(TMP, dividend, 31);
1987 if (ctz_imm > 32) {
1988 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1989 } else {
1990 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1991 }
1992 }
1993 __ Daddu(out, dividend, TMP);
1994 if (ctz_imm < 32) {
1995 __ Dsra(out, out, ctz_imm);
1996 } else {
1997 __ Dsra32(out, out, ctz_imm - 32);
1998 }
1999 if (imm < 0) {
2000 __ Dsubu(out, ZERO, out);
2001 }
2002 }
2003 } else {
2004 if (type == Primitive::kPrimInt) {
2005 if (ctz_imm == 1) {
2006 // Fast path for modulo +/-2, which is very common.
2007 __ Sra(TMP, dividend, 31);
2008 __ Subu(out, dividend, TMP);
2009 __ Andi(out, out, 1);
2010 __ Addu(out, out, TMP);
2011 } else {
2012 __ Sra(TMP, dividend, 31);
2013 __ Srl(TMP, TMP, 32 - ctz_imm);
2014 __ Addu(out, dividend, TMP);
2015 if (IsUint<16>(abs_imm - 1)) {
2016 __ Andi(out, out, abs_imm - 1);
2017 } else {
2018 __ Sll(out, out, 32 - ctz_imm);
2019 __ Srl(out, out, 32 - ctz_imm);
2020 }
2021 __ Subu(out, out, TMP);
2022 }
2023 } else {
2024 DCHECK_EQ(type, Primitive::kPrimLong);
2025 if (ctz_imm == 1) {
2026 // Fast path for modulo +/-2, which is very common.
2027 __ Dsra32(TMP, dividend, 31);
2028 __ Dsubu(out, dividend, TMP);
2029 __ Andi(out, out, 1);
2030 __ Daddu(out, out, TMP);
2031 } else {
2032 __ Dsra32(TMP, dividend, 31);
2033 if (ctz_imm > 32) {
2034 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2035 } else {
2036 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2037 }
2038 __ Daddu(out, dividend, TMP);
2039 if (IsUint<16>(abs_imm - 1)) {
2040 __ Andi(out, out, abs_imm - 1);
2041 } else {
2042 if (ctz_imm > 32) {
2043 __ Dsll(out, out, 64 - ctz_imm);
2044 __ Dsrl(out, out, 64 - ctz_imm);
2045 } else {
2046 __ Dsll32(out, out, 32 - ctz_imm);
2047 __ Dsrl32(out, out, 32 - ctz_imm);
2048 }
2049 }
2050 __ Dsubu(out, out, TMP);
2051 }
2052 }
2053 }
2054}
2055
2056void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2057 DCHECK(instruction->IsDiv() || instruction->IsRem());
2058
2059 LocationSummary* locations = instruction->GetLocations();
2060 Location second = locations->InAt(1);
2061 DCHECK(second.IsConstant());
2062
2063 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2064 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2065 int64_t imm = Int64FromConstant(second.GetConstant());
2066
2067 Primitive::Type type = instruction->GetResultType();
2068 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2069
2070 int64_t magic;
2071 int shift;
2072 CalculateMagicAndShiftForDivRem(imm,
2073 (type == Primitive::kPrimLong),
2074 &magic,
2075 &shift);
2076
2077 if (type == Primitive::kPrimInt) {
2078 __ LoadConst32(TMP, magic);
2079 __ MuhR6(TMP, dividend, TMP);
2080
2081 if (imm > 0 && magic < 0) {
2082 __ Addu(TMP, TMP, dividend);
2083 } else if (imm < 0 && magic > 0) {
2084 __ Subu(TMP, TMP, dividend);
2085 }
2086
2087 if (shift != 0) {
2088 __ Sra(TMP, TMP, shift);
2089 }
2090
2091 if (instruction->IsDiv()) {
2092 __ Sra(out, TMP, 31);
2093 __ Subu(out, TMP, out);
2094 } else {
2095 __ Sra(AT, TMP, 31);
2096 __ Subu(AT, TMP, AT);
2097 __ LoadConst32(TMP, imm);
2098 __ MulR6(TMP, AT, TMP);
2099 __ Subu(out, dividend, TMP);
2100 }
2101 } else {
2102 __ LoadConst64(TMP, magic);
2103 __ Dmuh(TMP, dividend, TMP);
2104
2105 if (imm > 0 && magic < 0) {
2106 __ Daddu(TMP, TMP, dividend);
2107 } else if (imm < 0 && magic > 0) {
2108 __ Dsubu(TMP, TMP, dividend);
2109 }
2110
2111 if (shift >= 32) {
2112 __ Dsra32(TMP, TMP, shift - 32);
2113 } else if (shift > 0) {
2114 __ Dsra(TMP, TMP, shift);
2115 }
2116
2117 if (instruction->IsDiv()) {
2118 __ Dsra32(out, TMP, 31);
2119 __ Dsubu(out, TMP, out);
2120 } else {
2121 __ Dsra32(AT, TMP, 31);
2122 __ Dsubu(AT, TMP, AT);
2123 __ LoadConst64(TMP, imm);
2124 __ Dmul(TMP, AT, TMP);
2125 __ Dsubu(out, dividend, TMP);
2126 }
2127 }
2128}
2129
2130void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2131 DCHECK(instruction->IsDiv() || instruction->IsRem());
2132 Primitive::Type type = instruction->GetResultType();
2133 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2134
2135 LocationSummary* locations = instruction->GetLocations();
2136 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2137 Location second = locations->InAt(1);
2138
2139 if (second.IsConstant()) {
2140 int64_t imm = Int64FromConstant(second.GetConstant());
2141 if (imm == 0) {
2142 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2143 } else if (imm == 1 || imm == -1) {
2144 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002145 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002146 DivRemByPowerOfTwo(instruction);
2147 } else {
2148 DCHECK(imm <= -2 || imm >= 2);
2149 GenerateDivRemWithAnyConstant(instruction);
2150 }
2151 } else {
2152 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2153 GpuRegister divisor = second.AsRegister<GpuRegister>();
2154 if (instruction->IsDiv()) {
2155 if (type == Primitive::kPrimInt)
2156 __ DivR6(out, dividend, divisor);
2157 else
2158 __ Ddiv(out, dividend, divisor);
2159 } else {
2160 if (type == Primitive::kPrimInt)
2161 __ ModR6(out, dividend, divisor);
2162 else
2163 __ Dmod(out, dividend, divisor);
2164 }
2165 }
2166}
2167
Alexey Frunze4dda3372015-06-01 18:31:49 -07002168void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2169 LocationSummary* locations =
2170 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2171 switch (div->GetResultType()) {
2172 case Primitive::kPrimInt:
2173 case Primitive::kPrimLong:
2174 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002175 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002176 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2177 break;
2178
2179 case Primitive::kPrimFloat:
2180 case Primitive::kPrimDouble:
2181 locations->SetInAt(0, Location::RequiresFpuRegister());
2182 locations->SetInAt(1, Location::RequiresFpuRegister());
2183 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2184 break;
2185
2186 default:
2187 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2188 }
2189}
2190
2191void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2192 Primitive::Type type = instruction->GetType();
2193 LocationSummary* locations = instruction->GetLocations();
2194
2195 switch (type) {
2196 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002197 case Primitive::kPrimLong:
2198 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002199 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002200 case Primitive::kPrimFloat:
2201 case Primitive::kPrimDouble: {
2202 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2203 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2204 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2205 if (type == Primitive::kPrimFloat)
2206 __ DivS(dst, lhs, rhs);
2207 else
2208 __ DivD(dst, lhs, rhs);
2209 break;
2210 }
2211 default:
2212 LOG(FATAL) << "Unexpected div type " << type;
2213 }
2214}
2215
2216void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002217 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2218 ? LocationSummary::kCallOnSlowPath
2219 : LocationSummary::kNoCall;
2220 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002221 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2222 if (instruction->HasUses()) {
2223 locations->SetOut(Location::SameAsFirstInput());
2224 }
2225}
2226
2227void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2228 SlowPathCodeMIPS64* slow_path =
2229 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2230 codegen_->AddSlowPath(slow_path);
2231 Location value = instruction->GetLocations()->InAt(0);
2232
2233 Primitive::Type type = instruction->GetType();
2234
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002235 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002236 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002237 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002238 }
2239
2240 if (value.IsConstant()) {
2241 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2242 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002243 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002244 } else {
2245 // A division by a non-null constant is valid. We don't need to perform
2246 // any check, so simply fall through.
2247 }
2248 } else {
2249 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2250 }
2251}
2252
2253void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2254 LocationSummary* locations =
2255 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2256 locations->SetOut(Location::ConstantLocation(constant));
2257}
2258
2259void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2260 // Will be generated at use site.
2261}
2262
2263void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2264 exit->SetLocations(nullptr);
2265}
2266
2267void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2268}
2269
2270void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2271 LocationSummary* locations =
2272 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2273 locations->SetOut(Location::ConstantLocation(constant));
2274}
2275
2276void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2277 // Will be generated at use site.
2278}
2279
David Brazdilfc6a86a2015-06-26 10:33:45 +00002280void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002281 DCHECK(!successor->IsExitBlock());
2282 HBasicBlock* block = got->GetBlock();
2283 HInstruction* previous = got->GetPrevious();
2284 HLoopInformation* info = block->GetLoopInformation();
2285
2286 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2287 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2288 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2289 return;
2290 }
2291 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2292 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2293 }
2294 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002295 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002296 }
2297}
2298
David Brazdilfc6a86a2015-06-26 10:33:45 +00002299void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2300 got->SetLocations(nullptr);
2301}
2302
2303void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2304 HandleGoto(got, got->GetSuccessor());
2305}
2306
2307void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2308 try_boundary->SetLocations(nullptr);
2309}
2310
2311void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2312 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2313 if (!successor->IsExitBlock()) {
2314 HandleGoto(try_boundary, successor);
2315 }
2316}
2317
Alexey Frunze299a9392015-12-08 16:08:02 -08002318void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2319 bool is64bit,
2320 LocationSummary* locations) {
2321 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2322 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2323 Location rhs_location = locations->InAt(1);
2324 GpuRegister rhs_reg = ZERO;
2325 int64_t rhs_imm = 0;
2326 bool use_imm = rhs_location.IsConstant();
2327 if (use_imm) {
2328 if (is64bit) {
2329 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2330 } else {
2331 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2332 }
2333 } else {
2334 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2335 }
2336 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2337
2338 switch (cond) {
2339 case kCondEQ:
2340 case kCondNE:
2341 if (use_imm && IsUint<16>(rhs_imm)) {
2342 __ Xori(dst, lhs, rhs_imm);
2343 } else {
2344 if (use_imm) {
2345 rhs_reg = TMP;
2346 __ LoadConst64(rhs_reg, rhs_imm);
2347 }
2348 __ Xor(dst, lhs, rhs_reg);
2349 }
2350 if (cond == kCondEQ) {
2351 __ Sltiu(dst, dst, 1);
2352 } else {
2353 __ Sltu(dst, ZERO, dst);
2354 }
2355 break;
2356
2357 case kCondLT:
2358 case kCondGE:
2359 if (use_imm && IsInt<16>(rhs_imm)) {
2360 __ Slti(dst, lhs, rhs_imm);
2361 } else {
2362 if (use_imm) {
2363 rhs_reg = TMP;
2364 __ LoadConst64(rhs_reg, rhs_imm);
2365 }
2366 __ Slt(dst, lhs, rhs_reg);
2367 }
2368 if (cond == kCondGE) {
2369 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2370 // only the slt instruction but no sge.
2371 __ Xori(dst, dst, 1);
2372 }
2373 break;
2374
2375 case kCondLE:
2376 case kCondGT:
2377 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2378 // Simulate lhs <= rhs via lhs < rhs + 1.
2379 __ Slti(dst, lhs, rhs_imm_plus_one);
2380 if (cond == kCondGT) {
2381 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2382 // only the slti instruction but no sgti.
2383 __ Xori(dst, dst, 1);
2384 }
2385 } else {
2386 if (use_imm) {
2387 rhs_reg = TMP;
2388 __ LoadConst64(rhs_reg, rhs_imm);
2389 }
2390 __ Slt(dst, rhs_reg, lhs);
2391 if (cond == kCondLE) {
2392 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2393 // only the slt instruction but no sle.
2394 __ Xori(dst, dst, 1);
2395 }
2396 }
2397 break;
2398
2399 case kCondB:
2400 case kCondAE:
2401 if (use_imm && IsInt<16>(rhs_imm)) {
2402 // Sltiu sign-extends its 16-bit immediate operand before
2403 // the comparison and thus lets us compare directly with
2404 // unsigned values in the ranges [0, 0x7fff] and
2405 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2406 __ Sltiu(dst, lhs, rhs_imm);
2407 } else {
2408 if (use_imm) {
2409 rhs_reg = TMP;
2410 __ LoadConst64(rhs_reg, rhs_imm);
2411 }
2412 __ Sltu(dst, lhs, rhs_reg);
2413 }
2414 if (cond == kCondAE) {
2415 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2416 // only the sltu instruction but no sgeu.
2417 __ Xori(dst, dst, 1);
2418 }
2419 break;
2420
2421 case kCondBE:
2422 case kCondA:
2423 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2424 // Simulate lhs <= rhs via lhs < rhs + 1.
2425 // Note that this only works if rhs + 1 does not overflow
2426 // to 0, hence the check above.
2427 // Sltiu sign-extends its 16-bit immediate operand before
2428 // the comparison and thus lets us compare directly with
2429 // unsigned values in the ranges [0, 0x7fff] and
2430 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2431 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2432 if (cond == kCondA) {
2433 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2434 // only the sltiu instruction but no sgtiu.
2435 __ Xori(dst, dst, 1);
2436 }
2437 } else {
2438 if (use_imm) {
2439 rhs_reg = TMP;
2440 __ LoadConst64(rhs_reg, rhs_imm);
2441 }
2442 __ Sltu(dst, rhs_reg, lhs);
2443 if (cond == kCondBE) {
2444 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2445 // only the sltu instruction but no sleu.
2446 __ Xori(dst, dst, 1);
2447 }
2448 }
2449 break;
2450 }
2451}
2452
2453void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2454 bool is64bit,
2455 LocationSummary* locations,
2456 Mips64Label* label) {
2457 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2458 Location rhs_location = locations->InAt(1);
2459 GpuRegister rhs_reg = ZERO;
2460 int64_t rhs_imm = 0;
2461 bool use_imm = rhs_location.IsConstant();
2462 if (use_imm) {
2463 if (is64bit) {
2464 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2465 } else {
2466 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2467 }
2468 } else {
2469 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2470 }
2471
2472 if (use_imm && rhs_imm == 0) {
2473 switch (cond) {
2474 case kCondEQ:
2475 case kCondBE: // <= 0 if zero
2476 __ Beqzc(lhs, label);
2477 break;
2478 case kCondNE:
2479 case kCondA: // > 0 if non-zero
2480 __ Bnezc(lhs, label);
2481 break;
2482 case kCondLT:
2483 __ Bltzc(lhs, label);
2484 break;
2485 case kCondGE:
2486 __ Bgezc(lhs, label);
2487 break;
2488 case kCondLE:
2489 __ Blezc(lhs, label);
2490 break;
2491 case kCondGT:
2492 __ Bgtzc(lhs, label);
2493 break;
2494 case kCondB: // always false
2495 break;
2496 case kCondAE: // always true
2497 __ Bc(label);
2498 break;
2499 }
2500 } else {
2501 if (use_imm) {
2502 rhs_reg = TMP;
2503 __ LoadConst64(rhs_reg, rhs_imm);
2504 }
2505 switch (cond) {
2506 case kCondEQ:
2507 __ Beqc(lhs, rhs_reg, label);
2508 break;
2509 case kCondNE:
2510 __ Bnec(lhs, rhs_reg, label);
2511 break;
2512 case kCondLT:
2513 __ Bltc(lhs, rhs_reg, label);
2514 break;
2515 case kCondGE:
2516 __ Bgec(lhs, rhs_reg, label);
2517 break;
2518 case kCondLE:
2519 __ Bgec(rhs_reg, lhs, label);
2520 break;
2521 case kCondGT:
2522 __ Bltc(rhs_reg, lhs, label);
2523 break;
2524 case kCondB:
2525 __ Bltuc(lhs, rhs_reg, label);
2526 break;
2527 case kCondAE:
2528 __ Bgeuc(lhs, rhs_reg, label);
2529 break;
2530 case kCondBE:
2531 __ Bgeuc(rhs_reg, lhs, label);
2532 break;
2533 case kCondA:
2534 __ Bltuc(rhs_reg, lhs, label);
2535 break;
2536 }
2537 }
2538}
2539
2540void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2541 bool gt_bias,
2542 Primitive::Type type,
2543 LocationSummary* locations,
2544 Mips64Label* label) {
2545 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2546 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2547 if (type == Primitive::kPrimFloat) {
2548 switch (cond) {
2549 case kCondEQ:
2550 __ CmpEqS(FTMP, lhs, rhs);
2551 __ Bc1nez(FTMP, label);
2552 break;
2553 case kCondNE:
2554 __ CmpEqS(FTMP, lhs, rhs);
2555 __ Bc1eqz(FTMP, label);
2556 break;
2557 case kCondLT:
2558 if (gt_bias) {
2559 __ CmpLtS(FTMP, lhs, rhs);
2560 } else {
2561 __ CmpUltS(FTMP, lhs, rhs);
2562 }
2563 __ Bc1nez(FTMP, label);
2564 break;
2565 case kCondLE:
2566 if (gt_bias) {
2567 __ CmpLeS(FTMP, lhs, rhs);
2568 } else {
2569 __ CmpUleS(FTMP, lhs, rhs);
2570 }
2571 __ Bc1nez(FTMP, label);
2572 break;
2573 case kCondGT:
2574 if (gt_bias) {
2575 __ CmpUltS(FTMP, rhs, lhs);
2576 } else {
2577 __ CmpLtS(FTMP, rhs, lhs);
2578 }
2579 __ Bc1nez(FTMP, label);
2580 break;
2581 case kCondGE:
2582 if (gt_bias) {
2583 __ CmpUleS(FTMP, rhs, lhs);
2584 } else {
2585 __ CmpLeS(FTMP, rhs, lhs);
2586 }
2587 __ Bc1nez(FTMP, label);
2588 break;
2589 default:
2590 LOG(FATAL) << "Unexpected non-floating-point condition";
2591 }
2592 } else {
2593 DCHECK_EQ(type, Primitive::kPrimDouble);
2594 switch (cond) {
2595 case kCondEQ:
2596 __ CmpEqD(FTMP, lhs, rhs);
2597 __ Bc1nez(FTMP, label);
2598 break;
2599 case kCondNE:
2600 __ CmpEqD(FTMP, lhs, rhs);
2601 __ Bc1eqz(FTMP, label);
2602 break;
2603 case kCondLT:
2604 if (gt_bias) {
2605 __ CmpLtD(FTMP, lhs, rhs);
2606 } else {
2607 __ CmpUltD(FTMP, lhs, rhs);
2608 }
2609 __ Bc1nez(FTMP, label);
2610 break;
2611 case kCondLE:
2612 if (gt_bias) {
2613 __ CmpLeD(FTMP, lhs, rhs);
2614 } else {
2615 __ CmpUleD(FTMP, lhs, rhs);
2616 }
2617 __ Bc1nez(FTMP, label);
2618 break;
2619 case kCondGT:
2620 if (gt_bias) {
2621 __ CmpUltD(FTMP, rhs, lhs);
2622 } else {
2623 __ CmpLtD(FTMP, rhs, lhs);
2624 }
2625 __ Bc1nez(FTMP, label);
2626 break;
2627 case kCondGE:
2628 if (gt_bias) {
2629 __ CmpUleD(FTMP, rhs, lhs);
2630 } else {
2631 __ CmpLeD(FTMP, rhs, lhs);
2632 }
2633 __ Bc1nez(FTMP, label);
2634 break;
2635 default:
2636 LOG(FATAL) << "Unexpected non-floating-point condition";
2637 }
2638 }
2639}
2640
Alexey Frunze4dda3372015-06-01 18:31:49 -07002641void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002642 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002643 Mips64Label* true_target,
2644 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002645 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002646
David Brazdil0debae72015-11-12 18:37:00 +00002647 if (true_target == nullptr && false_target == nullptr) {
2648 // Nothing to do. The code always falls through.
2649 return;
2650 } else if (cond->IsIntConstant()) {
2651 // Constant condition, statically compared against 1.
2652 if (cond->AsIntConstant()->IsOne()) {
2653 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002654 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002655 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002656 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002657 DCHECK(cond->AsIntConstant()->IsZero());
2658 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002659 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002660 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002661 }
David Brazdil0debae72015-11-12 18:37:00 +00002662 return;
2663 }
2664
2665 // The following code generates these patterns:
2666 // (1) true_target == nullptr && false_target != nullptr
2667 // - opposite condition true => branch to false_target
2668 // (2) true_target != nullptr && false_target == nullptr
2669 // - condition true => branch to true_target
2670 // (3) true_target != nullptr && false_target != nullptr
2671 // - condition true => branch to true_target
2672 // - branch to false_target
2673 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002674 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002675 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002676 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002677 if (true_target == nullptr) {
2678 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2679 } else {
2680 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2681 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002682 } else {
2683 // The condition instruction has not been materialized, use its inputs as
2684 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002685 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002686 Primitive::Type type = condition->InputAt(0)->GetType();
2687 LocationSummary* locations = cond->GetLocations();
2688 IfCondition if_cond = condition->GetCondition();
2689 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002690
David Brazdil0debae72015-11-12 18:37:00 +00002691 if (true_target == nullptr) {
2692 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002693 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002694 }
2695
Alexey Frunze299a9392015-12-08 16:08:02 -08002696 switch (type) {
2697 default:
2698 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2699 break;
2700 case Primitive::kPrimLong:
2701 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2702 break;
2703 case Primitive::kPrimFloat:
2704 case Primitive::kPrimDouble:
2705 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2706 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002707 }
2708 }
David Brazdil0debae72015-11-12 18:37:00 +00002709
2710 // If neither branch falls through (case 3), the conditional branch to `true_target`
2711 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2712 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002713 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002714 }
2715}
2716
2717void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002719 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002720 locations->SetInAt(0, Location::RequiresRegister());
2721 }
2722}
2723
2724void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002725 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2726 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002727 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002728 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002729 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002730 nullptr : codegen_->GetLabelOf(false_successor);
2731 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002732}
2733
2734void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2735 LocationSummary* locations = new (GetGraph()->GetArena())
2736 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002737 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002738 locations->SetInAt(0, Location::RequiresRegister());
2739 }
2740}
2741
2742void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002743 SlowPathCodeMIPS64* slow_path =
2744 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002745 GenerateTestAndBranch(deoptimize,
2746 /* condition_input_index */ 0,
2747 slow_path->GetEntryLabel(),
2748 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002749}
2750
David Srbecky0cf44932015-12-09 14:09:59 +00002751void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2752 new (GetGraph()->GetArena()) LocationSummary(info);
2753}
2754
2755void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002756 if (codegen_->HasStackMapAtCurrentPc()) {
2757 // Ensure that we do not collide with the stack map of the previous instruction.
2758 __ Nop();
2759 }
David Srbecky0cf44932015-12-09 14:09:59 +00002760 codegen_->RecordPcInfo(info, info->GetDexPc());
2761}
2762
Alexey Frunze4dda3372015-06-01 18:31:49 -07002763void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2764 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2765 LocationSummary* locations =
2766 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2767 locations->SetInAt(0, Location::RequiresRegister());
2768 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2769 locations->SetOut(Location::RequiresFpuRegister());
2770 } else {
2771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2772 }
2773}
2774
2775void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2776 const FieldInfo& field_info) {
2777 Primitive::Type type = field_info.GetFieldType();
2778 LocationSummary* locations = instruction->GetLocations();
2779 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2780 LoadOperandType load_type = kLoadUnsignedByte;
2781 switch (type) {
2782 case Primitive::kPrimBoolean:
2783 load_type = kLoadUnsignedByte;
2784 break;
2785 case Primitive::kPrimByte:
2786 load_type = kLoadSignedByte;
2787 break;
2788 case Primitive::kPrimShort:
2789 load_type = kLoadSignedHalfword;
2790 break;
2791 case Primitive::kPrimChar:
2792 load_type = kLoadUnsignedHalfword;
2793 break;
2794 case Primitive::kPrimInt:
2795 case Primitive::kPrimFloat:
2796 load_type = kLoadWord;
2797 break;
2798 case Primitive::kPrimLong:
2799 case Primitive::kPrimDouble:
2800 load_type = kLoadDoubleword;
2801 break;
2802 case Primitive::kPrimNot:
2803 load_type = kLoadUnsignedWord;
2804 break;
2805 case Primitive::kPrimVoid:
2806 LOG(FATAL) << "Unreachable type " << type;
2807 UNREACHABLE();
2808 }
2809 if (!Primitive::IsFloatingPointType(type)) {
2810 DCHECK(locations->Out().IsRegister());
2811 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2812 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2813 } else {
2814 DCHECK(locations->Out().IsFpuRegister());
2815 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2816 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2817 }
2818
2819 codegen_->MaybeRecordImplicitNullCheck(instruction);
2820 // TODO: memory barrier?
2821}
2822
2823void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2824 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2825 LocationSummary* locations =
2826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2827 locations->SetInAt(0, Location::RequiresRegister());
2828 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2829 locations->SetInAt(1, Location::RequiresFpuRegister());
2830 } else {
2831 locations->SetInAt(1, Location::RequiresRegister());
2832 }
2833}
2834
2835void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002836 const FieldInfo& field_info,
2837 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002838 Primitive::Type type = field_info.GetFieldType();
2839 LocationSummary* locations = instruction->GetLocations();
2840 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2841 StoreOperandType store_type = kStoreByte;
2842 switch (type) {
2843 case Primitive::kPrimBoolean:
2844 case Primitive::kPrimByte:
2845 store_type = kStoreByte;
2846 break;
2847 case Primitive::kPrimShort:
2848 case Primitive::kPrimChar:
2849 store_type = kStoreHalfword;
2850 break;
2851 case Primitive::kPrimInt:
2852 case Primitive::kPrimFloat:
2853 case Primitive::kPrimNot:
2854 store_type = kStoreWord;
2855 break;
2856 case Primitive::kPrimLong:
2857 case Primitive::kPrimDouble:
2858 store_type = kStoreDoubleword;
2859 break;
2860 case Primitive::kPrimVoid:
2861 LOG(FATAL) << "Unreachable type " << type;
2862 UNREACHABLE();
2863 }
2864 if (!Primitive::IsFloatingPointType(type)) {
2865 DCHECK(locations->InAt(1).IsRegister());
2866 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2867 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2868 } else {
2869 DCHECK(locations->InAt(1).IsFpuRegister());
2870 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2871 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2872 }
2873
2874 codegen_->MaybeRecordImplicitNullCheck(instruction);
2875 // TODO: memory barriers?
2876 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2877 DCHECK(locations->InAt(1).IsRegister());
2878 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002879 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002880 }
2881}
2882
2883void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2884 HandleFieldGet(instruction, instruction->GetFieldInfo());
2885}
2886
2887void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2888 HandleFieldGet(instruction, instruction->GetFieldInfo());
2889}
2890
2891void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2892 HandleFieldSet(instruction, instruction->GetFieldInfo());
2893}
2894
2895void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002896 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002897}
2898
2899void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2900 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002901 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2903 locations->SetInAt(0, Location::RequiresRegister());
2904 locations->SetInAt(1, Location::RequiresRegister());
2905 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002906 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002907 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2908}
2909
2910void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2911 LocationSummary* locations = instruction->GetLocations();
2912 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2913 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2914 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2915
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002916 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002917
2918 // Return 0 if `obj` is null.
2919 // TODO: Avoid this check if we know `obj` is not null.
2920 __ Move(out, ZERO);
2921 __ Beqzc(obj, &done);
2922
2923 // Compare the class of `obj` with `cls`.
2924 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002925 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002926 // Classes must be equal for the instanceof to succeed.
2927 __ Xor(out, out, cls);
2928 __ Sltiu(out, out, 1);
2929 } else {
2930 // If the classes are not equal, we go into a slow path.
2931 DCHECK(locations->OnlyCallsOnSlowPath());
2932 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002933 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002934 codegen_->AddSlowPath(slow_path);
2935 __ Bnec(out, cls, slow_path->GetEntryLabel());
2936 __ LoadConst32(out, 1);
2937 __ Bind(slow_path->GetExitLabel());
2938 }
2939
2940 __ Bind(&done);
2941}
2942
2943void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2945 locations->SetOut(Location::ConstantLocation(constant));
2946}
2947
2948void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2949 // Will be generated at use site.
2950}
2951
2952void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2953 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2954 locations->SetOut(Location::ConstantLocation(constant));
2955}
2956
2957void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2958 // Will be generated at use site.
2959}
2960
Calin Juravle175dc732015-08-25 15:42:32 +01002961void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2962 // The trampoline uses the same calling convention as dex calling conventions,
2963 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2964 // the method_idx.
2965 HandleInvoke(invoke);
2966}
2967
2968void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2969 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2970}
2971
Alexey Frunze4dda3372015-06-01 18:31:49 -07002972void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2973 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2974 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2975}
2976
2977void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2978 HandleInvoke(invoke);
2979 // The register T0 is required to be used for the hidden argument in
2980 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2981 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2982}
2983
2984void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2985 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2986 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2987 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2988 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2989 Location receiver = invoke->GetLocations()->InAt(0);
2990 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2991 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
2992
2993 // Set the hidden argument.
2994 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2995 invoke->GetDexMethodIndex());
2996
2997 // temp = object->GetClass();
2998 if (receiver.IsStackSlot()) {
2999 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3000 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3001 } else {
3002 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3003 }
3004 codegen_->MaybeRecordImplicitNullCheck(invoke);
3005 // temp = temp->GetImtEntryAt(method_offset);
3006 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3007 // T9 = temp->GetEntryPoint();
3008 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3009 // T9();
3010 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003011 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003012 DCHECK(!codegen_->IsLeafMethod());
3013 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3014}
3015
3016void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003017 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3018 if (intrinsic.TryDispatch(invoke)) {
3019 return;
3020 }
3021
Alexey Frunze4dda3372015-06-01 18:31:49 -07003022 HandleInvoke(invoke);
3023}
3024
3025void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003026 // Explicit clinit checks triggered by static invokes must have been pruned by
3027 // art::PrepareForRegisterAllocation.
3028 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003029
Chris Larsen3039e382015-08-26 07:54:08 -07003030 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3031 if (intrinsic.TryDispatch(invoke)) {
3032 return;
3033 }
3034
Alexey Frunze4dda3372015-06-01 18:31:49 -07003035 HandleInvoke(invoke);
3036
3037 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
3038 // clobbering somewhere else, reduce further register pressure by avoiding
3039 // allocation of a register for the current method pointer like on x86 baseline.
3040 // TODO: remove this once all the issues with register saving/restoring are
3041 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003042 if (invoke->HasCurrentMethodInput()) {
3043 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00003044 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003045 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003046 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003047 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003048 }
3049}
3050
Chris Larsen3039e382015-08-26 07:54:08 -07003051static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003052 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003053 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3054 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003055 return true;
3056 }
3057 return false;
3058}
3059
Vladimir Markodc151b22015-10-15 18:02:30 +01003060HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3061 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3062 MethodReference target_method ATTRIBUTE_UNUSED) {
3063 switch (desired_dispatch_info.method_load_kind) {
3064 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3065 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3066 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3067 return HInvokeStaticOrDirect::DispatchInfo {
3068 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3069 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3070 0u,
3071 0u
3072 };
3073 default:
3074 break;
3075 }
3076 switch (desired_dispatch_info.code_ptr_location) {
3077 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3078 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3079 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3080 return HInvokeStaticOrDirect::DispatchInfo {
3081 desired_dispatch_info.method_load_kind,
3082 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3083 desired_dispatch_info.method_load_data,
3084 0u
3085 };
3086 default:
3087 return desired_dispatch_info;
3088 }
3089}
3090
Alexey Frunze4dda3372015-06-01 18:31:49 -07003091void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3092 // All registers are assumed to be correctly set up per the calling convention.
3093
Vladimir Marko58155012015-08-19 12:49:41 +00003094 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3095 switch (invoke->GetMethodLoadKind()) {
3096 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3097 // temp = thread->string_init_entrypoint
3098 __ LoadFromOffset(kLoadDoubleword,
3099 temp.AsRegister<GpuRegister>(),
3100 TR,
3101 invoke->GetStringInitOffset());
3102 break;
3103 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003104 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003105 break;
3106 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3107 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3108 break;
3109 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003110 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003111 // TODO: Implement these types.
3112 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3113 LOG(FATAL) << "Unsupported";
3114 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003115 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003116 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003117 GpuRegister reg = temp.AsRegister<GpuRegister>();
3118 GpuRegister method_reg;
3119 if (current_method.IsRegister()) {
3120 method_reg = current_method.AsRegister<GpuRegister>();
3121 } else {
3122 // TODO: use the appropriate DCHECK() here if possible.
3123 // DCHECK(invoke->GetLocations()->Intrinsified());
3124 DCHECK(!current_method.IsValid());
3125 method_reg = reg;
3126 __ Ld(reg, SP, kCurrentMethodStackOffset);
3127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003128
Vladimir Marko58155012015-08-19 12:49:41 +00003129 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003130 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003131 reg,
3132 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003133 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003134 // temp = temp[index_in_cache]
3135 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3136 __ LoadFromOffset(kLoadDoubleword,
3137 reg,
3138 reg,
3139 CodeGenerator::GetCachePointerOffset(index_in_cache));
3140 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003141 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003142 }
3143
Vladimir Marko58155012015-08-19 12:49:41 +00003144 switch (invoke->GetCodePtrLocation()) {
3145 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003146 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003147 break;
3148 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3149 // LR = invoke->GetDirectCodePtr();
3150 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3151 // LR()
3152 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003153 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003154 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003155 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003156 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3157 // TODO: Implement these types.
3158 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3159 LOG(FATAL) << "Unsupported";
3160 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003161 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3162 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3163 __ LoadFromOffset(kLoadDoubleword,
3164 T9,
3165 callee_method.AsRegister<GpuRegister>(),
3166 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3167 kMips64WordSize).Int32Value());
3168 // T9()
3169 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003170 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003171 break;
3172 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003173 DCHECK(!IsLeafMethod());
3174}
3175
3176void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003177 // Explicit clinit checks triggered by static invokes must have been pruned by
3178 // art::PrepareForRegisterAllocation.
3179 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003180
3181 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3182 return;
3183 }
3184
3185 LocationSummary* locations = invoke->GetLocations();
3186 codegen_->GenerateStaticOrDirectCall(invoke,
3187 locations->HasTemps()
3188 ? locations->GetTemp(0)
3189 : Location::NoLocation());
3190 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3191}
3192
Alexey Frunze53afca12015-11-05 16:34:23 -08003193void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003194 // Use the calling convention instead of the location of the receiver, as
3195 // intrinsics may have put the receiver in a different register. In the intrinsics
3196 // slow path, the arguments have been moved to the right place, so here we are
3197 // guaranteed that the receiver is the first register of the calling convention.
3198 InvokeDexCallingConvention calling_convention;
3199 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3200
Alexey Frunze53afca12015-11-05 16:34:23 -08003201 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003202 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3203 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3204 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3205 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
3206
3207 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003208 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003209 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003210 // temp = temp->GetMethodAt(method_offset);
3211 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3212 // T9 = temp->GetEntryPoint();
3213 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3214 // T9();
3215 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003216 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003217}
3218
3219void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3220 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3221 return;
3222 }
3223
3224 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003225 DCHECK(!codegen_->IsLeafMethod());
3226 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3227}
3228
3229void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003230 InvokeRuntimeCallingConvention calling_convention;
3231 CodeGenerator::CreateLoadClassLocationSummary(
3232 cls,
3233 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003234 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003235}
3236
3237void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3238 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003239 if (cls->NeedsAccessCheck()) {
3240 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3241 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3242 cls,
3243 cls->GetDexPc(),
3244 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003245 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003246 return;
3247 }
3248
3249 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3250 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3251 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003252 DCHECK(!cls->CanCallRuntime());
3253 DCHECK(!cls->MustGenerateClinitCheck());
3254 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3255 ArtMethod::DeclaringClassOffset().Int32Value());
3256 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003257 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3258 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003259 __ LoadFromOffset(
3260 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003261 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003262 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3263 DCHECK(cls->CanCallRuntime());
3264 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3265 cls,
3266 cls,
3267 cls->GetDexPc(),
3268 cls->MustGenerateClinitCheck());
3269 codegen_->AddSlowPath(slow_path);
3270 if (!cls->IsInDexCache()) {
3271 __ Beqzc(out, slow_path->GetEntryLabel());
3272 }
3273 if (cls->MustGenerateClinitCheck()) {
3274 GenerateClassInitializationCheck(slow_path, out);
3275 } else {
3276 __ Bind(slow_path->GetExitLabel());
3277 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003278 }
3279 }
3280}
3281
David Brazdilcb1c0552015-08-04 16:22:25 +01003282static int32_t GetExceptionTlsOffset() {
3283 return Thread::ExceptionOffset<kMips64WordSize>().Int32Value();
3284}
3285
Alexey Frunze4dda3372015-06-01 18:31:49 -07003286void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3287 LocationSummary* locations =
3288 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3289 locations->SetOut(Location::RequiresRegister());
3290}
3291
3292void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3293 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003294 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3295}
3296
3297void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3298 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3299}
3300
3301void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3302 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003303}
3304
3305void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3306 load->SetLocations(nullptr);
3307}
3308
3309void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3310 // Nothing to do, this is driven by the code generator.
3311}
3312
3313void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00003314 LocationSummary::CallKind call_kind = load->IsInDexCache()
3315 ? LocationSummary::kNoCall
3316 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003318 locations->SetInAt(0, Location::RequiresRegister());
3319 locations->SetOut(Location::RequiresRegister());
3320}
3321
3322void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003323 LocationSummary* locations = load->GetLocations();
3324 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3325 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3326 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3327 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003328 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003329 __ LoadFromOffset(
3330 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003331 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003332
3333 if (!load->IsInDexCache()) {
3334 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3335 codegen_->AddSlowPath(slow_path);
3336 __ Beqzc(out, slow_path->GetEntryLabel());
3337 __ Bind(slow_path->GetExitLabel());
3338 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003339}
3340
3341void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3342 local->SetLocations(nullptr);
3343}
3344
3345void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3346 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3347}
3348
3349void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3351 locations->SetOut(Location::ConstantLocation(constant));
3352}
3353
3354void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3355 // Will be generated at use site.
3356}
3357
3358void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3359 LocationSummary* locations =
3360 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3361 InvokeRuntimeCallingConvention calling_convention;
3362 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3363}
3364
3365void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3366 codegen_->InvokeRuntime(instruction->IsEnter()
3367 ? QUICK_ENTRY_POINT(pLockObject)
3368 : QUICK_ENTRY_POINT(pUnlockObject),
3369 instruction,
3370 instruction->GetDexPc(),
3371 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003372 if (instruction->IsEnter()) {
3373 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3374 } else {
3375 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3376 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003377}
3378
3379void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3380 LocationSummary* locations =
3381 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3382 switch (mul->GetResultType()) {
3383 case Primitive::kPrimInt:
3384 case Primitive::kPrimLong:
3385 locations->SetInAt(0, Location::RequiresRegister());
3386 locations->SetInAt(1, Location::RequiresRegister());
3387 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3388 break;
3389
3390 case Primitive::kPrimFloat:
3391 case Primitive::kPrimDouble:
3392 locations->SetInAt(0, Location::RequiresFpuRegister());
3393 locations->SetInAt(1, Location::RequiresFpuRegister());
3394 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3395 break;
3396
3397 default:
3398 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3399 }
3400}
3401
3402void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3403 Primitive::Type type = instruction->GetType();
3404 LocationSummary* locations = instruction->GetLocations();
3405
3406 switch (type) {
3407 case Primitive::kPrimInt:
3408 case Primitive::kPrimLong: {
3409 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3410 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3411 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3412 if (type == Primitive::kPrimInt)
3413 __ MulR6(dst, lhs, rhs);
3414 else
3415 __ Dmul(dst, lhs, rhs);
3416 break;
3417 }
3418 case Primitive::kPrimFloat:
3419 case Primitive::kPrimDouble: {
3420 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3421 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3422 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3423 if (type == Primitive::kPrimFloat)
3424 __ MulS(dst, lhs, rhs);
3425 else
3426 __ MulD(dst, lhs, rhs);
3427 break;
3428 }
3429 default:
3430 LOG(FATAL) << "Unexpected mul type " << type;
3431 }
3432}
3433
3434void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3435 LocationSummary* locations =
3436 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3437 switch (neg->GetResultType()) {
3438 case Primitive::kPrimInt:
3439 case Primitive::kPrimLong:
3440 locations->SetInAt(0, Location::RequiresRegister());
3441 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3442 break;
3443
3444 case Primitive::kPrimFloat:
3445 case Primitive::kPrimDouble:
3446 locations->SetInAt(0, Location::RequiresFpuRegister());
3447 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3448 break;
3449
3450 default:
3451 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3452 }
3453}
3454
3455void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3456 Primitive::Type type = instruction->GetType();
3457 LocationSummary* locations = instruction->GetLocations();
3458
3459 switch (type) {
3460 case Primitive::kPrimInt:
3461 case Primitive::kPrimLong: {
3462 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3463 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3464 if (type == Primitive::kPrimInt)
3465 __ Subu(dst, ZERO, src);
3466 else
3467 __ Dsubu(dst, ZERO, src);
3468 break;
3469 }
3470 case Primitive::kPrimFloat:
3471 case Primitive::kPrimDouble: {
3472 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3473 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3474 if (type == Primitive::kPrimFloat)
3475 __ NegS(dst, src);
3476 else
3477 __ NegD(dst, src);
3478 break;
3479 }
3480 default:
3481 LOG(FATAL) << "Unexpected neg type " << type;
3482 }
3483}
3484
3485void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3486 LocationSummary* locations =
3487 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3488 InvokeRuntimeCallingConvention calling_convention;
3489 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3490 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3491 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3492 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3493}
3494
3495void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3496 LocationSummary* locations = instruction->GetLocations();
3497 // Move an uint16_t value to a register.
3498 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003499 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3500 instruction,
3501 instruction->GetDexPc(),
3502 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003503 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3504}
3505
3506void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3507 LocationSummary* locations =
3508 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3509 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003510 if (instruction->IsStringAlloc()) {
3511 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3512 } else {
3513 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3514 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3515 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003516 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3517}
3518
3519void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003520 if (instruction->IsStringAlloc()) {
3521 // String is allocated through StringFactory. Call NewEmptyString entry point.
3522 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
3523 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
3524 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3525 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3526 __ Jalr(T9);
3527 __ Nop();
3528 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3529 } else {
3530 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3531 instruction,
3532 instruction->GetDexPc(),
3533 nullptr);
3534 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3535 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003536}
3537
3538void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3539 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3540 locations->SetInAt(0, Location::RequiresRegister());
3541 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3542}
3543
3544void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3545 Primitive::Type type = instruction->GetType();
3546 LocationSummary* locations = instruction->GetLocations();
3547
3548 switch (type) {
3549 case Primitive::kPrimInt:
3550 case Primitive::kPrimLong: {
3551 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3552 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3553 __ Nor(dst, src, ZERO);
3554 break;
3555 }
3556
3557 default:
3558 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3559 }
3560}
3561
3562void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3564 locations->SetInAt(0, Location::RequiresRegister());
3565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3566}
3567
3568void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3569 LocationSummary* locations = instruction->GetLocations();
3570 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3571 locations->InAt(0).AsRegister<GpuRegister>(),
3572 1);
3573}
3574
3575void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003576 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3577 ? LocationSummary::kCallOnSlowPath
3578 : LocationSummary::kNoCall;
3579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003580 locations->SetInAt(0, Location::RequiresRegister());
3581 if (instruction->HasUses()) {
3582 locations->SetOut(Location::SameAsFirstInput());
3583 }
3584}
3585
3586void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3587 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3588 return;
3589 }
3590 Location obj = instruction->GetLocations()->InAt(0);
3591
3592 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3593 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3594}
3595
3596void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3597 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3598 codegen_->AddSlowPath(slow_path);
3599
3600 Location obj = instruction->GetLocations()->InAt(0);
3601
3602 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3603}
3604
3605void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003606 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003607 GenerateImplicitNullCheck(instruction);
3608 } else {
3609 GenerateExplicitNullCheck(instruction);
3610 }
3611}
3612
3613void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3614 HandleBinaryOp(instruction);
3615}
3616
3617void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3618 HandleBinaryOp(instruction);
3619}
3620
3621void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3622 LOG(FATAL) << "Unreachable";
3623}
3624
3625void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3626 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3627}
3628
3629void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3630 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3631 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3632 if (location.IsStackSlot()) {
3633 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3634 } else if (location.IsDoubleStackSlot()) {
3635 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3636 }
3637 locations->SetOut(location);
3638}
3639
3640void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3641 ATTRIBUTE_UNUSED) {
3642 // Nothing to do, the parameter is already at its location.
3643}
3644
3645void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3646 LocationSummary* locations =
3647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3648 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3649}
3650
3651void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3652 ATTRIBUTE_UNUSED) {
3653 // Nothing to do, the method is already at its location.
3654}
3655
3656void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3657 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3658 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3659 locations->SetInAt(i, Location::Any());
3660 }
3661 locations->SetOut(Location::Any());
3662}
3663
3664void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3665 LOG(FATAL) << "Unreachable";
3666}
3667
3668void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3669 Primitive::Type type = rem->GetResultType();
3670 LocationSummary::CallKind call_kind =
3671 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3673
3674 switch (type) {
3675 case Primitive::kPrimInt:
3676 case Primitive::kPrimLong:
3677 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003678 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3680 break;
3681
3682 case Primitive::kPrimFloat:
3683 case Primitive::kPrimDouble: {
3684 InvokeRuntimeCallingConvention calling_convention;
3685 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3686 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3687 locations->SetOut(calling_convention.GetReturnLocation(type));
3688 break;
3689 }
3690
3691 default:
3692 LOG(FATAL) << "Unexpected rem type " << type;
3693 }
3694}
3695
3696void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3697 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003698
3699 switch (type) {
3700 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003701 case Primitive::kPrimLong:
3702 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003703 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003704
3705 case Primitive::kPrimFloat:
3706 case Primitive::kPrimDouble: {
3707 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3708 : QUICK_ENTRY_POINT(pFmod);
3709 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003710 if (type == Primitive::kPrimFloat) {
3711 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3712 } else {
3713 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3714 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003715 break;
3716 }
3717 default:
3718 LOG(FATAL) << "Unexpected rem type " << type;
3719 }
3720}
3721
3722void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3723 memory_barrier->SetLocations(nullptr);
3724}
3725
3726void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3727 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3728}
3729
3730void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3732 Primitive::Type return_type = ret->InputAt(0)->GetType();
3733 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3734}
3735
3736void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3737 codegen_->GenerateFrameExit();
3738}
3739
3740void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3741 ret->SetLocations(nullptr);
3742}
3743
3744void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3745 codegen_->GenerateFrameExit();
3746}
3747
Alexey Frunze92d90602015-12-18 18:16:36 -08003748void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3749 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003750}
3751
Alexey Frunze92d90602015-12-18 18:16:36 -08003752void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3753 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003754}
3755
Alexey Frunze4dda3372015-06-01 18:31:49 -07003756void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3757 HandleShift(shl);
3758}
3759
3760void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3761 HandleShift(shl);
3762}
3763
3764void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3765 HandleShift(shr);
3766}
3767
3768void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3769 HandleShift(shr);
3770}
3771
3772void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3773 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3774 Primitive::Type field_type = store->InputAt(1)->GetType();
3775 switch (field_type) {
3776 case Primitive::kPrimNot:
3777 case Primitive::kPrimBoolean:
3778 case Primitive::kPrimByte:
3779 case Primitive::kPrimChar:
3780 case Primitive::kPrimShort:
3781 case Primitive::kPrimInt:
3782 case Primitive::kPrimFloat:
3783 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3784 break;
3785
3786 case Primitive::kPrimLong:
3787 case Primitive::kPrimDouble:
3788 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3789 break;
3790
3791 default:
3792 LOG(FATAL) << "Unimplemented local type " << field_type;
3793 }
3794}
3795
3796void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3797}
3798
3799void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3800 HandleBinaryOp(instruction);
3801}
3802
3803void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3804 HandleBinaryOp(instruction);
3805}
3806
3807void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3808 HandleFieldGet(instruction, instruction->GetFieldInfo());
3809}
3810
3811void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3812 HandleFieldGet(instruction, instruction->GetFieldInfo());
3813}
3814
3815void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3816 HandleFieldSet(instruction, instruction->GetFieldInfo());
3817}
3818
3819void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003820 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003821}
3822
Calin Juravlee460d1d2015-09-29 04:52:17 +01003823void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3824 HUnresolvedInstanceFieldGet* instruction) {
3825 FieldAccessCallingConventionMIPS64 calling_convention;
3826 codegen_->CreateUnresolvedFieldLocationSummary(
3827 instruction, instruction->GetFieldType(), calling_convention);
3828}
3829
3830void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3831 HUnresolvedInstanceFieldGet* instruction) {
3832 FieldAccessCallingConventionMIPS64 calling_convention;
3833 codegen_->GenerateUnresolvedFieldAccess(instruction,
3834 instruction->GetFieldType(),
3835 instruction->GetFieldIndex(),
3836 instruction->GetDexPc(),
3837 calling_convention);
3838}
3839
3840void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3841 HUnresolvedInstanceFieldSet* instruction) {
3842 FieldAccessCallingConventionMIPS64 calling_convention;
3843 codegen_->CreateUnresolvedFieldLocationSummary(
3844 instruction, instruction->GetFieldType(), calling_convention);
3845}
3846
3847void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3848 HUnresolvedInstanceFieldSet* instruction) {
3849 FieldAccessCallingConventionMIPS64 calling_convention;
3850 codegen_->GenerateUnresolvedFieldAccess(instruction,
3851 instruction->GetFieldType(),
3852 instruction->GetFieldIndex(),
3853 instruction->GetDexPc(),
3854 calling_convention);
3855}
3856
3857void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3858 HUnresolvedStaticFieldGet* instruction) {
3859 FieldAccessCallingConventionMIPS64 calling_convention;
3860 codegen_->CreateUnresolvedFieldLocationSummary(
3861 instruction, instruction->GetFieldType(), calling_convention);
3862}
3863
3864void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3865 HUnresolvedStaticFieldGet* instruction) {
3866 FieldAccessCallingConventionMIPS64 calling_convention;
3867 codegen_->GenerateUnresolvedFieldAccess(instruction,
3868 instruction->GetFieldType(),
3869 instruction->GetFieldIndex(),
3870 instruction->GetDexPc(),
3871 calling_convention);
3872}
3873
3874void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3875 HUnresolvedStaticFieldSet* instruction) {
3876 FieldAccessCallingConventionMIPS64 calling_convention;
3877 codegen_->CreateUnresolvedFieldLocationSummary(
3878 instruction, instruction->GetFieldType(), calling_convention);
3879}
3880
3881void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3882 HUnresolvedStaticFieldSet* instruction) {
3883 FieldAccessCallingConventionMIPS64 calling_convention;
3884 codegen_->GenerateUnresolvedFieldAccess(instruction,
3885 instruction->GetFieldType(),
3886 instruction->GetFieldIndex(),
3887 instruction->GetDexPc(),
3888 calling_convention);
3889}
3890
Alexey Frunze4dda3372015-06-01 18:31:49 -07003891void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3893}
3894
3895void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3896 HBasicBlock* block = instruction->GetBlock();
3897 if (block->GetLoopInformation() != nullptr) {
3898 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3899 // The back edge will generate the suspend check.
3900 return;
3901 }
3902 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3903 // The goto will generate the suspend check.
3904 return;
3905 }
3906 GenerateSuspendCheck(instruction, nullptr);
3907}
3908
3909void LocationsBuilderMIPS64::VisitTemporary(HTemporary* temp) {
3910 temp->SetLocations(nullptr);
3911}
3912
3913void InstructionCodeGeneratorMIPS64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3914 // Nothing to do, this is driven by the code generator.
3915}
3916
3917void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3918 LocationSummary* locations =
3919 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3920 InvokeRuntimeCallingConvention calling_convention;
3921 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3922}
3923
3924void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3925 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3926 instruction,
3927 instruction->GetDexPc(),
3928 nullptr);
3929 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3930}
3931
3932void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3933 Primitive::Type input_type = conversion->GetInputType();
3934 Primitive::Type result_type = conversion->GetResultType();
3935 DCHECK_NE(input_type, result_type);
3936
3937 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3938 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3939 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3940 }
3941
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003942 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3943
3944 if (Primitive::IsFloatingPointType(input_type)) {
3945 locations->SetInAt(0, Location::RequiresFpuRegister());
3946 } else {
3947 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003948 }
3949
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003950 if (Primitive::IsFloatingPointType(result_type)) {
3951 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003952 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003953 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003954 }
3955}
3956
3957void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3958 LocationSummary* locations = conversion->GetLocations();
3959 Primitive::Type result_type = conversion->GetResultType();
3960 Primitive::Type input_type = conversion->GetInputType();
3961
3962 DCHECK_NE(input_type, result_type);
3963
3964 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3965 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3966 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3967
3968 switch (result_type) {
3969 case Primitive::kPrimChar:
3970 __ Andi(dst, src, 0xFFFF);
3971 break;
3972 case Primitive::kPrimByte:
3973 // long is never converted into types narrower than int directly,
3974 // so SEB and SEH can be used without ever causing unpredictable results
3975 // on 64-bit inputs
3976 DCHECK(input_type != Primitive::kPrimLong);
3977 __ Seb(dst, src);
3978 break;
3979 case Primitive::kPrimShort:
3980 // long is never converted into types narrower than int directly,
3981 // so SEB and SEH can be used without ever causing unpredictable results
3982 // on 64-bit inputs
3983 DCHECK(input_type != Primitive::kPrimLong);
3984 __ Seh(dst, src);
3985 break;
3986 case Primitive::kPrimInt:
3987 case Primitive::kPrimLong:
3988 // Sign-extend 32-bit int into bits 32 through 63 for
3989 // int-to-long and long-to-int conversions
3990 __ Sll(dst, src, 0);
3991 break;
3992
3993 default:
3994 LOG(FATAL) << "Unexpected type conversion from " << input_type
3995 << " to " << result_type;
3996 }
3997 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003998 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3999 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4000 if (input_type == Primitive::kPrimLong) {
4001 __ Dmtc1(src, FTMP);
4002 if (result_type == Primitive::kPrimFloat) {
4003 __ Cvtsl(dst, FTMP);
4004 } else {
4005 __ Cvtdl(dst, FTMP);
4006 }
4007 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004008 __ Mtc1(src, FTMP);
4009 if (result_type == Primitive::kPrimFloat) {
4010 __ Cvtsw(dst, FTMP);
4011 } else {
4012 __ Cvtdw(dst, FTMP);
4013 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004014 }
4015 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4016 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004017 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4018 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4019 Mips64Label truncate;
4020 Mips64Label done;
4021
4022 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4023 // value when the input is either a NaN or is outside of the range of the output type
4024 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4025 // the same result.
4026 //
4027 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4028 // value of the output type if the input is outside of the range after the truncation or
4029 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4030 // results. This matches the desired float/double-to-int/long conversion exactly.
4031 //
4032 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4033 //
4034 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4035 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4036 // even though it must be NAN2008=1 on R6.
4037 //
4038 // The code takes care of the different behaviors by first comparing the input to the
4039 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4040 // If the input is greater than or equal to the minimum, it procedes to the truncate
4041 // instruction, which will handle such an input the same way irrespective of NAN2008.
4042 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4043 // in order to return either zero or the minimum value.
4044 //
4045 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4046 // truncate instruction for MIPS64R6.
4047 if (input_type == Primitive::kPrimFloat) {
4048 uint32_t min_val = (result_type == Primitive::kPrimLong)
4049 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4050 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4051 __ LoadConst32(TMP, min_val);
4052 __ Mtc1(TMP, FTMP);
4053 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004054 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004055 uint64_t min_val = (result_type == Primitive::kPrimLong)
4056 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4057 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4058 __ LoadConst64(TMP, min_val);
4059 __ Dmtc1(TMP, FTMP);
4060 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004061 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004062
4063 __ Bc1nez(FTMP, &truncate);
4064
4065 if (input_type == Primitive::kPrimFloat) {
4066 __ CmpEqS(FTMP, src, src);
4067 } else {
4068 __ CmpEqD(FTMP, src, src);
4069 }
4070 if (result_type == Primitive::kPrimLong) {
4071 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4072 } else {
4073 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4074 }
4075 __ Mfc1(TMP, FTMP);
4076 __ And(dst, dst, TMP);
4077
4078 __ Bc(&done);
4079
4080 __ Bind(&truncate);
4081
4082 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004083 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004084 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004085 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004086 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004087 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004088 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004089 } else {
4090 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004091 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004092 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004093 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004094 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004095 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004096 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004097
4098 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004099 } else if (Primitive::IsFloatingPointType(result_type) &&
4100 Primitive::IsFloatingPointType(input_type)) {
4101 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4102 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4103 if (result_type == Primitive::kPrimFloat) {
4104 __ Cvtsd(dst, src);
4105 } else {
4106 __ Cvtds(dst, src);
4107 }
4108 } else {
4109 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4110 << " to " << result_type;
4111 }
4112}
4113
4114void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4115 HandleShift(ushr);
4116}
4117
4118void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4119 HandleShift(ushr);
4120}
4121
4122void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4123 HandleBinaryOp(instruction);
4124}
4125
4126void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4127 HandleBinaryOp(instruction);
4128}
4129
4130void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4131 // Nothing to do, this should be removed during prepare for register allocator.
4132 LOG(FATAL) << "Unreachable";
4133}
4134
4135void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4136 // Nothing to do, this should be removed during prepare for register allocator.
4137 LOG(FATAL) << "Unreachable";
4138}
4139
4140void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004141 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004142}
4143
4144void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004145 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004146}
4147
4148void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004149 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004150}
4151
4152void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004153 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004154}
4155
4156void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004157 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004158}
4159
4160void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004161 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004162}
4163
4164void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004165 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004166}
4167
4168void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004169 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004170}
4171
4172void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004173 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004174}
4175
4176void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004177 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004178}
4179
4180void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004181 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004182}
4183
4184void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004185 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004186}
4187
Aart Bike9f37602015-10-09 11:15:55 -07004188void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004189 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004190}
4191
4192void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004193 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004194}
4195
4196void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004197 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004198}
4199
4200void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004201 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004202}
4203
4204void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004205 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004206}
4207
4208void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004209 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004210}
4211
4212void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004213 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004214}
4215
4216void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004217 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004218}
4219
Mark Mendellfe57faa2015-09-18 09:26:15 -04004220// Simple implementation of packed switch - generate cascaded compare/jumps.
4221void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4222 LocationSummary* locations =
4223 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4224 locations->SetInAt(0, Location::RequiresRegister());
4225}
4226
4227void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4228 int32_t lower_bound = switch_instr->GetStartValue();
4229 int32_t num_entries = switch_instr->GetNumEntries();
4230 LocationSummary* locations = switch_instr->GetLocations();
4231 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4232 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4233
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004234 // Create a set of compare/jumps.
4235 GpuRegister temp_reg = TMP;
4236 if (IsInt<16>(-lower_bound)) {
4237 __ Addiu(temp_reg, value_reg, -lower_bound);
4238 } else {
4239 __ LoadConst32(AT, -lower_bound);
4240 __ Addu(temp_reg, value_reg, AT);
4241 }
4242 // Jump to default if index is negative
4243 // Note: We don't check the case that index is positive while value < lower_bound, because in
4244 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4245 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4246
Mark Mendellfe57faa2015-09-18 09:26:15 -04004247 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004248 // Jump to successors[0] if value == lower_bound.
4249 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4250 int32_t last_index = 0;
4251 for (; num_entries - last_index > 2; last_index += 2) {
4252 __ Addiu(temp_reg, temp_reg, -2);
4253 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4254 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4255 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4256 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4257 }
4258 if (num_entries - last_index == 2) {
4259 // The last missing case_value.
4260 __ Addiu(temp_reg, temp_reg, -1);
4261 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004262 }
4263
4264 // And the default for any other value.
4265 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004266 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004267 }
4268}
4269
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004270void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4271 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4272}
4273
4274void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4275 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4276}
4277
Alexey Frunze4dda3372015-06-01 18:31:49 -07004278} // namespace mips64
4279} // namespace art
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004280