blob: 31be1c21a69c625134a725ebfa9eef6b4ffd95d3 [file] [log] [blame]
Maja Gagic6ea651f2015-02-24 16:55:04 +01001/*
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/* This file contains codegen for the Mips64 ISA */
18
19#include "codegen_mips64.h"
20
21#include "base/logging.h"
22#include "dex/mir_graph.h"
23#include "dex/quick/mir_to_lir-inl.h"
24#include "entrypoints/quick/quick_entrypoints.h"
25#include "gc/accounting/card_table.h"
26#include "mips64_lir.h"
27#include "mirror/art_method.h"
28#include "mirror/object_array-inl.h"
29
30namespace art {
31
32bool Mips64Mir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special) {
33 // TODO
34 UNUSED(bb, mir, special);
35 return false;
36}
37
38/*
39 * The lack of pc-relative loads on Mips64 presents somewhat of a challenge
40 * for our PIC switch table strategy. To materialize the current location
41 * we'll do a dummy JAL and reference our tables using rRA as the
42 * base register. Note that rRA will be used both as the base to
43 * locate the switch table data and as the reference base for the switch
44 * target offsets stored in the table. We'll use a special pseudo-instruction
45 * to represent the jal and trigger the construction of the
46 * switch table offsets (which will happen after final assembly and all
47 * labels are fixed).
48 *
49 * The test loop will look something like:
50 *
51 * ori r_end, rZERO, #table_size ; size in bytes
52 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
53 * nop ; opportunistically fill
54 * BaseLabel:
55 * addiu r_base, rRA, <table> - <BaseLabel> ; table relative to BaseLabel
56 addu r_end, r_end, r_base ; end of table
57 * lw r_val, [rSP, v_reg_off] ; Test Value
58 * loop:
59 * beq r_base, r_end, done
60 * lw r_key, 0(r_base)
61 * addu r_base, 8
62 * bne r_val, r_key, loop
63 * lw r_disp, -4(r_base)
64 * addu rRA, r_disp
65 * jalr rZERO, rRA
66 * done:
67 *
68 */
69void Mips64Mir2Lir::GenLargeSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
70 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
71 // Add the table to the list - we'll process it later.
72 SwitchTable* tab_rec = static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable),
73 kArenaAllocData));
74 tab_rec->switch_mir = mir;
75 tab_rec->table = table;
76 tab_rec->vaddr = current_dalvik_offset_;
77 int elements = table[1];
78 switch_tables_.push_back(tab_rec);
79
80 // The table is composed of 8-byte key/disp pairs.
81 int byte_size = elements * 8;
82
83 int size_hi = byte_size >> 16;
84 int size_lo = byte_size & 0xffff;
85
86 RegStorage r_end = AllocTempWide();
87 if (size_hi) {
88 NewLIR2(kMips64Lui, r_end.GetReg(), size_hi);
89 }
90 // Must prevent code motion for the curr pc pair.
91 GenBarrier(); // Scheduling barrier.
92 NewLIR0(kMips64CurrPC); // Really a jal to .+8.
93 // Now, fill the branch delay slot.
94 if (size_hi) {
95 NewLIR3(kMips64Ori, r_end.GetReg(), r_end.GetReg(), size_lo);
96 } else {
97 NewLIR3(kMips64Ori, r_end.GetReg(), rZERO, size_lo);
98 }
99 GenBarrier(); // Scheduling barrier.
100
101 // Construct BaseLabel and set up table base register.
102 LIR* base_label = NewLIR0(kPseudoTargetLabel);
103 // Remember base label so offsets can be computed later.
104 tab_rec->anchor = base_label;
105 RegStorage r_base = AllocTempWide();
106 NewLIR4(kMips64Delta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
107 OpRegRegReg(kOpAdd, r_end, r_end, r_base);
108
109 // Grab switch test value.
110 rl_src = LoadValue(rl_src, kCoreReg);
111
112 // Test loop.
113 RegStorage r_key = AllocTemp();
114 LIR* loop_label = NewLIR0(kPseudoTargetLabel);
115 LIR* exit_branch = OpCmpBranch(kCondEq, r_base, r_end, NULL);
116 Load32Disp(r_base, 0, r_key);
117 OpRegImm(kOpAdd, r_base, 8);
118 OpCmpBranch(kCondNe, rl_src.reg, r_key, loop_label);
119 RegStorage r_disp = AllocTemp();
120 Load32Disp(r_base, -4, r_disp);
121 OpRegRegReg(kOpAdd, TargetReg(kLr, kWide), TargetReg(kLr, kWide), r_disp);
122 OpReg(kOpBx, TargetReg(kLr, kWide));
123
124 // Loop exit.
125 LIR* exit_label = NewLIR0(kPseudoTargetLabel);
126 exit_branch->target = exit_label;
127}
128
129/*
130 * Code pattern will look something like:
131 *
132 * lw r_val
133 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
134 * nop ; opportunistically fill
135 * [subiu r_val, bias] ; Remove bias if low_val != 0
136 * bound check -> done
137 * lw r_disp, [rRA, r_val]
138 * addu rRA, r_disp
139 * jalr rZERO, rRA
140 * done:
141 */
142void Mips64Mir2Lir::GenLargePackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
143 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
144 // Add the table to the list - we'll process it later.
145 SwitchTable* tab_rec =
146 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
147 tab_rec->switch_mir = mir;
148 tab_rec->table = table;
149 tab_rec->vaddr = current_dalvik_offset_;
150 int size = table[1];
151 switch_tables_.push_back(tab_rec);
152
153 // Get the switch value.
154 rl_src = LoadValue(rl_src, kCoreReg);
155
156 // Prepare the bias. If too big, handle 1st stage here.
157 int low_key = s4FromSwitchData(&table[2]);
158 bool large_bias = false;
159 RegStorage r_key;
160 if (low_key == 0) {
161 r_key = rl_src.reg;
162 } else if ((low_key & 0xffff) != low_key) {
163 r_key = AllocTemp();
164 LoadConstant(r_key, low_key);
165 large_bias = true;
166 } else {
167 r_key = AllocTemp();
168 }
169
170 // Must prevent code motion for the curr pc pair.
171 GenBarrier();
172 NewLIR0(kMips64CurrPC); // Really a jal to .+8.
173 // Now, fill the branch delay slot with bias strip.
174 if (low_key == 0) {
175 NewLIR0(kMips64Nop);
176 } else {
177 if (large_bias) {
178 OpRegRegReg(kOpSub, r_key, rl_src.reg, r_key);
179 } else {
180 OpRegRegImm(kOpSub, r_key, rl_src.reg, low_key);
181 }
182 }
183 GenBarrier(); // Scheduling barrier.
184
185 // Construct BaseLabel and set up table base register.
186 LIR* base_label = NewLIR0(kPseudoTargetLabel);
187 // Remember base label so offsets can be computed later.
188 tab_rec->anchor = base_label;
189
190 // Bounds check - if < 0 or >= size continue following switch.
191 LIR* branch_over = OpCmpImmBranch(kCondHi, r_key, size-1, NULL);
192
193 // Materialize the table base pointer.
194 RegStorage r_base = AllocTempWide();
195 NewLIR4(kMips64Delta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
196
197 // Load the displacement from the switch table.
198 RegStorage r_disp = AllocTemp();
199 LoadBaseIndexed(r_base, r_key, r_disp, 2, k32);
200
201 // Add to rAP and go.
202 OpRegRegReg(kOpAdd, TargetReg(kLr, kWide), TargetReg(kLr, kWide), r_disp);
203 OpReg(kOpBx, TargetReg(kLr, kWide));
204
205 // Branch_over target here.
206 LIR* target = NewLIR0(kPseudoTargetLabel);
207 branch_over->target = target;
208}
209
210void Mips64Mir2Lir::GenMoveException(RegLocation rl_dest) {
211 int ex_offset = Thread::ExceptionOffset<8>().Int32Value();
212 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
213 RegStorage reset_reg = AllocTempRef();
214 LoadRefDisp(rs_rMIPS64_SELF, ex_offset, rl_result.reg, kNotVolatile);
215 LoadConstant(reset_reg, 0);
216 StoreRefDisp(rs_rMIPS64_SELF, ex_offset, reset_reg, kNotVolatile);
217 FreeTemp(reset_reg);
218 StoreValue(rl_dest, rl_result);
219}
220
221void Mips64Mir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
222 RegStorage reg_card_base = AllocTempWide();
223 RegStorage reg_card_no = AllocTempWide();
224 // NOTE: native pointer.
225 LoadWordDisp(rs_rMIPS64_SELF, Thread::CardTableOffset<8>().Int32Value(), reg_card_base);
226 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
227 StoreBaseIndexed(reg_card_base, reg_card_no, As32BitReg(reg_card_base), 0, kUnsignedByte);
228 FreeTemp(reg_card_base);
229 FreeTemp(reg_card_no);
230}
231
232void Mips64Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
233 int spill_count = num_core_spills_ + num_fp_spills_;
234 /*
235 * On entry, rMIPS64_ARG0, rMIPS64_ARG1, rMIPS64_ARG2, rMIPS64_ARG3,
236 * rMIPS64_ARG4, rMIPS64_ARG5, rMIPS64_ARG6 & rMIPS64_ARG7 are live.
237 * Let the register allocation mechanism know so it doesn't try to
238 * use any of them when expanding the frame or flushing.
239 */
240 LockTemp(rs_rMIPS64_ARG0);
241 LockTemp(rs_rMIPS64_ARG1);
242 LockTemp(rs_rMIPS64_ARG2);
243 LockTemp(rs_rMIPS64_ARG3);
244 LockTemp(rs_rMIPS64_ARG4);
245 LockTemp(rs_rMIPS64_ARG5);
246 LockTemp(rs_rMIPS64_ARG6);
247 LockTemp(rs_rMIPS64_ARG7);
248
249 /*
250 * We can safely skip the stack overflow check if we're
251 * a leaf *and* our frame size < fudge factor.
252 */
253 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_,
254 kMips64);
255 NewLIR0(kPseudoMethodEntry);
256 RegStorage check_reg = AllocTempWide();
257 RegStorage new_sp = AllocTempWide();
258 if (!skip_overflow_check) {
259 // Load stack limit.
260 LoadWordDisp(rs_rMIPS64_SELF, Thread::StackEndOffset<8>().Int32Value(), check_reg);
261 }
262 // Spill core callee saves.
263 SpillCoreRegs();
264 // NOTE: promotion of FP regs currently unsupported, thus no FP spill.
265 DCHECK_EQ(num_fp_spills_, 0);
266 const int frame_sub = frame_size_ - spill_count * 8;
267 if (!skip_overflow_check) {
268 class StackOverflowSlowPath : public LIRSlowPath {
269 public:
270 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
271 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), sp_displace_(sp_displace) {
272 }
273 void Compile() OVERRIDE {
274 m2l_->ResetRegPool();
275 m2l_->ResetDefTracking();
276 GenerateTargetLabel(kPseudoThrowTarget);
277 // Load RA from the top of the frame.
278 m2l_->LoadWordDisp(rs_rMIPS64_SP, sp_displace_ - 8, rs_rRAd);
279 m2l_->OpRegImm(kOpAdd, rs_rMIPS64_SP, sp_displace_);
280 m2l_->ClobberCallerSave();
281 RegStorage r_tgt = m2l_->CallHelperSetup(kQuickThrowStackOverflow); // Doesn't clobber LR.
282 m2l_->CallHelper(r_tgt, kQuickThrowStackOverflow, false /* MarkSafepointPC */,
283 false /* UseLink */);
284 }
285
286 private:
287 const size_t sp_displace_;
288 };
289 OpRegRegImm(kOpSub, new_sp, rs_rMIPS64_SP, frame_sub);
290 LIR* branch = OpCmpBranch(kCondUlt, new_sp, check_reg, nullptr);
291 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, spill_count * 8));
292 // TODO: avoid copy for small frame sizes.
293 OpRegCopy(rs_rMIPS64_SP, new_sp); // Establish stack.
294 } else {
295 OpRegImm(kOpSub, rs_rMIPS64_SP, frame_sub);
296 }
297
298 FlushIns(ArgLocs, rl_method);
299
300 FreeTemp(rs_rMIPS64_ARG0);
301 FreeTemp(rs_rMIPS64_ARG1);
302 FreeTemp(rs_rMIPS64_ARG2);
303 FreeTemp(rs_rMIPS64_ARG3);
304 FreeTemp(rs_rMIPS64_ARG4);
305 FreeTemp(rs_rMIPS64_ARG5);
306 FreeTemp(rs_rMIPS64_ARG6);
307 FreeTemp(rs_rMIPS64_ARG7);
308}
309
310void Mips64Mir2Lir::GenExitSequence() {
311 /*
312 * In the exit path, rMIPS64_RET0/rMIPS64_RET1 are live - make sure they aren't
313 * allocated by the register utilities as temps.
314 */
315 LockTemp(rs_rMIPS64_RET0);
316 LockTemp(rs_rMIPS64_RET1);
317
318 NewLIR0(kPseudoMethodExit);
319 UnSpillCoreRegs();
320 OpReg(kOpBx, rs_rRAd);
321}
322
323void Mips64Mir2Lir::GenSpecialExitSequence() {
324 OpReg(kOpBx, rs_rRAd);
325}
326
327void Mips64Mir2Lir::GenSpecialEntryForSuspend() {
328 // Keep 16-byte stack alignment - push A0, i.e. ArtMethod* and RA.
329 core_spill_mask_ = (1u << rs_rRAd.GetRegNum());
330 num_core_spills_ = 1u;
331 fp_spill_mask_ = 0u;
332 num_fp_spills_ = 0u;
333 frame_size_ = 16u;
334 core_vmap_table_.clear();
335 fp_vmap_table_.clear();
336 OpRegImm(kOpSub, rs_rMIPS64_SP, frame_size_);
337 StoreWordDisp(rs_rMIPS64_SP, frame_size_ - 8, rs_rRAd);
338 StoreWordDisp(rs_rMIPS64_SP, 0, rs_rA0d);
339}
340
341void Mips64Mir2Lir::GenSpecialExitForSuspend() {
342 // Pop the frame. Don't pop ArtMethod*, it's no longer needed.
343 LoadWordDisp(rs_rMIPS64_SP, frame_size_ - 8, rs_rRAd);
344 OpRegImm(kOpAdd, rs_rMIPS64_SP, frame_size_);
345}
346
347/*
348 * Bit of a hack here - in the absence of a real scheduling pass,
349 * emit the next instruction in static & direct invoke sequences.
350 */
351static int Mips64NextSDCallInsn(CompilationUnit* cu, CallInfo* info ATTRIBUTE_UNUSED, int state,
352 const MethodReference& target_method, uint32_t,
353 uintptr_t direct_code, uintptr_t direct_method, InvokeType type) {
354 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
355 if (direct_code != 0 && direct_method != 0) {
356 switch (state) {
357 case 0: // Get the current Method* [sets kArg0]
358 if (direct_code != static_cast<uintptr_t>(-1)) {
Mathieu Chartier921d6eb2015-03-13 16:32:44 -0700359 cg->LoadConstantWide(cg->TargetPtrReg(kInvokeTgt), direct_code);
Maja Gagic6ea651f2015-02-24 16:55:04 +0100360 } else {
361 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
362 }
363 if (direct_method != static_cast<uintptr_t>(-1)) {
Mathieu Chartier921d6eb2015-03-13 16:32:44 -0700364 cg->LoadConstantWide(cg->TargetReg(kArg0, kRef), direct_method);
Maja Gagic6ea651f2015-02-24 16:55:04 +0100365 } else {
366 cg->LoadMethodAddress(target_method, type, kArg0);
367 }
368 break;
369 default:
370 return -1;
371 }
372 } else {
373 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
374 switch (state) {
375 case 0: // Get the current Method* [sets kArg0]
376 // TUNING: we can save a reg copy if Method* has been promoted.
377 cg->LoadCurrMethodDirect(arg0_ref);
378 break;
379 case 1: // Get method->dex_cache_resolved_methods_
380 cg->LoadRefDisp(arg0_ref, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
381 arg0_ref, kNotVolatile);
382 // Set up direct code if known.
383 if (direct_code != 0) {
384 if (direct_code != static_cast<uintptr_t>(-1)) {
Mathieu Chartier921d6eb2015-03-13 16:32:44 -0700385 cg->LoadConstantWide(cg->TargetPtrReg(kInvokeTgt), direct_code);
Maja Gagic6ea651f2015-02-24 16:55:04 +0100386 } else {
387 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
388 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
389 }
390 }
391 break;
392 case 2: // Grab target method*
393 CHECK_EQ(cu->dex_file, target_method.dex_file);
394 cg->LoadRefDisp(arg0_ref, mirror::ObjectArray<mirror::Object>::
395 OffsetOfElement(target_method.dex_method_index).Int32Value(), arg0_ref,
396 kNotVolatile);
397 break;
398 case 3: // Grab the code from the method*
399 if (direct_code == 0) {
400 int32_t offset = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
401 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
402 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
403 cg->LoadWordDisp(arg0_ref, offset, cg->TargetPtrReg(kInvokeTgt));
404 }
405 break;
406 default:
407 return -1;
408 }
409 }
410 return state + 1;
411}
412
413NextCallInsn Mips64Mir2Lir::GetNextSDCallInsn() {
414 return Mips64NextSDCallInsn;
415}
416
417LIR* Mips64Mir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info ATTRIBUTE_UNUSED) {
418 return OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
419}
420
421} // namespace art