Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [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 "arm64_lir.h" |
| 20 | #include "codegen_arm64.h" |
| 21 | #include "dex/quick/mir_to_lir-inl.h" |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 22 | #include "gc/accounting/card_table.h" |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 23 | #include "entrypoints/quick/quick_entrypoints.h" |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 24 | #include "mirror/art_method.h" |
| 25 | #include "mirror/object_array-inl.h" |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | /* |
| 30 | * The sparse table in the literal pool is an array of <key,displacement> |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 31 | * pairs. For each set, we'll load them as a pair using ldp. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 32 | * The test loop will look something like: |
| 33 | * |
| 34 | * adr r_base, <table> |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 35 | * ldr r_val, [rA64_SP, v_reg_off] |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 36 | * mov r_idx, #table_size |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 37 | * loop: |
| 38 | * cbz r_idx, quit |
| 39 | * ldp r_key, r_disp, [r_base], #8 |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 40 | * sub r_idx, #1 |
| 41 | * cmp r_val, r_key |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 42 | * b.ne loop |
| 43 | * adr r_base, #0 ; This is the instruction from which we compute displacements |
| 44 | * add r_base, r_disp |
| 45 | * br r_base |
| 46 | * quit: |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 47 | */ |
Andreas Gampe | 48971b3 | 2014-08-06 10:09:01 -0700 | [diff] [blame] | 48 | void Arm64Mir2Lir::GenLargeSparseSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 49 | const uint16_t* table = mir_graph_->GetTable(mir, table_offset); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 50 | if (cu_->verbose) { |
| 51 | DumpSparseSwitchTable(table); |
| 52 | } |
| 53 | // Add the table to the list - we'll process it later |
| 54 | SwitchTable *tab_rec = |
| 55 | static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData)); |
| 56 | tab_rec->table = table; |
| 57 | tab_rec->vaddr = current_dalvik_offset_; |
| 58 | uint32_t size = table[1]; |
| 59 | tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR)); |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 60 | switch_tables_.push_back(tab_rec); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 61 | |
| 62 | // Get the switch value |
| 63 | rl_src = LoadValue(rl_src, kCoreReg); |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 64 | RegStorage r_base = AllocTempWide(); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 65 | // Allocate key and disp temps. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 66 | RegStorage r_key = AllocTemp(); |
| 67 | RegStorage r_disp = AllocTemp(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 68 | // Materialize a pointer to the switch table |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 69 | NewLIR3(kA64Adr2xd, r_base.GetReg(), 0, WrapPointer(tab_rec)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 70 | // Set up r_idx |
| 71 | RegStorage r_idx = AllocTemp(); |
| 72 | LoadConstant(r_idx, size); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 73 | |
| 74 | // Entry of loop. |
| 75 | LIR* loop_entry = NewLIR0(kPseudoTargetLabel); |
| 76 | LIR* branch_out = NewLIR2(kA64Cbz2rt, r_idx.GetReg(), 0); |
| 77 | |
| 78 | // Load next key/disp. |
| 79 | NewLIR4(kA64LdpPost4rrXD, r_key.GetReg(), r_disp.GetReg(), r_base.GetReg(), 2); |
| 80 | OpRegRegImm(kOpSub, r_idx, r_idx, 1); |
| 81 | |
| 82 | // Go to next case, if key does not match. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 83 | OpRegReg(kOpCmp, r_key, rl_src.reg); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 84 | OpCondBranch(kCondNe, loop_entry); |
| 85 | |
| 86 | // Key does match: branch to case label. |
| 87 | LIR* switch_label = NewLIR3(kA64Adr2xd, r_base.GetReg(), 0, -1); |
| 88 | tab_rec->anchor = switch_label; |
| 89 | |
| 90 | // Add displacement to base branch address and go! |
Andreas Gampe | 47b31aa | 2014-06-19 01:10:07 -0700 | [diff] [blame] | 91 | OpRegRegRegExtend(kOpAdd, r_base, r_base, As64BitReg(r_disp), kA64Sxtw, 0U); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 92 | NewLIR1(kA64Br1x, r_base.GetReg()); |
| 93 | |
| 94 | // Loop exit label. |
| 95 | LIR* loop_exit = NewLIR0(kPseudoTargetLabel); |
| 96 | branch_out->target = loop_exit; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
Andreas Gampe | 48971b3 | 2014-08-06 10:09:01 -0700 | [diff] [blame] | 100 | void Arm64Mir2Lir::GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 101 | const uint16_t* table = mir_graph_->GetTable(mir, table_offset); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 102 | if (cu_->verbose) { |
| 103 | DumpPackedSwitchTable(table); |
| 104 | } |
| 105 | // Add the table to the list - we'll process it later |
| 106 | SwitchTable *tab_rec = |
| 107 | static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData)); |
| 108 | tab_rec->table = table; |
| 109 | tab_rec->vaddr = current_dalvik_offset_; |
| 110 | uint32_t size = table[1]; |
| 111 | tab_rec->targets = |
| 112 | static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR)); |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 113 | switch_tables_.push_back(tab_rec); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 114 | |
| 115 | // Get the switch value |
| 116 | rl_src = LoadValue(rl_src, kCoreReg); |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 117 | RegStorage table_base = AllocTempWide(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 118 | // Materialize a pointer to the switch table |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 119 | NewLIR3(kA64Adr2xd, table_base.GetReg(), 0, WrapPointer(tab_rec)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 120 | int low_key = s4FromSwitchData(&table[2]); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 121 | RegStorage key_reg; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 122 | // Remove the bias, if necessary |
| 123 | if (low_key == 0) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 124 | key_reg = rl_src.reg; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 125 | } else { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 126 | key_reg = AllocTemp(); |
| 127 | OpRegRegImm(kOpSub, key_reg, rl_src.reg, low_key); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 128 | } |
| 129 | // Bounds check - if < 0 or >= size continue following switch |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 130 | OpRegImm(kOpCmp, key_reg, size - 1); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 131 | LIR* branch_over = OpCondBranch(kCondHi, NULL); |
| 132 | |
| 133 | // Load the displacement from the switch table |
| 134 | RegStorage disp_reg = AllocTemp(); |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 135 | LoadBaseIndexed(table_base, As64BitReg(key_reg), disp_reg, 2, k32); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 136 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 137 | // Get base branch address. |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 138 | RegStorage branch_reg = AllocTempWide(); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 139 | LIR* switch_label = NewLIR3(kA64Adr2xd, branch_reg.GetReg(), 0, -1); |
| 140 | tab_rec->anchor = switch_label; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 141 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 142 | // Add displacement to base branch address and go! |
Andreas Gampe | 47b31aa | 2014-06-19 01:10:07 -0700 | [diff] [blame] | 143 | OpRegRegRegExtend(kOpAdd, branch_reg, branch_reg, As64BitReg(disp_reg), kA64Sxtw, 0U); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 144 | NewLIR1(kA64Br1x, branch_reg.GetReg()); |
| 145 | |
| 146 | // branch_over target here |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 147 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 148 | branch_over->target = target; |
| 149 | } |
| 150 | |
| 151 | /* |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 152 | * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more |
| 153 | * details see monitor.cc. |
| 154 | */ |
| 155 | void Arm64Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 156 | // x0/w0 = object |
| 157 | // w1 = thin lock thread id |
| 158 | // x2 = address of lock word |
| 159 | // w3 = lock word / store failure |
| 160 | // TUNING: How much performance we get when we inline this? |
| 161 | // Since we've already flush all register. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 162 | FlushAllRegs(); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 163 | LoadValueDirectFixed(rl_src, rs_x0); // = TargetReg(kArg0, kRef) |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 164 | LockCallTemps(); // Prepare for explicit register usage |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 165 | LIR* null_check_branch = nullptr; |
| 166 | if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) { |
| 167 | null_check_branch = nullptr; // No null check. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 168 | } else { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 169 | // If the null-check fails its handled by the slow-path to reduce exception related meta-data. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 170 | if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 171 | null_check_branch = OpCmpImmBranch(kCondEq, rs_x0, 0, NULL); |
| 172 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 173 | } |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 174 | Load32Disp(rs_xSELF, Thread::ThinLockIdOffset<8>().Int32Value(), rs_w1); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 175 | OpRegRegImm(kOpAdd, rs_x2, rs_x0, mirror::Object::MonitorOffset().Int32Value()); |
| 176 | NewLIR2(kA64Ldxr2rX, rw3, rx2); |
| 177 | MarkPossibleNullPointerException(opt_flags); |
buzbee | 5d13f12 | 2014-08-19 16:47:06 -0700 | [diff] [blame] | 178 | LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_w3, 0, NULL); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 179 | NewLIR3(kA64Stxr3wrX, rw3, rw1, rx2); |
buzbee | 5d13f12 | 2014-08-19 16:47:06 -0700 | [diff] [blame] | 180 | LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_w3, 0, NULL); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 181 | |
| 182 | LIR* slow_path_target = NewLIR0(kPseudoTargetLabel); |
| 183 | not_unlocked_branch->target = slow_path_target; |
| 184 | if (null_check_branch != nullptr) { |
| 185 | null_check_branch->target = slow_path_target; |
| 186 | } |
| 187 | // TODO: move to a slow path. |
| 188 | // Go expensive route - artLockObjectFromCode(obj); |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 189 | LoadWordDisp(rs_xSELF, QUICK_ENTRYPOINT_OFFSET(8, pLockObject).Int32Value(), rs_xLR); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 190 | ClobberCallerSave(); |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 191 | LIR* call_inst = OpReg(kOpBlx, rs_xLR); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 192 | MarkSafepointPC(call_inst); |
| 193 | |
| 194 | LIR* success_target = NewLIR0(kPseudoTargetLabel); |
| 195 | lock_success_branch->target = success_target; |
Hans Boehm | 48f5c47 | 2014-06-27 14:50:10 -0700 | [diff] [blame] | 196 | GenMemBarrier(kLoadAny); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 201 | * details see monitor.cc. Note the code below doesn't use ldxr/stxr as the code holds the lock |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 202 | * and can only give away ownership if its suspended. |
| 203 | */ |
| 204 | void Arm64Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 205 | // x0/w0 = object |
| 206 | // w1 = thin lock thread id |
| 207 | // w2 = lock word |
| 208 | // TUNING: How much performance we get when we inline this? |
| 209 | // Since we've already flush all register. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 210 | FlushAllRegs(); |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 211 | LoadValueDirectFixed(rl_src, rs_x0); // Get obj |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 212 | LockCallTemps(); // Prepare for explicit register usage |
| 213 | LIR* null_check_branch = nullptr; |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 214 | if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) { |
| 215 | null_check_branch = nullptr; // No null check. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 216 | } else { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 217 | // If the null-check fails its handled by the slow-path to reduce exception related meta-data. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 218 | if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 219 | null_check_branch = OpCmpImmBranch(kCondEq, rs_x0, 0, NULL); |
| 220 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 221 | } |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 222 | Load32Disp(rs_xSELF, Thread::ThinLockIdOffset<8>().Int32Value(), rs_w1); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 223 | Load32Disp(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_w2); |
| 224 | MarkPossibleNullPointerException(opt_flags); |
| 225 | LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_w1, rs_w2, NULL); |
Hans Boehm | 48f5c47 | 2014-06-27 14:50:10 -0700 | [diff] [blame] | 226 | GenMemBarrier(kAnyStore); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 227 | Store32Disp(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_wzr); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 228 | LIR* unlock_success_branch = OpUnconditionalBranch(NULL); |
| 229 | |
| 230 | LIR* slow_path_target = NewLIR0(kPseudoTargetLabel); |
| 231 | slow_unlock_branch->target = slow_path_target; |
| 232 | if (null_check_branch != nullptr) { |
| 233 | null_check_branch->target = slow_path_target; |
| 234 | } |
| 235 | // TODO: move to a slow path. |
| 236 | // Go expensive route - artUnlockObjectFromCode(obj); |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 237 | LoadWordDisp(rs_xSELF, QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject).Int32Value(), rs_xLR); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 238 | ClobberCallerSave(); |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 239 | LIR* call_inst = OpReg(kOpBlx, rs_xLR); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 240 | MarkSafepointPC(call_inst); |
| 241 | |
| 242 | LIR* success_target = NewLIR0(kPseudoTargetLabel); |
| 243 | unlock_success_branch->target = success_target; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void Arm64Mir2Lir::GenMoveException(RegLocation rl_dest) { |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 247 | int ex_offset = Thread::ExceptionOffset<8>().Int32Value(); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 248 | RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true); |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 249 | LoadRefDisp(rs_xSELF, ex_offset, rl_result.reg, kNotVolatile); |
| 250 | StoreRefDisp(rs_xSELF, ex_offset, rs_xzr, kNotVolatile); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 251 | StoreValue(rl_dest, rl_result); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Mark garbage collection card. Skip if the value we're storing is null. |
| 256 | */ |
| 257 | void Arm64Mir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) { |
Matteo Franchin | fd2e291 | 2014-06-06 10:09:56 +0100 | [diff] [blame] | 258 | RegStorage reg_card_base = AllocTempWide(); |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 259 | RegStorage reg_card_no = AllocTempWide(); // Needs to be wide as addr is ref=64b |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 260 | LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL); |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 261 | LoadWordDisp(rs_xSELF, Thread::CardTableOffset<8>().Int32Value(), reg_card_base); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 262 | OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift); |
Matteo Franchin | fd2e291 | 2014-06-06 10:09:56 +0100 | [diff] [blame] | 263 | // TODO(Arm64): generate "strb wB, [xB, wC, uxtw]" rather than "strb wB, [xB, xC]"? |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 264 | StoreBaseIndexed(reg_card_base, reg_card_no, As32BitReg(reg_card_base), |
Matteo Franchin | fd2e291 | 2014-06-06 10:09:56 +0100 | [diff] [blame] | 265 | 0, kUnsignedByte); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 266 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 267 | branch_over->target = target; |
| 268 | FreeTemp(reg_card_base); |
| 269 | FreeTemp(reg_card_no); |
| 270 | } |
| 271 | |
| 272 | void Arm64Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 273 | /* |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 274 | * On entry, x0 to x7 are live. Let the register allocation |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 275 | * mechanism know so it doesn't try to use any of them when |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 276 | * expanding the frame or flushing. |
| 277 | * Reserve x8 & x9 for temporaries. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 278 | */ |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 279 | LockTemp(rs_x0); |
| 280 | LockTemp(rs_x1); |
| 281 | LockTemp(rs_x2); |
| 282 | LockTemp(rs_x3); |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 283 | LockTemp(rs_x4); |
| 284 | LockTemp(rs_x5); |
| 285 | LockTemp(rs_x6); |
| 286 | LockTemp(rs_x7); |
Zheng Xu | b551fdc | 2014-07-25 11:49:42 +0800 | [diff] [blame] | 287 | LockTemp(rs_xIP0); |
| 288 | LockTemp(rs_xIP1); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 289 | |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 290 | /* TUNING: |
| 291 | * Use AllocTemp() and reuse LR if possible to give us the freedom on adjusting the number |
| 292 | * of temp registers. |
| 293 | */ |
| 294 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 295 | /* |
| 296 | * We can safely skip the stack overflow check if we're |
| 297 | * a leaf *and* our frame size < fudge factor. |
| 298 | */ |
Matteo Franchin | 2431452 | 2014-11-12 18:06:14 +0000 | [diff] [blame^] | 299 | bool skip_overflow_check = mir_graph_->MethodIsLeaf() && |
| 300 | !FrameNeedsStackCheck(frame_size_, kArm64); |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 301 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 302 | NewLIR0(kPseudoMethodEntry); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 303 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 304 | const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(kArm64); |
| 305 | const bool large_frame = static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes; |
| 306 | bool generate_explicit_stack_overflow_check = large_frame || |
| 307 | !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks(); |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 308 | const int spill_count = num_core_spills_ + num_fp_spills_; |
| 309 | const int spill_size = (spill_count * kArm64PointerSize + 15) & ~0xf; // SP 16 byte alignment. |
| 310 | const int frame_size_without_spills = frame_size_ - spill_size; |
| 311 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 312 | if (!skip_overflow_check) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 313 | if (generate_explicit_stack_overflow_check) { |
Andreas Gampe | f29ecd6 | 2014-07-29 00:35:00 -0700 | [diff] [blame] | 314 | // Load stack limit |
| 315 | LoadWordDisp(rs_xSELF, Thread::StackEndOffset<8>().Int32Value(), rs_xIP1); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 316 | } else { |
| 317 | // Implicit stack overflow check. |
| 318 | // Generate a load from [sp, #-framesize]. If this is in the stack |
| 319 | // redzone we will get a segmentation fault. |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 320 | |
Andreas Gampe | f29ecd6 | 2014-07-29 00:35:00 -0700 | [diff] [blame] | 321 | // TODO: If the frame size is small enough, is it possible to make this a pre-indexed load, |
| 322 | // so that we can avoid the following "sub sp" when spilling? |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 323 | OpRegRegImm(kOpSub, rs_x8, rs_sp, GetStackOverflowReservedBytes(kArm64)); |
Matteo Franchin | 2431452 | 2014-11-12 18:06:14 +0000 | [diff] [blame^] | 324 | Load32Disp(rs_x8, 0, rs_wzr); |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 325 | MarkPossibleStackOverflowException(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 326 | } |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Andreas Gampe | f29ecd6 | 2014-07-29 00:35:00 -0700 | [diff] [blame] | 329 | int spilled_already = 0; |
| 330 | if (spill_size > 0) { |
| 331 | spilled_already = SpillRegs(rs_sp, core_spill_mask_, fp_spill_mask_, frame_size_); |
| 332 | DCHECK(spill_size == spilled_already || frame_size_ == spilled_already); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 333 | } |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 334 | |
Andreas Gampe | f29ecd6 | 2014-07-29 00:35:00 -0700 | [diff] [blame] | 335 | if (spilled_already != frame_size_) { |
| 336 | OpRegImm(kOpSub, rs_sp, frame_size_without_spills); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 339 | if (!skip_overflow_check) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 340 | if (generate_explicit_stack_overflow_check) { |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 341 | class StackOverflowSlowPath: public LIRSlowPath { |
| 342 | public: |
| 343 | StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace) : |
| 344 | LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), |
| 345 | sp_displace_(sp_displace) { |
| 346 | } |
| 347 | void Compile() OVERRIDE { |
| 348 | m2l_->ResetRegPool(); |
| 349 | m2l_->ResetDefTracking(); |
| 350 | GenerateTargetLabel(kPseudoThrowTarget); |
| 351 | // Unwinds stack. |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 352 | m2l_->OpRegImm(kOpAdd, rs_sp, sp_displace_); |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 353 | m2l_->ClobberCallerSave(); |
| 354 | ThreadOffset<8> func_offset = QUICK_ENTRYPOINT_OFFSET(8, pThrowStackOverflow); |
Zheng Xu | b551fdc | 2014-07-25 11:49:42 +0800 | [diff] [blame] | 355 | m2l_->LockTemp(rs_xIP0); |
| 356 | m2l_->LoadWordDisp(rs_xSELF, func_offset.Int32Value(), rs_xIP0); |
| 357 | m2l_->NewLIR1(kA64Br1x, rs_xIP0.GetReg()); |
| 358 | m2l_->FreeTemp(rs_xIP0); |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | private: |
| 362 | const size_t sp_displace_; |
| 363 | }; |
| 364 | |
Andreas Gampe | f29ecd6 | 2014-07-29 00:35:00 -0700 | [diff] [blame] | 365 | LIR* branch = OpCmpBranch(kCondUlt, rs_sp, rs_xIP1, nullptr); |
| 366 | AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, frame_size_)); |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 367 | } |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 370 | FlushIns(ArgLocs, rl_method); |
| 371 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 372 | FreeTemp(rs_x0); |
| 373 | FreeTemp(rs_x1); |
| 374 | FreeTemp(rs_x2); |
| 375 | FreeTemp(rs_x3); |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame] | 376 | FreeTemp(rs_x4); |
| 377 | FreeTemp(rs_x5); |
| 378 | FreeTemp(rs_x6); |
| 379 | FreeTemp(rs_x7); |
Zheng Xu | b551fdc | 2014-07-25 11:49:42 +0800 | [diff] [blame] | 380 | FreeTemp(rs_xIP0); |
| 381 | FreeTemp(rs_xIP1); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void Arm64Mir2Lir::GenExitSequence() { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 385 | /* |
| 386 | * In the exit path, r0/r1 are live - make sure they aren't |
| 387 | * allocated by the register utilities as temps. |
| 388 | */ |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 389 | LockTemp(rs_x0); |
| 390 | LockTemp(rs_x1); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 391 | |
| 392 | NewLIR0(kPseudoMethodExit); |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 393 | |
Andreas Gampe | f29ecd6 | 2014-07-29 00:35:00 -0700 | [diff] [blame] | 394 | UnspillRegs(rs_sp, core_spill_mask_, fp_spill_mask_, frame_size_); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 395 | |
buzbee | b5860fb | 2014-06-21 15:31:01 -0700 | [diff] [blame] | 396 | // Finally return. |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 397 | NewLIR0(kA64Ret); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void Arm64Mir2Lir::GenSpecialExitSequence() { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 401 | NewLIR0(kA64Ret); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 404 | static bool Arm64UseRelativeCall(CompilationUnit* cu, const MethodReference& target_method) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 405 | UNUSED(cu, target_method); |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 406 | // Always emit relative calls. |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Bit of a hack here - in the absence of a real scheduling pass, |
| 412 | * emit the next instruction in static & direct invoke sequences. |
| 413 | */ |
| 414 | static int Arm64NextSDCallInsn(CompilationUnit* cu, CallInfo* info, |
| 415 | int state, const MethodReference& target_method, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 416 | uint32_t unused_idx, |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 417 | uintptr_t direct_code, uintptr_t direct_method, |
| 418 | InvokeType type) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 419 | UNUSED(info, unused_idx); |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 420 | Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get()); |
| 421 | if (direct_code != 0 && direct_method != 0) { |
| 422 | switch (state) { |
| 423 | case 0: // Get the current Method* [sets kArg0] |
| 424 | if (direct_code != static_cast<uintptr_t>(-1)) { |
| 425 | cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code); |
| 426 | } else if (Arm64UseRelativeCall(cu, target_method)) { |
| 427 | // Defer to linker patch. |
| 428 | } else { |
| 429 | cg->LoadCodeAddress(target_method, type, kInvokeTgt); |
| 430 | } |
| 431 | if (direct_method != static_cast<uintptr_t>(-1)) { |
| 432 | cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method); |
| 433 | } else { |
| 434 | cg->LoadMethodAddress(target_method, type, kArg0); |
| 435 | } |
| 436 | break; |
| 437 | default: |
| 438 | return -1; |
| 439 | } |
| 440 | } else { |
| 441 | RegStorage arg0_ref = cg->TargetReg(kArg0, kRef); |
| 442 | switch (state) { |
| 443 | case 0: // Get the current Method* [sets kArg0] |
| 444 | // TUNING: we can save a reg copy if Method* has been promoted. |
| 445 | cg->LoadCurrMethodDirect(arg0_ref); |
| 446 | break; |
| 447 | case 1: // Get method->dex_cache_resolved_methods_ |
| 448 | cg->LoadRefDisp(arg0_ref, |
| 449 | mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(), |
| 450 | arg0_ref, |
| 451 | kNotVolatile); |
| 452 | // Set up direct code if known. |
| 453 | if (direct_code != 0) { |
| 454 | if (direct_code != static_cast<uintptr_t>(-1)) { |
| 455 | cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code); |
| 456 | } else if (Arm64UseRelativeCall(cu, target_method)) { |
| 457 | // Defer to linker patch. |
| 458 | } else { |
| 459 | CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds()); |
| 460 | cg->LoadCodeAddress(target_method, type, kInvokeTgt); |
| 461 | } |
| 462 | } |
| 463 | break; |
| 464 | case 2: // Grab target method* |
| 465 | CHECK_EQ(cu->dex_file, target_method.dex_file); |
| 466 | cg->LoadRefDisp(arg0_ref, |
| 467 | mirror::ObjectArray<mirror::Object>::OffsetOfElement( |
| 468 | target_method.dex_method_index).Int32Value(), |
| 469 | arg0_ref, |
| 470 | kNotVolatile); |
| 471 | break; |
| 472 | case 3: // Grab the code from the method* |
| 473 | if (direct_code == 0) { |
| 474 | // kInvokeTgt := arg0_ref->entrypoint |
| 475 | cg->LoadWordDisp(arg0_ref, |
| 476 | mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(), |
| 477 | cg->TargetPtrReg(kInvokeTgt)); |
| 478 | } |
| 479 | break; |
| 480 | default: |
| 481 | return -1; |
| 482 | } |
| 483 | } |
| 484 | return state + 1; |
| 485 | } |
| 486 | |
| 487 | NextCallInsn Arm64Mir2Lir::GetNextSDCallInsn() { |
| 488 | return Arm64NextSDCallInsn; |
| 489 | } |
| 490 | |
| 491 | LIR* Arm64Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) { |
| 492 | // For ARM64, just generate a relative BL instruction that will be filled in at 'link time'. |
| 493 | // If the target turns out to be too far, the linker will generate a thunk for dispatch. |
| 494 | int target_method_idx = target_method.dex_method_index; |
| 495 | const DexFile* target_dex_file = target_method.dex_file; |
| 496 | |
| 497 | // Generate the call instruction and save index, dex_file, and type. |
| 498 | // NOTE: Method deduplication takes linker patches into account, so we can just pass 0 |
| 499 | // as a placeholder for the offset. |
| 500 | LIR* call = RawLIR(current_dalvik_offset_, kA64Bl1t, 0, |
| 501 | target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type); |
| 502 | AppendLIR(call); |
| 503 | call_method_insns_.push_back(call); |
| 504 | return call; |
| 505 | } |
| 506 | |
| 507 | LIR* Arm64Mir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info) { |
| 508 | LIR* call_insn; |
| 509 | if (method_info.FastPath() && Arm64UseRelativeCall(cu_, method_info.GetTargetMethod()) && |
| 510 | (method_info.GetSharpType() == kDirect || method_info.GetSharpType() == kStatic) && |
| 511 | method_info.DirectCode() == static_cast<uintptr_t>(-1)) { |
| 512 | call_insn = CallWithLinkerFixup(method_info.GetTargetMethod(), method_info.GetSharpType()); |
| 513 | } else { |
| 514 | call_insn = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt)); |
| 515 | } |
| 516 | return call_insn; |
| 517 | } |
| 518 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 519 | } // namespace art |