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" |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 21 | #include "dex/quick/dex_file_method_inliner.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | #include "dex/quick/mir_to_lir-inl.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 23 | #include "entrypoints/quick/quick_entrypoints.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 27 | // TODO: generalize & move to RegUtil.cc |
| 28 | // The number of dalvik registers passed in core registers. |
| 29 | constexpr int kInArgsInCoreRegs = 3; |
| 30 | // The core register corresponding to the first (index 0) input argument. |
| 31 | constexpr int kInArg0CoreReg = r1; // r0 is Method*. |
| 32 | // Offset, in words, for getting args from stack (even core reg args have space on stack). |
| 33 | constexpr int kInArgToStackOffset = 1; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 35 | /* Lock argument if it's in register. */ |
| 36 | void ArmMir2Lir::LockArg(int in_position, bool wide) { |
| 37 | if (in_position < kInArgsInCoreRegs) { |
| 38 | LockTemp(kInArg0CoreReg + in_position); |
| 39 | } |
| 40 | if (wide && in_position + 1 < kInArgsInCoreRegs) { |
| 41 | LockTemp(kInArg0CoreReg + in_position + 1); |
| 42 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 45 | /* Load argument into register. LockArg(in_position, wide) must have been previously called. */ |
| 46 | int ArmMir2Lir::LoadArg(int in_position, bool wide) { |
| 47 | if (in_position < kInArgsInCoreRegs) { |
| 48 | int low_reg = kInArg0CoreReg + in_position; |
| 49 | if (!wide) { |
| 50 | return low_reg; |
| 51 | } |
| 52 | int high_reg = (in_position != kInArgsInCoreRegs - 1) ? low_reg + 1 : LoadArg(in_position + 1); |
| 53 | return (low_reg & 0xff) | ((high_reg & 0xff) << 8); |
| 54 | } |
| 55 | int low_reg = AllocTemp(); |
| 56 | int offset = (in_position + kInArgToStackOffset) * sizeof(uint32_t); |
| 57 | if (!wide) { |
| 58 | LoadWordDisp(rARM_SP, offset, low_reg); |
| 59 | return low_reg; |
| 60 | } |
| 61 | int high_reg = AllocTemp(); |
| 62 | LoadBaseDispWide(rARM_SP, offset, low_reg, high_reg, INVALID_SREG); |
| 63 | return (low_reg & 0xff) | ((high_reg & 0xff) << 8); |
| 64 | } |
| 65 | |
| 66 | void ArmMir2Lir::LoadArgDirect(int in_position, RegLocation rl_dest) { |
| 67 | int reg = kInArg0CoreReg + in_position; |
| 68 | int offset = (in_position + kInArgToStackOffset) * sizeof(uint32_t); |
| 69 | if (!rl_dest.wide) { |
| 70 | if (in_position < kInArgsInCoreRegs) { |
| 71 | OpRegCopy(rl_dest.low_reg, reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 72 | } else { |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 73 | LoadWordDisp(rARM_SP, offset, rl_dest.low_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 74 | } |
| 75 | } else { |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 76 | if (in_position < kInArgsInCoreRegs - 1) { |
| 77 | OpRegCopyWide(rl_dest.low_reg, rl_dest.high_reg, reg, reg + 1); |
| 78 | } else if (in_position == kInArgsInCoreRegs - 1) { |
| 79 | OpRegCopy(rl_dest.low_reg, reg); |
| 80 | LoadWordDisp(rARM_SP, offset + sizeof(uint32_t), rl_dest.high_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 81 | } else { |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 82 | LoadBaseDispWide(rARM_SP, offset, rl_dest.low_reg, rl_dest.high_reg, INVALID_SREG); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* Find the next MIR, which may be in a following basic block */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 88 | // TODO: make this a utility in mir_graph. |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 89 | MIR* ArmMir2Lir::GetNextMir(BasicBlock** p_bb, MIR* mir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 90 | BasicBlock* bb = *p_bb; |
| 91 | MIR* orig_mir = mir; |
| 92 | while (bb != NULL) { |
| 93 | if (mir != NULL) { |
| 94 | mir = mir->next; |
| 95 | } |
| 96 | if (mir != NULL) { |
| 97 | return mir; |
| 98 | } else { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 99 | bb = mir_graph_->GetBasicBlock(bb->fall_through); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 100 | *p_bb = bb; |
| 101 | if (bb) { |
| 102 | mir = bb->first_mir_insn; |
| 103 | if (mir != NULL) { |
| 104 | return mir; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return orig_mir; |
| 110 | } |
| 111 | |
| 112 | /* Used for the "verbose" listing */ |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 113 | // TODO: move to common code |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 114 | void ArmMir2Lir::GenPrintLabel(MIR* mir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 115 | /* Mark the beginning of a Dalvik instruction for line tracking */ |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 116 | if (cu_->verbose) { |
| 117 | char* inst_str = mir_graph_->GetDalvikDisassembly(mir); |
| 118 | MarkBoundary(mir->offset, inst_str); |
| 119 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 122 | MIR* ArmMir2Lir::SpecialIGet(BasicBlock** bb, MIR* mir, const InlineMethod& special) { |
| 123 | // FastInstance() already checked by DexFileMethodInliner. |
| 124 | const InlineIGetIPutData& data = special.d.ifield_data; |
Vladimir Marko | c9bf407 | 2014-02-10 12:34:34 +0000 | [diff] [blame] | 125 | if (data.method_is_static || data.object_arg != 0) { |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 126 | return NULL; // The object is not "this" and has to be null-checked. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 127 | } |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 128 | |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 129 | DCHECK_NE(data.op_size, kDouble); // The inliner doesn't distinguish kDouble, uses kLong. |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 130 | bool wide = (data.op_size == kLong); |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 131 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 132 | // Point of no return - no aborts after this |
| 133 | ArmMir2Lir::GenPrintLabel(mir); |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 134 | LockArg(data.object_arg); |
| 135 | RegLocation rl_dest = wide ? GetReturnWide(false) : GetReturn(false); |
| 136 | int reg_obj = LoadArg(data.object_arg); |
| 137 | if (wide) { |
| 138 | LoadBaseDispWide(reg_obj, data.field_offset, rl_dest.low_reg, rl_dest.high_reg, INVALID_SREG); |
| 139 | } else { |
| 140 | LoadBaseDisp(reg_obj, data.field_offset, rl_dest.low_reg, kWord, INVALID_SREG); |
| 141 | } |
| 142 | if (data.is_volatile) { |
| 143 | GenMemBarrier(kLoadLoad); |
| 144 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 145 | return GetNextMir(bb, mir); |
| 146 | } |
| 147 | |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 148 | MIR* ArmMir2Lir::SpecialIPut(BasicBlock** bb, MIR* mir, const InlineMethod& special) { |
| 149 | // FastInstance() already checked by DexFileMethodInliner. |
| 150 | const InlineIGetIPutData& data = special.d.ifield_data; |
Vladimir Marko | c9bf407 | 2014-02-10 12:34:34 +0000 | [diff] [blame] | 151 | if (data.method_is_static || data.object_arg != 0) { |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 152 | return NULL; // The object is not "this" and has to be null-checked. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 153 | } |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 154 | |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 155 | DCHECK_NE(data.op_size, kDouble); // The inliner doesn't distinguish kDouble, uses kLong. |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 156 | bool wide = (data.op_size == kLong); |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 157 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 158 | // Point of no return - no aborts after this |
| 159 | ArmMir2Lir::GenPrintLabel(mir); |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 160 | LockArg(data.object_arg); |
| 161 | LockArg(data.src_arg, wide); |
| 162 | int reg_obj = LoadArg(data.object_arg); |
| 163 | int reg_src = LoadArg(data.src_arg, wide); |
| 164 | if (data.is_volatile) { |
| 165 | GenMemBarrier(kStoreStore); |
| 166 | } |
| 167 | if (wide) { |
| 168 | StoreBaseDispWide(reg_obj, data.field_offset, reg_src & 0xff, reg_src >> 8); |
| 169 | } else { |
| 170 | StoreBaseDisp(reg_obj, data.field_offset, reg_src, kWord); |
| 171 | } |
| 172 | if (data.is_volatile) { |
| 173 | GenMemBarrier(kLoadLoad); |
| 174 | } |
| 175 | if (data.is_object) { |
| 176 | MarkGCCard(reg_src, reg_obj); |
| 177 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 178 | return GetNextMir(bb, mir); |
| 179 | } |
| 180 | |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 181 | MIR* ArmMir2Lir::SpecialIdentity(MIR* mir, const InlineMethod& special) { |
| 182 | const InlineReturnArgData& data = special.d.return_data; |
| 183 | DCHECK_NE(data.op_size, kDouble); // The inliner doesn't distinguish kDouble, uses kLong. |
| 184 | bool wide = (data.op_size == kLong); |
| 185 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 186 | // Point of no return - no aborts after this |
| 187 | ArmMir2Lir::GenPrintLabel(mir); |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 188 | LockArg(data.arg, wide); |
| 189 | RegLocation rl_dest = wide ? GetReturnWide(false) : GetReturn(false); |
| 190 | LoadArgDirect(data.arg, rl_dest); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 191 | return mir; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Special-case code genration for simple non-throwing leaf methods. |
| 196 | */ |
| 197 | void ArmMir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 198 | const InlineMethod& special) { |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 199 | DCHECK(special.flags & kInlineSpecial); |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 200 | current_dalvik_offset_ = mir->offset; |
| 201 | MIR* next_mir = NULL; |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 202 | switch (special.opcode) { |
| 203 | case kInlineOpNop: |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 204 | DCHECK(mir->dalvikInsn.opcode == Instruction::RETURN_VOID); |
| 205 | next_mir = mir; |
| 206 | break; |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 207 | case kInlineOpConst: |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 208 | ArmMir2Lir::GenPrintLabel(mir); |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 209 | LoadConstant(rARM_RET0, static_cast<int>(special.d.data)); |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 210 | next_mir = GetNextMir(&bb, mir); |
| 211 | break; |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 212 | case kInlineOpIGet: |
| 213 | next_mir = SpecialIGet(&bb, mir, special); |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 214 | break; |
Vladimir Marko | 2bc4780 | 2014-02-10 09:43:07 +0000 | [diff] [blame] | 215 | case kInlineOpIPut: |
| 216 | next_mir = SpecialIPut(&bb, mir, special); |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 217 | break; |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 218 | case kInlineOpReturnArg: |
Vladimir Marko | 502c2a8 | 2014-02-06 11:52:07 +0000 | [diff] [blame] | 219 | next_mir = SpecialIdentity(mir, special); |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 220 | break; |
| 221 | default: |
| 222 | return; |
| 223 | } |
| 224 | if (next_mir != NULL) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 225 | current_dalvik_offset_ = next_mir->offset; |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 226 | if (special.opcode != kInlineOpReturnArg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 227 | ArmMir2Lir::GenPrintLabel(next_mir); |
| 228 | } |
| 229 | NewLIR1(kThumbBx, rARM_LR); |
| 230 | core_spill_mask_ = 0; |
| 231 | num_core_spills_ = 0; |
| 232 | fp_spill_mask_ = 0; |
| 233 | num_fp_spills_ = 0; |
| 234 | frame_size_ = 0; |
| 235 | core_vmap_table_.clear(); |
| 236 | fp_vmap_table_.clear(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * The sparse table in the literal pool is an array of <key,displacement> |
| 242 | * pairs. For each set, we'll load them as a pair using ldmia. |
| 243 | * This means that the register number of the temp we use for the key |
| 244 | * must be lower than the reg for the displacement. |
| 245 | * |
| 246 | * The test loop will look something like: |
| 247 | * |
| 248 | * adr rBase, <table> |
| 249 | * ldr r_val, [rARM_SP, v_reg_off] |
| 250 | * mov r_idx, #table_size |
| 251 | * lp: |
| 252 | * ldmia rBase!, {r_key, r_disp} |
| 253 | * sub r_idx, #1 |
| 254 | * cmp r_val, r_key |
| 255 | * ifeq |
| 256 | * add rARM_PC, r_disp ; This is the branch from which we compute displacement |
| 257 | * cbnz r_idx, lp |
| 258 | */ |
| 259 | void ArmMir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 260 | RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 261 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 262 | if (cu_->verbose) { |
| 263 | DumpSparseSwitchTable(table); |
| 264 | } |
| 265 | // Add the table to the list - we'll process it later |
| 266 | SwitchTable *tab_rec = |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 267 | static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), ArenaAllocator::kAllocData)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 268 | tab_rec->table = table; |
| 269 | tab_rec->vaddr = current_dalvik_offset_; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 270 | uint32_t size = table[1]; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 271 | tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 272 | ArenaAllocator::kAllocLIR)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | switch_tables_.Insert(tab_rec); |
| 274 | |
| 275 | // Get the switch value |
| 276 | rl_src = LoadValue(rl_src, kCoreReg); |
| 277 | int rBase = AllocTemp(); |
| 278 | /* Allocate key and disp temps */ |
| 279 | int r_key = AllocTemp(); |
| 280 | int r_disp = AllocTemp(); |
| 281 | // Make sure r_key's register number is less than r_disp's number for ldmia |
| 282 | if (r_key > r_disp) { |
| 283 | int tmp = r_disp; |
| 284 | r_disp = r_key; |
| 285 | r_key = tmp; |
| 286 | } |
| 287 | // Materialize a pointer to the switch table |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 288 | NewLIR3(kThumb2Adr, rBase, 0, WrapPointer(tab_rec)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 289 | // Set up r_idx |
| 290 | int r_idx = AllocTemp(); |
| 291 | LoadConstant(r_idx, size); |
| 292 | // Establish loop branch target |
| 293 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 294 | // Load next key/disp |
| 295 | NewLIR2(kThumb2LdmiaWB, rBase, (1 << r_key) | (1 << r_disp)); |
| 296 | OpRegReg(kOpCmp, r_key, rl_src.low_reg); |
| 297 | // Go if match. NOTE: No instruction set switch here - must stay Thumb2 |
| 298 | OpIT(kCondEq, ""); |
| 299 | LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp); |
| 300 | tab_rec->anchor = switch_branch; |
| 301 | // Needs to use setflags encoding here |
| 302 | NewLIR3(kThumb2SubsRRI12, r_idx, r_idx, 1); |
| 303 | OpCondBranch(kCondNe, target); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | void ArmMir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 308 | RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 309 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 310 | if (cu_->verbose) { |
| 311 | DumpPackedSwitchTable(table); |
| 312 | } |
| 313 | // Add the table to the list - we'll process it later |
| 314 | SwitchTable *tab_rec = |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 315 | static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), ArenaAllocator::kAllocData)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 316 | tab_rec->table = table; |
| 317 | tab_rec->vaddr = current_dalvik_offset_; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 318 | uint32_t size = table[1]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 319 | tab_rec->targets = |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 320 | static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), ArenaAllocator::kAllocLIR)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 321 | switch_tables_.Insert(tab_rec); |
| 322 | |
| 323 | // Get the switch value |
| 324 | rl_src = LoadValue(rl_src, kCoreReg); |
| 325 | int table_base = AllocTemp(); |
| 326 | // Materialize a pointer to the switch table |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 327 | NewLIR3(kThumb2Adr, table_base, 0, WrapPointer(tab_rec)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 328 | int low_key = s4FromSwitchData(&table[2]); |
| 329 | int keyReg; |
| 330 | // Remove the bias, if necessary |
| 331 | if (low_key == 0) { |
| 332 | keyReg = rl_src.low_reg; |
| 333 | } else { |
| 334 | keyReg = AllocTemp(); |
| 335 | OpRegRegImm(kOpSub, keyReg, rl_src.low_reg, low_key); |
| 336 | } |
| 337 | // Bounds check - if < 0 or >= size continue following switch |
| 338 | OpRegImm(kOpCmp, keyReg, size-1); |
| 339 | LIR* branch_over = OpCondBranch(kCondHi, NULL); |
| 340 | |
| 341 | // Load the displacement from the switch table |
| 342 | int disp_reg = AllocTemp(); |
| 343 | LoadBaseIndexed(table_base, keyReg, disp_reg, 2, kWord); |
| 344 | |
| 345 | // ..and go! NOTE: No instruction set switch here - must stay Thumb2 |
| 346 | LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg); |
| 347 | tab_rec->anchor = switch_branch; |
| 348 | |
| 349 | /* branch_over target here */ |
| 350 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 351 | branch_over->target = target; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Array data table format: |
| 356 | * ushort ident = 0x0300 magic value |
| 357 | * ushort width width of each element in the table |
| 358 | * uint size number of elements in the table |
| 359 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 360 | * padding at the end) |
| 361 | * |
| 362 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 363 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 364 | void ArmMir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 365 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 366 | // Add the table to the list - we'll process it later |
| 367 | FillArrayData *tab_rec = |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 368 | static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), ArenaAllocator::kAllocData)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 369 | tab_rec->table = table; |
| 370 | tab_rec->vaddr = current_dalvik_offset_; |
| 371 | uint16_t width = tab_rec->table[1]; |
| 372 | uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16); |
| 373 | tab_rec->size = (size * width) + 8; |
| 374 | |
| 375 | fill_array_data_.Insert(tab_rec); |
| 376 | |
| 377 | // Making a call - use explicit registers |
| 378 | FlushAllRegs(); /* Everything to home location */ |
| 379 | LoadValueDirectFixed(rl_src, r0); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 380 | LoadWordDisp(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pHandleFillArrayData).Int32Value(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 381 | rARM_LR); |
| 382 | // Materialize a pointer to the fill data image |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 383 | NewLIR3(kThumb2Adr, r1, 0, WrapPointer(tab_rec)); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 384 | ClobberCallerSave(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 385 | LIR* call_inst = OpReg(kOpBlx, rARM_LR); |
| 386 | MarkSafepointPC(call_inst); |
| 387 | } |
| 388 | |
| 389 | /* |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 390 | * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more |
| 391 | * details see monitor.cc. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 392 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 393 | void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 394 | FlushAllRegs(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 395 | LoadValueDirectFixed(rl_src, r0); // Get obj |
| 396 | LockCallTemps(); // Prepare for explicit register usage |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 397 | constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15. |
| 398 | if (kArchVariantHasGoodBranchPredictor) { |
| 399 | LIR* null_check_branch; |
| 400 | if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) { |
| 401 | null_check_branch = nullptr; // No null check. |
| 402 | } else { |
| 403 | // If the null-check fails its handled by the slow-path to reduce exception related meta-data. |
| 404 | null_check_branch = OpCmpImmBranch(kCondEq, r0, 0, NULL); |
| 405 | } |
| 406 | LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
| 407 | NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2); |
| 408 | LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, r1, 0, NULL); |
| 409 | NewLIR4(kThumb2Strex, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2); |
| 410 | LIR* lock_success_branch = OpCmpImmBranch(kCondEq, r1, 0, NULL); |
| 411 | |
| 412 | |
| 413 | LIR* slow_path_target = NewLIR0(kPseudoTargetLabel); |
| 414 | not_unlocked_branch->target = slow_path_target; |
| 415 | if (null_check_branch != nullptr) { |
| 416 | null_check_branch->target = slow_path_target; |
| 417 | } |
| 418 | // TODO: move to a slow path. |
| 419 | // Go expensive route - artLockObjectFromCode(obj); |
| 420 | LoadWordDisp(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pLockObject).Int32Value(), rARM_LR); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 421 | ClobberCallerSave(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 422 | LIR* call_inst = OpReg(kOpBlx, rARM_LR); |
| 423 | MarkSafepointPC(call_inst); |
| 424 | |
| 425 | LIR* success_target = NewLIR0(kPseudoTargetLabel); |
| 426 | lock_success_branch->target = success_target; |
| 427 | GenMemBarrier(kLoadLoad); |
| 428 | } else { |
| 429 | // Explicit null-check as slow-path is entered using an IT. |
| 430 | GenNullCheck(rl_src.s_reg_low, r0, opt_flags); |
| 431 | LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
| 432 | NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2); |
| 433 | OpRegImm(kOpCmp, r1, 0); |
| 434 | OpIT(kCondEq, ""); |
| 435 | NewLIR4(kThumb2Strex/*eq*/, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2); |
| 436 | OpRegImm(kOpCmp, r1, 0); |
| 437 | OpIT(kCondNe, "T"); |
| 438 | // Go expensive route - artLockObjectFromCode(self, obj); |
| 439 | LoadWordDisp/*ne*/(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pLockObject).Int32Value(), rARM_LR); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 440 | ClobberCallerSave(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 441 | LIR* call_inst = OpReg(kOpBlx/*ne*/, rARM_LR); |
| 442 | MarkSafepointPC(call_inst); |
| 443 | GenMemBarrier(kLoadLoad); |
| 444 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 448 | * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more |
| 449 | * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock |
| 450 | * and can only give away ownership if its suspended. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 451 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 452 | void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 453 | FlushAllRegs(); |
| 454 | LoadValueDirectFixed(rl_src, r0); // Get obj |
| 455 | LockCallTemps(); // Prepare for explicit register usage |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 456 | LIR* null_check_branch; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 457 | LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 458 | constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15. |
| 459 | if (kArchVariantHasGoodBranchPredictor) { |
| 460 | if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) { |
| 461 | null_check_branch = nullptr; // No null check. |
| 462 | } else { |
| 463 | // If the null-check fails its handled by the slow-path to reduce exception related meta-data. |
| 464 | null_check_branch = OpCmpImmBranch(kCondEq, r0, 0, NULL); |
| 465 | } |
| 466 | LoadWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r1); |
| 467 | LoadConstantNoClobber(r3, 0); |
| 468 | LIR* slow_unlock_branch = OpCmpBranch(kCondNe, r1, r2, NULL); |
| 469 | StoreWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r3); |
| 470 | LIR* unlock_success_branch = OpUnconditionalBranch(NULL); |
| 471 | |
| 472 | LIR* slow_path_target = NewLIR0(kPseudoTargetLabel); |
| 473 | slow_unlock_branch->target = slow_path_target; |
| 474 | if (null_check_branch != nullptr) { |
| 475 | null_check_branch->target = slow_path_target; |
| 476 | } |
| 477 | // TODO: move to a slow path. |
| 478 | // Go expensive route - artUnlockObjectFromCode(obj); |
| 479 | LoadWordDisp(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pUnlockObject).Int32Value(), rARM_LR); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 480 | ClobberCallerSave(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 481 | LIR* call_inst = OpReg(kOpBlx, rARM_LR); |
| 482 | MarkSafepointPC(call_inst); |
| 483 | |
| 484 | LIR* success_target = NewLIR0(kPseudoTargetLabel); |
| 485 | unlock_success_branch->target = success_target; |
| 486 | GenMemBarrier(kStoreLoad); |
| 487 | } else { |
| 488 | // Explicit null-check as slow-path is entered using an IT. |
| 489 | GenNullCheck(rl_src.s_reg_low, r0, opt_flags); |
| 490 | LoadWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r1); // Get lock |
| 491 | LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
| 492 | LoadConstantNoClobber(r3, 0); |
| 493 | // Is lock unheld on lock or held by us (==thread_id) on unlock? |
| 494 | OpRegReg(kOpCmp, r1, r2); |
| 495 | OpIT(kCondEq, "EE"); |
| 496 | StoreWordDisp/*eq*/(r0, mirror::Object::MonitorOffset().Int32Value(), r3); |
| 497 | // Go expensive route - UnlockObjectFromCode(obj); |
| 498 | LoadWordDisp/*ne*/(rARM_SELF, QUICK_ENTRYPOINT_OFFSET(pUnlockObject).Int32Value(), rARM_LR); |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 499 | ClobberCallerSave(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 500 | LIR* call_inst = OpReg(kOpBlx/*ne*/, rARM_LR); |
| 501 | MarkSafepointPC(call_inst); |
| 502 | GenMemBarrier(kStoreLoad); |
| 503 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 506 | void ArmMir2Lir::GenMoveException(RegLocation rl_dest) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 507 | int ex_offset = Thread::ExceptionOffset().Int32Value(); |
| 508 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 509 | int reset_reg = AllocTemp(); |
| 510 | LoadWordDisp(rARM_SELF, ex_offset, rl_result.low_reg); |
| 511 | LoadConstant(reset_reg, 0); |
| 512 | StoreWordDisp(rARM_SELF, ex_offset, reset_reg); |
| 513 | FreeTemp(reset_reg); |
| 514 | StoreValue(rl_dest, rl_result); |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Mark garbage collection card. Skip if the value we're storing is null. |
| 519 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 520 | void ArmMir2Lir::MarkGCCard(int val_reg, int tgt_addr_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 521 | int reg_card_base = AllocTemp(); |
| 522 | int reg_card_no = AllocTemp(); |
| 523 | LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL); |
| 524 | LoadWordDisp(rARM_SELF, Thread::CardTableOffset().Int32Value(), reg_card_base); |
| 525 | OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift); |
| 526 | StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, |
| 527 | kUnsignedByte); |
| 528 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 529 | branch_over->target = target; |
| 530 | FreeTemp(reg_card_base); |
| 531 | FreeTemp(reg_card_no); |
| 532 | } |
| 533 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 534 | void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 535 | int spill_count = num_core_spills_ + num_fp_spills_; |
| 536 | /* |
| 537 | * On entry, r0, r1, r2 & r3 are live. Let the register allocation |
| 538 | * mechanism know so it doesn't try to use any of them when |
| 539 | * expanding the frame or flushing. This leaves the utility |
| 540 | * code with a single temp: r12. This should be enough. |
| 541 | */ |
| 542 | LockTemp(r0); |
| 543 | LockTemp(r1); |
| 544 | LockTemp(r2); |
| 545 | LockTemp(r3); |
| 546 | |
| 547 | /* |
| 548 | * We can safely skip the stack overflow check if we're |
| 549 | * a leaf *and* our frame size < fudge factor. |
| 550 | */ |
| 551 | bool skip_overflow_check = (mir_graph_->MethodIsLeaf() && |
| 552 | (static_cast<size_t>(frame_size_) < |
| 553 | Thread::kStackOverflowReservedBytes)); |
| 554 | NewLIR0(kPseudoMethodEntry); |
| 555 | if (!skip_overflow_check) { |
| 556 | /* Load stack limit */ |
| 557 | LoadWordDisp(rARM_SELF, Thread::StackEndOffset().Int32Value(), r12); |
| 558 | } |
| 559 | /* Spill core callee saves */ |
| 560 | NewLIR1(kThumb2Push, core_spill_mask_); |
| 561 | /* Need to spill any FP regs? */ |
| 562 | if (num_fp_spills_) { |
| 563 | /* |
| 564 | * NOTE: fp spills are a little different from core spills in that |
| 565 | * they are pushed as a contiguous block. When promoting from |
| 566 | * the fp set, we must allocate all singles from s16..highest-promoted |
| 567 | */ |
| 568 | NewLIR1(kThumb2VPushCS, num_fp_spills_); |
| 569 | } |
| 570 | if (!skip_overflow_check) { |
| 571 | OpRegRegImm(kOpSub, rARM_LR, rARM_SP, frame_size_ - (spill_count * 4)); |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 572 | GenRegRegCheck(kCondUlt, rARM_LR, r12, kThrowStackOverflow); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 573 | OpRegCopy(rARM_SP, rARM_LR); // Establish stack |
| 574 | } else { |
| 575 | OpRegImm(kOpSub, rARM_SP, frame_size_ - (spill_count * 4)); |
| 576 | } |
| 577 | |
| 578 | FlushIns(ArgLocs, rl_method); |
| 579 | |
| 580 | FreeTemp(r0); |
| 581 | FreeTemp(r1); |
| 582 | FreeTemp(r2); |
| 583 | FreeTemp(r3); |
| 584 | } |
| 585 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 586 | void ArmMir2Lir::GenExitSequence() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 587 | int spill_count = num_core_spills_ + num_fp_spills_; |
| 588 | /* |
| 589 | * In the exit path, r0/r1 are live - make sure they aren't |
| 590 | * allocated by the register utilities as temps. |
| 591 | */ |
| 592 | LockTemp(r0); |
| 593 | LockTemp(r1); |
| 594 | |
| 595 | NewLIR0(kPseudoMethodExit); |
| 596 | OpRegImm(kOpAdd, rARM_SP, frame_size_ - (spill_count * 4)); |
| 597 | /* Need to restore any FP callee saves? */ |
| 598 | if (num_fp_spills_) { |
| 599 | NewLIR1(kThumb2VPopCS, num_fp_spills_); |
| 600 | } |
| 601 | if (core_spill_mask_ & (1 << rARM_LR)) { |
| 602 | /* Unspill rARM_LR to rARM_PC */ |
| 603 | core_spill_mask_ &= ~(1 << rARM_LR); |
| 604 | core_spill_mask_ |= (1 << rARM_PC); |
| 605 | } |
| 606 | NewLIR1(kThumb2Pop, core_spill_mask_); |
| 607 | if (!(core_spill_mask_ & (1 << rARM_PC))) { |
| 608 | /* We didn't pop to rARM_PC, so must do a bv rARM_LR */ |
| 609 | NewLIR1(kThumbBx, rARM_LR); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | } // namespace art |