blob: f15b727857a622c4c972d76f8979f41c18262072 [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 if (cu_->verbose) {
56 DumpSparseSwitchTable(table);
57 }
58 // Add the table to the list - we'll process it later
59 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000060 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 tab_rec->table = table;
62 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -070063 uint32_t size = table[1];
buzbee091cc402014-03-31 10:14:40 -070064 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010065 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070066
67 // Get the switch value
68 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080069 RegStorage r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 /* Allocate key and disp temps */
buzbee2700f7e2014-03-07 09:46:20 -080071 RegStorage r_key = AllocTemp();
72 RegStorage r_disp = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 // Make sure r_key's register number is less than r_disp's number for ldmia
buzbee2700f7e2014-03-07 09:46:20 -080074 if (r_key.GetReg() > r_disp.GetReg()) {
75 RegStorage tmp = r_disp;
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 r_disp = r_key;
77 r_key = tmp;
78 }
79 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080080 NewLIR3(kThumb2Adr, r_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 // Set up r_idx
buzbee2700f7e2014-03-07 09:46:20 -080082 RegStorage r_idx = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 LoadConstant(r_idx, size);
84 // Establish loop branch target
85 LIR* target = NewLIR0(kPseudoTargetLabel);
86 // Load next key/disp
buzbee091cc402014-03-31 10:14:40 -070087 NewLIR2(kThumb2LdmiaWB, r_base.GetReg(), (1 << r_key.GetRegNum()) | (1 << r_disp.GetRegNum()));
buzbee2700f7e2014-03-07 09:46:20 -080088 OpRegReg(kOpCmp, r_key, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
Dave Allison3da67a52014-04-02 17:03:45 -070090 LIR* it = OpIT(kCondEq, "");
buzbee2700f7e2014-03-07 09:46:20 -080091 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp.GetReg());
Dave Allison3da67a52014-04-02 17:03:45 -070092 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 tab_rec->anchor = switch_branch;
94 // Needs to use setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +000095 OpRegRegImm(kOpSub, r_idx, r_idx, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +010096 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 OpCondBranch(kCondNe, target);
98}
99
100
Andreas Gampe48971b32014-08-06 10:09:01 -0700101void ArmMir2Lir::GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700102 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103 if (cu_->verbose) {
104 DumpPackedSwitchTable(table);
105 }
106 // Add the table to the list - we'll process it later
107 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000108 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 tab_rec->table = table;
110 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700111 uint32_t size = table[1];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 tab_rec->targets =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000113 static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100114 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115
116 // Get the switch value
117 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800118 RegStorage table_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800120 NewLIR3(kThumb2Adr, table_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 int low_key = s4FromSwitchData(&table[2]);
buzbee2700f7e2014-03-07 09:46:20 -0800122 RegStorage keyReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 // Remove the bias, if necessary
124 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800125 keyReg = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 } else {
127 keyReg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800128 OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 }
130 // Bounds check - if < 0 or >= size continue following switch
131 OpRegImm(kOpCmp, keyReg, size-1);
132 LIR* branch_over = OpCondBranch(kCondHi, NULL);
133
134 // Load the displacement from the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800135 RegStorage disp_reg = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700136 LoadBaseIndexed(table_base, keyReg, disp_reg, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137
138 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbee2700f7e2014-03-07 09:46:20 -0800139 LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 tab_rec->anchor = switch_branch;
141
142 /* branch_over target here */
143 LIR* target = NewLIR0(kPseudoTargetLabel);
144 branch_over->target = target;
145}
146
147/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700148 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
149 * details see monitor.cc.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700151void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 FlushAllRegs();
buzbee695d13a2014-04-19 13:32:20 -0700153 // FIXME: need separate LoadValues for object references.
buzbee2700f7e2014-03-07 09:46:20 -0800154 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700156 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
157 if (kArchVariantHasGoodBranchPredictor) {
Dave Allisonf9439142014-03-27 15:10:22 -0700158 LIR* null_check_branch = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700159 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
160 null_check_branch = nullptr; // No null check.
161 } else {
162 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000163 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700164 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
165 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700166 }
buzbee695d13a2014-04-19 13:32:20 -0700167 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700168 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
169 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700170 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800171 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_r1, 0, NULL);
buzbee091cc402014-03-31 10:14:40 -0700172 NewLIR4(kThumb2Strex, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
173 mirror::Object::MonitorOffset().Int32Value() >> 2);
buzbee2700f7e2014-03-07 09:46:20 -0800174 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_r1, 0, NULL);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700175
176
177 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
178 not_unlocked_branch->target = slow_path_target;
179 if (null_check_branch != nullptr) {
180 null_check_branch->target = slow_path_target;
181 }
182 // TODO: move to a slow path.
183 // Go expensive route - artLockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700184 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000185 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800186 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700187 MarkSafepointPC(call_inst);
188
189 LIR* success_target = NewLIR0(kPseudoTargetLabel);
190 lock_success_branch->target = success_target;
Hans Boehm48f5c472014-06-27 14:50:10 -0700191 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700192 } else {
193 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800194 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700195 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700196 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
197 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700198 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800199 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700200 LIR* it = OpIT(kCondEq, "");
buzbee091cc402014-03-31 10:14:40 -0700201 NewLIR4(kThumb2Strex/*eq*/, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
202 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allison3da67a52014-04-02 17:03:45 -0700203 OpEndIT(it);
buzbee2700f7e2014-03-07 09:46:20 -0800204 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700205 it = OpIT(kCondNe, "T");
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700206 // Go expensive route - artLockObjectFromCode(self, obj);
buzbee091cc402014-03-31 10:14:40 -0700207 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(),
208 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000209 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800210 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700211 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700212 MarkSafepointPC(call_inst);
Hans Boehm48f5c472014-06-27 14:50:10 -0700213 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700214 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215}
216
217/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700218 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
219 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
220 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700222void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800224 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 LockCallTemps(); // Prepare for explicit register usage
Dave Allisonf9439142014-03-27 15:10:22 -0700226 LIR* null_check_branch = nullptr;
buzbee695d13a2014-04-19 13:32:20 -0700227 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700228 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
229 if (kArchVariantHasGoodBranchPredictor) {
230 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
231 null_check_branch = nullptr; // No null check.
232 } else {
233 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000234 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700235 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
236 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700237 }
buzbee695d13a2014-04-19 13:32:20 -0700238 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1);
Dave Allisonf9439142014-03-27 15:10:22 -0700239 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800240 LoadConstantNoClobber(rs_r3, 0);
241 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_r1, rs_r2, NULL);
Hans Boehm48f5c472014-06-27 14:50:10 -0700242 GenMemBarrier(kAnyStore);
buzbee695d13a2014-04-19 13:32:20 -0700243 Store32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700244 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
245
246 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
247 slow_unlock_branch->target = slow_path_target;
248 if (null_check_branch != nullptr) {
249 null_check_branch->target = slow_path_target;
250 }
251 // TODO: move to a slow path.
252 // Go expensive route - artUnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700253 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000254 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800255 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700256 MarkSafepointPC(call_inst);
257
258 LIR* success_target = NewLIR0(kPseudoTargetLabel);
259 unlock_success_branch->target = success_target;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700260 } else {
261 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800262 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700263 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock
Dave Allisonb373e092014-02-20 16:06:36 -0800264 MarkPossibleNullPointerException(opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700265 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee2700f7e2014-03-07 09:46:20 -0800266 LoadConstantNoClobber(rs_r3, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700267 // Is lock unheld on lock or held by us (==thread_id) on unlock?
buzbee2700f7e2014-03-07 09:46:20 -0800268 OpRegReg(kOpCmp, rs_r1, rs_r2);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700269
270 LIR* it = OpIT(kCondEq, "EE");
Hans Boehm48f5c472014-06-27 14:50:10 -0700271 if (GenMemBarrier(kAnyStore)) {
Andreas Gampeb14329f2014-05-15 11:16:06 -0700272 UpdateIT(it, "TEE");
273 }
buzbee695d13a2014-04-19 13:32:20 -0700274 Store32Disp/*eq*/(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700275 // Go expensive route - UnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700276 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -0800277 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000278 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800279 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700280 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700281 MarkSafepointPC(call_inst);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700282 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283}
284
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700285void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700286 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700287 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
288 RegStorage reset_reg = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000289 LoadRefDisp(rs_rARM_SELF, ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 LoadConstant(reset_reg, 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000291 StoreRefDisp(rs_rARM_SELF, ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 FreeTemp(reset_reg);
293 StoreValue(rl_dest, rl_result);
294}
295
Vladimir Markobf535be2014-11-19 18:52:35 +0000296void ArmMir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800297 RegStorage reg_card_base = AllocTemp();
298 RegStorage reg_card_no = AllocTemp();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700299 LoadWordDisp(rs_rARM_SELF, Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800301 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 FreeTemp(reg_card_base);
303 FreeTemp(reg_card_no);
304}
305
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700306void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 int spill_count = num_core_spills_ + num_fp_spills_;
308 /*
309 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
310 * mechanism know so it doesn't try to use any of them when
311 * expanding the frame or flushing. This leaves the utility
312 * code with a single temp: r12. This should be enough.
313 */
buzbee091cc402014-03-31 10:14:40 -0700314 LockTemp(rs_r0);
315 LockTemp(rs_r1);
316 LockTemp(rs_r2);
317 LockTemp(rs_r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318
319 /*
320 * We can safely skip the stack overflow check if we're
321 * a leaf *and* our frame size < fudge factor.
322 */
Dave Allison648d7112014-07-25 16:15:27 -0700323 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, kArm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 NewLIR0(kPseudoMethodEntry);
Dave Allison648d7112014-07-25 16:15:27 -0700325 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(kArm);
Andreas Gampe7cd26f32014-06-18 17:01:15 -0700326 bool large_frame = (static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes);
Dave Allison648d7112014-07-25 16:15:27 -0700327 bool generate_explicit_stack_overflow_check = large_frame ||
328 !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700330 if (generate_explicit_stack_overflow_check) {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000331 if (!large_frame) {
332 /* Load stack limit */
333 LockTemp(rs_r12);
334 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
335 }
Dave Allison5cd33752014-04-15 15:57:58 -0700336 } else {
337 // Implicit stack overflow check.
338 // Generate a load from [sp, #-overflowsize]. If this is in the stack
339 // redzone we will get a segmentation fault.
340 //
341 // Caveat coder: if someone changes the kStackOverflowReservedBytes value
342 // we need to make sure that it's loadable in an immediate field of
343 // a sub instruction. Otherwise we will get a temp allocation and the
344 // code size will increase.
345 //
346 // This is done before the callee save instructions to avoid any possibility
347 // of these overflowing. This uses r12 and that's never saved in a callee
348 // save.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700349 OpRegRegImm(kOpSub, rs_r12, rs_rARM_SP, GetStackOverflowReservedBytes(kArm));
Dave Allison5cd33752014-04-15 15:57:58 -0700350 Load32Disp(rs_r12, 0, rs_r12);
351 MarkPossibleStackOverflowException();
Dave Allisonb373e092014-02-20 16:06:36 -0800352 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 }
354 /* Spill core callee saves */
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000355 if (core_spill_mask_ == 0u) {
356 // Nothing to spill.
357 } else if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_LR.GetRegNum()))) == 0u) {
358 // Spilling only low regs and/or LR, use 16-bit PUSH.
359 constexpr int lr_bit_shift = rs_rARM_LR.GetRegNum() - 8;
360 NewLIR1(kThumbPush,
361 (core_spill_mask_ & ~(1u << rs_rARM_LR.GetRegNum())) |
362 ((core_spill_mask_ & (1u << rs_rARM_LR.GetRegNum())) >> lr_bit_shift));
363 } else if (IsPowerOfTwo(core_spill_mask_)) {
364 // kThumb2Push cannot be used to spill a single register.
365 NewLIR1(kThumb2Push1, CTZ(core_spill_mask_));
366 } else {
367 NewLIR1(kThumb2Push, core_spill_mask_);
368 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 /* Need to spill any FP regs? */
370 if (num_fp_spills_) {
371 /*
372 * NOTE: fp spills are a little different from core spills in that
373 * they are pushed as a contiguous block. When promoting from
374 * the fp set, we must allocate all singles from s16..highest-promoted
375 */
376 NewLIR1(kThumb2VPushCS, num_fp_spills_);
377 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700378
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700379 const int spill_size = spill_count * 4;
380 const int frame_size_without_spills = frame_size_ - spill_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700382 if (generate_explicit_stack_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700383 class StackOverflowSlowPath : public LIRSlowPath {
384 public:
385 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, bool restore_lr, size_t sp_displace)
386 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), restore_lr_(restore_lr),
387 sp_displace_(sp_displace) {
388 }
389 void Compile() OVERRIDE {
390 m2l_->ResetRegPool();
391 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700392 GenerateTargetLabel(kPseudoThrowTarget);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700393 if (restore_lr_) {
buzbee2700f7e2014-03-07 09:46:20 -0800394 m2l_->LoadWordDisp(rs_rARM_SP, sp_displace_ - 4, rs_rARM_LR);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700395 }
buzbee2700f7e2014-03-07 09:46:20 -0800396 m2l_->OpRegImm(kOpAdd, rs_rARM_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700397 m2l_->ClobberCallerSave();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700398 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700399 // Load the entrypoint directly into the pc instead of doing a load + branch. Assumes
400 // codegen and target are in thumb2 mode.
buzbee695d13a2014-04-19 13:32:20 -0700401 // NOTE: native pointer.
buzbee2700f7e2014-03-07 09:46:20 -0800402 m2l_->LoadWordDisp(rs_rARM_SELF, func_offset.Int32Value(), rs_rARM_PC);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700403 }
404
405 private:
406 const bool restore_lr_;
407 const size_t sp_displace_;
408 };
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000409 if (large_frame) {
410 // Note: may need a temp reg, and we only have r12 free at this point.
buzbee2700f7e2014-03-07 09:46:20 -0800411 OpRegRegImm(kOpSub, rs_rARM_LR, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000412 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
buzbee2700f7e2014-03-07 09:46:20 -0800413 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_LR, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700414 // Need to restore LR since we used it as a temp.
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700415 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, true, spill_size));
buzbee2700f7e2014-03-07 09:46:20 -0800416 OpRegCopy(rs_rARM_SP, rs_rARM_LR); // Establish stack
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700417 } else {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000418 /*
419 * If the frame is small enough we are guaranteed to have enough space that remains to
420 * handle signals on the user stack. However, we may not have any free temp
421 * registers at this point, so we'll temporarily add LR to the temp pool.
422 */
423 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
424 MarkTemp(rs_rARM_LR);
425 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800426 OpRegRegImm(kOpSub, rs_rARM_SP, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000427 Clobber(rs_rARM_LR);
428 UnmarkTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800429 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_SP, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700430 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, false, frame_size_));
431 }
Dave Allisonb373e092014-02-20 16:06:36 -0800432 } else {
Dave Allison5cd33752014-04-15 15:57:58 -0700433 // Implicit stack overflow check has already been done. Just make room on the
434 // stack for the frame now.
Dave Allisonf9439142014-03-27 15:10:22 -0700435 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Dave Allisonb373e092014-02-20 16:06:36 -0800436 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800438 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 }
440
441 FlushIns(ArgLocs, rl_method);
442
buzbee091cc402014-03-31 10:14:40 -0700443 FreeTemp(rs_r0);
444 FreeTemp(rs_r1);
445 FreeTemp(rs_r2);
446 FreeTemp(rs_r3);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000447 FreeTemp(rs_r12);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448}
449
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700450void ArmMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 int spill_count = num_core_spills_ + num_fp_spills_;
452 /*
453 * In the exit path, r0/r1 are live - make sure they aren't
454 * allocated by the register utilities as temps.
455 */
buzbee091cc402014-03-31 10:14:40 -0700456 LockTemp(rs_r0);
457 LockTemp(rs_r1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458
459 NewLIR0(kPseudoMethodExit);
buzbee2700f7e2014-03-07 09:46:20 -0800460 OpRegImm(kOpAdd, rs_rARM_SP, frame_size_ - (spill_count * 4));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 /* Need to restore any FP callee saves? */
462 if (num_fp_spills_) {
463 NewLIR1(kThumb2VPopCS, num_fp_spills_);
464 }
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000465 if ((core_spill_mask_ & (1 << rs_rARM_LR.GetRegNum())) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 /* Unspill rARM_LR to rARM_PC */
buzbee091cc402014-03-31 10:14:40 -0700467 core_spill_mask_ &= ~(1 << rs_rARM_LR.GetRegNum());
468 core_spill_mask_ |= (1 << rs_rARM_PC.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 }
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000470 if (core_spill_mask_ == 0u) {
471 // Nothing to unspill.
472 } else if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_PC.GetRegNum()))) == 0u) {
473 // Unspilling only low regs and/or PC, use 16-bit POP.
474 constexpr int pc_bit_shift = rs_rARM_PC.GetRegNum() - 8;
475 NewLIR1(kThumbPop,
476 (core_spill_mask_ & ~(1u << rs_rARM_PC.GetRegNum())) |
477 ((core_spill_mask_ & (1u << rs_rARM_PC.GetRegNum())) >> pc_bit_shift));
478 } else if (IsPowerOfTwo(core_spill_mask_)) {
479 // kThumb2Pop cannot be used to unspill a single register.
480 NewLIR1(kThumb2Pop1, CTZ(core_spill_mask_));
481 } else {
482 NewLIR1(kThumb2Pop, core_spill_mask_);
483 }
484 if ((core_spill_mask_ & (1 << rs_rARM_PC.GetRegNum())) == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
buzbee091cc402014-03-31 10:14:40 -0700486 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 }
488}
489
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800490void ArmMir2Lir::GenSpecialExitSequence() {
buzbee091cc402014-03-31 10:14:40 -0700491 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800492}
493
Vladimir Markof4da6752014-08-01 19:04:18 +0100494static bool ArmUseRelativeCall(CompilationUnit* cu, const MethodReference& target_method) {
495 // Emit relative calls only within a dex file due to the limited range of the BL insn.
496 return cu->dex_file == target_method.dex_file;
497}
498
499/*
500 * Bit of a hack here - in the absence of a real scheduling pass,
501 * emit the next instruction in static & direct invoke sequences.
502 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700503static int ArmNextSDCallInsn(CompilationUnit* cu, CallInfo* info ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100504 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700505 uint32_t unused_idx ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100506 uintptr_t direct_code, uintptr_t direct_method,
507 InvokeType type) {
508 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
509 if (direct_code != 0 && direct_method != 0) {
510 switch (state) {
511 case 0: // Get the current Method* [sets kArg0]
512 if (direct_code != static_cast<uintptr_t>(-1)) {
513 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
514 } else if (ArmUseRelativeCall(cu, target_method)) {
515 // Defer to linker patch.
516 } else {
517 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
518 }
519 if (direct_method != static_cast<uintptr_t>(-1)) {
520 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
521 } else {
522 cg->LoadMethodAddress(target_method, type, kArg0);
523 }
524 break;
525 default:
526 return -1;
527 }
528 } else {
529 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
530 switch (state) {
531 case 0: // Get the current Method* [sets kArg0]
532 // TUNING: we can save a reg copy if Method* has been promoted.
533 cg->LoadCurrMethodDirect(arg0_ref);
534 break;
535 case 1: // Get method->dex_cache_resolved_methods_
536 cg->LoadRefDisp(arg0_ref,
537 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
538 arg0_ref,
539 kNotVolatile);
540 // Set up direct code if known.
541 if (direct_code != 0) {
542 if (direct_code != static_cast<uintptr_t>(-1)) {
543 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
544 } else if (ArmUseRelativeCall(cu, target_method)) {
545 // Defer to linker patch.
546 } else {
547 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
548 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
549 }
550 }
551 break;
552 case 2: // Grab target method*
553 CHECK_EQ(cu->dex_file, target_method.dex_file);
554 cg->LoadRefDisp(arg0_ref,
555 mirror::ObjectArray<mirror::Object>::OffsetOfElement(
556 target_method.dex_method_index).Int32Value(),
557 arg0_ref,
558 kNotVolatile);
559 break;
560 case 3: // Grab the code from the method*
561 if (direct_code == 0) {
562 // kInvokeTgt := arg0_ref->entrypoint
563 cg->LoadWordDisp(arg0_ref,
Mathieu Chartier2d721012014-11-10 11:08:06 -0800564 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
565 kArmPointerSize).Int32Value(), cg->TargetPtrReg(kInvokeTgt));
Vladimir Markof4da6752014-08-01 19:04:18 +0100566 }
567 break;
568 default:
569 return -1;
570 }
571 }
572 return state + 1;
573}
574
575NextCallInsn ArmMir2Lir::GetNextSDCallInsn() {
576 return ArmNextSDCallInsn;
577}
578
579LIR* ArmMir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
580 // For ARM, just generate a relative BL instruction that will be filled in at 'link time'.
581 // If the target turns out to be too far, the linker will generate a thunk for dispatch.
582 int target_method_idx = target_method.dex_method_index;
583 const DexFile* target_dex_file = target_method.dex_file;
584
585 // Generate the call instruction and save index, dex_file, and type.
586 // NOTE: Method deduplication takes linker patches into account, so we can just pass 0
587 // as a placeholder for the offset.
588 LIR* call = RawLIR(current_dalvik_offset_, kThumb2Bl, 0,
589 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
590 AppendLIR(call);
591 call_method_insns_.push_back(call);
592 return call;
593}
594
595LIR* ArmMir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info) {
596 LIR* call_insn;
597 if (method_info.FastPath() && ArmUseRelativeCall(cu_, method_info.GetTargetMethod()) &&
598 (method_info.GetSharpType() == kDirect || method_info.GetSharpType() == kStatic) &&
599 method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
600 call_insn = CallWithLinkerFixup(method_info.GetTargetMethod(), method_info.GetSharpType());
601 } else {
602 call_insn = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
603 }
604 return call_insn;
605}
606
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607} // namespace art