blob: 8d5dc84df9409b394f6170ba7fbfef61ee2fb1b9 [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
Alexey Frunze4dda3372015-06-01 18:31:49 -070040Location Mips64ReturnLocation(Primitive::Type return_type) {
41 switch (return_type) {
42 case Primitive::kPrimBoolean:
43 case Primitive::kPrimByte:
44 case Primitive::kPrimChar:
45 case Primitive::kPrimShort:
46 case Primitive::kPrimInt:
47 case Primitive::kPrimNot:
48 case Primitive::kPrimLong:
49 return Location::RegisterLocation(V0);
50
51 case Primitive::kPrimFloat:
52 case Primitive::kPrimDouble:
53 return Location::FpuRegisterLocation(F0);
54
55 case Primitive::kPrimVoid:
56 return Location();
57 }
58 UNREACHABLE();
59}
60
61Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
62 return Mips64ReturnLocation(type);
63}
64
65Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
66 return Location::RegisterLocation(kMethodRegisterArgument);
67}
68
69Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
70 Location next_location;
71 if (type == Primitive::kPrimVoid) {
72 LOG(FATAL) << "Unexpected parameter type " << type;
73 }
74
75 if (Primitive::IsFloatingPointType(type) &&
76 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
77 next_location = Location::FpuRegisterLocation(
78 calling_convention.GetFpuRegisterAt(float_index_++));
79 gp_index_++;
80 } else if (!Primitive::IsFloatingPointType(type) &&
81 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
82 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
83 float_index_++;
84 } else {
85 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
86 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
87 : Location::StackSlot(stack_offset);
88 }
89
90 // Space on the stack is reserved for all arguments.
91 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
92
93 // TODO: review
94
95 // TODO: shouldn't we use a whole machine word per argument on the stack?
96 // Implicit 4-byte method pointer (and such) will cause misalignment.
97
98 return next_location;
99}
100
101Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
102 return Mips64ReturnLocation(type);
103}
104
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700105// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
106#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Lazar Trsicd9672662015-09-03 17:33:01 +0200107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108
109class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
110 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000111 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700112
113 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100114 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700115 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
116 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000117 if (instruction_->CanThrowIntoCatchBlock()) {
118 // Live registers will be restored in the catch block if caught.
119 SaveLiveRegisters(codegen, instruction_->GetLocations());
120 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 // We're moving two locations to locations that could overlap, so we need a parallel
122 // move resolver.
123 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100124 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700125 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
126 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100127 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700128 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
129 Primitive::kPrimInt);
130 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
131 instruction_,
132 instruction_->GetDexPc(),
133 this);
134 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
135 }
136
Alexandre Rames8158f282015-08-07 10:26:17 +0100137 bool IsFatal() const OVERRIDE { return true; }
138
Roland Levillain46648892015-06-19 16:07:18 +0100139 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
140
Alexey Frunze4dda3372015-06-01 18:31:49 -0700141 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700142 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
143};
144
145class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
146 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000147 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700148
149 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
150 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
151 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000152 if (instruction_->CanThrowIntoCatchBlock()) {
153 // Live registers will be restored in the catch block if caught.
154 SaveLiveRegisters(codegen, instruction_->GetLocations());
155 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700156 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
157 instruction_,
158 instruction_->GetDexPc(),
159 this);
160 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
161 }
162
Alexandre Rames8158f282015-08-07 10:26:17 +0100163 bool IsFatal() const OVERRIDE { return true; }
164
Roland Levillain46648892015-06-19 16:07:18 +0100165 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
166
Alexey Frunze4dda3372015-06-01 18:31:49 -0700167 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700168 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
169};
170
171class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
172 public:
173 LoadClassSlowPathMIPS64(HLoadClass* cls,
174 HInstruction* at,
175 uint32_t dex_pc,
176 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000177 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700178 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
179 }
180
181 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
182 LocationSummary* locations = at_->GetLocations();
183 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
184
185 __ Bind(GetEntryLabel());
186 SaveLiveRegisters(codegen, locations);
187
188 InvokeRuntimeCallingConvention calling_convention;
189 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
190 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
191 : QUICK_ENTRY_POINT(pInitializeType);
192 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
193 if (do_clinit_) {
194 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
195 } else {
196 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
197 }
198
199 // Move the class to the desired location.
200 Location out = locations->Out();
201 if (out.IsValid()) {
202 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
203 Primitive::Type type = at_->GetType();
204 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
205 }
206
207 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700208 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700209 }
210
Roland Levillain46648892015-06-19 16:07:18 +0100211 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
212
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 private:
214 // The class this slow path will load.
215 HLoadClass* const cls_;
216
217 // The instruction where this slow path is happening.
218 // (Might be the load class or an initialization check).
219 HInstruction* const at_;
220
221 // The dex PC of `at_`.
222 const uint32_t dex_pc_;
223
224 // Whether to initialize the class.
225 const bool do_clinit_;
226
227 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
228};
229
230class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
231 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000232 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700233
234 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
235 LocationSummary* locations = instruction_->GetLocations();
236 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
237 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
238
239 __ Bind(GetEntryLabel());
240 SaveLiveRegisters(codegen, locations);
241
242 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000243 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
244 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700245 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
246 instruction_,
247 instruction_->GetDexPc(),
248 this);
249 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
250 Primitive::Type type = instruction_->GetType();
251 mips64_codegen->MoveLocation(locations->Out(),
252 calling_convention.GetReturnLocation(type),
253 type);
254
255 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700256 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700257 }
258
Roland Levillain46648892015-06-19 16:07:18 +0100259 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
260
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700262 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
263};
264
265class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
266 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000267 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700268
269 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
270 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
271 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000272 if (instruction_->CanThrowIntoCatchBlock()) {
273 // Live registers will be restored in the catch block if caught.
274 SaveLiveRegisters(codegen, instruction_->GetLocations());
275 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700276 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
277 instruction_,
278 instruction_->GetDexPc(),
279 this);
280 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
281 }
282
Alexandre Rames8158f282015-08-07 10:26:17 +0100283 bool IsFatal() const OVERRIDE { return true; }
284
Roland Levillain46648892015-06-19 16:07:18 +0100285 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
286
Alexey Frunze4dda3372015-06-01 18:31:49 -0700287 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700288 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
289};
290
291class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
292 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100293 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000294 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295
296 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
297 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
298 __ Bind(GetEntryLabel());
299 SaveLiveRegisters(codegen, instruction_->GetLocations());
300 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
301 instruction_,
302 instruction_->GetDexPc(),
303 this);
304 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
305 RestoreLiveRegisters(codegen, instruction_->GetLocations());
306 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700307 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700308 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700309 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 }
311 }
312
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700313 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314 DCHECK(successor_ == nullptr);
315 return &return_label_;
316 }
317
Roland Levillain46648892015-06-19 16:07:18 +0100318 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
319
Alexey Frunze4dda3372015-06-01 18:31:49 -0700320 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700321 // If not null, the block to branch to after the suspend check.
322 HBasicBlock* const successor_;
323
324 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700325 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700326
327 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
328};
329
330class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
331 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000332 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700333
334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
335 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200336 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100337 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700338 DCHECK(instruction_->IsCheckCast()
339 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
340 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
341
342 __ Bind(GetEntryLabel());
343 SaveLiveRegisters(codegen, locations);
344
345 // We're moving two locations to locations that could overlap, so we need a parallel
346 // move resolver.
347 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100348 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700349 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
350 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100351 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700352 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
353 Primitive::kPrimNot);
354
355 if (instruction_->IsInstanceOf()) {
356 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
357 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100358 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700359 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000360 CheckEntrypointTypes<
361 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700362 Primitive::Type ret_type = instruction_->GetType();
363 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
364 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700365 } else {
366 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100367 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700368 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
369 }
370
371 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700372 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700373 }
374
Roland Levillain46648892015-06-19 16:07:18 +0100375 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
376
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700378 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
379};
380
381class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
382 public:
Aart Bik42249c32016-01-07 15:33:50 -0800383 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800387 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 __ Bind(GetEntryLabel());
389 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800390 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
391 instruction_,
392 instruction_->GetDexPc(),
393 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000394 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395 }
396
Roland Levillain46648892015-06-19 16:07:18 +0100397 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
398
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700400 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
401};
402
403CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
404 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100405 const CompilerOptions& compiler_options,
406 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 : CodeGenerator(graph,
408 kNumberOfGpuRegisters,
409 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000410 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
412 arraysize(kCoreCalleeSaves)),
413 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
414 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100415 compiler_options,
416 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100417 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700418 location_builder_(graph, this),
419 instruction_visitor_(graph, this),
420 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100421 assembler_(graph->GetArena()),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700422 isa_features_(isa_features) {
423 // Save RA (containing the return address) to mimic Quick.
424 AddAllocatedRegister(Location::RegisterLocation(RA));
425}
426
427#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700428// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
429#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Lazar Trsicd9672662015-09-03 17:33:01 +0200430#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700431
432void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700433 // Ensure that we fix up branches.
434 __ FinalizeCode();
435
436 // Adjust native pc offsets in stack maps.
437 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
438 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
439 uint32_t new_position = __ GetAdjustedPosition(old_position);
440 DCHECK_GE(new_position, old_position);
441 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
442 }
443
444 // Adjust pc offsets for the disassembly information.
445 if (disasm_info_ != nullptr) {
446 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
447 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
448 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
449 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
450 it.second.start = __ GetAdjustedPosition(it.second.start);
451 it.second.end = __ GetAdjustedPosition(it.second.end);
452 }
453 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
454 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
455 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
456 }
457 }
458
Alexey Frunze4dda3372015-06-01 18:31:49 -0700459 CodeGenerator::Finalize(allocator);
460}
461
462Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
463 return codegen_->GetAssembler();
464}
465
466void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100467 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700468 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
469}
470
471void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100472 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700473 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
474}
475
476void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
477 // Pop reg
478 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200479 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700480}
481
482void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
483 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200484 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700485 __ Sd(GpuRegister(reg), SP, 0);
486}
487
488void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
489 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
490 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
491 // Allocate a scratch register other than TMP, if available.
492 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
493 // automatically unspilled when the scratch scope object is destroyed).
494 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
495 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200496 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700497 __ LoadFromOffset(load_type,
498 GpuRegister(ensure_scratch.GetRegister()),
499 SP,
500 index1 + stack_offset);
501 __ LoadFromOffset(load_type,
502 TMP,
503 SP,
504 index2 + stack_offset);
505 __ StoreToOffset(store_type,
506 GpuRegister(ensure_scratch.GetRegister()),
507 SP,
508 index2 + stack_offset);
509 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
510}
511
512static dwarf::Reg DWARFReg(GpuRegister reg) {
513 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
514}
515
David Srbeckyba702002016-02-01 18:15:29 +0000516static dwarf::Reg DWARFReg(FpuRegister reg) {
517 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
518}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700519
520void CodeGeneratorMIPS64::GenerateFrameEntry() {
521 __ Bind(&frame_entry_label_);
522
523 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
524
525 if (do_overflow_check) {
526 __ LoadFromOffset(kLoadWord,
527 ZERO,
528 SP,
529 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
530 RecordPcInfo(nullptr, 0);
531 }
532
533 // TODO: anything related to T9/GP/GOT/PIC/.so's?
534
535 if (HasEmptyFrame()) {
536 return;
537 }
538
539 // Make sure the frame size isn't unreasonably large. Per the various APIs
540 // it looks like it should always be less than 2GB in size, which allows
541 // us using 32-bit signed offsets from the stack pointer.
542 if (GetFrameSize() > 0x7FFFFFFF)
543 LOG(FATAL) << "Stack frame larger than 2GB";
544
545 // Spill callee-saved registers.
546 // Note that their cumulative size is small and they can be indexed using
547 // 16-bit offsets.
548
549 // TODO: increment/decrement SP in one step instead of two or remove this comment.
550
551 uint32_t ofs = FrameEntrySpillSize();
552 __ IncreaseFrameSize(ofs);
553
554 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
555 GpuRegister reg = kCoreCalleeSaves[i];
556 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200557 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700558 __ Sd(reg, SP, ofs);
559 __ cfi().RelOffset(DWARFReg(reg), ofs);
560 }
561 }
562
563 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
564 FpuRegister reg = kFpuCalleeSaves[i];
565 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200566 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700567 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000568 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700569 }
570 }
571
572 // Allocate the rest of the frame and store the current method pointer
573 // at its end.
574
575 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
576
577 static_assert(IsInt<16>(kCurrentMethodStackOffset),
578 "kCurrentMethodStackOffset must fit into int16_t");
579 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
580}
581
582void CodeGeneratorMIPS64::GenerateFrameExit() {
583 __ cfi().RememberState();
584
585 // TODO: anything related to T9/GP/GOT/PIC/.so's?
586
587 if (!HasEmptyFrame()) {
588 // Deallocate the rest of the frame.
589
590 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
591
592 // Restore callee-saved registers.
593 // Note that their cumulative size is small and they can be indexed using
594 // 16-bit offsets.
595
596 // TODO: increment/decrement SP in one step instead of two or remove this comment.
597
598 uint32_t ofs = 0;
599
600 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
601 FpuRegister reg = kFpuCalleeSaves[i];
602 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
603 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200604 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000605 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700606 }
607 }
608
609 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
610 GpuRegister reg = kCoreCalleeSaves[i];
611 if (allocated_registers_.ContainsCoreRegister(reg)) {
612 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200613 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700614 __ cfi().Restore(DWARFReg(reg));
615 }
616 }
617
618 DCHECK_EQ(ofs, FrameEntrySpillSize());
619 __ DecreaseFrameSize(ofs);
620 }
621
622 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700623 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700624
625 __ cfi().RestoreState();
626 __ cfi().DefCFAOffset(GetFrameSize());
627}
628
629void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
630 __ Bind(GetLabelOf(block));
631}
632
633void CodeGeneratorMIPS64::MoveLocation(Location destination,
634 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100635 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700636 if (source.Equals(destination)) {
637 return;
638 }
639
640 // A valid move can always be inferred from the destination and source
641 // locations. When moving from and to a register, the argument type can be
642 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100643 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700644 DCHECK_EQ(unspecified_type, false);
645
646 if (destination.IsRegister() || destination.IsFpuRegister()) {
647 if (unspecified_type) {
648 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
649 if (source.IsStackSlot() ||
650 (src_cst != nullptr && (src_cst->IsIntConstant()
651 || src_cst->IsFloatConstant()
652 || src_cst->IsNullConstant()))) {
653 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100654 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700655 } else {
656 // If the source is a double stack slot or a 64bit constant, a 64bit
657 // type is appropriate. Else the source is a register, and since the
658 // type has not been specified, we chose a 64bit type to force a 64bit
659 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100660 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700661 }
662 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100663 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
664 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700665 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
666 // Move to GPR/FPR from stack
667 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100668 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700669 __ LoadFpuFromOffset(load_type,
670 destination.AsFpuRegister<FpuRegister>(),
671 SP,
672 source.GetStackIndex());
673 } else {
674 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
675 __ LoadFromOffset(load_type,
676 destination.AsRegister<GpuRegister>(),
677 SP,
678 source.GetStackIndex());
679 }
680 } else if (source.IsConstant()) {
681 // Move to GPR/FPR from constant
682 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100683 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700684 gpr = destination.AsRegister<GpuRegister>();
685 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100686 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700687 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
688 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
689 gpr = ZERO;
690 } else {
691 __ LoadConst32(gpr, value);
692 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700693 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700694 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
695 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
696 gpr = ZERO;
697 } else {
698 __ LoadConst64(gpr, value);
699 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700700 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100701 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700702 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100703 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700704 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
705 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100706 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700707 if (destination.IsRegister()) {
708 // Move to GPR from GPR
709 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
710 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 DCHECK(destination.IsFpuRegister());
712 if (Primitive::Is64BitType(dst_type)) {
713 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
714 } else {
715 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
716 }
717 }
718 } else if (source.IsFpuRegister()) {
719 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700720 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100721 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700722 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
723 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100724 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700725 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
726 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100727 } else {
728 DCHECK(destination.IsRegister());
729 if (Primitive::Is64BitType(dst_type)) {
730 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
731 } else {
732 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
733 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700734 }
735 }
736 } else { // The destination is not a register. It must be a stack slot.
737 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
738 if (source.IsRegister() || source.IsFpuRegister()) {
739 if (unspecified_type) {
740 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100741 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700742 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100743 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700744 }
745 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100746 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
747 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700748 // Move to stack from GPR/FPR
749 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
750 if (source.IsRegister()) {
751 __ StoreToOffset(store_type,
752 source.AsRegister<GpuRegister>(),
753 SP,
754 destination.GetStackIndex());
755 } else {
756 __ StoreFpuToOffset(store_type,
757 source.AsFpuRegister<FpuRegister>(),
758 SP,
759 destination.GetStackIndex());
760 }
761 } else if (source.IsConstant()) {
762 // Move to stack from constant
763 HConstant* src_cst = source.GetConstant();
764 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700765 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700766 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700767 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
768 if (value != 0) {
769 gpr = TMP;
770 __ LoadConst32(gpr, value);
771 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700772 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700773 DCHECK(destination.IsDoubleStackSlot());
774 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
775 if (value != 0) {
776 gpr = TMP;
777 __ LoadConst64(gpr, value);
778 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700779 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700780 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700781 } else {
782 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
783 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
784 // Move to stack from stack
785 if (destination.IsStackSlot()) {
786 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
787 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
788 } else {
789 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
790 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
791 }
792 }
793 }
794}
795
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700796void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700797 DCHECK(!loc1.IsConstant());
798 DCHECK(!loc2.IsConstant());
799
800 if (loc1.Equals(loc2)) {
801 return;
802 }
803
804 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
805 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
806 bool is_fp_reg1 = loc1.IsFpuRegister();
807 bool is_fp_reg2 = loc2.IsFpuRegister();
808
809 if (loc2.IsRegister() && loc1.IsRegister()) {
810 // Swap 2 GPRs
811 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
812 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
813 __ Move(TMP, r2);
814 __ Move(r2, r1);
815 __ Move(r1, TMP);
816 } else if (is_fp_reg2 && is_fp_reg1) {
817 // Swap 2 FPRs
818 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
819 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700820 if (type == Primitive::kPrimFloat) {
821 __ MovS(FTMP, r1);
822 __ MovS(r1, r2);
823 __ MovS(r2, FTMP);
824 } else {
825 DCHECK_EQ(type, Primitive::kPrimDouble);
826 __ MovD(FTMP, r1);
827 __ MovD(r1, r2);
828 __ MovD(r2, FTMP);
829 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700830 } else if (is_slot1 != is_slot2) {
831 // Swap GPR/FPR and stack slot
832 Location reg_loc = is_slot1 ? loc2 : loc1;
833 Location mem_loc = is_slot1 ? loc1 : loc2;
834 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
835 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
836 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
837 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
838 if (reg_loc.IsFpuRegister()) {
839 __ StoreFpuToOffset(store_type,
840 reg_loc.AsFpuRegister<FpuRegister>(),
841 SP,
842 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700843 if (mem_loc.IsStackSlot()) {
844 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
845 } else {
846 DCHECK(mem_loc.IsDoubleStackSlot());
847 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
848 }
849 } else {
850 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
851 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
852 }
853 } else if (is_slot1 && is_slot2) {
854 move_resolver_.Exchange(loc1.GetStackIndex(),
855 loc2.GetStackIndex(),
856 loc1.IsDoubleStackSlot());
857 } else {
858 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
859 }
860}
861
Calin Juravle175dc732015-08-25 15:42:32 +0100862void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
863 DCHECK(location.IsRegister());
864 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
865}
866
Calin Juravlee460d1d2015-09-29 04:52:17 +0100867void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
868 if (location.IsRegister()) {
869 locations->AddTemp(location);
870 } else {
871 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
872 }
873}
874
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100875void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
876 GpuRegister value,
877 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700878 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700879 GpuRegister card = AT;
880 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100881 if (value_can_be_null) {
882 __ Beqzc(value, &done);
883 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700884 __ LoadFromOffset(kLoadDoubleword,
885 card,
886 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200887 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700888 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
889 __ Daddu(temp, card, temp);
890 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100891 if (value_can_be_null) {
892 __ Bind(&done);
893 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700894}
895
David Brazdil58282f42016-01-14 12:45:10 +0000896void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700897 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
898 blocked_core_registers_[ZERO] = true;
899 blocked_core_registers_[K0] = true;
900 blocked_core_registers_[K1] = true;
901 blocked_core_registers_[GP] = true;
902 blocked_core_registers_[SP] = true;
903 blocked_core_registers_[RA] = true;
904
Lazar Trsicd9672662015-09-03 17:33:01 +0200905 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
906 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700907 blocked_core_registers_[AT] = true;
908 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200909 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700910 blocked_fpu_registers_[FTMP] = true;
911
912 // Reserve suspend and thread registers.
913 blocked_core_registers_[S0] = true;
914 blocked_core_registers_[TR] = true;
915
916 // Reserve T9 for function calls
917 blocked_core_registers_[T9] = true;
918
919 // TODO: review; anything else?
920
David Brazdil58282f42016-01-14 12:45:10 +0000921 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700922 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
923 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
924 }
925
926 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
927 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
928 }
929}
930
Alexey Frunze4dda3372015-06-01 18:31:49 -0700931size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
932 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200933 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700934}
935
936size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
937 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200938 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700939}
940
941size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
942 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200943 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944}
945
946size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
947 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200948 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700949}
950
951void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100952 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700953}
954
955void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100956 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700957}
958
Calin Juravle175dc732015-08-25 15:42:32 +0100959void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
960 HInstruction* instruction,
961 uint32_t dex_pc,
962 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200963 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100964 instruction,
965 dex_pc,
966 slow_path);
967}
968
Alexey Frunze4dda3372015-06-01 18:31:49 -0700969void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
970 HInstruction* instruction,
971 uint32_t dex_pc,
972 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100973 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700974 // TODO: anything related to T9/GP/GOT/PIC/.so's?
975 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
976 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700977 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700978 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700979}
980
981void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
982 GpuRegister class_reg) {
983 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
984 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
985 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
986 // TODO: barrier needed?
987 __ Bind(slow_path->GetExitLabel());
988}
989
990void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
991 __ Sync(0); // only stype 0 is supported
992}
993
994void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
995 HBasicBlock* successor) {
996 SuspendCheckSlowPathMIPS64* slow_path =
997 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
998 codegen_->AddSlowPath(slow_path);
999
1000 __ LoadFromOffset(kLoadUnsignedHalfword,
1001 TMP,
1002 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001003 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001004 if (successor == nullptr) {
1005 __ Bnezc(TMP, slow_path->GetEntryLabel());
1006 __ Bind(slow_path->GetReturnLabel());
1007 } else {
1008 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001009 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001010 // slow_path will return to GetLabelOf(successor).
1011 }
1012}
1013
1014InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1015 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001016 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001017 assembler_(codegen->GetAssembler()),
1018 codegen_(codegen) {}
1019
1020void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1021 DCHECK_EQ(instruction->InputCount(), 2U);
1022 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1023 Primitive::Type type = instruction->GetResultType();
1024 switch (type) {
1025 case Primitive::kPrimInt:
1026 case Primitive::kPrimLong: {
1027 locations->SetInAt(0, Location::RequiresRegister());
1028 HInstruction* right = instruction->InputAt(1);
1029 bool can_use_imm = false;
1030 if (right->IsConstant()) {
1031 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1032 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1033 can_use_imm = IsUint<16>(imm);
1034 } else if (instruction->IsAdd()) {
1035 can_use_imm = IsInt<16>(imm);
1036 } else {
1037 DCHECK(instruction->IsSub());
1038 can_use_imm = IsInt<16>(-imm);
1039 }
1040 }
1041 if (can_use_imm)
1042 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1043 else
1044 locations->SetInAt(1, Location::RequiresRegister());
1045 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1046 }
1047 break;
1048
1049 case Primitive::kPrimFloat:
1050 case Primitive::kPrimDouble:
1051 locations->SetInAt(0, Location::RequiresFpuRegister());
1052 locations->SetInAt(1, Location::RequiresFpuRegister());
1053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1054 break;
1055
1056 default:
1057 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1058 }
1059}
1060
1061void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1062 Primitive::Type type = instruction->GetType();
1063 LocationSummary* locations = instruction->GetLocations();
1064
1065 switch (type) {
1066 case Primitive::kPrimInt:
1067 case Primitive::kPrimLong: {
1068 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1069 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1070 Location rhs_location = locations->InAt(1);
1071
1072 GpuRegister rhs_reg = ZERO;
1073 int64_t rhs_imm = 0;
1074 bool use_imm = rhs_location.IsConstant();
1075 if (use_imm) {
1076 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1077 } else {
1078 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1079 }
1080
1081 if (instruction->IsAnd()) {
1082 if (use_imm)
1083 __ Andi(dst, lhs, rhs_imm);
1084 else
1085 __ And(dst, lhs, rhs_reg);
1086 } else if (instruction->IsOr()) {
1087 if (use_imm)
1088 __ Ori(dst, lhs, rhs_imm);
1089 else
1090 __ Or(dst, lhs, rhs_reg);
1091 } else if (instruction->IsXor()) {
1092 if (use_imm)
1093 __ Xori(dst, lhs, rhs_imm);
1094 else
1095 __ Xor(dst, lhs, rhs_reg);
1096 } else if (instruction->IsAdd()) {
1097 if (type == Primitive::kPrimInt) {
1098 if (use_imm)
1099 __ Addiu(dst, lhs, rhs_imm);
1100 else
1101 __ Addu(dst, lhs, rhs_reg);
1102 } else {
1103 if (use_imm)
1104 __ Daddiu(dst, lhs, rhs_imm);
1105 else
1106 __ Daddu(dst, lhs, rhs_reg);
1107 }
1108 } else {
1109 DCHECK(instruction->IsSub());
1110 if (type == Primitive::kPrimInt) {
1111 if (use_imm)
1112 __ Addiu(dst, lhs, -rhs_imm);
1113 else
1114 __ Subu(dst, lhs, rhs_reg);
1115 } else {
1116 if (use_imm)
1117 __ Daddiu(dst, lhs, -rhs_imm);
1118 else
1119 __ Dsubu(dst, lhs, rhs_reg);
1120 }
1121 }
1122 break;
1123 }
1124 case Primitive::kPrimFloat:
1125 case Primitive::kPrimDouble: {
1126 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1127 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1128 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1129 if (instruction->IsAdd()) {
1130 if (type == Primitive::kPrimFloat)
1131 __ AddS(dst, lhs, rhs);
1132 else
1133 __ AddD(dst, lhs, rhs);
1134 } else if (instruction->IsSub()) {
1135 if (type == Primitive::kPrimFloat)
1136 __ SubS(dst, lhs, rhs);
1137 else
1138 __ SubD(dst, lhs, rhs);
1139 } else {
1140 LOG(FATAL) << "Unexpected floating-point binary operation";
1141 }
1142 break;
1143 }
1144 default:
1145 LOG(FATAL) << "Unexpected binary operation type " << type;
1146 }
1147}
1148
1149void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001150 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001151
1152 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1153 Primitive::Type type = instr->GetResultType();
1154 switch (type) {
1155 case Primitive::kPrimInt:
1156 case Primitive::kPrimLong: {
1157 locations->SetInAt(0, Location::RequiresRegister());
1158 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001160 break;
1161 }
1162 default:
1163 LOG(FATAL) << "Unexpected shift type " << type;
1164 }
1165}
1166
1167void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001168 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001169 LocationSummary* locations = instr->GetLocations();
1170 Primitive::Type type = instr->GetType();
1171
1172 switch (type) {
1173 case Primitive::kPrimInt:
1174 case Primitive::kPrimLong: {
1175 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1176 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1177 Location rhs_location = locations->InAt(1);
1178
1179 GpuRegister rhs_reg = ZERO;
1180 int64_t rhs_imm = 0;
1181 bool use_imm = rhs_location.IsConstant();
1182 if (use_imm) {
1183 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1184 } else {
1185 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1186 }
1187
1188 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001189 uint32_t shift_value = rhs_imm &
1190 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001191
Alexey Frunze92d90602015-12-18 18:16:36 -08001192 if (shift_value == 0) {
1193 if (dst != lhs) {
1194 __ Move(dst, lhs);
1195 }
1196 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001197 if (instr->IsShl()) {
1198 __ Sll(dst, lhs, shift_value);
1199 } else if (instr->IsShr()) {
1200 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001201 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001202 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001203 } else {
1204 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001205 }
1206 } else {
1207 if (shift_value < 32) {
1208 if (instr->IsShl()) {
1209 __ Dsll(dst, lhs, shift_value);
1210 } else if (instr->IsShr()) {
1211 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001212 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001213 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001214 } else {
1215 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001216 }
1217 } else {
1218 shift_value -= 32;
1219 if (instr->IsShl()) {
1220 __ Dsll32(dst, lhs, shift_value);
1221 } else if (instr->IsShr()) {
1222 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001223 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001225 } else {
1226 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001227 }
1228 }
1229 }
1230 } else {
1231 if (type == Primitive::kPrimInt) {
1232 if (instr->IsShl()) {
1233 __ Sllv(dst, lhs, rhs_reg);
1234 } else if (instr->IsShr()) {
1235 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001236 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001237 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001238 } else {
1239 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001240 }
1241 } else {
1242 if (instr->IsShl()) {
1243 __ Dsllv(dst, lhs, rhs_reg);
1244 } else if (instr->IsShr()) {
1245 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001246 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001247 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001248 } else {
1249 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001250 }
1251 }
1252 }
1253 break;
1254 }
1255 default:
1256 LOG(FATAL) << "Unexpected shift operation type " << type;
1257 }
1258}
1259
1260void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1261 HandleBinaryOp(instruction);
1262}
1263
1264void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1265 HandleBinaryOp(instruction);
1266}
1267
1268void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1269 HandleBinaryOp(instruction);
1270}
1271
1272void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1273 HandleBinaryOp(instruction);
1274}
1275
1276void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1277 LocationSummary* locations =
1278 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1279 locations->SetInAt(0, Location::RequiresRegister());
1280 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1281 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1282 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1283 } else {
1284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1285 }
1286}
1287
1288void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1289 LocationSummary* locations = instruction->GetLocations();
1290 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1291 Location index = locations->InAt(1);
1292 Primitive::Type type = instruction->GetType();
1293
1294 switch (type) {
1295 case Primitive::kPrimBoolean: {
1296 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1297 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1298 if (index.IsConstant()) {
1299 size_t offset =
1300 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1301 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1302 } else {
1303 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1304 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1305 }
1306 break;
1307 }
1308
1309 case Primitive::kPrimByte: {
1310 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1311 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1312 if (index.IsConstant()) {
1313 size_t offset =
1314 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1315 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1316 } else {
1317 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1318 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1319 }
1320 break;
1321 }
1322
1323 case Primitive::kPrimShort: {
1324 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1325 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1326 if (index.IsConstant()) {
1327 size_t offset =
1328 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1329 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1330 } else {
1331 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1332 __ Daddu(TMP, obj, TMP);
1333 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1334 }
1335 break;
1336 }
1337
1338 case Primitive::kPrimChar: {
1339 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1340 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1341 if (index.IsConstant()) {
1342 size_t offset =
1343 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1344 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1345 } else {
1346 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1347 __ Daddu(TMP, obj, TMP);
1348 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1349 }
1350 break;
1351 }
1352
1353 case Primitive::kPrimInt:
1354 case Primitive::kPrimNot: {
1355 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1356 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1357 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1358 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1359 if (index.IsConstant()) {
1360 size_t offset =
1361 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1362 __ LoadFromOffset(load_type, out, obj, offset);
1363 } else {
1364 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1365 __ Daddu(TMP, obj, TMP);
1366 __ LoadFromOffset(load_type, out, TMP, data_offset);
1367 }
1368 break;
1369 }
1370
1371 case Primitive::kPrimLong: {
1372 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1373 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1374 if (index.IsConstant()) {
1375 size_t offset =
1376 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1377 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1378 } else {
1379 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1380 __ Daddu(TMP, obj, TMP);
1381 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1382 }
1383 break;
1384 }
1385
1386 case Primitive::kPrimFloat: {
1387 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1388 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1389 if (index.IsConstant()) {
1390 size_t offset =
1391 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1392 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1393 } else {
1394 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1395 __ Daddu(TMP, obj, TMP);
1396 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1397 }
1398 break;
1399 }
1400
1401 case Primitive::kPrimDouble: {
1402 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1403 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1404 if (index.IsConstant()) {
1405 size_t offset =
1406 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1407 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1408 } else {
1409 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1410 __ Daddu(TMP, obj, TMP);
1411 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1412 }
1413 break;
1414 }
1415
1416 case Primitive::kPrimVoid:
1417 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1418 UNREACHABLE();
1419 }
1420 codegen_->MaybeRecordImplicitNullCheck(instruction);
1421}
1422
1423void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1424 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1425 locations->SetInAt(0, Location::RequiresRegister());
1426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1427}
1428
1429void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1430 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001431 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001432 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1433 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1434 __ LoadFromOffset(kLoadWord, out, obj, offset);
1435 codegen_->MaybeRecordImplicitNullCheck(instruction);
1436}
1437
1438void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001439 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001440 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1441 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001442 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1443 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001444 InvokeRuntimeCallingConvention calling_convention;
1445 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1446 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1447 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1448 } else {
1449 locations->SetInAt(0, Location::RequiresRegister());
1450 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1451 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1452 locations->SetInAt(2, Location::RequiresFpuRegister());
1453 } else {
1454 locations->SetInAt(2, Location::RequiresRegister());
1455 }
1456 }
1457}
1458
1459void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1460 LocationSummary* locations = instruction->GetLocations();
1461 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1462 Location index = locations->InAt(1);
1463 Primitive::Type value_type = instruction->GetComponentType();
1464 bool needs_runtime_call = locations->WillCall();
1465 bool needs_write_barrier =
1466 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1467
1468 switch (value_type) {
1469 case Primitive::kPrimBoolean:
1470 case Primitive::kPrimByte: {
1471 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1472 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1473 if (index.IsConstant()) {
1474 size_t offset =
1475 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1476 __ StoreToOffset(kStoreByte, value, obj, offset);
1477 } else {
1478 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1479 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1480 }
1481 break;
1482 }
1483
1484 case Primitive::kPrimShort:
1485 case Primitive::kPrimChar: {
1486 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1487 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1488 if (index.IsConstant()) {
1489 size_t offset =
1490 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1491 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1492 } else {
1493 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1494 __ Daddu(TMP, obj, TMP);
1495 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1496 }
1497 break;
1498 }
1499
1500 case Primitive::kPrimInt:
1501 case Primitive::kPrimNot: {
1502 if (!needs_runtime_call) {
1503 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1504 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1505 if (index.IsConstant()) {
1506 size_t offset =
1507 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1508 __ StoreToOffset(kStoreWord, value, obj, offset);
1509 } else {
1510 DCHECK(index.IsRegister()) << index;
1511 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1512 __ Daddu(TMP, obj, TMP);
1513 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1514 }
1515 codegen_->MaybeRecordImplicitNullCheck(instruction);
1516 if (needs_write_barrier) {
1517 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001518 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001519 }
1520 } else {
1521 DCHECK_EQ(value_type, Primitive::kPrimNot);
1522 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1523 instruction,
1524 instruction->GetDexPc(),
1525 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001526 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001527 }
1528 break;
1529 }
1530
1531 case Primitive::kPrimLong: {
1532 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1533 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1534 if (index.IsConstant()) {
1535 size_t offset =
1536 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1537 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1538 } else {
1539 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1540 __ Daddu(TMP, obj, TMP);
1541 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1542 }
1543 break;
1544 }
1545
1546 case Primitive::kPrimFloat: {
1547 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1548 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1549 DCHECK(locations->InAt(2).IsFpuRegister());
1550 if (index.IsConstant()) {
1551 size_t offset =
1552 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1553 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1554 } else {
1555 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1556 __ Daddu(TMP, obj, TMP);
1557 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1558 }
1559 break;
1560 }
1561
1562 case Primitive::kPrimDouble: {
1563 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1564 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1565 DCHECK(locations->InAt(2).IsFpuRegister());
1566 if (index.IsConstant()) {
1567 size_t offset =
1568 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1569 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1570 } else {
1571 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1572 __ Daddu(TMP, obj, TMP);
1573 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1574 }
1575 break;
1576 }
1577
1578 case Primitive::kPrimVoid:
1579 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1580 UNREACHABLE();
1581 }
1582
1583 // Ints and objects are handled in the switch.
1584 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1585 codegen_->MaybeRecordImplicitNullCheck(instruction);
1586 }
1587}
1588
1589void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001590 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1591 ? LocationSummary::kCallOnSlowPath
1592 : LocationSummary::kNoCall;
1593 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001594 locations->SetInAt(0, Location::RequiresRegister());
1595 locations->SetInAt(1, Location::RequiresRegister());
1596 if (instruction->HasUses()) {
1597 locations->SetOut(Location::SameAsFirstInput());
1598 }
1599}
1600
1601void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1602 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001603 BoundsCheckSlowPathMIPS64* slow_path =
1604 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001605 codegen_->AddSlowPath(slow_path);
1606
1607 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1608 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1609
1610 // length is limited by the maximum positive signed 32-bit integer.
1611 // Unsigned comparison of length and index checks for index < 0
1612 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001613 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001614}
1615
1616void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1618 instruction,
1619 LocationSummary::kCallOnSlowPath);
1620 locations->SetInAt(0, Location::RequiresRegister());
1621 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001622 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001623 locations->AddTemp(Location::RequiresRegister());
1624}
1625
1626void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1627 LocationSummary* locations = instruction->GetLocations();
1628 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1629 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1630 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1631
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001632 SlowPathCodeMIPS64* slow_path =
1633 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001634 codegen_->AddSlowPath(slow_path);
1635
1636 // TODO: avoid this check if we know obj is not null.
1637 __ Beqzc(obj, slow_path->GetExitLabel());
1638 // Compare the class of `obj` with `cls`.
1639 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1640 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1641 __ Bind(slow_path->GetExitLabel());
1642}
1643
1644void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1645 LocationSummary* locations =
1646 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1647 locations->SetInAt(0, Location::RequiresRegister());
1648 if (check->HasUses()) {
1649 locations->SetOut(Location::SameAsFirstInput());
1650 }
1651}
1652
1653void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1654 // We assume the class is not null.
1655 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1656 check->GetLoadClass(),
1657 check,
1658 check->GetDexPc(),
1659 true);
1660 codegen_->AddSlowPath(slow_path);
1661 GenerateClassInitializationCheck(slow_path,
1662 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1663}
1664
1665void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1666 Primitive::Type in_type = compare->InputAt(0)->GetType();
1667
Alexey Frunze299a9392015-12-08 16:08:02 -08001668 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001669
1670 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001671 case Primitive::kPrimBoolean:
1672 case Primitive::kPrimByte:
1673 case Primitive::kPrimShort:
1674 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001675 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676 case Primitive::kPrimLong:
1677 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001678 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1680 break;
1681
1682 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001683 case Primitive::kPrimDouble:
1684 locations->SetInAt(0, Location::RequiresFpuRegister());
1685 locations->SetInAt(1, Location::RequiresFpuRegister());
1686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001687 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001688
1689 default:
1690 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1691 }
1692}
1693
1694void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1695 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001696 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001697 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1698
1699 // 0 if: left == right
1700 // 1 if: left > right
1701 // -1 if: left < right
1702 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001703 case Primitive::kPrimBoolean:
1704 case Primitive::kPrimByte:
1705 case Primitive::kPrimShort:
1706 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001707 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001708 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001709 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001710 Location rhs_location = locations->InAt(1);
1711 bool use_imm = rhs_location.IsConstant();
1712 GpuRegister rhs = ZERO;
1713 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001714 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001715 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1716 if (value != 0) {
1717 rhs = AT;
1718 __ LoadConst64(rhs, value);
1719 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001720 } else {
1721 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1722 if (value != 0) {
1723 rhs = AT;
1724 __ LoadConst32(rhs, value);
1725 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001726 }
1727 } else {
1728 rhs = rhs_location.AsRegister<GpuRegister>();
1729 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001730 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001731 __ Slt(res, rhs, lhs);
1732 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001733 break;
1734 }
1735
Alexey Frunze299a9392015-12-08 16:08:02 -08001736 case Primitive::kPrimFloat: {
1737 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1738 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1739 Mips64Label done;
1740 __ CmpEqS(FTMP, lhs, rhs);
1741 __ LoadConst32(res, 0);
1742 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001743 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001744 __ CmpLtS(FTMP, lhs, rhs);
1745 __ LoadConst32(res, -1);
1746 __ Bc1nez(FTMP, &done);
1747 __ LoadConst32(res, 1);
1748 } else {
1749 __ CmpLtS(FTMP, rhs, lhs);
1750 __ LoadConst32(res, 1);
1751 __ Bc1nez(FTMP, &done);
1752 __ LoadConst32(res, -1);
1753 }
1754 __ Bind(&done);
1755 break;
1756 }
1757
Alexey Frunze4dda3372015-06-01 18:31:49 -07001758 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001759 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1760 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1761 Mips64Label done;
1762 __ CmpEqD(FTMP, lhs, rhs);
1763 __ LoadConst32(res, 0);
1764 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001765 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001766 __ CmpLtD(FTMP, lhs, rhs);
1767 __ LoadConst32(res, -1);
1768 __ Bc1nez(FTMP, &done);
1769 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001770 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001771 __ CmpLtD(FTMP, rhs, lhs);
1772 __ LoadConst32(res, 1);
1773 __ Bc1nez(FTMP, &done);
1774 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001775 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001776 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001777 break;
1778 }
1779
1780 default:
1781 LOG(FATAL) << "Unimplemented compare type " << in_type;
1782 }
1783}
1784
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001786 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001787 switch (instruction->InputAt(0)->GetType()) {
1788 default:
1789 case Primitive::kPrimLong:
1790 locations->SetInAt(0, Location::RequiresRegister());
1791 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1792 break;
1793
1794 case Primitive::kPrimFloat:
1795 case Primitive::kPrimDouble:
1796 locations->SetInAt(0, Location::RequiresFpuRegister());
1797 locations->SetInAt(1, Location::RequiresFpuRegister());
1798 break;
1799 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001800 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1802 }
1803}
1804
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001806 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001807 return;
1808 }
1809
Alexey Frunze299a9392015-12-08 16:08:02 -08001810 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001811 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001812 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001813 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001814
Alexey Frunze299a9392015-12-08 16:08:02 -08001815 switch (type) {
1816 default:
1817 // Integer case.
1818 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1819 return;
1820 case Primitive::kPrimLong:
1821 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1822 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001823
Alexey Frunze299a9392015-12-08 16:08:02 -08001824 case Primitive::kPrimFloat:
1825 case Primitive::kPrimDouble:
1826 // TODO: don't use branches.
1827 GenerateFpCompareAndBranch(instruction->GetCondition(),
1828 instruction->IsGtBias(),
1829 type,
1830 locations,
1831 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001832 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001833 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001834
1835 // Convert the branches into the result.
1836 Mips64Label done;
1837
1838 // False case: result = 0.
1839 __ LoadConst32(dst, 0);
1840 __ Bc(&done);
1841
1842 // True case: result = 1.
1843 __ Bind(&true_label);
1844 __ LoadConst32(dst, 1);
1845 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001846}
1847
Alexey Frunzec857c742015-09-23 15:12:39 -07001848void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1849 DCHECK(instruction->IsDiv() || instruction->IsRem());
1850 Primitive::Type type = instruction->GetResultType();
1851
1852 LocationSummary* locations = instruction->GetLocations();
1853 Location second = locations->InAt(1);
1854 DCHECK(second.IsConstant());
1855
1856 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1857 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1858 int64_t imm = Int64FromConstant(second.GetConstant());
1859 DCHECK(imm == 1 || imm == -1);
1860
1861 if (instruction->IsRem()) {
1862 __ Move(out, ZERO);
1863 } else {
1864 if (imm == -1) {
1865 if (type == Primitive::kPrimInt) {
1866 __ Subu(out, ZERO, dividend);
1867 } else {
1868 DCHECK_EQ(type, Primitive::kPrimLong);
1869 __ Dsubu(out, ZERO, dividend);
1870 }
1871 } else if (out != dividend) {
1872 __ Move(out, dividend);
1873 }
1874 }
1875}
1876
1877void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1878 DCHECK(instruction->IsDiv() || instruction->IsRem());
1879 Primitive::Type type = instruction->GetResultType();
1880
1881 LocationSummary* locations = instruction->GetLocations();
1882 Location second = locations->InAt(1);
1883 DCHECK(second.IsConstant());
1884
1885 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1886 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1887 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001888 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001889 int ctz_imm = CTZ(abs_imm);
1890
1891 if (instruction->IsDiv()) {
1892 if (type == Primitive::kPrimInt) {
1893 if (ctz_imm == 1) {
1894 // Fast path for division by +/-2, which is very common.
1895 __ Srl(TMP, dividend, 31);
1896 } else {
1897 __ Sra(TMP, dividend, 31);
1898 __ Srl(TMP, TMP, 32 - ctz_imm);
1899 }
1900 __ Addu(out, dividend, TMP);
1901 __ Sra(out, out, ctz_imm);
1902 if (imm < 0) {
1903 __ Subu(out, ZERO, out);
1904 }
1905 } else {
1906 DCHECK_EQ(type, Primitive::kPrimLong);
1907 if (ctz_imm == 1) {
1908 // Fast path for division by +/-2, which is very common.
1909 __ Dsrl32(TMP, dividend, 31);
1910 } else {
1911 __ Dsra32(TMP, dividend, 31);
1912 if (ctz_imm > 32) {
1913 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1914 } else {
1915 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1916 }
1917 }
1918 __ Daddu(out, dividend, TMP);
1919 if (ctz_imm < 32) {
1920 __ Dsra(out, out, ctz_imm);
1921 } else {
1922 __ Dsra32(out, out, ctz_imm - 32);
1923 }
1924 if (imm < 0) {
1925 __ Dsubu(out, ZERO, out);
1926 }
1927 }
1928 } else {
1929 if (type == Primitive::kPrimInt) {
1930 if (ctz_imm == 1) {
1931 // Fast path for modulo +/-2, which is very common.
1932 __ Sra(TMP, dividend, 31);
1933 __ Subu(out, dividend, TMP);
1934 __ Andi(out, out, 1);
1935 __ Addu(out, out, TMP);
1936 } else {
1937 __ Sra(TMP, dividend, 31);
1938 __ Srl(TMP, TMP, 32 - ctz_imm);
1939 __ Addu(out, dividend, TMP);
1940 if (IsUint<16>(abs_imm - 1)) {
1941 __ Andi(out, out, abs_imm - 1);
1942 } else {
1943 __ Sll(out, out, 32 - ctz_imm);
1944 __ Srl(out, out, 32 - ctz_imm);
1945 }
1946 __ Subu(out, out, TMP);
1947 }
1948 } else {
1949 DCHECK_EQ(type, Primitive::kPrimLong);
1950 if (ctz_imm == 1) {
1951 // Fast path for modulo +/-2, which is very common.
1952 __ Dsra32(TMP, dividend, 31);
1953 __ Dsubu(out, dividend, TMP);
1954 __ Andi(out, out, 1);
1955 __ Daddu(out, out, TMP);
1956 } else {
1957 __ Dsra32(TMP, dividend, 31);
1958 if (ctz_imm > 32) {
1959 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1960 } else {
1961 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1962 }
1963 __ Daddu(out, dividend, TMP);
1964 if (IsUint<16>(abs_imm - 1)) {
1965 __ Andi(out, out, abs_imm - 1);
1966 } else {
1967 if (ctz_imm > 32) {
1968 __ Dsll(out, out, 64 - ctz_imm);
1969 __ Dsrl(out, out, 64 - ctz_imm);
1970 } else {
1971 __ Dsll32(out, out, 32 - ctz_imm);
1972 __ Dsrl32(out, out, 32 - ctz_imm);
1973 }
1974 }
1975 __ Dsubu(out, out, TMP);
1976 }
1977 }
1978 }
1979}
1980
1981void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1982 DCHECK(instruction->IsDiv() || instruction->IsRem());
1983
1984 LocationSummary* locations = instruction->GetLocations();
1985 Location second = locations->InAt(1);
1986 DCHECK(second.IsConstant());
1987
1988 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1989 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1990 int64_t imm = Int64FromConstant(second.GetConstant());
1991
1992 Primitive::Type type = instruction->GetResultType();
1993 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1994
1995 int64_t magic;
1996 int shift;
1997 CalculateMagicAndShiftForDivRem(imm,
1998 (type == Primitive::kPrimLong),
1999 &magic,
2000 &shift);
2001
2002 if (type == Primitive::kPrimInt) {
2003 __ LoadConst32(TMP, magic);
2004 __ MuhR6(TMP, dividend, TMP);
2005
2006 if (imm > 0 && magic < 0) {
2007 __ Addu(TMP, TMP, dividend);
2008 } else if (imm < 0 && magic > 0) {
2009 __ Subu(TMP, TMP, dividend);
2010 }
2011
2012 if (shift != 0) {
2013 __ Sra(TMP, TMP, shift);
2014 }
2015
2016 if (instruction->IsDiv()) {
2017 __ Sra(out, TMP, 31);
2018 __ Subu(out, TMP, out);
2019 } else {
2020 __ Sra(AT, TMP, 31);
2021 __ Subu(AT, TMP, AT);
2022 __ LoadConst32(TMP, imm);
2023 __ MulR6(TMP, AT, TMP);
2024 __ Subu(out, dividend, TMP);
2025 }
2026 } else {
2027 __ LoadConst64(TMP, magic);
2028 __ Dmuh(TMP, dividend, TMP);
2029
2030 if (imm > 0 && magic < 0) {
2031 __ Daddu(TMP, TMP, dividend);
2032 } else if (imm < 0 && magic > 0) {
2033 __ Dsubu(TMP, TMP, dividend);
2034 }
2035
2036 if (shift >= 32) {
2037 __ Dsra32(TMP, TMP, shift - 32);
2038 } else if (shift > 0) {
2039 __ Dsra(TMP, TMP, shift);
2040 }
2041
2042 if (instruction->IsDiv()) {
2043 __ Dsra32(out, TMP, 31);
2044 __ Dsubu(out, TMP, out);
2045 } else {
2046 __ Dsra32(AT, TMP, 31);
2047 __ Dsubu(AT, TMP, AT);
2048 __ LoadConst64(TMP, imm);
2049 __ Dmul(TMP, AT, TMP);
2050 __ Dsubu(out, dividend, TMP);
2051 }
2052 }
2053}
2054
2055void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2056 DCHECK(instruction->IsDiv() || instruction->IsRem());
2057 Primitive::Type type = instruction->GetResultType();
2058 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2059
2060 LocationSummary* locations = instruction->GetLocations();
2061 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2062 Location second = locations->InAt(1);
2063
2064 if (second.IsConstant()) {
2065 int64_t imm = Int64FromConstant(second.GetConstant());
2066 if (imm == 0) {
2067 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2068 } else if (imm == 1 || imm == -1) {
2069 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002070 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002071 DivRemByPowerOfTwo(instruction);
2072 } else {
2073 DCHECK(imm <= -2 || imm >= 2);
2074 GenerateDivRemWithAnyConstant(instruction);
2075 }
2076 } else {
2077 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2078 GpuRegister divisor = second.AsRegister<GpuRegister>();
2079 if (instruction->IsDiv()) {
2080 if (type == Primitive::kPrimInt)
2081 __ DivR6(out, dividend, divisor);
2082 else
2083 __ Ddiv(out, dividend, divisor);
2084 } else {
2085 if (type == Primitive::kPrimInt)
2086 __ ModR6(out, dividend, divisor);
2087 else
2088 __ Dmod(out, dividend, divisor);
2089 }
2090 }
2091}
2092
Alexey Frunze4dda3372015-06-01 18:31:49 -07002093void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2094 LocationSummary* locations =
2095 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2096 switch (div->GetResultType()) {
2097 case Primitive::kPrimInt:
2098 case Primitive::kPrimLong:
2099 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002100 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002101 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2102 break;
2103
2104 case Primitive::kPrimFloat:
2105 case Primitive::kPrimDouble:
2106 locations->SetInAt(0, Location::RequiresFpuRegister());
2107 locations->SetInAt(1, Location::RequiresFpuRegister());
2108 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2109 break;
2110
2111 default:
2112 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2113 }
2114}
2115
2116void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2117 Primitive::Type type = instruction->GetType();
2118 LocationSummary* locations = instruction->GetLocations();
2119
2120 switch (type) {
2121 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002122 case Primitive::kPrimLong:
2123 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002124 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002125 case Primitive::kPrimFloat:
2126 case Primitive::kPrimDouble: {
2127 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2128 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2129 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2130 if (type == Primitive::kPrimFloat)
2131 __ DivS(dst, lhs, rhs);
2132 else
2133 __ DivD(dst, lhs, rhs);
2134 break;
2135 }
2136 default:
2137 LOG(FATAL) << "Unexpected div type " << type;
2138 }
2139}
2140
2141void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002142 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2143 ? LocationSummary::kCallOnSlowPath
2144 : LocationSummary::kNoCall;
2145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002146 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2147 if (instruction->HasUses()) {
2148 locations->SetOut(Location::SameAsFirstInput());
2149 }
2150}
2151
2152void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2153 SlowPathCodeMIPS64* slow_path =
2154 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2155 codegen_->AddSlowPath(slow_path);
2156 Location value = instruction->GetLocations()->InAt(0);
2157
2158 Primitive::Type type = instruction->GetType();
2159
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002160 if (!Primitive::IsIntegralType(type)) {
2161 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002162 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002163 }
2164
2165 if (value.IsConstant()) {
2166 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2167 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002168 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002169 } else {
2170 // A division by a non-null constant is valid. We don't need to perform
2171 // any check, so simply fall through.
2172 }
2173 } else {
2174 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2175 }
2176}
2177
2178void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2179 LocationSummary* locations =
2180 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2181 locations->SetOut(Location::ConstantLocation(constant));
2182}
2183
2184void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2185 // Will be generated at use site.
2186}
2187
2188void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2189 exit->SetLocations(nullptr);
2190}
2191
2192void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2193}
2194
2195void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2196 LocationSummary* locations =
2197 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2198 locations->SetOut(Location::ConstantLocation(constant));
2199}
2200
2201void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2202 // Will be generated at use site.
2203}
2204
David Brazdilfc6a86a2015-06-26 10:33:45 +00002205void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002206 DCHECK(!successor->IsExitBlock());
2207 HBasicBlock* block = got->GetBlock();
2208 HInstruction* previous = got->GetPrevious();
2209 HLoopInformation* info = block->GetLoopInformation();
2210
2211 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2212 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2213 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2214 return;
2215 }
2216 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2217 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2218 }
2219 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002220 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002221 }
2222}
2223
David Brazdilfc6a86a2015-06-26 10:33:45 +00002224void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2225 got->SetLocations(nullptr);
2226}
2227
2228void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2229 HandleGoto(got, got->GetSuccessor());
2230}
2231
2232void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2233 try_boundary->SetLocations(nullptr);
2234}
2235
2236void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2237 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2238 if (!successor->IsExitBlock()) {
2239 HandleGoto(try_boundary, successor);
2240 }
2241}
2242
Alexey Frunze299a9392015-12-08 16:08:02 -08002243void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2244 bool is64bit,
2245 LocationSummary* locations) {
2246 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2247 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2248 Location rhs_location = locations->InAt(1);
2249 GpuRegister rhs_reg = ZERO;
2250 int64_t rhs_imm = 0;
2251 bool use_imm = rhs_location.IsConstant();
2252 if (use_imm) {
2253 if (is64bit) {
2254 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2255 } else {
2256 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2257 }
2258 } else {
2259 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2260 }
2261 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2262
2263 switch (cond) {
2264 case kCondEQ:
2265 case kCondNE:
2266 if (use_imm && IsUint<16>(rhs_imm)) {
2267 __ Xori(dst, lhs, rhs_imm);
2268 } else {
2269 if (use_imm) {
2270 rhs_reg = TMP;
2271 __ LoadConst64(rhs_reg, rhs_imm);
2272 }
2273 __ Xor(dst, lhs, rhs_reg);
2274 }
2275 if (cond == kCondEQ) {
2276 __ Sltiu(dst, dst, 1);
2277 } else {
2278 __ Sltu(dst, ZERO, dst);
2279 }
2280 break;
2281
2282 case kCondLT:
2283 case kCondGE:
2284 if (use_imm && IsInt<16>(rhs_imm)) {
2285 __ Slti(dst, lhs, rhs_imm);
2286 } else {
2287 if (use_imm) {
2288 rhs_reg = TMP;
2289 __ LoadConst64(rhs_reg, rhs_imm);
2290 }
2291 __ Slt(dst, lhs, rhs_reg);
2292 }
2293 if (cond == kCondGE) {
2294 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2295 // only the slt instruction but no sge.
2296 __ Xori(dst, dst, 1);
2297 }
2298 break;
2299
2300 case kCondLE:
2301 case kCondGT:
2302 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2303 // Simulate lhs <= rhs via lhs < rhs + 1.
2304 __ Slti(dst, lhs, rhs_imm_plus_one);
2305 if (cond == kCondGT) {
2306 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2307 // only the slti instruction but no sgti.
2308 __ Xori(dst, dst, 1);
2309 }
2310 } else {
2311 if (use_imm) {
2312 rhs_reg = TMP;
2313 __ LoadConst64(rhs_reg, rhs_imm);
2314 }
2315 __ Slt(dst, rhs_reg, lhs);
2316 if (cond == kCondLE) {
2317 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2318 // only the slt instruction but no sle.
2319 __ Xori(dst, dst, 1);
2320 }
2321 }
2322 break;
2323
2324 case kCondB:
2325 case kCondAE:
2326 if (use_imm && IsInt<16>(rhs_imm)) {
2327 // Sltiu sign-extends its 16-bit immediate operand before
2328 // the comparison and thus lets us compare directly with
2329 // unsigned values in the ranges [0, 0x7fff] and
2330 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2331 __ Sltiu(dst, lhs, rhs_imm);
2332 } else {
2333 if (use_imm) {
2334 rhs_reg = TMP;
2335 __ LoadConst64(rhs_reg, rhs_imm);
2336 }
2337 __ Sltu(dst, lhs, rhs_reg);
2338 }
2339 if (cond == kCondAE) {
2340 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2341 // only the sltu instruction but no sgeu.
2342 __ Xori(dst, dst, 1);
2343 }
2344 break;
2345
2346 case kCondBE:
2347 case kCondA:
2348 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2349 // Simulate lhs <= rhs via lhs < rhs + 1.
2350 // Note that this only works if rhs + 1 does not overflow
2351 // to 0, hence the check above.
2352 // Sltiu sign-extends its 16-bit immediate operand before
2353 // the comparison and thus lets us compare directly with
2354 // unsigned values in the ranges [0, 0x7fff] and
2355 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2356 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2357 if (cond == kCondA) {
2358 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2359 // only the sltiu instruction but no sgtiu.
2360 __ Xori(dst, dst, 1);
2361 }
2362 } else {
2363 if (use_imm) {
2364 rhs_reg = TMP;
2365 __ LoadConst64(rhs_reg, rhs_imm);
2366 }
2367 __ Sltu(dst, rhs_reg, lhs);
2368 if (cond == kCondBE) {
2369 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2370 // only the sltu instruction but no sleu.
2371 __ Xori(dst, dst, 1);
2372 }
2373 }
2374 break;
2375 }
2376}
2377
2378void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2379 bool is64bit,
2380 LocationSummary* locations,
2381 Mips64Label* label) {
2382 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2383 Location rhs_location = locations->InAt(1);
2384 GpuRegister rhs_reg = ZERO;
2385 int64_t rhs_imm = 0;
2386 bool use_imm = rhs_location.IsConstant();
2387 if (use_imm) {
2388 if (is64bit) {
2389 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2390 } else {
2391 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2392 }
2393 } else {
2394 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2395 }
2396
2397 if (use_imm && rhs_imm == 0) {
2398 switch (cond) {
2399 case kCondEQ:
2400 case kCondBE: // <= 0 if zero
2401 __ Beqzc(lhs, label);
2402 break;
2403 case kCondNE:
2404 case kCondA: // > 0 if non-zero
2405 __ Bnezc(lhs, label);
2406 break;
2407 case kCondLT:
2408 __ Bltzc(lhs, label);
2409 break;
2410 case kCondGE:
2411 __ Bgezc(lhs, label);
2412 break;
2413 case kCondLE:
2414 __ Blezc(lhs, label);
2415 break;
2416 case kCondGT:
2417 __ Bgtzc(lhs, label);
2418 break;
2419 case kCondB: // always false
2420 break;
2421 case kCondAE: // always true
2422 __ Bc(label);
2423 break;
2424 }
2425 } else {
2426 if (use_imm) {
2427 rhs_reg = TMP;
2428 __ LoadConst64(rhs_reg, rhs_imm);
2429 }
2430 switch (cond) {
2431 case kCondEQ:
2432 __ Beqc(lhs, rhs_reg, label);
2433 break;
2434 case kCondNE:
2435 __ Bnec(lhs, rhs_reg, label);
2436 break;
2437 case kCondLT:
2438 __ Bltc(lhs, rhs_reg, label);
2439 break;
2440 case kCondGE:
2441 __ Bgec(lhs, rhs_reg, label);
2442 break;
2443 case kCondLE:
2444 __ Bgec(rhs_reg, lhs, label);
2445 break;
2446 case kCondGT:
2447 __ Bltc(rhs_reg, lhs, label);
2448 break;
2449 case kCondB:
2450 __ Bltuc(lhs, rhs_reg, label);
2451 break;
2452 case kCondAE:
2453 __ Bgeuc(lhs, rhs_reg, label);
2454 break;
2455 case kCondBE:
2456 __ Bgeuc(rhs_reg, lhs, label);
2457 break;
2458 case kCondA:
2459 __ Bltuc(rhs_reg, lhs, label);
2460 break;
2461 }
2462 }
2463}
2464
2465void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2466 bool gt_bias,
2467 Primitive::Type type,
2468 LocationSummary* locations,
2469 Mips64Label* label) {
2470 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2471 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2472 if (type == Primitive::kPrimFloat) {
2473 switch (cond) {
2474 case kCondEQ:
2475 __ CmpEqS(FTMP, lhs, rhs);
2476 __ Bc1nez(FTMP, label);
2477 break;
2478 case kCondNE:
2479 __ CmpEqS(FTMP, lhs, rhs);
2480 __ Bc1eqz(FTMP, label);
2481 break;
2482 case kCondLT:
2483 if (gt_bias) {
2484 __ CmpLtS(FTMP, lhs, rhs);
2485 } else {
2486 __ CmpUltS(FTMP, lhs, rhs);
2487 }
2488 __ Bc1nez(FTMP, label);
2489 break;
2490 case kCondLE:
2491 if (gt_bias) {
2492 __ CmpLeS(FTMP, lhs, rhs);
2493 } else {
2494 __ CmpUleS(FTMP, lhs, rhs);
2495 }
2496 __ Bc1nez(FTMP, label);
2497 break;
2498 case kCondGT:
2499 if (gt_bias) {
2500 __ CmpUltS(FTMP, rhs, lhs);
2501 } else {
2502 __ CmpLtS(FTMP, rhs, lhs);
2503 }
2504 __ Bc1nez(FTMP, label);
2505 break;
2506 case kCondGE:
2507 if (gt_bias) {
2508 __ CmpUleS(FTMP, rhs, lhs);
2509 } else {
2510 __ CmpLeS(FTMP, rhs, lhs);
2511 }
2512 __ Bc1nez(FTMP, label);
2513 break;
2514 default:
2515 LOG(FATAL) << "Unexpected non-floating-point condition";
2516 }
2517 } else {
2518 DCHECK_EQ(type, Primitive::kPrimDouble);
2519 switch (cond) {
2520 case kCondEQ:
2521 __ CmpEqD(FTMP, lhs, rhs);
2522 __ Bc1nez(FTMP, label);
2523 break;
2524 case kCondNE:
2525 __ CmpEqD(FTMP, lhs, rhs);
2526 __ Bc1eqz(FTMP, label);
2527 break;
2528 case kCondLT:
2529 if (gt_bias) {
2530 __ CmpLtD(FTMP, lhs, rhs);
2531 } else {
2532 __ CmpUltD(FTMP, lhs, rhs);
2533 }
2534 __ Bc1nez(FTMP, label);
2535 break;
2536 case kCondLE:
2537 if (gt_bias) {
2538 __ CmpLeD(FTMP, lhs, rhs);
2539 } else {
2540 __ CmpUleD(FTMP, lhs, rhs);
2541 }
2542 __ Bc1nez(FTMP, label);
2543 break;
2544 case kCondGT:
2545 if (gt_bias) {
2546 __ CmpUltD(FTMP, rhs, lhs);
2547 } else {
2548 __ CmpLtD(FTMP, rhs, lhs);
2549 }
2550 __ Bc1nez(FTMP, label);
2551 break;
2552 case kCondGE:
2553 if (gt_bias) {
2554 __ CmpUleD(FTMP, rhs, lhs);
2555 } else {
2556 __ CmpLeD(FTMP, rhs, lhs);
2557 }
2558 __ Bc1nez(FTMP, label);
2559 break;
2560 default:
2561 LOG(FATAL) << "Unexpected non-floating-point condition";
2562 }
2563 }
2564}
2565
Alexey Frunze4dda3372015-06-01 18:31:49 -07002566void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002567 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002568 Mips64Label* true_target,
2569 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002570 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002571
David Brazdil0debae72015-11-12 18:37:00 +00002572 if (true_target == nullptr && false_target == nullptr) {
2573 // Nothing to do. The code always falls through.
2574 return;
2575 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002576 // Constant condition, statically compared against "true" (integer value 1).
2577 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002578 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002579 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002580 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002581 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002582 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002583 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002584 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002585 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002586 }
David Brazdil0debae72015-11-12 18:37:00 +00002587 return;
2588 }
2589
2590 // The following code generates these patterns:
2591 // (1) true_target == nullptr && false_target != nullptr
2592 // - opposite condition true => branch to false_target
2593 // (2) true_target != nullptr && false_target == nullptr
2594 // - condition true => branch to true_target
2595 // (3) true_target != nullptr && false_target != nullptr
2596 // - condition true => branch to true_target
2597 // - branch to false_target
2598 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002599 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002600 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002601 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002602 if (true_target == nullptr) {
2603 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2604 } else {
2605 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2606 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002607 } else {
2608 // The condition instruction has not been materialized, use its inputs as
2609 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002610 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002611 Primitive::Type type = condition->InputAt(0)->GetType();
2612 LocationSummary* locations = cond->GetLocations();
2613 IfCondition if_cond = condition->GetCondition();
2614 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002615
David Brazdil0debae72015-11-12 18:37:00 +00002616 if (true_target == nullptr) {
2617 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002618 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002619 }
2620
Alexey Frunze299a9392015-12-08 16:08:02 -08002621 switch (type) {
2622 default:
2623 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2624 break;
2625 case Primitive::kPrimLong:
2626 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2627 break;
2628 case Primitive::kPrimFloat:
2629 case Primitive::kPrimDouble:
2630 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2631 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002632 }
2633 }
David Brazdil0debae72015-11-12 18:37:00 +00002634
2635 // If neither branch falls through (case 3), the conditional branch to `true_target`
2636 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2637 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002638 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002639 }
2640}
2641
2642void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2643 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002644 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002645 locations->SetInAt(0, Location::RequiresRegister());
2646 }
2647}
2648
2649void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002650 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2651 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002652 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002653 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002654 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002655 nullptr : codegen_->GetLabelOf(false_successor);
2656 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002657}
2658
2659void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2660 LocationSummary* locations = new (GetGraph()->GetArena())
2661 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002662 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002663 locations->SetInAt(0, Location::RequiresRegister());
2664 }
2665}
2666
2667void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002668 SlowPathCodeMIPS64* slow_path =
2669 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002670 GenerateTestAndBranch(deoptimize,
2671 /* condition_input_index */ 0,
2672 slow_path->GetEntryLabel(),
2673 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002674}
2675
David Brazdil74eb1b22015-12-14 11:44:01 +00002676void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2677 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2678 if (Primitive::IsFloatingPointType(select->GetType())) {
2679 locations->SetInAt(0, Location::RequiresFpuRegister());
2680 locations->SetInAt(1, Location::RequiresFpuRegister());
2681 } else {
2682 locations->SetInAt(0, Location::RequiresRegister());
2683 locations->SetInAt(1, Location::RequiresRegister());
2684 }
2685 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2686 locations->SetInAt(2, Location::RequiresRegister());
2687 }
2688 locations->SetOut(Location::SameAsFirstInput());
2689}
2690
2691void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2692 LocationSummary* locations = select->GetLocations();
2693 Mips64Label false_target;
2694 GenerateTestAndBranch(select,
2695 /* condition_input_index */ 2,
2696 /* true_target */ nullptr,
2697 &false_target);
2698 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2699 __ Bind(&false_target);
2700}
2701
David Srbecky0cf44932015-12-09 14:09:59 +00002702void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2703 new (GetGraph()->GetArena()) LocationSummary(info);
2704}
2705
David Srbeckyd28f4a02016-03-14 17:14:24 +00002706void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2707 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002708}
2709
2710void CodeGeneratorMIPS64::GenerateNop() {
2711 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002712}
2713
Alexey Frunze4dda3372015-06-01 18:31:49 -07002714void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2715 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2716 LocationSummary* locations =
2717 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2718 locations->SetInAt(0, Location::RequiresRegister());
2719 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2720 locations->SetOut(Location::RequiresFpuRegister());
2721 } else {
2722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2723 }
2724}
2725
2726void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2727 const FieldInfo& field_info) {
2728 Primitive::Type type = field_info.GetFieldType();
2729 LocationSummary* locations = instruction->GetLocations();
2730 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2731 LoadOperandType load_type = kLoadUnsignedByte;
2732 switch (type) {
2733 case Primitive::kPrimBoolean:
2734 load_type = kLoadUnsignedByte;
2735 break;
2736 case Primitive::kPrimByte:
2737 load_type = kLoadSignedByte;
2738 break;
2739 case Primitive::kPrimShort:
2740 load_type = kLoadSignedHalfword;
2741 break;
2742 case Primitive::kPrimChar:
2743 load_type = kLoadUnsignedHalfword;
2744 break;
2745 case Primitive::kPrimInt:
2746 case Primitive::kPrimFloat:
2747 load_type = kLoadWord;
2748 break;
2749 case Primitive::kPrimLong:
2750 case Primitive::kPrimDouble:
2751 load_type = kLoadDoubleword;
2752 break;
2753 case Primitive::kPrimNot:
2754 load_type = kLoadUnsignedWord;
2755 break;
2756 case Primitive::kPrimVoid:
2757 LOG(FATAL) << "Unreachable type " << type;
2758 UNREACHABLE();
2759 }
2760 if (!Primitive::IsFloatingPointType(type)) {
2761 DCHECK(locations->Out().IsRegister());
2762 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2763 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2764 } else {
2765 DCHECK(locations->Out().IsFpuRegister());
2766 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2767 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2768 }
2769
2770 codegen_->MaybeRecordImplicitNullCheck(instruction);
2771 // TODO: memory barrier?
2772}
2773
2774void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2775 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2776 LocationSummary* locations =
2777 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2778 locations->SetInAt(0, Location::RequiresRegister());
2779 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2780 locations->SetInAt(1, Location::RequiresFpuRegister());
2781 } else {
2782 locations->SetInAt(1, Location::RequiresRegister());
2783 }
2784}
2785
2786void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002787 const FieldInfo& field_info,
2788 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002789 Primitive::Type type = field_info.GetFieldType();
2790 LocationSummary* locations = instruction->GetLocations();
2791 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2792 StoreOperandType store_type = kStoreByte;
2793 switch (type) {
2794 case Primitive::kPrimBoolean:
2795 case Primitive::kPrimByte:
2796 store_type = kStoreByte;
2797 break;
2798 case Primitive::kPrimShort:
2799 case Primitive::kPrimChar:
2800 store_type = kStoreHalfword;
2801 break;
2802 case Primitive::kPrimInt:
2803 case Primitive::kPrimFloat:
2804 case Primitive::kPrimNot:
2805 store_type = kStoreWord;
2806 break;
2807 case Primitive::kPrimLong:
2808 case Primitive::kPrimDouble:
2809 store_type = kStoreDoubleword;
2810 break;
2811 case Primitive::kPrimVoid:
2812 LOG(FATAL) << "Unreachable type " << type;
2813 UNREACHABLE();
2814 }
2815 if (!Primitive::IsFloatingPointType(type)) {
2816 DCHECK(locations->InAt(1).IsRegister());
2817 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2818 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2819 } else {
2820 DCHECK(locations->InAt(1).IsFpuRegister());
2821 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2822 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2823 }
2824
2825 codegen_->MaybeRecordImplicitNullCheck(instruction);
2826 // TODO: memory barriers?
2827 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2828 DCHECK(locations->InAt(1).IsRegister());
2829 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002830 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002831 }
2832}
2833
2834void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2835 HandleFieldGet(instruction, instruction->GetFieldInfo());
2836}
2837
2838void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2839 HandleFieldGet(instruction, instruction->GetFieldInfo());
2840}
2841
2842void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2843 HandleFieldSet(instruction, instruction->GetFieldInfo());
2844}
2845
2846void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002847 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002848}
2849
2850void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2851 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002852 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2854 locations->SetInAt(0, Location::RequiresRegister());
2855 locations->SetInAt(1, Location::RequiresRegister());
2856 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002857 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002858 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2859}
2860
2861void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2862 LocationSummary* locations = instruction->GetLocations();
2863 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2864 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2865 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2866
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002867 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002868
2869 // Return 0 if `obj` is null.
2870 // TODO: Avoid this check if we know `obj` is not null.
2871 __ Move(out, ZERO);
2872 __ Beqzc(obj, &done);
2873
2874 // Compare the class of `obj` with `cls`.
2875 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002876 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002877 // Classes must be equal for the instanceof to succeed.
2878 __ Xor(out, out, cls);
2879 __ Sltiu(out, out, 1);
2880 } else {
2881 // If the classes are not equal, we go into a slow path.
2882 DCHECK(locations->OnlyCallsOnSlowPath());
2883 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002884 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002885 codegen_->AddSlowPath(slow_path);
2886 __ Bnec(out, cls, slow_path->GetEntryLabel());
2887 __ LoadConst32(out, 1);
2888 __ Bind(slow_path->GetExitLabel());
2889 }
2890
2891 __ Bind(&done);
2892}
2893
2894void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2895 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2896 locations->SetOut(Location::ConstantLocation(constant));
2897}
2898
2899void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2900 // Will be generated at use site.
2901}
2902
2903void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2904 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2905 locations->SetOut(Location::ConstantLocation(constant));
2906}
2907
2908void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2909 // Will be generated at use site.
2910}
2911
Calin Juravle175dc732015-08-25 15:42:32 +01002912void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2913 // The trampoline uses the same calling convention as dex calling conventions,
2914 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2915 // the method_idx.
2916 HandleInvoke(invoke);
2917}
2918
2919void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2920 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2921}
2922
Alexey Frunze4dda3372015-06-01 18:31:49 -07002923void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2924 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2925 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2926}
2927
2928void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2929 HandleInvoke(invoke);
2930 // The register T0 is required to be used for the hidden argument in
2931 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2932 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2933}
2934
2935void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2936 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2937 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002938 Location receiver = invoke->GetLocations()->InAt(0);
2939 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02002940 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002941
2942 // Set the hidden argument.
2943 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2944 invoke->GetDexMethodIndex());
2945
2946 // temp = object->GetClass();
2947 if (receiver.IsStackSlot()) {
2948 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2949 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2950 } else {
2951 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2952 }
2953 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nelli Kimbadee982016-05-13 13:08:53 +03002954 __ LoadFromOffset(kLoadDoubleword, temp, temp,
2955 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
2956 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2957 invoke->GetImtIndex() % ImTable::kSize, kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002958 // temp = temp->GetImtEntryAt(method_offset);
2959 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2960 // T9 = temp->GetEntryPoint();
2961 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2962 // T9();
2963 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002964 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002965 DCHECK(!codegen_->IsLeafMethod());
2966 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2967}
2968
2969void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002970 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2971 if (intrinsic.TryDispatch(invoke)) {
2972 return;
2973 }
2974
Alexey Frunze4dda3372015-06-01 18:31:49 -07002975 HandleInvoke(invoke);
2976}
2977
2978void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002979 // Explicit clinit checks triggered by static invokes must have been pruned by
2980 // art::PrepareForRegisterAllocation.
2981 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002982
Chris Larsen3039e382015-08-26 07:54:08 -07002983 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2984 if (intrinsic.TryDispatch(invoke)) {
2985 return;
2986 }
2987
Alexey Frunze4dda3372015-06-01 18:31:49 -07002988 HandleInvoke(invoke);
2989
2990 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
2991 // clobbering somewhere else, reduce further register pressure by avoiding
2992 // allocation of a register for the current method pointer like on x86 baseline.
2993 // TODO: remove this once all the issues with register saving/restoring are
2994 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002995 if (invoke->HasCurrentMethodInput()) {
2996 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00002997 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002998 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002999 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003000 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003001 }
3002}
3003
Chris Larsen3039e382015-08-26 07:54:08 -07003004static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003005 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003006 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3007 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003008 return true;
3009 }
3010 return false;
3011}
3012
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003013HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
3014 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3015 // TODO: Implement other kinds.
3016 return HLoadString::LoadKind::kDexCacheViaMethod;
3017}
3018
Vladimir Markodc151b22015-10-15 18:02:30 +01003019HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3020 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3021 MethodReference target_method ATTRIBUTE_UNUSED) {
3022 switch (desired_dispatch_info.method_load_kind) {
3023 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3024 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3025 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3026 return HInvokeStaticOrDirect::DispatchInfo {
3027 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3028 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3029 0u,
3030 0u
3031 };
3032 default:
3033 break;
3034 }
3035 switch (desired_dispatch_info.code_ptr_location) {
3036 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3037 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3038 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3039 return HInvokeStaticOrDirect::DispatchInfo {
3040 desired_dispatch_info.method_load_kind,
3041 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3042 desired_dispatch_info.method_load_data,
3043 0u
3044 };
3045 default:
3046 return desired_dispatch_info;
3047 }
3048}
3049
Alexey Frunze4dda3372015-06-01 18:31:49 -07003050void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3051 // All registers are assumed to be correctly set up per the calling convention.
3052
Vladimir Marko58155012015-08-19 12:49:41 +00003053 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3054 switch (invoke->GetMethodLoadKind()) {
3055 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3056 // temp = thread->string_init_entrypoint
3057 __ LoadFromOffset(kLoadDoubleword,
3058 temp.AsRegister<GpuRegister>(),
3059 TR,
3060 invoke->GetStringInitOffset());
3061 break;
3062 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003063 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003064 break;
3065 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3066 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3067 break;
3068 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003069 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003070 // TODO: Implement these types.
3071 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3072 LOG(FATAL) << "Unsupported";
3073 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003074 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003075 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003076 GpuRegister reg = temp.AsRegister<GpuRegister>();
3077 GpuRegister method_reg;
3078 if (current_method.IsRegister()) {
3079 method_reg = current_method.AsRegister<GpuRegister>();
3080 } else {
3081 // TODO: use the appropriate DCHECK() here if possible.
3082 // DCHECK(invoke->GetLocations()->Intrinsified());
3083 DCHECK(!current_method.IsValid());
3084 method_reg = reg;
3085 __ Ld(reg, SP, kCurrentMethodStackOffset);
3086 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003087
Vladimir Marko58155012015-08-19 12:49:41 +00003088 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003089 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003090 reg,
3091 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003092 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003093 // temp = temp[index_in_cache];
3094 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3095 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003096 __ LoadFromOffset(kLoadDoubleword,
3097 reg,
3098 reg,
3099 CodeGenerator::GetCachePointerOffset(index_in_cache));
3100 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003101 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003102 }
3103
Vladimir Marko58155012015-08-19 12:49:41 +00003104 switch (invoke->GetCodePtrLocation()) {
3105 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003106 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003107 break;
3108 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3109 // LR = invoke->GetDirectCodePtr();
3110 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3111 // LR()
3112 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003113 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003114 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003115 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003116 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3117 // TODO: Implement these types.
3118 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3119 LOG(FATAL) << "Unsupported";
3120 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003121 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3122 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3123 __ LoadFromOffset(kLoadDoubleword,
3124 T9,
3125 callee_method.AsRegister<GpuRegister>(),
3126 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003127 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003128 // T9()
3129 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003130 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003131 break;
3132 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003133 DCHECK(!IsLeafMethod());
3134}
3135
3136void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003137 // Explicit clinit checks triggered by static invokes must have been pruned by
3138 // art::PrepareForRegisterAllocation.
3139 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003140
3141 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3142 return;
3143 }
3144
3145 LocationSummary* locations = invoke->GetLocations();
3146 codegen_->GenerateStaticOrDirectCall(invoke,
3147 locations->HasTemps()
3148 ? locations->GetTemp(0)
3149 : Location::NoLocation());
3150 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3151}
3152
Alexey Frunze53afca12015-11-05 16:34:23 -08003153void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003154 // Use the calling convention instead of the location of the receiver, as
3155 // intrinsics may have put the receiver in a different register. In the intrinsics
3156 // slow path, the arguments have been moved to the right place, so here we are
3157 // guaranteed that the receiver is the first register of the calling convention.
3158 InvokeDexCallingConvention calling_convention;
3159 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3160
Alexey Frunze53afca12015-11-05 16:34:23 -08003161 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003162 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3163 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3164 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003165 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003166
3167 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003168 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003169 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003170 // temp = temp->GetMethodAt(method_offset);
3171 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3172 // T9 = temp->GetEntryPoint();
3173 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3174 // T9();
3175 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003176 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003177}
3178
3179void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3180 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3181 return;
3182 }
3183
3184 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003185 DCHECK(!codegen_->IsLeafMethod());
3186 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3187}
3188
3189void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003190 InvokeRuntimeCallingConvention calling_convention;
3191 CodeGenerator::CreateLoadClassLocationSummary(
3192 cls,
3193 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003194 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003195}
3196
3197void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3198 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003199 if (cls->NeedsAccessCheck()) {
3200 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3201 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3202 cls,
3203 cls->GetDexPc(),
3204 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003205 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003206 return;
3207 }
3208
3209 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3210 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3211 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003212 DCHECK(!cls->CanCallRuntime());
3213 DCHECK(!cls->MustGenerateClinitCheck());
3214 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3215 ArtMethod::DeclaringClassOffset().Int32Value());
3216 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003217 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3218 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003219 __ LoadFromOffset(
3220 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003221 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003222 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3223 DCHECK(cls->CanCallRuntime());
3224 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3225 cls,
3226 cls,
3227 cls->GetDexPc(),
3228 cls->MustGenerateClinitCheck());
3229 codegen_->AddSlowPath(slow_path);
3230 if (!cls->IsInDexCache()) {
3231 __ Beqzc(out, slow_path->GetEntryLabel());
3232 }
3233 if (cls->MustGenerateClinitCheck()) {
3234 GenerateClassInitializationCheck(slow_path, out);
3235 } else {
3236 __ Bind(slow_path->GetExitLabel());
3237 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003238 }
3239 }
3240}
3241
David Brazdilcb1c0552015-08-04 16:22:25 +01003242static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003243 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003244}
3245
Alexey Frunze4dda3372015-06-01 18:31:49 -07003246void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3247 LocationSummary* locations =
3248 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3249 locations->SetOut(Location::RequiresRegister());
3250}
3251
3252void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3253 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003254 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3255}
3256
3257void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3258 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3259}
3260
3261void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3262 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003263}
3264
Alexey Frunze4dda3372015-06-01 18:31:49 -07003265void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003266 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3267 ? LocationSummary::kCallOnSlowPath
3268 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003270 locations->SetInAt(0, Location::RequiresRegister());
3271 locations->SetOut(Location::RequiresRegister());
3272}
3273
3274void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003275 LocationSummary* locations = load->GetLocations();
3276 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3277 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3278 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3279 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003280 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003281 __ LoadFromOffset(
3282 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003283 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003284
3285 if (!load->IsInDexCache()) {
3286 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3287 codegen_->AddSlowPath(slow_path);
3288 __ Beqzc(out, slow_path->GetEntryLabel());
3289 __ Bind(slow_path->GetExitLabel());
3290 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003291}
3292
Alexey Frunze4dda3372015-06-01 18:31:49 -07003293void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3294 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3295 locations->SetOut(Location::ConstantLocation(constant));
3296}
3297
3298void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3299 // Will be generated at use site.
3300}
3301
3302void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3303 LocationSummary* locations =
3304 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3305 InvokeRuntimeCallingConvention calling_convention;
3306 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3307}
3308
3309void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3310 codegen_->InvokeRuntime(instruction->IsEnter()
3311 ? QUICK_ENTRY_POINT(pLockObject)
3312 : QUICK_ENTRY_POINT(pUnlockObject),
3313 instruction,
3314 instruction->GetDexPc(),
3315 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003316 if (instruction->IsEnter()) {
3317 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3318 } else {
3319 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3320 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003321}
3322
3323void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3324 LocationSummary* locations =
3325 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3326 switch (mul->GetResultType()) {
3327 case Primitive::kPrimInt:
3328 case Primitive::kPrimLong:
3329 locations->SetInAt(0, Location::RequiresRegister());
3330 locations->SetInAt(1, Location::RequiresRegister());
3331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3332 break;
3333
3334 case Primitive::kPrimFloat:
3335 case Primitive::kPrimDouble:
3336 locations->SetInAt(0, Location::RequiresFpuRegister());
3337 locations->SetInAt(1, Location::RequiresFpuRegister());
3338 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3339 break;
3340
3341 default:
3342 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3343 }
3344}
3345
3346void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3347 Primitive::Type type = instruction->GetType();
3348 LocationSummary* locations = instruction->GetLocations();
3349
3350 switch (type) {
3351 case Primitive::kPrimInt:
3352 case Primitive::kPrimLong: {
3353 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3354 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3355 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3356 if (type == Primitive::kPrimInt)
3357 __ MulR6(dst, lhs, rhs);
3358 else
3359 __ Dmul(dst, lhs, rhs);
3360 break;
3361 }
3362 case Primitive::kPrimFloat:
3363 case Primitive::kPrimDouble: {
3364 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3365 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3366 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3367 if (type == Primitive::kPrimFloat)
3368 __ MulS(dst, lhs, rhs);
3369 else
3370 __ MulD(dst, lhs, rhs);
3371 break;
3372 }
3373 default:
3374 LOG(FATAL) << "Unexpected mul type " << type;
3375 }
3376}
3377
3378void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3379 LocationSummary* locations =
3380 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3381 switch (neg->GetResultType()) {
3382 case Primitive::kPrimInt:
3383 case Primitive::kPrimLong:
3384 locations->SetInAt(0, Location::RequiresRegister());
3385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3386 break;
3387
3388 case Primitive::kPrimFloat:
3389 case Primitive::kPrimDouble:
3390 locations->SetInAt(0, Location::RequiresFpuRegister());
3391 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3392 break;
3393
3394 default:
3395 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3396 }
3397}
3398
3399void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3400 Primitive::Type type = instruction->GetType();
3401 LocationSummary* locations = instruction->GetLocations();
3402
3403 switch (type) {
3404 case Primitive::kPrimInt:
3405 case Primitive::kPrimLong: {
3406 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3407 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3408 if (type == Primitive::kPrimInt)
3409 __ Subu(dst, ZERO, src);
3410 else
3411 __ Dsubu(dst, ZERO, src);
3412 break;
3413 }
3414 case Primitive::kPrimFloat:
3415 case Primitive::kPrimDouble: {
3416 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3417 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3418 if (type == Primitive::kPrimFloat)
3419 __ NegS(dst, src);
3420 else
3421 __ NegD(dst, src);
3422 break;
3423 }
3424 default:
3425 LOG(FATAL) << "Unexpected neg type " << type;
3426 }
3427}
3428
3429void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3430 LocationSummary* locations =
3431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3432 InvokeRuntimeCallingConvention calling_convention;
3433 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3434 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3435 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3436 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3437}
3438
3439void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3440 LocationSummary* locations = instruction->GetLocations();
3441 // Move an uint16_t value to a register.
3442 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003443 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3444 instruction,
3445 instruction->GetDexPc(),
3446 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003447 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3448}
3449
3450void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3451 LocationSummary* locations =
3452 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3453 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003454 if (instruction->IsStringAlloc()) {
3455 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3456 } else {
3457 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3458 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3459 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003460 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3461}
3462
3463void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003464 if (instruction->IsStringAlloc()) {
3465 // String is allocated through StringFactory. Call NewEmptyString entry point.
3466 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003467 MemberOffset code_offset =
3468 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003469 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3470 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3471 __ Jalr(T9);
3472 __ Nop();
3473 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3474 } else {
3475 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3476 instruction,
3477 instruction->GetDexPc(),
3478 nullptr);
3479 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3480 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003481}
3482
3483void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3484 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3485 locations->SetInAt(0, Location::RequiresRegister());
3486 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3487}
3488
3489void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3490 Primitive::Type type = instruction->GetType();
3491 LocationSummary* locations = instruction->GetLocations();
3492
3493 switch (type) {
3494 case Primitive::kPrimInt:
3495 case Primitive::kPrimLong: {
3496 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3497 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3498 __ Nor(dst, src, ZERO);
3499 break;
3500 }
3501
3502 default:
3503 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3504 }
3505}
3506
3507void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3509 locations->SetInAt(0, Location::RequiresRegister());
3510 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3511}
3512
3513void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3514 LocationSummary* locations = instruction->GetLocations();
3515 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3516 locations->InAt(0).AsRegister<GpuRegister>(),
3517 1);
3518}
3519
3520void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003521 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3522 ? LocationSummary::kCallOnSlowPath
3523 : LocationSummary::kNoCall;
3524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003525 locations->SetInAt(0, Location::RequiresRegister());
3526 if (instruction->HasUses()) {
3527 locations->SetOut(Location::SameAsFirstInput());
3528 }
3529}
3530
Calin Juravle2ae48182016-03-16 14:05:09 +00003531void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3532 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003533 return;
3534 }
3535 Location obj = instruction->GetLocations()->InAt(0);
3536
3537 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003538 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003539}
3540
Calin Juravle2ae48182016-03-16 14:05:09 +00003541void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003542 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003543 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003544
3545 Location obj = instruction->GetLocations()->InAt(0);
3546
3547 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3548}
3549
3550void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003551 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003552}
3553
3554void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3555 HandleBinaryOp(instruction);
3556}
3557
3558void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3559 HandleBinaryOp(instruction);
3560}
3561
3562void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3563 LOG(FATAL) << "Unreachable";
3564}
3565
3566void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3567 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3568}
3569
3570void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3572 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3573 if (location.IsStackSlot()) {
3574 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3575 } else if (location.IsDoubleStackSlot()) {
3576 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3577 }
3578 locations->SetOut(location);
3579}
3580
3581void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3582 ATTRIBUTE_UNUSED) {
3583 // Nothing to do, the parameter is already at its location.
3584}
3585
3586void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3587 LocationSummary* locations =
3588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3589 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3590}
3591
3592void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3593 ATTRIBUTE_UNUSED) {
3594 // Nothing to do, the method is already at its location.
3595}
3596
3597void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003599 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003600 locations->SetInAt(i, Location::Any());
3601 }
3602 locations->SetOut(Location::Any());
3603}
3604
3605void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3606 LOG(FATAL) << "Unreachable";
3607}
3608
3609void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3610 Primitive::Type type = rem->GetResultType();
3611 LocationSummary::CallKind call_kind =
3612 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3614
3615 switch (type) {
3616 case Primitive::kPrimInt:
3617 case Primitive::kPrimLong:
3618 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003619 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003620 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3621 break;
3622
3623 case Primitive::kPrimFloat:
3624 case Primitive::kPrimDouble: {
3625 InvokeRuntimeCallingConvention calling_convention;
3626 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3627 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3628 locations->SetOut(calling_convention.GetReturnLocation(type));
3629 break;
3630 }
3631
3632 default:
3633 LOG(FATAL) << "Unexpected rem type " << type;
3634 }
3635}
3636
3637void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3638 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003639
3640 switch (type) {
3641 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003642 case Primitive::kPrimLong:
3643 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003644 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003645
3646 case Primitive::kPrimFloat:
3647 case Primitive::kPrimDouble: {
3648 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3649 : QUICK_ENTRY_POINT(pFmod);
3650 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003651 if (type == Primitive::kPrimFloat) {
3652 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3653 } else {
3654 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3655 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003656 break;
3657 }
3658 default:
3659 LOG(FATAL) << "Unexpected rem type " << type;
3660 }
3661}
3662
3663void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3664 memory_barrier->SetLocations(nullptr);
3665}
3666
3667void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3668 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3669}
3670
3671void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3673 Primitive::Type return_type = ret->InputAt(0)->GetType();
3674 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3675}
3676
3677void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3678 codegen_->GenerateFrameExit();
3679}
3680
3681void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3682 ret->SetLocations(nullptr);
3683}
3684
3685void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3686 codegen_->GenerateFrameExit();
3687}
3688
Alexey Frunze92d90602015-12-18 18:16:36 -08003689void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3690 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003691}
3692
Alexey Frunze92d90602015-12-18 18:16:36 -08003693void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3694 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003695}
3696
Alexey Frunze4dda3372015-06-01 18:31:49 -07003697void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3698 HandleShift(shl);
3699}
3700
3701void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3702 HandleShift(shl);
3703}
3704
3705void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3706 HandleShift(shr);
3707}
3708
3709void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3710 HandleShift(shr);
3711}
3712
Alexey Frunze4dda3372015-06-01 18:31:49 -07003713void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3714 HandleBinaryOp(instruction);
3715}
3716
3717void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3718 HandleBinaryOp(instruction);
3719}
3720
3721void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3722 HandleFieldGet(instruction, instruction->GetFieldInfo());
3723}
3724
3725void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3726 HandleFieldGet(instruction, instruction->GetFieldInfo());
3727}
3728
3729void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3730 HandleFieldSet(instruction, instruction->GetFieldInfo());
3731}
3732
3733void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003734 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003735}
3736
Calin Juravlee460d1d2015-09-29 04:52:17 +01003737void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3738 HUnresolvedInstanceFieldGet* instruction) {
3739 FieldAccessCallingConventionMIPS64 calling_convention;
3740 codegen_->CreateUnresolvedFieldLocationSummary(
3741 instruction, instruction->GetFieldType(), calling_convention);
3742}
3743
3744void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3745 HUnresolvedInstanceFieldGet* instruction) {
3746 FieldAccessCallingConventionMIPS64 calling_convention;
3747 codegen_->GenerateUnresolvedFieldAccess(instruction,
3748 instruction->GetFieldType(),
3749 instruction->GetFieldIndex(),
3750 instruction->GetDexPc(),
3751 calling_convention);
3752}
3753
3754void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3755 HUnresolvedInstanceFieldSet* instruction) {
3756 FieldAccessCallingConventionMIPS64 calling_convention;
3757 codegen_->CreateUnresolvedFieldLocationSummary(
3758 instruction, instruction->GetFieldType(), calling_convention);
3759}
3760
3761void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3762 HUnresolvedInstanceFieldSet* instruction) {
3763 FieldAccessCallingConventionMIPS64 calling_convention;
3764 codegen_->GenerateUnresolvedFieldAccess(instruction,
3765 instruction->GetFieldType(),
3766 instruction->GetFieldIndex(),
3767 instruction->GetDexPc(),
3768 calling_convention);
3769}
3770
3771void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3772 HUnresolvedStaticFieldGet* instruction) {
3773 FieldAccessCallingConventionMIPS64 calling_convention;
3774 codegen_->CreateUnresolvedFieldLocationSummary(
3775 instruction, instruction->GetFieldType(), calling_convention);
3776}
3777
3778void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3779 HUnresolvedStaticFieldGet* instruction) {
3780 FieldAccessCallingConventionMIPS64 calling_convention;
3781 codegen_->GenerateUnresolvedFieldAccess(instruction,
3782 instruction->GetFieldType(),
3783 instruction->GetFieldIndex(),
3784 instruction->GetDexPc(),
3785 calling_convention);
3786}
3787
3788void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3789 HUnresolvedStaticFieldSet* instruction) {
3790 FieldAccessCallingConventionMIPS64 calling_convention;
3791 codegen_->CreateUnresolvedFieldLocationSummary(
3792 instruction, instruction->GetFieldType(), calling_convention);
3793}
3794
3795void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3796 HUnresolvedStaticFieldSet* instruction) {
3797 FieldAccessCallingConventionMIPS64 calling_convention;
3798 codegen_->GenerateUnresolvedFieldAccess(instruction,
3799 instruction->GetFieldType(),
3800 instruction->GetFieldIndex(),
3801 instruction->GetDexPc(),
3802 calling_convention);
3803}
3804
Alexey Frunze4dda3372015-06-01 18:31:49 -07003805void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3806 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3807}
3808
3809void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3810 HBasicBlock* block = instruction->GetBlock();
3811 if (block->GetLoopInformation() != nullptr) {
3812 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3813 // The back edge will generate the suspend check.
3814 return;
3815 }
3816 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3817 // The goto will generate the suspend check.
3818 return;
3819 }
3820 GenerateSuspendCheck(instruction, nullptr);
3821}
3822
Alexey Frunze4dda3372015-06-01 18:31:49 -07003823void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3824 LocationSummary* locations =
3825 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3826 InvokeRuntimeCallingConvention calling_convention;
3827 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3828}
3829
3830void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3831 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3832 instruction,
3833 instruction->GetDexPc(),
3834 nullptr);
3835 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3836}
3837
3838void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3839 Primitive::Type input_type = conversion->GetInputType();
3840 Primitive::Type result_type = conversion->GetResultType();
3841 DCHECK_NE(input_type, result_type);
3842
3843 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3844 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3845 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3846 }
3847
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3849
3850 if (Primitive::IsFloatingPointType(input_type)) {
3851 locations->SetInAt(0, Location::RequiresFpuRegister());
3852 } else {
3853 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003854 }
3855
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003856 if (Primitive::IsFloatingPointType(result_type)) {
3857 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003858 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003860 }
3861}
3862
3863void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3864 LocationSummary* locations = conversion->GetLocations();
3865 Primitive::Type result_type = conversion->GetResultType();
3866 Primitive::Type input_type = conversion->GetInputType();
3867
3868 DCHECK_NE(input_type, result_type);
3869
3870 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3871 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3872 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3873
3874 switch (result_type) {
3875 case Primitive::kPrimChar:
3876 __ Andi(dst, src, 0xFFFF);
3877 break;
3878 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003879 if (input_type == Primitive::kPrimLong) {
3880 // Type conversion from long to types narrower than int is a result of code
3881 // transformations. To avoid unpredictable results for SEB and SEH, we first
3882 // need to sign-extend the low 32-bit value into bits 32 through 63.
3883 __ Sll(dst, src, 0);
3884 __ Seb(dst, dst);
3885 } else {
3886 __ Seb(dst, src);
3887 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003888 break;
3889 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003890 if (input_type == Primitive::kPrimLong) {
3891 // Type conversion from long to types narrower than int is a result of code
3892 // transformations. To avoid unpredictable results for SEB and SEH, we first
3893 // need to sign-extend the low 32-bit value into bits 32 through 63.
3894 __ Sll(dst, src, 0);
3895 __ Seh(dst, dst);
3896 } else {
3897 __ Seh(dst, src);
3898 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003899 break;
3900 case Primitive::kPrimInt:
3901 case Primitive::kPrimLong:
3902 // Sign-extend 32-bit int into bits 32 through 63 for
3903 // int-to-long and long-to-int conversions
3904 __ Sll(dst, src, 0);
3905 break;
3906
3907 default:
3908 LOG(FATAL) << "Unexpected type conversion from " << input_type
3909 << " to " << result_type;
3910 }
3911 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003912 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3913 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3914 if (input_type == Primitive::kPrimLong) {
3915 __ Dmtc1(src, FTMP);
3916 if (result_type == Primitive::kPrimFloat) {
3917 __ Cvtsl(dst, FTMP);
3918 } else {
3919 __ Cvtdl(dst, FTMP);
3920 }
3921 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003922 __ Mtc1(src, FTMP);
3923 if (result_type == Primitive::kPrimFloat) {
3924 __ Cvtsw(dst, FTMP);
3925 } else {
3926 __ Cvtdw(dst, FTMP);
3927 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003928 }
3929 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3930 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003931 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3932 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3933 Mips64Label truncate;
3934 Mips64Label done;
3935
3936 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3937 // value when the input is either a NaN or is outside of the range of the output type
3938 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3939 // the same result.
3940 //
3941 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3942 // value of the output type if the input is outside of the range after the truncation or
3943 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3944 // results. This matches the desired float/double-to-int/long conversion exactly.
3945 //
3946 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3947 //
3948 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3949 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3950 // even though it must be NAN2008=1 on R6.
3951 //
3952 // The code takes care of the different behaviors by first comparing the input to the
3953 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3954 // If the input is greater than or equal to the minimum, it procedes to the truncate
3955 // instruction, which will handle such an input the same way irrespective of NAN2008.
3956 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3957 // in order to return either zero or the minimum value.
3958 //
3959 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3960 // truncate instruction for MIPS64R6.
3961 if (input_type == Primitive::kPrimFloat) {
3962 uint32_t min_val = (result_type == Primitive::kPrimLong)
3963 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3964 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3965 __ LoadConst32(TMP, min_val);
3966 __ Mtc1(TMP, FTMP);
3967 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003968 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003969 uint64_t min_val = (result_type == Primitive::kPrimLong)
3970 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3971 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3972 __ LoadConst64(TMP, min_val);
3973 __ Dmtc1(TMP, FTMP);
3974 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003975 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003976
3977 __ Bc1nez(FTMP, &truncate);
3978
3979 if (input_type == Primitive::kPrimFloat) {
3980 __ CmpEqS(FTMP, src, src);
3981 } else {
3982 __ CmpEqD(FTMP, src, src);
3983 }
3984 if (result_type == Primitive::kPrimLong) {
3985 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3986 } else {
3987 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3988 }
3989 __ Mfc1(TMP, FTMP);
3990 __ And(dst, dst, TMP);
3991
3992 __ Bc(&done);
3993
3994 __ Bind(&truncate);
3995
3996 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003997 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003998 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003999 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004000 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004001 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004002 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004003 } else {
4004 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004005 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004006 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004007 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004008 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004009 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004010 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004011
4012 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004013 } else if (Primitive::IsFloatingPointType(result_type) &&
4014 Primitive::IsFloatingPointType(input_type)) {
4015 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4016 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4017 if (result_type == Primitive::kPrimFloat) {
4018 __ Cvtsd(dst, src);
4019 } else {
4020 __ Cvtds(dst, src);
4021 }
4022 } else {
4023 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4024 << " to " << result_type;
4025 }
4026}
4027
4028void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4029 HandleShift(ushr);
4030}
4031
4032void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4033 HandleShift(ushr);
4034}
4035
4036void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4037 HandleBinaryOp(instruction);
4038}
4039
4040void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4041 HandleBinaryOp(instruction);
4042}
4043
4044void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4045 // Nothing to do, this should be removed during prepare for register allocator.
4046 LOG(FATAL) << "Unreachable";
4047}
4048
4049void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4050 // Nothing to do, this should be removed during prepare for register allocator.
4051 LOG(FATAL) << "Unreachable";
4052}
4053
4054void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004055 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004056}
4057
4058void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004059 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004060}
4061
4062void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004063 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004064}
4065
4066void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004067 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004068}
4069
4070void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004071 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004072}
4073
4074void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004075 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004076}
4077
4078void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004079 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004080}
4081
4082void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004083 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004084}
4085
4086void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004087 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004088}
4089
4090void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004091 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004092}
4093
4094void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004095 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004096}
4097
4098void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004099 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004100}
4101
Aart Bike9f37602015-10-09 11:15:55 -07004102void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004103 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004104}
4105
4106void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004107 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004108}
4109
4110void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004111 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004112}
4113
4114void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004115 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004116}
4117
4118void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004119 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004120}
4121
4122void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004123 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004124}
4125
4126void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004127 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004128}
4129
4130void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004131 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004132}
4133
Mark Mendellfe57faa2015-09-18 09:26:15 -04004134// Simple implementation of packed switch - generate cascaded compare/jumps.
4135void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4136 LocationSummary* locations =
4137 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4138 locations->SetInAt(0, Location::RequiresRegister());
4139}
4140
4141void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4142 int32_t lower_bound = switch_instr->GetStartValue();
4143 int32_t num_entries = switch_instr->GetNumEntries();
4144 LocationSummary* locations = switch_instr->GetLocations();
4145 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4146 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4147
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004148 // Create a set of compare/jumps.
4149 GpuRegister temp_reg = TMP;
4150 if (IsInt<16>(-lower_bound)) {
4151 __ Addiu(temp_reg, value_reg, -lower_bound);
4152 } else {
4153 __ LoadConst32(AT, -lower_bound);
4154 __ Addu(temp_reg, value_reg, AT);
4155 }
4156 // Jump to default if index is negative
4157 // Note: We don't check the case that index is positive while value < lower_bound, because in
4158 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4159 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4160
Mark Mendellfe57faa2015-09-18 09:26:15 -04004161 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004162 // Jump to successors[0] if value == lower_bound.
4163 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4164 int32_t last_index = 0;
4165 for (; num_entries - last_index > 2; last_index += 2) {
4166 __ Addiu(temp_reg, temp_reg, -2);
4167 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4168 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4169 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4170 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4171 }
4172 if (num_entries - last_index == 2) {
4173 // The last missing case_value.
4174 __ Addiu(temp_reg, temp_reg, -1);
4175 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004176 }
4177
4178 // And the default for any other value.
4179 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004180 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004181 }
4182}
4183
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004184void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4185 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4186}
4187
4188void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4189 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4190}
4191
Alexey Frunze4dda3372015-06-01 18:31:49 -07004192} // namespace mips64
4193} // namespace art