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 register alloction support. */ |
| 18 | |
| 19 | #include "dex/compiler_ir.h" |
| 20 | #include "dex/compiler_internals.h" |
| 21 | #include "mir_to_lir-inl.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | /* |
| 26 | * Free all allocated temps in the temp pools. Note that this does |
| 27 | * not affect the "liveness" of a temp register, which will stay |
| 28 | * live until it is either explicitly killed or reallocated. |
| 29 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 30 | void Mir2Lir::ResetRegPool() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | int i; |
| 32 | for (i=0; i < reg_pool_->num_core_regs; i++) { |
| 33 | if (reg_pool_->core_regs[i].is_temp) |
| 34 | reg_pool_->core_regs[i].in_use = false; |
| 35 | } |
| 36 | for (i=0; i < reg_pool_->num_fp_regs; i++) { |
| 37 | if (reg_pool_->FPRegs[i].is_temp) |
| 38 | reg_pool_->FPRegs[i].in_use = false; |
| 39 | } |
| 40 | // Reset temp tracking sanity check. |
| 41 | if (kIsDebugBuild) { |
| 42 | live_sreg_ = INVALID_SREG; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Set up temp & preserved register pools specialized by target. |
| 48 | * Note: num_regs may be zero. |
| 49 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 50 | void Mir2Lir::CompilerInitPool(RegisterInfo* regs, int* reg_nums, int num) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 51 | int i; |
| 52 | for (i=0; i < num; i++) { |
| 53 | regs[i].reg = reg_nums[i]; |
| 54 | regs[i].in_use = false; |
| 55 | regs[i].is_temp = false; |
| 56 | regs[i].pair = false; |
| 57 | regs[i].live = false; |
| 58 | regs[i].dirty = false; |
| 59 | regs[i].s_reg = INVALID_SREG; |
| 60 | } |
| 61 | } |
| 62 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 63 | void Mir2Lir::DumpRegPool(RegisterInfo* p, int num_regs) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 64 | LOG(INFO) << "================================================"; |
| 65 | for (int i = 0; i < num_regs; i++) { |
| 66 | LOG(INFO) << StringPrintf( |
| 67 | "R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d, ST:%x, EN:%x", |
| 68 | p[i].reg, p[i].is_temp, p[i].in_use, p[i].pair, p[i].partner, |
| 69 | p[i].live, p[i].dirty, p[i].s_reg, reinterpret_cast<uintptr_t>(p[i].def_start), |
| 70 | reinterpret_cast<uintptr_t>(p[i].def_end)); |
| 71 | } |
| 72 | LOG(INFO) << "================================================"; |
| 73 | } |
| 74 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 75 | void Mir2Lir::DumpCoreRegPool() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 76 | DumpRegPool(reg_pool_->core_regs, reg_pool_->num_core_regs); |
| 77 | } |
| 78 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 79 | void Mir2Lir::DumpFpRegPool() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 80 | DumpRegPool(reg_pool_->FPRegs, reg_pool_->num_fp_regs); |
| 81 | } |
| 82 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 83 | void Mir2Lir::ClobberSRegBody(RegisterInfo* p, int num_regs, int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 84 | int i; |
| 85 | for (i=0; i< num_regs; i++) { |
| 86 | if (p[i].s_reg == s_reg) { |
| 87 | if (p[i].is_temp) { |
| 88 | p[i].live = false; |
| 89 | } |
| 90 | p[i].def_start = NULL; |
| 91 | p[i].def_end = NULL; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Break the association between a Dalvik vreg and a physical temp register of either register |
| 98 | * class. |
| 99 | * TODO: Ideally, the public version of this code should not exist. Besides its local usage |
| 100 | * in the register utilities, is is also used by code gen routines to work around a deficiency in |
| 101 | * local register allocation, which fails to distinguish between the "in" and "out" identities |
| 102 | * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg |
| 103 | * is used both as the source and destination register of an operation in which the type |
| 104 | * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is |
| 105 | * addressed. |
| 106 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 107 | void Mir2Lir::ClobberSReg(int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 108 | /* Reset live temp tracking sanity checker */ |
| 109 | if (kIsDebugBuild) { |
| 110 | if (s_reg == live_sreg_) { |
| 111 | live_sreg_ = INVALID_SREG; |
| 112 | } |
| 113 | } |
| 114 | ClobberSRegBody(reg_pool_->core_regs, reg_pool_->num_core_regs, s_reg); |
| 115 | ClobberSRegBody(reg_pool_->FPRegs, reg_pool_->num_fp_regs, s_reg); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * SSA names associated with the initial definitions of Dalvik |
| 120 | * registers are the same as the Dalvik register number (and |
| 121 | * thus take the same position in the promotion_map. However, |
| 122 | * the special Method* and compiler temp resisters use negative |
| 123 | * v_reg numbers to distinguish them and can have an arbitrary |
| 124 | * ssa name (above the last original Dalvik register). This function |
| 125 | * maps SSA names to positions in the promotion_map array. |
| 126 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 127 | int Mir2Lir::SRegToPMap(int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 128 | DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs()); |
| 129 | DCHECK_GE(s_reg, 0); |
| 130 | int v_reg = mir_graph_->SRegToVReg(s_reg); |
| 131 | if (v_reg >= 0) { |
| 132 | DCHECK_LT(v_reg, cu_->num_dalvik_registers); |
| 133 | return v_reg; |
| 134 | } else { |
| 135 | int pos = std::abs(v_reg) - std::abs(SSA_METHOD_BASEREG); |
| 136 | DCHECK_LE(pos, cu_->num_compiler_temps); |
| 137 | return cu_->num_dalvik_registers + pos; |
| 138 | } |
| 139 | } |
| 140 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 141 | void Mir2Lir::RecordCorePromotion(int reg, int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 142 | int p_map_idx = SRegToPMap(s_reg); |
| 143 | int v_reg = mir_graph_->SRegToVReg(s_reg); |
| 144 | GetRegInfo(reg)->in_use = true; |
| 145 | core_spill_mask_ |= (1 << reg); |
| 146 | // Include reg for later sort |
| 147 | core_vmap_table_.push_back(reg << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1))); |
| 148 | num_core_spills_++; |
| 149 | promotion_map_[p_map_idx].core_location = kLocPhysReg; |
| 150 | promotion_map_[p_map_idx].core_reg = reg; |
| 151 | } |
| 152 | |
| 153 | /* Reserve a callee-save register. Return -1 if none available */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 154 | int Mir2Lir::AllocPreservedCoreReg(int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 155 | int res = -1; |
| 156 | RegisterInfo* core_regs = reg_pool_->core_regs; |
| 157 | for (int i = 0; i < reg_pool_->num_core_regs; i++) { |
| 158 | if (!core_regs[i].is_temp && !core_regs[i].in_use) { |
| 159 | res = core_regs[i].reg; |
| 160 | RecordCorePromotion(res, s_reg); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | return res; |
| 165 | } |
| 166 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 167 | void Mir2Lir::RecordFpPromotion(int reg, int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 168 | int p_map_idx = SRegToPMap(s_reg); |
| 169 | int v_reg = mir_graph_->SRegToVReg(s_reg); |
| 170 | GetRegInfo(reg)->in_use = true; |
| 171 | MarkPreservedSingle(v_reg, reg); |
| 172 | promotion_map_[p_map_idx].fp_location = kLocPhysReg; |
| 173 | promotion_map_[p_map_idx].FpReg = reg; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Reserve a callee-save fp single register. Try to fullfill request for |
| 178 | * even/odd allocation, but go ahead and allocate anything if not |
| 179 | * available. If nothing's available, return -1. |
| 180 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 181 | int Mir2Lir::AllocPreservedSingle(int s_reg, bool even) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 182 | int res = -1; |
| 183 | RegisterInfo* FPRegs = reg_pool_->FPRegs; |
| 184 | for (int i = 0; i < reg_pool_->num_fp_regs; i++) { |
| 185 | if (!FPRegs[i].is_temp && !FPRegs[i].in_use && |
| 186 | ((FPRegs[i].reg & 0x1) == 0) == even) { |
| 187 | res = FPRegs[i].reg; |
| 188 | RecordFpPromotion(res, s_reg); |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | return res; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Somewhat messy code here. We want to allocate a pair of contiguous |
| 197 | * physical single-precision floating point registers starting with |
| 198 | * an even numbered reg. It is possible that the paired s_reg (s_reg+1) |
| 199 | * has already been allocated - try to fit if possible. Fail to |
| 200 | * allocate if we can't meet the requirements for the pair of |
| 201 | * s_reg<=sX[even] & (s_reg+1)<= sX+1. |
| 202 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 203 | int Mir2Lir::AllocPreservedDouble(int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 204 | int res = -1; // Assume failure |
| 205 | int v_reg = mir_graph_->SRegToVReg(s_reg); |
| 206 | int p_map_idx = SRegToPMap(s_reg); |
| 207 | if (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg) { |
| 208 | // Upper reg is already allocated. Can we fit? |
| 209 | int high_reg = promotion_map_[p_map_idx+1].FpReg; |
| 210 | if ((high_reg & 1) == 0) { |
| 211 | // High reg is even - fail. |
| 212 | return res; |
| 213 | } |
| 214 | // Is the low reg of the pair free? |
| 215 | RegisterInfo* p = GetRegInfo(high_reg-1); |
| 216 | if (p->in_use || p->is_temp) { |
| 217 | // Already allocated or not preserved - fail. |
| 218 | return res; |
| 219 | } |
| 220 | // OK - good to go. |
| 221 | res = p->reg; |
| 222 | p->in_use = true; |
| 223 | DCHECK_EQ((res & 1), 0); |
| 224 | MarkPreservedSingle(v_reg, res); |
| 225 | } else { |
| 226 | RegisterInfo* FPRegs = reg_pool_->FPRegs; |
| 227 | for (int i = 0; i < reg_pool_->num_fp_regs; i++) { |
| 228 | if (!FPRegs[i].is_temp && !FPRegs[i].in_use && |
| 229 | ((FPRegs[i].reg & 0x1) == 0x0) && |
| 230 | !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use && |
| 231 | ((FPRegs[i+1].reg & 0x1) == 0x1) && |
| 232 | (FPRegs[i].reg + 1) == FPRegs[i+1].reg) { |
| 233 | res = FPRegs[i].reg; |
| 234 | FPRegs[i].in_use = true; |
| 235 | MarkPreservedSingle(v_reg, res); |
| 236 | FPRegs[i+1].in_use = true; |
| 237 | DCHECK_EQ(res + 1, FPRegs[i+1].reg); |
| 238 | MarkPreservedSingle(v_reg+1, res+1); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | if (res != -1) { |
| 244 | promotion_map_[p_map_idx].fp_location = kLocPhysReg; |
| 245 | promotion_map_[p_map_idx].FpReg = res; |
| 246 | promotion_map_[p_map_idx+1].fp_location = kLocPhysReg; |
| 247 | promotion_map_[p_map_idx+1].FpReg = res + 1; |
| 248 | } |
| 249 | return res; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | /* |
| 254 | * Reserve a callee-save fp register. If this register can be used |
| 255 | * as the first of a double, attempt to allocate an even pair of fp |
| 256 | * single regs (but if can't still attempt to allocate a single, preferring |
| 257 | * first to allocate an odd register. |
| 258 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 259 | int Mir2Lir::AllocPreservedFPReg(int s_reg, bool double_start) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 260 | int res = -1; |
| 261 | if (double_start) { |
| 262 | res = AllocPreservedDouble(s_reg); |
| 263 | } |
| 264 | if (res == -1) { |
| 265 | res = AllocPreservedSingle(s_reg, false /* try odd # */); |
| 266 | } |
| 267 | if (res == -1) |
| 268 | res = AllocPreservedSingle(s_reg, true /* try even # */); |
| 269 | return res; |
| 270 | } |
| 271 | |
| 272 | int Mir2Lir::AllocTempBody(RegisterInfo* p, int num_regs, int* next_temp, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 273 | bool required) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 274 | int i; |
| 275 | int next = *next_temp; |
| 276 | for (i=0; i< num_regs; i++) { |
| 277 | if (next >= num_regs) |
| 278 | next = 0; |
| 279 | if (p[next].is_temp && !p[next].in_use && !p[next].live) { |
| 280 | Clobber(p[next].reg); |
| 281 | p[next].in_use = true; |
| 282 | p[next].pair = false; |
| 283 | *next_temp = next + 1; |
| 284 | return p[next].reg; |
| 285 | } |
| 286 | next++; |
| 287 | } |
| 288 | next = *next_temp; |
| 289 | for (i=0; i< num_regs; i++) { |
| 290 | if (next >= num_regs) |
| 291 | next = 0; |
| 292 | if (p[next].is_temp && !p[next].in_use) { |
| 293 | Clobber(p[next].reg); |
| 294 | p[next].in_use = true; |
| 295 | p[next].pair = false; |
| 296 | *next_temp = next + 1; |
| 297 | return p[next].reg; |
| 298 | } |
| 299 | next++; |
| 300 | } |
| 301 | if (required) { |
| 302 | CodegenDump(); |
| 303 | DumpRegPool(reg_pool_->core_regs, |
| 304 | reg_pool_->num_core_regs); |
| 305 | LOG(FATAL) << "No free temp registers"; |
| 306 | } |
| 307 | return -1; // No register available |
| 308 | } |
| 309 | |
| 310 | //REDO: too many assumptions. |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 311 | int Mir2Lir::AllocTempDouble() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 312 | RegisterInfo* p = reg_pool_->FPRegs; |
| 313 | int num_regs = reg_pool_->num_fp_regs; |
| 314 | /* Start looking at an even reg */ |
| 315 | int next = reg_pool_->next_fp_reg & ~0x1; |
| 316 | |
| 317 | // First try to avoid allocating live registers |
| 318 | for (int i=0; i < num_regs; i+=2) { |
| 319 | if (next >= num_regs) |
| 320 | next = 0; |
| 321 | if ((p[next].is_temp && !p[next].in_use && !p[next].live) && |
| 322 | (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) { |
| 323 | Clobber(p[next].reg); |
| 324 | Clobber(p[next+1].reg); |
| 325 | p[next].in_use = true; |
| 326 | p[next+1].in_use = true; |
| 327 | DCHECK_EQ((p[next].reg+1), p[next+1].reg); |
| 328 | DCHECK_EQ((p[next].reg & 0x1), 0); |
| 329 | reg_pool_->next_fp_reg = next + 2; |
| 330 | if (reg_pool_->next_fp_reg >= num_regs) { |
| 331 | reg_pool_->next_fp_reg = 0; |
| 332 | } |
| 333 | return p[next].reg; |
| 334 | } |
| 335 | next += 2; |
| 336 | } |
| 337 | next = reg_pool_->next_fp_reg & ~0x1; |
| 338 | |
| 339 | // No choice - find a pair and kill it. |
| 340 | for (int i=0; i < num_regs; i+=2) { |
| 341 | if (next >= num_regs) |
| 342 | next = 0; |
| 343 | if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp && |
| 344 | !p[next+1].in_use) { |
| 345 | Clobber(p[next].reg); |
| 346 | Clobber(p[next+1].reg); |
| 347 | p[next].in_use = true; |
| 348 | p[next+1].in_use = true; |
| 349 | DCHECK_EQ((p[next].reg+1), p[next+1].reg); |
| 350 | DCHECK_EQ((p[next].reg & 0x1), 0); |
| 351 | reg_pool_->next_fp_reg = next + 2; |
| 352 | if (reg_pool_->next_fp_reg >= num_regs) { |
| 353 | reg_pool_->next_fp_reg = 0; |
| 354 | } |
| 355 | return p[next].reg; |
| 356 | } |
| 357 | next += 2; |
| 358 | } |
| 359 | LOG(FATAL) << "No free temp registers (pair)"; |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | /* Return a temp if one is available, -1 otherwise */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 364 | int Mir2Lir::AllocFreeTemp() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 365 | return AllocTempBody(reg_pool_->core_regs, |
| 366 | reg_pool_->num_core_regs, |
| 367 | ®_pool_->next_core_reg, true); |
| 368 | } |
| 369 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 370 | int Mir2Lir::AllocTemp() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 371 | return AllocTempBody(reg_pool_->core_regs, |
| 372 | reg_pool_->num_core_regs, |
| 373 | ®_pool_->next_core_reg, true); |
| 374 | } |
| 375 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 376 | int Mir2Lir::AllocTempFloat() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 377 | return AllocTempBody(reg_pool_->FPRegs, |
| 378 | reg_pool_->num_fp_regs, |
| 379 | ®_pool_->next_fp_reg, true); |
| 380 | } |
| 381 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 382 | Mir2Lir::RegisterInfo* Mir2Lir::AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 383 | int i; |
| 384 | if (s_reg == -1) |
| 385 | return NULL; |
| 386 | for (i=0; i < num_regs; i++) { |
| 387 | if (p[i].live && (p[i].s_reg == s_reg)) { |
| 388 | if (p[i].is_temp) |
| 389 | p[i].in_use = true; |
| 390 | return &p[i]; |
| 391 | } |
| 392 | } |
| 393 | return NULL; |
| 394 | } |
| 395 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 396 | Mir2Lir::RegisterInfo* Mir2Lir::AllocLive(int s_reg, int reg_class) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 397 | RegisterInfo* res = NULL; |
| 398 | switch (reg_class) { |
| 399 | case kAnyReg: |
| 400 | res = AllocLiveBody(reg_pool_->FPRegs, |
| 401 | reg_pool_->num_fp_regs, s_reg); |
| 402 | if (res) |
| 403 | break; |
| 404 | /* Intentional fallthrough */ |
| 405 | case kCoreReg: |
| 406 | res = AllocLiveBody(reg_pool_->core_regs, |
| 407 | reg_pool_->num_core_regs, s_reg); |
| 408 | break; |
| 409 | case kFPReg: |
| 410 | res = AllocLiveBody(reg_pool_->FPRegs, |
| 411 | reg_pool_->num_fp_regs, s_reg); |
| 412 | break; |
| 413 | default: |
| 414 | LOG(FATAL) << "Invalid register type"; |
| 415 | } |
| 416 | return res; |
| 417 | } |
| 418 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 419 | void Mir2Lir::FreeTemp(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 420 | RegisterInfo* p = reg_pool_->core_regs; |
| 421 | int num_regs = reg_pool_->num_core_regs; |
| 422 | int i; |
| 423 | for (i=0; i< num_regs; i++) { |
| 424 | if (p[i].reg == reg) { |
| 425 | if (p[i].is_temp) { |
| 426 | p[i].in_use = false; |
| 427 | } |
| 428 | p[i].pair = false; |
| 429 | return; |
| 430 | } |
| 431 | } |
| 432 | p = reg_pool_->FPRegs; |
| 433 | num_regs = reg_pool_->num_fp_regs; |
| 434 | for (i=0; i< num_regs; i++) { |
| 435 | if (p[i].reg == reg) { |
| 436 | if (p[i].is_temp) { |
| 437 | p[i].in_use = false; |
| 438 | } |
| 439 | p[i].pair = false; |
| 440 | return; |
| 441 | } |
| 442 | } |
| 443 | LOG(FATAL) << "Tried to free a non-existant temp: r" << reg; |
| 444 | } |
| 445 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 446 | Mir2Lir::RegisterInfo* Mir2Lir::IsLive(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 447 | RegisterInfo* p = reg_pool_->core_regs; |
| 448 | int num_regs = reg_pool_->num_core_regs; |
| 449 | int i; |
| 450 | for (i=0; i< num_regs; i++) { |
| 451 | if (p[i].reg == reg) { |
| 452 | return p[i].live ? &p[i] : NULL; |
| 453 | } |
| 454 | } |
| 455 | p = reg_pool_->FPRegs; |
| 456 | num_regs = reg_pool_->num_fp_regs; |
| 457 | for (i=0; i< num_regs; i++) { |
| 458 | if (p[i].reg == reg) { |
| 459 | return p[i].live ? &p[i] : NULL; |
| 460 | } |
| 461 | } |
| 462 | return NULL; |
| 463 | } |
| 464 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 465 | Mir2Lir::RegisterInfo* Mir2Lir::IsTemp(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 466 | RegisterInfo* p = GetRegInfo(reg); |
| 467 | return (p->is_temp) ? p : NULL; |
| 468 | } |
| 469 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 470 | Mir2Lir::RegisterInfo* Mir2Lir::IsPromoted(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 471 | RegisterInfo* p = GetRegInfo(reg); |
| 472 | return (p->is_temp) ? NULL : p; |
| 473 | } |
| 474 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 475 | bool Mir2Lir::IsDirty(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 476 | RegisterInfo* p = GetRegInfo(reg); |
| 477 | return p->dirty; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Similar to AllocTemp(), but forces the allocation of a specific |
| 482 | * register. No check is made to see if the register was previously |
| 483 | * allocated. Use with caution. |
| 484 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 485 | void Mir2Lir::LockTemp(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 486 | RegisterInfo* p = reg_pool_->core_regs; |
| 487 | int num_regs = reg_pool_->num_core_regs; |
| 488 | int i; |
| 489 | for (i=0; i< num_regs; i++) { |
| 490 | if (p[i].reg == reg) { |
| 491 | DCHECK(p[i].is_temp); |
| 492 | p[i].in_use = true; |
| 493 | p[i].live = false; |
| 494 | return; |
| 495 | } |
| 496 | } |
| 497 | p = reg_pool_->FPRegs; |
| 498 | num_regs = reg_pool_->num_fp_regs; |
| 499 | for (i=0; i< num_regs; i++) { |
| 500 | if (p[i].reg == reg) { |
| 501 | DCHECK(p[i].is_temp); |
| 502 | p[i].in_use = true; |
| 503 | p[i].live = false; |
| 504 | return; |
| 505 | } |
| 506 | } |
| 507 | LOG(FATAL) << "Tried to lock a non-existant temp: r" << reg; |
| 508 | } |
| 509 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 510 | void Mir2Lir::ResetDef(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 511 | ResetDefBody(GetRegInfo(reg)); |
| 512 | } |
| 513 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 514 | void Mir2Lir::NullifyRange(LIR *start, LIR *finish, int s_reg1, int s_reg2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 515 | if (start && finish) { |
| 516 | LIR *p; |
| 517 | DCHECK_EQ(s_reg1, s_reg2); |
| 518 | for (p = start; ;p = p->next) { |
| 519 | NopLIR(p); |
| 520 | if (p == finish) |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Mark the beginning and end LIR of a def sequence. Note that |
| 528 | * on entry start points to the LIR prior to the beginning of the |
| 529 | * sequence. |
| 530 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 531 | void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 532 | DCHECK(!rl.wide); |
| 533 | DCHECK(start && start->next); |
| 534 | DCHECK(finish); |
| 535 | RegisterInfo* p = GetRegInfo(rl.low_reg); |
| 536 | p->def_start = start->next; |
| 537 | p->def_end = finish; |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * Mark the beginning and end LIR of a def sequence. Note that |
| 542 | * on entry start points to the LIR prior to the beginning of the |
| 543 | * sequence. |
| 544 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 545 | void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 546 | DCHECK(rl.wide); |
| 547 | DCHECK(start && start->next); |
| 548 | DCHECK(finish); |
| 549 | RegisterInfo* p = GetRegInfo(rl.low_reg); |
| 550 | ResetDef(rl.high_reg); // Only track low of pair |
| 551 | p->def_start = start->next; |
| 552 | p->def_end = finish; |
| 553 | } |
| 554 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 555 | RegLocation Mir2Lir::WideToNarrow(RegLocation rl) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 556 | DCHECK(rl.wide); |
| 557 | if (rl.location == kLocPhysReg) { |
| 558 | RegisterInfo* info_lo = GetRegInfo(rl.low_reg); |
| 559 | RegisterInfo* info_hi = GetRegInfo(rl.high_reg); |
| 560 | if (info_lo->is_temp) { |
| 561 | info_lo->pair = false; |
| 562 | info_lo->def_start = NULL; |
| 563 | info_lo->def_end = NULL; |
| 564 | } |
| 565 | if (info_hi->is_temp) { |
| 566 | info_hi->pair = false; |
| 567 | info_hi->def_start = NULL; |
| 568 | info_hi->def_end = NULL; |
| 569 | } |
| 570 | } |
| 571 | rl.wide = false; |
| 572 | return rl; |
| 573 | } |
| 574 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 575 | void Mir2Lir::ResetDefLoc(RegLocation rl) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 576 | DCHECK(!rl.wide); |
| 577 | RegisterInfo* p = IsTemp(rl.low_reg); |
| 578 | if (p && !(cu_->disable_opt & (1 << kSuppressLoads))) { |
| 579 | DCHECK(!p->pair); |
| 580 | NullifyRange(p->def_start, p->def_end, p->s_reg, rl.s_reg_low); |
| 581 | } |
| 582 | ResetDef(rl.low_reg); |
| 583 | } |
| 584 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 585 | void Mir2Lir::ResetDefLocWide(RegLocation rl) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 586 | DCHECK(rl.wide); |
| 587 | RegisterInfo* p_low = IsTemp(rl.low_reg); |
| 588 | RegisterInfo* p_high = IsTemp(rl.high_reg); |
| 589 | if (p_low && !(cu_->disable_opt & (1 << kSuppressLoads))) { |
| 590 | DCHECK(p_low->pair); |
| 591 | NullifyRange(p_low->def_start, p_low->def_end, p_low->s_reg, rl.s_reg_low); |
| 592 | } |
| 593 | if (p_high && !(cu_->disable_opt & (1 << kSuppressLoads))) { |
| 594 | DCHECK(p_high->pair); |
| 595 | } |
| 596 | ResetDef(rl.low_reg); |
| 597 | ResetDef(rl.high_reg); |
| 598 | } |
| 599 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 600 | void Mir2Lir::ResetDefTracking() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 601 | int i; |
| 602 | for (i=0; i< reg_pool_->num_core_regs; i++) { |
| 603 | ResetDefBody(®_pool_->core_regs[i]); |
| 604 | } |
| 605 | for (i=0; i< reg_pool_->num_fp_regs; i++) { |
| 606 | ResetDefBody(®_pool_->FPRegs[i]); |
| 607 | } |
| 608 | } |
| 609 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 610 | void Mir2Lir::ClobberAllRegs() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 611 | int i; |
| 612 | for (i=0; i< reg_pool_->num_core_regs; i++) { |
| 613 | ClobberBody(®_pool_->core_regs[i]); |
| 614 | } |
| 615 | for (i=0; i< reg_pool_->num_fp_regs; i++) { |
| 616 | ClobberBody(®_pool_->FPRegs[i]); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // Make sure nothing is live and dirty |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 621 | void Mir2Lir::FlushAllRegsBody(RegisterInfo* info, int num_regs) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 622 | int i; |
| 623 | for (i=0; i < num_regs; i++) { |
| 624 | if (info[i].live && info[i].dirty) { |
| 625 | if (info[i].pair) { |
| 626 | FlushRegWide(info[i].reg, info[i].partner); |
| 627 | } else { |
| 628 | FlushReg(info[i].reg); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 634 | void Mir2Lir::FlushAllRegs() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 635 | FlushAllRegsBody(reg_pool_->core_regs, |
| 636 | reg_pool_->num_core_regs); |
| 637 | FlushAllRegsBody(reg_pool_->FPRegs, |
| 638 | reg_pool_->num_fp_regs); |
| 639 | ClobberAllRegs(); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | //TUNING: rewrite all of this reg stuff. Probably use an attribute table |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 644 | bool Mir2Lir::RegClassMatches(int reg_class, int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 645 | if (reg_class == kAnyReg) { |
| 646 | return true; |
| 647 | } else if (reg_class == kCoreReg) { |
| 648 | return !IsFpReg(reg); |
| 649 | } else { |
| 650 | return IsFpReg(reg); |
| 651 | } |
| 652 | } |
| 653 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 654 | void Mir2Lir::MarkLive(int reg, int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 655 | RegisterInfo* info = GetRegInfo(reg); |
| 656 | if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) { |
| 657 | return; /* already live */ |
| 658 | } else if (s_reg != INVALID_SREG) { |
| 659 | ClobberSReg(s_reg); |
| 660 | if (info->is_temp) { |
| 661 | info->live = true; |
| 662 | } |
| 663 | } else { |
| 664 | /* Can't be live if no associated s_reg */ |
| 665 | DCHECK(info->is_temp); |
| 666 | info->live = false; |
| 667 | } |
| 668 | info->s_reg = s_reg; |
| 669 | } |
| 670 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 671 | void Mir2Lir::MarkTemp(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 672 | RegisterInfo* info = GetRegInfo(reg); |
| 673 | info->is_temp = true; |
| 674 | } |
| 675 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 676 | void Mir2Lir::UnmarkTemp(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 677 | RegisterInfo* info = GetRegInfo(reg); |
| 678 | info->is_temp = false; |
| 679 | } |
| 680 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 681 | void Mir2Lir::MarkPair(int low_reg, int high_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 682 | RegisterInfo* info_lo = GetRegInfo(low_reg); |
| 683 | RegisterInfo* info_hi = GetRegInfo(high_reg); |
| 684 | info_lo->pair = info_hi->pair = true; |
| 685 | info_lo->partner = high_reg; |
| 686 | info_hi->partner = low_reg; |
| 687 | } |
| 688 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 689 | void Mir2Lir::MarkClean(RegLocation loc) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 690 | RegisterInfo* info = GetRegInfo(loc.low_reg); |
| 691 | info->dirty = false; |
| 692 | if (loc.wide) { |
| 693 | info = GetRegInfo(loc.high_reg); |
| 694 | info->dirty = false; |
| 695 | } |
| 696 | } |
| 697 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 698 | void Mir2Lir::MarkDirty(RegLocation loc) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 699 | if (loc.home) { |
| 700 | // If already home, can't be dirty |
| 701 | return; |
| 702 | } |
| 703 | RegisterInfo* info = GetRegInfo(loc.low_reg); |
| 704 | info->dirty = true; |
| 705 | if (loc.wide) { |
| 706 | info = GetRegInfo(loc.high_reg); |
| 707 | info->dirty = true; |
| 708 | } |
| 709 | } |
| 710 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 711 | void Mir2Lir::MarkInUse(int reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 712 | RegisterInfo* info = GetRegInfo(reg); |
| 713 | info->in_use = true; |
| 714 | } |
| 715 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 716 | void Mir2Lir::CopyRegInfo(int new_reg, int old_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 717 | RegisterInfo* new_info = GetRegInfo(new_reg); |
| 718 | RegisterInfo* old_info = GetRegInfo(old_reg); |
| 719 | // Target temp status must not change |
| 720 | bool is_temp = new_info->is_temp; |
| 721 | *new_info = *old_info; |
| 722 | // Restore target's temp status |
| 723 | new_info->is_temp = is_temp; |
| 724 | new_info->reg = new_reg; |
| 725 | } |
| 726 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 727 | bool Mir2Lir::CheckCorePoolSanity() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 728 | for (static int i = 0; i < reg_pool_->num_core_regs; i++) { |
| 729 | if (reg_pool_->core_regs[i].pair) { |
| 730 | static int my_reg = reg_pool_->core_regs[i].reg; |
| 731 | static int my_sreg = reg_pool_->core_regs[i].s_reg; |
| 732 | static int partner_reg = reg_pool_->core_regs[i].partner; |
| 733 | static RegisterInfo* partner = GetRegInfo(partner_reg); |
| 734 | DCHECK(partner != NULL); |
| 735 | DCHECK(partner->pair); |
| 736 | DCHECK_EQ(my_reg, partner->partner); |
| 737 | static int partner_sreg = partner->s_reg; |
| 738 | if (my_sreg == INVALID_SREG) { |
| 739 | DCHECK_EQ(partner_sreg, INVALID_SREG); |
| 740 | } else { |
| 741 | int diff = my_sreg - partner_sreg; |
| 742 | DCHECK((diff == -1) || (diff == 1)); |
| 743 | } |
| 744 | } |
| 745 | if (!reg_pool_->core_regs[i].live) { |
| 746 | DCHECK(reg_pool_->core_regs[i].def_start == NULL); |
| 747 | DCHECK(reg_pool_->core_regs[i].def_end == NULL); |
| 748 | } |
| 749 | } |
| 750 | return true; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Return an updated location record with current in-register status. |
| 755 | * If the value lives in live temps, reflect that fact. No code |
| 756 | * is generated. If the live value is part of an older pair, |
| 757 | * clobber both low and high. |
| 758 | * TUNING: clobbering both is a bit heavy-handed, but the alternative |
| 759 | * is a bit complex when dealing with FP regs. Examine code to see |
| 760 | * if it's worthwhile trying to be more clever here. |
| 761 | */ |
| 762 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 763 | RegLocation Mir2Lir::UpdateLoc(RegLocation loc) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 764 | DCHECK(!loc.wide); |
| 765 | DCHECK(CheckCorePoolSanity()); |
| 766 | if (loc.location != kLocPhysReg) { |
| 767 | DCHECK((loc.location == kLocDalvikFrame) || |
| 768 | (loc.location == kLocCompilerTemp)); |
| 769 | RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg); |
| 770 | if (info_lo) { |
| 771 | if (info_lo->pair) { |
| 772 | Clobber(info_lo->reg); |
| 773 | Clobber(info_lo->partner); |
| 774 | FreeTemp(info_lo->reg); |
| 775 | } else { |
| 776 | loc.low_reg = info_lo->reg; |
| 777 | loc.location = kLocPhysReg; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | return loc; |
| 783 | } |
| 784 | |
| 785 | /* see comments for update_loc */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 786 | RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 787 | DCHECK(loc.wide); |
| 788 | DCHECK(CheckCorePoolSanity()); |
| 789 | if (loc.location != kLocPhysReg) { |
| 790 | DCHECK((loc.location == kLocDalvikFrame) || |
| 791 | (loc.location == kLocCompilerTemp)); |
| 792 | // Are the dalvik regs already live in physical registers? |
| 793 | RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg); |
| 794 | RegisterInfo* info_hi = AllocLive(GetSRegHi(loc.s_reg_low), kAnyReg); |
| 795 | bool match = true; |
| 796 | match = match && (info_lo != NULL); |
| 797 | match = match && (info_hi != NULL); |
| 798 | // Are they both core or both FP? |
| 799 | match = match && (IsFpReg(info_lo->reg) == IsFpReg(info_hi->reg)); |
| 800 | // If a pair of floating point singles, are they properly aligned? |
| 801 | if (match && IsFpReg(info_lo->reg)) { |
| 802 | match &= ((info_lo->reg & 0x1) == 0); |
| 803 | match &= ((info_hi->reg - info_lo->reg) == 1); |
| 804 | } |
| 805 | // If previously used as a pair, it is the same pair? |
| 806 | if (match && (info_lo->pair || info_hi->pair)) { |
| 807 | match = (info_lo->pair == info_hi->pair); |
| 808 | match &= ((info_lo->reg == info_hi->partner) && |
| 809 | (info_hi->reg == info_lo->partner)); |
| 810 | } |
| 811 | if (match) { |
| 812 | // Can reuse - update the register usage info |
| 813 | loc.low_reg = info_lo->reg; |
| 814 | loc.high_reg = info_hi->reg; |
| 815 | loc.location = kLocPhysReg; |
| 816 | MarkPair(loc.low_reg, loc.high_reg); |
| 817 | DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0)); |
| 818 | return loc; |
| 819 | } |
| 820 | // Can't easily reuse - clobber and free any overlaps |
| 821 | if (info_lo) { |
| 822 | Clobber(info_lo->reg); |
| 823 | FreeTemp(info_lo->reg); |
| 824 | if (info_lo->pair) |
| 825 | Clobber(info_lo->partner); |
| 826 | } |
| 827 | if (info_hi) { |
| 828 | Clobber(info_hi->reg); |
| 829 | FreeTemp(info_hi->reg); |
| 830 | if (info_hi->pair) |
| 831 | Clobber(info_hi->partner); |
| 832 | } |
| 833 | } |
| 834 | return loc; |
| 835 | } |
| 836 | |
| 837 | |
| 838 | /* For use in cases we don't know (or care) width */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 839 | RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 840 | if (loc.wide) |
| 841 | return UpdateLocWide(loc); |
| 842 | else |
| 843 | return UpdateLoc(loc); |
| 844 | } |
| 845 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 846 | RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 847 | DCHECK(loc.wide); |
| 848 | int new_regs; |
| 849 | int low_reg; |
| 850 | int high_reg; |
| 851 | |
| 852 | loc = UpdateLocWide(loc); |
| 853 | |
| 854 | /* If already in registers, we can assume proper form. Right reg class? */ |
| 855 | if (loc.location == kLocPhysReg) { |
| 856 | DCHECK_EQ(IsFpReg(loc.low_reg), IsFpReg(loc.high_reg)); |
| 857 | DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0)); |
| 858 | if (!RegClassMatches(reg_class, loc.low_reg)) { |
| 859 | /* Wrong register class. Reallocate and copy */ |
| 860 | new_regs = AllocTypedTempPair(loc.fp, reg_class); |
| 861 | low_reg = new_regs & 0xff; |
| 862 | high_reg = (new_regs >> 8) & 0xff; |
| 863 | OpRegCopyWide(low_reg, high_reg, loc.low_reg, loc.high_reg); |
| 864 | CopyRegInfo(low_reg, loc.low_reg); |
| 865 | CopyRegInfo(high_reg, loc.high_reg); |
| 866 | Clobber(loc.low_reg); |
| 867 | Clobber(loc.high_reg); |
| 868 | loc.low_reg = low_reg; |
| 869 | loc.high_reg = high_reg; |
| 870 | MarkPair(loc.low_reg, loc.high_reg); |
| 871 | DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0)); |
| 872 | } |
| 873 | return loc; |
| 874 | } |
| 875 | |
| 876 | DCHECK_NE(loc.s_reg_low, INVALID_SREG); |
| 877 | DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG); |
| 878 | |
| 879 | new_regs = AllocTypedTempPair(loc.fp, reg_class); |
| 880 | loc.low_reg = new_regs & 0xff; |
| 881 | loc.high_reg = (new_regs >> 8) & 0xff; |
| 882 | |
| 883 | MarkPair(loc.low_reg, loc.high_reg); |
| 884 | if (update) { |
| 885 | loc.location = kLocPhysReg; |
| 886 | MarkLive(loc.low_reg, loc.s_reg_low); |
| 887 | MarkLive(loc.high_reg, GetSRegHi(loc.s_reg_low)); |
| 888 | } |
| 889 | DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0)); |
| 890 | return loc; |
| 891 | } |
| 892 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 893 | RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 894 | int new_reg; |
| 895 | |
| 896 | if (loc.wide) |
| 897 | return EvalLocWide(loc, reg_class, update); |
| 898 | |
| 899 | loc = UpdateLoc(loc); |
| 900 | |
| 901 | if (loc.location == kLocPhysReg) { |
| 902 | if (!RegClassMatches(reg_class, loc.low_reg)) { |
| 903 | /* Wrong register class. Realloc, copy and transfer ownership */ |
| 904 | new_reg = AllocTypedTemp(loc.fp, reg_class); |
| 905 | OpRegCopy(new_reg, loc.low_reg); |
| 906 | CopyRegInfo(new_reg, loc.low_reg); |
| 907 | Clobber(loc.low_reg); |
| 908 | loc.low_reg = new_reg; |
| 909 | } |
| 910 | return loc; |
| 911 | } |
| 912 | |
| 913 | DCHECK_NE(loc.s_reg_low, INVALID_SREG); |
| 914 | |
| 915 | new_reg = AllocTypedTemp(loc.fp, reg_class); |
| 916 | loc.low_reg = new_reg; |
| 917 | |
| 918 | if (update) { |
| 919 | loc.location = kLocPhysReg; |
| 920 | MarkLive(loc.low_reg, loc.s_reg_low); |
| 921 | } |
| 922 | return loc; |
| 923 | } |
| 924 | |
| 925 | /* USE SSA names to count references of base Dalvik v_regs. */ |
| 926 | void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts) { |
| 927 | for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) { |
| 928 | RegLocation loc = mir_graph_->reg_location_[i]; |
| 929 | RefCounts* counts = loc.fp ? fp_counts : core_counts; |
| 930 | int p_map_idx = SRegToPMap(loc.s_reg_low); |
| 931 | //Don't count easily regenerated immediates |
| 932 | if (loc.fp || !IsInexpensiveConstant(loc)) { |
| 933 | counts[p_map_idx].count += mir_graph_->GetUseCount(i); |
| 934 | } |
| 935 | if (loc.wide && loc.fp && !loc.high_word) { |
| 936 | counts[p_map_idx].double_start = true; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /* qsort callback function, sort descending */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 942 | static int SortCounts(const void *val1, const void *val2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 943 | const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1); |
| 944 | const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2); |
| 945 | return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1); |
| 946 | } |
| 947 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 948 | void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 949 | LOG(INFO) << msg; |
| 950 | for (int i = 0; i < size; i++) { |
| 951 | LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Note: some portions of this code required even if the kPromoteRegs |
| 957 | * optimization is disabled. |
| 958 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 959 | void Mir2Lir::DoPromotion() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 960 | int reg_bias = cu_->num_compiler_temps + 1; |
| 961 | int dalvik_regs = cu_->num_dalvik_registers; |
| 962 | int num_regs = dalvik_regs + reg_bias; |
| 963 | const int promotion_threshold = 1; |
| 964 | |
| 965 | // Allow target code to add any special registers |
| 966 | AdjustSpillMask(); |
| 967 | |
| 968 | /* |
| 969 | * Simple register promotion. Just do a static count of the uses |
| 970 | * of Dalvik registers. Note that we examine the SSA names, but |
| 971 | * count based on original Dalvik register name. Count refs |
| 972 | * separately based on type in order to give allocation |
| 973 | * preference to fp doubles - which must be allocated sequential |
| 974 | * physical single fp registers started with an even-numbered |
| 975 | * reg. |
| 976 | * TUNING: replace with linear scan once we have the ability |
| 977 | * to describe register live ranges for GC. |
| 978 | */ |
| 979 | RefCounts *core_regs = |
| 980 | static_cast<RefCounts*>(arena_->NewMem(sizeof(RefCounts) * num_regs, true, |
| 981 | ArenaAllocator::kAllocRegAlloc)); |
| 982 | RefCounts *FpRegs = |
| 983 | static_cast<RefCounts *>(arena_->NewMem(sizeof(RefCounts) * num_regs, true, |
| 984 | ArenaAllocator::kAllocRegAlloc)); |
| 985 | // Set ssa names for original Dalvik registers |
| 986 | for (int i = 0; i < dalvik_regs; i++) { |
| 987 | core_regs[i].s_reg = FpRegs[i].s_reg = i; |
| 988 | } |
| 989 | // Set ssa name for Method* |
| 990 | core_regs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg(); |
| 991 | FpRegs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg(); // For consistecy |
| 992 | // Set ssa names for compiler_temps |
| 993 | for (int i = 1; i <= cu_->num_compiler_temps; i++) { |
| 994 | CompilerTemp* ct = mir_graph_->compiler_temps_.Get(i); |
| 995 | core_regs[dalvik_regs + i].s_reg = ct->s_reg; |
| 996 | FpRegs[dalvik_regs + i].s_reg = ct->s_reg; |
| 997 | } |
| 998 | |
| 999 | // Sum use counts of SSA regs by original Dalvik vreg. |
| 1000 | CountRefs(core_regs, FpRegs); |
| 1001 | |
| 1002 | /* |
| 1003 | * Ideally, we'd allocate doubles starting with an even-numbered |
| 1004 | * register. Bias the counts to try to allocate any vreg that's |
| 1005 | * used as the start of a pair first. |
| 1006 | */ |
| 1007 | for (int i = 0; i < num_regs; i++) { |
| 1008 | if (FpRegs[i].double_start) { |
| 1009 | FpRegs[i].count *= 2; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | // Sort the count arrays |
| 1014 | qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts); |
| 1015 | qsort(FpRegs, num_regs, sizeof(RefCounts), SortCounts); |
| 1016 | |
| 1017 | if (cu_->verbose) { |
| 1018 | DumpCounts(core_regs, num_regs, "Core regs after sort"); |
| 1019 | DumpCounts(FpRegs, num_regs, "Fp regs after sort"); |
| 1020 | } |
| 1021 | |
| 1022 | if (!(cu_->disable_opt & (1 << kPromoteRegs))) { |
| 1023 | // Promote FpRegs |
| 1024 | for (int i = 0; (i < num_regs) && |
| 1025 | (FpRegs[i].count >= promotion_threshold ); i++) { |
| 1026 | int p_map_idx = SRegToPMap(FpRegs[i].s_reg); |
| 1027 | if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) { |
| 1028 | int reg = AllocPreservedFPReg(FpRegs[i].s_reg, |
| 1029 | FpRegs[i].double_start); |
| 1030 | if (reg < 0) { |
| 1031 | break; // No more left |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | // Promote core regs |
| 1037 | for (int i = 0; (i < num_regs) && |
| 1038 | (core_regs[i].count >= promotion_threshold); i++) { |
| 1039 | int p_map_idx = SRegToPMap(core_regs[i].s_reg); |
| 1040 | if (promotion_map_[p_map_idx].core_location != |
| 1041 | kLocPhysReg) { |
| 1042 | int reg = AllocPreservedCoreReg(core_regs[i].s_reg); |
| 1043 | if (reg < 0) { |
| 1044 | break; // No more left |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | // Now, update SSA names to new home locations |
| 1051 | for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) { |
| 1052 | RegLocation *curr = &mir_graph_->reg_location_[i]; |
| 1053 | int p_map_idx = SRegToPMap(curr->s_reg_low); |
| 1054 | if (!curr->wide) { |
| 1055 | if (curr->fp) { |
| 1056 | if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) { |
| 1057 | curr->location = kLocPhysReg; |
| 1058 | curr->low_reg = promotion_map_[p_map_idx].FpReg; |
| 1059 | curr->home = true; |
| 1060 | } |
| 1061 | } else { |
| 1062 | if (promotion_map_[p_map_idx].core_location == kLocPhysReg) { |
| 1063 | curr->location = kLocPhysReg; |
| 1064 | curr->low_reg = promotion_map_[p_map_idx].core_reg; |
| 1065 | curr->home = true; |
| 1066 | } |
| 1067 | } |
| 1068 | curr->high_reg = INVALID_REG; |
| 1069 | } else { |
| 1070 | if (curr->high_word) { |
| 1071 | continue; |
| 1072 | } |
| 1073 | if (curr->fp) { |
| 1074 | if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) && |
| 1075 | (promotion_map_[p_map_idx+1].fp_location == |
| 1076 | kLocPhysReg)) { |
| 1077 | int low_reg = promotion_map_[p_map_idx].FpReg; |
| 1078 | int high_reg = promotion_map_[p_map_idx+1].FpReg; |
| 1079 | // Doubles require pair of singles starting at even reg |
| 1080 | if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) { |
| 1081 | curr->location = kLocPhysReg; |
| 1082 | curr->low_reg = low_reg; |
| 1083 | curr->high_reg = high_reg; |
| 1084 | curr->home = true; |
| 1085 | } |
| 1086 | } |
| 1087 | } else { |
| 1088 | if ((promotion_map_[p_map_idx].core_location == kLocPhysReg) |
| 1089 | && (promotion_map_[p_map_idx+1].core_location == |
| 1090 | kLocPhysReg)) { |
| 1091 | curr->location = kLocPhysReg; |
| 1092 | curr->low_reg = promotion_map_[p_map_idx].core_reg; |
| 1093 | curr->high_reg = promotion_map_[p_map_idx+1].core_reg; |
| 1094 | curr->home = true; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | if (cu_->verbose) { |
| 1100 | DumpPromotionMap(); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /* Returns sp-relative offset in bytes for a VReg */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 1105 | int Mir2Lir::VRegOffset(int v_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1106 | return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_, |
| 1107 | fp_spill_mask_, frame_size_, v_reg); |
| 1108 | } |
| 1109 | |
| 1110 | /* Returns sp-relative offset in bytes for a SReg */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 1111 | int Mir2Lir::SRegOffset(int s_reg) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1112 | return VRegOffset(mir_graph_->SRegToVReg(s_reg)); |
| 1113 | } |
| 1114 | |
| 1115 | /* Mark register usage state and return long retloc */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 1116 | RegLocation Mir2Lir::GetReturnWide(bool is_double) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1117 | RegLocation gpr_res = LocCReturnWide(); |
| 1118 | RegLocation fpr_res = LocCReturnDouble(); |
| 1119 | RegLocation res = is_double ? fpr_res : gpr_res; |
| 1120 | Clobber(res.low_reg); |
| 1121 | Clobber(res.high_reg); |
| 1122 | LockTemp(res.low_reg); |
| 1123 | LockTemp(res.high_reg); |
| 1124 | MarkPair(res.low_reg, res.high_reg); |
| 1125 | return res; |
| 1126 | } |
| 1127 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 1128 | RegLocation Mir2Lir::GetReturn(bool is_float) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1129 | RegLocation gpr_res = LocCReturn(); |
| 1130 | RegLocation fpr_res = LocCReturnFloat(); |
| 1131 | RegLocation res = is_float ? fpr_res : gpr_res; |
| 1132 | Clobber(res.low_reg); |
| 1133 | if (cu_->instruction_set == kMips) { |
| 1134 | MarkInUse(res.low_reg); |
| 1135 | } else { |
| 1136 | LockTemp(res.low_reg); |
| 1137 | } |
| 1138 | return res; |
| 1139 | } |
| 1140 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame^] | 1141 | void Mir2Lir::SimpleRegAlloc() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1142 | DoPromotion(); |
| 1143 | |
| 1144 | if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) { |
| 1145 | LOG(INFO) << "After Promotion"; |
| 1146 | mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs()); |
| 1147 | } |
| 1148 | |
| 1149 | /* Set the frame size */ |
| 1150 | frame_size_ = ComputeFrameSize(); |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Get the "real" sreg number associated with an s_reg slot. In general, |
| 1155 | * s_reg values passed through codegen are the SSA names created by |
| 1156 | * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location |
| 1157 | * array. However, renaming is accomplished by simply replacing RegLocation |
| 1158 | * entries in the reglocation[] array. Therefore, when location |
| 1159 | * records for operands are first created, we need to ask the locRecord |
| 1160 | * identified by the dataflow pass what it's new name is. |
| 1161 | */ |
| 1162 | int Mir2Lir::GetSRegHi(int lowSreg) { |
| 1163 | return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1; |
| 1164 | } |
| 1165 | |
| 1166 | bool Mir2Lir::oat_live_out(int s_reg) { |
| 1167 | //For now. |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
| 1171 | int Mir2Lir::oatSSASrc(MIR* mir, int num) { |
| 1172 | DCHECK_GT(mir->ssa_rep->num_uses, num); |
| 1173 | return mir->ssa_rep->uses[num]; |
| 1174 | } |
| 1175 | |
| 1176 | } // namespace art |