blob: a30e80a575a1aaa215f0617d0c1e99c1e14435a0 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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 Thumb2 ISA. */
18
19#include "arm_lir.h"
20#include "codegen_arm.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000021#include "dex/quick/dex_file_method_inliner.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070023#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
25namespace art {
26
Vladimir Marko502c2a82014-02-06 11:52:07 +000027// TODO: generalize & move to RegUtil.cc
28// The number of dalvik registers passed in core registers.
29constexpr int kInArgsInCoreRegs = 3;
30// The core register corresponding to the first (index 0) input argument.
31constexpr int kInArg0CoreReg = r1; // r0 is Method*.
32// Offset, in words, for getting args from stack (even core reg args have space on stack).
33constexpr int kInArgToStackOffset = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -070034
Vladimir Marko502c2a82014-02-06 11:52:07 +000035/* Lock argument if it's in register. */
36void ArmMir2Lir::LockArg(int in_position, bool wide) {
37 if (in_position < kInArgsInCoreRegs) {
38 LockTemp(kInArg0CoreReg + in_position);
39 }
40 if (wide && in_position + 1 < kInArgsInCoreRegs) {
41 LockTemp(kInArg0CoreReg + in_position + 1);
42 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070043}
44
Vladimir Marko502c2a82014-02-06 11:52:07 +000045/* Load argument into register. LockArg(in_position, wide) must have been previously called. */
46int ArmMir2Lir::LoadArg(int in_position, bool wide) {
47 if (in_position < kInArgsInCoreRegs) {
48 int low_reg = kInArg0CoreReg + in_position;
49 if (!wide) {
50 return low_reg;
51 }
52 int high_reg = (in_position != kInArgsInCoreRegs - 1) ? low_reg + 1 : LoadArg(in_position + 1);
53 return (low_reg & 0xff) | ((high_reg & 0xff) << 8);
54 }
55 int low_reg = AllocTemp();
56 int offset = (in_position + kInArgToStackOffset) * sizeof(uint32_t);
57 if (!wide) {
58 LoadWordDisp(rARM_SP, offset, low_reg);
59 return low_reg;
60 }
61 int high_reg = AllocTemp();
62 LoadBaseDispWide(rARM_SP, offset, low_reg, high_reg, INVALID_SREG);
63 return (low_reg & 0xff) | ((high_reg & 0xff) << 8);
64}
65
66void ArmMir2Lir::LoadArgDirect(int in_position, RegLocation rl_dest) {
67 int reg = kInArg0CoreReg + in_position;
68 int offset = (in_position + kInArgToStackOffset) * sizeof(uint32_t);
69 if (!rl_dest.wide) {
70 if (in_position < kInArgsInCoreRegs) {
71 OpRegCopy(rl_dest.low_reg, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 } else {
Vladimir Marko502c2a82014-02-06 11:52:07 +000073 LoadWordDisp(rARM_SP, offset, rl_dest.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 }
75 } else {
Vladimir Marko502c2a82014-02-06 11:52:07 +000076 if (in_position < kInArgsInCoreRegs - 1) {
77 OpRegCopyWide(rl_dest.low_reg, rl_dest.high_reg, reg, reg + 1);
78 } else if (in_position == kInArgsInCoreRegs - 1) {
79 OpRegCopy(rl_dest.low_reg, reg);
80 LoadWordDisp(rARM_SP, offset + sizeof(uint32_t), rl_dest.high_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 } else {
Vladimir Marko502c2a82014-02-06 11:52:07 +000082 LoadBaseDispWide(rARM_SP, offset, rl_dest.low_reg, rl_dest.high_reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 }
84 }
85}
86
87/* Find the next MIR, which may be in a following basic block */
buzbee0d829482013-10-11 15:24:55 -070088// TODO: make this a utility in mir_graph.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070089MIR* ArmMir2Lir::GetNextMir(BasicBlock** p_bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 BasicBlock* bb = *p_bb;
91 MIR* orig_mir = mir;
92 while (bb != NULL) {
93 if (mir != NULL) {
94 mir = mir->next;
95 }
96 if (mir != NULL) {
97 return mir;
98 } else {
buzbee0d829482013-10-11 15:24:55 -070099 bb = mir_graph_->GetBasicBlock(bb->fall_through);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100 *p_bb = bb;
101 if (bb) {
102 mir = bb->first_mir_insn;
103 if (mir != NULL) {
104 return mir;
105 }
106 }
107 }
108 }
109 return orig_mir;
110}
111
112/* Used for the "verbose" listing */
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700113// TODO: move to common code
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700114void ArmMir2Lir::GenPrintLabel(MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 /* Mark the beginning of a Dalvik instruction for line tracking */
buzbee252254b2013-09-08 16:20:53 -0700116 if (cu_->verbose) {
117 char* inst_str = mir_graph_->GetDalvikDisassembly(mir);
118 MarkBoundary(mir->offset, inst_str);
119 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120}
121
Vladimir Marko2bc47802014-02-10 09:43:07 +0000122MIR* ArmMir2Lir::SpecialIGet(BasicBlock** bb, MIR* mir, const InlineMethod& special) {
123 // FastInstance() already checked by DexFileMethodInliner.
124 const InlineIGetIPutData& data = special.d.ifield_data;
Vladimir Markoc9bf4072014-02-10 12:34:34 +0000125 if (data.method_is_static || data.object_arg != 0) {
Vladimir Marko2bc47802014-02-10 09:43:07 +0000126 return NULL; // The object is not "this" and has to be null-checked.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 }
Vladimir Marko2bc47802014-02-10 09:43:07 +0000128
Vladimir Marko2bc47802014-02-10 09:43:07 +0000129 DCHECK_NE(data.op_size, kDouble); // The inliner doesn't distinguish kDouble, uses kLong.
Vladimir Marko502c2a82014-02-06 11:52:07 +0000130 bool wide = (data.op_size == kLong);
Vladimir Marko2bc47802014-02-10 09:43:07 +0000131
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 // Point of no return - no aborts after this
133 ArmMir2Lir::GenPrintLabel(mir);
Vladimir Marko502c2a82014-02-06 11:52:07 +0000134 LockArg(data.object_arg);
135 RegLocation rl_dest = wide ? GetReturnWide(false) : GetReturn(false);
136 int reg_obj = LoadArg(data.object_arg);
137 if (wide) {
138 LoadBaseDispWide(reg_obj, data.field_offset, rl_dest.low_reg, rl_dest.high_reg, INVALID_SREG);
139 } else {
140 LoadBaseDisp(reg_obj, data.field_offset, rl_dest.low_reg, kWord, INVALID_SREG);
141 }
142 if (data.is_volatile) {
143 GenMemBarrier(kLoadLoad);
144 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 return GetNextMir(bb, mir);
146}
147
Vladimir Marko2bc47802014-02-10 09:43:07 +0000148MIR* ArmMir2Lir::SpecialIPut(BasicBlock** bb, MIR* mir, const InlineMethod& special) {
149 // FastInstance() already checked by DexFileMethodInliner.
150 const InlineIGetIPutData& data = special.d.ifield_data;
Vladimir Markoc9bf4072014-02-10 12:34:34 +0000151 if (data.method_is_static || data.object_arg != 0) {
Vladimir Marko2bc47802014-02-10 09:43:07 +0000152 return NULL; // The object is not "this" and has to be null-checked.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 }
Vladimir Marko2bc47802014-02-10 09:43:07 +0000154
Vladimir Marko2bc47802014-02-10 09:43:07 +0000155 DCHECK_NE(data.op_size, kDouble); // The inliner doesn't distinguish kDouble, uses kLong.
Vladimir Marko502c2a82014-02-06 11:52:07 +0000156 bool wide = (data.op_size == kLong);
Vladimir Marko2bc47802014-02-10 09:43:07 +0000157
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 // Point of no return - no aborts after this
159 ArmMir2Lir::GenPrintLabel(mir);
Vladimir Marko502c2a82014-02-06 11:52:07 +0000160 LockArg(data.object_arg);
161 LockArg(data.src_arg, wide);
162 int reg_obj = LoadArg(data.object_arg);
163 int reg_src = LoadArg(data.src_arg, wide);
164 if (data.is_volatile) {
165 GenMemBarrier(kStoreStore);
166 }
167 if (wide) {
168 StoreBaseDispWide(reg_obj, data.field_offset, reg_src & 0xff, reg_src >> 8);
169 } else {
170 StoreBaseDisp(reg_obj, data.field_offset, reg_src, kWord);
171 }
172 if (data.is_volatile) {
173 GenMemBarrier(kLoadLoad);
174 }
175 if (data.is_object) {
176 MarkGCCard(reg_src, reg_obj);
177 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 return GetNextMir(bb, mir);
179}
180
Vladimir Marko502c2a82014-02-06 11:52:07 +0000181MIR* ArmMir2Lir::SpecialIdentity(MIR* mir, const InlineMethod& special) {
182 const InlineReturnArgData& data = special.d.return_data;
183 DCHECK_NE(data.op_size, kDouble); // The inliner doesn't distinguish kDouble, uses kLong.
184 bool wide = (data.op_size == kLong);
185
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 // Point of no return - no aborts after this
187 ArmMir2Lir::GenPrintLabel(mir);
Vladimir Marko502c2a82014-02-06 11:52:07 +0000188 LockArg(data.arg, wide);
189 RegLocation rl_dest = wide ? GetReturnWide(false) : GetReturn(false);
190 LoadArgDirect(data.arg, rl_dest);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 return mir;
192}
193
194/*
195 * Special-case code genration for simple non-throwing leaf methods.
196 */
197void ArmMir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir,
Vladimir Marko5816ed42013-11-27 17:04:20 +0000198 const InlineMethod& special) {
Vladimir Marko5816ed42013-11-27 17:04:20 +0000199 DCHECK(special.flags & kInlineSpecial);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700200 current_dalvik_offset_ = mir->offset;
201 MIR* next_mir = NULL;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000202 switch (special.opcode) {
203 case kInlineOpNop:
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700204 DCHECK(mir->dalvikInsn.opcode == Instruction::RETURN_VOID);
205 next_mir = mir;
206 break;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000207 case kInlineOpConst:
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700208 ArmMir2Lir::GenPrintLabel(mir);
Vladimir Marko2bc47802014-02-10 09:43:07 +0000209 LoadConstant(rARM_RET0, static_cast<int>(special.d.data));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700210 next_mir = GetNextMir(&bb, mir);
211 break;
Vladimir Marko2bc47802014-02-10 09:43:07 +0000212 case kInlineOpIGet:
213 next_mir = SpecialIGet(&bb, mir, special);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700214 break;
Vladimir Marko2bc47802014-02-10 09:43:07 +0000215 case kInlineOpIPut:
216 next_mir = SpecialIPut(&bb, mir, special);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700217 break;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000218 case kInlineOpReturnArg:
Vladimir Marko502c2a82014-02-06 11:52:07 +0000219 next_mir = SpecialIdentity(mir, special);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700220 break;
221 default:
222 return;
223 }
224 if (next_mir != NULL) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 current_dalvik_offset_ = next_mir->offset;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000226 if (special.opcode != kInlineOpReturnArg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 ArmMir2Lir::GenPrintLabel(next_mir);
228 }
229 NewLIR1(kThumbBx, rARM_LR);
230 core_spill_mask_ = 0;
231 num_core_spills_ = 0;
232 fp_spill_mask_ = 0;
233 num_fp_spills_ = 0;
234 frame_size_ = 0;
235 core_vmap_table_.clear();
236 fp_vmap_table_.clear();
237 }
238}
239
240/*
241 * The sparse table in the literal pool is an array of <key,displacement>
242 * pairs. For each set, we'll load them as a pair using ldmia.
243 * This means that the register number of the temp we use for the key
244 * must be lower than the reg for the displacement.
245 *
246 * The test loop will look something like:
247 *
248 * adr rBase, <table>
249 * ldr r_val, [rARM_SP, v_reg_off]
250 * mov r_idx, #table_size
251 * lp:
252 * ldmia rBase!, {r_key, r_disp}
253 * sub r_idx, #1
254 * cmp r_val, r_key
255 * ifeq
256 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
257 * cbnz r_idx, lp
258 */
259void ArmMir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700260 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
262 if (cu_->verbose) {
263 DumpSparseSwitchTable(table);
264 }
265 // Add the table to the list - we'll process it later
266 SwitchTable *tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700267 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 tab_rec->table = table;
269 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700270 uint32_t size = table[1];
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700271 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*),
buzbee0d829482013-10-11 15:24:55 -0700272 ArenaAllocator::kAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 switch_tables_.Insert(tab_rec);
274
275 // Get the switch value
276 rl_src = LoadValue(rl_src, kCoreReg);
277 int rBase = AllocTemp();
278 /* Allocate key and disp temps */
279 int r_key = AllocTemp();
280 int r_disp = AllocTemp();
281 // Make sure r_key's register number is less than r_disp's number for ldmia
282 if (r_key > r_disp) {
283 int tmp = r_disp;
284 r_disp = r_key;
285 r_key = tmp;
286 }
287 // Materialize a pointer to the switch table
buzbee0d829482013-10-11 15:24:55 -0700288 NewLIR3(kThumb2Adr, rBase, 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 // Set up r_idx
290 int r_idx = AllocTemp();
291 LoadConstant(r_idx, size);
292 // Establish loop branch target
293 LIR* target = NewLIR0(kPseudoTargetLabel);
294 // Load next key/disp
295 NewLIR2(kThumb2LdmiaWB, rBase, (1 << r_key) | (1 << r_disp));
296 OpRegReg(kOpCmp, r_key, rl_src.low_reg);
297 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
298 OpIT(kCondEq, "");
299 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp);
300 tab_rec->anchor = switch_branch;
301 // Needs to use setflags encoding here
302 NewLIR3(kThumb2SubsRRI12, r_idx, r_idx, 1);
303 OpCondBranch(kCondNe, target);
304}
305
306
307void ArmMir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700308 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
310 if (cu_->verbose) {
311 DumpPackedSwitchTable(table);
312 }
313 // Add the table to the list - we'll process it later
314 SwitchTable *tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700315 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 tab_rec->table = table;
317 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700318 uint32_t size = table[1];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 tab_rec->targets =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700320 static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), ArenaAllocator::kAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 switch_tables_.Insert(tab_rec);
322
323 // Get the switch value
324 rl_src = LoadValue(rl_src, kCoreReg);
325 int table_base = AllocTemp();
326 // Materialize a pointer to the switch table
buzbee0d829482013-10-11 15:24:55 -0700327 NewLIR3(kThumb2Adr, table_base, 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 int low_key = s4FromSwitchData(&table[2]);
329 int keyReg;
330 // Remove the bias, if necessary
331 if (low_key == 0) {
332 keyReg = rl_src.low_reg;
333 } else {
334 keyReg = AllocTemp();
335 OpRegRegImm(kOpSub, keyReg, rl_src.low_reg, low_key);
336 }
337 // Bounds check - if < 0 or >= size continue following switch
338 OpRegImm(kOpCmp, keyReg, size-1);
339 LIR* branch_over = OpCondBranch(kCondHi, NULL);
340
341 // Load the displacement from the switch table
342 int disp_reg = AllocTemp();
343 LoadBaseIndexed(table_base, keyReg, disp_reg, 2, kWord);
344
345 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
346 LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg);
347 tab_rec->anchor = switch_branch;
348
349 /* branch_over target here */
350 LIR* target = NewLIR0(kPseudoTargetLabel);
351 branch_over->target = target;
352}
353
354/*
355 * Array data table format:
356 * ushort ident = 0x0300 magic value
357 * ushort width width of each element in the table
358 * uint size number of elements in the table
359 * ubyte data[size*width] table of data values (may contain a single-byte
360 * padding at the end)
361 *
362 * Total size is 4+(width * size + 1)/2 16-bit code units.
363 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700364void ArmMir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
366 // Add the table to the list - we'll process it later
367 FillArrayData *tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700368 static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 tab_rec->table = table;
370 tab_rec->vaddr = current_dalvik_offset_;
371 uint16_t width = tab_rec->table[1];
372 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
373 tab_rec->size = (size * width) + 8;
374
375 fill_array_data_.Insert(tab_rec);
376
377 // Making a call - use explicit registers
378 FlushAllRegs(); /* Everything to home location */
379 LoadValueDirectFixed(rl_src, r0);
Ian Rogers468532e2013-08-05 10:56:33 -0700380 LoadWordDisp(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pHandleFillArrayData).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 rARM_LR);
382 // Materialize a pointer to the fill data image
buzbee0d829482013-10-11 15:24:55 -0700383 NewLIR3(kThumb2Adr, r1, 0, WrapPointer(tab_rec));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000384 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 LIR* call_inst = OpReg(kOpBlx, rARM_LR);
386 MarkSafepointPC(call_inst);
387}
388
389/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700390 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
391 * details see monitor.cc.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700393void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 FlushAllRegs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 LoadValueDirectFixed(rl_src, r0); // Get obj
396 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700397 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
398 if (kArchVariantHasGoodBranchPredictor) {
399 LIR* null_check_branch;
400 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
401 null_check_branch = nullptr; // No null check.
402 } else {
403 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
404 null_check_branch = OpCmpImmBranch(kCondEq, r0, 0, NULL);
405 }
406 LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
407 NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
408 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, r1, 0, NULL);
409 NewLIR4(kThumb2Strex, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
410 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, r1, 0, NULL);
411
412
413 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
414 not_unlocked_branch->target = slow_path_target;
415 if (null_check_branch != nullptr) {
416 null_check_branch->target = slow_path_target;
417 }
418 // TODO: move to a slow path.
419 // Go expensive route - artLockObjectFromCode(obj);
420 LoadWordDisp(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pLockObject).Int32Value(), rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000421 ClobberCallerSave();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700422 LIR* call_inst = OpReg(kOpBlx, rARM_LR);
423 MarkSafepointPC(call_inst);
424
425 LIR* success_target = NewLIR0(kPseudoTargetLabel);
426 lock_success_branch->target = success_target;
427 GenMemBarrier(kLoadLoad);
428 } else {
429 // Explicit null-check as slow-path is entered using an IT.
430 GenNullCheck(rl_src.s_reg_low, r0, opt_flags);
431 LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
432 NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
433 OpRegImm(kOpCmp, r1, 0);
434 OpIT(kCondEq, "");
435 NewLIR4(kThumb2Strex/*eq*/, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
436 OpRegImm(kOpCmp, r1, 0);
437 OpIT(kCondNe, "T");
438 // Go expensive route - artLockObjectFromCode(self, obj);
439 LoadWordDisp/*ne*/(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pLockObject).Int32Value(), rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000440 ClobberCallerSave();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700441 LIR* call_inst = OpReg(kOpBlx/*ne*/, rARM_LR);
442 MarkSafepointPC(call_inst);
443 GenMemBarrier(kLoadLoad);
444 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445}
446
447/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700448 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
449 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
450 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700452void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 FlushAllRegs();
454 LoadValueDirectFixed(rl_src, r0); // Get obj
455 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700456 LIR* null_check_branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700458 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
459 if (kArchVariantHasGoodBranchPredictor) {
460 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
461 null_check_branch = nullptr; // No null check.
462 } else {
463 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
464 null_check_branch = OpCmpImmBranch(kCondEq, r0, 0, NULL);
465 }
466 LoadWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r1);
467 LoadConstantNoClobber(r3, 0);
468 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, r1, r2, NULL);
469 StoreWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r3);
470 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
471
472 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
473 slow_unlock_branch->target = slow_path_target;
474 if (null_check_branch != nullptr) {
475 null_check_branch->target = slow_path_target;
476 }
477 // TODO: move to a slow path.
478 // Go expensive route - artUnlockObjectFromCode(obj);
479 LoadWordDisp(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pUnlockObject).Int32Value(), rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000480 ClobberCallerSave();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700481 LIR* call_inst = OpReg(kOpBlx, rARM_LR);
482 MarkSafepointPC(call_inst);
483
484 LIR* success_target = NewLIR0(kPseudoTargetLabel);
485 unlock_success_branch->target = success_target;
486 GenMemBarrier(kStoreLoad);
487 } else {
488 // Explicit null-check as slow-path is entered using an IT.
489 GenNullCheck(rl_src.s_reg_low, r0, opt_flags);
490 LoadWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r1); // Get lock
491 LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
492 LoadConstantNoClobber(r3, 0);
493 // Is lock unheld on lock or held by us (==thread_id) on unlock?
494 OpRegReg(kOpCmp, r1, r2);
495 OpIT(kCondEq, "EE");
496 StoreWordDisp/*eq*/(r0, mirror::Object::MonitorOffset().Int32Value(), r3);
497 // Go expensive route - UnlockObjectFromCode(obj);
498 LoadWordDisp/*ne*/(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pUnlockObject).Int32Value(), rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000499 ClobberCallerSave();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700500 LIR* call_inst = OpReg(kOpBlx/*ne*/, rARM_LR);
501 MarkSafepointPC(call_inst);
502 GenMemBarrier(kStoreLoad);
503 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504}
505
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700506void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 int ex_offset = Thread::ExceptionOffset().Int32Value();
508 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
509 int reset_reg = AllocTemp();
510 LoadWordDisp(rARM_SELF, ex_offset, rl_result.low_reg);
511 LoadConstant(reset_reg, 0);
512 StoreWordDisp(rARM_SELF, ex_offset, reset_reg);
513 FreeTemp(reset_reg);
514 StoreValue(rl_dest, rl_result);
515}
516
517/*
518 * Mark garbage collection card. Skip if the value we're storing is null.
519 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700520void ArmMir2Lir::MarkGCCard(int val_reg, int tgt_addr_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 int reg_card_base = AllocTemp();
522 int reg_card_no = AllocTemp();
523 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
524 LoadWordDisp(rARM_SELF, Thread::CardTableOffset().Int32Value(), reg_card_base);
525 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
526 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0,
527 kUnsignedByte);
528 LIR* target = NewLIR0(kPseudoTargetLabel);
529 branch_over->target = target;
530 FreeTemp(reg_card_base);
531 FreeTemp(reg_card_no);
532}
533
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700534void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 int spill_count = num_core_spills_ + num_fp_spills_;
536 /*
537 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
538 * mechanism know so it doesn't try to use any of them when
539 * expanding the frame or flushing. This leaves the utility
540 * code with a single temp: r12. This should be enough.
541 */
542 LockTemp(r0);
543 LockTemp(r1);
544 LockTemp(r2);
545 LockTemp(r3);
546
547 /*
548 * We can safely skip the stack overflow check if we're
549 * a leaf *and* our frame size < fudge factor.
550 */
551 bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
552 (static_cast<size_t>(frame_size_) <
553 Thread::kStackOverflowReservedBytes));
554 NewLIR0(kPseudoMethodEntry);
555 if (!skip_overflow_check) {
556 /* Load stack limit */
557 LoadWordDisp(rARM_SELF, Thread::StackEndOffset().Int32Value(), r12);
558 }
559 /* Spill core callee saves */
560 NewLIR1(kThumb2Push, core_spill_mask_);
561 /* Need to spill any FP regs? */
562 if (num_fp_spills_) {
563 /*
564 * NOTE: fp spills are a little different from core spills in that
565 * they are pushed as a contiguous block. When promoting from
566 * the fp set, we must allocate all singles from s16..highest-promoted
567 */
568 NewLIR1(kThumb2VPushCS, num_fp_spills_);
569 }
570 if (!skip_overflow_check) {
571 OpRegRegImm(kOpSub, rARM_LR, rARM_SP, frame_size_ - (spill_count * 4));
Vladimir Marko58af1f92013-12-19 13:31:15 +0000572 GenRegRegCheck(kCondUlt, rARM_LR, r12, kThrowStackOverflow);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 OpRegCopy(rARM_SP, rARM_LR); // Establish stack
574 } else {
575 OpRegImm(kOpSub, rARM_SP, frame_size_ - (spill_count * 4));
576 }
577
578 FlushIns(ArgLocs, rl_method);
579
580 FreeTemp(r0);
581 FreeTemp(r1);
582 FreeTemp(r2);
583 FreeTemp(r3);
584}
585
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700586void ArmMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 int spill_count = num_core_spills_ + num_fp_spills_;
588 /*
589 * In the exit path, r0/r1 are live - make sure they aren't
590 * allocated by the register utilities as temps.
591 */
592 LockTemp(r0);
593 LockTemp(r1);
594
595 NewLIR0(kPseudoMethodExit);
596 OpRegImm(kOpAdd, rARM_SP, frame_size_ - (spill_count * 4));
597 /* Need to restore any FP callee saves? */
598 if (num_fp_spills_) {
599 NewLIR1(kThumb2VPopCS, num_fp_spills_);
600 }
601 if (core_spill_mask_ & (1 << rARM_LR)) {
602 /* Unspill rARM_LR to rARM_PC */
603 core_spill_mask_ &= ~(1 << rARM_LR);
604 core_spill_mask_ |= (1 << rARM_PC);
605 }
606 NewLIR1(kThumb2Pop, core_spill_mask_);
607 if (!(core_spill_mask_ & (1 << rARM_PC))) {
608 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
609 NewLIR1(kThumbBx, rARM_LR);
610 }
611}
612
613} // namespace art