blob: 0713b7a18eb7591b1d73fb69e7415584b7b65330 [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"
21#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070022#include "gc/accounting/card_table.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010023#include "mirror/art_method.h"
24#include "mirror/object_array-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070025#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080026#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027
28namespace art {
29
Brian Carlstrom7940e442013-07-12 13:46:57 -070030/*
31 * The sparse table in the literal pool is an array of <key,displacement>
32 * pairs. For each set, we'll load them as a pair using ldmia.
33 * This means that the register number of the temp we use for the key
34 * must be lower than the reg for the displacement.
35 *
36 * The test loop will look something like:
37 *
buzbee2700f7e2014-03-07 09:46:20 -080038 * adr r_base, <table>
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 * ldr r_val, [rARM_SP, v_reg_off]
40 * mov r_idx, #table_size
41 * lp:
buzbee2700f7e2014-03-07 09:46:20 -080042 * ldmia r_base!, {r_key, r_disp}
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 * sub r_idx, #1
44 * cmp r_val, r_key
45 * ifeq
46 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
47 * cbnz r_idx, lp
48 */
Andreas Gampe48971b32014-08-06 10:09:01 -070049void ArmMir2Lir::GenLargeSparseSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070050 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070051 if (cu_->verbose) {
52 DumpSparseSwitchTable(table);
53 }
54 // Add the table to the list - we'll process it later
55 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000056 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 tab_rec->table = table;
58 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -070059 uint32_t size = table[1];
buzbee091cc402014-03-31 10:14:40 -070060 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010061 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070062
63 // Get the switch value
64 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080065 RegStorage r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 /* Allocate key and disp temps */
buzbee2700f7e2014-03-07 09:46:20 -080067 RegStorage r_key = AllocTemp();
68 RegStorage r_disp = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 // Make sure r_key's register number is less than r_disp's number for ldmia
buzbee2700f7e2014-03-07 09:46:20 -080070 if (r_key.GetReg() > r_disp.GetReg()) {
71 RegStorage tmp = r_disp;
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 r_disp = r_key;
73 r_key = tmp;
74 }
75 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080076 NewLIR3(kThumb2Adr, r_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 // Set up r_idx
buzbee2700f7e2014-03-07 09:46:20 -080078 RegStorage r_idx = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 LoadConstant(r_idx, size);
80 // Establish loop branch target
81 LIR* target = NewLIR0(kPseudoTargetLabel);
82 // Load next key/disp
buzbee091cc402014-03-31 10:14:40 -070083 NewLIR2(kThumb2LdmiaWB, r_base.GetReg(), (1 << r_key.GetRegNum()) | (1 << r_disp.GetRegNum()));
buzbee2700f7e2014-03-07 09:46:20 -080084 OpRegReg(kOpCmp, r_key, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070085 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
Dave Allison3da67a52014-04-02 17:03:45 -070086 LIR* it = OpIT(kCondEq, "");
buzbee2700f7e2014-03-07 09:46:20 -080087 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp.GetReg());
Dave Allison3da67a52014-04-02 17:03:45 -070088 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 tab_rec->anchor = switch_branch;
90 // Needs to use setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +000091 OpRegRegImm(kOpSub, r_idx, r_idx, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +010092 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 OpCondBranch(kCondNe, target);
94}
95
96
Andreas Gampe48971b32014-08-06 10:09:01 -070097void ArmMir2Lir::GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070098 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 if (cu_->verbose) {
100 DumpPackedSwitchTable(table);
101 }
102 // Add the table to the list - we'll process it later
103 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000104 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 tab_rec->table = table;
106 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700107 uint32_t size = table[1];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 tab_rec->targets =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000109 static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100110 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111
112 // Get the switch value
113 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800114 RegStorage table_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800116 NewLIR3(kThumb2Adr, table_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 int low_key = s4FromSwitchData(&table[2]);
buzbee2700f7e2014-03-07 09:46:20 -0800118 RegStorage keyReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 // Remove the bias, if necessary
120 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800121 keyReg = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 } else {
123 keyReg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800124 OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 }
126 // Bounds check - if < 0 or >= size continue following switch
127 OpRegImm(kOpCmp, keyReg, size-1);
128 LIR* branch_over = OpCondBranch(kCondHi, NULL);
129
130 // Load the displacement from the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800131 RegStorage disp_reg = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700132 LoadBaseIndexed(table_base, keyReg, disp_reg, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133
134 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbee2700f7e2014-03-07 09:46:20 -0800135 LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 tab_rec->anchor = switch_branch;
137
138 /* branch_over target here */
139 LIR* target = NewLIR0(kPseudoTargetLabel);
140 branch_over->target = target;
141}
142
143/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700144 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
145 * details see monitor.cc.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700147void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 FlushAllRegs();
buzbee695d13a2014-04-19 13:32:20 -0700149 // FIXME: need separate LoadValues for object references.
buzbee2700f7e2014-03-07 09:46:20 -0800150 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700152 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
153 if (kArchVariantHasGoodBranchPredictor) {
Dave Allisonf9439142014-03-27 15:10:22 -0700154 LIR* null_check_branch = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700155 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
156 null_check_branch = nullptr; // No null check.
157 } else {
158 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000159 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700160 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
161 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700162 }
buzbee695d13a2014-04-19 13:32:20 -0700163 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700164 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
165 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700166 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800167 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_r1, 0, NULL);
buzbee091cc402014-03-31 10:14:40 -0700168 NewLIR4(kThumb2Strex, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
169 mirror::Object::MonitorOffset().Int32Value() >> 2);
buzbee2700f7e2014-03-07 09:46:20 -0800170 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_r1, 0, NULL);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700171
172
173 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
174 not_unlocked_branch->target = slow_path_target;
175 if (null_check_branch != nullptr) {
176 null_check_branch->target = slow_path_target;
177 }
178 // TODO: move to a slow path.
179 // Go expensive route - artLockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700180 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000181 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800182 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700183 MarkSafepointPC(call_inst);
184
185 LIR* success_target = NewLIR0(kPseudoTargetLabel);
186 lock_success_branch->target = success_target;
Hans Boehm48f5c472014-06-27 14:50:10 -0700187 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700188 } else {
189 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800190 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700191 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700192 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
193 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700194 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800195 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700196 LIR* it = OpIT(kCondEq, "");
buzbee091cc402014-03-31 10:14:40 -0700197 NewLIR4(kThumb2Strex/*eq*/, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
198 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allison3da67a52014-04-02 17:03:45 -0700199 OpEndIT(it);
buzbee2700f7e2014-03-07 09:46:20 -0800200 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700201 it = OpIT(kCondNe, "T");
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700202 // Go expensive route - artLockObjectFromCode(self, obj);
buzbee091cc402014-03-31 10:14:40 -0700203 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(),
204 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000205 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800206 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700207 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700208 MarkSafepointPC(call_inst);
Hans Boehm48f5c472014-06-27 14:50:10 -0700209 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700210 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211}
212
213/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700214 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
215 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
216 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700218void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800220 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 LockCallTemps(); // Prepare for explicit register usage
Dave Allisonf9439142014-03-27 15:10:22 -0700222 LIR* null_check_branch = nullptr;
buzbee695d13a2014-04-19 13:32:20 -0700223 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700224 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
225 if (kArchVariantHasGoodBranchPredictor) {
226 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
227 null_check_branch = nullptr; // No null check.
228 } else {
229 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000230 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700231 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
232 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700233 }
buzbee695d13a2014-04-19 13:32:20 -0700234 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1);
Dave Allisonf9439142014-03-27 15:10:22 -0700235 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800236 LoadConstantNoClobber(rs_r3, 0);
237 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_r1, rs_r2, NULL);
Hans Boehm48f5c472014-06-27 14:50:10 -0700238 GenMemBarrier(kAnyStore);
buzbee695d13a2014-04-19 13:32:20 -0700239 Store32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700240 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
241
242 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
243 slow_unlock_branch->target = slow_path_target;
244 if (null_check_branch != nullptr) {
245 null_check_branch->target = slow_path_target;
246 }
247 // TODO: move to a slow path.
248 // Go expensive route - artUnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700249 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000250 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800251 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700252 MarkSafepointPC(call_inst);
253
254 LIR* success_target = NewLIR0(kPseudoTargetLabel);
255 unlock_success_branch->target = success_target;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700256 } else {
257 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800258 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700259 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock
Dave Allisonb373e092014-02-20 16:06:36 -0800260 MarkPossibleNullPointerException(opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700261 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee2700f7e2014-03-07 09:46:20 -0800262 LoadConstantNoClobber(rs_r3, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700263 // Is lock unheld on lock or held by us (==thread_id) on unlock?
buzbee2700f7e2014-03-07 09:46:20 -0800264 OpRegReg(kOpCmp, rs_r1, rs_r2);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700265
266 LIR* it = OpIT(kCondEq, "EE");
Hans Boehm48f5c472014-06-27 14:50:10 -0700267 if (GenMemBarrier(kAnyStore)) {
Andreas Gampeb14329f2014-05-15 11:16:06 -0700268 UpdateIT(it, "TEE");
269 }
buzbee695d13a2014-04-19 13:32:20 -0700270 Store32Disp/*eq*/(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700271 // Go expensive route - UnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700272 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -0800273 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000274 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800275 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700276 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700277 MarkSafepointPC(call_inst);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700278 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279}
280
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700281void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700282 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700283 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
284 RegStorage reset_reg = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000285 LoadRefDisp(rs_rARM_SELF, ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 LoadConstant(reset_reg, 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000287 StoreRefDisp(rs_rARM_SELF, ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 FreeTemp(reset_reg);
289 StoreValue(rl_dest, rl_result);
290}
291
Vladimir Markobf535be2014-11-19 18:52:35 +0000292void ArmMir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800293 RegStorage reg_card_base = AllocTemp();
294 RegStorage reg_card_no = AllocTemp();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700295 LoadWordDisp(rs_rARM_SELF, Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800297 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 FreeTemp(reg_card_base);
299 FreeTemp(reg_card_no);
300}
301
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700302void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 int spill_count = num_core_spills_ + num_fp_spills_;
304 /*
305 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
306 * mechanism know so it doesn't try to use any of them when
307 * expanding the frame or flushing. This leaves the utility
308 * code with a single temp: r12. This should be enough.
309 */
buzbee091cc402014-03-31 10:14:40 -0700310 LockTemp(rs_r0);
311 LockTemp(rs_r1);
312 LockTemp(rs_r2);
313 LockTemp(rs_r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314
315 /*
316 * We can safely skip the stack overflow check if we're
317 * a leaf *and* our frame size < fudge factor.
318 */
Dave Allison648d7112014-07-25 16:15:27 -0700319 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, kArm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 NewLIR0(kPseudoMethodEntry);
Dave Allison648d7112014-07-25 16:15:27 -0700321 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(kArm);
Andreas Gampe7cd26f32014-06-18 17:01:15 -0700322 bool large_frame = (static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes);
Dave Allison648d7112014-07-25 16:15:27 -0700323 bool generate_explicit_stack_overflow_check = large_frame ||
324 !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700326 if (generate_explicit_stack_overflow_check) {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000327 if (!large_frame) {
328 /* Load stack limit */
329 LockTemp(rs_r12);
330 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
331 }
Dave Allison5cd33752014-04-15 15:57:58 -0700332 } else {
333 // Implicit stack overflow check.
334 // Generate a load from [sp, #-overflowsize]. If this is in the stack
335 // redzone we will get a segmentation fault.
336 //
337 // Caveat coder: if someone changes the kStackOverflowReservedBytes value
338 // we need to make sure that it's loadable in an immediate field of
339 // a sub instruction. Otherwise we will get a temp allocation and the
340 // code size will increase.
341 //
342 // This is done before the callee save instructions to avoid any possibility
343 // of these overflowing. This uses r12 and that's never saved in a callee
344 // save.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700345 OpRegRegImm(kOpSub, rs_r12, rs_rARM_SP, GetStackOverflowReservedBytes(kArm));
Dave Allison5cd33752014-04-15 15:57:58 -0700346 Load32Disp(rs_r12, 0, rs_r12);
347 MarkPossibleStackOverflowException();
Dave Allisonb373e092014-02-20 16:06:36 -0800348 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 }
350 /* Spill core callee saves */
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000351 if (core_spill_mask_ == 0u) {
352 // Nothing to spill.
353 } else if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_LR.GetRegNum()))) == 0u) {
354 // Spilling only low regs and/or LR, use 16-bit PUSH.
355 constexpr int lr_bit_shift = rs_rARM_LR.GetRegNum() - 8;
356 NewLIR1(kThumbPush,
357 (core_spill_mask_ & ~(1u << rs_rARM_LR.GetRegNum())) |
358 ((core_spill_mask_ & (1u << rs_rARM_LR.GetRegNum())) >> lr_bit_shift));
359 } else if (IsPowerOfTwo(core_spill_mask_)) {
360 // kThumb2Push cannot be used to spill a single register.
361 NewLIR1(kThumb2Push1, CTZ(core_spill_mask_));
362 } else {
363 NewLIR1(kThumb2Push, core_spill_mask_);
364 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 /* Need to spill any FP regs? */
366 if (num_fp_spills_) {
367 /*
368 * NOTE: fp spills are a little different from core spills in that
369 * they are pushed as a contiguous block. When promoting from
370 * the fp set, we must allocate all singles from s16..highest-promoted
371 */
372 NewLIR1(kThumb2VPushCS, num_fp_spills_);
373 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700374
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700375 const int spill_size = spill_count * 4;
376 const int frame_size_without_spills = frame_size_ - spill_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700378 if (generate_explicit_stack_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700379 class StackOverflowSlowPath : public LIRSlowPath {
380 public:
381 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, bool restore_lr, size_t sp_displace)
382 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), restore_lr_(restore_lr),
383 sp_displace_(sp_displace) {
384 }
385 void Compile() OVERRIDE {
386 m2l_->ResetRegPool();
387 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700388 GenerateTargetLabel(kPseudoThrowTarget);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700389 if (restore_lr_) {
buzbee2700f7e2014-03-07 09:46:20 -0800390 m2l_->LoadWordDisp(rs_rARM_SP, sp_displace_ - 4, rs_rARM_LR);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700391 }
buzbee2700f7e2014-03-07 09:46:20 -0800392 m2l_->OpRegImm(kOpAdd, rs_rARM_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700393 m2l_->ClobberCallerSave();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700394 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700395 // Load the entrypoint directly into the pc instead of doing a load + branch. Assumes
396 // codegen and target are in thumb2 mode.
buzbee695d13a2014-04-19 13:32:20 -0700397 // NOTE: native pointer.
buzbee2700f7e2014-03-07 09:46:20 -0800398 m2l_->LoadWordDisp(rs_rARM_SELF, func_offset.Int32Value(), rs_rARM_PC);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700399 }
400
401 private:
402 const bool restore_lr_;
403 const size_t sp_displace_;
404 };
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000405 if (large_frame) {
406 // Note: may need a temp reg, and we only have r12 free at this point.
buzbee2700f7e2014-03-07 09:46:20 -0800407 OpRegRegImm(kOpSub, rs_rARM_LR, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000408 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
buzbee2700f7e2014-03-07 09:46:20 -0800409 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_LR, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700410 // Need to restore LR since we used it as a temp.
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700411 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, true, spill_size));
buzbee2700f7e2014-03-07 09:46:20 -0800412 OpRegCopy(rs_rARM_SP, rs_rARM_LR); // Establish stack
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700413 } else {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000414 /*
415 * If the frame is small enough we are guaranteed to have enough space that remains to
416 * handle signals on the user stack. However, we may not have any free temp
417 * registers at this point, so we'll temporarily add LR to the temp pool.
418 */
419 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
420 MarkTemp(rs_rARM_LR);
421 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800422 OpRegRegImm(kOpSub, rs_rARM_SP, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000423 Clobber(rs_rARM_LR);
424 UnmarkTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800425 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_SP, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700426 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, false, frame_size_));
427 }
Dave Allisonb373e092014-02-20 16:06:36 -0800428 } else {
Dave Allison5cd33752014-04-15 15:57:58 -0700429 // Implicit stack overflow check has already been done. Just make room on the
430 // stack for the frame now.
Dave Allisonf9439142014-03-27 15:10:22 -0700431 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Dave Allisonb373e092014-02-20 16:06:36 -0800432 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800434 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 }
436
437 FlushIns(ArgLocs, rl_method);
438
buzbee091cc402014-03-31 10:14:40 -0700439 FreeTemp(rs_r0);
440 FreeTemp(rs_r1);
441 FreeTemp(rs_r2);
442 FreeTemp(rs_r3);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000443 FreeTemp(rs_r12);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444}
445
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700446void ArmMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 int spill_count = num_core_spills_ + num_fp_spills_;
448 /*
449 * In the exit path, r0/r1 are live - make sure they aren't
450 * allocated by the register utilities as temps.
451 */
buzbee091cc402014-03-31 10:14:40 -0700452 LockTemp(rs_r0);
453 LockTemp(rs_r1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454
455 NewLIR0(kPseudoMethodExit);
buzbee2700f7e2014-03-07 09:46:20 -0800456 OpRegImm(kOpAdd, rs_rARM_SP, frame_size_ - (spill_count * 4));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 /* Need to restore any FP callee saves? */
458 if (num_fp_spills_) {
459 NewLIR1(kThumb2VPopCS, num_fp_spills_);
460 }
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000461 if ((core_spill_mask_ & (1 << rs_rARM_LR.GetRegNum())) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 /* Unspill rARM_LR to rARM_PC */
buzbee091cc402014-03-31 10:14:40 -0700463 core_spill_mask_ &= ~(1 << rs_rARM_LR.GetRegNum());
464 core_spill_mask_ |= (1 << rs_rARM_PC.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 }
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000466 if (core_spill_mask_ == 0u) {
467 // Nothing to unspill.
468 } else if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_PC.GetRegNum()))) == 0u) {
469 // Unspilling only low regs and/or PC, use 16-bit POP.
470 constexpr int pc_bit_shift = rs_rARM_PC.GetRegNum() - 8;
471 NewLIR1(kThumbPop,
472 (core_spill_mask_ & ~(1u << rs_rARM_PC.GetRegNum())) |
473 ((core_spill_mask_ & (1u << rs_rARM_PC.GetRegNum())) >> pc_bit_shift));
474 } else if (IsPowerOfTwo(core_spill_mask_)) {
475 // kThumb2Pop cannot be used to unspill a single register.
476 NewLIR1(kThumb2Pop1, CTZ(core_spill_mask_));
477 } else {
478 NewLIR1(kThumb2Pop, core_spill_mask_);
479 }
480 if ((core_spill_mask_ & (1 << rs_rARM_PC.GetRegNum())) == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
buzbee091cc402014-03-31 10:14:40 -0700482 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 }
484}
485
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800486void ArmMir2Lir::GenSpecialExitSequence() {
buzbee091cc402014-03-31 10:14:40 -0700487 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800488}
489
Vladimir Markof4da6752014-08-01 19:04:18 +0100490static bool ArmUseRelativeCall(CompilationUnit* cu, const MethodReference& target_method) {
491 // Emit relative calls only within a dex file due to the limited range of the BL insn.
492 return cu->dex_file == target_method.dex_file;
493}
494
495/*
496 * Bit of a hack here - in the absence of a real scheduling pass,
497 * emit the next instruction in static & direct invoke sequences.
498 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700499static int ArmNextSDCallInsn(CompilationUnit* cu, CallInfo* info ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100500 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700501 uint32_t unused_idx ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100502 uintptr_t direct_code, uintptr_t direct_method,
503 InvokeType type) {
504 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
505 if (direct_code != 0 && direct_method != 0) {
506 switch (state) {
507 case 0: // Get the current Method* [sets kArg0]
508 if (direct_code != static_cast<uintptr_t>(-1)) {
509 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
510 } else if (ArmUseRelativeCall(cu, target_method)) {
511 // Defer to linker patch.
512 } else {
513 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
514 }
515 if (direct_method != static_cast<uintptr_t>(-1)) {
516 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
517 } else {
518 cg->LoadMethodAddress(target_method, type, kArg0);
519 }
520 break;
521 default:
522 return -1;
523 }
524 } else {
525 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
526 switch (state) {
527 case 0: // Get the current Method* [sets kArg0]
528 // TUNING: we can save a reg copy if Method* has been promoted.
529 cg->LoadCurrMethodDirect(arg0_ref);
530 break;
531 case 1: // Get method->dex_cache_resolved_methods_
532 cg->LoadRefDisp(arg0_ref,
533 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
534 arg0_ref,
535 kNotVolatile);
536 // Set up direct code if known.
537 if (direct_code != 0) {
538 if (direct_code != static_cast<uintptr_t>(-1)) {
539 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
540 } else if (ArmUseRelativeCall(cu, target_method)) {
541 // Defer to linker patch.
542 } else {
543 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
544 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
545 }
546 }
547 break;
548 case 2: // Grab target method*
549 CHECK_EQ(cu->dex_file, target_method.dex_file);
550 cg->LoadRefDisp(arg0_ref,
551 mirror::ObjectArray<mirror::Object>::OffsetOfElement(
552 target_method.dex_method_index).Int32Value(),
553 arg0_ref,
554 kNotVolatile);
555 break;
556 case 3: // Grab the code from the method*
557 if (direct_code == 0) {
558 // kInvokeTgt := arg0_ref->entrypoint
559 cg->LoadWordDisp(arg0_ref,
Mathieu Chartier2d721012014-11-10 11:08:06 -0800560 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
561 kArmPointerSize).Int32Value(), cg->TargetPtrReg(kInvokeTgt));
Vladimir Markof4da6752014-08-01 19:04:18 +0100562 }
563 break;
564 default:
565 return -1;
566 }
567 }
568 return state + 1;
569}
570
571NextCallInsn ArmMir2Lir::GetNextSDCallInsn() {
572 return ArmNextSDCallInsn;
573}
574
575LIR* ArmMir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
576 // For ARM, just generate a relative BL instruction that will be filled in at 'link time'.
577 // If the target turns out to be too far, the linker will generate a thunk for dispatch.
578 int target_method_idx = target_method.dex_method_index;
579 const DexFile* target_dex_file = target_method.dex_file;
580
581 // Generate the call instruction and save index, dex_file, and type.
582 // NOTE: Method deduplication takes linker patches into account, so we can just pass 0
583 // as a placeholder for the offset.
584 LIR* call = RawLIR(current_dalvik_offset_, kThumb2Bl, 0,
585 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
586 AppendLIR(call);
587 call_method_insns_.push_back(call);
588 return call;
589}
590
591LIR* ArmMir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info) {
592 LIR* call_insn;
593 if (method_info.FastPath() && ArmUseRelativeCall(cu_, method_info.GetTargetMethod()) &&
594 (method_info.GetSharpType() == kDirect || method_info.GetSharpType() == kStatic) &&
595 method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
596 call_insn = CallWithLinkerFixup(method_info.GetTargetMethod(), method_info.GetSharpType());
597 } else {
598 call_insn = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
599 }
600 return call_insn;
601}
602
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603} // namespace art