Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 22 | #include "entrypoints/quick/quick_entrypoints.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | /* |
| 27 | * The sparse table in the literal pool is an array of <key,displacement> |
| 28 | * pairs. For each set, we'll load them as a pair using ldmia. |
| 29 | * This means that the register number of the temp we use for the key |
| 30 | * must be lower than the reg for the displacement. |
| 31 | * |
| 32 | * The test loop will look something like: |
| 33 | * |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 34 | * adr r_base, <table> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 35 | * ldr r_val, [rARM_SP, v_reg_off] |
| 36 | * mov r_idx, #table_size |
| 37 | * lp: |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 38 | * ldmia r_base!, {r_key, r_disp} |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 39 | * sub r_idx, #1 |
| 40 | * cmp r_val, r_key |
| 41 | * ifeq |
| 42 | * add rARM_PC, r_disp ; This is the branch from which we compute displacement |
| 43 | * cbnz r_idx, lp |
| 44 | */ |
| 45 | void ArmMir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 46 | RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 47 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 48 | if (cu_->verbose) { |
| 49 | DumpSparseSwitchTable(table); |
| 50 | } |
| 51 | // Add the table to the list - we'll process it later |
| 52 | SwitchTable *tab_rec = |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 53 | static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 54 | tab_rec->table = table; |
| 55 | tab_rec->vaddr = current_dalvik_offset_; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 56 | uint32_t size = table[1]; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 57 | tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 58 | kArenaAllocLIR)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 59 | switch_tables_.Insert(tab_rec); |
| 60 | |
| 61 | // Get the switch value |
| 62 | rl_src = LoadValue(rl_src, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 63 | RegStorage r_base = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 64 | /* Allocate key and disp temps */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 65 | RegStorage r_key = AllocTemp(); |
| 66 | RegStorage r_disp = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 67 | // Make sure r_key's register number is less than r_disp's number for ldmia |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 68 | if (r_key.GetReg() > r_disp.GetReg()) { |
| 69 | RegStorage tmp = r_disp; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 70 | r_disp = r_key; |
| 71 | r_key = tmp; |
| 72 | } |
| 73 | // Materialize a pointer to the switch table |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 74 | NewLIR3(kThumb2Adr, r_base.GetReg(), 0, WrapPointer(tab_rec)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 75 | // Set up r_idx |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 76 | RegStorage r_idx = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 77 | LoadConstant(r_idx, size); |
| 78 | // Establish loop branch target |
| 79 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 80 | // Load next key/disp |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 81 | NewLIR2(kThumb2LdmiaWB, r_base.GetReg(), (1 << r_key.GetReg()) | (1 << r_disp.GetReg())); |
| 82 | OpRegReg(kOpCmp, r_key, rl_src.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 83 | // Go if match. NOTE: No instruction set switch here - must stay Thumb2 |
| 84 | OpIT(kCondEq, ""); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 85 | LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp.GetReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 86 | tab_rec->anchor = switch_branch; |
| 87 | // Needs to use setflags encoding here |
Vladimir Marko | dbb8c49 | 2014-02-28 17:36:39 +0000 | [diff] [blame] | 88 | OpRegRegImm(kOpSub, r_idx, r_idx, 1); // For value == 1, this should set flags. |
| 89 | DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 90 | OpCondBranch(kCondNe, target); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void ArmMir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 95 | RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 96 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 97 | if (cu_->verbose) { |
| 98 | DumpPackedSwitchTable(table); |
| 99 | } |
| 100 | // Add the table to the list - we'll process it later |
| 101 | SwitchTable *tab_rec = |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 102 | static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 103 | tab_rec->table = table; |
| 104 | tab_rec->vaddr = current_dalvik_offset_; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 105 | uint32_t size = table[1]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 106 | tab_rec->targets = |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 107 | static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 108 | switch_tables_.Insert(tab_rec); |
| 109 | |
| 110 | // Get the switch value |
| 111 | rl_src = LoadValue(rl_src, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 112 | RegStorage table_base = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 113 | // Materialize a pointer to the switch table |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 114 | NewLIR3(kThumb2Adr, table_base.GetReg(), 0, WrapPointer(tab_rec)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 115 | int low_key = s4FromSwitchData(&table[2]); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 116 | RegStorage keyReg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 117 | // Remove the bias, if necessary |
| 118 | if (low_key == 0) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 119 | keyReg = rl_src.reg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 120 | } else { |
| 121 | keyReg = AllocTemp(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 122 | OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 123 | } |
| 124 | // Bounds check - if < 0 or >= size continue following switch |
| 125 | OpRegImm(kOpCmp, keyReg, size-1); |
| 126 | LIR* branch_over = OpCondBranch(kCondHi, NULL); |
| 127 | |
| 128 | // Load the displacement from the switch table |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 129 | RegStorage disp_reg = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 130 | LoadBaseIndexed(table_base, keyReg, disp_reg, 2, kWord); |
| 131 | |
| 132 | // ..and go! NOTE: No instruction set switch here - must stay Thumb2 |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 133 | LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg.GetReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 134 | tab_rec->anchor = switch_branch; |
| 135 | |
| 136 | /* branch_over target here */ |
| 137 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 138 | branch_over->target = target; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Array data table format: |
| 143 | * ushort ident = 0x0300 magic value |
| 144 | * ushort width width of each element in the table |
| 145 | * uint size number of elements in the table |
| 146 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 147 | * padding at the end) |
| 148 | * |
| 149 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 150 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 151 | void ArmMir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 152 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 153 | // Add the table to the list - we'll process it later |
| 154 | FillArrayData *tab_rec = |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 155 | static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), kArenaAllocData)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 156 | tab_rec->table = table; |
| 157 | tab_rec->vaddr = current_dalvik_offset_; |
| 158 | uint16_t width = tab_rec->table[1]; |
| 159 | uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16); |
| 160 | tab_rec->size = (size * width) + 8; |
| 161 | |
| 162 | fill_array_data_.Insert(tab_rec); |
| 163 | |
| 164 | // Making a call - use explicit registers |
| 165 | FlushAllRegs(); /* Everything to home location */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 166 | LoadValueDirectFixed(rl_src, rs_r0); |
| 167 | LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pHandleFillArrayData).Int32Value(), |
| 168 | rs_rARM_LR); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 169 | // Materialize a pointer to the fill data image |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 170 | NewLIR3(kThumb2Adr, r1, 0, WrapPointer(tab_rec)); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 171 | ClobberCallerSave(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 172 | LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 173 | MarkSafepointPC(call_inst); |
| 174 | } |
| 175 | |
| 176 | /* |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 177 | * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more |
| 178 | * details see monitor.cc. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 179 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 180 | void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 181 | FlushAllRegs(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 182 | LoadValueDirectFixed(rl_src, rs_r0); // Get obj |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 183 | LockCallTemps(); // Prepare for explicit register usage |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 184 | constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15. |
| 185 | if (kArchVariantHasGoodBranchPredictor) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 186 | LIR* null_check_branch = nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 187 | if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) { |
| 188 | null_check_branch = nullptr; // No null check. |
| 189 | } else { |
| 190 | // If the null-check fails its handled by the slow-path to reduce exception related meta-data. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 191 | if (Runtime::Current()->ExplicitNullChecks()) { |
| 192 | null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL); |
| 193 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 194 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 195 | LoadWordDisp(rs_rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), rs_r2); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 196 | NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 197 | MarkPossibleNullPointerException(opt_flags); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 198 | LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_r1, 0, NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 199 | NewLIR4(kThumb2Strex, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 200 | LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_r1, 0, NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 201 | |
| 202 | |
| 203 | LIR* slow_path_target = NewLIR0(kPseudoTargetLabel); |
| 204 | not_unlocked_branch->target = slow_path_target; |
| 205 | if (null_check_branch != nullptr) { |
| 206 | null_check_branch->target = slow_path_target; |
| 207 | } |
| 208 | // TODO: move to a slow path. |
| 209 | // Go expensive route - artLockObjectFromCode(obj); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 210 | LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pLockObject).Int32Value(), rs_rARM_LR); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 211 | ClobberCallerSave(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 212 | LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 213 | MarkSafepointPC(call_inst); |
| 214 | |
| 215 | LIR* success_target = NewLIR0(kPseudoTargetLabel); |
| 216 | lock_success_branch->target = success_target; |
| 217 | GenMemBarrier(kLoadLoad); |
| 218 | } else { |
| 219 | // Explicit null-check as slow-path is entered using an IT. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 220 | GenNullCheck(rs_r0, opt_flags); |
| 221 | LoadWordDisp(rs_rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), rs_r2); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 222 | NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 223 | MarkPossibleNullPointerException(opt_flags); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 224 | OpRegImm(kOpCmp, rs_r1, 0); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 225 | OpIT(kCondEq, ""); |
| 226 | NewLIR4(kThumb2Strex/*eq*/, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 227 | OpRegImm(kOpCmp, rs_r1, 0); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 228 | OpIT(kCondNe, "T"); |
| 229 | // Go expensive route - artLockObjectFromCode(self, obj); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 230 | LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pLockObject).Int32Value(), rs_rARM_LR); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 231 | ClobberCallerSave(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 232 | LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 233 | MarkSafepointPC(call_inst); |
| 234 | GenMemBarrier(kLoadLoad); |
| 235 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 239 | * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more |
| 240 | * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock |
| 241 | * and can only give away ownership if its suspended. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 242 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 243 | void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 244 | FlushAllRegs(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 245 | LoadValueDirectFixed(rl_src, rs_r0); // Get obj |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 246 | LockCallTemps(); // Prepare for explicit register usage |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 247 | LIR* null_check_branch = nullptr; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 248 | LoadWordDisp(rs_rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), rs_r2); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 249 | constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15. |
| 250 | if (kArchVariantHasGoodBranchPredictor) { |
| 251 | if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) { |
| 252 | null_check_branch = nullptr; // No null check. |
| 253 | } else { |
| 254 | // If the null-check fails its handled by the slow-path to reduce exception related meta-data. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 255 | if (Runtime::Current()->ExplicitNullChecks()) { |
| 256 | null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL); |
| 257 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 258 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 259 | LoadWordDisp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 260 | MarkPossibleNullPointerException(opt_flags); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 261 | LoadConstantNoClobber(rs_r3, 0); |
| 262 | LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_r1, rs_r2, NULL); |
| 263 | StoreWordDisp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 264 | LIR* unlock_success_branch = OpUnconditionalBranch(NULL); |
| 265 | |
| 266 | LIR* slow_path_target = NewLIR0(kPseudoTargetLabel); |
| 267 | slow_unlock_branch->target = slow_path_target; |
| 268 | if (null_check_branch != nullptr) { |
| 269 | null_check_branch->target = slow_path_target; |
| 270 | } |
| 271 | // TODO: move to a slow path. |
| 272 | // Go expensive route - artUnlockObjectFromCode(obj); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 273 | LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pUnlockObject).Int32Value(), rs_rARM_LR); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 274 | ClobberCallerSave(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 275 | LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 276 | MarkSafepointPC(call_inst); |
| 277 | |
| 278 | LIR* success_target = NewLIR0(kPseudoTargetLabel); |
| 279 | unlock_success_branch->target = success_target; |
| 280 | GenMemBarrier(kStoreLoad); |
| 281 | } else { |
| 282 | // Explicit null-check as slow-path is entered using an IT. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 283 | GenNullCheck(rs_r0, opt_flags); |
| 284 | LoadWordDisp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 285 | MarkPossibleNullPointerException(opt_flags); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 286 | LoadWordDisp(rs_rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), rs_r2); |
| 287 | LoadConstantNoClobber(rs_r3, 0); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 288 | // Is lock unheld on lock or held by us (==thread_id) on unlock? |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 289 | OpRegReg(kOpCmp, rs_r1, rs_r2); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 290 | OpIT(kCondEq, "EE"); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 291 | StoreWordDisp/*eq*/(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 292 | // Go expensive route - UnlockObjectFromCode(obj); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 293 | LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pUnlockObject).Int32Value(), |
| 294 | rs_rARM_LR); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 295 | ClobberCallerSave(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 296 | LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 297 | MarkSafepointPC(call_inst); |
| 298 | GenMemBarrier(kStoreLoad); |
| 299 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 302 | void ArmMir2Lir::GenMoveException(RegLocation rl_dest) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 303 | int ex_offset = Thread::ExceptionOffset().Int32Value(); |
| 304 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 305 | RegStorage reset_reg = AllocTemp(); |
| 306 | LoadWordDisp(rs_rARM_SELF, ex_offset, rl_result.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 307 | LoadConstant(reset_reg, 0); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 308 | StoreWordDisp(rs_rARM_SELF, ex_offset, reset_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 309 | FreeTemp(reset_reg); |
| 310 | StoreValue(rl_dest, rl_result); |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Mark garbage collection card. Skip if the value we're storing is null. |
| 315 | */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 316 | void ArmMir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) { |
| 317 | RegStorage reg_card_base = AllocTemp(); |
| 318 | RegStorage reg_card_no = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 319 | LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 320 | LoadWordDisp(rs_rARM_SELF, Thread::CardTableOffset().Int32Value(), reg_card_base); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 321 | OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 322 | StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 323 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 324 | branch_over->target = target; |
| 325 | FreeTemp(reg_card_base); |
| 326 | FreeTemp(reg_card_no); |
| 327 | } |
| 328 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 329 | void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 330 | int spill_count = num_core_spills_ + num_fp_spills_; |
| 331 | /* |
| 332 | * On entry, r0, r1, r2 & r3 are live. Let the register allocation |
| 333 | * mechanism know so it doesn't try to use any of them when |
| 334 | * expanding the frame or flushing. This leaves the utility |
| 335 | * code with a single temp: r12. This should be enough. |
| 336 | */ |
| 337 | LockTemp(r0); |
| 338 | LockTemp(r1); |
| 339 | LockTemp(r2); |
| 340 | LockTemp(r3); |
| 341 | |
| 342 | /* |
| 343 | * We can safely skip the stack overflow check if we're |
| 344 | * a leaf *and* our frame size < fudge factor. |
| 345 | */ |
| 346 | bool skip_overflow_check = (mir_graph_->MethodIsLeaf() && |
| 347 | (static_cast<size_t>(frame_size_) < |
| 348 | Thread::kStackOverflowReservedBytes)); |
| 349 | NewLIR0(kPseudoMethodEntry); |
| 350 | if (!skip_overflow_check) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 351 | if (Runtime::Current()->ExplicitStackOverflowChecks()) { |
| 352 | /* Load stack limit */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 353 | LoadWordDisp(rs_rARM_SELF, Thread::StackEndOffset().Int32Value(), rs_r12); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 354 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 355 | } |
| 356 | /* Spill core callee saves */ |
| 357 | NewLIR1(kThumb2Push, core_spill_mask_); |
| 358 | /* Need to spill any FP regs? */ |
| 359 | if (num_fp_spills_) { |
| 360 | /* |
| 361 | * NOTE: fp spills are a little different from core spills in that |
| 362 | * they are pushed as a contiguous block. When promoting from |
| 363 | * the fp set, we must allocate all singles from s16..highest-promoted |
| 364 | */ |
| 365 | NewLIR1(kThumb2VPushCS, num_fp_spills_); |
| 366 | } |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 367 | |
| 368 | // TODO: 64 bit will be different code. |
| 369 | const int frame_size_without_spills = frame_size_ - spill_count * 4; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 370 | if (!skip_overflow_check) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 371 | if (Runtime::Current()->ExplicitStackOverflowChecks()) { |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 372 | class StackOverflowSlowPath : public LIRSlowPath { |
| 373 | public: |
| 374 | StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, bool restore_lr, size_t sp_displace) |
| 375 | : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), restore_lr_(restore_lr), |
| 376 | sp_displace_(sp_displace) { |
| 377 | } |
| 378 | void Compile() OVERRIDE { |
| 379 | m2l_->ResetRegPool(); |
| 380 | m2l_->ResetDefTracking(); |
| 381 | GenerateTargetLabel(); |
| 382 | if (restore_lr_) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 383 | m2l_->LoadWordDisp(rs_rARM_SP, sp_displace_ - 4, rs_rARM_LR); |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 384 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 385 | m2l_->OpRegImm(kOpAdd, rs_rARM_SP, sp_displace_); |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 386 | m2l_->ClobberCallerSave(); |
| 387 | ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowStackOverflow); |
| 388 | // Load the entrypoint directly into the pc instead of doing a load + branch. Assumes |
| 389 | // codegen and target are in thumb2 mode. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 390 | m2l_->LoadWordDisp(rs_rARM_SELF, func_offset.Int32Value(), rs_rARM_PC); |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | private: |
| 394 | const bool restore_lr_; |
| 395 | const size_t sp_displace_; |
| 396 | }; |
| 397 | if (static_cast<size_t>(frame_size_) > Thread::kStackOverflowReservedUsableBytes) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 398 | OpRegRegImm(kOpSub, rs_rARM_LR, rs_rARM_SP, frame_size_without_spills); |
| 399 | LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_LR, rs_r12, nullptr); |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 400 | // Need to restore LR since we used it as a temp. |
| 401 | AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, true, |
| 402 | frame_size_without_spills)); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 403 | OpRegCopy(rs_rARM_SP, rs_rARM_LR); // Establish stack |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 404 | } else { |
| 405 | // If the frame is small enough we are guaranteed to have enough space that remains to |
| 406 | // handle signals on the user stack. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 407 | OpRegRegImm(kOpSub, rs_rARM_SP, rs_rARM_SP, frame_size_without_spills); |
| 408 | LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_SP, rs_r12, nullptr); |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 409 | AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, false, frame_size_)); |
| 410 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 411 | } else { |
| 412 | // Implicit stack overflow check. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 413 | // Generate a load from [sp, #-overflowsize]. If this is in the stack |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 414 | // redzone we will get a segmentation fault. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 415 | // |
| 416 | // Caveat coder: if someone changes the kStackOverflowReservedBytes value |
| 417 | // we need to make sure that it's loadable in an immediate field of |
| 418 | // a sub instruction. Otherwise we will get a temp allocation and the |
| 419 | // code size will increase. |
| 420 | OpRegRegImm(kOpSub, rs_r12, rs_rARM_SP, Thread::kStackOverflowReservedBytes); |
| 421 | LoadWordDisp(rs_r12, 0, rs_r12); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 422 | MarkPossibleStackOverflowException(); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame^] | 423 | OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 424 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 425 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 426 | OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | FlushIns(ArgLocs, rl_method); |
| 430 | |
| 431 | FreeTemp(r0); |
| 432 | FreeTemp(r1); |
| 433 | FreeTemp(r2); |
| 434 | FreeTemp(r3); |
| 435 | } |
| 436 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 437 | void ArmMir2Lir::GenExitSequence() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 438 | int spill_count = num_core_spills_ + num_fp_spills_; |
| 439 | /* |
| 440 | * In the exit path, r0/r1 are live - make sure they aren't |
| 441 | * allocated by the register utilities as temps. |
| 442 | */ |
| 443 | LockTemp(r0); |
| 444 | LockTemp(r1); |
| 445 | |
| 446 | NewLIR0(kPseudoMethodExit); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 447 | OpRegImm(kOpAdd, rs_rARM_SP, frame_size_ - (spill_count * 4)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 448 | /* Need to restore any FP callee saves? */ |
| 449 | if (num_fp_spills_) { |
| 450 | NewLIR1(kThumb2VPopCS, num_fp_spills_); |
| 451 | } |
| 452 | if (core_spill_mask_ & (1 << rARM_LR)) { |
| 453 | /* Unspill rARM_LR to rARM_PC */ |
| 454 | core_spill_mask_ &= ~(1 << rARM_LR); |
| 455 | core_spill_mask_ |= (1 << rARM_PC); |
| 456 | } |
| 457 | NewLIR1(kThumb2Pop, core_spill_mask_); |
| 458 | if (!(core_spill_mask_ & (1 << rARM_PC))) { |
| 459 | /* We didn't pop to rARM_PC, so must do a bv rARM_LR */ |
| 460 | NewLIR1(kThumbBx, rARM_LR); |
| 461 | } |
| 462 | } |
| 463 | |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 464 | void ArmMir2Lir::GenSpecialExitSequence() { |
| 465 | NewLIR1(kThumbBx, rARM_LR); |
| 466 | } |
| 467 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 468 | } // namespace art |