blob: 9cf005bc4805771d3d80cb9e5f8a4f3bdf4973cd [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
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "codegen_arm.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020
21#include "arm_lir.h"
22#include "base/logging.h"
23#include "dex/mir_graph.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "dex/quick/mir_to_lir-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080025#include "driver/compiler_driver.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070026#include "gc/accounting/card_table.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010027#include "mirror/art_method.h"
28#include "mirror/object_array-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070029#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080030#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031
32namespace art {
33
Brian Carlstrom7940e442013-07-12 13:46:57 -070034/*
35 * The sparse table in the literal pool is an array of <key,displacement>
36 * pairs. For each set, we'll load them as a pair using ldmia.
37 * This means that the register number of the temp we use for the key
38 * must be lower than the reg for the displacement.
39 *
40 * The test loop will look something like:
41 *
buzbee2700f7e2014-03-07 09:46:20 -080042 * adr r_base, <table>
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 * ldr r_val, [rARM_SP, v_reg_off]
44 * mov r_idx, #table_size
45 * lp:
buzbee2700f7e2014-03-07 09:46:20 -080046 * ldmia r_base!, {r_key, r_disp}
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 * sub r_idx, #1
48 * cmp r_val, r_key
49 * ifeq
50 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
51 * cbnz r_idx, lp
52 */
Andreas Gampe48971b32014-08-06 10:09:01 -070053void ArmMir2Lir::GenLargeSparseSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070054 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 // Add the table to the list - we'll process it later
56 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000057 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -080058 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 tab_rec->table = table;
60 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -070061 uint32_t size = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +010062 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070063
64 // Get the switch value
65 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080066 RegStorage r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 /* Allocate key and disp temps */
buzbee2700f7e2014-03-07 09:46:20 -080068 RegStorage r_key = AllocTemp();
69 RegStorage r_disp = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 // Make sure r_key's register number is less than r_disp's number for ldmia
buzbee2700f7e2014-03-07 09:46:20 -080071 if (r_key.GetReg() > r_disp.GetReg()) {
72 RegStorage tmp = r_disp;
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 r_disp = r_key;
74 r_key = tmp;
75 }
76 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080077 NewLIR3(kThumb2Adr, r_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 // Set up r_idx
buzbee2700f7e2014-03-07 09:46:20 -080079 RegStorage r_idx = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 LoadConstant(r_idx, size);
81 // Establish loop branch target
82 LIR* target = NewLIR0(kPseudoTargetLabel);
83 // Load next key/disp
buzbee091cc402014-03-31 10:14:40 -070084 NewLIR2(kThumb2LdmiaWB, r_base.GetReg(), (1 << r_key.GetRegNum()) | (1 << r_disp.GetRegNum()));
buzbee2700f7e2014-03-07 09:46:20 -080085 OpRegReg(kOpCmp, r_key, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
Dave Allison3da67a52014-04-02 17:03:45 -070087 LIR* it = OpIT(kCondEq, "");
buzbee2700f7e2014-03-07 09:46:20 -080088 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp.GetReg());
Dave Allison3da67a52014-04-02 17:03:45 -070089 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 tab_rec->anchor = switch_branch;
91 // Needs to use setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +000092 OpRegRegImm(kOpSub, r_idx, r_idx, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +010093 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 OpCondBranch(kCondNe, target);
95}
96
97
Andreas Gampe48971b32014-08-06 10:09:01 -070098void ArmMir2Lir::GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070099 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100 // Add the table to the list - we'll process it later
101 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000102 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800103 tab_rec->switch_mir = mir;
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];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100107 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108
109 // Get the switch value
110 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800111 RegStorage table_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800113 NewLIR3(kThumb2Adr, table_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 int low_key = s4FromSwitchData(&table[2]);
buzbee2700f7e2014-03-07 09:46:20 -0800115 RegStorage keyReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 // Remove the bias, if necessary
117 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800118 keyReg = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 } else {
120 keyReg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800121 OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 }
123 // Bounds check - if < 0 or >= size continue following switch
124 OpRegImm(kOpCmp, keyReg, size-1);
125 LIR* branch_over = OpCondBranch(kCondHi, NULL);
126
127 // Load the displacement from the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800128 RegStorage disp_reg = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700129 LoadBaseIndexed(table_base, keyReg, disp_reg, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130
131 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbee2700f7e2014-03-07 09:46:20 -0800132 LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 tab_rec->anchor = switch_branch;
134
135 /* branch_over target here */
136 LIR* target = NewLIR0(kPseudoTargetLabel);
137 branch_over->target = target;
138}
139
140/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700141 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
142 * details see monitor.cc.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700144void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 FlushAllRegs();
buzbee695d13a2014-04-19 13:32:20 -0700146 // FIXME: need separate LoadValues for object references.
buzbee2700f7e2014-03-07 09:46:20 -0800147 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700149 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
150 if (kArchVariantHasGoodBranchPredictor) {
Dave Allisonf9439142014-03-27 15:10:22 -0700151 LIR* null_check_branch = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700152 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
153 null_check_branch = nullptr; // No null check.
154 } else {
155 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000156 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700157 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
158 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700159 }
buzbee695d13a2014-04-19 13:32:20 -0700160 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700161 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
162 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700163 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800164 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_r1, 0, NULL);
buzbee091cc402014-03-31 10:14:40 -0700165 NewLIR4(kThumb2Strex, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
166 mirror::Object::MonitorOffset().Int32Value() >> 2);
buzbee2700f7e2014-03-07 09:46:20 -0800167 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_r1, 0, NULL);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700168
169
170 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
171 not_unlocked_branch->target = slow_path_target;
172 if (null_check_branch != nullptr) {
173 null_check_branch->target = slow_path_target;
174 }
175 // TODO: move to a slow path.
176 // Go expensive route - artLockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700177 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000178 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800179 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700180 MarkSafepointPC(call_inst);
181
182 LIR* success_target = NewLIR0(kPseudoTargetLabel);
183 lock_success_branch->target = success_target;
Hans Boehm48f5c472014-06-27 14:50:10 -0700184 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700185 } else {
186 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800187 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700188 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700189 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
190 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700191 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800192 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700193 LIR* it = OpIT(kCondEq, "");
buzbee091cc402014-03-31 10:14:40 -0700194 NewLIR4(kThumb2Strex/*eq*/, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
195 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allison3da67a52014-04-02 17:03:45 -0700196 OpEndIT(it);
buzbee2700f7e2014-03-07 09:46:20 -0800197 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700198 it = OpIT(kCondNe, "T");
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700199 // Go expensive route - artLockObjectFromCode(self, obj);
buzbee091cc402014-03-31 10:14:40 -0700200 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(),
201 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000202 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800203 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700204 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700205 MarkSafepointPC(call_inst);
Hans Boehm48f5c472014-06-27 14:50:10 -0700206 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700207 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208}
209
210/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700211 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
212 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
213 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700215void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800217 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 LockCallTemps(); // Prepare for explicit register usage
Dave Allisonf9439142014-03-27 15:10:22 -0700219 LIR* null_check_branch = nullptr;
buzbee695d13a2014-04-19 13:32:20 -0700220 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700221 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
222 if (kArchVariantHasGoodBranchPredictor) {
223 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
224 null_check_branch = nullptr; // No null check.
225 } else {
226 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000227 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700228 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
229 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700230 }
buzbee695d13a2014-04-19 13:32:20 -0700231 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1);
Dave Allisonf9439142014-03-27 15:10:22 -0700232 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800233 LoadConstantNoClobber(rs_r3, 0);
234 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_r1, rs_r2, NULL);
Hans Boehm48f5c472014-06-27 14:50:10 -0700235 GenMemBarrier(kAnyStore);
buzbee695d13a2014-04-19 13:32:20 -0700236 Store32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700237 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
238
239 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
240 slow_unlock_branch->target = slow_path_target;
241 if (null_check_branch != nullptr) {
242 null_check_branch->target = slow_path_target;
243 }
244 // TODO: move to a slow path.
245 // Go expensive route - artUnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700246 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000247 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800248 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700249 MarkSafepointPC(call_inst);
250
251 LIR* success_target = NewLIR0(kPseudoTargetLabel);
252 unlock_success_branch->target = success_target;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700253 } else {
254 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800255 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700256 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock
Dave Allisonb373e092014-02-20 16:06:36 -0800257 MarkPossibleNullPointerException(opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700258 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee2700f7e2014-03-07 09:46:20 -0800259 LoadConstantNoClobber(rs_r3, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700260 // Is lock unheld on lock or held by us (==thread_id) on unlock?
buzbee2700f7e2014-03-07 09:46:20 -0800261 OpRegReg(kOpCmp, rs_r1, rs_r2);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700262
263 LIR* it = OpIT(kCondEq, "EE");
Hans Boehm48f5c472014-06-27 14:50:10 -0700264 if (GenMemBarrier(kAnyStore)) {
Andreas Gampeb14329f2014-05-15 11:16:06 -0700265 UpdateIT(it, "TEE");
266 }
buzbee695d13a2014-04-19 13:32:20 -0700267 Store32Disp/*eq*/(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700268 // Go expensive route - UnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700269 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -0800270 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000271 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800272 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700273 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700274 MarkSafepointPC(call_inst);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700275 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276}
277
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700278void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700279 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700280 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
281 RegStorage reset_reg = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000282 LoadRefDisp(rs_rARM_SELF, ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 LoadConstant(reset_reg, 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000284 StoreRefDisp(rs_rARM_SELF, ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 FreeTemp(reset_reg);
286 StoreValue(rl_dest, rl_result);
287}
288
Vladimir Markobf535be2014-11-19 18:52:35 +0000289void ArmMir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800290 RegStorage reg_card_base = AllocTemp();
291 RegStorage reg_card_no = AllocTemp();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700292 LoadWordDisp(rs_rARM_SELF, Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800294 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 FreeTemp(reg_card_base);
296 FreeTemp(reg_card_no);
297}
298
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700299void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 int spill_count = num_core_spills_ + num_fp_spills_;
301 /*
302 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
303 * mechanism know so it doesn't try to use any of them when
304 * expanding the frame or flushing. This leaves the utility
305 * code with a single temp: r12. This should be enough.
306 */
buzbee091cc402014-03-31 10:14:40 -0700307 LockTemp(rs_r0);
308 LockTemp(rs_r1);
309 LockTemp(rs_r2);
310 LockTemp(rs_r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311
312 /*
313 * We can safely skip the stack overflow check if we're
314 * a leaf *and* our frame size < fudge factor.
315 */
Dave Allison648d7112014-07-25 16:15:27 -0700316 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, kArm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 NewLIR0(kPseudoMethodEntry);
Dave Allison648d7112014-07-25 16:15:27 -0700318 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(kArm);
Andreas Gampe7cd26f32014-06-18 17:01:15 -0700319 bool large_frame = (static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes);
Dave Allison648d7112014-07-25 16:15:27 -0700320 bool generate_explicit_stack_overflow_check = large_frame ||
321 !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700323 if (generate_explicit_stack_overflow_check) {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000324 if (!large_frame) {
325 /* Load stack limit */
326 LockTemp(rs_r12);
327 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
328 }
Dave Allison5cd33752014-04-15 15:57:58 -0700329 } else {
330 // Implicit stack overflow check.
331 // Generate a load from [sp, #-overflowsize]. If this is in the stack
332 // redzone we will get a segmentation fault.
333 //
334 // Caveat coder: if someone changes the kStackOverflowReservedBytes value
335 // we need to make sure that it's loadable in an immediate field of
336 // a sub instruction. Otherwise we will get a temp allocation and the
337 // code size will increase.
338 //
339 // This is done before the callee save instructions to avoid any possibility
340 // of these overflowing. This uses r12 and that's never saved in a callee
341 // save.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700342 OpRegRegImm(kOpSub, rs_r12, rs_rARM_SP, GetStackOverflowReservedBytes(kArm));
Dave Allison5cd33752014-04-15 15:57:58 -0700343 Load32Disp(rs_r12, 0, rs_r12);
344 MarkPossibleStackOverflowException();
Dave Allisonb373e092014-02-20 16:06:36 -0800345 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 }
347 /* Spill core callee saves */
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000348 if (core_spill_mask_ == 0u) {
349 // Nothing to spill.
350 } else if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_LR.GetRegNum()))) == 0u) {
351 // Spilling only low regs and/or LR, use 16-bit PUSH.
352 constexpr int lr_bit_shift = rs_rARM_LR.GetRegNum() - 8;
353 NewLIR1(kThumbPush,
354 (core_spill_mask_ & ~(1u << rs_rARM_LR.GetRegNum())) |
355 ((core_spill_mask_ & (1u << rs_rARM_LR.GetRegNum())) >> lr_bit_shift));
356 } else if (IsPowerOfTwo(core_spill_mask_)) {
357 // kThumb2Push cannot be used to spill a single register.
358 NewLIR1(kThumb2Push1, CTZ(core_spill_mask_));
359 } else {
360 NewLIR1(kThumb2Push, core_spill_mask_);
361 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 /* Need to spill any FP regs? */
363 if (num_fp_spills_) {
364 /*
365 * NOTE: fp spills are a little different from core spills in that
366 * they are pushed as a contiguous block. When promoting from
367 * the fp set, we must allocate all singles from s16..highest-promoted
368 */
369 NewLIR1(kThumb2VPushCS, num_fp_spills_);
370 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700371
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700372 const int spill_size = spill_count * 4;
373 const int frame_size_without_spills = frame_size_ - spill_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700375 if (generate_explicit_stack_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700376 class StackOverflowSlowPath : public LIRSlowPath {
377 public:
378 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, bool restore_lr, size_t sp_displace)
379 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), restore_lr_(restore_lr),
380 sp_displace_(sp_displace) {
381 }
382 void Compile() OVERRIDE {
383 m2l_->ResetRegPool();
384 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700385 GenerateTargetLabel(kPseudoThrowTarget);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700386 if (restore_lr_) {
buzbee2700f7e2014-03-07 09:46:20 -0800387 m2l_->LoadWordDisp(rs_rARM_SP, sp_displace_ - 4, rs_rARM_LR);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700388 }
buzbee2700f7e2014-03-07 09:46:20 -0800389 m2l_->OpRegImm(kOpAdd, rs_rARM_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700390 m2l_->ClobberCallerSave();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700391 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700392 // Load the entrypoint directly into the pc instead of doing a load + branch. Assumes
393 // codegen and target are in thumb2 mode.
buzbee695d13a2014-04-19 13:32:20 -0700394 // NOTE: native pointer.
buzbee2700f7e2014-03-07 09:46:20 -0800395 m2l_->LoadWordDisp(rs_rARM_SELF, func_offset.Int32Value(), rs_rARM_PC);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700396 }
397
398 private:
399 const bool restore_lr_;
400 const size_t sp_displace_;
401 };
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000402 if (large_frame) {
403 // Note: may need a temp reg, and we only have r12 free at this point.
buzbee2700f7e2014-03-07 09:46:20 -0800404 OpRegRegImm(kOpSub, rs_rARM_LR, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000405 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
buzbee2700f7e2014-03-07 09:46:20 -0800406 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_LR, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700407 // Need to restore LR since we used it as a temp.
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700408 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, true, spill_size));
buzbee2700f7e2014-03-07 09:46:20 -0800409 OpRegCopy(rs_rARM_SP, rs_rARM_LR); // Establish stack
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700410 } else {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000411 /*
412 * If the frame is small enough we are guaranteed to have enough space that remains to
413 * handle signals on the user stack. However, we may not have any free temp
414 * registers at this point, so we'll temporarily add LR to the temp pool.
415 */
416 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
417 MarkTemp(rs_rARM_LR);
418 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800419 OpRegRegImm(kOpSub, rs_rARM_SP, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000420 Clobber(rs_rARM_LR);
421 UnmarkTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800422 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_SP, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700423 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, false, frame_size_));
424 }
Dave Allisonb373e092014-02-20 16:06:36 -0800425 } else {
Dave Allison5cd33752014-04-15 15:57:58 -0700426 // Implicit stack overflow check has already been done. Just make room on the
427 // stack for the frame now.
Dave Allisonf9439142014-03-27 15:10:22 -0700428 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Dave Allisonb373e092014-02-20 16:06:36 -0800429 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800431 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 }
433
434 FlushIns(ArgLocs, rl_method);
435
buzbee091cc402014-03-31 10:14:40 -0700436 FreeTemp(rs_r0);
437 FreeTemp(rs_r1);
438 FreeTemp(rs_r2);
439 FreeTemp(rs_r3);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000440 FreeTemp(rs_r12);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441}
442
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700443void ArmMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 int spill_count = num_core_spills_ + num_fp_spills_;
445 /*
446 * In the exit path, r0/r1 are live - make sure they aren't
447 * allocated by the register utilities as temps.
448 */
buzbee091cc402014-03-31 10:14:40 -0700449 LockTemp(rs_r0);
450 LockTemp(rs_r1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451
452 NewLIR0(kPseudoMethodExit);
buzbee2700f7e2014-03-07 09:46:20 -0800453 OpRegImm(kOpAdd, rs_rARM_SP, frame_size_ - (spill_count * 4));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 /* Need to restore any FP callee saves? */
455 if (num_fp_spills_) {
456 NewLIR1(kThumb2VPopCS, num_fp_spills_);
457 }
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000458 if ((core_spill_mask_ & (1 << rs_rARM_LR.GetRegNum())) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 /* Unspill rARM_LR to rARM_PC */
buzbee091cc402014-03-31 10:14:40 -0700460 core_spill_mask_ &= ~(1 << rs_rARM_LR.GetRegNum());
461 core_spill_mask_ |= (1 << rs_rARM_PC.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 }
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000463 if (core_spill_mask_ == 0u) {
464 // Nothing to unspill.
465 } else if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_PC.GetRegNum()))) == 0u) {
466 // Unspilling only low regs and/or PC, use 16-bit POP.
467 constexpr int pc_bit_shift = rs_rARM_PC.GetRegNum() - 8;
468 NewLIR1(kThumbPop,
469 (core_spill_mask_ & ~(1u << rs_rARM_PC.GetRegNum())) |
470 ((core_spill_mask_ & (1u << rs_rARM_PC.GetRegNum())) >> pc_bit_shift));
471 } else if (IsPowerOfTwo(core_spill_mask_)) {
472 // kThumb2Pop cannot be used to unspill a single register.
473 NewLIR1(kThumb2Pop1, CTZ(core_spill_mask_));
474 } else {
475 NewLIR1(kThumb2Pop, core_spill_mask_);
476 }
477 if ((core_spill_mask_ & (1 << rs_rARM_PC.GetRegNum())) == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
buzbee091cc402014-03-31 10:14:40 -0700479 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 }
481}
482
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800483void ArmMir2Lir::GenSpecialExitSequence() {
buzbee091cc402014-03-31 10:14:40 -0700484 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800485}
486
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000487void ArmMir2Lir::GenSpecialEntryForSuspend() {
488 // Keep 16-byte stack alignment - push r0, i.e. ArtMethod*, r5, r6, lr.
489 DCHECK(!IsTemp(rs_r5));
490 DCHECK(!IsTemp(rs_r6));
491 core_spill_mask_ =
492 (1u << rs_r5.GetRegNum()) | (1u << rs_r6.GetRegNum()) | (1u << rs_rARM_LR.GetRegNum());
493 num_core_spills_ = 3u;
494 fp_spill_mask_ = 0u;
495 num_fp_spills_ = 0u;
496 frame_size_ = 16u;
497 core_vmap_table_.clear();
498 fp_vmap_table_.clear();
499 NewLIR1(kThumbPush, (1u << rs_r0.GetRegNum()) | // ArtMethod*
500 (core_spill_mask_ & ~(1u << rs_rARM_LR.GetRegNum())) | // Spills other than LR.
501 (1u << 8)); // LR encoded for 16-bit push.
502}
503
504void ArmMir2Lir::GenSpecialExitForSuspend() {
505 // Pop the frame. (ArtMethod* no longer needed but restore it anyway.)
506 NewLIR1(kThumb2Pop, (1u << rs_r0.GetRegNum()) | core_spill_mask_); // 32-bit because of LR.
507}
508
Vladimir Markof4da6752014-08-01 19:04:18 +0100509static bool ArmUseRelativeCall(CompilationUnit* cu, const MethodReference& target_method) {
510 // Emit relative calls only within a dex file due to the limited range of the BL insn.
511 return cu->dex_file == target_method.dex_file;
512}
513
514/*
515 * Bit of a hack here - in the absence of a real scheduling pass,
516 * emit the next instruction in static & direct invoke sequences.
517 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700518static int ArmNextSDCallInsn(CompilationUnit* cu, CallInfo* info ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100519 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700520 uint32_t unused_idx ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100521 uintptr_t direct_code, uintptr_t direct_method,
522 InvokeType type) {
523 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
524 if (direct_code != 0 && direct_method != 0) {
525 switch (state) {
526 case 0: // Get the current Method* [sets kArg0]
527 if (direct_code != static_cast<uintptr_t>(-1)) {
528 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
529 } else if (ArmUseRelativeCall(cu, target_method)) {
530 // Defer to linker patch.
531 } else {
532 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
533 }
534 if (direct_method != static_cast<uintptr_t>(-1)) {
535 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
536 } else {
537 cg->LoadMethodAddress(target_method, type, kArg0);
538 }
539 break;
540 default:
541 return -1;
542 }
543 } else {
544 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
545 switch (state) {
546 case 0: // Get the current Method* [sets kArg0]
547 // TUNING: we can save a reg copy if Method* has been promoted.
548 cg->LoadCurrMethodDirect(arg0_ref);
549 break;
550 case 1: // Get method->dex_cache_resolved_methods_
551 cg->LoadRefDisp(arg0_ref,
552 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
553 arg0_ref,
554 kNotVolatile);
555 // Set up direct code if known.
556 if (direct_code != 0) {
557 if (direct_code != static_cast<uintptr_t>(-1)) {
558 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
559 } else if (ArmUseRelativeCall(cu, target_method)) {
560 // Defer to linker patch.
561 } else {
562 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
563 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
564 }
565 }
566 break;
567 case 2: // Grab target method*
568 CHECK_EQ(cu->dex_file, target_method.dex_file);
569 cg->LoadRefDisp(arg0_ref,
570 mirror::ObjectArray<mirror::Object>::OffsetOfElement(
571 target_method.dex_method_index).Int32Value(),
572 arg0_ref,
573 kNotVolatile);
574 break;
575 case 3: // Grab the code from the method*
576 if (direct_code == 0) {
577 // kInvokeTgt := arg0_ref->entrypoint
578 cg->LoadWordDisp(arg0_ref,
Mathieu Chartier2d721012014-11-10 11:08:06 -0800579 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
580 kArmPointerSize).Int32Value(), cg->TargetPtrReg(kInvokeTgt));
Vladimir Markof4da6752014-08-01 19:04:18 +0100581 }
582 break;
583 default:
584 return -1;
585 }
586 }
587 return state + 1;
588}
589
590NextCallInsn ArmMir2Lir::GetNextSDCallInsn() {
591 return ArmNextSDCallInsn;
592}
593
594LIR* ArmMir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
595 // For ARM, just generate a relative BL instruction that will be filled in at 'link time'.
596 // If the target turns out to be too far, the linker will generate a thunk for dispatch.
597 int target_method_idx = target_method.dex_method_index;
598 const DexFile* target_dex_file = target_method.dex_file;
599
600 // Generate the call instruction and save index, dex_file, and type.
601 // NOTE: Method deduplication takes linker patches into account, so we can just pass 0
602 // as a placeholder for the offset.
603 LIR* call = RawLIR(current_dalvik_offset_, kThumb2Bl, 0,
604 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
605 AppendLIR(call);
606 call_method_insns_.push_back(call);
607 return call;
608}
609
610LIR* ArmMir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info) {
611 LIR* call_insn;
612 if (method_info.FastPath() && ArmUseRelativeCall(cu_, method_info.GetTargetMethod()) &&
613 (method_info.GetSharpType() == kDirect || method_info.GetSharpType() == kStatic) &&
614 method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
615 call_insn = CallWithLinkerFixup(method_info.GetTargetMethod(), method_info.GetSharpType());
616 } else {
617 call_insn = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
618 }
619 return call_insn;
620}
621
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622} // namespace art