blob: 99b21660300bb2b075327ee459cd7fcc054407ea [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"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
27namespace art {
28
Brian Carlstrom7940e442013-07-12 13:46:57 -070029/*
30 * The sparse table in the literal pool is an array of <key,displacement>
31 * pairs. For each set, we'll load them as a pair using ldmia.
32 * This means that the register number of the temp we use for the key
33 * must be lower than the reg for the displacement.
34 *
35 * The test loop will look something like:
36 *
buzbee2700f7e2014-03-07 09:46:20 -080037 * adr r_base, <table>
Brian Carlstrom7940e442013-07-12 13:46:57 -070038 * ldr r_val, [rARM_SP, v_reg_off]
39 * mov r_idx, #table_size
40 * lp:
buzbee2700f7e2014-03-07 09:46:20 -080041 * ldmia r_base!, {r_key, r_disp}
Brian Carlstrom7940e442013-07-12 13:46:57 -070042 * sub r_idx, #1
43 * cmp r_val, r_key
44 * ifeq
45 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
46 * cbnz r_idx, lp
47 */
Andreas Gampe48971b32014-08-06 10:09:01 -070048void ArmMir2Lir::GenLargeSparseSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070049 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070050 if (cu_->verbose) {
51 DumpSparseSwitchTable(table);
52 }
53 // Add the table to the list - we'll process it later
54 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000055 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 tab_rec->table = table;
57 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -070058 uint32_t size = table[1];
buzbee091cc402014-03-31 10:14:40 -070059 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010060 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070061
62 // Get the switch value
63 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080064 RegStorage r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070065 /* Allocate key and disp temps */
buzbee2700f7e2014-03-07 09:46:20 -080066 RegStorage r_key = AllocTemp();
67 RegStorage r_disp = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070068 // Make sure r_key's register number is less than r_disp's number for ldmia
buzbee2700f7e2014-03-07 09:46:20 -080069 if (r_key.GetReg() > r_disp.GetReg()) {
70 RegStorage tmp = r_disp;
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 r_disp = r_key;
72 r_key = tmp;
73 }
74 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080075 NewLIR3(kThumb2Adr, r_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 // Set up r_idx
buzbee2700f7e2014-03-07 09:46:20 -080077 RegStorage r_idx = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 LoadConstant(r_idx, size);
79 // Establish loop branch target
80 LIR* target = NewLIR0(kPseudoTargetLabel);
81 // Load next key/disp
buzbee091cc402014-03-31 10:14:40 -070082 NewLIR2(kThumb2LdmiaWB, r_base.GetReg(), (1 << r_key.GetRegNum()) | (1 << r_disp.GetRegNum()));
buzbee2700f7e2014-03-07 09:46:20 -080083 OpRegReg(kOpCmp, r_key, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
Dave Allison3da67a52014-04-02 17:03:45 -070085 LIR* it = OpIT(kCondEq, "");
buzbee2700f7e2014-03-07 09:46:20 -080086 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp.GetReg());
Dave Allison3da67a52014-04-02 17:03:45 -070087 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 tab_rec->anchor = switch_branch;
89 // Needs to use setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +000090 OpRegRegImm(kOpSub, r_idx, r_idx, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +010091 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 OpCondBranch(kCondNe, target);
93}
94
95
Andreas Gampe48971b32014-08-06 10:09:01 -070096void ArmMir2Lir::GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070097 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 if (cu_->verbose) {
99 DumpPackedSwitchTable(table);
100 }
101 // Add the table to the list - we'll process it later
102 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000103 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104 tab_rec->table = table;
105 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700106 uint32_t size = table[1];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 tab_rec->targets =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000108 static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100109 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110
111 // Get the switch value
112 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800113 RegStorage table_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800115 NewLIR3(kThumb2Adr, table_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 int low_key = s4FromSwitchData(&table[2]);
buzbee2700f7e2014-03-07 09:46:20 -0800117 RegStorage keyReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 // Remove the bias, if necessary
119 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800120 keyReg = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 } else {
122 keyReg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800123 OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 }
125 // Bounds check - if < 0 or >= size continue following switch
126 OpRegImm(kOpCmp, keyReg, size-1);
127 LIR* branch_over = OpCondBranch(kCondHi, NULL);
128
129 // Load the displacement from the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800130 RegStorage disp_reg = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700131 LoadBaseIndexed(table_base, keyReg, disp_reg, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132
133 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbee2700f7e2014-03-07 09:46:20 -0800134 LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 tab_rec->anchor = switch_branch;
136
137 /* branch_over target here */
138 LIR* target = NewLIR0(kPseudoTargetLabel);
139 branch_over->target = target;
140}
141
142/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700143 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
144 * details see monitor.cc.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700146void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 FlushAllRegs();
buzbee695d13a2014-04-19 13:32:20 -0700148 // FIXME: need separate LoadValues for object references.
buzbee2700f7e2014-03-07 09:46:20 -0800149 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700151 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
152 if (kArchVariantHasGoodBranchPredictor) {
Dave Allisonf9439142014-03-27 15:10:22 -0700153 LIR* null_check_branch = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700154 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
155 null_check_branch = nullptr; // No null check.
156 } else {
157 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000158 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700159 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
160 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700161 }
buzbee695d13a2014-04-19 13:32:20 -0700162 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700163 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
164 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700165 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800166 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_r1, 0, NULL);
buzbee091cc402014-03-31 10:14:40 -0700167 NewLIR4(kThumb2Strex, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
168 mirror::Object::MonitorOffset().Int32Value() >> 2);
buzbee2700f7e2014-03-07 09:46:20 -0800169 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_r1, 0, NULL);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700170
171
172 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
173 not_unlocked_branch->target = slow_path_target;
174 if (null_check_branch != nullptr) {
175 null_check_branch->target = slow_path_target;
176 }
177 // TODO: move to a slow path.
178 // Go expensive route - artLockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700179 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000180 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800181 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700182 MarkSafepointPC(call_inst);
183
184 LIR* success_target = NewLIR0(kPseudoTargetLabel);
185 lock_success_branch->target = success_target;
Hans Boehm48f5c472014-06-27 14:50:10 -0700186 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700187 } else {
188 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800189 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700190 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700191 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
192 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700193 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800194 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700195 LIR* it = OpIT(kCondEq, "");
buzbee091cc402014-03-31 10:14:40 -0700196 NewLIR4(kThumb2Strex/*eq*/, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
197 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allison3da67a52014-04-02 17:03:45 -0700198 OpEndIT(it);
buzbee2700f7e2014-03-07 09:46:20 -0800199 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700200 it = OpIT(kCondNe, "T");
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700201 // Go expensive route - artLockObjectFromCode(self, obj);
buzbee091cc402014-03-31 10:14:40 -0700202 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(),
203 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000204 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800205 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700206 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700207 MarkSafepointPC(call_inst);
Hans Boehm48f5c472014-06-27 14:50:10 -0700208 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700209 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210}
211
212/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700213 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
214 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
215 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700217void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800219 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 LockCallTemps(); // Prepare for explicit register usage
Dave Allisonf9439142014-03-27 15:10:22 -0700221 LIR* null_check_branch = nullptr;
buzbee695d13a2014-04-19 13:32:20 -0700222 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700223 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
224 if (kArchVariantHasGoodBranchPredictor) {
225 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
226 null_check_branch = nullptr; // No null check.
227 } else {
228 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000229 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700230 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
231 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700232 }
buzbee695d13a2014-04-19 13:32:20 -0700233 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1);
Dave Allisonf9439142014-03-27 15:10:22 -0700234 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800235 LoadConstantNoClobber(rs_r3, 0);
236 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_r1, rs_r2, NULL);
Hans Boehm48f5c472014-06-27 14:50:10 -0700237 GenMemBarrier(kAnyStore);
buzbee695d13a2014-04-19 13:32:20 -0700238 Store32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700239 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
240
241 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
242 slow_unlock_branch->target = slow_path_target;
243 if (null_check_branch != nullptr) {
244 null_check_branch->target = slow_path_target;
245 }
246 // TODO: move to a slow path.
247 // Go expensive route - artUnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700248 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000249 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800250 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700251 MarkSafepointPC(call_inst);
252
253 LIR* success_target = NewLIR0(kPseudoTargetLabel);
254 unlock_success_branch->target = success_target;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700255 } else {
256 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800257 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700258 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock
Dave Allisonb373e092014-02-20 16:06:36 -0800259 MarkPossibleNullPointerException(opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700260 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee2700f7e2014-03-07 09:46:20 -0800261 LoadConstantNoClobber(rs_r3, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700262 // Is lock unheld on lock or held by us (==thread_id) on unlock?
buzbee2700f7e2014-03-07 09:46:20 -0800263 OpRegReg(kOpCmp, rs_r1, rs_r2);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700264
265 LIR* it = OpIT(kCondEq, "EE");
Hans Boehm48f5c472014-06-27 14:50:10 -0700266 if (GenMemBarrier(kAnyStore)) {
Andreas Gampeb14329f2014-05-15 11:16:06 -0700267 UpdateIT(it, "TEE");
268 }
buzbee695d13a2014-04-19 13:32:20 -0700269 Store32Disp/*eq*/(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700270 // Go expensive route - UnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700271 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -0800272 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000273 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800274 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700275 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700276 MarkSafepointPC(call_inst);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700277 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278}
279
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700280void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700281 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700282 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
283 RegStorage reset_reg = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000284 LoadRefDisp(rs_rARM_SELF, ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 LoadConstant(reset_reg, 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000286 StoreRefDisp(rs_rARM_SELF, ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 FreeTemp(reset_reg);
288 StoreValue(rl_dest, rl_result);
289}
290
Vladimir Markobf535be2014-11-19 18:52:35 +0000291void ArmMir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800292 RegStorage reg_card_base = AllocTemp();
293 RegStorage reg_card_no = AllocTemp();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700294 LoadWordDisp(rs_rARM_SELF, Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800296 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 FreeTemp(reg_card_base);
298 FreeTemp(reg_card_no);
299}
300
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700301void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 int spill_count = num_core_spills_ + num_fp_spills_;
303 /*
304 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
305 * mechanism know so it doesn't try to use any of them when
306 * expanding the frame or flushing. This leaves the utility
307 * code with a single temp: r12. This should be enough.
308 */
buzbee091cc402014-03-31 10:14:40 -0700309 LockTemp(rs_r0);
310 LockTemp(rs_r1);
311 LockTemp(rs_r2);
312 LockTemp(rs_r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313
314 /*
315 * We can safely skip the stack overflow check if we're
316 * a leaf *and* our frame size < fudge factor.
317 */
Dave Allison648d7112014-07-25 16:15:27 -0700318 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, kArm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 NewLIR0(kPseudoMethodEntry);
Dave Allison648d7112014-07-25 16:15:27 -0700320 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(kArm);
Andreas Gampe7cd26f32014-06-18 17:01:15 -0700321 bool large_frame = (static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes);
Dave Allison648d7112014-07-25 16:15:27 -0700322 bool generate_explicit_stack_overflow_check = large_frame ||
323 !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700325 if (generate_explicit_stack_overflow_check) {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000326 if (!large_frame) {
327 /* Load stack limit */
328 LockTemp(rs_r12);
329 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
330 }
Dave Allison5cd33752014-04-15 15:57:58 -0700331 } else {
332 // Implicit stack overflow check.
333 // Generate a load from [sp, #-overflowsize]. If this is in the stack
334 // redzone we will get a segmentation fault.
335 //
336 // Caveat coder: if someone changes the kStackOverflowReservedBytes value
337 // we need to make sure that it's loadable in an immediate field of
338 // a sub instruction. Otherwise we will get a temp allocation and the
339 // code size will increase.
340 //
341 // This is done before the callee save instructions to avoid any possibility
342 // of these overflowing. This uses r12 and that's never saved in a callee
343 // save.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700344 OpRegRegImm(kOpSub, rs_r12, rs_rARM_SP, GetStackOverflowReservedBytes(kArm));
Dave Allison5cd33752014-04-15 15:57:58 -0700345 Load32Disp(rs_r12, 0, rs_r12);
346 MarkPossibleStackOverflowException();
Dave Allisonb373e092014-02-20 16:06:36 -0800347 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 }
349 /* Spill core callee saves */
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000350 if (core_spill_mask_ == 0u) {
351 // Nothing to spill.
352 } else if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_LR.GetRegNum()))) == 0u) {
353 // Spilling only low regs and/or LR, use 16-bit PUSH.
354 constexpr int lr_bit_shift = rs_rARM_LR.GetRegNum() - 8;
355 NewLIR1(kThumbPush,
356 (core_spill_mask_ & ~(1u << rs_rARM_LR.GetRegNum())) |
357 ((core_spill_mask_ & (1u << rs_rARM_LR.GetRegNum())) >> lr_bit_shift));
358 } else if (IsPowerOfTwo(core_spill_mask_)) {
359 // kThumb2Push cannot be used to spill a single register.
360 NewLIR1(kThumb2Push1, CTZ(core_spill_mask_));
361 } else {
362 NewLIR1(kThumb2Push, core_spill_mask_);
363 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 /* Need to spill any FP regs? */
365 if (num_fp_spills_) {
366 /*
367 * NOTE: fp spills are a little different from core spills in that
368 * they are pushed as a contiguous block. When promoting from
369 * the fp set, we must allocate all singles from s16..highest-promoted
370 */
371 NewLIR1(kThumb2VPushCS, num_fp_spills_);
372 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700373
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700374 const int spill_size = spill_count * 4;
375 const int frame_size_without_spills = frame_size_ - spill_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700377 if (generate_explicit_stack_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700378 class StackOverflowSlowPath : public LIRSlowPath {
379 public:
380 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, bool restore_lr, size_t sp_displace)
381 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), restore_lr_(restore_lr),
382 sp_displace_(sp_displace) {
383 }
384 void Compile() OVERRIDE {
385 m2l_->ResetRegPool();
386 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700387 GenerateTargetLabel(kPseudoThrowTarget);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700388 if (restore_lr_) {
buzbee2700f7e2014-03-07 09:46:20 -0800389 m2l_->LoadWordDisp(rs_rARM_SP, sp_displace_ - 4, rs_rARM_LR);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700390 }
buzbee2700f7e2014-03-07 09:46:20 -0800391 m2l_->OpRegImm(kOpAdd, rs_rARM_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700392 m2l_->ClobberCallerSave();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700393 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700394 // Load the entrypoint directly into the pc instead of doing a load + branch. Assumes
395 // codegen and target are in thumb2 mode.
buzbee695d13a2014-04-19 13:32:20 -0700396 // NOTE: native pointer.
buzbee2700f7e2014-03-07 09:46:20 -0800397 m2l_->LoadWordDisp(rs_rARM_SELF, func_offset.Int32Value(), rs_rARM_PC);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700398 }
399
400 private:
401 const bool restore_lr_;
402 const size_t sp_displace_;
403 };
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000404 if (large_frame) {
405 // Note: may need a temp reg, and we only have r12 free at this point.
buzbee2700f7e2014-03-07 09:46:20 -0800406 OpRegRegImm(kOpSub, rs_rARM_LR, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000407 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
buzbee2700f7e2014-03-07 09:46:20 -0800408 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_LR, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700409 // Need to restore LR since we used it as a temp.
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700410 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, true, spill_size));
buzbee2700f7e2014-03-07 09:46:20 -0800411 OpRegCopy(rs_rARM_SP, rs_rARM_LR); // Establish stack
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700412 } else {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000413 /*
414 * If the frame is small enough we are guaranteed to have enough space that remains to
415 * handle signals on the user stack. However, we may not have any free temp
416 * registers at this point, so we'll temporarily add LR to the temp pool.
417 */
418 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
419 MarkTemp(rs_rARM_LR);
420 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800421 OpRegRegImm(kOpSub, rs_rARM_SP, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000422 Clobber(rs_rARM_LR);
423 UnmarkTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800424 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_SP, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700425 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, false, frame_size_));
426 }
Dave Allisonb373e092014-02-20 16:06:36 -0800427 } else {
Dave Allison5cd33752014-04-15 15:57:58 -0700428 // Implicit stack overflow check has already been done. Just make room on the
429 // stack for the frame now.
Dave Allisonf9439142014-03-27 15:10:22 -0700430 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Dave Allisonb373e092014-02-20 16:06:36 -0800431 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800433 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 }
435
436 FlushIns(ArgLocs, rl_method);
437
buzbee091cc402014-03-31 10:14:40 -0700438 FreeTemp(rs_r0);
439 FreeTemp(rs_r1);
440 FreeTemp(rs_r2);
441 FreeTemp(rs_r3);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000442 FreeTemp(rs_r12);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443}
444
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700445void ArmMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 int spill_count = num_core_spills_ + num_fp_spills_;
447 /*
448 * In the exit path, r0/r1 are live - make sure they aren't
449 * allocated by the register utilities as temps.
450 */
buzbee091cc402014-03-31 10:14:40 -0700451 LockTemp(rs_r0);
452 LockTemp(rs_r1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453
454 NewLIR0(kPseudoMethodExit);
buzbee2700f7e2014-03-07 09:46:20 -0800455 OpRegImm(kOpAdd, rs_rARM_SP, frame_size_ - (spill_count * 4));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 /* Need to restore any FP callee saves? */
457 if (num_fp_spills_) {
458 NewLIR1(kThumb2VPopCS, num_fp_spills_);
459 }
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000460 if ((core_spill_mask_ & (1 << rs_rARM_LR.GetRegNum())) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 /* Unspill rARM_LR to rARM_PC */
buzbee091cc402014-03-31 10:14:40 -0700462 core_spill_mask_ &= ~(1 << rs_rARM_LR.GetRegNum());
463 core_spill_mask_ |= (1 << rs_rARM_PC.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 }
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000465 if (core_spill_mask_ == 0u) {
466 // Nothing to unspill.
467 } else if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_PC.GetRegNum()))) == 0u) {
468 // Unspilling only low regs and/or PC, use 16-bit POP.
469 constexpr int pc_bit_shift = rs_rARM_PC.GetRegNum() - 8;
470 NewLIR1(kThumbPop,
471 (core_spill_mask_ & ~(1u << rs_rARM_PC.GetRegNum())) |
472 ((core_spill_mask_ & (1u << rs_rARM_PC.GetRegNum())) >> pc_bit_shift));
473 } else if (IsPowerOfTwo(core_spill_mask_)) {
474 // kThumb2Pop cannot be used to unspill a single register.
475 NewLIR1(kThumb2Pop1, CTZ(core_spill_mask_));
476 } else {
477 NewLIR1(kThumb2Pop, core_spill_mask_);
478 }
479 if ((core_spill_mask_ & (1 << rs_rARM_PC.GetRegNum())) == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
buzbee091cc402014-03-31 10:14:40 -0700481 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 }
483}
484
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800485void ArmMir2Lir::GenSpecialExitSequence() {
buzbee091cc402014-03-31 10:14:40 -0700486 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800487}
488
Vladimir Markof4da6752014-08-01 19:04:18 +0100489static bool ArmUseRelativeCall(CompilationUnit* cu, const MethodReference& target_method) {
490 // Emit relative calls only within a dex file due to the limited range of the BL insn.
491 return cu->dex_file == target_method.dex_file;
492}
493
494/*
495 * Bit of a hack here - in the absence of a real scheduling pass,
496 * emit the next instruction in static & direct invoke sequences.
497 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700498static int ArmNextSDCallInsn(CompilationUnit* cu, CallInfo* info ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100499 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700500 uint32_t unused_idx ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100501 uintptr_t direct_code, uintptr_t direct_method,
502 InvokeType type) {
503 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
504 if (direct_code != 0 && direct_method != 0) {
505 switch (state) {
506 case 0: // Get the current Method* [sets kArg0]
507 if (direct_code != static_cast<uintptr_t>(-1)) {
508 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
509 } else if (ArmUseRelativeCall(cu, target_method)) {
510 // Defer to linker patch.
511 } else {
512 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
513 }
514 if (direct_method != static_cast<uintptr_t>(-1)) {
515 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
516 } else {
517 cg->LoadMethodAddress(target_method, type, kArg0);
518 }
519 break;
520 default:
521 return -1;
522 }
523 } else {
524 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
525 switch (state) {
526 case 0: // Get the current Method* [sets kArg0]
527 // TUNING: we can save a reg copy if Method* has been promoted.
528 cg->LoadCurrMethodDirect(arg0_ref);
529 break;
530 case 1: // Get method->dex_cache_resolved_methods_
531 cg->LoadRefDisp(arg0_ref,
532 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
533 arg0_ref,
534 kNotVolatile);
535 // Set up direct code if known.
536 if (direct_code != 0) {
537 if (direct_code != static_cast<uintptr_t>(-1)) {
538 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
539 } else if (ArmUseRelativeCall(cu, target_method)) {
540 // Defer to linker patch.
541 } else {
542 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
543 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
544 }
545 }
546 break;
547 case 2: // Grab target method*
548 CHECK_EQ(cu->dex_file, target_method.dex_file);
549 cg->LoadRefDisp(arg0_ref,
550 mirror::ObjectArray<mirror::Object>::OffsetOfElement(
551 target_method.dex_method_index).Int32Value(),
552 arg0_ref,
553 kNotVolatile);
554 break;
555 case 3: // Grab the code from the method*
556 if (direct_code == 0) {
557 // kInvokeTgt := arg0_ref->entrypoint
558 cg->LoadWordDisp(arg0_ref,
Mathieu Chartier2d721012014-11-10 11:08:06 -0800559 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
560 kArmPointerSize).Int32Value(), cg->TargetPtrReg(kInvokeTgt));
Vladimir Markof4da6752014-08-01 19:04:18 +0100561 }
562 break;
563 default:
564 return -1;
565 }
566 }
567 return state + 1;
568}
569
570NextCallInsn ArmMir2Lir::GetNextSDCallInsn() {
571 return ArmNextSDCallInsn;
572}
573
574LIR* ArmMir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
575 // For ARM, just generate a relative BL instruction that will be filled in at 'link time'.
576 // If the target turns out to be too far, the linker will generate a thunk for dispatch.
577 int target_method_idx = target_method.dex_method_index;
578 const DexFile* target_dex_file = target_method.dex_file;
579
580 // Generate the call instruction and save index, dex_file, and type.
581 // NOTE: Method deduplication takes linker patches into account, so we can just pass 0
582 // as a placeholder for the offset.
583 LIR* call = RawLIR(current_dalvik_offset_, kThumb2Bl, 0,
584 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
585 AppendLIR(call);
586 call_method_insns_.push_back(call);
587 return call;
588}
589
590LIR* ArmMir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info) {
591 LIR* call_insn;
592 if (method_info.FastPath() && ArmUseRelativeCall(cu_, method_info.GetTargetMethod()) &&
593 (method_info.GetSharpType() == kDirect || method_info.GetSharpType() == kStatic) &&
594 method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
595 call_insn = CallWithLinkerFixup(method_info.GetTargetMethod(), method_info.GetSharpType());
596 } else {
597 call_insn = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
598 }
599 return call_insn;
600}
601
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602} // namespace art