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