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" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 20 | #include "gc_map_builder.h" |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 21 | #include "mapping_table.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | #include "mir_to_lir-inl.h" |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 23 | #include "dex/quick/dex_file_method_inliner.h" |
| 24 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 25 | #include "dex/verification_results.h" |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 26 | #include "dex/verified_method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 27 | #include "verifier/dex_gc_map.h" |
| 28 | #include "verifier/method_verifier.h" |
Vladimir Marko | 2e589aa | 2014-02-25 17:53:53 +0000 | [diff] [blame] | 29 | #include "vmap_table.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | /* Dump a mapping table */ |
| 36 | template <typename It> |
| 37 | void DumpMappingTable(const char* table_name, const char* descriptor, const char* name, |
| 38 | const Signature& signature, uint32_t size, It first) { |
| 39 | if (size != 0) { |
Ian Rogers | 107c31e | 2014-01-23 20:55:29 -0800 | [diff] [blame] | 40 | std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name, |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 41 | descriptor, name, signature.ToString().c_str(), size)); |
| 42 | std::replace(line.begin(), line.end(), ';', '_'); |
| 43 | LOG(INFO) << line; |
| 44 | for (uint32_t i = 0; i != size; ++i) { |
| 45 | line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc()); |
| 46 | ++first; |
| 47 | LOG(INFO) << line; |
| 48 | } |
| 49 | LOG(INFO) <<" };\n\n"; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | } // anonymous namespace |
| 54 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 55 | bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 56 | bool res = false; |
| 57 | if (rl_src.is_const) { |
| 58 | if (rl_src.wide) { |
| 59 | if (rl_src.fp) { |
| 60 | res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src)); |
| 61 | } else { |
| 62 | res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src)); |
| 63 | } |
| 64 | } else { |
| 65 | if (rl_src.fp) { |
| 66 | res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src)); |
| 67 | } else { |
| 68 | res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src)); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | return res; |
| 73 | } |
| 74 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 75 | void Mir2Lir::MarkSafepointPC(LIR* inst) { |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 76 | DCHECK(!inst->flags.use_def_invalid); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 77 | inst->u.m.def_mask = &kEncodeAll; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 78 | LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 79 | DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 82 | void Mir2Lir::MarkSafepointPCAfter(LIR* after) { |
| 83 | DCHECK(!after->flags.use_def_invalid); |
| 84 | after->u.m.def_mask = &kEncodeAll; |
| 85 | // As NewLIR0 uses Append, we need to create the LIR by hand. |
| 86 | LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC); |
| 87 | if (after->next == nullptr) { |
| 88 | DCHECK_EQ(after, last_lir_insn_); |
| 89 | AppendLIR(safepoint_pc); |
| 90 | } else { |
| 91 | InsertLIRAfter(after, safepoint_pc); |
| 92 | } |
| 93 | DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll)); |
| 94 | } |
| 95 | |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 96 | /* Remove a LIR from the list. */ |
| 97 | void Mir2Lir::UnlinkLIR(LIR* lir) { |
| 98 | if (UNLIKELY(lir == first_lir_insn_)) { |
| 99 | first_lir_insn_ = lir->next; |
| 100 | if (lir->next != NULL) { |
| 101 | lir->next->prev = NULL; |
| 102 | } else { |
| 103 | DCHECK(lir->next == NULL); |
| 104 | DCHECK(lir == last_lir_insn_); |
| 105 | last_lir_insn_ = NULL; |
| 106 | } |
| 107 | } else if (lir == last_lir_insn_) { |
| 108 | last_lir_insn_ = lir->prev; |
| 109 | lir->prev->next = NULL; |
| 110 | } else if ((lir->prev != NULL) && (lir->next != NULL)) { |
| 111 | lir->prev->next = lir->next; |
| 112 | lir->next->prev = lir->prev; |
| 113 | } |
| 114 | } |
| 115 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 116 | /* Convert an instruction to a NOP */ |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 117 | void Mir2Lir::NopLIR(LIR* lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 118 | lir->flags.is_nop = true; |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 119 | if (!cu_->verbose) { |
| 120 | UnlinkLIR(lir); |
| 121 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 124 | void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 125 | DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE)); |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 126 | DCHECK(!lir->flags.use_def_invalid); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 127 | // TODO: Avoid the extra Arena allocation! |
| 128 | const ResourceMask** mask_ptr; |
| 129 | ResourceMask mask; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 130 | if (is_load) { |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 131 | mask_ptr = &lir->u.m.use_mask; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 132 | } else { |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 133 | mask_ptr = &lir->u.m.def_mask; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 134 | } |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 135 | mask = **mask_ptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 136 | /* Clear out the memref flags */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 137 | mask.ClearBits(kEncodeMem); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 138 | /* ..and then add back the one we need */ |
| 139 | switch (mem_type) { |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 140 | case ResourceMask::kLiteral: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 141 | DCHECK(is_load); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 142 | mask.SetBit(ResourceMask::kLiteral); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 143 | break; |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 144 | case ResourceMask::kDalvikReg: |
| 145 | mask.SetBit(ResourceMask::kDalvikReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 146 | break; |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 147 | case ResourceMask::kHeapRef: |
| 148 | mask.SetBit(ResourceMask::kHeapRef); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 149 | break; |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 150 | case ResourceMask::kMustNotAlias: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 151 | /* Currently only loads can be marked as kMustNotAlias */ |
| 152 | DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE)); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 153 | mask.SetBit(ResourceMask::kMustNotAlias); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 154 | break; |
| 155 | default: |
| 156 | LOG(FATAL) << "Oat: invalid memref kind - " << mem_type; |
| 157 | } |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 158 | *mask_ptr = mask_cache_.GetMask(mask); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Mark load/store instructions that access Dalvik registers through the stack. |
| 163 | */ |
| 164 | void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 165 | bool is64bit) { |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 166 | DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals( |
| 167 | kEncodeDalvikReg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit |
| 171 | * access. |
| 172 | */ |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 173 | lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Debugging macros |
| 178 | */ |
| 179 | #define DUMP_RESOURCE_MASK(X) |
| 180 | |
| 181 | /* Pretty-print a LIR instruction */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 182 | void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 183 | int offset = lir->offset; |
| 184 | int dest = lir->operands[0]; |
| 185 | const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops)); |
| 186 | |
| 187 | /* Handle pseudo-ops individually, and all regular insns as a group */ |
| 188 | switch (lir->opcode) { |
| 189 | case kPseudoMethodEntry: |
| 190 | LOG(INFO) << "-------- method entry " |
| 191 | << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 192 | break; |
| 193 | case kPseudoMethodExit: |
| 194 | LOG(INFO) << "-------- Method_Exit"; |
| 195 | break; |
| 196 | case kPseudoBarrier: |
| 197 | LOG(INFO) << "-------- BARRIER"; |
| 198 | break; |
| 199 | case kPseudoEntryBlock: |
| 200 | LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest; |
| 201 | break; |
| 202 | case kPseudoDalvikByteCodeBoundary: |
| 203 | if (lir->operands[0] == 0) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 204 | // NOTE: only used for debug listings. |
| 205 | lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string")); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 206 | } |
| 207 | LOG(INFO) << "-------- dalvik offset: 0x" << std::hex |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 208 | << lir->dalvik_offset << " @ " |
| 209 | << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0])); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 210 | break; |
| 211 | case kPseudoExitBlock: |
| 212 | LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest; |
| 213 | break; |
| 214 | case kPseudoPseudoAlign4: |
| 215 | LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex |
| 216 | << offset << "): .align4"; |
| 217 | break; |
| 218 | case kPseudoEHBlockLabel: |
| 219 | LOG(INFO) << "Exception_Handling:"; |
| 220 | break; |
| 221 | case kPseudoTargetLabel: |
| 222 | case kPseudoNormalBlockLabel: |
| 223 | LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":"; |
| 224 | break; |
| 225 | case kPseudoThrowTarget: |
| 226 | LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":"; |
| 227 | break; |
| 228 | case kPseudoIntrinsicRetry: |
| 229 | LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":"; |
| 230 | break; |
| 231 | case kPseudoSuspendTarget: |
| 232 | LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":"; |
| 233 | break; |
| 234 | case kPseudoSafepointPC: |
| 235 | LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":"; |
| 236 | break; |
| 237 | case kPseudoExportedPC: |
| 238 | LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":"; |
| 239 | break; |
| 240 | case kPseudoCaseLabel: |
| 241 | LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x" |
| 242 | << std::hex << lir->operands[0] << "|" << std::dec << |
| 243 | lir->operands[0]; |
| 244 | break; |
| 245 | default: |
| 246 | if (lir->flags.is_nop && !dump_nop) { |
| 247 | break; |
| 248 | } else { |
| 249 | std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode), |
| 250 | lir, base_addr)); |
| 251 | std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode), |
| 252 | lir, base_addr)); |
Ian Rogers | 107c31e | 2014-01-23 20:55:29 -0800 | [diff] [blame] | 253 | LOG(INFO) << StringPrintf("%5p: %-9s%s%s", |
| 254 | base_addr + offset, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 255 | op_name.c_str(), op_operands.c_str(), |
| 256 | lir->flags.is_nop ? "(nop)" : ""); |
| 257 | } |
| 258 | break; |
| 259 | } |
| 260 | |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 261 | if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) { |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 262 | DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use")); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 263 | } |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 264 | if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) { |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 265 | DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def")); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 269 | void Mir2Lir::DumpPromotionMap() { |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 270 | int num_regs = cu_->num_dalvik_registers + mir_graph_->GetNumUsedCompilerTemps(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 271 | for (int i = 0; i < num_regs; i++) { |
| 272 | PromotionMap v_reg_map = promotion_map_[i]; |
| 273 | std::string buf; |
| 274 | if (v_reg_map.fp_location == kLocPhysReg) { |
buzbee | b5860fb | 2014-06-21 15:31:01 -0700 | [diff] [blame^] | 275 | StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | std::string buf3; |
| 279 | if (i < cu_->num_dalvik_registers) { |
| 280 | StringAppendF(&buf3, "%02d", i); |
| 281 | } else if (i == mir_graph_->GetMethodSReg()) { |
| 282 | buf3 = "Method*"; |
| 283 | } else { |
| 284 | StringAppendF(&buf3, "ct%d", i - cu_->num_dalvik_registers); |
| 285 | } |
| 286 | |
| 287 | LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(), |
| 288 | v_reg_map.core_location == kLocPhysReg ? |
| 289 | "r" : "SP+", v_reg_map.core_location == kLocPhysReg ? |
| 290 | v_reg_map.core_reg : SRegOffset(i), |
| 291 | buf.c_str()); |
| 292 | } |
| 293 | } |
| 294 | |
buzbee | 7a11ab0 | 2014-04-28 20:02:38 -0700 | [diff] [blame] | 295 | void Mir2Lir::UpdateLIROffsets() { |
| 296 | // Only used for code listings. |
| 297 | size_t offset = 0; |
| 298 | for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) { |
| 299 | lir->offset = offset; |
| 300 | if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) { |
| 301 | offset += GetInsnSize(lir); |
| 302 | } else if (lir->opcode == kPseudoPseudoAlign4) { |
| 303 | offset += (offset & 0x2); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 308 | /* Dump instructions and constant pool contents */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 309 | void Mir2Lir::CodegenDump() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 310 | LOG(INFO) << "Dumping LIR insns for " |
| 311 | << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 312 | LIR* lir_insn; |
| 313 | int insns_size = cu_->code_item->insns_size_in_code_units_; |
| 314 | |
| 315 | LOG(INFO) << "Regs (excluding ins) : " << cu_->num_regs; |
| 316 | LOG(INFO) << "Ins : " << cu_->num_ins; |
| 317 | LOG(INFO) << "Outs : " << cu_->num_outs; |
| 318 | LOG(INFO) << "CoreSpills : " << num_core_spills_; |
| 319 | LOG(INFO) << "FPSpills : " << num_fp_spills_; |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 320 | LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 321 | LOG(INFO) << "Frame size : " << frame_size_; |
| 322 | LOG(INFO) << "code size is " << total_size_ << |
| 323 | " bytes, Dalvik size is " << insns_size * 2; |
| 324 | LOG(INFO) << "expansion factor: " |
| 325 | << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2); |
| 326 | DumpPromotionMap(); |
buzbee | 7a11ab0 | 2014-04-28 20:02:38 -0700 | [diff] [blame] | 327 | UpdateLIROffsets(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 328 | for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) { |
| 329 | DumpLIRInsn(lir_insn, 0); |
| 330 | } |
| 331 | for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) { |
| 332 | LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset, |
| 333 | lir_insn->operands[0]); |
| 334 | } |
| 335 | |
| 336 | const DexFile::MethodId& method_id = |
| 337 | cu_->dex_file->GetMethodId(cu_->method_idx); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 338 | const Signature signature = cu_->dex_file->GetMethodSignature(method_id); |
| 339 | const char* name = cu_->dex_file->GetMethodName(method_id); |
| 340 | const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 341 | |
| 342 | // Dump mapping tables |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 343 | if (!encoded_mapping_table_.empty()) { |
| 344 | MappingTable table(&encoded_mapping_table_[0]); |
| 345 | DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, |
| 346 | table.PcToDexSize(), table.PcToDexBegin()); |
| 347 | DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, |
| 348 | table.DexToPcSize(), table.DexToPcBegin()); |
| 349 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Search the existing constants in the literal pool for an exact or close match |
| 354 | * within specified delta (greater or equal to 0). |
| 355 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 356 | LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 357 | while (data_target) { |
| 358 | if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta) |
| 359 | return data_target; |
| 360 | data_target = data_target->next; |
| 361 | } |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | /* 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] | 366 | LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 367 | bool lo_match = false; |
| 368 | LIR* lo_target = NULL; |
| 369 | while (data_target) { |
| 370 | if (lo_match && (data_target->operands[0] == val_hi)) { |
| 371 | // Record high word in case we need to expand this later. |
| 372 | lo_target->operands[1] = val_hi; |
| 373 | return lo_target; |
| 374 | } |
| 375 | lo_match = false; |
| 376 | if (data_target->operands[0] == val_lo) { |
| 377 | lo_match = true; |
| 378 | lo_target = data_target; |
| 379 | } |
| 380 | data_target = data_target->next; |
| 381 | } |
| 382 | return NULL; |
| 383 | } |
| 384 | |
Vladimir Marko | a51a0b0 | 2014-05-21 12:08:39 +0100 | [diff] [blame] | 385 | /* Search the existing constants in the literal pool for an exact method match */ |
| 386 | LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) { |
| 387 | while (data_target) { |
| 388 | if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index && |
| 389 | UnwrapPointer(data_target->operands[1]) == method.dex_file) { |
| 390 | return data_target; |
| 391 | } |
| 392 | data_target = data_target->next; |
| 393 | } |
| 394 | return nullptr; |
| 395 | } |
| 396 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 397 | /* |
| 398 | * The following are building blocks to insert constants into the pool or |
| 399 | * instruction streams. |
| 400 | */ |
| 401 | |
| 402 | /* Add a 32-bit constant to the constant pool */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 403 | LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 404 | /* Add the constant to the literal pool */ |
| 405 | if (constant_list_p) { |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 406 | LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 407 | new_value->operands[0] = value; |
| 408 | new_value->next = *constant_list_p; |
| 409 | *constant_list_p = new_value; |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 410 | estimated_native_code_size_ += sizeof(value); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 411 | return new_value; |
| 412 | } |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | /* 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] | 417 | 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] | 418 | AddWordData(constant_list_p, val_hi); |
| 419 | return AddWordData(constant_list_p, val_lo); |
| 420 | } |
| 421 | |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 422 | static void Push32(std::vector<uint8_t>&buf, int data) { |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 423 | buf.push_back(data & 0xff); |
| 424 | buf.push_back((data >> 8) & 0xff); |
| 425 | buf.push_back((data >> 16) & 0xff); |
| 426 | buf.push_back((data >> 24) & 0xff); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 429 | // Push 8 bytes on 64-bit target systems; 4 on 32-bit target systems. |
| 430 | static void PushPointer(std::vector<uint8_t>&buf, const void* pointer, bool target64) { |
| 431 | uint64_t data = reinterpret_cast<uintptr_t>(pointer); |
| 432 | if (target64) { |
| 433 | Push32(buf, data & 0xFFFFFFFF); |
| 434 | Push32(buf, (data >> 32) & 0xFFFFFFFF); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 435 | } else { |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 436 | Push32(buf, static_cast<uint32_t>(data)); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 440 | static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) { |
| 441 | while (buf.size() < offset) { |
| 442 | buf.push_back(0); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* Write the literal pool to the output stream */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 447 | void Mir2Lir::InstallLiteralPools() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 448 | AlignBuffer(code_buffer_, data_offset_); |
| 449 | LIR* data_lir = literal_list_; |
| 450 | while (data_lir != NULL) { |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 451 | Push32(code_buffer_, data_lir->operands[0]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 452 | data_lir = NEXT_LIR(data_lir); |
| 453 | } |
| 454 | // Push code and method literals, record offsets for the compiler to patch. |
| 455 | data_lir = code_literal_list_; |
| 456 | while (data_lir != NULL) { |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 457 | uint32_t target_method_idx = data_lir->operands[0]; |
| 458 | const DexFile* target_dex_file = |
| 459 | reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1])); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 460 | cu_->compiler_driver->AddCodePatch(cu_->dex_file, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 461 | cu_->class_def_idx, |
| 462 | cu_->method_idx, |
| 463 | cu_->invoke_type, |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 464 | target_method_idx, |
| 465 | target_dex_file, |
| 466 | static_cast<InvokeType>(data_lir->operands[2]), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 467 | code_buffer_.size()); |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 468 | const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 469 | // unique value based on target to ensure code deduplication works |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 470 | PushPointer(code_buffer_, &target_method_id, cu_->target64); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 471 | data_lir = NEXT_LIR(data_lir); |
| 472 | } |
| 473 | data_lir = method_literal_list_; |
| 474 | while (data_lir != NULL) { |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 475 | uint32_t target_method_idx = data_lir->operands[0]; |
| 476 | const DexFile* target_dex_file = |
| 477 | reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1])); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 478 | cu_->compiler_driver->AddMethodPatch(cu_->dex_file, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 479 | cu_->class_def_idx, |
| 480 | cu_->method_idx, |
| 481 | cu_->invoke_type, |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 482 | target_method_idx, |
| 483 | target_dex_file, |
| 484 | static_cast<InvokeType>(data_lir->operands[2]), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 485 | code_buffer_.size()); |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 486 | const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 487 | // unique value based on target to ensure code deduplication works |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 488 | PushPointer(code_buffer_, &target_method_id, cu_->target64); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 489 | data_lir = NEXT_LIR(data_lir); |
| 490 | } |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 491 | // Push class literals. |
| 492 | data_lir = class_literal_list_; |
| 493 | while (data_lir != NULL) { |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 494 | uint32_t target_method_idx = data_lir->operands[0]; |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 495 | cu_->compiler_driver->AddClassPatch(cu_->dex_file, |
| 496 | cu_->class_def_idx, |
| 497 | cu_->method_idx, |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 498 | target_method_idx, |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 499 | code_buffer_.size()); |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 500 | const DexFile::TypeId& target_method_id = cu_->dex_file->GetTypeId(target_method_idx); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 501 | // unique value based on target to ensure code deduplication works |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 502 | PushPointer(code_buffer_, &target_method_id, cu_->target64); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 503 | data_lir = NEXT_LIR(data_lir); |
| 504 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* Write the switch tables to the output stream */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 508 | void Mir2Lir::InstallSwitchTables() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 509 | GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_); |
| 510 | while (true) { |
| 511 | Mir2Lir::SwitchTable* tab_rec = iterator.Next(); |
| 512 | if (tab_rec == NULL) break; |
| 513 | AlignBuffer(code_buffer_, tab_rec->offset); |
| 514 | /* |
| 515 | * For Arm, our reference point is the address of the bx |
| 516 | * instruction that does the launch, so we have to subtract |
| 517 | * the auto pc-advance. For other targets the reference point |
| 518 | * is a label, so we can use the offset as-is. |
| 519 | */ |
| 520 | int bx_offset = INVALID_OFFSET; |
| 521 | switch (cu_->instruction_set) { |
| 522 | case kThumb2: |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 523 | DCHECK(tab_rec->anchor->flags.fixup != kFixupNone); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 524 | bx_offset = tab_rec->anchor->offset + 4; |
| 525 | break; |
| 526 | case kX86: |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 527 | case kX86_64: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 528 | bx_offset = 0; |
| 529 | break; |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 530 | case kArm64: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 531 | case kMips: |
| 532 | bx_offset = tab_rec->anchor->offset; |
| 533 | break; |
| 534 | default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set; |
| 535 | } |
| 536 | if (cu_->verbose) { |
| 537 | LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset; |
| 538 | } |
| 539 | if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 540 | 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] | 541 | for (int elems = 0; elems < tab_rec->table[1]; elems++) { |
| 542 | int disp = tab_rec->targets[elems]->offset - bx_offset; |
| 543 | if (cu_->verbose) { |
| 544 | LOG(INFO) << " Case[" << elems << "] key: 0x" |
| 545 | << std::hex << keys[elems] << ", disp: 0x" |
| 546 | << std::hex << disp; |
| 547 | } |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 548 | Push32(code_buffer_, keys[elems]); |
| 549 | Push32(code_buffer_, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 550 | tab_rec->targets[elems]->offset - bx_offset); |
| 551 | } |
| 552 | } else { |
| 553 | DCHECK_EQ(static_cast<int>(tab_rec->table[0]), |
| 554 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
| 555 | for (int elems = 0; elems < tab_rec->table[1]; elems++) { |
| 556 | int disp = tab_rec->targets[elems]->offset - bx_offset; |
| 557 | if (cu_->verbose) { |
| 558 | LOG(INFO) << " Case[" << elems << "] disp: 0x" |
| 559 | << std::hex << disp; |
| 560 | } |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 561 | Push32(code_buffer_, tab_rec->targets[elems]->offset - bx_offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | /* Write the fill array dta to the output stream */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 568 | void Mir2Lir::InstallFillArrayData() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 569 | GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_); |
| 570 | while (true) { |
| 571 | Mir2Lir::FillArrayData *tab_rec = iterator.Next(); |
| 572 | if (tab_rec == NULL) break; |
| 573 | AlignBuffer(code_buffer_, tab_rec->offset); |
| 574 | for (int i = 0; i < (tab_rec->size + 1) / 2; i++) { |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 575 | code_buffer_.push_back(tab_rec->table[i] & 0xFF); |
| 576 | code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 581 | static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) { |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 582 | for (; lir != NULL; lir = lir->next) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 583 | lir->offset = offset; |
| 584 | offset += 4; |
| 585 | } |
| 586 | return offset; |
| 587 | } |
| 588 | |
Ian Rogers | ff093b3 | 2014-04-30 19:04:27 -0700 | [diff] [blame] | 589 | static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset, |
| 590 | unsigned int element_size) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 591 | // Align to natural pointer size. |
Andreas Gampe | 6601882 | 2014-05-05 20:47:19 -0700 | [diff] [blame] | 592 | offset = RoundUp(offset, element_size); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 593 | for (; lir != NULL; lir = lir->next) { |
| 594 | lir->offset = offset; |
| 595 | offset += element_size; |
| 596 | } |
| 597 | return offset; |
| 598 | } |
| 599 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 600 | // Make sure we have a code address for every declared catch entry |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 601 | bool Mir2Lir::VerifyCatchEntries() { |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 602 | MappingTable table(&encoded_mapping_table_[0]); |
| 603 | std::vector<uint32_t> dex_pcs; |
| 604 | dex_pcs.reserve(table.DexToPcSize()); |
| 605 | for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) { |
| 606 | dex_pcs.push_back(it.DexPc()); |
| 607 | } |
| 608 | // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_. |
| 609 | std::sort(dex_pcs.begin(), dex_pcs.end()); |
| 610 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 611 | bool success = true; |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 612 | auto it = dex_pcs.begin(), end = dex_pcs.end(); |
| 613 | for (uint32_t dex_pc : mir_graph_->catches_) { |
| 614 | while (it != end && *it < dex_pc) { |
| 615 | LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it; |
| 616 | ++it; |
| 617 | success = false; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 618 | } |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 619 | if (it == end || *it > dex_pc) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 620 | LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc; |
| 621 | success = false; |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 622 | } else { |
| 623 | ++it; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | if (!success) { |
| 627 | LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 628 | LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: " |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 629 | << table.DexToPcSize(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 630 | } |
| 631 | return success; |
| 632 | } |
| 633 | |
| 634 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 635 | void Mir2Lir::CreateMappingTables() { |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 636 | uint32_t pc2dex_data_size = 0u; |
| 637 | uint32_t pc2dex_entries = 0u; |
| 638 | uint32_t pc2dex_offset = 0u; |
| 639 | uint32_t pc2dex_dalvik_offset = 0u; |
| 640 | uint32_t dex2pc_data_size = 0u; |
| 641 | uint32_t dex2pc_entries = 0u; |
| 642 | uint32_t dex2pc_offset = 0u; |
| 643 | uint32_t dex2pc_dalvik_offset = 0u; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 644 | for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) { |
| 645 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) { |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 646 | pc2dex_entries += 1; |
| 647 | DCHECK(pc2dex_offset <= tgt_lir->offset); |
| 648 | pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset); |
| 649 | pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) - |
| 650 | static_cast<int32_t>(pc2dex_dalvik_offset)); |
| 651 | pc2dex_offset = tgt_lir->offset; |
| 652 | pc2dex_dalvik_offset = tgt_lir->dalvik_offset; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 653 | } |
| 654 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) { |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 655 | dex2pc_entries += 1; |
| 656 | DCHECK(dex2pc_offset <= tgt_lir->offset); |
| 657 | dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset); |
| 658 | dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) - |
| 659 | static_cast<int32_t>(dex2pc_dalvik_offset)); |
| 660 | dex2pc_offset = tgt_lir->offset; |
| 661 | dex2pc_dalvik_offset = tgt_lir->dalvik_offset; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 664 | |
| 665 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 666 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 667 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 668 | encoded_mapping_table_.resize(data_size); |
| 669 | uint8_t* write_pos = &encoded_mapping_table_[0]; |
| 670 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 671 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 672 | DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size); |
| 673 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 674 | |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 675 | pc2dex_offset = 0u; |
| 676 | pc2dex_dalvik_offset = 0u; |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 677 | dex2pc_offset = 0u; |
| 678 | dex2pc_dalvik_offset = 0u; |
| 679 | for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) { |
| 680 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) { |
| 681 | DCHECK(pc2dex_offset <= tgt_lir->offset); |
| 682 | write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset); |
| 683 | write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) - |
| 684 | static_cast<int32_t>(pc2dex_dalvik_offset)); |
| 685 | pc2dex_offset = tgt_lir->offset; |
| 686 | pc2dex_dalvik_offset = tgt_lir->dalvik_offset; |
| 687 | } |
| 688 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) { |
| 689 | DCHECK(dex2pc_offset <= tgt_lir->offset); |
| 690 | write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset); |
| 691 | write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) - |
| 692 | static_cast<int32_t>(dex2pc_dalvik_offset)); |
| 693 | dex2pc_offset = tgt_lir->offset; |
| 694 | dex2pc_dalvik_offset = tgt_lir->dalvik_offset; |
| 695 | } |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 696 | } |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 697 | DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), |
| 698 | hdr_data_size + pc2dex_data_size); |
| 699 | DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size); |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 700 | |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 701 | if (kIsDebugBuild) { |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 702 | CHECK(VerifyCatchEntries()); |
| 703 | |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 704 | // Verify the encoded table holds the expected data. |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 705 | MappingTable table(&encoded_mapping_table_[0]); |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 706 | CHECK_EQ(table.TotalSize(), total_entries); |
| 707 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 708 | auto it = table.PcToDexBegin(); |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 709 | auto it2 = table.DexToPcBegin(); |
| 710 | for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) { |
| 711 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) { |
| 712 | CHECK_EQ(tgt_lir->offset, it.NativePcOffset()); |
| 713 | CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc()); |
| 714 | ++it; |
| 715 | } |
| 716 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) { |
| 717 | CHECK_EQ(tgt_lir->offset, it2.NativePcOffset()); |
| 718 | CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc()); |
| 719 | ++it2; |
| 720 | } |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 721 | } |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 722 | CHECK(it == table.PcToDexEnd()); |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 723 | CHECK(it2 == table.DexToPcEnd()); |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 724 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 727 | void Mir2Lir::CreateNativeGcMap() { |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 728 | DCHECK(!encoded_mapping_table_.empty()); |
| 729 | MappingTable mapping_table(&encoded_mapping_table_[0]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 730 | uint32_t max_native_offset = 0; |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 731 | for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) { |
| 732 | uint32_t native_offset = it.NativePcOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 733 | if (native_offset > max_native_offset) { |
| 734 | max_native_offset = native_offset; |
| 735 | } |
| 736 | } |
| 737 | MethodReference method_ref(cu_->dex_file, cu_->method_idx); |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 738 | const std::vector<uint8_t>& gc_map_raw = |
| 739 | mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap(); |
| 740 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 741 | DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 742 | // Compute native offset to references size. |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 743 | GcMapBuilder native_gc_map_builder(&native_gc_map_, |
| 744 | mapping_table.PcToDexSize(), |
| 745 | max_native_offset, dex_gc_map.RegWidth()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 746 | |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 747 | for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) { |
| 748 | uint32_t native_offset = it.NativePcOffset(); |
| 749 | uint32_t dex_pc = it.DexPc(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 750 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 751 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc << |
| 752 | ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 753 | native_gc_map_builder.AddEntry(native_offset, references); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /* Determine the offset of each literal field */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 758 | int Mir2Lir::AssignLiteralOffset(CodeOffset offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 759 | offset = AssignLiteralOffsetCommon(literal_list_, offset); |
Ian Rogers | ff093b3 | 2014-04-30 19:04:27 -0700 | [diff] [blame] | 760 | unsigned int ptr_size = GetInstructionSetPointerSize(cu_->instruction_set); |
| 761 | offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size); |
| 762 | offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size); |
| 763 | offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 764 | return offset; |
| 765 | } |
| 766 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 767 | int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 768 | GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_); |
| 769 | while (true) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 770 | Mir2Lir::SwitchTable* tab_rec = iterator.Next(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 771 | if (tab_rec == NULL) break; |
| 772 | tab_rec->offset = offset; |
| 773 | if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) { |
| 774 | offset += tab_rec->table[1] * (sizeof(int) * 2); |
| 775 | } else { |
| 776 | DCHECK_EQ(static_cast<int>(tab_rec->table[0]), |
| 777 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
| 778 | offset += tab_rec->table[1] * sizeof(int); |
| 779 | } |
| 780 | } |
| 781 | return offset; |
| 782 | } |
| 783 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 784 | int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 785 | GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_); |
| 786 | while (true) { |
| 787 | Mir2Lir::FillArrayData *tab_rec = iterator.Next(); |
| 788 | if (tab_rec == NULL) break; |
| 789 | tab_rec->offset = offset; |
| 790 | offset += tab_rec->size; |
| 791 | // word align |
Andreas Gampe | 6601882 | 2014-05-05 20:47:19 -0700 | [diff] [blame] | 792 | offset = RoundUp(offset, 4); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 793 | } |
| 794 | return offset; |
| 795 | } |
| 796 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 797 | /* |
| 798 | * Insert a kPseudoCaseLabel at the beginning of the Dalvik |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 799 | * offset vaddr if pretty-printing, otherise use the standard block |
| 800 | * label. The selected label will be used to fix up the case |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 801 | * branch table during the assembly phase. All resource flags |
| 802 | * are set to prevent code motion. KeyVal is just there for debugging. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 803 | */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 804 | LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) { |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 805 | LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id]; |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 806 | LIR* res = boundary_lir; |
| 807 | if (cu_->verbose) { |
| 808 | // Only pay the expense if we're pretty-printing. |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 809 | LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR)); |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 810 | new_label->dalvik_offset = vaddr; |
| 811 | new_label->opcode = kPseudoCaseLabel; |
| 812 | new_label->operands[0] = keyVal; |
| 813 | new_label->flags.fixup = kFixupLabel; |
| 814 | DCHECK(!new_label->flags.use_def_invalid); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 815 | new_label->u.m.def_mask = &kEncodeAll; |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 816 | InsertLIRAfter(boundary_lir, new_label); |
| 817 | res = new_label; |
| 818 | } |
| 819 | return res; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 820 | } |
| 821 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 822 | void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 823 | const uint16_t* table = tab_rec->table; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 824 | DexOffset base_vaddr = tab_rec->vaddr; |
| 825 | const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 826 | int entries = table[1]; |
| 827 | int low_key = s4FromSwitchData(&table[2]); |
| 828 | for (int i = 0; i < entries; i++) { |
| 829 | tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key); |
| 830 | } |
| 831 | } |
| 832 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 833 | void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 834 | const uint16_t* table = tab_rec->table; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 835 | DexOffset base_vaddr = tab_rec->vaddr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 836 | int entries = table[1]; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 837 | const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]); |
| 838 | const int32_t* targets = &keys[entries]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 839 | for (int i = 0; i < entries; i++) { |
| 840 | tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]); |
| 841 | } |
| 842 | } |
| 843 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 844 | void Mir2Lir::ProcessSwitchTables() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 845 | GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_); |
| 846 | while (true) { |
| 847 | Mir2Lir::SwitchTable *tab_rec = iterator.Next(); |
| 848 | if (tab_rec == NULL) break; |
| 849 | if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) { |
| 850 | MarkPackedCaseLabels(tab_rec); |
| 851 | } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) { |
| 852 | MarkSparseCaseLabels(tab_rec); |
| 853 | } else { |
| 854 | LOG(FATAL) << "Invalid switch table"; |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 859 | void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 860 | /* |
| 861 | * Sparse switch data format: |
| 862 | * ushort ident = 0x0200 magic value |
| 863 | * ushort size number of entries in the table; > 0 |
| 864 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 865 | * int targets[size] branch targets, relative to switch opcode |
| 866 | * |
| 867 | * Total size is (2+size*4) 16-bit code units. |
| 868 | */ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 869 | uint16_t ident = table[0]; |
| 870 | int entries = table[1]; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 871 | const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]); |
| 872 | const int32_t* targets = &keys[entries]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 873 | LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident |
| 874 | << ", entries: " << std::dec << entries; |
| 875 | for (int i = 0; i < entries; i++) { |
| 876 | LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i]; |
| 877 | } |
| 878 | } |
| 879 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 880 | void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 881 | /* |
| 882 | * Packed switch data format: |
| 883 | * ushort ident = 0x0100 magic value |
| 884 | * ushort size number of entries in the table |
| 885 | * int first_key first (and lowest) switch case value |
| 886 | * int targets[size] branch targets, relative to switch opcode |
| 887 | * |
| 888 | * Total size is (4+size*2) 16-bit code units. |
| 889 | */ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 890 | uint16_t ident = table[0]; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 891 | const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 892 | int entries = table[1]; |
| 893 | int low_key = s4FromSwitchData(&table[2]); |
| 894 | LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident |
| 895 | << ", entries: " << std::dec << entries << ", low_key: " << low_key; |
| 896 | for (int i = 0; i < entries; i++) { |
| 897 | LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex |
| 898 | << targets[i]; |
| 899 | } |
| 900 | } |
| 901 | |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 902 | /* 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] | 903 | void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) { |
| 904 | // NOTE: only used for debug listings. |
| 905 | NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 908 | bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 909 | bool is_taken; |
| 910 | switch (opcode) { |
| 911 | case Instruction::IF_EQ: is_taken = (src1 == src2); break; |
| 912 | case Instruction::IF_NE: is_taken = (src1 != src2); break; |
| 913 | case Instruction::IF_LT: is_taken = (src1 < src2); break; |
| 914 | case Instruction::IF_GE: is_taken = (src1 >= src2); break; |
| 915 | case Instruction::IF_GT: is_taken = (src1 > src2); break; |
| 916 | case Instruction::IF_LE: is_taken = (src1 <= src2); break; |
| 917 | case Instruction::IF_EQZ: is_taken = (src1 == 0); break; |
| 918 | case Instruction::IF_NEZ: is_taken = (src1 != 0); break; |
| 919 | case Instruction::IF_LTZ: is_taken = (src1 < 0); break; |
| 920 | case Instruction::IF_GEZ: is_taken = (src1 >= 0); break; |
| 921 | case Instruction::IF_GTZ: is_taken = (src1 > 0); break; |
| 922 | case Instruction::IF_LEZ: is_taken = (src1 <= 0); break; |
| 923 | default: |
| 924 | LOG(FATAL) << "Unexpected opcode " << opcode; |
| 925 | is_taken = false; |
| 926 | } |
| 927 | return is_taken; |
| 928 | } |
| 929 | |
| 930 | // Convert relation of src1/src2 to src2/src1 |
| 931 | ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) { |
| 932 | ConditionCode res; |
| 933 | switch (before) { |
| 934 | case kCondEq: res = kCondEq; break; |
| 935 | case kCondNe: res = kCondNe; break; |
| 936 | case kCondLt: res = kCondGt; break; |
| 937 | case kCondGt: res = kCondLt; break; |
| 938 | case kCondLe: res = kCondGe; break; |
| 939 | case kCondGe: res = kCondLe; break; |
| 940 | default: |
| 941 | res = static_cast<ConditionCode>(0); |
| 942 | LOG(FATAL) << "Unexpected ccode " << before; |
| 943 | } |
| 944 | return res; |
| 945 | } |
| 946 | |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 947 | ConditionCode Mir2Lir::NegateComparison(ConditionCode before) { |
| 948 | ConditionCode res; |
| 949 | switch (before) { |
| 950 | case kCondEq: res = kCondNe; break; |
| 951 | case kCondNe: res = kCondEq; break; |
| 952 | case kCondLt: res = kCondGe; break; |
| 953 | case kCondGt: res = kCondLe; break; |
| 954 | case kCondLe: res = kCondGt; break; |
| 955 | case kCondGe: res = kCondLt; break; |
| 956 | default: |
| 957 | res = static_cast<ConditionCode>(0); |
| 958 | LOG(FATAL) << "Unexpected ccode " << before; |
| 959 | } |
| 960 | return res; |
| 961 | } |
| 962 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 963 | // TODO: move to mir_to_lir.cc |
| 964 | Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena) |
| 965 | : Backend(arena), |
| 966 | literal_list_(NULL), |
| 967 | method_literal_list_(NULL), |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 968 | class_literal_list_(NULL), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 969 | code_literal_list_(NULL), |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 970 | first_fixup_(NULL), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 971 | cu_(cu), |
| 972 | mir_graph_(mir_graph), |
| 973 | switch_tables_(arena, 4, kGrowableArraySwitchTables), |
| 974 | fill_array_data_(arena, 4, kGrowableArrayFillArrayData), |
buzbee | bd663de | 2013-09-10 15:41:31 -0700 | [diff] [blame] | 975 | tempreg_info_(arena, 20, kGrowableArrayMisc), |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 976 | reginfo_map_(arena, RegStorage::kMaxRegs, kGrowableArrayMisc), |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 977 | pointer_storage_(arena, 128, kGrowableArrayMisc), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 978 | data_offset_(0), |
| 979 | total_size_(0), |
| 980 | block_label_list_(NULL), |
buzbee | d69835d | 2014-02-03 14:40:27 -0800 | [diff] [blame] | 981 | promotion_map_(NULL), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 982 | current_dalvik_offset_(0), |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 983 | estimated_native_code_size_(0), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 984 | reg_pool_(NULL), |
| 985 | live_sreg_(0), |
| 986 | num_core_spills_(0), |
| 987 | num_fp_spills_(0), |
| 988 | frame_size_(0), |
| 989 | core_spill_mask_(0), |
| 990 | fp_spill_mask_(0), |
| 991 | first_lir_insn_(NULL), |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 992 | last_lir_insn_(NULL), |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 993 | slow_paths_(arena, 32, kGrowableArraySlowPaths), |
| 994 | mem_ref_type_(ResourceMask::kHeapRef), |
| 995 | mask_cache_(arena) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 996 | // Reserve pointer id 0 for NULL. |
| 997 | size_t null_idx = WrapPointer(NULL); |
| 998 | DCHECK_EQ(null_idx, 0U); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void Mir2Lir::Materialize() { |
buzbee | a61f495 | 2013-08-23 14:27:06 -0700 | [diff] [blame] | 1002 | cu_->NewTimingSplit("RegisterAllocation"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1003 | CompilerInitializeRegAlloc(); // Needs to happen after SSA naming |
| 1004 | |
| 1005 | /* Allocate Registers using simple local allocation scheme */ |
| 1006 | SimpleRegAlloc(); |
| 1007 | |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 1008 | /* First try the custom light codegen for special cases. */ |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 1009 | DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr); |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 1010 | bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file) |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 1011 | ->GenSpecial(this, cu_->method_idx); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1012 | |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 1013 | /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */ |
| 1014 | if (special_worked == false) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1015 | MethodMIR2LIR(); |
| 1016 | } |
| 1017 | |
| 1018 | /* Method is not empty */ |
| 1019 | if (first_lir_insn_) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1020 | // mark the targets of switch statement case labels |
| 1021 | ProcessSwitchTables(); |
| 1022 | |
| 1023 | /* Convert LIR into machine code. */ |
| 1024 | AssembleLIR(); |
| 1025 | |
buzbee | b01bf15 | 2014-05-13 15:59:07 -0700 | [diff] [blame] | 1026 | if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1027 | CodegenDump(); |
| 1028 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1029 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | CompiledMethod* Mir2Lir::GetCompiledMethod() { |
Vladimir Marko | 2e589aa | 2014-02-25 17:53:53 +0000 | [diff] [blame] | 1033 | // Combine vmap tables - core regs, then fp regs - into vmap_table. |
| 1034 | Leb128EncodingVector vmap_encoder; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1035 | if (frame_size_ > 0) { |
Vladimir Marko | 2e589aa | 2014-02-25 17:53:53 +0000 | [diff] [blame] | 1036 | // Prefix the encoded data with its size. |
| 1037 | size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size(); |
| 1038 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 1039 | vmap_encoder.PushBackUnsigned(size); |
| 1040 | // Core regs may have been inserted out of order - sort first. |
| 1041 | std::sort(core_vmap_table_.begin(), core_vmap_table_.end()); |
| 1042 | for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) { |
| 1043 | // Copy, stripping out the phys register sort key. |
| 1044 | vmap_encoder.PushBackUnsigned( |
| 1045 | ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment)); |
| 1046 | } |
| 1047 | // Push a marker to take place of lr. |
| 1048 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 1049 | // fp regs already sorted. |
| 1050 | for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) { |
| 1051 | vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment); |
| 1052 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1053 | } else { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 1054 | DCHECK_EQ(POPCOUNT(core_spill_mask_), 0); |
| 1055 | DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0); |
Vladimir Marko | 2e589aa | 2014-02-25 17:53:53 +0000 | [diff] [blame] | 1056 | DCHECK_EQ(core_vmap_table_.size(), 0u); |
| 1057 | DCHECK_EQ(fp_vmap_table_.size(), 0u); |
| 1058 | vmap_encoder.PushBackUnsigned(0u); // Size is 0. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1059 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1060 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1061 | std::unique_ptr<std::vector<uint8_t>> cfi_info(ReturnCallFrameInformation()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1062 | CompiledMethod* result = |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 1063 | new CompiledMethod(cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_, |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 1064 | core_spill_mask_, fp_spill_mask_, encoded_mapping_table_, |
Dave Allison | d6ed642 | 2014-04-09 23:36:15 +0000 | [diff] [blame] | 1065 | vmap_encoder.GetData(), native_gc_map_, cfi_info.get()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1066 | return result; |
| 1067 | } |
| 1068 | |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 1069 | size_t Mir2Lir::GetMaxPossibleCompilerTemps() const { |
| 1070 | // Chose a reasonably small value in order to contain stack growth. |
| 1071 | // Backends that are smarter about spill region can return larger values. |
| 1072 | const size_t max_compiler_temps = 10; |
| 1073 | return max_compiler_temps; |
| 1074 | } |
| 1075 | |
| 1076 | size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() { |
| 1077 | // By default assume that the Mir2Lir will need one slot for each temporary. |
| 1078 | // If the backend can better determine temps that have non-overlapping ranges and |
| 1079 | // temps that do not need spilled, it can actually provide a small region. |
| 1080 | return (mir_graph_->GetNumUsedCompilerTemps() * sizeof(uint32_t)); |
| 1081 | } |
| 1082 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1083 | int Mir2Lir::ComputeFrameSize() { |
| 1084 | /* Figure out the frame size */ |
Dmitry Petrochenko | f29a424 | 2014-05-05 20:28:47 +0700 | [diff] [blame] | 1085 | uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set) |
| 1086 | + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set) |
| 1087 | + sizeof(uint32_t) // Filler. |
| 1088 | + (cu_->num_regs + cu_->num_outs) * sizeof(uint32_t) |
| 1089 | + GetNumBytesForCompilerTempSpillRegion(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1090 | /* Align and set */ |
Andreas Gampe | 6601882 | 2014-05-05 20:47:19 -0700 | [diff] [blame] | 1091 | return RoundUp(size, kStackAlignment); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * Append an LIR instruction to the LIR list maintained by a compilation |
| 1096 | * unit |
| 1097 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1098 | void Mir2Lir::AppendLIR(LIR* lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1099 | if (first_lir_insn_ == NULL) { |
| 1100 | DCHECK(last_lir_insn_ == NULL); |
| 1101 | last_lir_insn_ = first_lir_insn_ = lir; |
| 1102 | lir->prev = lir->next = NULL; |
| 1103 | } else { |
| 1104 | last_lir_insn_->next = lir; |
| 1105 | lir->prev = last_lir_insn_; |
| 1106 | lir->next = NULL; |
| 1107 | last_lir_insn_ = lir; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * Insert an LIR instruction before the current instruction, which cannot be the |
| 1113 | * first instruction. |
| 1114 | * |
| 1115 | * prev_lir <-> new_lir <-> current_lir |
| 1116 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1117 | void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1118 | DCHECK(current_lir->prev != NULL); |
| 1119 | LIR *prev_lir = current_lir->prev; |
| 1120 | |
| 1121 | prev_lir->next = new_lir; |
| 1122 | new_lir->prev = prev_lir; |
| 1123 | new_lir->next = current_lir; |
| 1124 | current_lir->prev = new_lir; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * Insert an LIR instruction after the current instruction, which cannot be the |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1129 | * last instruction. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1130 | * |
| 1131 | * current_lir -> new_lir -> old_next |
| 1132 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1133 | void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1134 | new_lir->prev = current_lir; |
| 1135 | new_lir->next = current_lir->next; |
| 1136 | current_lir->next = new_lir; |
| 1137 | new_lir->next->prev = new_lir; |
| 1138 | } |
| 1139 | |
Mark Mendell | 4708dcd | 2014-01-22 09:05:18 -0800 | [diff] [blame] | 1140 | bool Mir2Lir::IsPowerOfTwo(uint64_t x) { |
| 1141 | return (x & (x - 1)) == 0; |
| 1142 | } |
| 1143 | |
| 1144 | // Returns the index of the lowest set bit in 'x'. |
| 1145 | int32_t Mir2Lir::LowestSetBit(uint64_t x) { |
| 1146 | int bit_posn = 0; |
| 1147 | while ((x & 0xf) == 0) { |
| 1148 | bit_posn += 4; |
| 1149 | x >>= 4; |
| 1150 | } |
| 1151 | while ((x & 1) == 0) { |
| 1152 | bit_posn++; |
| 1153 | x >>= 1; |
| 1154 | } |
| 1155 | return bit_posn; |
| 1156 | } |
| 1157 | |
| 1158 | bool Mir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) { |
| 1159 | DCHECK(rl_src.wide); |
| 1160 | DCHECK(rl_dest.wide); |
| 1161 | return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1); |
| 1162 | } |
| 1163 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1164 | LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg, |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 1165 | int offset, int check_value, LIR* target) { |
| 1166 | // Handle this for architectures that can't compare to memory. |
buzbee | 695d13a | 2014-04-19 13:32:20 -0700 | [diff] [blame] | 1167 | Load32Disp(base_reg, offset, temp_reg); |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 1168 | LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target); |
| 1169 | return branch; |
| 1170 | } |
| 1171 | |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1172 | void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) { |
| 1173 | slow_paths_.Insert(slowpath); |
| 1174 | } |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 1175 | |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 1176 | void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type, |
| 1177 | SpecialTargetRegister symbolic_reg) { |
Vladimir Marko | a51a0b0 | 2014-05-21 12:08:39 +0100 | [diff] [blame] | 1178 | LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method); |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 1179 | if (data_target == NULL) { |
Vladimir Marko | a51a0b0 | 2014-05-21 12:08:39 +0100 | [diff] [blame] | 1180 | data_target = AddWordData(&code_literal_list_, target_method.dex_method_index); |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 1181 | data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file)); |
Vladimir Marko | a51a0b0 | 2014-05-21 12:08:39 +0100 | [diff] [blame] | 1182 | // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have |
| 1183 | // the same method invoked with kVirtual, kSuper and kInterface but the class linker will |
| 1184 | // resolve these invokes to the same method, so we don't care which one we record here. |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 1185 | data_target->operands[2] = type; |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 1186 | } |
buzbee | b5860fb | 2014-06-21 15:31:01 -0700 | [diff] [blame^] | 1187 | // TODO: This is actually a pointer, not a reference. |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 1188 | LIR* load_pc_rel = OpPcRelLoad(TargetRefReg(symbolic_reg), data_target); |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 1189 | AppendLIR(load_pc_rel); |
| 1190 | DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target); |
| 1191 | } |
| 1192 | |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 1193 | void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type, |
| 1194 | SpecialTargetRegister symbolic_reg) { |
Vladimir Marko | a51a0b0 | 2014-05-21 12:08:39 +0100 | [diff] [blame] | 1195 | LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method); |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 1196 | if (data_target == NULL) { |
Vladimir Marko | a51a0b0 | 2014-05-21 12:08:39 +0100 | [diff] [blame] | 1197 | data_target = AddWordData(&method_literal_list_, target_method.dex_method_index); |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 1198 | data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file)); |
Vladimir Marko | a51a0b0 | 2014-05-21 12:08:39 +0100 | [diff] [blame] | 1199 | // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have |
| 1200 | // the same method invoked with kVirtual, kSuper and kInterface but the class linker will |
| 1201 | // resolve these invokes to the same method, so we don't care which one we record here. |
Jeff Hao | 49161ce | 2014-03-12 11:05:25 -0700 | [diff] [blame] | 1202 | data_target->operands[2] = type; |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 1203 | } |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 1204 | LIR* load_pc_rel = OpPcRelLoad(TargetRefReg(symbolic_reg), data_target); |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 1205 | AppendLIR(load_pc_rel); |
| 1206 | DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target); |
| 1207 | } |
| 1208 | |
| 1209 | void Mir2Lir::LoadClassType(uint32_t type_idx, SpecialTargetRegister symbolic_reg) { |
| 1210 | // Use the literal pool and a PC-relative load from a data word. |
| 1211 | LIR* data_target = ScanLiteralPool(class_literal_list_, type_idx, 0); |
| 1212 | if (data_target == nullptr) { |
| 1213 | data_target = AddWordData(&class_literal_list_, type_idx); |
| 1214 | } |
buzbee | b5860fb | 2014-06-21 15:31:01 -0700 | [diff] [blame^] | 1215 | LIR* load_pc_rel = OpPcRelLoad(TargetRefReg(symbolic_reg), data_target); |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 1216 | AppendLIR(load_pc_rel); |
| 1217 | } |
| 1218 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1219 | std::vector<uint8_t>* Mir2Lir::ReturnCallFrameInformation() { |
| 1220 | // Default case is to do nothing. |
| 1221 | return nullptr; |
| 1222 | } |
| 1223 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1224 | RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 1225 | if (loc.location == kLocPhysReg) { |
buzbee | 85089dd | 2014-05-25 15:10:52 -0700 | [diff] [blame] | 1226 | DCHECK(!loc.reg.Is32Bit()); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 1227 | if (loc.reg.IsPair()) { |
buzbee | 85089dd | 2014-05-25 15:10:52 -0700 | [diff] [blame] | 1228 | RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow()); |
| 1229 | RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh()); |
| 1230 | info_lo->SetIsWide(false); |
| 1231 | info_hi->SetIsWide(false); |
| 1232 | loc.reg = info_lo->GetReg(); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 1233 | } else { |
buzbee | 85089dd | 2014-05-25 15:10:52 -0700 | [diff] [blame] | 1234 | RegisterInfo* info = GetRegInfo(loc.reg); |
| 1235 | RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask); |
| 1236 | DCHECK(info_new != nullptr); |
| 1237 | if (info->IsLive() && (info->SReg() == loc.s_reg_low)) { |
| 1238 | info->MarkDead(); |
| 1239 | info_new->MarkLive(loc.s_reg_low); |
| 1240 | } |
| 1241 | loc.reg = info_new->GetReg(); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 1242 | } |
buzbee | 85089dd | 2014-05-25 15:10:52 -0700 | [diff] [blame] | 1243 | DCHECK(loc.reg.Valid()); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1244 | } |
buzbee | 85089dd | 2014-05-25 15:10:52 -0700 | [diff] [blame] | 1245 | loc.wide = false; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1246 | return loc; |
| 1247 | } |
| 1248 | |
Mark Mendell | d65c51a | 2014-04-29 16:55:20 -0400 | [diff] [blame] | 1249 | void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) { |
| 1250 | LOG(FATAL) << "Unknown MIR opcode not supported on this architecture"; |
| 1251 | } |
| 1252 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1253 | } // namespace art |