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 | #include "dex/compiler_internals.h" |
| 18 | #include "dex_file-inl.h" |
| 19 | #include "gc_map.h" |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 20 | #include "mapping_table.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | #include "mir_to_lir-inl.h" |
| 22 | #include "verifier/dex_gc_map.h" |
| 23 | #include "verifier/method_verifier.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 27 | bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | bool res = false; |
| 29 | if (rl_src.is_const) { |
| 30 | if (rl_src.wide) { |
| 31 | if (rl_src.fp) { |
| 32 | res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src)); |
| 33 | } else { |
| 34 | res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src)); |
| 35 | } |
| 36 | } else { |
| 37 | if (rl_src.fp) { |
| 38 | res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src)); |
| 39 | } else { |
| 40 | res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src)); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | return res; |
| 45 | } |
| 46 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 47 | void Mir2Lir::MarkSafepointPC(LIR* inst) { |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 48 | DCHECK(!inst->flags.use_def_invalid); |
| 49 | inst->u.m.def_mask = ENCODE_ALL; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 50 | LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC); |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 51 | DCHECK_EQ(safepoint_pc->u.m.def_mask, ENCODE_ALL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Ian Rogers | 9b297bf | 2013-09-06 11:11:25 -0700 | [diff] [blame] | 54 | bool Mir2Lir::FastInstance(uint32_t field_idx, bool is_put, int* field_offset, bool* is_volatile) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 55 | return cu_->compiler_driver->ComputeInstanceFieldInfo( |
Ian Rogers | 9b297bf | 2013-09-06 11:11:25 -0700 | [diff] [blame] | 56 | field_idx, mir_graph_->GetCurrentDexCompilationUnit(), is_put, field_offset, is_volatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 57 | } |
| 58 | |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 59 | /* Remove a LIR from the list. */ |
| 60 | void Mir2Lir::UnlinkLIR(LIR* lir) { |
| 61 | if (UNLIKELY(lir == first_lir_insn_)) { |
| 62 | first_lir_insn_ = lir->next; |
| 63 | if (lir->next != NULL) { |
| 64 | lir->next->prev = NULL; |
| 65 | } else { |
| 66 | DCHECK(lir->next == NULL); |
| 67 | DCHECK(lir == last_lir_insn_); |
| 68 | last_lir_insn_ = NULL; |
| 69 | } |
| 70 | } else if (lir == last_lir_insn_) { |
| 71 | last_lir_insn_ = lir->prev; |
| 72 | lir->prev->next = NULL; |
| 73 | } else if ((lir->prev != NULL) && (lir->next != NULL)) { |
| 74 | lir->prev->next = lir->next; |
| 75 | lir->next->prev = lir->prev; |
| 76 | } |
| 77 | } |
| 78 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 79 | /* Convert an instruction to a NOP */ |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 80 | void Mir2Lir::NopLIR(LIR* lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 81 | lir->flags.is_nop = true; |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 82 | if (!cu_->verbose) { |
| 83 | UnlinkLIR(lir); |
| 84 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 87 | void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 88 | uint64_t *mask_ptr; |
Brian Carlstrom | f69863b | 2013-07-17 21:53:13 -0700 | [diff] [blame] | 89 | uint64_t mask = ENCODE_MEM; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 90 | DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE)); |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 91 | DCHECK(!lir->flags.use_def_invalid); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 92 | if (is_load) { |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 93 | mask_ptr = &lir->u.m.use_mask; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 94 | } else { |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 95 | mask_ptr = &lir->u.m.def_mask; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 96 | } |
| 97 | /* Clear out the memref flags */ |
| 98 | *mask_ptr &= ~mask; |
| 99 | /* ..and then add back the one we need */ |
| 100 | switch (mem_type) { |
| 101 | case kLiteral: |
| 102 | DCHECK(is_load); |
| 103 | *mask_ptr |= ENCODE_LITERAL; |
| 104 | break; |
| 105 | case kDalvikReg: |
| 106 | *mask_ptr |= ENCODE_DALVIK_REG; |
| 107 | break; |
| 108 | case kHeapRef: |
| 109 | *mask_ptr |= ENCODE_HEAP_REF; |
| 110 | break; |
| 111 | case kMustNotAlias: |
| 112 | /* Currently only loads can be marked as kMustNotAlias */ |
| 113 | DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE)); |
| 114 | *mask_ptr |= ENCODE_MUST_NOT_ALIAS; |
| 115 | break; |
| 116 | default: |
| 117 | LOG(FATAL) << "Oat: invalid memref kind - " << mem_type; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Mark load/store instructions that access Dalvik registers through the stack. |
| 123 | */ |
| 124 | void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 125 | bool is64bit) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 126 | SetMemRefType(lir, is_load, kDalvikReg); |
| 127 | |
| 128 | /* |
| 129 | * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit |
| 130 | * access. |
| 131 | */ |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 132 | lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Debugging macros |
| 137 | */ |
| 138 | #define DUMP_RESOURCE_MASK(X) |
| 139 | |
| 140 | /* Pretty-print a LIR instruction */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 141 | void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 142 | int offset = lir->offset; |
| 143 | int dest = lir->operands[0]; |
| 144 | const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops)); |
| 145 | |
| 146 | /* Handle pseudo-ops individually, and all regular insns as a group */ |
| 147 | switch (lir->opcode) { |
| 148 | case kPseudoMethodEntry: |
| 149 | LOG(INFO) << "-------- method entry " |
| 150 | << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 151 | break; |
| 152 | case kPseudoMethodExit: |
| 153 | LOG(INFO) << "-------- Method_Exit"; |
| 154 | break; |
| 155 | case kPseudoBarrier: |
| 156 | LOG(INFO) << "-------- BARRIER"; |
| 157 | break; |
| 158 | case kPseudoEntryBlock: |
| 159 | LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest; |
| 160 | break; |
| 161 | case kPseudoDalvikByteCodeBoundary: |
| 162 | if (lir->operands[0] == 0) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 163 | // NOTE: only used for debug listings. |
| 164 | lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string")); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 165 | } |
| 166 | LOG(INFO) << "-------- dalvik offset: 0x" << std::hex |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 167 | << lir->dalvik_offset << " @ " |
| 168 | << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0])); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 169 | break; |
| 170 | case kPseudoExitBlock: |
| 171 | LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest; |
| 172 | break; |
| 173 | case kPseudoPseudoAlign4: |
| 174 | LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex |
| 175 | << offset << "): .align4"; |
| 176 | break; |
| 177 | case kPseudoEHBlockLabel: |
| 178 | LOG(INFO) << "Exception_Handling:"; |
| 179 | break; |
| 180 | case kPseudoTargetLabel: |
| 181 | case kPseudoNormalBlockLabel: |
| 182 | LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":"; |
| 183 | break; |
| 184 | case kPseudoThrowTarget: |
| 185 | LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":"; |
| 186 | break; |
| 187 | case kPseudoIntrinsicRetry: |
| 188 | LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":"; |
| 189 | break; |
| 190 | case kPseudoSuspendTarget: |
| 191 | LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":"; |
| 192 | break; |
| 193 | case kPseudoSafepointPC: |
| 194 | LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":"; |
| 195 | break; |
| 196 | case kPseudoExportedPC: |
| 197 | LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":"; |
| 198 | break; |
| 199 | case kPseudoCaseLabel: |
| 200 | LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x" |
| 201 | << std::hex << lir->operands[0] << "|" << std::dec << |
| 202 | lir->operands[0]; |
| 203 | break; |
| 204 | default: |
| 205 | if (lir->flags.is_nop && !dump_nop) { |
| 206 | break; |
| 207 | } else { |
| 208 | std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode), |
| 209 | lir, base_addr)); |
| 210 | std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode), |
| 211 | lir, base_addr)); |
| 212 | LOG(INFO) << StringPrintf("%05x: %-9s%s%s", |
| 213 | reinterpret_cast<unsigned int>(base_addr + offset), |
| 214 | op_name.c_str(), op_operands.c_str(), |
| 215 | lir->flags.is_nop ? "(nop)" : ""); |
| 216 | } |
| 217 | break; |
| 218 | } |
| 219 | |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 220 | if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) { |
| 221 | DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.use_mask, "use")); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 222 | } |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 223 | if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) { |
| 224 | DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.def_mask, "def")); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 228 | void Mir2Lir::DumpPromotionMap() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 229 | int num_regs = cu_->num_dalvik_registers + cu_->num_compiler_temps + 1; |
| 230 | for (int i = 0; i < num_regs; i++) { |
| 231 | PromotionMap v_reg_map = promotion_map_[i]; |
| 232 | std::string buf; |
| 233 | if (v_reg_map.fp_location == kLocPhysReg) { |
| 234 | StringAppendF(&buf, " : s%d", v_reg_map.FpReg & FpRegMask()); |
| 235 | } |
| 236 | |
| 237 | std::string buf3; |
| 238 | if (i < cu_->num_dalvik_registers) { |
| 239 | StringAppendF(&buf3, "%02d", i); |
| 240 | } else if (i == mir_graph_->GetMethodSReg()) { |
| 241 | buf3 = "Method*"; |
| 242 | } else { |
| 243 | StringAppendF(&buf3, "ct%d", i - cu_->num_dalvik_registers); |
| 244 | } |
| 245 | |
| 246 | LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(), |
| 247 | v_reg_map.core_location == kLocPhysReg ? |
| 248 | "r" : "SP+", v_reg_map.core_location == kLocPhysReg ? |
| 249 | v_reg_map.core_reg : SRegOffset(i), |
| 250 | buf.c_str()); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* Dump a mapping table */ |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 255 | void Mir2Lir::DumpMappingTable(const char* table_name, const char* descriptor, |
| 256 | const char* name, const Signature& signature, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 257 | const std::vector<uint32_t>& v) { |
| 258 | if (v.size() > 0) { |
| 259 | std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 260 | descriptor, name, signature.ToString().c_str(), v.size())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 261 | std::replace(line.begin(), line.end(), ';', '_'); |
| 262 | LOG(INFO) << line; |
| 263 | for (uint32_t i = 0; i < v.size(); i+=2) { |
| 264 | line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]); |
| 265 | LOG(INFO) << line; |
| 266 | } |
| 267 | LOG(INFO) <<" };\n\n"; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* Dump instructions and constant pool contents */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 272 | void Mir2Lir::CodegenDump() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | LOG(INFO) << "Dumping LIR insns for " |
| 274 | << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 275 | LIR* lir_insn; |
| 276 | int insns_size = cu_->code_item->insns_size_in_code_units_; |
| 277 | |
| 278 | LOG(INFO) << "Regs (excluding ins) : " << cu_->num_regs; |
| 279 | LOG(INFO) << "Ins : " << cu_->num_ins; |
| 280 | LOG(INFO) << "Outs : " << cu_->num_outs; |
| 281 | LOG(INFO) << "CoreSpills : " << num_core_spills_; |
| 282 | LOG(INFO) << "FPSpills : " << num_fp_spills_; |
| 283 | LOG(INFO) << "CompilerTemps : " << cu_->num_compiler_temps; |
| 284 | LOG(INFO) << "Frame size : " << frame_size_; |
| 285 | LOG(INFO) << "code size is " << total_size_ << |
| 286 | " bytes, Dalvik size is " << insns_size * 2; |
| 287 | LOG(INFO) << "expansion factor: " |
| 288 | << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2); |
| 289 | DumpPromotionMap(); |
| 290 | for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) { |
| 291 | DumpLIRInsn(lir_insn, 0); |
| 292 | } |
| 293 | for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) { |
| 294 | LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset, |
| 295 | lir_insn->operands[0]); |
| 296 | } |
| 297 | |
| 298 | const DexFile::MethodId& method_id = |
| 299 | cu_->dex_file->GetMethodId(cu_->method_idx); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 300 | const Signature signature = cu_->dex_file->GetMethodSignature(method_id); |
| 301 | const char* name = cu_->dex_file->GetMethodName(method_id); |
| 302 | const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 303 | |
| 304 | // Dump mapping tables |
| 305 | DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, pc2dex_mapping_table_); |
| 306 | DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, dex2pc_mapping_table_); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Search the existing constants in the literal pool for an exact or close match |
| 311 | * within specified delta (greater or equal to 0). |
| 312 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 313 | LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 314 | while (data_target) { |
| 315 | if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta) |
| 316 | return data_target; |
| 317 | data_target = data_target->next; |
| 318 | } |
| 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | /* Search the existing constants in the literal pool for an exact wide match */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 323 | LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 324 | bool lo_match = false; |
| 325 | LIR* lo_target = NULL; |
| 326 | while (data_target) { |
| 327 | if (lo_match && (data_target->operands[0] == val_hi)) { |
| 328 | // Record high word in case we need to expand this later. |
| 329 | lo_target->operands[1] = val_hi; |
| 330 | return lo_target; |
| 331 | } |
| 332 | lo_match = false; |
| 333 | if (data_target->operands[0] == val_lo) { |
| 334 | lo_match = true; |
| 335 | lo_target = data_target; |
| 336 | } |
| 337 | data_target = data_target->next; |
| 338 | } |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * The following are building blocks to insert constants into the pool or |
| 344 | * instruction streams. |
| 345 | */ |
| 346 | |
| 347 | /* Add a 32-bit constant to the constant pool */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 348 | LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 349 | /* Add the constant to the literal pool */ |
| 350 | if (constant_list_p) { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 351 | LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocData)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 352 | new_value->operands[0] = value; |
| 353 | new_value->next = *constant_list_p; |
| 354 | *constant_list_p = new_value; |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 355 | estimated_native_code_size_ += sizeof(value); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 356 | return new_value; |
| 357 | } |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | /* Add a 64-bit constant to the constant pool or mixed with code */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 362 | LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 363 | AddWordData(constant_list_p, val_hi); |
| 364 | return AddWordData(constant_list_p, val_lo); |
| 365 | } |
| 366 | |
| 367 | static void PushWord(std::vector<uint8_t>&buf, int data) { |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 368 | buf.push_back(data & 0xff); |
| 369 | buf.push_back((data >> 8) & 0xff); |
| 370 | buf.push_back((data >> 16) & 0xff); |
| 371 | buf.push_back((data >> 24) & 0xff); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 372 | } |
| 373 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 374 | // Push 8 bytes on 64-bit systems; 4 on 32-bit systems. |
| 375 | static void PushPointer(std::vector<uint8_t>&buf, void const* pointer) { |
| 376 | uintptr_t data = reinterpret_cast<uintptr_t>(pointer); |
| 377 | if (sizeof(void*) == sizeof(uint64_t)) { |
| 378 | PushWord(buf, (data >> (sizeof(void*) * 4)) & 0xFFFFFFFF); |
| 379 | PushWord(buf, data & 0xFFFFFFFF); |
| 380 | } else { |
| 381 | PushWord(buf, data); |
| 382 | } |
| 383 | } |
| 384 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 385 | static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) { |
| 386 | while (buf.size() < offset) { |
| 387 | buf.push_back(0); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* Write the literal pool to the output stream */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 392 | void Mir2Lir::InstallLiteralPools() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 393 | AlignBuffer(code_buffer_, data_offset_); |
| 394 | LIR* data_lir = literal_list_; |
| 395 | while (data_lir != NULL) { |
| 396 | PushWord(code_buffer_, data_lir->operands[0]); |
| 397 | data_lir = NEXT_LIR(data_lir); |
| 398 | } |
| 399 | // Push code and method literals, record offsets for the compiler to patch. |
| 400 | data_lir = code_literal_list_; |
| 401 | while (data_lir != NULL) { |
| 402 | uint32_t target = data_lir->operands[0]; |
| 403 | cu_->compiler_driver->AddCodePatch(cu_->dex_file, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 404 | cu_->class_def_idx, |
| 405 | cu_->method_idx, |
| 406 | cu_->invoke_type, |
| 407 | target, |
| 408 | static_cast<InvokeType>(data_lir->operands[1]), |
| 409 | code_buffer_.size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 410 | const DexFile::MethodId& id = cu_->dex_file->GetMethodId(target); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 411 | // unique value based on target to ensure code deduplication works |
| 412 | PushPointer(code_buffer_, &id); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 413 | data_lir = NEXT_LIR(data_lir); |
| 414 | } |
| 415 | data_lir = method_literal_list_; |
| 416 | while (data_lir != NULL) { |
| 417 | uint32_t target = data_lir->operands[0]; |
| 418 | cu_->compiler_driver->AddMethodPatch(cu_->dex_file, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 419 | cu_->class_def_idx, |
| 420 | cu_->method_idx, |
| 421 | cu_->invoke_type, |
| 422 | target, |
| 423 | static_cast<InvokeType>(data_lir->operands[1]), |
| 424 | code_buffer_.size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 425 | const DexFile::MethodId& id = cu_->dex_file->GetMethodId(target); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 426 | // unique value based on target to ensure code deduplication works |
| 427 | PushPointer(code_buffer_, &id); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 428 | data_lir = NEXT_LIR(data_lir); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* Write the switch tables to the output stream */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 433 | void Mir2Lir::InstallSwitchTables() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 434 | GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_); |
| 435 | while (true) { |
| 436 | Mir2Lir::SwitchTable* tab_rec = iterator.Next(); |
| 437 | if (tab_rec == NULL) break; |
| 438 | AlignBuffer(code_buffer_, tab_rec->offset); |
| 439 | /* |
| 440 | * For Arm, our reference point is the address of the bx |
| 441 | * instruction that does the launch, so we have to subtract |
| 442 | * the auto pc-advance. For other targets the reference point |
| 443 | * is a label, so we can use the offset as-is. |
| 444 | */ |
| 445 | int bx_offset = INVALID_OFFSET; |
| 446 | switch (cu_->instruction_set) { |
| 447 | case kThumb2: |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 448 | DCHECK(tab_rec->anchor->flags.fixup != kFixupNone); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 449 | bx_offset = tab_rec->anchor->offset + 4; |
| 450 | break; |
| 451 | case kX86: |
| 452 | bx_offset = 0; |
| 453 | break; |
| 454 | case kMips: |
| 455 | bx_offset = tab_rec->anchor->offset; |
| 456 | break; |
| 457 | default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set; |
| 458 | } |
| 459 | if (cu_->verbose) { |
| 460 | LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset; |
| 461 | } |
| 462 | if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 463 | const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2])); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 464 | for (int elems = 0; elems < tab_rec->table[1]; elems++) { |
| 465 | int disp = tab_rec->targets[elems]->offset - bx_offset; |
| 466 | if (cu_->verbose) { |
| 467 | LOG(INFO) << " Case[" << elems << "] key: 0x" |
| 468 | << std::hex << keys[elems] << ", disp: 0x" |
| 469 | << std::hex << disp; |
| 470 | } |
| 471 | PushWord(code_buffer_, keys[elems]); |
| 472 | PushWord(code_buffer_, |
| 473 | tab_rec->targets[elems]->offset - bx_offset); |
| 474 | } |
| 475 | } else { |
| 476 | DCHECK_EQ(static_cast<int>(tab_rec->table[0]), |
| 477 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
| 478 | for (int elems = 0; elems < tab_rec->table[1]; elems++) { |
| 479 | int disp = tab_rec->targets[elems]->offset - bx_offset; |
| 480 | if (cu_->verbose) { |
| 481 | LOG(INFO) << " Case[" << elems << "] disp: 0x" |
| 482 | << std::hex << disp; |
| 483 | } |
| 484 | PushWord(code_buffer_, tab_rec->targets[elems]->offset - bx_offset); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /* Write the fill array dta to the output stream */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 491 | void Mir2Lir::InstallFillArrayData() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 492 | GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_); |
| 493 | while (true) { |
| 494 | Mir2Lir::FillArrayData *tab_rec = iterator.Next(); |
| 495 | if (tab_rec == NULL) break; |
| 496 | AlignBuffer(code_buffer_, tab_rec->offset); |
| 497 | for (int i = 0; i < (tab_rec->size + 1) / 2; i++) { |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 498 | code_buffer_.push_back(tab_rec->table[i] & 0xFF); |
| 499 | code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 504 | static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) { |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 505 | for (; lir != NULL; lir = lir->next) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 506 | lir->offset = offset; |
| 507 | offset += 4; |
| 508 | } |
| 509 | return offset; |
| 510 | } |
| 511 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 512 | static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset) { |
| 513 | unsigned int element_size = sizeof(void*); |
| 514 | // Align to natural pointer size. |
| 515 | offset = (offset + (element_size - 1)) & ~(element_size - 1); |
| 516 | for (; lir != NULL; lir = lir->next) { |
| 517 | lir->offset = offset; |
| 518 | offset += element_size; |
| 519 | } |
| 520 | return offset; |
| 521 | } |
| 522 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 523 | // Make sure we have a code address for every declared catch entry |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 524 | bool Mir2Lir::VerifyCatchEntries() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 525 | bool success = true; |
| 526 | for (std::set<uint32_t>::const_iterator it = mir_graph_->catches_.begin(); |
| 527 | it != mir_graph_->catches_.end(); ++it) { |
| 528 | uint32_t dex_pc = *it; |
| 529 | bool found = false; |
| 530 | for (size_t i = 0; i < dex2pc_mapping_table_.size(); i += 2) { |
| 531 | if (dex_pc == dex2pc_mapping_table_[i+1]) { |
| 532 | found = true; |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | if (!found) { |
| 537 | LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc; |
| 538 | success = false; |
| 539 | } |
| 540 | } |
| 541 | // Now, try in the other direction |
| 542 | for (size_t i = 0; i < dex2pc_mapping_table_.size(); i += 2) { |
| 543 | uint32_t dex_pc = dex2pc_mapping_table_[i+1]; |
| 544 | if (mir_graph_->catches_.find(dex_pc) == mir_graph_->catches_.end()) { |
| 545 | LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dex_pc; |
| 546 | success = false; |
| 547 | } |
| 548 | } |
| 549 | if (!success) { |
| 550 | LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 551 | LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: " |
| 552 | << dex2pc_mapping_table_.size()/2; |
| 553 | } |
| 554 | return success; |
| 555 | } |
| 556 | |
| 557 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 558 | void Mir2Lir::CreateMappingTables() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 559 | for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) { |
| 560 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) { |
| 561 | pc2dex_mapping_table_.push_back(tgt_lir->offset); |
| 562 | pc2dex_mapping_table_.push_back(tgt_lir->dalvik_offset); |
| 563 | } |
| 564 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) { |
| 565 | dex2pc_mapping_table_.push_back(tgt_lir->offset); |
| 566 | dex2pc_mapping_table_.push_back(tgt_lir->dalvik_offset); |
| 567 | } |
| 568 | } |
| 569 | if (kIsDebugBuild) { |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 570 | CHECK(VerifyCatchEntries()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 571 | } |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 572 | CHECK_EQ(pc2dex_mapping_table_.size() & 1, 0U); |
| 573 | CHECK_EQ(dex2pc_mapping_table_.size() & 1, 0U); |
| 574 | uint32_t total_entries = (pc2dex_mapping_table_.size() + dex2pc_mapping_table_.size()) / 2; |
| 575 | uint32_t pc2dex_entries = pc2dex_mapping_table_.size() / 2; |
| 576 | encoded_mapping_table_.PushBack(total_entries); |
| 577 | encoded_mapping_table_.PushBack(pc2dex_entries); |
| 578 | encoded_mapping_table_.InsertBack(pc2dex_mapping_table_.begin(), pc2dex_mapping_table_.end()); |
| 579 | encoded_mapping_table_.InsertBack(dex2pc_mapping_table_.begin(), dex2pc_mapping_table_.end()); |
| 580 | if (kIsDebugBuild) { |
| 581 | // Verify the encoded table holds the expected data. |
| 582 | MappingTable table(&encoded_mapping_table_.GetData()[0]); |
| 583 | CHECK_EQ(table.TotalSize(), total_entries); |
| 584 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 585 | CHECK_EQ(table.DexToPcSize(), dex2pc_mapping_table_.size() / 2); |
| 586 | MappingTable::PcToDexIterator it = table.PcToDexBegin(); |
| 587 | for (uint32_t i = 0; i < pc2dex_mapping_table_.size(); ++i, ++it) { |
| 588 | CHECK_EQ(pc2dex_mapping_table_.at(i), it.NativePcOffset()); |
| 589 | ++i; |
| 590 | CHECK_EQ(pc2dex_mapping_table_.at(i), it.DexPc()); |
| 591 | } |
| 592 | MappingTable::DexToPcIterator it2 = table.DexToPcBegin(); |
| 593 | for (uint32_t i = 0; i < dex2pc_mapping_table_.size(); ++i, ++it2) { |
| 594 | CHECK_EQ(dex2pc_mapping_table_.at(i), it2.NativePcOffset()); |
| 595 | ++i; |
| 596 | CHECK_EQ(dex2pc_mapping_table_.at(i), it2.DexPc()); |
| 597 | } |
| 598 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | class NativePcToReferenceMapBuilder { |
| 602 | public: |
| 603 | NativePcToReferenceMapBuilder(std::vector<uint8_t>* table, |
| 604 | size_t entries, uint32_t max_native_offset, |
| 605 | size_t references_width) : entries_(entries), |
| 606 | references_width_(references_width), in_use_(entries), |
| 607 | table_(table) { |
| 608 | // Compute width in bytes needed to hold max_native_offset. |
| 609 | native_offset_width_ = 0; |
| 610 | while (max_native_offset != 0) { |
| 611 | native_offset_width_++; |
| 612 | max_native_offset >>= 8; |
| 613 | } |
| 614 | // Resize table and set up header. |
| 615 | table->resize((EntryWidth() * entries) + sizeof(uint32_t)); |
| 616 | CHECK_LT(native_offset_width_, 1U << 3); |
| 617 | (*table)[0] = native_offset_width_ & 7; |
| 618 | CHECK_LT(references_width_, 1U << 13); |
| 619 | (*table)[0] |= (references_width_ << 3) & 0xFF; |
| 620 | (*table)[1] = (references_width_ >> 5) & 0xFF; |
| 621 | CHECK_LT(entries, 1U << 16); |
| 622 | (*table)[2] = entries & 0xFF; |
| 623 | (*table)[3] = (entries >> 8) & 0xFF; |
| 624 | } |
| 625 | |
| 626 | void AddEntry(uint32_t native_offset, const uint8_t* references) { |
| 627 | size_t table_index = TableIndex(native_offset); |
| 628 | while (in_use_[table_index]) { |
| 629 | table_index = (table_index + 1) % entries_; |
| 630 | } |
| 631 | in_use_[table_index] = true; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 632 | SetCodeOffset(table_index, native_offset); |
| 633 | DCHECK_EQ(native_offset, GetCodeOffset(table_index)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 634 | SetReferences(table_index, references); |
| 635 | } |
| 636 | |
| 637 | private: |
| 638 | size_t TableIndex(uint32_t native_offset) { |
| 639 | return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_; |
| 640 | } |
| 641 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 642 | uint32_t GetCodeOffset(size_t table_index) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 643 | uint32_t native_offset = 0; |
| 644 | size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t); |
| 645 | for (size_t i = 0; i < native_offset_width_; i++) { |
| 646 | native_offset |= (*table_)[table_offset + i] << (i * 8); |
| 647 | } |
| 648 | return native_offset; |
| 649 | } |
| 650 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 651 | void SetCodeOffset(size_t table_index, uint32_t native_offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 652 | size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t); |
| 653 | for (size_t i = 0; i < native_offset_width_; i++) { |
| 654 | (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void SetReferences(size_t table_index, const uint8_t* references) { |
| 659 | size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t); |
| 660 | memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_); |
| 661 | } |
| 662 | |
| 663 | size_t EntryWidth() const { |
| 664 | return native_offset_width_ + references_width_; |
| 665 | } |
| 666 | |
| 667 | // Number of entries in the table. |
| 668 | const size_t entries_; |
| 669 | // Number of bytes used to encode the reference bitmap. |
| 670 | const size_t references_width_; |
| 671 | // Number of bytes used to encode a native offset. |
| 672 | size_t native_offset_width_; |
| 673 | // Entries that are in use. |
| 674 | std::vector<bool> in_use_; |
| 675 | // The table we're building. |
| 676 | std::vector<uint8_t>* const table_; |
| 677 | }; |
| 678 | |
| 679 | void Mir2Lir::CreateNativeGcMap() { |
| 680 | const std::vector<uint32_t>& mapping_table = pc2dex_mapping_table_; |
| 681 | uint32_t max_native_offset = 0; |
| 682 | for (size_t i = 0; i < mapping_table.size(); i += 2) { |
| 683 | uint32_t native_offset = mapping_table[i + 0]; |
| 684 | if (native_offset > max_native_offset) { |
| 685 | max_native_offset = native_offset; |
| 686 | } |
| 687 | } |
| 688 | MethodReference method_ref(cu_->dex_file, cu_->method_idx); |
| 689 | const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref); |
| 690 | verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4); |
| 691 | // Compute native offset to references size. |
| 692 | NativePcToReferenceMapBuilder native_gc_map_builder(&native_gc_map_, |
| 693 | mapping_table.size() / 2, max_native_offset, |
| 694 | dex_gc_map.RegWidth()); |
| 695 | |
| 696 | for (size_t i = 0; i < mapping_table.size(); i += 2) { |
| 697 | uint32_t native_offset = mapping_table[i + 0]; |
| 698 | uint32_t dex_pc = mapping_table[i + 1]; |
| 699 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
| 700 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 701 | native_gc_map_builder.AddEntry(native_offset, references); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /* Determine the offset of each literal field */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 706 | int Mir2Lir::AssignLiteralOffset(CodeOffset offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 707 | offset = AssignLiteralOffsetCommon(literal_list_, offset); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 708 | offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset); |
| 709 | offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 710 | return offset; |
| 711 | } |
| 712 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 713 | int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 714 | GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_); |
| 715 | while (true) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 716 | Mir2Lir::SwitchTable* tab_rec = iterator.Next(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 717 | if (tab_rec == NULL) break; |
| 718 | tab_rec->offset = offset; |
| 719 | if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) { |
| 720 | offset += tab_rec->table[1] * (sizeof(int) * 2); |
| 721 | } else { |
| 722 | DCHECK_EQ(static_cast<int>(tab_rec->table[0]), |
| 723 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
| 724 | offset += tab_rec->table[1] * sizeof(int); |
| 725 | } |
| 726 | } |
| 727 | return offset; |
| 728 | } |
| 729 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 730 | int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 731 | GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_); |
| 732 | while (true) { |
| 733 | Mir2Lir::FillArrayData *tab_rec = iterator.Next(); |
| 734 | if (tab_rec == NULL) break; |
| 735 | tab_rec->offset = offset; |
| 736 | offset += tab_rec->size; |
| 737 | // word align |
| 738 | offset = (offset + 3) & ~3; |
| 739 | } |
| 740 | return offset; |
| 741 | } |
| 742 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 743 | /* |
| 744 | * Insert a kPseudoCaseLabel at the beginning of the Dalvik |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 745 | * offset vaddr if pretty-printing, otherise use the standard block |
| 746 | * label. The selected label will be used to fix up the case |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 747 | * branch table during the assembly phase. All resource flags |
| 748 | * are set to prevent code motion. KeyVal is just there for debugging. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 749 | */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 750 | LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) { |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 751 | LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id]; |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 752 | LIR* res = boundary_lir; |
| 753 | if (cu_->verbose) { |
| 754 | // Only pay the expense if we're pretty-printing. |
| 755 | LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocLIR)); |
| 756 | new_label->dalvik_offset = vaddr; |
| 757 | new_label->opcode = kPseudoCaseLabel; |
| 758 | new_label->operands[0] = keyVal; |
| 759 | new_label->flags.fixup = kFixupLabel; |
| 760 | DCHECK(!new_label->flags.use_def_invalid); |
| 761 | new_label->u.m.def_mask = ENCODE_ALL; |
| 762 | InsertLIRAfter(boundary_lir, new_label); |
| 763 | res = new_label; |
| 764 | } |
| 765 | return res; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 766 | } |
| 767 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 768 | void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 769 | const uint16_t* table = tab_rec->table; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 770 | DexOffset base_vaddr = tab_rec->vaddr; |
| 771 | const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 772 | int entries = table[1]; |
| 773 | int low_key = s4FromSwitchData(&table[2]); |
| 774 | for (int i = 0; i < entries; i++) { |
| 775 | tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key); |
| 776 | } |
| 777 | } |
| 778 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 779 | void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 780 | const uint16_t* table = tab_rec->table; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 781 | DexOffset base_vaddr = tab_rec->vaddr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 782 | int entries = table[1]; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 783 | const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]); |
| 784 | const int32_t* targets = &keys[entries]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 785 | for (int i = 0; i < entries; i++) { |
| 786 | tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]); |
| 787 | } |
| 788 | } |
| 789 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 790 | void Mir2Lir::ProcessSwitchTables() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 791 | GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_); |
| 792 | while (true) { |
| 793 | Mir2Lir::SwitchTable *tab_rec = iterator.Next(); |
| 794 | if (tab_rec == NULL) break; |
| 795 | if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) { |
| 796 | MarkPackedCaseLabels(tab_rec); |
| 797 | } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) { |
| 798 | MarkSparseCaseLabels(tab_rec); |
| 799 | } else { |
| 800 | LOG(FATAL) << "Invalid switch table"; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 805 | void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 806 | /* |
| 807 | * Sparse switch data format: |
| 808 | * ushort ident = 0x0200 magic value |
| 809 | * ushort size number of entries in the table; > 0 |
| 810 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 811 | * int targets[size] branch targets, relative to switch opcode |
| 812 | * |
| 813 | * Total size is (2+size*4) 16-bit code units. |
| 814 | */ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 815 | uint16_t ident = table[0]; |
| 816 | int entries = table[1]; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 817 | const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]); |
| 818 | const int32_t* targets = &keys[entries]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 819 | LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident |
| 820 | << ", entries: " << std::dec << entries; |
| 821 | for (int i = 0; i < entries; i++) { |
| 822 | LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i]; |
| 823 | } |
| 824 | } |
| 825 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 826 | void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 827 | /* |
| 828 | * Packed switch data format: |
| 829 | * ushort ident = 0x0100 magic value |
| 830 | * ushort size number of entries in the table |
| 831 | * int first_key first (and lowest) switch case value |
| 832 | * int targets[size] branch targets, relative to switch opcode |
| 833 | * |
| 834 | * Total size is (4+size*2) 16-bit code units. |
| 835 | */ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 836 | uint16_t ident = table[0]; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 837 | const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 838 | int entries = table[1]; |
| 839 | int low_key = s4FromSwitchData(&table[2]); |
| 840 | LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident |
| 841 | << ", entries: " << std::dec << entries << ", low_key: " << low_key; |
| 842 | for (int i = 0; i < entries; i++) { |
| 843 | LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex |
| 844 | << targets[i]; |
| 845 | } |
| 846 | } |
| 847 | |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 848 | /* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 849 | void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) { |
| 850 | // NOTE: only used for debug listings. |
| 851 | NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 854 | bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 855 | bool is_taken; |
| 856 | switch (opcode) { |
| 857 | case Instruction::IF_EQ: is_taken = (src1 == src2); break; |
| 858 | case Instruction::IF_NE: is_taken = (src1 != src2); break; |
| 859 | case Instruction::IF_LT: is_taken = (src1 < src2); break; |
| 860 | case Instruction::IF_GE: is_taken = (src1 >= src2); break; |
| 861 | case Instruction::IF_GT: is_taken = (src1 > src2); break; |
| 862 | case Instruction::IF_LE: is_taken = (src1 <= src2); break; |
| 863 | case Instruction::IF_EQZ: is_taken = (src1 == 0); break; |
| 864 | case Instruction::IF_NEZ: is_taken = (src1 != 0); break; |
| 865 | case Instruction::IF_LTZ: is_taken = (src1 < 0); break; |
| 866 | case Instruction::IF_GEZ: is_taken = (src1 >= 0); break; |
| 867 | case Instruction::IF_GTZ: is_taken = (src1 > 0); break; |
| 868 | case Instruction::IF_LEZ: is_taken = (src1 <= 0); break; |
| 869 | default: |
| 870 | LOG(FATAL) << "Unexpected opcode " << opcode; |
| 871 | is_taken = false; |
| 872 | } |
| 873 | return is_taken; |
| 874 | } |
| 875 | |
| 876 | // Convert relation of src1/src2 to src2/src1 |
| 877 | ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) { |
| 878 | ConditionCode res; |
| 879 | switch (before) { |
| 880 | case kCondEq: res = kCondEq; break; |
| 881 | case kCondNe: res = kCondNe; break; |
| 882 | case kCondLt: res = kCondGt; break; |
| 883 | case kCondGt: res = kCondLt; break; |
| 884 | case kCondLe: res = kCondGe; break; |
| 885 | case kCondGe: res = kCondLe; break; |
| 886 | default: |
| 887 | res = static_cast<ConditionCode>(0); |
| 888 | LOG(FATAL) << "Unexpected ccode " << before; |
| 889 | } |
| 890 | return res; |
| 891 | } |
| 892 | |
| 893 | // TODO: move to mir_to_lir.cc |
| 894 | Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena) |
| 895 | : Backend(arena), |
| 896 | literal_list_(NULL), |
| 897 | method_literal_list_(NULL), |
| 898 | code_literal_list_(NULL), |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 899 | first_fixup_(NULL), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 900 | cu_(cu), |
| 901 | mir_graph_(mir_graph), |
| 902 | switch_tables_(arena, 4, kGrowableArraySwitchTables), |
| 903 | fill_array_data_(arena, 4, kGrowableArrayFillArrayData), |
| 904 | throw_launchpads_(arena, 2048, kGrowableArrayThrowLaunchPads), |
| 905 | suspend_launchpads_(arena, 4, kGrowableArraySuspendLaunchPads), |
| 906 | intrinsic_launchpads_(arena, 2048, kGrowableArrayMisc), |
buzbee | bd663de | 2013-09-10 15:41:31 -0700 | [diff] [blame] | 907 | tempreg_info_(arena, 20, kGrowableArrayMisc), |
| 908 | reginfo_map_(arena, 64, kGrowableArrayMisc), |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 909 | pointer_storage_(arena, 128, kGrowableArrayMisc), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 910 | data_offset_(0), |
| 911 | total_size_(0), |
| 912 | block_label_list_(NULL), |
| 913 | current_dalvik_offset_(0), |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 914 | estimated_native_code_size_(0), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 915 | reg_pool_(NULL), |
| 916 | live_sreg_(0), |
| 917 | num_core_spills_(0), |
| 918 | num_fp_spills_(0), |
| 919 | frame_size_(0), |
| 920 | core_spill_mask_(0), |
| 921 | fp_spill_mask_(0), |
| 922 | first_lir_insn_(NULL), |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 923 | last_lir_insn_(NULL), |
| 924 | inliner_(nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 925 | promotion_map_ = static_cast<PromotionMap*> |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 926 | (arena_->Alloc((cu_->num_dalvik_registers + cu_->num_compiler_temps + 1) * |
| 927 | sizeof(promotion_map_[0]), ArenaAllocator::kAllocRegAlloc)); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 928 | // Reserve pointer id 0 for NULL. |
| 929 | size_t null_idx = WrapPointer(NULL); |
| 930 | DCHECK_EQ(null_idx, 0U); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | void Mir2Lir::Materialize() { |
buzbee | a61f495 | 2013-08-23 14:27:06 -0700 | [diff] [blame] | 934 | cu_->NewTimingSplit("RegisterAllocation"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 935 | CompilerInitializeRegAlloc(); // Needs to happen after SSA naming |
| 936 | |
| 937 | /* Allocate Registers using simple local allocation scheme */ |
| 938 | SimpleRegAlloc(); |
| 939 | |
buzbee | 479f83c | 2013-07-19 10:58:21 -0700 | [diff] [blame] | 940 | if (mir_graph_->IsSpecialCase()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 941 | /* |
| 942 | * Custom codegen for special cases. If for any reason the |
| 943 | * special codegen doesn't succeed, first_lir_insn_ will |
| 944 | * set to NULL; |
| 945 | */ |
buzbee | a61f495 | 2013-08-23 14:27:06 -0700 | [diff] [blame] | 946 | cu_->NewTimingSplit("SpecialMIR2LIR"); |
buzbee | 479f83c | 2013-07-19 10:58:21 -0700 | [diff] [blame] | 947 | SpecialMIR2LIR(mir_graph_->GetSpecialCase()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* Convert MIR to LIR, etc. */ |
| 951 | if (first_lir_insn_ == NULL) { |
| 952 | MethodMIR2LIR(); |
| 953 | } |
| 954 | |
| 955 | /* Method is not empty */ |
| 956 | if (first_lir_insn_) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 957 | // mark the targets of switch statement case labels |
| 958 | ProcessSwitchTables(); |
| 959 | |
| 960 | /* Convert LIR into machine code. */ |
| 961 | AssembleLIR(); |
| 962 | |
| 963 | if (cu_->verbose) { |
| 964 | CodegenDump(); |
| 965 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 966 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | CompiledMethod* Mir2Lir::GetCompiledMethod() { |
| 970 | // Combine vmap tables - core regs, then fp regs - into vmap_table |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 971 | std::vector<uint16_t> raw_vmap_table; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 972 | // Core regs may have been inserted out of order - sort first |
| 973 | std::sort(core_vmap_table_.begin(), core_vmap_table_.end()); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 974 | for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 975 | // Copy, stripping out the phys register sort key |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 976 | raw_vmap_table.push_back(~(-1 << VREG_NUM_WIDTH) & core_vmap_table_[i]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 977 | } |
| 978 | // If we have a frame, push a marker to take place of lr |
| 979 | if (frame_size_ > 0) { |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 980 | raw_vmap_table.push_back(INVALID_VREG); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 981 | } else { |
| 982 | DCHECK_EQ(__builtin_popcount(core_spill_mask_), 0); |
| 983 | DCHECK_EQ(__builtin_popcount(fp_spill_mask_), 0); |
| 984 | } |
| 985 | // Combine vmap tables - core regs, then fp regs. fp regs already sorted |
| 986 | for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) { |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 987 | raw_vmap_table.push_back(fp_vmap_table_[i]); |
| 988 | } |
| 989 | UnsignedLeb128EncodingVector vmap_encoder; |
| 990 | // Prefix the encoded data with its size. |
| 991 | vmap_encoder.PushBack(raw_vmap_table.size()); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 992 | for (uint16_t cur : raw_vmap_table) { |
| 993 | vmap_encoder.PushBack(cur); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 994 | } |
| 995 | CompiledMethod* result = |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 996 | new CompiledMethod(*cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_, |
| 997 | core_spill_mask_, fp_spill_mask_, encoded_mapping_table_.GetData(), |
| 998 | vmap_encoder.GetData(), native_gc_map_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 999 | return result; |
| 1000 | } |
| 1001 | |
| 1002 | int Mir2Lir::ComputeFrameSize() { |
| 1003 | /* Figure out the frame size */ |
| 1004 | static const uint32_t kAlignMask = kStackAlignment - 1; |
| 1005 | uint32_t size = (num_core_spills_ + num_fp_spills_ + |
| 1006 | 1 /* filler word */ + cu_->num_regs + cu_->num_outs + |
| 1007 | cu_->num_compiler_temps + 1 /* cur_method* */) |
| 1008 | * sizeof(uint32_t); |
| 1009 | /* Align and set */ |
| 1010 | return (size + kAlignMask) & ~(kAlignMask); |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * Append an LIR instruction to the LIR list maintained by a compilation |
| 1015 | * unit |
| 1016 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1017 | void Mir2Lir::AppendLIR(LIR* lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1018 | if (first_lir_insn_ == NULL) { |
| 1019 | DCHECK(last_lir_insn_ == NULL); |
| 1020 | last_lir_insn_ = first_lir_insn_ = lir; |
| 1021 | lir->prev = lir->next = NULL; |
| 1022 | } else { |
| 1023 | last_lir_insn_->next = lir; |
| 1024 | lir->prev = last_lir_insn_; |
| 1025 | lir->next = NULL; |
| 1026 | last_lir_insn_ = lir; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | * Insert an LIR instruction before the current instruction, which cannot be the |
| 1032 | * first instruction. |
| 1033 | * |
| 1034 | * prev_lir <-> new_lir <-> current_lir |
| 1035 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1036 | void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1037 | DCHECK(current_lir->prev != NULL); |
| 1038 | LIR *prev_lir = current_lir->prev; |
| 1039 | |
| 1040 | prev_lir->next = new_lir; |
| 1041 | new_lir->prev = prev_lir; |
| 1042 | new_lir->next = current_lir; |
| 1043 | current_lir->prev = new_lir; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * Insert an LIR instruction after the current instruction, which cannot be the |
| 1048 | * first instruction. |
| 1049 | * |
| 1050 | * current_lir -> new_lir -> old_next |
| 1051 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1052 | void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1053 | new_lir->prev = current_lir; |
| 1054 | new_lir->next = current_lir->next; |
| 1055 | current_lir->next = new_lir; |
| 1056 | new_lir->next->prev = new_lir; |
| 1057 | } |
| 1058 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1059 | } // namespace art |