blob: 22466f08cf798832dbf7729997f0fcf3f5bc49da [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
27
28/* Return the position of an ssa name within the argument list */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070029int ArmMir2Lir::InPosition(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070030 int v_reg = mir_graph_->SRegToVReg(s_reg);
31 return v_reg - cu_->num_regs;
32}
33
34/*
35 * Describe an argument. If it's already in an arg register, just leave it
36 * there. NOTE: all live arg registers must be locked prior to this call
37 * to avoid having them allocated as a temp by downstream utilities.
38 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070039RegLocation ArmMir2Lir::ArgLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 int arg_num = InPosition(loc.s_reg_low);
41 if (loc.wide) {
42 if (arg_num == 2) {
43 // Bad case - half in register, half in frame. Just punt
44 loc.location = kLocInvalid;
45 } else if (arg_num < 2) {
46 loc.low_reg = rARM_ARG1 + arg_num;
47 loc.high_reg = loc.low_reg + 1;
48 loc.location = kLocPhysReg;
49 } else {
50 loc.location = kLocDalvikFrame;
51 }
52 } else {
53 if (arg_num < 3) {
54 loc.low_reg = rARM_ARG1 + arg_num;
55 loc.location = kLocPhysReg;
56 } else {
57 loc.location = kLocDalvikFrame;
58 }
59 }
60 return loc;
61}
62
63/*
64 * Load an argument. If already in a register, just return. If in
65 * the frame, we can't use the normal LoadValue() because it assumed
66 * a proper frame - and we're frameless.
67 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070068RegLocation ArmMir2Lir::LoadArg(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 if (loc.location == kLocDalvikFrame) {
70 int start = (InPosition(loc.s_reg_low) + 1) * sizeof(uint32_t);
71 loc.low_reg = AllocTemp();
72 LoadWordDisp(rARM_SP, start, loc.low_reg);
73 if (loc.wide) {
74 loc.high_reg = AllocTemp();
75 LoadWordDisp(rARM_SP, start + sizeof(uint32_t), loc.high_reg);
76 }
77 loc.location = kLocPhysReg;
78 }
79 return loc;
80}
81
82/* Lock any referenced arguments that arrive in registers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070083void ArmMir2Lir::LockLiveArgs(MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 int first_in = cu_->num_regs;
85 const int num_arg_regs = 3; // TODO: generalize & move to RegUtil.cc
86 for (int i = 0; i < mir->ssa_rep->num_uses; i++) {
87 int v_reg = mir_graph_->SRegToVReg(mir->ssa_rep->uses[i]);
88 int InPosition = v_reg - first_in;
89 if (InPosition < num_arg_regs) {
90 LockTemp(rARM_ARG1 + InPosition);
91 }
92 }
93}
94
95/* Find the next MIR, which may be in a following basic block */
buzbee0d829482013-10-11 15:24:55 -070096// TODO: make this a utility in mir_graph.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070097MIR* ArmMir2Lir::GetNextMir(BasicBlock** p_bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 BasicBlock* bb = *p_bb;
99 MIR* orig_mir = mir;
100 while (bb != NULL) {
101 if (mir != NULL) {
102 mir = mir->next;
103 }
104 if (mir != NULL) {
105 return mir;
106 } else {
buzbee0d829482013-10-11 15:24:55 -0700107 bb = mir_graph_->GetBasicBlock(bb->fall_through);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 *p_bb = bb;
109 if (bb) {
110 mir = bb->first_mir_insn;
111 if (mir != NULL) {
112 return mir;
113 }
114 }
115 }
116 }
117 return orig_mir;
118}
119
120/* Used for the "verbose" listing */
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700121// TODO: move to common code
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700122void ArmMir2Lir::GenPrintLabel(MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 /* Mark the beginning of a Dalvik instruction for line tracking */
buzbee252254b2013-09-08 16:20:53 -0700124 if (cu_->verbose) {
125 char* inst_str = mir_graph_->GetDalvikDisassembly(mir);
126 MarkBoundary(mir->offset, inst_str);
127 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128}
129
Vladimir Marko5dc57272014-02-05 14:53:36 +0000130MIR* ArmMir2Lir::SpecialIGet(BasicBlock** bb, MIR* mir, const InlineMethod& special) {
131 // FastInstance() already checked by DexFileMethodInliner.
132 const InlineIGetIPutData& data = special.d.ifield_data;
133 if (!data.method_is_static || data.object_arg != 0) {
134 return NULL; // The object is not "this" and has to be null-checked.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 }
Vladimir Marko5dc57272014-02-05 14:53:36 +0000136
137 OpSize size = static_cast<OpSize>(data.op_size);
138 DCHECK_NE(data.op_size, kDouble); // The inliner doesn't distinguish kDouble, uses kLong.
139 bool long_or_double = (data.op_size == kLong);
140 bool is_object = data.is_object;
141
142 // TODO: Generate the method using only the data in special.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 RegLocation rl_obj = mir_graph_->GetSrc(mir, 0);
144 LockLiveArgs(mir);
145 rl_obj = ArmMir2Lir::ArgLoc(rl_obj);
146 RegLocation rl_dest;
147 if (long_or_double) {
148 rl_dest = GetReturnWide(false);
149 } else {
150 rl_dest = GetReturn(false);
151 }
152 // Point of no return - no aborts after this
153 ArmMir2Lir::GenPrintLabel(mir);
154 rl_obj = LoadArg(rl_obj);
Vladimir Marko5dc57272014-02-05 14:53:36 +0000155 uint32_t field_idx = mir->dalvikInsn.vC;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 GenIGet(field_idx, mir->optimization_flags, size, rl_dest, rl_obj, long_or_double, is_object);
157 return GetNextMir(bb, mir);
158}
159
Vladimir Marko5dc57272014-02-05 14:53:36 +0000160MIR* ArmMir2Lir::SpecialIPut(BasicBlock** bb, MIR* mir, const InlineMethod& special) {
161 // FastInstance() already checked by DexFileMethodInliner.
162 const InlineIGetIPutData& data = special.d.ifield_data;
163 if (!data.method_is_static || data.object_arg != 0) {
164 return NULL; // The object is not "this" and has to be null-checked.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 }
Vladimir Marko5dc57272014-02-05 14:53:36 +0000166
167 OpSize size = static_cast<OpSize>(data.op_size);
168 DCHECK_NE(data.op_size, kDouble); // The inliner doesn't distinguish kDouble, uses kLong.
169 bool long_or_double = (data.op_size == kLong);
170 bool is_object = data.is_object;
171
172 // TODO: Generate the method using only the data in special.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 RegLocation rl_src;
174 RegLocation rl_obj;
175 LockLiveArgs(mir);
176 if (long_or_double) {
177 rl_src = mir_graph_->GetSrcWide(mir, 0);
178 rl_obj = mir_graph_->GetSrc(mir, 2);
179 } else {
180 rl_src = mir_graph_->GetSrc(mir, 0);
181 rl_obj = mir_graph_->GetSrc(mir, 1);
182 }
183 rl_src = ArmMir2Lir::ArgLoc(rl_src);
184 rl_obj = ArmMir2Lir::ArgLoc(rl_obj);
185 // Reject if source is split across registers & frame
Vladimir Marko5dc57272014-02-05 14:53:36 +0000186 if (rl_src.location == kLocInvalid) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187 ResetRegPool();
188 return NULL;
189 }
190 // Point of no return - no aborts after this
191 ArmMir2Lir::GenPrintLabel(mir);
192 rl_obj = LoadArg(rl_obj);
193 rl_src = LoadArg(rl_src);
Vladimir Marko5dc57272014-02-05 14:53:36 +0000194 uint32_t field_idx = mir->dalvikInsn.vC;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195 GenIPut(field_idx, mir->optimization_flags, size, rl_src, rl_obj, long_or_double, is_object);
196 return GetNextMir(bb, mir);
197}
198
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700199MIR* ArmMir2Lir::SpecialIdentity(MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 RegLocation rl_src;
201 RegLocation rl_dest;
202 bool wide = (mir->ssa_rep->num_uses == 2);
203 if (wide) {
204 rl_src = mir_graph_->GetSrcWide(mir, 0);
205 rl_dest = GetReturnWide(false);
206 } else {
207 rl_src = mir_graph_->GetSrc(mir, 0);
208 rl_dest = GetReturn(false);
209 }
210 LockLiveArgs(mir);
211 rl_src = ArmMir2Lir::ArgLoc(rl_src);
212 if (rl_src.location == kLocInvalid) {
213 ResetRegPool();
214 return NULL;
215 }
216 // Point of no return - no aborts after this
217 ArmMir2Lir::GenPrintLabel(mir);
218 rl_src = LoadArg(rl_src);
219 if (wide) {
220 StoreValueWide(rl_dest, rl_src);
221 } else {
222 StoreValue(rl_dest, rl_src);
223 }
224 return mir;
225}
226
227/*
228 * Special-case code genration for simple non-throwing leaf methods.
229 */
230void ArmMir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir,
Vladimir Marko5816ed42013-11-27 17:04:20 +0000231 const InlineMethod& special) {
Vladimir Marko5816ed42013-11-27 17:04:20 +0000232 DCHECK(special.flags & kInlineSpecial);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700233 current_dalvik_offset_ = mir->offset;
234 MIR* next_mir = NULL;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000235 switch (special.opcode) {
236 case kInlineOpNop:
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700237 DCHECK(mir->dalvikInsn.opcode == Instruction::RETURN_VOID);
238 next_mir = mir;
239 break;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000240 case kInlineOpConst:
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700241 ArmMir2Lir::GenPrintLabel(mir);
Vladimir Marko5dc57272014-02-05 14:53:36 +0000242 LoadConstant(rARM_RET0, static_cast<int>(special.d.data));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700243 next_mir = GetNextMir(&bb, mir);
244 break;
Vladimir Marko5dc57272014-02-05 14:53:36 +0000245 case kInlineOpIGet:
246 next_mir = SpecialIGet(&bb, mir, special);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700247 break;
Vladimir Marko5dc57272014-02-05 14:53:36 +0000248 case kInlineOpIPut:
249 next_mir = SpecialIPut(&bb, mir, special);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700250 break;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000251 case kInlineOpReturnArg:
Vladimir Marko5dc57272014-02-05 14:53:36 +0000252 // TODO: Generate the method using only the data in special.
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700253 next_mir = SpecialIdentity(mir);
254 break;
255 default:
256 return;
257 }
258 if (next_mir != NULL) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 current_dalvik_offset_ = next_mir->offset;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000260 if (special.opcode != kInlineOpReturnArg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 ArmMir2Lir::GenPrintLabel(next_mir);
262 }
263 NewLIR1(kThumbBx, rARM_LR);
264 core_spill_mask_ = 0;
265 num_core_spills_ = 0;
266 fp_spill_mask_ = 0;
267 num_fp_spills_ = 0;
268 frame_size_ = 0;
269 core_vmap_table_.clear();
270 fp_vmap_table_.clear();
271 }
272}
273
274/*
275 * The sparse table in the literal pool is an array of <key,displacement>
276 * pairs. For each set, we'll load them as a pair using ldmia.
277 * This means that the register number of the temp we use for the key
278 * must be lower than the reg for the displacement.
279 *
280 * The test loop will look something like:
281 *
282 * adr rBase, <table>
283 * ldr r_val, [rARM_SP, v_reg_off]
284 * mov r_idx, #table_size
285 * lp:
286 * ldmia rBase!, {r_key, r_disp}
287 * sub r_idx, #1
288 * cmp r_val, r_key
289 * ifeq
290 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
291 * cbnz r_idx, lp
292 */
293void ArmMir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700294 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
296 if (cu_->verbose) {
297 DumpSparseSwitchTable(table);
298 }
299 // Add the table to the list - we'll process it later
300 SwitchTable *tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700301 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 tab_rec->table = table;
303 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700304 uint32_t size = table[1];
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700305 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*),
buzbee0d829482013-10-11 15:24:55 -0700306 ArenaAllocator::kAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 switch_tables_.Insert(tab_rec);
308
309 // Get the switch value
310 rl_src = LoadValue(rl_src, kCoreReg);
311 int rBase = AllocTemp();
312 /* Allocate key and disp temps */
313 int r_key = AllocTemp();
314 int r_disp = AllocTemp();
315 // Make sure r_key's register number is less than r_disp's number for ldmia
316 if (r_key > r_disp) {
317 int tmp = r_disp;
318 r_disp = r_key;
319 r_key = tmp;
320 }
321 // Materialize a pointer to the switch table
buzbee0d829482013-10-11 15:24:55 -0700322 NewLIR3(kThumb2Adr, rBase, 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 // Set up r_idx
324 int r_idx = AllocTemp();
325 LoadConstant(r_idx, size);
326 // Establish loop branch target
327 LIR* target = NewLIR0(kPseudoTargetLabel);
328 // Load next key/disp
329 NewLIR2(kThumb2LdmiaWB, rBase, (1 << r_key) | (1 << r_disp));
330 OpRegReg(kOpCmp, r_key, rl_src.low_reg);
331 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
332 OpIT(kCondEq, "");
333 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp);
334 tab_rec->anchor = switch_branch;
335 // Needs to use setflags encoding here
336 NewLIR3(kThumb2SubsRRI12, r_idx, r_idx, 1);
337 OpCondBranch(kCondNe, target);
338}
339
340
341void ArmMir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700342 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
344 if (cu_->verbose) {
345 DumpPackedSwitchTable(table);
346 }
347 // Add the table to the list - we'll process it later
348 SwitchTable *tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700349 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 tab_rec->table = table;
351 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700352 uint32_t size = table[1];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 tab_rec->targets =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700354 static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), ArenaAllocator::kAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 switch_tables_.Insert(tab_rec);
356
357 // Get the switch value
358 rl_src = LoadValue(rl_src, kCoreReg);
359 int table_base = AllocTemp();
360 // Materialize a pointer to the switch table
buzbee0d829482013-10-11 15:24:55 -0700361 NewLIR3(kThumb2Adr, table_base, 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 int low_key = s4FromSwitchData(&table[2]);
363 int keyReg;
364 // Remove the bias, if necessary
365 if (low_key == 0) {
366 keyReg = rl_src.low_reg;
367 } else {
368 keyReg = AllocTemp();
369 OpRegRegImm(kOpSub, keyReg, rl_src.low_reg, low_key);
370 }
371 // Bounds check - if < 0 or >= size continue following switch
372 OpRegImm(kOpCmp, keyReg, size-1);
373 LIR* branch_over = OpCondBranch(kCondHi, NULL);
374
375 // Load the displacement from the switch table
376 int disp_reg = AllocTemp();
377 LoadBaseIndexed(table_base, keyReg, disp_reg, 2, kWord);
378
379 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
380 LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg);
381 tab_rec->anchor = switch_branch;
382
383 /* branch_over target here */
384 LIR* target = NewLIR0(kPseudoTargetLabel);
385 branch_over->target = target;
386}
387
388/*
389 * Array data table format:
390 * ushort ident = 0x0300 magic value
391 * ushort width width of each element in the table
392 * uint size number of elements in the table
393 * ubyte data[size*width] table of data values (may contain a single-byte
394 * padding at the end)
395 *
396 * Total size is 4+(width * size + 1)/2 16-bit code units.
397 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700398void ArmMir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
400 // Add the table to the list - we'll process it later
401 FillArrayData *tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700402 static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 tab_rec->table = table;
404 tab_rec->vaddr = current_dalvik_offset_;
405 uint16_t width = tab_rec->table[1];
406 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
407 tab_rec->size = (size * width) + 8;
408
409 fill_array_data_.Insert(tab_rec);
410
411 // Making a call - use explicit registers
412 FlushAllRegs(); /* Everything to home location */
413 LoadValueDirectFixed(rl_src, r0);
Ian Rogers468532e2013-08-05 10:56:33 -0700414 LoadWordDisp(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pHandleFillArrayData).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 rARM_LR);
416 // Materialize a pointer to the fill data image
buzbee0d829482013-10-11 15:24:55 -0700417 NewLIR3(kThumb2Adr, r1, 0, WrapPointer(tab_rec));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000418 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 LIR* call_inst = OpReg(kOpBlx, rARM_LR);
420 MarkSafepointPC(call_inst);
421}
422
423/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700424 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
425 * details see monitor.cc.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700427void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 FlushAllRegs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 LoadValueDirectFixed(rl_src, r0); // Get obj
430 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700431 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
432 if (kArchVariantHasGoodBranchPredictor) {
433 LIR* null_check_branch;
434 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
435 null_check_branch = nullptr; // No null check.
436 } else {
437 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
438 null_check_branch = OpCmpImmBranch(kCondEq, r0, 0, NULL);
439 }
440 LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
441 NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
442 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, r1, 0, NULL);
443 NewLIR4(kThumb2Strex, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
444 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, r1, 0, NULL);
445
446
447 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
448 not_unlocked_branch->target = slow_path_target;
449 if (null_check_branch != nullptr) {
450 null_check_branch->target = slow_path_target;
451 }
452 // TODO: move to a slow path.
453 // Go expensive route - artLockObjectFromCode(obj);
454 LoadWordDisp(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pLockObject).Int32Value(), rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000455 ClobberCallerSave();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700456 LIR* call_inst = OpReg(kOpBlx, rARM_LR);
457 MarkSafepointPC(call_inst);
458
459 LIR* success_target = NewLIR0(kPseudoTargetLabel);
460 lock_success_branch->target = success_target;
461 GenMemBarrier(kLoadLoad);
462 } else {
463 // Explicit null-check as slow-path is entered using an IT.
464 GenNullCheck(rl_src.s_reg_low, r0, opt_flags);
465 LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
466 NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
467 OpRegImm(kOpCmp, r1, 0);
468 OpIT(kCondEq, "");
469 NewLIR4(kThumb2Strex/*eq*/, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
470 OpRegImm(kOpCmp, r1, 0);
471 OpIT(kCondNe, "T");
472 // Go expensive route - artLockObjectFromCode(self, obj);
473 LoadWordDisp/*ne*/(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pLockObject).Int32Value(), rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000474 ClobberCallerSave();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700475 LIR* call_inst = OpReg(kOpBlx/*ne*/, rARM_LR);
476 MarkSafepointPC(call_inst);
477 GenMemBarrier(kLoadLoad);
478 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479}
480
481/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700482 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
483 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
484 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700486void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 FlushAllRegs();
488 LoadValueDirectFixed(rl_src, r0); // Get obj
489 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700490 LIR* null_check_branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700492 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
493 if (kArchVariantHasGoodBranchPredictor) {
494 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
495 null_check_branch = nullptr; // No null check.
496 } else {
497 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
498 null_check_branch = OpCmpImmBranch(kCondEq, r0, 0, NULL);
499 }
500 LoadWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r1);
501 LoadConstantNoClobber(r3, 0);
502 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, r1, r2, NULL);
503 StoreWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r3);
504 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
505
506 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
507 slow_unlock_branch->target = slow_path_target;
508 if (null_check_branch != nullptr) {
509 null_check_branch->target = slow_path_target;
510 }
511 // TODO: move to a slow path.
512 // Go expensive route - artUnlockObjectFromCode(obj);
513 LoadWordDisp(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pUnlockObject).Int32Value(), rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000514 ClobberCallerSave();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700515 LIR* call_inst = OpReg(kOpBlx, rARM_LR);
516 MarkSafepointPC(call_inst);
517
518 LIR* success_target = NewLIR0(kPseudoTargetLabel);
519 unlock_success_branch->target = success_target;
520 GenMemBarrier(kStoreLoad);
521 } else {
522 // Explicit null-check as slow-path is entered using an IT.
523 GenNullCheck(rl_src.s_reg_low, r0, opt_flags);
524 LoadWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r1); // Get lock
525 LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
526 LoadConstantNoClobber(r3, 0);
527 // Is lock unheld on lock or held by us (==thread_id) on unlock?
528 OpRegReg(kOpCmp, r1, r2);
529 OpIT(kCondEq, "EE");
530 StoreWordDisp/*eq*/(r0, mirror::Object::MonitorOffset().Int32Value(), r3);
531 // Go expensive route - UnlockObjectFromCode(obj);
532 LoadWordDisp/*ne*/(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pUnlockObject).Int32Value(), rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000533 ClobberCallerSave();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700534 LIR* call_inst = OpReg(kOpBlx/*ne*/, rARM_LR);
535 MarkSafepointPC(call_inst);
536 GenMemBarrier(kStoreLoad);
537 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538}
539
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700540void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 int ex_offset = Thread::ExceptionOffset().Int32Value();
542 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
543 int reset_reg = AllocTemp();
544 LoadWordDisp(rARM_SELF, ex_offset, rl_result.low_reg);
545 LoadConstant(reset_reg, 0);
546 StoreWordDisp(rARM_SELF, ex_offset, reset_reg);
547 FreeTemp(reset_reg);
548 StoreValue(rl_dest, rl_result);
549}
550
551/*
552 * Mark garbage collection card. Skip if the value we're storing is null.
553 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700554void ArmMir2Lir::MarkGCCard(int val_reg, int tgt_addr_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 int reg_card_base = AllocTemp();
556 int reg_card_no = AllocTemp();
557 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
558 LoadWordDisp(rARM_SELF, Thread::CardTableOffset().Int32Value(), reg_card_base);
559 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
560 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0,
561 kUnsignedByte);
562 LIR* target = NewLIR0(kPseudoTargetLabel);
563 branch_over->target = target;
564 FreeTemp(reg_card_base);
565 FreeTemp(reg_card_no);
566}
567
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700568void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 int spill_count = num_core_spills_ + num_fp_spills_;
570 /*
571 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
572 * mechanism know so it doesn't try to use any of them when
573 * expanding the frame or flushing. This leaves the utility
574 * code with a single temp: r12. This should be enough.
575 */
576 LockTemp(r0);
577 LockTemp(r1);
578 LockTemp(r2);
579 LockTemp(r3);
580
581 /*
582 * We can safely skip the stack overflow check if we're
583 * a leaf *and* our frame size < fudge factor.
584 */
585 bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
586 (static_cast<size_t>(frame_size_) <
587 Thread::kStackOverflowReservedBytes));
588 NewLIR0(kPseudoMethodEntry);
589 if (!skip_overflow_check) {
590 /* Load stack limit */
591 LoadWordDisp(rARM_SELF, Thread::StackEndOffset().Int32Value(), r12);
592 }
593 /* Spill core callee saves */
594 NewLIR1(kThumb2Push, core_spill_mask_);
595 /* Need to spill any FP regs? */
596 if (num_fp_spills_) {
597 /*
598 * NOTE: fp spills are a little different from core spills in that
599 * they are pushed as a contiguous block. When promoting from
600 * the fp set, we must allocate all singles from s16..highest-promoted
601 */
602 NewLIR1(kThumb2VPushCS, num_fp_spills_);
603 }
604 if (!skip_overflow_check) {
605 OpRegRegImm(kOpSub, rARM_LR, rARM_SP, frame_size_ - (spill_count * 4));
Vladimir Marko58af1f92013-12-19 13:31:15 +0000606 GenRegRegCheck(kCondUlt, rARM_LR, r12, kThrowStackOverflow);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 OpRegCopy(rARM_SP, rARM_LR); // Establish stack
608 } else {
609 OpRegImm(kOpSub, rARM_SP, frame_size_ - (spill_count * 4));
610 }
611
612 FlushIns(ArgLocs, rl_method);
613
614 FreeTemp(r0);
615 FreeTemp(r1);
616 FreeTemp(r2);
617 FreeTemp(r3);
618}
619
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700620void ArmMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 int spill_count = num_core_spills_ + num_fp_spills_;
622 /*
623 * In the exit path, r0/r1 are live - make sure they aren't
624 * allocated by the register utilities as temps.
625 */
626 LockTemp(r0);
627 LockTemp(r1);
628
629 NewLIR0(kPseudoMethodExit);
630 OpRegImm(kOpAdd, rARM_SP, frame_size_ - (spill_count * 4));
631 /* Need to restore any FP callee saves? */
632 if (num_fp_spills_) {
633 NewLIR1(kThumb2VPopCS, num_fp_spills_);
634 }
635 if (core_spill_mask_ & (1 << rARM_LR)) {
636 /* Unspill rARM_LR to rARM_PC */
637 core_spill_mask_ &= ~(1 << rARM_LR);
638 core_spill_mask_ |= (1 << rARM_PC);
639 }
640 NewLIR1(kThumb2Pop, core_spill_mask_);
641 if (!(core_spill_mask_ & (1 << rARM_PC))) {
642 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
643 NewLIR1(kThumbBx, rARM_LR);
644 }
645}
646
647} // namespace art