blob: 8530fe7a36e8d18ce451c658ab9800d2ddab3715 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070021#include "entrypoints/quick/quick_entrypoints.h"
22#include "entrypoints/quick/quick_entrypoints_enum.h"
23#include "gc/accounting/card_table.h"
24#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070025#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070026#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "offsets.h"
29#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070030#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "utils/stack_checks.h"
33
34namespace art {
35namespace mips64 {
36
37static constexpr int kCurrentMethodStackOffset = 0;
38static constexpr GpuRegister kMethodRegisterArgument = A0;
39
40// We need extra temporary/scratch registers (in addition to AT) in some cases.
Alexey Frunze4dda3372015-06-01 18:31:49 -070041static constexpr FpuRegister FTMP = F8;
42
Alexey Frunze4dda3372015-06-01 18:31:49 -070043Location Mips64ReturnLocation(Primitive::Type return_type) {
44 switch (return_type) {
45 case Primitive::kPrimBoolean:
46 case Primitive::kPrimByte:
47 case Primitive::kPrimChar:
48 case Primitive::kPrimShort:
49 case Primitive::kPrimInt:
50 case Primitive::kPrimNot:
51 case Primitive::kPrimLong:
52 return Location::RegisterLocation(V0);
53
54 case Primitive::kPrimFloat:
55 case Primitive::kPrimDouble:
56 return Location::FpuRegisterLocation(F0);
57
58 case Primitive::kPrimVoid:
59 return Location();
60 }
61 UNREACHABLE();
62}
63
64Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
65 return Mips64ReturnLocation(type);
66}
67
68Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
69 return Location::RegisterLocation(kMethodRegisterArgument);
70}
71
72Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
73 Location next_location;
74 if (type == Primitive::kPrimVoid) {
75 LOG(FATAL) << "Unexpected parameter type " << type;
76 }
77
78 if (Primitive::IsFloatingPointType(type) &&
79 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
80 next_location = Location::FpuRegisterLocation(
81 calling_convention.GetFpuRegisterAt(float_index_++));
82 gp_index_++;
83 } else if (!Primitive::IsFloatingPointType(type) &&
84 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
85 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
86 float_index_++;
87 } else {
88 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
89 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
90 : Location::StackSlot(stack_offset);
91 }
92
93 // Space on the stack is reserved for all arguments.
94 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
95
96 // TODO: review
97
98 // TODO: shouldn't we use a whole machine word per argument on the stack?
99 // Implicit 4-byte method pointer (and such) will cause misalignment.
100
101 return next_location;
102}
103
104Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
105 return Mips64ReturnLocation(type);
106}
107
108#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
110
111class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
112 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100113 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114
115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100116 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
118 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000119 if (instruction_->CanThrowIntoCatchBlock()) {
120 // Live registers will be restored in the catch block if caught.
121 SaveLiveRegisters(codegen, instruction_->GetLocations());
122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700123 // We're moving two locations to locations that could overlap, so we need a parallel
124 // move resolver.
125 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
128 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100129 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700130 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
131 Primitive::kPrimInt);
132 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
133 instruction_,
134 instruction_->GetDexPc(),
135 this);
136 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
137 }
138
Alexandre Rames8158f282015-08-07 10:26:17 +0100139 bool IsFatal() const OVERRIDE { return true; }
140
Roland Levillain46648892015-06-19 16:07:18 +0100141 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
142
Alexey Frunze4dda3372015-06-01 18:31:49 -0700143 private:
144 HBoundsCheck* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145
146 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
147};
148
149class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
150 public:
151 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : instruction_(instruction) {}
152
153 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
154 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
155 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000156 if (instruction_->CanThrowIntoCatchBlock()) {
157 // Live registers will be restored in the catch block if caught.
158 SaveLiveRegisters(codegen, instruction_->GetLocations());
159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
161 instruction_,
162 instruction_->GetDexPc(),
163 this);
164 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
165 }
166
Alexandre Rames8158f282015-08-07 10:26:17 +0100167 bool IsFatal() const OVERRIDE { return true; }
168
Roland Levillain46648892015-06-19 16:07:18 +0100169 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
170
Alexey Frunze4dda3372015-06-01 18:31:49 -0700171 private:
172 HDivZeroCheck* const instruction_;
173 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
174};
175
176class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
177 public:
178 LoadClassSlowPathMIPS64(HLoadClass* cls,
179 HInstruction* at,
180 uint32_t dex_pc,
181 bool do_clinit)
182 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
185
186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
187 LocationSummary* locations = at_->GetLocations();
188 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
189
190 __ Bind(GetEntryLabel());
191 SaveLiveRegisters(codegen, locations);
192
193 InvokeRuntimeCallingConvention calling_convention;
194 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
195 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
196 : QUICK_ENTRY_POINT(pInitializeType);
197 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
198 if (do_clinit_) {
199 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
200 } else {
201 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
202 }
203
204 // Move the class to the desired location.
205 Location out = locations->Out();
206 if (out.IsValid()) {
207 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
208 Primitive::Type type = at_->GetType();
209 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
210 }
211
212 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700213 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700214 }
215
Roland Levillain46648892015-06-19 16:07:18 +0100216 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
217
Alexey Frunze4dda3372015-06-01 18:31:49 -0700218 private:
219 // The class this slow path will load.
220 HLoadClass* const cls_;
221
222 // The instruction where this slow path is happening.
223 // (Might be the load class or an initialization check).
224 HInstruction* const at_;
225
226 // The dex PC of `at_`.
227 const uint32_t dex_pc_;
228
229 // Whether to initialize the class.
230 const bool do_clinit_;
231
232 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
233};
234
235class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
236 public:
237 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : instruction_(instruction) {}
238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
240 LocationSummary* locations = instruction_->GetLocations();
241 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
242 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
243
244 __ Bind(GetEntryLabel());
245 SaveLiveRegisters(codegen, locations);
246
247 InvokeRuntimeCallingConvention calling_convention;
248 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
249 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
250 instruction_,
251 instruction_->GetDexPc(),
252 this);
253 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
254 Primitive::Type type = instruction_->GetType();
255 mips64_codegen->MoveLocation(locations->Out(),
256 calling_convention.GetReturnLocation(type),
257 type);
258
259 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700260 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 }
262
Roland Levillain46648892015-06-19 16:07:18 +0100263 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
264
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 private:
266 HLoadString* const instruction_;
267
268 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
269};
270
271class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
272 public:
273 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : instruction_(instr) {}
274
275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
276 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
277 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000278 if (instruction_->CanThrowIntoCatchBlock()) {
279 // Live registers will be restored in the catch block if caught.
280 SaveLiveRegisters(codegen, instruction_->GetLocations());
281 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
283 instruction_,
284 instruction_->GetDexPc(),
285 this);
286 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
287 }
288
Alexandre Rames8158f282015-08-07 10:26:17 +0100289 bool IsFatal() const OVERRIDE { return true; }
290
Roland Levillain46648892015-06-19 16:07:18 +0100291 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
292
Alexey Frunze4dda3372015-06-01 18:31:49 -0700293 private:
294 HNullCheck* const instruction_;
295
296 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
297};
298
299class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
300 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100301 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700302 : instruction_(instruction), successor_(successor) {}
303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
307 SaveLiveRegisters(codegen, instruction_->GetLocations());
308 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
309 instruction_,
310 instruction_->GetDexPc(),
311 this);
312 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
313 RestoreLiveRegisters(codegen, instruction_->GetLocations());
314 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700317 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700318 }
319 }
320
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700321 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322 DCHECK(successor_ == nullptr);
323 return &return_label_;
324 }
325
Roland Levillain46648892015-06-19 16:07:18 +0100326 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
327
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328 private:
329 HSuspendCheck* const instruction_;
330 // If not null, the block to branch to after the suspend check.
331 HBasicBlock* const successor_;
332
333 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700334 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
337};
338
339class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
340 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700347 DCHECK(instruction_->IsCheckCast()
348 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
349 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
350
351 __ Bind(GetEntryLabel());
352 SaveLiveRegisters(codegen, locations);
353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100357 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
359 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100360 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
363
364 if (instruction_->IsInstanceOf()) {
365 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
366 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100367 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700368 this);
369 Primitive::Type ret_type = instruction_->GetType();
370 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
371 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
372 CheckEntrypointTypes<kQuickInstanceofNonTrivial,
373 uint32_t,
374 const mirror::Class*,
375 const mirror::Class*>();
376 } else {
377 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100378 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
380 }
381
382 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700383 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700384 }
385
Roland Levillain46648892015-06-19 16:07:18 +0100386 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
387
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 private:
389 HInstruction* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700390
391 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
392};
393
394class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
395 public:
396 explicit DeoptimizationSlowPathMIPS64(HInstruction* instruction)
397 : instruction_(instruction) {}
398
399 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
400 __ Bind(GetEntryLabel());
401 SaveLiveRegisters(codegen, instruction_->GetLocations());
402 DCHECK(instruction_->IsDeoptimize());
403 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
404 uint32_t dex_pc = deoptimize->GetDexPc();
405 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
406 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
407 }
408
Roland Levillain46648892015-06-19 16:07:18 +0100409 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
410
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 private:
412 HInstruction* const instruction_;
413 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
414};
415
416CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
417 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100418 const CompilerOptions& compiler_options,
419 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700420 : CodeGenerator(graph,
421 kNumberOfGpuRegisters,
422 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000423 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700424 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
425 arraysize(kCoreCalleeSaves)),
426 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
427 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100428 compiler_options,
429 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100430 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700431 location_builder_(graph, this),
432 instruction_visitor_(graph, this),
433 move_resolver_(graph->GetArena(), this),
434 isa_features_(isa_features) {
435 // Save RA (containing the return address) to mimic Quick.
436 AddAllocatedRegister(Location::RegisterLocation(RA));
437}
438
439#undef __
440#define __ down_cast<Mips64Assembler*>(GetAssembler())->
441#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
442
443void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700444 // Ensure that we fix up branches.
445 __ FinalizeCode();
446
447 // Adjust native pc offsets in stack maps.
448 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
449 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
450 uint32_t new_position = __ GetAdjustedPosition(old_position);
451 DCHECK_GE(new_position, old_position);
452 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
453 }
454
455 // Adjust pc offsets for the disassembly information.
456 if (disasm_info_ != nullptr) {
457 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
458 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
459 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
460 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
461 it.second.start = __ GetAdjustedPosition(it.second.start);
462 it.second.end = __ GetAdjustedPosition(it.second.end);
463 }
464 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
465 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
466 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
467 }
468 }
469
Alexey Frunze4dda3372015-06-01 18:31:49 -0700470 CodeGenerator::Finalize(allocator);
471}
472
473Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
474 return codegen_->GetAssembler();
475}
476
477void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100478 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700479 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
480}
481
482void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100483 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700484 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
485}
486
487void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
488 // Pop reg
489 __ Ld(GpuRegister(reg), SP, 0);
490 __ DecreaseFrameSize(kMips64WordSize);
491}
492
493void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
494 // Push reg
495 __ IncreaseFrameSize(kMips64WordSize);
496 __ Sd(GpuRegister(reg), SP, 0);
497}
498
499void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
500 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
501 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
502 // Allocate a scratch register other than TMP, if available.
503 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
504 // automatically unspilled when the scratch scope object is destroyed).
505 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
506 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
507 int stack_offset = ensure_scratch.IsSpilled() ? kMips64WordSize : 0;
508 __ LoadFromOffset(load_type,
509 GpuRegister(ensure_scratch.GetRegister()),
510 SP,
511 index1 + stack_offset);
512 __ LoadFromOffset(load_type,
513 TMP,
514 SP,
515 index2 + stack_offset);
516 __ StoreToOffset(store_type,
517 GpuRegister(ensure_scratch.GetRegister()),
518 SP,
519 index2 + stack_offset);
520 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
521}
522
523static dwarf::Reg DWARFReg(GpuRegister reg) {
524 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
525}
526
527// TODO: mapping of floating-point registers to DWARF
528
529void CodeGeneratorMIPS64::GenerateFrameEntry() {
530 __ Bind(&frame_entry_label_);
531
532 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
533
534 if (do_overflow_check) {
535 __ LoadFromOffset(kLoadWord,
536 ZERO,
537 SP,
538 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
539 RecordPcInfo(nullptr, 0);
540 }
541
542 // TODO: anything related to T9/GP/GOT/PIC/.so's?
543
544 if (HasEmptyFrame()) {
545 return;
546 }
547
548 // Make sure the frame size isn't unreasonably large. Per the various APIs
549 // it looks like it should always be less than 2GB in size, which allows
550 // us using 32-bit signed offsets from the stack pointer.
551 if (GetFrameSize() > 0x7FFFFFFF)
552 LOG(FATAL) << "Stack frame larger than 2GB";
553
554 // Spill callee-saved registers.
555 // Note that their cumulative size is small and they can be indexed using
556 // 16-bit offsets.
557
558 // TODO: increment/decrement SP in one step instead of two or remove this comment.
559
560 uint32_t ofs = FrameEntrySpillSize();
561 __ IncreaseFrameSize(ofs);
562
563 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
564 GpuRegister reg = kCoreCalleeSaves[i];
565 if (allocated_registers_.ContainsCoreRegister(reg)) {
566 ofs -= kMips64WordSize;
567 __ Sd(reg, SP, ofs);
568 __ cfi().RelOffset(DWARFReg(reg), ofs);
569 }
570 }
571
572 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
573 FpuRegister reg = kFpuCalleeSaves[i];
574 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
575 ofs -= kMips64WordSize;
576 __ Sdc1(reg, SP, ofs);
577 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
578 }
579 }
580
581 // Allocate the rest of the frame and store the current method pointer
582 // at its end.
583
584 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
585
586 static_assert(IsInt<16>(kCurrentMethodStackOffset),
587 "kCurrentMethodStackOffset must fit into int16_t");
588 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
589}
590
591void CodeGeneratorMIPS64::GenerateFrameExit() {
592 __ cfi().RememberState();
593
594 // TODO: anything related to T9/GP/GOT/PIC/.so's?
595
596 if (!HasEmptyFrame()) {
597 // Deallocate the rest of the frame.
598
599 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
600
601 // Restore callee-saved registers.
602 // Note that their cumulative size is small and they can be indexed using
603 // 16-bit offsets.
604
605 // TODO: increment/decrement SP in one step instead of two or remove this comment.
606
607 uint32_t ofs = 0;
608
609 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
610 FpuRegister reg = kFpuCalleeSaves[i];
611 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
612 __ Ldc1(reg, SP, ofs);
613 ofs += kMips64WordSize;
614 // TODO: __ cfi().Restore(DWARFReg(reg));
615 }
616 }
617
618 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
619 GpuRegister reg = kCoreCalleeSaves[i];
620 if (allocated_registers_.ContainsCoreRegister(reg)) {
621 __ Ld(reg, SP, ofs);
622 ofs += kMips64WordSize;
623 __ cfi().Restore(DWARFReg(reg));
624 }
625 }
626
627 DCHECK_EQ(ofs, FrameEntrySpillSize());
628 __ DecreaseFrameSize(ofs);
629 }
630
631 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700632 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700633
634 __ cfi().RestoreState();
635 __ cfi().DefCFAOffset(GetFrameSize());
636}
637
638void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
639 __ Bind(GetLabelOf(block));
640}
641
642void CodeGeneratorMIPS64::MoveLocation(Location destination,
643 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100644 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700645 if (source.Equals(destination)) {
646 return;
647 }
648
649 // A valid move can always be inferred from the destination and source
650 // locations. When moving from and to a register, the argument type can be
651 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100652 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700653 DCHECK_EQ(unspecified_type, false);
654
655 if (destination.IsRegister() || destination.IsFpuRegister()) {
656 if (unspecified_type) {
657 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
658 if (source.IsStackSlot() ||
659 (src_cst != nullptr && (src_cst->IsIntConstant()
660 || src_cst->IsFloatConstant()
661 || src_cst->IsNullConstant()))) {
662 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100663 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700664 } else {
665 // If the source is a double stack slot or a 64bit constant, a 64bit
666 // type is appropriate. Else the source is a register, and since the
667 // type has not been specified, we chose a 64bit type to force a 64bit
668 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100669 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700670 }
671 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100672 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
673 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700674 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
675 // Move to GPR/FPR from stack
676 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100677 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700678 __ LoadFpuFromOffset(load_type,
679 destination.AsFpuRegister<FpuRegister>(),
680 SP,
681 source.GetStackIndex());
682 } else {
683 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
684 __ LoadFromOffset(load_type,
685 destination.AsRegister<GpuRegister>(),
686 SP,
687 source.GetStackIndex());
688 }
689 } else if (source.IsConstant()) {
690 // Move to GPR/FPR from constant
691 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100692 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700693 gpr = destination.AsRegister<GpuRegister>();
694 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100695 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700696 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
697 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
698 gpr = ZERO;
699 } else {
700 __ LoadConst32(gpr, value);
701 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700702 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700703 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
704 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
705 gpr = ZERO;
706 } else {
707 __ LoadConst64(gpr, value);
708 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700709 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100710 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700711 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100712 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700713 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
714 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100715 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700716 if (destination.IsRegister()) {
717 // Move to GPR from GPR
718 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
719 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100720 DCHECK(destination.IsFpuRegister());
721 if (Primitive::Is64BitType(dst_type)) {
722 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
723 } else {
724 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
725 }
726 }
727 } else if (source.IsFpuRegister()) {
728 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700729 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100730 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700731 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
732 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100733 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700734 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
735 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100736 } else {
737 DCHECK(destination.IsRegister());
738 if (Primitive::Is64BitType(dst_type)) {
739 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
740 } else {
741 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
742 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700743 }
744 }
745 } else { // The destination is not a register. It must be a stack slot.
746 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
747 if (source.IsRegister() || source.IsFpuRegister()) {
748 if (unspecified_type) {
749 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100750 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700751 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100752 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700753 }
754 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100755 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
756 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700757 // Move to stack from GPR/FPR
758 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
759 if (source.IsRegister()) {
760 __ StoreToOffset(store_type,
761 source.AsRegister<GpuRegister>(),
762 SP,
763 destination.GetStackIndex());
764 } else {
765 __ StoreFpuToOffset(store_type,
766 source.AsFpuRegister<FpuRegister>(),
767 SP,
768 destination.GetStackIndex());
769 }
770 } else if (source.IsConstant()) {
771 // Move to stack from constant
772 HConstant* src_cst = source.GetConstant();
773 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700774 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700775 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700776 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
777 if (value != 0) {
778 gpr = TMP;
779 __ LoadConst32(gpr, value);
780 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700781 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700782 DCHECK(destination.IsDoubleStackSlot());
783 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
784 if (value != 0) {
785 gpr = TMP;
786 __ LoadConst64(gpr, value);
787 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700788 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700789 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700790 } else {
791 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
792 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
793 // Move to stack from stack
794 if (destination.IsStackSlot()) {
795 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
796 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
797 } else {
798 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
799 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
800 }
801 }
802 }
803}
804
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700805void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700806 DCHECK(!loc1.IsConstant());
807 DCHECK(!loc2.IsConstant());
808
809 if (loc1.Equals(loc2)) {
810 return;
811 }
812
813 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
814 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
815 bool is_fp_reg1 = loc1.IsFpuRegister();
816 bool is_fp_reg2 = loc2.IsFpuRegister();
817
818 if (loc2.IsRegister() && loc1.IsRegister()) {
819 // Swap 2 GPRs
820 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
821 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
822 __ Move(TMP, r2);
823 __ Move(r2, r1);
824 __ Move(r1, TMP);
825 } else if (is_fp_reg2 && is_fp_reg1) {
826 // Swap 2 FPRs
827 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
828 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700829 if (type == Primitive::kPrimFloat) {
830 __ MovS(FTMP, r1);
831 __ MovS(r1, r2);
832 __ MovS(r2, FTMP);
833 } else {
834 DCHECK_EQ(type, Primitive::kPrimDouble);
835 __ MovD(FTMP, r1);
836 __ MovD(r1, r2);
837 __ MovD(r2, FTMP);
838 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700839 } else if (is_slot1 != is_slot2) {
840 // Swap GPR/FPR and stack slot
841 Location reg_loc = is_slot1 ? loc2 : loc1;
842 Location mem_loc = is_slot1 ? loc1 : loc2;
843 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
844 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
845 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
846 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
847 if (reg_loc.IsFpuRegister()) {
848 __ StoreFpuToOffset(store_type,
849 reg_loc.AsFpuRegister<FpuRegister>(),
850 SP,
851 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700852 if (mem_loc.IsStackSlot()) {
853 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
854 } else {
855 DCHECK(mem_loc.IsDoubleStackSlot());
856 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
857 }
858 } else {
859 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
860 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
861 }
862 } else if (is_slot1 && is_slot2) {
863 move_resolver_.Exchange(loc1.GetStackIndex(),
864 loc2.GetStackIndex(),
865 loc1.IsDoubleStackSlot());
866 } else {
867 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
868 }
869}
870
871void CodeGeneratorMIPS64::Move(HInstruction* instruction,
872 Location location,
873 HInstruction* move_for) {
874 LocationSummary* locations = instruction->GetLocations();
875 Primitive::Type type = instruction->GetType();
876 DCHECK_NE(type, Primitive::kPrimVoid);
877
878 if (instruction->IsCurrentMethod()) {
879 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset), type);
880 } else if (locations != nullptr && locations->Out().Equals(location)) {
881 return;
882 } else if (instruction->IsIntConstant()
883 || instruction->IsLongConstant()
884 || instruction->IsNullConstant()) {
885 if (location.IsRegister()) {
886 // Move to GPR from constant
887 GpuRegister dst = location.AsRegister<GpuRegister>();
888 if (instruction->IsNullConstant() || instruction->IsIntConstant()) {
889 __ LoadConst32(dst, GetInt32ValueOf(instruction->AsConstant()));
890 } else {
891 __ LoadConst64(dst, instruction->AsLongConstant()->GetValue());
892 }
893 } else {
894 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
895 // Move to stack from constant
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700896 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700897 if (location.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700898 int32_t value = GetInt32ValueOf(instruction->AsConstant());
899 if (value != 0) {
900 gpr = TMP;
901 __ LoadConst32(gpr, value);
902 }
903 __ StoreToOffset(kStoreWord, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700904 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700905 DCHECK(location.IsDoubleStackSlot());
906 int64_t value = instruction->AsLongConstant()->GetValue();
907 if (value != 0) {
908 gpr = TMP;
909 __ LoadConst64(gpr, value);
910 }
911 __ StoreToOffset(kStoreDoubleword, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700912 }
913 }
914 } else if (instruction->IsTemporary()) {
915 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
916 MoveLocation(location, temp_location, type);
917 } else if (instruction->IsLoadLocal()) {
918 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
919 if (Primitive::Is64BitType(type)) {
920 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
921 } else {
922 MoveLocation(location, Location::StackSlot(stack_slot), type);
923 }
924 } else {
925 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
926 MoveLocation(location, locations->Out(), type);
927 }
928}
929
Calin Juravle175dc732015-08-25 15:42:32 +0100930void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
931 DCHECK(location.IsRegister());
932 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
933}
934
Calin Juravlee460d1d2015-09-29 04:52:17 +0100935void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
936 if (location.IsRegister()) {
937 locations->AddTemp(location);
938 } else {
939 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
940 }
941}
942
Alexey Frunze4dda3372015-06-01 18:31:49 -0700943Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
944 Primitive::Type type = load->GetType();
945
946 switch (type) {
947 case Primitive::kPrimNot:
948 case Primitive::kPrimInt:
949 case Primitive::kPrimFloat:
950 return Location::StackSlot(GetStackSlot(load->GetLocal()));
951
952 case Primitive::kPrimLong:
953 case Primitive::kPrimDouble:
954 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
955
956 case Primitive::kPrimBoolean:
957 case Primitive::kPrimByte:
958 case Primitive::kPrimChar:
959 case Primitive::kPrimShort:
960 case Primitive::kPrimVoid:
961 LOG(FATAL) << "Unexpected type " << type;
962 }
963
964 LOG(FATAL) << "Unreachable";
965 return Location::NoLocation();
966}
967
968void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object, GpuRegister value) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700969 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700970 GpuRegister card = AT;
971 GpuRegister temp = TMP;
972 __ Beqzc(value, &done);
973 __ LoadFromOffset(kLoadDoubleword,
974 card,
975 TR,
976 Thread::CardTableOffset<kMips64WordSize>().Int32Value());
977 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
978 __ Daddu(temp, card, temp);
979 __ Sb(card, temp, 0);
980 __ Bind(&done);
981}
982
983void CodeGeneratorMIPS64::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
984 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
985 blocked_core_registers_[ZERO] = true;
986 blocked_core_registers_[K0] = true;
987 blocked_core_registers_[K1] = true;
988 blocked_core_registers_[GP] = true;
989 blocked_core_registers_[SP] = true;
990 blocked_core_registers_[RA] = true;
991
992 // AT and TMP(T8) are used as temporary/scratch registers
993 // (similar to how AT is used by MIPS assemblers).
994 blocked_core_registers_[AT] = true;
995 blocked_core_registers_[TMP] = true;
996 blocked_fpu_registers_[FTMP] = true;
997
998 // Reserve suspend and thread registers.
999 blocked_core_registers_[S0] = true;
1000 blocked_core_registers_[TR] = true;
1001
1002 // Reserve T9 for function calls
1003 blocked_core_registers_[T9] = true;
1004
1005 // TODO: review; anything else?
1006
1007 // TODO: make these two for's conditional on is_baseline once
1008 // all the issues with register saving/restoring are sorted out.
1009 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1010 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1011 }
1012
1013 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1014 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1015 }
1016}
1017
1018Location CodeGeneratorMIPS64::AllocateFreeRegister(Primitive::Type type) const {
1019 if (type == Primitive::kPrimVoid) {
1020 LOG(FATAL) << "Unreachable type " << type;
1021 }
1022
1023 if (Primitive::IsFloatingPointType(type)) {
1024 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFpuRegisters);
1025 return Location::FpuRegisterLocation(reg);
1026 } else {
1027 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfGpuRegisters);
1028 return Location::RegisterLocation(reg);
1029 }
1030}
1031
1032size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1033 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
1034 return kMips64WordSize;
1035}
1036
1037size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1038 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
1039 return kMips64WordSize;
1040}
1041
1042size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1043 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
1044 return kMips64WordSize;
1045}
1046
1047size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1048 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
1049 return kMips64WordSize;
1050}
1051
1052void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001053 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001054}
1055
1056void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001057 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001058}
1059
Calin Juravle175dc732015-08-25 15:42:32 +01001060void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1061 HInstruction* instruction,
1062 uint32_t dex_pc,
1063 SlowPathCode* slow_path) {
1064 InvokeRuntime(GetThreadOffset<kMips64WordSize>(entrypoint).Int32Value(),
1065 instruction,
1066 dex_pc,
1067 slow_path);
1068}
1069
Alexey Frunze4dda3372015-06-01 18:31:49 -07001070void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
1071 HInstruction* instruction,
1072 uint32_t dex_pc,
1073 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001074 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001075 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1076 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1077 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001078 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001079 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001080}
1081
1082void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1083 GpuRegister class_reg) {
1084 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1085 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1086 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1087 // TODO: barrier needed?
1088 __ Bind(slow_path->GetExitLabel());
1089}
1090
1091void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1092 __ Sync(0); // only stype 0 is supported
1093}
1094
1095void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1096 HBasicBlock* successor) {
1097 SuspendCheckSlowPathMIPS64* slow_path =
1098 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1099 codegen_->AddSlowPath(slow_path);
1100
1101 __ LoadFromOffset(kLoadUnsignedHalfword,
1102 TMP,
1103 TR,
1104 Thread::ThreadFlagsOffset<kMips64WordSize>().Int32Value());
1105 if (successor == nullptr) {
1106 __ Bnezc(TMP, slow_path->GetEntryLabel());
1107 __ Bind(slow_path->GetReturnLabel());
1108 } else {
1109 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001110 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001111 // slow_path will return to GetLabelOf(successor).
1112 }
1113}
1114
1115InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1116 CodeGeneratorMIPS64* codegen)
1117 : HGraphVisitor(graph),
1118 assembler_(codegen->GetAssembler()),
1119 codegen_(codegen) {}
1120
1121void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1122 DCHECK_EQ(instruction->InputCount(), 2U);
1123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1124 Primitive::Type type = instruction->GetResultType();
1125 switch (type) {
1126 case Primitive::kPrimInt:
1127 case Primitive::kPrimLong: {
1128 locations->SetInAt(0, Location::RequiresRegister());
1129 HInstruction* right = instruction->InputAt(1);
1130 bool can_use_imm = false;
1131 if (right->IsConstant()) {
1132 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1133 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1134 can_use_imm = IsUint<16>(imm);
1135 } else if (instruction->IsAdd()) {
1136 can_use_imm = IsInt<16>(imm);
1137 } else {
1138 DCHECK(instruction->IsSub());
1139 can_use_imm = IsInt<16>(-imm);
1140 }
1141 }
1142 if (can_use_imm)
1143 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1144 else
1145 locations->SetInAt(1, Location::RequiresRegister());
1146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1147 }
1148 break;
1149
1150 case Primitive::kPrimFloat:
1151 case Primitive::kPrimDouble:
1152 locations->SetInAt(0, Location::RequiresFpuRegister());
1153 locations->SetInAt(1, Location::RequiresFpuRegister());
1154 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1155 break;
1156
1157 default:
1158 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1159 }
1160}
1161
1162void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1163 Primitive::Type type = instruction->GetType();
1164 LocationSummary* locations = instruction->GetLocations();
1165
1166 switch (type) {
1167 case Primitive::kPrimInt:
1168 case Primitive::kPrimLong: {
1169 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1170 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1171 Location rhs_location = locations->InAt(1);
1172
1173 GpuRegister rhs_reg = ZERO;
1174 int64_t rhs_imm = 0;
1175 bool use_imm = rhs_location.IsConstant();
1176 if (use_imm) {
1177 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1178 } else {
1179 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1180 }
1181
1182 if (instruction->IsAnd()) {
1183 if (use_imm)
1184 __ Andi(dst, lhs, rhs_imm);
1185 else
1186 __ And(dst, lhs, rhs_reg);
1187 } else if (instruction->IsOr()) {
1188 if (use_imm)
1189 __ Ori(dst, lhs, rhs_imm);
1190 else
1191 __ Or(dst, lhs, rhs_reg);
1192 } else if (instruction->IsXor()) {
1193 if (use_imm)
1194 __ Xori(dst, lhs, rhs_imm);
1195 else
1196 __ Xor(dst, lhs, rhs_reg);
1197 } else if (instruction->IsAdd()) {
1198 if (type == Primitive::kPrimInt) {
1199 if (use_imm)
1200 __ Addiu(dst, lhs, rhs_imm);
1201 else
1202 __ Addu(dst, lhs, rhs_reg);
1203 } else {
1204 if (use_imm)
1205 __ Daddiu(dst, lhs, rhs_imm);
1206 else
1207 __ Daddu(dst, lhs, rhs_reg);
1208 }
1209 } else {
1210 DCHECK(instruction->IsSub());
1211 if (type == Primitive::kPrimInt) {
1212 if (use_imm)
1213 __ Addiu(dst, lhs, -rhs_imm);
1214 else
1215 __ Subu(dst, lhs, rhs_reg);
1216 } else {
1217 if (use_imm)
1218 __ Daddiu(dst, lhs, -rhs_imm);
1219 else
1220 __ Dsubu(dst, lhs, rhs_reg);
1221 }
1222 }
1223 break;
1224 }
1225 case Primitive::kPrimFloat:
1226 case Primitive::kPrimDouble: {
1227 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1228 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1229 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1230 if (instruction->IsAdd()) {
1231 if (type == Primitive::kPrimFloat)
1232 __ AddS(dst, lhs, rhs);
1233 else
1234 __ AddD(dst, lhs, rhs);
1235 } else if (instruction->IsSub()) {
1236 if (type == Primitive::kPrimFloat)
1237 __ SubS(dst, lhs, rhs);
1238 else
1239 __ SubD(dst, lhs, rhs);
1240 } else {
1241 LOG(FATAL) << "Unexpected floating-point binary operation";
1242 }
1243 break;
1244 }
1245 default:
1246 LOG(FATAL) << "Unexpected binary operation type " << type;
1247 }
1248}
1249
1250void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
1251 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1252
1253 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1254 Primitive::Type type = instr->GetResultType();
1255 switch (type) {
1256 case Primitive::kPrimInt:
1257 case Primitive::kPrimLong: {
1258 locations->SetInAt(0, Location::RequiresRegister());
1259 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001261 break;
1262 }
1263 default:
1264 LOG(FATAL) << "Unexpected shift type " << type;
1265 }
1266}
1267
1268void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
1269 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1270 LocationSummary* locations = instr->GetLocations();
1271 Primitive::Type type = instr->GetType();
1272
1273 switch (type) {
1274 case Primitive::kPrimInt:
1275 case Primitive::kPrimLong: {
1276 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1277 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1278 Location rhs_location = locations->InAt(1);
1279
1280 GpuRegister rhs_reg = ZERO;
1281 int64_t rhs_imm = 0;
1282 bool use_imm = rhs_location.IsConstant();
1283 if (use_imm) {
1284 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1285 } else {
1286 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1287 }
1288
1289 if (use_imm) {
1290 uint32_t shift_value = (type == Primitive::kPrimInt)
1291 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1292 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1293
1294 if (type == Primitive::kPrimInt) {
1295 if (instr->IsShl()) {
1296 __ Sll(dst, lhs, shift_value);
1297 } else if (instr->IsShr()) {
1298 __ Sra(dst, lhs, shift_value);
1299 } else {
1300 __ Srl(dst, lhs, shift_value);
1301 }
1302 } else {
1303 if (shift_value < 32) {
1304 if (instr->IsShl()) {
1305 __ Dsll(dst, lhs, shift_value);
1306 } else if (instr->IsShr()) {
1307 __ Dsra(dst, lhs, shift_value);
1308 } else {
1309 __ Dsrl(dst, lhs, shift_value);
1310 }
1311 } else {
1312 shift_value -= 32;
1313 if (instr->IsShl()) {
1314 __ Dsll32(dst, lhs, shift_value);
1315 } else if (instr->IsShr()) {
1316 __ Dsra32(dst, lhs, shift_value);
1317 } else {
1318 __ Dsrl32(dst, lhs, shift_value);
1319 }
1320 }
1321 }
1322 } else {
1323 if (type == Primitive::kPrimInt) {
1324 if (instr->IsShl()) {
1325 __ Sllv(dst, lhs, rhs_reg);
1326 } else if (instr->IsShr()) {
1327 __ Srav(dst, lhs, rhs_reg);
1328 } else {
1329 __ Srlv(dst, lhs, rhs_reg);
1330 }
1331 } else {
1332 if (instr->IsShl()) {
1333 __ Dsllv(dst, lhs, rhs_reg);
1334 } else if (instr->IsShr()) {
1335 __ Dsrav(dst, lhs, rhs_reg);
1336 } else {
1337 __ Dsrlv(dst, lhs, rhs_reg);
1338 }
1339 }
1340 }
1341 break;
1342 }
1343 default:
1344 LOG(FATAL) << "Unexpected shift operation type " << type;
1345 }
1346}
1347
1348void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1349 HandleBinaryOp(instruction);
1350}
1351
1352void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1353 HandleBinaryOp(instruction);
1354}
1355
1356void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1357 HandleBinaryOp(instruction);
1358}
1359
1360void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1361 HandleBinaryOp(instruction);
1362}
1363
1364void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1365 LocationSummary* locations =
1366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1367 locations->SetInAt(0, Location::RequiresRegister());
1368 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1369 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1370 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1371 } else {
1372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1373 }
1374}
1375
1376void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1377 LocationSummary* locations = instruction->GetLocations();
1378 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1379 Location index = locations->InAt(1);
1380 Primitive::Type type = instruction->GetType();
1381
1382 switch (type) {
1383 case Primitive::kPrimBoolean: {
1384 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1385 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1386 if (index.IsConstant()) {
1387 size_t offset =
1388 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1389 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1390 } else {
1391 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1392 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1393 }
1394 break;
1395 }
1396
1397 case Primitive::kPrimByte: {
1398 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1399 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1400 if (index.IsConstant()) {
1401 size_t offset =
1402 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1403 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1404 } else {
1405 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1406 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1407 }
1408 break;
1409 }
1410
1411 case Primitive::kPrimShort: {
1412 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1413 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1414 if (index.IsConstant()) {
1415 size_t offset =
1416 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1417 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1418 } else {
1419 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1420 __ Daddu(TMP, obj, TMP);
1421 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1422 }
1423 break;
1424 }
1425
1426 case Primitive::kPrimChar: {
1427 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1428 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1429 if (index.IsConstant()) {
1430 size_t offset =
1431 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1432 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1433 } else {
1434 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1435 __ Daddu(TMP, obj, TMP);
1436 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1437 }
1438 break;
1439 }
1440
1441 case Primitive::kPrimInt:
1442 case Primitive::kPrimNot: {
1443 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1444 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1445 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1446 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1447 if (index.IsConstant()) {
1448 size_t offset =
1449 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1450 __ LoadFromOffset(load_type, out, obj, offset);
1451 } else {
1452 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1453 __ Daddu(TMP, obj, TMP);
1454 __ LoadFromOffset(load_type, out, TMP, data_offset);
1455 }
1456 break;
1457 }
1458
1459 case Primitive::kPrimLong: {
1460 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1461 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1462 if (index.IsConstant()) {
1463 size_t offset =
1464 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1465 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1466 } else {
1467 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1468 __ Daddu(TMP, obj, TMP);
1469 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1470 }
1471 break;
1472 }
1473
1474 case Primitive::kPrimFloat: {
1475 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1476 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1477 if (index.IsConstant()) {
1478 size_t offset =
1479 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1480 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1481 } else {
1482 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1483 __ Daddu(TMP, obj, TMP);
1484 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1485 }
1486 break;
1487 }
1488
1489 case Primitive::kPrimDouble: {
1490 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1491 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1492 if (index.IsConstant()) {
1493 size_t offset =
1494 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1495 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1496 } else {
1497 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1498 __ Daddu(TMP, obj, TMP);
1499 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1500 }
1501 break;
1502 }
1503
1504 case Primitive::kPrimVoid:
1505 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1506 UNREACHABLE();
1507 }
1508 codegen_->MaybeRecordImplicitNullCheck(instruction);
1509}
1510
1511void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1513 locations->SetInAt(0, Location::RequiresRegister());
1514 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1515}
1516
1517void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1518 LocationSummary* locations = instruction->GetLocations();
1519 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1520 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1521 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1522 __ LoadFromOffset(kLoadWord, out, obj, offset);
1523 codegen_->MaybeRecordImplicitNullCheck(instruction);
1524}
1525
1526void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001527 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1529 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001530 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1531 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001532 InvokeRuntimeCallingConvention calling_convention;
1533 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1534 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1535 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1536 } else {
1537 locations->SetInAt(0, Location::RequiresRegister());
1538 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1539 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1540 locations->SetInAt(2, Location::RequiresFpuRegister());
1541 } else {
1542 locations->SetInAt(2, Location::RequiresRegister());
1543 }
1544 }
1545}
1546
1547void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1548 LocationSummary* locations = instruction->GetLocations();
1549 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1550 Location index = locations->InAt(1);
1551 Primitive::Type value_type = instruction->GetComponentType();
1552 bool needs_runtime_call = locations->WillCall();
1553 bool needs_write_barrier =
1554 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1555
1556 switch (value_type) {
1557 case Primitive::kPrimBoolean:
1558 case Primitive::kPrimByte: {
1559 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1560 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1561 if (index.IsConstant()) {
1562 size_t offset =
1563 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1564 __ StoreToOffset(kStoreByte, value, obj, offset);
1565 } else {
1566 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1567 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1568 }
1569 break;
1570 }
1571
1572 case Primitive::kPrimShort:
1573 case Primitive::kPrimChar: {
1574 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1575 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1576 if (index.IsConstant()) {
1577 size_t offset =
1578 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1579 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1580 } else {
1581 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1582 __ Daddu(TMP, obj, TMP);
1583 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1584 }
1585 break;
1586 }
1587
1588 case Primitive::kPrimInt:
1589 case Primitive::kPrimNot: {
1590 if (!needs_runtime_call) {
1591 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1592 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1593 if (index.IsConstant()) {
1594 size_t offset =
1595 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1596 __ StoreToOffset(kStoreWord, value, obj, offset);
1597 } else {
1598 DCHECK(index.IsRegister()) << index;
1599 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1600 __ Daddu(TMP, obj, TMP);
1601 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1602 }
1603 codegen_->MaybeRecordImplicitNullCheck(instruction);
1604 if (needs_write_barrier) {
1605 DCHECK_EQ(value_type, Primitive::kPrimNot);
1606 codegen_->MarkGCCard(obj, value);
1607 }
1608 } else {
1609 DCHECK_EQ(value_type, Primitive::kPrimNot);
1610 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1611 instruction,
1612 instruction->GetDexPc(),
1613 nullptr);
1614 }
1615 break;
1616 }
1617
1618 case Primitive::kPrimLong: {
1619 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1620 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1621 if (index.IsConstant()) {
1622 size_t offset =
1623 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1624 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1625 } else {
1626 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1627 __ Daddu(TMP, obj, TMP);
1628 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1629 }
1630 break;
1631 }
1632
1633 case Primitive::kPrimFloat: {
1634 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1635 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1636 DCHECK(locations->InAt(2).IsFpuRegister());
1637 if (index.IsConstant()) {
1638 size_t offset =
1639 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1640 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1641 } else {
1642 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1643 __ Daddu(TMP, obj, TMP);
1644 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1645 }
1646 break;
1647 }
1648
1649 case Primitive::kPrimDouble: {
1650 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1651 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1652 DCHECK(locations->InAt(2).IsFpuRegister());
1653 if (index.IsConstant()) {
1654 size_t offset =
1655 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1656 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1657 } else {
1658 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1659 __ Daddu(TMP, obj, TMP);
1660 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1661 }
1662 break;
1663 }
1664
1665 case Primitive::kPrimVoid:
1666 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1667 UNREACHABLE();
1668 }
1669
1670 // Ints and objects are handled in the switch.
1671 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1672 codegen_->MaybeRecordImplicitNullCheck(instruction);
1673 }
1674}
1675
1676void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001677 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1678 ? LocationSummary::kCallOnSlowPath
1679 : LocationSummary::kNoCall;
1680 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001681 locations->SetInAt(0, Location::RequiresRegister());
1682 locations->SetInAt(1, Location::RequiresRegister());
1683 if (instruction->HasUses()) {
1684 locations->SetOut(Location::SameAsFirstInput());
1685 }
1686}
1687
1688void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1689 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001690 BoundsCheckSlowPathMIPS64* slow_path =
1691 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001692 codegen_->AddSlowPath(slow_path);
1693
1694 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1695 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1696
1697 // length is limited by the maximum positive signed 32-bit integer.
1698 // Unsigned comparison of length and index checks for index < 0
1699 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001700 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701}
1702
1703void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1705 instruction,
1706 LocationSummary::kCallOnSlowPath);
1707 locations->SetInAt(0, Location::RequiresRegister());
1708 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001709 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001710 locations->AddTemp(Location::RequiresRegister());
1711}
1712
1713void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1714 LocationSummary* locations = instruction->GetLocations();
1715 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1716 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1717 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1718
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001719 SlowPathCodeMIPS64* slow_path =
1720 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001721 codegen_->AddSlowPath(slow_path);
1722
1723 // TODO: avoid this check if we know obj is not null.
1724 __ Beqzc(obj, slow_path->GetExitLabel());
1725 // Compare the class of `obj` with `cls`.
1726 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1727 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1728 __ Bind(slow_path->GetExitLabel());
1729}
1730
1731void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1732 LocationSummary* locations =
1733 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1734 locations->SetInAt(0, Location::RequiresRegister());
1735 if (check->HasUses()) {
1736 locations->SetOut(Location::SameAsFirstInput());
1737 }
1738}
1739
1740void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1741 // We assume the class is not null.
1742 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1743 check->GetLoadClass(),
1744 check,
1745 check->GetDexPc(),
1746 true);
1747 codegen_->AddSlowPath(slow_path);
1748 GenerateClassInitializationCheck(slow_path,
1749 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1750}
1751
1752void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1753 Primitive::Type in_type = compare->InputAt(0)->GetType();
1754
1755 LocationSummary::CallKind call_kind = Primitive::IsFloatingPointType(in_type)
1756 ? LocationSummary::kCall
1757 : LocationSummary::kNoCall;
1758
1759 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare, call_kind);
1760
1761 switch (in_type) {
1762 case Primitive::kPrimLong:
1763 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001764 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001765 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1766 break;
1767
1768 case Primitive::kPrimFloat:
1769 case Primitive::kPrimDouble: {
1770 InvokeRuntimeCallingConvention calling_convention;
1771 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
1772 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
1773 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1774 break;
1775 }
1776
1777 default:
1778 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1779 }
1780}
1781
1782void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1783 LocationSummary* locations = instruction->GetLocations();
1784 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1785
1786 // 0 if: left == right
1787 // 1 if: left > right
1788 // -1 if: left < right
1789 switch (in_type) {
1790 case Primitive::kPrimLong: {
1791 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1792 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001793 Location rhs_location = locations->InAt(1);
1794 bool use_imm = rhs_location.IsConstant();
1795 GpuRegister rhs = ZERO;
1796 if (use_imm) {
1797 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1798 if (value != 0) {
1799 rhs = AT;
1800 __ LoadConst64(rhs, value);
1801 }
1802 } else {
1803 rhs = rhs_location.AsRegister<GpuRegister>();
1804 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001805 __ Slt(TMP, lhs, rhs);
1806 __ Slt(dst, rhs, lhs);
1807 __ Subu(dst, dst, TMP);
1808 break;
1809 }
1810
1811 case Primitive::kPrimFloat:
1812 case Primitive::kPrimDouble: {
1813 int32_t entry_point_offset;
1814 if (in_type == Primitive::kPrimFloat) {
1815 entry_point_offset = instruction->IsGtBias() ? QUICK_ENTRY_POINT(pCmpgFloat)
1816 : QUICK_ENTRY_POINT(pCmplFloat);
1817 } else {
1818 entry_point_offset = instruction->IsGtBias() ? QUICK_ENTRY_POINT(pCmpgDouble)
1819 : QUICK_ENTRY_POINT(pCmplDouble);
1820 }
1821 codegen_->InvokeRuntime(entry_point_offset, instruction, instruction->GetDexPc(), nullptr);
1822 break;
1823 }
1824
1825 default:
1826 LOG(FATAL) << "Unimplemented compare type " << in_type;
1827 }
1828}
1829
1830void LocationsBuilderMIPS64::VisitCondition(HCondition* instruction) {
1831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1834 if (instruction->NeedsMaterialization()) {
1835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1836 }
1837}
1838
1839void InstructionCodeGeneratorMIPS64::VisitCondition(HCondition* instruction) {
1840 if (!instruction->NeedsMaterialization()) {
1841 return;
1842 }
1843
Aart Bike9f37602015-10-09 11:15:55 -07001844 // TODO: generalize to long
1845 DCHECK_NE(instruction->InputAt(0)->GetType(), Primitive::kPrimLong);
1846
Alexey Frunze4dda3372015-06-01 18:31:49 -07001847 LocationSummary* locations = instruction->GetLocations();
1848
1849 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1850 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1851 Location rhs_location = locations->InAt(1);
1852
1853 GpuRegister rhs_reg = ZERO;
1854 int64_t rhs_imm = 0;
1855 bool use_imm = rhs_location.IsConstant();
1856 if (use_imm) {
1857 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1858 } else {
1859 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1860 }
1861
1862 IfCondition if_cond = instruction->GetCondition();
1863
1864 switch (if_cond) {
1865 case kCondEQ:
1866 case kCondNE:
1867 if (use_imm && IsUint<16>(rhs_imm)) {
1868 __ Xori(dst, lhs, rhs_imm);
1869 } else {
1870 if (use_imm) {
1871 rhs_reg = TMP;
1872 __ LoadConst32(rhs_reg, rhs_imm);
1873 }
1874 __ Xor(dst, lhs, rhs_reg);
1875 }
1876 if (if_cond == kCondEQ) {
1877 __ Sltiu(dst, dst, 1);
1878 } else {
1879 __ Sltu(dst, ZERO, dst);
1880 }
1881 break;
1882
1883 case kCondLT:
1884 case kCondGE:
1885 if (use_imm && IsInt<16>(rhs_imm)) {
1886 __ Slti(dst, lhs, rhs_imm);
1887 } else {
1888 if (use_imm) {
1889 rhs_reg = TMP;
1890 __ LoadConst32(rhs_reg, rhs_imm);
1891 }
1892 __ Slt(dst, lhs, rhs_reg);
1893 }
1894 if (if_cond == kCondGE) {
1895 // Simulate lhs >= rhs via !(lhs < rhs) since there's
1896 // only the slt instruction but no sge.
1897 __ Xori(dst, dst, 1);
1898 }
1899 break;
1900
1901 case kCondLE:
1902 case kCondGT:
1903 if (use_imm && IsInt<16>(rhs_imm + 1)) {
1904 // Simulate lhs <= rhs via lhs < rhs + 1.
1905 __ Slti(dst, lhs, rhs_imm + 1);
1906 if (if_cond == kCondGT) {
1907 // Simulate lhs > rhs via !(lhs <= rhs) since there's
1908 // only the slti instruction but no sgti.
1909 __ Xori(dst, dst, 1);
1910 }
1911 } else {
1912 if (use_imm) {
1913 rhs_reg = TMP;
1914 __ LoadConst32(rhs_reg, rhs_imm);
1915 }
1916 __ Slt(dst, rhs_reg, lhs);
1917 if (if_cond == kCondLE) {
1918 // Simulate lhs <= rhs via !(rhs < lhs) since there's
1919 // only the slt instruction but no sle.
1920 __ Xori(dst, dst, 1);
1921 }
1922 }
1923 break;
Aart Bike9f37602015-10-09 11:15:55 -07001924
1925 case kCondB:
1926 case kCondAE:
1927 if (use_imm && 0 <= rhs_imm && rhs_imm <= 0x7fff) {
1928 __ Sltiu(dst, lhs, rhs_imm);
1929 } else {
1930 if (use_imm) {
1931 rhs_reg = TMP;
1932 __ LoadConst32(rhs_reg, rhs_imm);
1933 }
1934 __ Sltu(dst, lhs, rhs_reg);
1935 }
1936 if (if_cond == kCondAE) {
1937 // Simulate lhs >= rhs via !(lhs < rhs) since there's
1938 // only the sltu instruction but no sgeu.
1939 __ Xori(dst, dst, 1);
1940 }
1941 break;
1942
1943 case kCondBE:
1944 case kCondA:
1945 if (use_imm && 0 <= rhs_imm && rhs_imm <= 0x7ffe) {
1946 // Simulate lhs <= rhs via lhs < rhs + 1.
1947 __ Sltiu(dst, lhs, rhs_imm + 1);
1948 if (if_cond == kCondA) {
1949 // Simulate lhs > rhs via !(lhs <= rhs) since there's
1950 // only the sltiu instruction but no sgtiu.
1951 __ Xori(dst, dst, 1);
1952 }
1953 } else {
1954 if (use_imm) {
1955 rhs_reg = TMP;
1956 __ LoadConst32(rhs_reg, rhs_imm);
1957 }
1958 __ Sltu(dst, rhs_reg, lhs);
1959 if (if_cond == kCondBE) {
1960 // Simulate lhs <= rhs via !(rhs < lhs) since there's
1961 // only the sltu instruction but no sleu.
1962 __ Xori(dst, dst, 1);
1963 }
1964 }
1965 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001966 }
1967}
1968
Alexey Frunzec857c742015-09-23 15:12:39 -07001969void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1970 DCHECK(instruction->IsDiv() || instruction->IsRem());
1971 Primitive::Type type = instruction->GetResultType();
1972
1973 LocationSummary* locations = instruction->GetLocations();
1974 Location second = locations->InAt(1);
1975 DCHECK(second.IsConstant());
1976
1977 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1978 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1979 int64_t imm = Int64FromConstant(second.GetConstant());
1980 DCHECK(imm == 1 || imm == -1);
1981
1982 if (instruction->IsRem()) {
1983 __ Move(out, ZERO);
1984 } else {
1985 if (imm == -1) {
1986 if (type == Primitive::kPrimInt) {
1987 __ Subu(out, ZERO, dividend);
1988 } else {
1989 DCHECK_EQ(type, Primitive::kPrimLong);
1990 __ Dsubu(out, ZERO, dividend);
1991 }
1992 } else if (out != dividend) {
1993 __ Move(out, dividend);
1994 }
1995 }
1996}
1997
1998void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1999 DCHECK(instruction->IsDiv() || instruction->IsRem());
2000 Primitive::Type type = instruction->GetResultType();
2001
2002 LocationSummary* locations = instruction->GetLocations();
2003 Location second = locations->InAt(1);
2004 DCHECK(second.IsConstant());
2005
2006 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2007 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2008 int64_t imm = Int64FromConstant(second.GetConstant());
2009 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
2010 DCHECK(IsPowerOfTwo(abs_imm));
2011 int ctz_imm = CTZ(abs_imm);
2012
2013 if (instruction->IsDiv()) {
2014 if (type == Primitive::kPrimInt) {
2015 if (ctz_imm == 1) {
2016 // Fast path for division by +/-2, which is very common.
2017 __ Srl(TMP, dividend, 31);
2018 } else {
2019 __ Sra(TMP, dividend, 31);
2020 __ Srl(TMP, TMP, 32 - ctz_imm);
2021 }
2022 __ Addu(out, dividend, TMP);
2023 __ Sra(out, out, ctz_imm);
2024 if (imm < 0) {
2025 __ Subu(out, ZERO, out);
2026 }
2027 } else {
2028 DCHECK_EQ(type, Primitive::kPrimLong);
2029 if (ctz_imm == 1) {
2030 // Fast path for division by +/-2, which is very common.
2031 __ Dsrl32(TMP, dividend, 31);
2032 } else {
2033 __ Dsra32(TMP, dividend, 31);
2034 if (ctz_imm > 32) {
2035 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2036 } else {
2037 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2038 }
2039 }
2040 __ Daddu(out, dividend, TMP);
2041 if (ctz_imm < 32) {
2042 __ Dsra(out, out, ctz_imm);
2043 } else {
2044 __ Dsra32(out, out, ctz_imm - 32);
2045 }
2046 if (imm < 0) {
2047 __ Dsubu(out, ZERO, out);
2048 }
2049 }
2050 } else {
2051 if (type == Primitive::kPrimInt) {
2052 if (ctz_imm == 1) {
2053 // Fast path for modulo +/-2, which is very common.
2054 __ Sra(TMP, dividend, 31);
2055 __ Subu(out, dividend, TMP);
2056 __ Andi(out, out, 1);
2057 __ Addu(out, out, TMP);
2058 } else {
2059 __ Sra(TMP, dividend, 31);
2060 __ Srl(TMP, TMP, 32 - ctz_imm);
2061 __ Addu(out, dividend, TMP);
2062 if (IsUint<16>(abs_imm - 1)) {
2063 __ Andi(out, out, abs_imm - 1);
2064 } else {
2065 __ Sll(out, out, 32 - ctz_imm);
2066 __ Srl(out, out, 32 - ctz_imm);
2067 }
2068 __ Subu(out, out, TMP);
2069 }
2070 } else {
2071 DCHECK_EQ(type, Primitive::kPrimLong);
2072 if (ctz_imm == 1) {
2073 // Fast path for modulo +/-2, which is very common.
2074 __ Dsra32(TMP, dividend, 31);
2075 __ Dsubu(out, dividend, TMP);
2076 __ Andi(out, out, 1);
2077 __ Daddu(out, out, TMP);
2078 } else {
2079 __ Dsra32(TMP, dividend, 31);
2080 if (ctz_imm > 32) {
2081 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2082 } else {
2083 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2084 }
2085 __ Daddu(out, dividend, TMP);
2086 if (IsUint<16>(abs_imm - 1)) {
2087 __ Andi(out, out, abs_imm - 1);
2088 } else {
2089 if (ctz_imm > 32) {
2090 __ Dsll(out, out, 64 - ctz_imm);
2091 __ Dsrl(out, out, 64 - ctz_imm);
2092 } else {
2093 __ Dsll32(out, out, 32 - ctz_imm);
2094 __ Dsrl32(out, out, 32 - ctz_imm);
2095 }
2096 }
2097 __ Dsubu(out, out, TMP);
2098 }
2099 }
2100 }
2101}
2102
2103void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2104 DCHECK(instruction->IsDiv() || instruction->IsRem());
2105
2106 LocationSummary* locations = instruction->GetLocations();
2107 Location second = locations->InAt(1);
2108 DCHECK(second.IsConstant());
2109
2110 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2111 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2112 int64_t imm = Int64FromConstant(second.GetConstant());
2113
2114 Primitive::Type type = instruction->GetResultType();
2115 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2116
2117 int64_t magic;
2118 int shift;
2119 CalculateMagicAndShiftForDivRem(imm,
2120 (type == Primitive::kPrimLong),
2121 &magic,
2122 &shift);
2123
2124 if (type == Primitive::kPrimInt) {
2125 __ LoadConst32(TMP, magic);
2126 __ MuhR6(TMP, dividend, TMP);
2127
2128 if (imm > 0 && magic < 0) {
2129 __ Addu(TMP, TMP, dividend);
2130 } else if (imm < 0 && magic > 0) {
2131 __ Subu(TMP, TMP, dividend);
2132 }
2133
2134 if (shift != 0) {
2135 __ Sra(TMP, TMP, shift);
2136 }
2137
2138 if (instruction->IsDiv()) {
2139 __ Sra(out, TMP, 31);
2140 __ Subu(out, TMP, out);
2141 } else {
2142 __ Sra(AT, TMP, 31);
2143 __ Subu(AT, TMP, AT);
2144 __ LoadConst32(TMP, imm);
2145 __ MulR6(TMP, AT, TMP);
2146 __ Subu(out, dividend, TMP);
2147 }
2148 } else {
2149 __ LoadConst64(TMP, magic);
2150 __ Dmuh(TMP, dividend, TMP);
2151
2152 if (imm > 0 && magic < 0) {
2153 __ Daddu(TMP, TMP, dividend);
2154 } else if (imm < 0 && magic > 0) {
2155 __ Dsubu(TMP, TMP, dividend);
2156 }
2157
2158 if (shift >= 32) {
2159 __ Dsra32(TMP, TMP, shift - 32);
2160 } else if (shift > 0) {
2161 __ Dsra(TMP, TMP, shift);
2162 }
2163
2164 if (instruction->IsDiv()) {
2165 __ Dsra32(out, TMP, 31);
2166 __ Dsubu(out, TMP, out);
2167 } else {
2168 __ Dsra32(AT, TMP, 31);
2169 __ Dsubu(AT, TMP, AT);
2170 __ LoadConst64(TMP, imm);
2171 __ Dmul(TMP, AT, TMP);
2172 __ Dsubu(out, dividend, TMP);
2173 }
2174 }
2175}
2176
2177void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2178 DCHECK(instruction->IsDiv() || instruction->IsRem());
2179 Primitive::Type type = instruction->GetResultType();
2180 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2181
2182 LocationSummary* locations = instruction->GetLocations();
2183 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2184 Location second = locations->InAt(1);
2185
2186 if (second.IsConstant()) {
2187 int64_t imm = Int64FromConstant(second.GetConstant());
2188 if (imm == 0) {
2189 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2190 } else if (imm == 1 || imm == -1) {
2191 DivRemOneOrMinusOne(instruction);
2192 } else if (IsPowerOfTwo(std::abs(imm))) {
2193 DivRemByPowerOfTwo(instruction);
2194 } else {
2195 DCHECK(imm <= -2 || imm >= 2);
2196 GenerateDivRemWithAnyConstant(instruction);
2197 }
2198 } else {
2199 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2200 GpuRegister divisor = second.AsRegister<GpuRegister>();
2201 if (instruction->IsDiv()) {
2202 if (type == Primitive::kPrimInt)
2203 __ DivR6(out, dividend, divisor);
2204 else
2205 __ Ddiv(out, dividend, divisor);
2206 } else {
2207 if (type == Primitive::kPrimInt)
2208 __ ModR6(out, dividend, divisor);
2209 else
2210 __ Dmod(out, dividend, divisor);
2211 }
2212 }
2213}
2214
Alexey Frunze4dda3372015-06-01 18:31:49 -07002215void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2216 LocationSummary* locations =
2217 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2218 switch (div->GetResultType()) {
2219 case Primitive::kPrimInt:
2220 case Primitive::kPrimLong:
2221 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002222 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002223 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2224 break;
2225
2226 case Primitive::kPrimFloat:
2227 case Primitive::kPrimDouble:
2228 locations->SetInAt(0, Location::RequiresFpuRegister());
2229 locations->SetInAt(1, Location::RequiresFpuRegister());
2230 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2231 break;
2232
2233 default:
2234 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2235 }
2236}
2237
2238void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2239 Primitive::Type type = instruction->GetType();
2240 LocationSummary* locations = instruction->GetLocations();
2241
2242 switch (type) {
2243 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002244 case Primitive::kPrimLong:
2245 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002246 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002247 case Primitive::kPrimFloat:
2248 case Primitive::kPrimDouble: {
2249 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2250 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2251 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2252 if (type == Primitive::kPrimFloat)
2253 __ DivS(dst, lhs, rhs);
2254 else
2255 __ DivD(dst, lhs, rhs);
2256 break;
2257 }
2258 default:
2259 LOG(FATAL) << "Unexpected div type " << type;
2260 }
2261}
2262
2263void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002264 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2265 ? LocationSummary::kCallOnSlowPath
2266 : LocationSummary::kNoCall;
2267 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002268 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2269 if (instruction->HasUses()) {
2270 locations->SetOut(Location::SameAsFirstInput());
2271 }
2272}
2273
2274void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2275 SlowPathCodeMIPS64* slow_path =
2276 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2277 codegen_->AddSlowPath(slow_path);
2278 Location value = instruction->GetLocations()->InAt(0);
2279
2280 Primitive::Type type = instruction->GetType();
2281
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002282 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002283 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002284 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002285 }
2286
2287 if (value.IsConstant()) {
2288 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2289 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002290 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002291 } else {
2292 // A division by a non-null constant is valid. We don't need to perform
2293 // any check, so simply fall through.
2294 }
2295 } else {
2296 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2297 }
2298}
2299
2300void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2301 LocationSummary* locations =
2302 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2303 locations->SetOut(Location::ConstantLocation(constant));
2304}
2305
2306void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2307 // Will be generated at use site.
2308}
2309
2310void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2311 exit->SetLocations(nullptr);
2312}
2313
2314void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2315}
2316
2317void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2318 LocationSummary* locations =
2319 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2320 locations->SetOut(Location::ConstantLocation(constant));
2321}
2322
2323void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2324 // Will be generated at use site.
2325}
2326
David Brazdilfc6a86a2015-06-26 10:33:45 +00002327void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002328 DCHECK(!successor->IsExitBlock());
2329 HBasicBlock* block = got->GetBlock();
2330 HInstruction* previous = got->GetPrevious();
2331 HLoopInformation* info = block->GetLoopInformation();
2332
2333 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2334 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2335 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2336 return;
2337 }
2338 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2339 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2340 }
2341 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002342 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 }
2344}
2345
David Brazdilfc6a86a2015-06-26 10:33:45 +00002346void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2347 got->SetLocations(nullptr);
2348}
2349
2350void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2351 HandleGoto(got, got->GetSuccessor());
2352}
2353
2354void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2355 try_boundary->SetLocations(nullptr);
2356}
2357
2358void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2359 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2360 if (!successor->IsExitBlock()) {
2361 HandleGoto(try_boundary, successor);
2362 }
2363}
2364
Alexey Frunze4dda3372015-06-01 18:31:49 -07002365void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002366 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002367 Mips64Label* true_target,
2368 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002369 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002370
David Brazdil0debae72015-11-12 18:37:00 +00002371 if (true_target == nullptr && false_target == nullptr) {
2372 // Nothing to do. The code always falls through.
2373 return;
2374 } else if (cond->IsIntConstant()) {
2375 // Constant condition, statically compared against 1.
2376 if (cond->AsIntConstant()->IsOne()) {
2377 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002378 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002379 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002380 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002381 DCHECK(cond->AsIntConstant()->IsZero());
2382 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002383 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002384 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002385 }
David Brazdil0debae72015-11-12 18:37:00 +00002386 return;
2387 }
2388
2389 // The following code generates these patterns:
2390 // (1) true_target == nullptr && false_target != nullptr
2391 // - opposite condition true => branch to false_target
2392 // (2) true_target != nullptr && false_target == nullptr
2393 // - condition true => branch to true_target
2394 // (3) true_target != nullptr && false_target != nullptr
2395 // - condition true => branch to true_target
2396 // - branch to false_target
2397 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002398 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002399 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002400 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002401 if (true_target == nullptr) {
2402 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2403 } else {
2404 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2405 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002406 } else {
2407 // The condition instruction has not been materialized, use its inputs as
2408 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002409 HCondition* condition = cond->AsCondition();
2410
Alexey Frunze4dda3372015-06-01 18:31:49 -07002411 GpuRegister lhs = condition->GetLocations()->InAt(0).AsRegister<GpuRegister>();
2412 Location rhs_location = condition->GetLocations()->InAt(1);
2413 GpuRegister rhs_reg = ZERO;
2414 int32_t rhs_imm = 0;
2415 bool use_imm = rhs_location.IsConstant();
2416 if (use_imm) {
2417 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2418 } else {
2419 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2420 }
2421
David Brazdil0debae72015-11-12 18:37:00 +00002422 IfCondition if_cond;
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002423 Mips64Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002424 if (true_target == nullptr) {
2425 if_cond = condition->GetOppositeCondition();
2426 non_fallthrough_target = false_target;
2427 } else {
2428 if_cond = condition->GetCondition();
2429 non_fallthrough_target = true_target;
2430 }
2431
Alexey Frunze4dda3372015-06-01 18:31:49 -07002432 if (use_imm && rhs_imm == 0) {
2433 switch (if_cond) {
2434 case kCondEQ:
David Brazdil0debae72015-11-12 18:37:00 +00002435 __ Beqzc(lhs, non_fallthrough_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002436 break;
2437 case kCondNE:
David Brazdil0debae72015-11-12 18:37:00 +00002438 __ Bnezc(lhs, non_fallthrough_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002439 break;
2440 case kCondLT:
David Brazdil0debae72015-11-12 18:37:00 +00002441 __ Bltzc(lhs, non_fallthrough_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002442 break;
2443 case kCondGE:
David Brazdil0debae72015-11-12 18:37:00 +00002444 __ Bgezc(lhs, non_fallthrough_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002445 break;
2446 case kCondLE:
David Brazdil0debae72015-11-12 18:37:00 +00002447 __ Blezc(lhs, non_fallthrough_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002448 break;
2449 case kCondGT:
David Brazdil0debae72015-11-12 18:37:00 +00002450 __ Bgtzc(lhs, non_fallthrough_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002451 break;
Aart Bike9f37602015-10-09 11:15:55 -07002452 case kCondB:
2453 break; // always false
2454 case kCondBE:
David Brazdil0debae72015-11-12 18:37:00 +00002455 __ Beqzc(lhs, non_fallthrough_target); // <= 0 if zero
Aart Bike9f37602015-10-09 11:15:55 -07002456 break;
2457 case kCondA:
David Brazdil0debae72015-11-12 18:37:00 +00002458 __ Bnezc(lhs, non_fallthrough_target); // > 0 if non-zero
Aart Bike9f37602015-10-09 11:15:55 -07002459 break;
2460 case kCondAE:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002461 __ Bc(non_fallthrough_target); // always true
Aart Bike9f37602015-10-09 11:15:55 -07002462 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002463 }
2464 } else {
2465 if (use_imm) {
2466 rhs_reg = TMP;
2467 __ LoadConst32(rhs_reg, rhs_imm);
2468 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002469 switch (if_cond) {
2470 case kCondEQ:
2471 __ Beqc(lhs, rhs_reg, non_fallthrough_target);
2472 break;
2473 case kCondNE:
2474 __ Bnec(lhs, rhs_reg, non_fallthrough_target);
2475 break;
2476 case kCondLT:
2477 __ Bltc(lhs, rhs_reg, non_fallthrough_target);
2478 break;
2479 case kCondGE:
2480 __ Bgec(lhs, rhs_reg, non_fallthrough_target);
2481 break;
2482 case kCondLE:
2483 __ Bgec(rhs_reg, lhs, non_fallthrough_target);
2484 break;
2485 case kCondGT:
2486 __ Bltc(rhs_reg, lhs, non_fallthrough_target);
2487 break;
2488 case kCondB:
2489 __ Bltuc(lhs, rhs_reg, non_fallthrough_target);
2490 break;
2491 case kCondAE:
2492 __ Bgeuc(lhs, rhs_reg, non_fallthrough_target);
2493 break;
2494 case kCondBE:
2495 __ Bgeuc(rhs_reg, lhs, non_fallthrough_target);
2496 break;
2497 case kCondA:
2498 __ Bltuc(rhs_reg, lhs, non_fallthrough_target);
2499 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002500 }
2501 }
2502 }
David Brazdil0debae72015-11-12 18:37:00 +00002503
2504 // If neither branch falls through (case 3), the conditional branch to `true_target`
2505 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2506 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002507 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002508 }
2509}
2510
2511void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002513 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002514 locations->SetInAt(0, Location::RequiresRegister());
2515 }
2516}
2517
2518void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002519 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2520 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002521 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002522 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002523 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002524 nullptr : codegen_->GetLabelOf(false_successor);
2525 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002526}
2527
2528void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2529 LocationSummary* locations = new (GetGraph()->GetArena())
2530 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002531 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002532 locations->SetInAt(0, Location::RequiresRegister());
2533 }
2534}
2535
2536void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2537 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
2538 DeoptimizationSlowPathMIPS64(deoptimize);
2539 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00002540 GenerateTestAndBranch(deoptimize,
2541 /* condition_input_index */ 0,
2542 slow_path->GetEntryLabel(),
2543 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002544}
2545
2546void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2547 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2548 LocationSummary* locations =
2549 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2550 locations->SetInAt(0, Location::RequiresRegister());
2551 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2552 locations->SetOut(Location::RequiresFpuRegister());
2553 } else {
2554 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2555 }
2556}
2557
2558void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2559 const FieldInfo& field_info) {
2560 Primitive::Type type = field_info.GetFieldType();
2561 LocationSummary* locations = instruction->GetLocations();
2562 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2563 LoadOperandType load_type = kLoadUnsignedByte;
2564 switch (type) {
2565 case Primitive::kPrimBoolean:
2566 load_type = kLoadUnsignedByte;
2567 break;
2568 case Primitive::kPrimByte:
2569 load_type = kLoadSignedByte;
2570 break;
2571 case Primitive::kPrimShort:
2572 load_type = kLoadSignedHalfword;
2573 break;
2574 case Primitive::kPrimChar:
2575 load_type = kLoadUnsignedHalfword;
2576 break;
2577 case Primitive::kPrimInt:
2578 case Primitive::kPrimFloat:
2579 load_type = kLoadWord;
2580 break;
2581 case Primitive::kPrimLong:
2582 case Primitive::kPrimDouble:
2583 load_type = kLoadDoubleword;
2584 break;
2585 case Primitive::kPrimNot:
2586 load_type = kLoadUnsignedWord;
2587 break;
2588 case Primitive::kPrimVoid:
2589 LOG(FATAL) << "Unreachable type " << type;
2590 UNREACHABLE();
2591 }
2592 if (!Primitive::IsFloatingPointType(type)) {
2593 DCHECK(locations->Out().IsRegister());
2594 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2595 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2596 } else {
2597 DCHECK(locations->Out().IsFpuRegister());
2598 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2599 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2600 }
2601
2602 codegen_->MaybeRecordImplicitNullCheck(instruction);
2603 // TODO: memory barrier?
2604}
2605
2606void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2607 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2608 LocationSummary* locations =
2609 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2610 locations->SetInAt(0, Location::RequiresRegister());
2611 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2612 locations->SetInAt(1, Location::RequiresFpuRegister());
2613 } else {
2614 locations->SetInAt(1, Location::RequiresRegister());
2615 }
2616}
2617
2618void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
2619 const FieldInfo& field_info) {
2620 Primitive::Type type = field_info.GetFieldType();
2621 LocationSummary* locations = instruction->GetLocations();
2622 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2623 StoreOperandType store_type = kStoreByte;
2624 switch (type) {
2625 case Primitive::kPrimBoolean:
2626 case Primitive::kPrimByte:
2627 store_type = kStoreByte;
2628 break;
2629 case Primitive::kPrimShort:
2630 case Primitive::kPrimChar:
2631 store_type = kStoreHalfword;
2632 break;
2633 case Primitive::kPrimInt:
2634 case Primitive::kPrimFloat:
2635 case Primitive::kPrimNot:
2636 store_type = kStoreWord;
2637 break;
2638 case Primitive::kPrimLong:
2639 case Primitive::kPrimDouble:
2640 store_type = kStoreDoubleword;
2641 break;
2642 case Primitive::kPrimVoid:
2643 LOG(FATAL) << "Unreachable type " << type;
2644 UNREACHABLE();
2645 }
2646 if (!Primitive::IsFloatingPointType(type)) {
2647 DCHECK(locations->InAt(1).IsRegister());
2648 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2649 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2650 } else {
2651 DCHECK(locations->InAt(1).IsFpuRegister());
2652 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2653 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2654 }
2655
2656 codegen_->MaybeRecordImplicitNullCheck(instruction);
2657 // TODO: memory barriers?
2658 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2659 DCHECK(locations->InAt(1).IsRegister());
2660 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2661 codegen_->MarkGCCard(obj, src);
2662 }
2663}
2664
2665void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2666 HandleFieldGet(instruction, instruction->GetFieldInfo());
2667}
2668
2669void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2670 HandleFieldGet(instruction, instruction->GetFieldInfo());
2671}
2672
2673void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2674 HandleFieldSet(instruction, instruction->GetFieldInfo());
2675}
2676
2677void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2678 HandleFieldSet(instruction, instruction->GetFieldInfo());
2679}
2680
2681void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2682 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002683 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002684 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2685 locations->SetInAt(0, Location::RequiresRegister());
2686 locations->SetInAt(1, Location::RequiresRegister());
2687 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002688 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002689 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2690}
2691
2692void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2693 LocationSummary* locations = instruction->GetLocations();
2694 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2695 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2696 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2697
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002698 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002699
2700 // Return 0 if `obj` is null.
2701 // TODO: Avoid this check if we know `obj` is not null.
2702 __ Move(out, ZERO);
2703 __ Beqzc(obj, &done);
2704
2705 // Compare the class of `obj` with `cls`.
2706 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002707 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002708 // Classes must be equal for the instanceof to succeed.
2709 __ Xor(out, out, cls);
2710 __ Sltiu(out, out, 1);
2711 } else {
2712 // If the classes are not equal, we go into a slow path.
2713 DCHECK(locations->OnlyCallsOnSlowPath());
2714 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002715 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002716 codegen_->AddSlowPath(slow_path);
2717 __ Bnec(out, cls, slow_path->GetEntryLabel());
2718 __ LoadConst32(out, 1);
2719 __ Bind(slow_path->GetExitLabel());
2720 }
2721
2722 __ Bind(&done);
2723}
2724
2725void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2726 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2727 locations->SetOut(Location::ConstantLocation(constant));
2728}
2729
2730void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2731 // Will be generated at use site.
2732}
2733
2734void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2735 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2736 locations->SetOut(Location::ConstantLocation(constant));
2737}
2738
2739void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2740 // Will be generated at use site.
2741}
2742
Calin Juravle175dc732015-08-25 15:42:32 +01002743void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2744 // The trampoline uses the same calling convention as dex calling conventions,
2745 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2746 // the method_idx.
2747 HandleInvoke(invoke);
2748}
2749
2750void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2751 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2752}
2753
Alexey Frunze4dda3372015-06-01 18:31:49 -07002754void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2755 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2756 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2757}
2758
2759void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2760 HandleInvoke(invoke);
2761 // The register T0 is required to be used for the hidden argument in
2762 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2763 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2764}
2765
2766void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2767 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2768 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2769 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2770 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2771 Location receiver = invoke->GetLocations()->InAt(0);
2772 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2773 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
2774
2775 // Set the hidden argument.
2776 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2777 invoke->GetDexMethodIndex());
2778
2779 // temp = object->GetClass();
2780 if (receiver.IsStackSlot()) {
2781 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2782 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2783 } else {
2784 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2785 }
2786 codegen_->MaybeRecordImplicitNullCheck(invoke);
2787 // temp = temp->GetImtEntryAt(method_offset);
2788 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2789 // T9 = temp->GetEntryPoint();
2790 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2791 // T9();
2792 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002793 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002794 DCHECK(!codegen_->IsLeafMethod());
2795 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2796}
2797
2798void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002799 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2800 if (intrinsic.TryDispatch(invoke)) {
2801 return;
2802 }
2803
Alexey Frunze4dda3372015-06-01 18:31:49 -07002804 HandleInvoke(invoke);
2805}
2806
2807void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2808 // When we do not run baseline, explicit clinit checks triggered by static
2809 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2810 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
2811
Chris Larsen3039e382015-08-26 07:54:08 -07002812 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2813 if (intrinsic.TryDispatch(invoke)) {
2814 return;
2815 }
2816
Alexey Frunze4dda3372015-06-01 18:31:49 -07002817 HandleInvoke(invoke);
2818
2819 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
2820 // clobbering somewhere else, reduce further register pressure by avoiding
2821 // allocation of a register for the current method pointer like on x86 baseline.
2822 // TODO: remove this once all the issues with register saving/restoring are
2823 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002824 if (invoke->HasCurrentMethodInput()) {
2825 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00002826 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002827 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002828 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002829 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002830 }
2831}
2832
Chris Larsen3039e382015-08-26 07:54:08 -07002833static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002834 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002835 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2836 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002837 return true;
2838 }
2839 return false;
2840}
2841
Vladimir Markodc151b22015-10-15 18:02:30 +01002842HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
2843 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
2844 MethodReference target_method ATTRIBUTE_UNUSED) {
2845 switch (desired_dispatch_info.method_load_kind) {
2846 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2847 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2848 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
2849 return HInvokeStaticOrDirect::DispatchInfo {
2850 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
2851 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2852 0u,
2853 0u
2854 };
2855 default:
2856 break;
2857 }
2858 switch (desired_dispatch_info.code_ptr_location) {
2859 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2860 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
2861 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
2862 return HInvokeStaticOrDirect::DispatchInfo {
2863 desired_dispatch_info.method_load_kind,
2864 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2865 desired_dispatch_info.method_load_data,
2866 0u
2867 };
2868 default:
2869 return desired_dispatch_info;
2870 }
2871}
2872
Alexey Frunze4dda3372015-06-01 18:31:49 -07002873void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
2874 // All registers are assumed to be correctly set up per the calling convention.
2875
Vladimir Marko58155012015-08-19 12:49:41 +00002876 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2877 switch (invoke->GetMethodLoadKind()) {
2878 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2879 // temp = thread->string_init_entrypoint
2880 __ LoadFromOffset(kLoadDoubleword,
2881 temp.AsRegister<GpuRegister>(),
2882 TR,
2883 invoke->GetStringInitOffset());
2884 break;
2885 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00002886 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00002887 break;
2888 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2889 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
2890 break;
2891 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00002892 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01002893 // TODO: Implement these types.
2894 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
2895 LOG(FATAL) << "Unsupported";
2896 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00002897 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002898 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00002899 GpuRegister reg = temp.AsRegister<GpuRegister>();
2900 GpuRegister method_reg;
2901 if (current_method.IsRegister()) {
2902 method_reg = current_method.AsRegister<GpuRegister>();
2903 } else {
2904 // TODO: use the appropriate DCHECK() here if possible.
2905 // DCHECK(invoke->GetLocations()->Intrinsified());
2906 DCHECK(!current_method.IsValid());
2907 method_reg = reg;
2908 __ Ld(reg, SP, kCurrentMethodStackOffset);
2909 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002910
Vladimir Marko58155012015-08-19 12:49:41 +00002911 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002912 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00002913 reg,
2914 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01002915 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00002916 // temp = temp[index_in_cache]
2917 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2918 __ LoadFromOffset(kLoadDoubleword,
2919 reg,
2920 reg,
2921 CodeGenerator::GetCachePointerOffset(index_in_cache));
2922 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002923 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002924 }
2925
Vladimir Marko58155012015-08-19 12:49:41 +00002926 switch (invoke->GetCodePtrLocation()) {
2927 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002928 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00002929 break;
2930 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2931 // LR = invoke->GetDirectCodePtr();
2932 __ LoadConst64(T9, invoke->GetDirectCodePtr());
2933 // LR()
2934 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002935 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00002936 break;
Vladimir Marko58155012015-08-19 12:49:41 +00002937 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01002938 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
2939 // TODO: Implement these types.
2940 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
2941 LOG(FATAL) << "Unsupported";
2942 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00002943 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2944 // T9 = callee_method->entry_point_from_quick_compiled_code_;
2945 __ LoadFromOffset(kLoadDoubleword,
2946 T9,
2947 callee_method.AsRegister<GpuRegister>(),
2948 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2949 kMips64WordSize).Int32Value());
2950 // T9()
2951 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002952 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00002953 break;
2954 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002955 DCHECK(!IsLeafMethod());
2956}
2957
2958void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2959 // When we do not run baseline, explicit clinit checks triggered by static
2960 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2961 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
2962
2963 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2964 return;
2965 }
2966
2967 LocationSummary* locations = invoke->GetLocations();
2968 codegen_->GenerateStaticOrDirectCall(invoke,
2969 locations->HasTemps()
2970 ? locations->GetTemp(0)
2971 : Location::NoLocation());
2972 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2973}
2974
Alexey Frunze53afca12015-11-05 16:34:23 -08002975void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002976 LocationSummary* locations = invoke->GetLocations();
2977 Location receiver = locations->InAt(0);
Alexey Frunze53afca12015-11-05 16:34:23 -08002978 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002979 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2980 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
2981 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2982 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
2983
2984 // temp = object->GetClass();
2985 DCHECK(receiver.IsRegister());
2986 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08002987 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002988 // temp = temp->GetMethodAt(method_offset);
2989 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2990 // T9 = temp->GetEntryPoint();
2991 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2992 // T9();
2993 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002994 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08002995}
2996
2997void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
2998 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2999 return;
3000 }
3001
3002 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003003 DCHECK(!codegen_->IsLeafMethod());
3004 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3005}
3006
3007void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003008 InvokeRuntimeCallingConvention calling_convention;
3009 CodeGenerator::CreateLoadClassLocationSummary(
3010 cls,
3011 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003012 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003013}
3014
3015void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3016 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003017 if (cls->NeedsAccessCheck()) {
3018 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3019 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3020 cls,
3021 cls->GetDexPc(),
3022 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01003023 return;
3024 }
3025
3026 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3027 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3028 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003029 DCHECK(!cls->CanCallRuntime());
3030 DCHECK(!cls->MustGenerateClinitCheck());
3031 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3032 ArtMethod::DeclaringClassOffset().Int32Value());
3033 } else {
3034 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003035 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3036 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003037 __ LoadFromOffset(kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003038 // TODO: We will need a read barrier here.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003039 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3040 cls,
3041 cls,
3042 cls->GetDexPc(),
3043 cls->MustGenerateClinitCheck());
3044 codegen_->AddSlowPath(slow_path);
3045 __ Beqzc(out, slow_path->GetEntryLabel());
3046 if (cls->MustGenerateClinitCheck()) {
3047 GenerateClassInitializationCheck(slow_path, out);
3048 } else {
3049 __ Bind(slow_path->GetExitLabel());
3050 }
3051 }
3052}
3053
David Brazdilcb1c0552015-08-04 16:22:25 +01003054static int32_t GetExceptionTlsOffset() {
3055 return Thread::ExceptionOffset<kMips64WordSize>().Int32Value();
3056}
3057
Alexey Frunze4dda3372015-06-01 18:31:49 -07003058void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3059 LocationSummary* locations =
3060 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3061 locations->SetOut(Location::RequiresRegister());
3062}
3063
3064void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3065 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003066 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3067}
3068
3069void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3070 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3071}
3072
3073void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3074 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003075}
3076
3077void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3078 load->SetLocations(nullptr);
3079}
3080
3081void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3082 // Nothing to do, this is driven by the code generator.
3083}
3084
3085void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
3086 LocationSummary* locations =
3087 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3088 locations->SetInAt(0, Location::RequiresRegister());
3089 locations->SetOut(Location::RequiresRegister());
3090}
3091
3092void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
3093 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3094 codegen_->AddSlowPath(slow_path);
3095
3096 LocationSummary* locations = load->GetLocations();
3097 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3098 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3099 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3100 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003101 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003102 __ LoadFromOffset(kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003103 // TODO: We will need a read barrier here.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003104 __ Beqzc(out, slow_path->GetEntryLabel());
3105 __ Bind(slow_path->GetExitLabel());
3106}
3107
3108void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3109 local->SetLocations(nullptr);
3110}
3111
3112void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3113 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3114}
3115
3116void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3117 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3118 locations->SetOut(Location::ConstantLocation(constant));
3119}
3120
3121void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3122 // Will be generated at use site.
3123}
3124
3125void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3126 LocationSummary* locations =
3127 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3128 InvokeRuntimeCallingConvention calling_convention;
3129 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3130}
3131
3132void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3133 codegen_->InvokeRuntime(instruction->IsEnter()
3134 ? QUICK_ENTRY_POINT(pLockObject)
3135 : QUICK_ENTRY_POINT(pUnlockObject),
3136 instruction,
3137 instruction->GetDexPc(),
3138 nullptr);
3139 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3140}
3141
3142void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3143 LocationSummary* locations =
3144 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3145 switch (mul->GetResultType()) {
3146 case Primitive::kPrimInt:
3147 case Primitive::kPrimLong:
3148 locations->SetInAt(0, Location::RequiresRegister());
3149 locations->SetInAt(1, Location::RequiresRegister());
3150 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3151 break;
3152
3153 case Primitive::kPrimFloat:
3154 case Primitive::kPrimDouble:
3155 locations->SetInAt(0, Location::RequiresFpuRegister());
3156 locations->SetInAt(1, Location::RequiresFpuRegister());
3157 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3158 break;
3159
3160 default:
3161 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3162 }
3163}
3164
3165void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3166 Primitive::Type type = instruction->GetType();
3167 LocationSummary* locations = instruction->GetLocations();
3168
3169 switch (type) {
3170 case Primitive::kPrimInt:
3171 case Primitive::kPrimLong: {
3172 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3173 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3174 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3175 if (type == Primitive::kPrimInt)
3176 __ MulR6(dst, lhs, rhs);
3177 else
3178 __ Dmul(dst, lhs, rhs);
3179 break;
3180 }
3181 case Primitive::kPrimFloat:
3182 case Primitive::kPrimDouble: {
3183 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3184 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3185 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3186 if (type == Primitive::kPrimFloat)
3187 __ MulS(dst, lhs, rhs);
3188 else
3189 __ MulD(dst, lhs, rhs);
3190 break;
3191 }
3192 default:
3193 LOG(FATAL) << "Unexpected mul type " << type;
3194 }
3195}
3196
3197void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3198 LocationSummary* locations =
3199 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3200 switch (neg->GetResultType()) {
3201 case Primitive::kPrimInt:
3202 case Primitive::kPrimLong:
3203 locations->SetInAt(0, Location::RequiresRegister());
3204 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3205 break;
3206
3207 case Primitive::kPrimFloat:
3208 case Primitive::kPrimDouble:
3209 locations->SetInAt(0, Location::RequiresFpuRegister());
3210 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3211 break;
3212
3213 default:
3214 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3215 }
3216}
3217
3218void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3219 Primitive::Type type = instruction->GetType();
3220 LocationSummary* locations = instruction->GetLocations();
3221
3222 switch (type) {
3223 case Primitive::kPrimInt:
3224 case Primitive::kPrimLong: {
3225 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3226 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3227 if (type == Primitive::kPrimInt)
3228 __ Subu(dst, ZERO, src);
3229 else
3230 __ Dsubu(dst, ZERO, src);
3231 break;
3232 }
3233 case Primitive::kPrimFloat:
3234 case Primitive::kPrimDouble: {
3235 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3236 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3237 if (type == Primitive::kPrimFloat)
3238 __ NegS(dst, src);
3239 else
3240 __ NegD(dst, src);
3241 break;
3242 }
3243 default:
3244 LOG(FATAL) << "Unexpected neg type " << type;
3245 }
3246}
3247
3248void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3249 LocationSummary* locations =
3250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3251 InvokeRuntimeCallingConvention calling_convention;
3252 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3253 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3254 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3255 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3256}
3257
3258void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3259 LocationSummary* locations = instruction->GetLocations();
3260 // Move an uint16_t value to a register.
3261 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003262 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3263 instruction,
3264 instruction->GetDexPc(),
3265 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003266 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3267}
3268
3269void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3270 LocationSummary* locations =
3271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3272 InvokeRuntimeCallingConvention calling_convention;
3273 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3274 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3275 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3276}
3277
3278void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
3279 LocationSummary* locations = instruction->GetLocations();
3280 // Move an uint16_t value to a register.
3281 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003282 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3283 instruction,
3284 instruction->GetDexPc(),
3285 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003286 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3287}
3288
3289void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3290 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3291 locations->SetInAt(0, Location::RequiresRegister());
3292 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3293}
3294
3295void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3296 Primitive::Type type = instruction->GetType();
3297 LocationSummary* locations = instruction->GetLocations();
3298
3299 switch (type) {
3300 case Primitive::kPrimInt:
3301 case Primitive::kPrimLong: {
3302 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3303 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3304 __ Nor(dst, src, ZERO);
3305 break;
3306 }
3307
3308 default:
3309 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3310 }
3311}
3312
3313void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3314 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3315 locations->SetInAt(0, Location::RequiresRegister());
3316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3317}
3318
3319void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3320 LocationSummary* locations = instruction->GetLocations();
3321 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3322 locations->InAt(0).AsRegister<GpuRegister>(),
3323 1);
3324}
3325
3326void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003327 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3328 ? LocationSummary::kCallOnSlowPath
3329 : LocationSummary::kNoCall;
3330 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003331 locations->SetInAt(0, Location::RequiresRegister());
3332 if (instruction->HasUses()) {
3333 locations->SetOut(Location::SameAsFirstInput());
3334 }
3335}
3336
3337void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3338 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3339 return;
3340 }
3341 Location obj = instruction->GetLocations()->InAt(0);
3342
3343 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3344 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3345}
3346
3347void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3348 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3349 codegen_->AddSlowPath(slow_path);
3350
3351 Location obj = instruction->GetLocations()->InAt(0);
3352
3353 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3354}
3355
3356void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003357 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003358 GenerateImplicitNullCheck(instruction);
3359 } else {
3360 GenerateExplicitNullCheck(instruction);
3361 }
3362}
3363
3364void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3365 HandleBinaryOp(instruction);
3366}
3367
3368void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3369 HandleBinaryOp(instruction);
3370}
3371
3372void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3373 LOG(FATAL) << "Unreachable";
3374}
3375
3376void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3377 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3378}
3379
3380void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3381 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3382 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3383 if (location.IsStackSlot()) {
3384 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3385 } else if (location.IsDoubleStackSlot()) {
3386 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3387 }
3388 locations->SetOut(location);
3389}
3390
3391void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3392 ATTRIBUTE_UNUSED) {
3393 // Nothing to do, the parameter is already at its location.
3394}
3395
3396void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3397 LocationSummary* locations =
3398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3399 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3400}
3401
3402void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3403 ATTRIBUTE_UNUSED) {
3404 // Nothing to do, the method is already at its location.
3405}
3406
3407void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3408 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3409 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3410 locations->SetInAt(i, Location::Any());
3411 }
3412 locations->SetOut(Location::Any());
3413}
3414
3415void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3416 LOG(FATAL) << "Unreachable";
3417}
3418
3419void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3420 Primitive::Type type = rem->GetResultType();
3421 LocationSummary::CallKind call_kind =
3422 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3423 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3424
3425 switch (type) {
3426 case Primitive::kPrimInt:
3427 case Primitive::kPrimLong:
3428 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003429 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3431 break;
3432
3433 case Primitive::kPrimFloat:
3434 case Primitive::kPrimDouble: {
3435 InvokeRuntimeCallingConvention calling_convention;
3436 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3437 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3438 locations->SetOut(calling_convention.GetReturnLocation(type));
3439 break;
3440 }
3441
3442 default:
3443 LOG(FATAL) << "Unexpected rem type " << type;
3444 }
3445}
3446
3447void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3448 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003449
3450 switch (type) {
3451 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003452 case Primitive::kPrimLong:
3453 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003454 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003455
3456 case Primitive::kPrimFloat:
3457 case Primitive::kPrimDouble: {
3458 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3459 : QUICK_ENTRY_POINT(pFmod);
3460 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
3461 break;
3462 }
3463 default:
3464 LOG(FATAL) << "Unexpected rem type " << type;
3465 }
3466}
3467
3468void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3469 memory_barrier->SetLocations(nullptr);
3470}
3471
3472void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3473 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3474}
3475
3476void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3478 Primitive::Type return_type = ret->InputAt(0)->GetType();
3479 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3480}
3481
3482void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3483 codegen_->GenerateFrameExit();
3484}
3485
3486void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3487 ret->SetLocations(nullptr);
3488}
3489
3490void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3491 codegen_->GenerateFrameExit();
3492}
3493
3494void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3495 HandleShift(shl);
3496}
3497
3498void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3499 HandleShift(shl);
3500}
3501
3502void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3503 HandleShift(shr);
3504}
3505
3506void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3507 HandleShift(shr);
3508}
3509
3510void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3512 Primitive::Type field_type = store->InputAt(1)->GetType();
3513 switch (field_type) {
3514 case Primitive::kPrimNot:
3515 case Primitive::kPrimBoolean:
3516 case Primitive::kPrimByte:
3517 case Primitive::kPrimChar:
3518 case Primitive::kPrimShort:
3519 case Primitive::kPrimInt:
3520 case Primitive::kPrimFloat:
3521 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3522 break;
3523
3524 case Primitive::kPrimLong:
3525 case Primitive::kPrimDouble:
3526 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3527 break;
3528
3529 default:
3530 LOG(FATAL) << "Unimplemented local type " << field_type;
3531 }
3532}
3533
3534void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3535}
3536
3537void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3538 HandleBinaryOp(instruction);
3539}
3540
3541void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3542 HandleBinaryOp(instruction);
3543}
3544
3545void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3546 HandleFieldGet(instruction, instruction->GetFieldInfo());
3547}
3548
3549void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3550 HandleFieldGet(instruction, instruction->GetFieldInfo());
3551}
3552
3553void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3554 HandleFieldSet(instruction, instruction->GetFieldInfo());
3555}
3556
3557void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3558 HandleFieldSet(instruction, instruction->GetFieldInfo());
3559}
3560
Calin Juravlee460d1d2015-09-29 04:52:17 +01003561void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3562 HUnresolvedInstanceFieldGet* instruction) {
3563 FieldAccessCallingConventionMIPS64 calling_convention;
3564 codegen_->CreateUnresolvedFieldLocationSummary(
3565 instruction, instruction->GetFieldType(), calling_convention);
3566}
3567
3568void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3569 HUnresolvedInstanceFieldGet* instruction) {
3570 FieldAccessCallingConventionMIPS64 calling_convention;
3571 codegen_->GenerateUnresolvedFieldAccess(instruction,
3572 instruction->GetFieldType(),
3573 instruction->GetFieldIndex(),
3574 instruction->GetDexPc(),
3575 calling_convention);
3576}
3577
3578void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3579 HUnresolvedInstanceFieldSet* instruction) {
3580 FieldAccessCallingConventionMIPS64 calling_convention;
3581 codegen_->CreateUnresolvedFieldLocationSummary(
3582 instruction, instruction->GetFieldType(), calling_convention);
3583}
3584
3585void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3586 HUnresolvedInstanceFieldSet* instruction) {
3587 FieldAccessCallingConventionMIPS64 calling_convention;
3588 codegen_->GenerateUnresolvedFieldAccess(instruction,
3589 instruction->GetFieldType(),
3590 instruction->GetFieldIndex(),
3591 instruction->GetDexPc(),
3592 calling_convention);
3593}
3594
3595void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3596 HUnresolvedStaticFieldGet* instruction) {
3597 FieldAccessCallingConventionMIPS64 calling_convention;
3598 codegen_->CreateUnresolvedFieldLocationSummary(
3599 instruction, instruction->GetFieldType(), calling_convention);
3600}
3601
3602void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3603 HUnresolvedStaticFieldGet* instruction) {
3604 FieldAccessCallingConventionMIPS64 calling_convention;
3605 codegen_->GenerateUnresolvedFieldAccess(instruction,
3606 instruction->GetFieldType(),
3607 instruction->GetFieldIndex(),
3608 instruction->GetDexPc(),
3609 calling_convention);
3610}
3611
3612void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3613 HUnresolvedStaticFieldSet* instruction) {
3614 FieldAccessCallingConventionMIPS64 calling_convention;
3615 codegen_->CreateUnresolvedFieldLocationSummary(
3616 instruction, instruction->GetFieldType(), calling_convention);
3617}
3618
3619void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3620 HUnresolvedStaticFieldSet* instruction) {
3621 FieldAccessCallingConventionMIPS64 calling_convention;
3622 codegen_->GenerateUnresolvedFieldAccess(instruction,
3623 instruction->GetFieldType(),
3624 instruction->GetFieldIndex(),
3625 instruction->GetDexPc(),
3626 calling_convention);
3627}
3628
Alexey Frunze4dda3372015-06-01 18:31:49 -07003629void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3630 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3631}
3632
3633void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3634 HBasicBlock* block = instruction->GetBlock();
3635 if (block->GetLoopInformation() != nullptr) {
3636 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3637 // The back edge will generate the suspend check.
3638 return;
3639 }
3640 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3641 // The goto will generate the suspend check.
3642 return;
3643 }
3644 GenerateSuspendCheck(instruction, nullptr);
3645}
3646
3647void LocationsBuilderMIPS64::VisitTemporary(HTemporary* temp) {
3648 temp->SetLocations(nullptr);
3649}
3650
3651void InstructionCodeGeneratorMIPS64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3652 // Nothing to do, this is driven by the code generator.
3653}
3654
3655void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3656 LocationSummary* locations =
3657 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3658 InvokeRuntimeCallingConvention calling_convention;
3659 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3660}
3661
3662void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3663 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3664 instruction,
3665 instruction->GetDexPc(),
3666 nullptr);
3667 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3668}
3669
3670void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3671 Primitive::Type input_type = conversion->GetInputType();
3672 Primitive::Type result_type = conversion->GetResultType();
3673 DCHECK_NE(input_type, result_type);
3674
3675 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3676 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3677 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3678 }
3679
3680 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3681 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
3682 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
3683 call_kind = LocationSummary::kCall;
3684 }
3685
3686 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3687
3688 if (call_kind == LocationSummary::kNoCall) {
3689 if (Primitive::IsFloatingPointType(input_type)) {
3690 locations->SetInAt(0, Location::RequiresFpuRegister());
3691 } else {
3692 locations->SetInAt(0, Location::RequiresRegister());
3693 }
3694
3695 if (Primitive::IsFloatingPointType(result_type)) {
3696 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3697 } else {
3698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3699 }
3700 } else {
3701 InvokeRuntimeCallingConvention calling_convention;
3702
3703 if (Primitive::IsFloatingPointType(input_type)) {
3704 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3705 } else {
3706 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3707 }
3708
3709 locations->SetOut(calling_convention.GetReturnLocation(result_type));
3710 }
3711}
3712
3713void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3714 LocationSummary* locations = conversion->GetLocations();
3715 Primitive::Type result_type = conversion->GetResultType();
3716 Primitive::Type input_type = conversion->GetInputType();
3717
3718 DCHECK_NE(input_type, result_type);
3719
3720 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3721 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3722 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3723
3724 switch (result_type) {
3725 case Primitive::kPrimChar:
3726 __ Andi(dst, src, 0xFFFF);
3727 break;
3728 case Primitive::kPrimByte:
3729 // long is never converted into types narrower than int directly,
3730 // so SEB and SEH can be used without ever causing unpredictable results
3731 // on 64-bit inputs
3732 DCHECK(input_type != Primitive::kPrimLong);
3733 __ Seb(dst, src);
3734 break;
3735 case Primitive::kPrimShort:
3736 // long is never converted into types narrower than int directly,
3737 // so SEB and SEH can be used without ever causing unpredictable results
3738 // on 64-bit inputs
3739 DCHECK(input_type != Primitive::kPrimLong);
3740 __ Seh(dst, src);
3741 break;
3742 case Primitive::kPrimInt:
3743 case Primitive::kPrimLong:
3744 // Sign-extend 32-bit int into bits 32 through 63 for
3745 // int-to-long and long-to-int conversions
3746 __ Sll(dst, src, 0);
3747 break;
3748
3749 default:
3750 LOG(FATAL) << "Unexpected type conversion from " << input_type
3751 << " to " << result_type;
3752 }
3753 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
3754 if (input_type != Primitive::kPrimLong) {
3755 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3756 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3757 __ Mtc1(src, FTMP);
3758 if (result_type == Primitive::kPrimFloat) {
3759 __ Cvtsw(dst, FTMP);
3760 } else {
3761 __ Cvtdw(dst, FTMP);
3762 }
3763 } else {
3764 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
3765 : QUICK_ENTRY_POINT(pL2d);
3766 codegen_->InvokeRuntime(entry_offset,
3767 conversion,
3768 conversion->GetDexPc(),
3769 nullptr);
3770 }
3771 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3772 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3773 int32_t entry_offset;
3774 if (result_type != Primitive::kPrimLong) {
3775 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
3776 : QUICK_ENTRY_POINT(pD2iz);
3777 } else {
3778 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
3779 : QUICK_ENTRY_POINT(pD2l);
3780 }
3781 codegen_->InvokeRuntime(entry_offset,
3782 conversion,
3783 conversion->GetDexPc(),
3784 nullptr);
3785 } else if (Primitive::IsFloatingPointType(result_type) &&
3786 Primitive::IsFloatingPointType(input_type)) {
3787 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3788 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3789 if (result_type == Primitive::kPrimFloat) {
3790 __ Cvtsd(dst, src);
3791 } else {
3792 __ Cvtds(dst, src);
3793 }
3794 } else {
3795 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3796 << " to " << result_type;
3797 }
3798}
3799
3800void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3801 HandleShift(ushr);
3802}
3803
3804void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3805 HandleShift(ushr);
3806}
3807
3808void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3809 HandleBinaryOp(instruction);
3810}
3811
3812void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3813 HandleBinaryOp(instruction);
3814}
3815
3816void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3817 // Nothing to do, this should be removed during prepare for register allocator.
3818 LOG(FATAL) << "Unreachable";
3819}
3820
3821void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3822 // Nothing to do, this should be removed during prepare for register allocator.
3823 LOG(FATAL) << "Unreachable";
3824}
3825
3826void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
3827 VisitCondition(comp);
3828}
3829
3830void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
3831 VisitCondition(comp);
3832}
3833
3834void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
3835 VisitCondition(comp);
3836}
3837
3838void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
3839 VisitCondition(comp);
3840}
3841
3842void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
3843 VisitCondition(comp);
3844}
3845
3846void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
3847 VisitCondition(comp);
3848}
3849
3850void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3851 VisitCondition(comp);
3852}
3853
3854void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3855 VisitCondition(comp);
3856}
3857
3858void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
3859 VisitCondition(comp);
3860}
3861
3862void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
3863 VisitCondition(comp);
3864}
3865
3866void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3867 VisitCondition(comp);
3868}
3869
3870void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3871 VisitCondition(comp);
3872}
3873
Aart Bike9f37602015-10-09 11:15:55 -07003874void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
3875 VisitCondition(comp);
3876}
3877
3878void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
3879 VisitCondition(comp);
3880}
3881
3882void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
3883 VisitCondition(comp);
3884}
3885
3886void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
3887 VisitCondition(comp);
3888}
3889
3890void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
3891 VisitCondition(comp);
3892}
3893
3894void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
3895 VisitCondition(comp);
3896}
3897
3898void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
3899 VisitCondition(comp);
3900}
3901
3902void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
3903 VisitCondition(comp);
3904}
3905
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003906void LocationsBuilderMIPS64::VisitFakeString(HFakeString* instruction) {
3907 DCHECK(codegen_->IsBaseline());
3908 LocationSummary* locations =
3909 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3910 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3911}
3912
3913void InstructionCodeGeneratorMIPS64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3914 DCHECK(codegen_->IsBaseline());
3915 // Will be generated at use site.
3916}
3917
Mark Mendellfe57faa2015-09-18 09:26:15 -04003918// Simple implementation of packed switch - generate cascaded compare/jumps.
3919void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3920 LocationSummary* locations =
3921 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3922 locations->SetInAt(0, Location::RequiresRegister());
3923}
3924
3925void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3926 int32_t lower_bound = switch_instr->GetStartValue();
3927 int32_t num_entries = switch_instr->GetNumEntries();
3928 LocationSummary* locations = switch_instr->GetLocations();
3929 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
3930 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3931
3932 // Create a series of compare/jumps.
3933 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3934 for (int32_t i = 0; i < num_entries; i++) {
3935 int32_t case_value = lower_bound + i;
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003936 Mips64Label* succ = codegen_->GetLabelOf(successors[i]);
Mark Mendellfe57faa2015-09-18 09:26:15 -04003937 if (case_value == 0) {
3938 __ Beqzc(value_reg, succ);
3939 } else {
3940 __ LoadConst32(TMP, case_value);
3941 __ Beqc(value_reg, TMP, succ);
3942 }
3943 }
3944
3945 // And the default for any other value.
3946 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003947 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04003948 }
3949}
3950
Alexey Frunze4dda3372015-06-01 18:31:49 -07003951} // namespace mips64
3952} // namespace art