blob: dbcc86865722a2ad0b0e0561fdb7becc18374c0e [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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
Andreas Gampe0b9203e2015-01-22 20:39:27 -080017#include "mir_to_lir-inl.h"
18
Vladimir Marko4bf90122015-10-08 19:07:04 +010019// Mac does not provide endian.h, so we'll use byte order agnostic code.
20#ifndef __APPLE__
Vladimir Markoec7802a2015-10-01 20:57:57 +010021#include <endian.h>
Vladimir Marko4bf90122015-10-08 19:07:04 +010022#endif
Vladimir Markoec7802a2015-10-01 20:57:57 +010023
Vladimir Marko767c7522015-03-20 12:47:30 +000024#include "base/bit_vector-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080025#include "dex/mir_graph.h"
26#include "driver/compiler_driver.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070027#include "driver/compiler_options.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080028#include "driver/dex_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "dex_file-inl.h"
30#include "gc_map.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000031#include "gc_map_builder.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070032#include "mapping_table.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000033#include "dex/quick/dex_file_method_inliner.h"
34#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000035#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000036#include "dex/verified_method.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000037#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070038#include "verifier/dex_gc_map.h"
39#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000040#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070041
42namespace art {
43
Vladimir Marko06606b92013-12-02 15:31:08 +000044namespace {
45
46/* Dump a mapping table */
47template <typename It>
48void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
49 const Signature& signature, uint32_t size, It first) {
50 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080051 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000052 descriptor, name, signature.ToString().c_str(), size));
53 std::replace(line.begin(), line.end(), ';', '_');
54 LOG(INFO) << line;
55 for (uint32_t i = 0; i != size; ++i) {
56 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
57 ++first;
58 LOG(INFO) << line;
59 }
60 LOG(INFO) <<" };\n\n";
61 }
62}
63
64} // anonymous namespace
65
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070066bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 bool res = false;
68 if (rl_src.is_const) {
69 if (rl_src.wide) {
Andreas Gampede0b9962014-08-27 14:24:42 -070070 // For wide registers, check whether we're the high partner. In that case we need to switch
71 // to the lower one for the correct value.
72 if (rl_src.high_word) {
73 rl_src.high_word = false;
74 rl_src.s_reg_low--;
75 rl_src.orig_sreg--;
76 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070078 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070080 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 }
82 } else {
83 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070084 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070085 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070086 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 }
88 }
89 }
90 return res;
91}
92
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070093void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070094 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010095 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010097 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +000098 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
99 safepoints_.emplace_back(safepoint_pc, current_mir_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100}
101
Andreas Gampe3c12c512014-06-24 18:46:29 +0000102void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
103 DCHECK(!after->flags.use_def_invalid);
104 after->u.m.def_mask = &kEncodeAll;
105 // As NewLIR0 uses Append, we need to create the LIR by hand.
106 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
107 if (after->next == nullptr) {
108 DCHECK_EQ(after, last_lir_insn_);
109 AppendLIR(safepoint_pc);
110 } else {
111 InsertLIRAfter(after, safepoint_pc);
112 }
113 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +0000114 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
115 safepoints_.emplace_back(safepoint_pc, current_mir_);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000116}
117
buzbee252254b2013-09-08 16:20:53 -0700118/* Remove a LIR from the list. */
119void Mir2Lir::UnlinkLIR(LIR* lir) {
120 if (UNLIKELY(lir == first_lir_insn_)) {
121 first_lir_insn_ = lir->next;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700122 if (lir->next != nullptr) {
123 lir->next->prev = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700124 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700125 DCHECK(lir->next == nullptr);
buzbee252254b2013-09-08 16:20:53 -0700126 DCHECK(lir == last_lir_insn_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700127 last_lir_insn_ = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700128 }
129 } else if (lir == last_lir_insn_) {
130 last_lir_insn_ = lir->prev;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700131 lir->prev->next = nullptr;
132 } else if ((lir->prev != nullptr) && (lir->next != nullptr)) {
buzbee252254b2013-09-08 16:20:53 -0700133 lir->prev->next = lir->next;
134 lir->next->prev = lir->prev;
135 }
136}
137
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700139void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700141 if (!cu_->verbose) {
142 UnlinkLIR(lir);
143 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144}
145
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700146void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700148 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100149 // TODO: Avoid the extra Arena allocation!
150 const ResourceMask** mask_ptr;
151 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700153 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700155 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100157 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100159 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 /* ..and then add back the one we need */
161 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100162 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100164 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100166 case ResourceMask::kDalvikReg:
167 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100169 case ResourceMask::kHeapRef:
170 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100172 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 /* Currently only loads can be marked as kMustNotAlias */
174 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100175 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 break;
177 default:
178 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
179 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100180 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181}
182
183/*
184 * Mark load/store instructions that access Dalvik registers through the stack.
185 */
186void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700187 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100188 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
189 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190
191 /*
192 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
193 * access.
194 */
buzbeeb48819d2013-09-14 16:15:25 -0700195 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196}
197
198/*
199 * Debugging macros
200 */
201#define DUMP_RESOURCE_MASK(X)
202
203/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700204void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 int offset = lir->offset;
206 int dest = lir->operands[0];
207 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
208
209 /* Handle pseudo-ops individually, and all regular insns as a group */
210 switch (lir->opcode) {
David Srbecky6f715892015-03-30 14:21:42 +0100211 case kPseudoPrologueBegin:
212 LOG(INFO) << "-------- PrologueBegin";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 break;
David Srbecky6f715892015-03-30 14:21:42 +0100214 case kPseudoPrologueEnd:
215 LOG(INFO) << "-------- PrologueEnd";
216 break;
217 case kPseudoEpilogueBegin:
218 LOG(INFO) << "-------- EpilogueBegin";
219 break;
220 case kPseudoEpilogueEnd:
221 LOG(INFO) << "-------- EpilogueEnd";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 break;
223 case kPseudoBarrier:
224 LOG(INFO) << "-------- BARRIER";
225 break;
226 case kPseudoEntryBlock:
227 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
228 break;
229 case kPseudoDalvikByteCodeBoundary:
230 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700231 // NOTE: only used for debug listings.
232 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 }
234 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000235 << lir->dalvik_offset << " @ "
Vladimir Markof6737f72015-03-23 17:05:14 +0000236 << UnwrapPointer<char>(lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 break;
238 case kPseudoExitBlock:
239 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
240 break;
241 case kPseudoPseudoAlign4:
242 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
243 << offset << "): .align4";
244 break;
245 case kPseudoEHBlockLabel:
246 LOG(INFO) << "Exception_Handling:";
247 break;
248 case kPseudoTargetLabel:
249 case kPseudoNormalBlockLabel:
250 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
251 break;
252 case kPseudoThrowTarget:
253 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
254 break;
255 case kPseudoIntrinsicRetry:
256 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
257 break;
258 case kPseudoSuspendTarget:
259 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
260 break;
261 case kPseudoSafepointPC:
262 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
263 break;
264 case kPseudoExportedPC:
265 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
266 break;
267 case kPseudoCaseLabel:
268 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
269 << std::hex << lir->operands[0] << "|" << std::dec <<
270 lir->operands[0];
271 break;
272 default:
273 if (lir->flags.is_nop && !dump_nop) {
274 break;
275 } else {
276 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
277 lir, base_addr));
278 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
279 lir, base_addr));
David Srbecky6f715892015-03-30 14:21:42 +0100280 LOG(INFO) << StringPrintf("%5p|0x%02x: %-9s%s%s",
Ian Rogers107c31e2014-01-23 20:55:29 -0800281 base_addr + offset,
David Srbecky6f715892015-03-30 14:21:42 +0100282 lir->dalvik_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 op_name.c_str(), op_operands.c_str(),
284 lir->flags.is_nop ? "(nop)" : "");
285 }
286 break;
287 }
288
buzbeeb48819d2013-09-14 16:15:25 -0700289 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100290 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 }
buzbeeb48819d2013-09-14 16:15:25 -0700292 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100293 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 }
295}
296
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700297void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700298 uint32_t num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
299 for (uint32_t i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 PromotionMap v_reg_map = promotion_map_[i];
301 std::string buf;
302 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700303 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 }
305
306 std::string buf3;
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700307 if (i < mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 StringAppendF(&buf3, "%02d", i);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700309 } else if (i == mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 buf3 = "Method*";
311 } else {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700312 uint32_t diff = i - mir_graph_->GetNumOfCodeVRs();
313 StringAppendF(&buf3, "ct%d", diff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 }
315
316 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
317 v_reg_map.core_location == kLocPhysReg ?
318 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
319 v_reg_map.core_reg : SRegOffset(i),
320 buf.c_str());
321 }
322}
323
buzbee7a11ab02014-04-28 20:02:38 -0700324void Mir2Lir::UpdateLIROffsets() {
325 // Only used for code listings.
326 size_t offset = 0;
327 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
328 lir->offset = offset;
329 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
330 offset += GetInsnSize(lir);
331 } else if (lir->opcode == kPseudoPseudoAlign4) {
332 offset += (offset & 0x2);
333 }
334 }
335}
336
Vladimir Marko743b98c2014-11-24 19:45:41 +0000337void Mir2Lir::MarkGCCard(int opt_flags, RegStorage val_reg, RegStorage tgt_addr_reg) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000338 DCHECK(val_reg.Valid());
339 DCHECK_EQ(val_reg.Is64Bit(), cu_->target64);
Vladimir Marko743b98c2014-11-24 19:45:41 +0000340 if ((opt_flags & MIR_STORE_NON_NULL_VALUE) != 0) {
341 UnconditionallyMarkGCCard(tgt_addr_reg);
342 } else {
343 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, nullptr);
344 UnconditionallyMarkGCCard(tgt_addr_reg);
345 LIR* target = NewLIR0(kPseudoTargetLabel);
346 branch_over->target = target;
347 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000348}
349
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700351void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 LOG(INFO) << "Dumping LIR insns for "
353 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
354 LIR* lir_insn;
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700355 int insns_size = mir_graph_->GetNumDalvikInsns();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700357 LOG(INFO) << "Regs (excluding ins) : " << mir_graph_->GetNumOfLocalCodeVRs();
358 LOG(INFO) << "Ins : " << mir_graph_->GetNumOfInVRs();
359 LOG(INFO) << "Outs : " << mir_graph_->GetNumOfOutVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 LOG(INFO) << "CoreSpills : " << num_core_spills_;
361 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800362 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 LOG(INFO) << "Frame size : " << frame_size_;
364 LOG(INFO) << "code size is " << total_size_ <<
365 " bytes, Dalvik size is " << insns_size * 2;
366 LOG(INFO) << "expansion factor: "
367 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
368 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700369 UpdateLIROffsets();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700370 for (lir_insn = first_lir_insn_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 DumpLIRInsn(lir_insn, 0);
372 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700373 for (lir_insn = literal_list_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
375 lir_insn->operands[0]);
376 }
377
378 const DexFile::MethodId& method_id =
379 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700380 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
381 const char* name = cu_->dex_file->GetMethodName(method_id);
382 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383
384 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000385 if (!encoded_mapping_table_.empty()) {
386 MappingTable table(&encoded_mapping_table_[0]);
387 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
388 table.PcToDexSize(), table.PcToDexBegin());
389 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
390 table.DexToPcSize(), table.DexToPcBegin());
391 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392}
393
394/*
395 * Search the existing constants in the literal pool for an exact or close match
396 * within specified delta (greater or equal to 0).
397 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700398LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 while (data_target) {
400 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
401 return data_target;
402 data_target = data_target->next;
403 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700404 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405}
406
407/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700408LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 bool lo_match = false;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700410 LIR* lo_target = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 while (data_target) {
412 if (lo_match && (data_target->operands[0] == val_hi)) {
413 // Record high word in case we need to expand this later.
414 lo_target->operands[1] = val_hi;
415 return lo_target;
416 }
417 lo_match = false;
418 if (data_target->operands[0] == val_lo) {
419 lo_match = true;
420 lo_target = data_target;
421 }
422 data_target = data_target->next;
423 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700424 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425}
426
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100427/* Search the existing constants in the literal pool for an exact method match */
428LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
429 while (data_target) {
430 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000431 UnwrapPointer<DexFile>(data_target->operands[1]) == method.dex_file) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100432 return data_target;
433 }
434 data_target = data_target->next;
435 }
436 return nullptr;
437}
438
Fred Shihe7f82e22014-08-06 10:46:37 -0700439/* Search the existing constants in the literal pool for an exact class match */
440LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
441 while (data_target) {
442 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000443 UnwrapPointer<DexFile>(data_target->operands[1]) == &dex_file) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700444 return data_target;
445 }
446 data_target = data_target->next;
447 }
448 return nullptr;
449}
450
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451/*
452 * The following are building blocks to insert constants into the pool or
453 * instruction streams.
454 */
455
456/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700457LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 /* Add the constant to the literal pool */
459 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000460 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 new_value->operands[0] = value;
462 new_value->next = *constant_list_p;
463 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700464 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 return new_value;
466 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700467 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468}
469
470/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700471LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 AddWordData(constant_list_p, val_hi);
473 return AddWordData(constant_list_p, val_lo);
474}
475
Matteo Franchin27cc0932014-09-08 18:29:24 +0100476/**
477 * @brief Push a compressed reference which needs patching at link/patchoat-time.
478 * @details This needs to be kept consistent with the code which actually does the patching in
479 * oat_writer.cc and in the patchoat tool.
480 */
Vladimir Marko80b96d12015-02-19 15:50:28 +0000481static void PushUnpatchedReference(CodeBuffer* buf) {
Matteo Franchin27cc0932014-09-08 18:29:24 +0100482 // Note that we can safely initialize the patches to zero. The code deduplication mechanism takes
483 // the patches into account when determining whether two pieces of codes are functionally
484 // equivalent.
485 Push32(buf, UINT32_C(0));
buzbee0d829482013-10-11 15:24:55 -0700486}
487
Vladimir Marko80b96d12015-02-19 15:50:28 +0000488static void AlignBuffer(CodeBuffer* buf, size_t offset) {
489 DCHECK_LE(buf->size(), offset);
490 buf->insert(buf->end(), offset - buf->size(), 0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491}
492
493/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700494void Mir2Lir::InstallLiteralPools() {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000495 AlignBuffer(&code_buffer_, data_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 LIR* data_lir = literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700497 while (data_lir != nullptr) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000498 Push32(&code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 data_lir = NEXT_LIR(data_lir);
500 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100501 // TODO: patches_.reserve() as needed.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 // Push code and method literals, record offsets for the compiler to patch.
503 data_lir = code_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700504 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700505 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000506 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100507 patches_.push_back(LinkerPatch::CodePatch(code_buffer_.size(),
508 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000509 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 data_lir = NEXT_LIR(data_lir);
511 }
512 data_lir = method_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700513 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700514 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000515 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100516 patches_.push_back(LinkerPatch::MethodPatch(code_buffer_.size(),
517 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000518 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 data_lir = NEXT_LIR(data_lir);
520 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800521 // Push class literals.
522 data_lir = class_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700523 while (data_lir != nullptr) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100524 uint32_t target_type_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000525 const DexFile* class_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100526 patches_.push_back(LinkerPatch::TypePatch(code_buffer_.size(),
527 class_dex_file, target_type_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000528 PushUnpatchedReference(&code_buffer_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800529 data_lir = NEXT_LIR(data_lir);
530 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531}
532
533/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700534void Mir2Lir::InstallSwitchTables() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100535 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000536 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 /*
538 * For Arm, our reference point is the address of the bx
539 * instruction that does the launch, so we have to subtract
540 * the auto pc-advance. For other targets the reference point
541 * is a label, so we can use the offset as-is.
542 */
543 int bx_offset = INVALID_OFFSET;
544 switch (cu_->instruction_set) {
545 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700546 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 bx_offset = tab_rec->anchor->offset + 4;
548 break;
Mark Mendell27dee8b2014-12-01 19:06:12 -0500549 case kX86_64:
550 // RIP relative to switch table.
551 bx_offset = tab_rec->offset;
552 break;
Vladimir Marko1961b602015-04-08 20:51:48 +0100553 case kX86:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100554 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100556 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 bx_offset = tab_rec->anchor->offset;
558 break;
559 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
560 }
561 if (cu_->verbose) {
562 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
563 }
564 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800565 DCHECK(tab_rec->switch_mir != nullptr);
566 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
567 DCHECK(bb != nullptr);
568 int elems = 0;
569 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
570 int key = successor_block_info->key;
571 int target = successor_block_info->block;
572 LIR* boundary_lir = InsertCaseLabel(target, key);
573 DCHECK(boundary_lir != nullptr);
574 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000575 Push32(&code_buffer_, key);
576 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 if (cu_->verbose) {
578 LOG(INFO) << " Case[" << elems << "] key: 0x"
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800579 << std::hex << key << ", disp: 0x"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 << std::hex << disp;
581 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800582 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800584 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 } else {
586 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
587 static_cast<int>(Instruction::kPackedSwitchSignature));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800588 DCHECK(tab_rec->switch_mir != nullptr);
589 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
590 DCHECK(bb != nullptr);
591 int elems = 0;
592 int low_key = s4FromSwitchData(&tab_rec->table[2]);
593 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
594 int key = successor_block_info->key;
595 DCHECK_EQ(elems + low_key, key);
596 int target = successor_block_info->block;
597 LIR* boundary_lir = InsertCaseLabel(target, key);
598 DCHECK(boundary_lir != nullptr);
599 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000600 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 if (cu_->verbose) {
602 LOG(INFO) << " Case[" << elems << "] disp: 0x"
603 << std::hex << disp;
604 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800605 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800607 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 }
609 }
610}
611
612/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700613void Mir2Lir::InstallFillArrayData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100614 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000615 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700617 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
618 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 }
620 }
621}
622
buzbee0d829482013-10-11 15:24:55 -0700623static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700624 for (; lir != nullptr; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 lir->offset = offset;
626 offset += 4;
627 }
628 return offset;
629}
630
Ian Rogersff093b32014-04-30 19:04:27 -0700631static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
632 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700633 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700634 offset = RoundUp(offset, element_size);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700635 for (; lir != nullptr; lir = lir->next) {
buzbee0d829482013-10-11 15:24:55 -0700636 lir->offset = offset;
637 offset += element_size;
638 }
639 return offset;
640}
641
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700643bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000644 MappingTable table(&encoded_mapping_table_[0]);
645 std::vector<uint32_t> dex_pcs;
646 dex_pcs.reserve(table.DexToPcSize());
647 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
648 dex_pcs.push_back(it.DexPc());
649 }
650 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
651 std::sort(dex_pcs.begin(), dex_pcs.end());
652
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000654 auto it = dex_pcs.begin(), end = dex_pcs.end();
655 for (uint32_t dex_pc : mir_graph_->catches_) {
656 while (it != end && *it < dex_pc) {
657 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
658 ++it;
659 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000661 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
663 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000664 } else {
665 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 }
667 }
668 if (!success) {
669 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
670 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000671 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 }
673 return success;
674}
675
676
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700677void Mir2Lir::CreateMappingTables() {
David Srbecky8363c772015-05-28 16:12:43 +0100678 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetGenerateDebugInfo();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700679
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000680 uint32_t pc2dex_data_size = 0u;
681 uint32_t pc2dex_entries = 0u;
682 uint32_t pc2dex_offset = 0u;
683 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700684 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000685 uint32_t dex2pc_data_size = 0u;
686 uint32_t dex2pc_entries = 0u;
687 uint32_t dex2pc_offset = 0u;
688 uint32_t dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700689 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700690 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000692 pc2dex_entries += 1;
693 DCHECK(pc2dex_offset <= tgt_lir->offset);
694 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
695 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
696 static_cast<int32_t>(pc2dex_dalvik_offset));
697 pc2dex_offset = tgt_lir->offset;
698 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 }
700 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000701 dex2pc_entries += 1;
702 DCHECK(dex2pc_offset <= tgt_lir->offset);
703 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
704 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
705 static_cast<int32_t>(dex2pc_dalvik_offset));
706 dex2pc_offset = tgt_lir->offset;
707 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 }
709 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000710
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700711 if (generate_src_map) {
712 src_mapping_table_.reserve(pc2dex_src_entries);
713 }
714
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000715 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
716 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
717 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000718 encoded_mapping_table_.resize(data_size);
719 uint8_t* write_pos = &encoded_mapping_table_[0];
720 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
721 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
722 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
723 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000724
David Srbecky6f715892015-03-30 14:21:42 +0100725 bool is_in_prologue_or_epilogue = false;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000726 pc2dex_offset = 0u;
727 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000728 dex2pc_offset = 0u;
729 dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700730 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
David Srbecky6f715892015-03-30 14:21:42 +0100731 if (generate_src_map && !tgt_lir->flags.is_nop && tgt_lir->opcode >= 0) {
732 if (!is_in_prologue_or_epilogue) {
733 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
734 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
735 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700736 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000737 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
738 DCHECK(pc2dex_offset <= tgt_lir->offset);
739 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
740 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
741 static_cast<int32_t>(pc2dex_dalvik_offset));
742 pc2dex_offset = tgt_lir->offset;
743 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
744 }
745 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
746 DCHECK(dex2pc_offset <= tgt_lir->offset);
747 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
748 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
749 static_cast<int32_t>(dex2pc_dalvik_offset));
750 dex2pc_offset = tgt_lir->offset;
751 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
752 }
David Srbecky6f715892015-03-30 14:21:42 +0100753 if (tgt_lir->opcode == kPseudoPrologueBegin || tgt_lir->opcode == kPseudoEpilogueBegin) {
754 is_in_prologue_or_epilogue = true;
755 }
756 if (tgt_lir->opcode == kPseudoPrologueEnd || tgt_lir->opcode == kPseudoEpilogueEnd) {
757 is_in_prologue_or_epilogue = false;
758 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000759 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000760 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
761 hdr_data_size + pc2dex_data_size);
762 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000763
Ian Rogers96faf5b2013-08-09 22:05:32 -0700764 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000765 CHECK(VerifyCatchEntries());
766
Ian Rogers96faf5b2013-08-09 22:05:32 -0700767 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000768 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700769 CHECK_EQ(table.TotalSize(), total_entries);
770 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000771 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000772 auto it2 = table.DexToPcBegin();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700773 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000774 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
775 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
776 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
777 ++it;
778 }
779 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
780 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
781 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
782 ++it2;
783 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700784 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000785 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000786 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700787 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788}
789
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko767c7522015-03-20 12:47:30 +0000791 if (UNLIKELY((cu_->disable_opt & (1u << kPromoteRegs)) != 0u)) {
792 // If we're not promoting to physical registers, it's safe to use the verifier's notion of
793 // references. (We disable register promotion when type inference finds a type conflict and
794 // in that the case we defer to the verifier to avoid using the compiler's conflicting info.)
795 CreateNativeGcMapWithoutRegisterPromotion();
796 return;
797 }
798
799 ArenaBitVector* references = new (arena_) ArenaBitVector(arena_, mir_graph_->GetNumSSARegs(),
800 false);
801
802 // Calculate max native offset and max reference vreg.
803 MIR* prev_mir = nullptr;
804 int max_ref_vreg = -1;
805 CodeOffset max_native_offset = 0u;
806 for (const auto& entry : safepoints_) {
807 uint32_t native_offset = entry.first->offset;
808 max_native_offset = std::max(max_native_offset, native_offset);
809 MIR* mir = entry.second;
810 UpdateReferenceVRegs(mir, prev_mir, references);
811 max_ref_vreg = std::max(max_ref_vreg, references->GetHighestBitSet());
812 prev_mir = mir;
813 }
814
Vladimir Marko6e071832015-03-25 11:13:39 +0000815#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
816 static constexpr bool kLittleEndian = true;
817#else
818 static constexpr bool kLittleEndian = false;
819#endif
820
Vladimir Marko767c7522015-03-20 12:47:30 +0000821 // Build the GC map.
822 uint32_t reg_width = static_cast<uint32_t>((max_ref_vreg + 8) / 8);
823 GcMapBuilder native_gc_map_builder(&native_gc_map_,
824 safepoints_.size(),
825 max_native_offset, reg_width);
Vladimir Marko6e071832015-03-25 11:13:39 +0000826 if (kLittleEndian) {
827 for (const auto& entry : safepoints_) {
828 uint32_t native_offset = entry.first->offset;
829 MIR* mir = entry.second;
830 UpdateReferenceVRegs(mir, prev_mir, references);
831 // For little-endian, the bytes comprising the bit vector's raw storage are what we need.
832 native_gc_map_builder.AddEntry(native_offset,
833 reinterpret_cast<const uint8_t*>(references->GetRawStorage()));
834 prev_mir = mir;
Vladimir Marko767c7522015-03-20 12:47:30 +0000835 }
Vladimir Marko6e071832015-03-25 11:13:39 +0000836 } else {
837 ArenaVector<uint8_t> references_buffer(arena_->Adapter());
838 references_buffer.resize(reg_width);
839 for (const auto& entry : safepoints_) {
840 uint32_t native_offset = entry.first->offset;
841 MIR* mir = entry.second;
842 UpdateReferenceVRegs(mir, prev_mir, references);
843 // Big-endian or unknown endianness, manually translate the bit vector data.
844 const auto* raw_storage = references->GetRawStorage();
845 for (size_t i = 0; i != reg_width; ++i) {
846 references_buffer[i] = static_cast<uint8_t>(
847 raw_storage[i / sizeof(raw_storage[0])] >> (8u * (i % sizeof(raw_storage[0]))));
848 }
Vladimir Markoec7802a2015-10-01 20:57:57 +0100849 native_gc_map_builder.AddEntry(native_offset, references_buffer.data());
Vladimir Marko6e071832015-03-25 11:13:39 +0000850 prev_mir = mir;
851 }
Vladimir Marko767c7522015-03-20 12:47:30 +0000852 }
853}
854
855void Mir2Lir::CreateNativeGcMapWithoutRegisterPromotion() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000856 DCHECK(!encoded_mapping_table_.empty());
857 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000859 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
860 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 if (native_offset > max_native_offset) {
862 max_native_offset = native_offset;
863 }
864 }
865 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000866 const std::vector<uint8_t>& gc_map_raw =
867 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
868 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
869 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000871 GcMapBuilder native_gc_map_builder(&native_gc_map_,
872 mapping_table.PcToDexSize(),
873 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874
Vladimir Marko06606b92013-12-02 15:31:08 +0000875 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
876 uint32_t native_offset = it.NativePcOffset();
877 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700879 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
Dave Allisonf9439142014-03-27 15:10:22 -0700880 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 native_gc_map_builder.AddEntry(native_offset, references);
882 }
Mathieu Chartierab972ef2014-12-03 17:38:22 -0800883
884 // Maybe not necessary, but this could help prevent errors where we access the verified method
885 // after it has been deleted.
886 mir_graph_->GetCurrentDexCompilationUnit()->ClearVerifiedMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887}
888
889/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700890int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100892 constexpr unsigned int ptr_size = sizeof(uint32_t);
Andreas Gampe785d2f22014-11-03 22:57:30 -0800893 static_assert(ptr_size >= sizeof(mirror::HeapReference<mirror::Object>),
894 "Pointer size cannot hold a heap reference");
Ian Rogersff093b32014-04-30 19:04:27 -0700895 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
896 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
897 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 return offset;
899}
900
buzbee0d829482013-10-11 15:24:55 -0700901int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100902 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 tab_rec->offset = offset;
904 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
905 offset += tab_rec->table[1] * (sizeof(int) * 2);
906 } else {
907 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
908 static_cast<int>(Instruction::kPackedSwitchSignature));
909 offset += tab_rec->table[1] * sizeof(int);
910 }
911 }
912 return offset;
913}
914
buzbee0d829482013-10-11 15:24:55 -0700915int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100916 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700917 tab_rec->offset = offset;
918 offset += tab_rec->size;
919 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700920 offset = RoundUp(offset, 4);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100921 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 return offset;
923}
924
Brian Carlstrom7940e442013-07-12 13:46:57 -0700925/*
926 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700927 * offset vaddr if pretty-printing, otherise use the standard block
928 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700929 * branch table during the assembly phase. All resource flags
930 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931 */
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800932LIR* Mir2Lir::InsertCaseLabel(uint32_t bbid, int keyVal) {
933 LIR* boundary_lir = &block_label_list_[bbid];
buzbeeb48819d2013-09-14 16:15:25 -0700934 LIR* res = boundary_lir;
935 if (cu_->verbose) {
936 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000937 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800938 BasicBlock* bb = mir_graph_->GetBasicBlock(bbid);
939 DCHECK(bb != nullptr);
940 new_label->dalvik_offset = bb->start_offset;
buzbeeb48819d2013-09-14 16:15:25 -0700941 new_label->opcode = kPseudoCaseLabel;
942 new_label->operands[0] = keyVal;
943 new_label->flags.fixup = kFixupLabel;
944 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100945 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700946 InsertLIRAfter(boundary_lir, new_label);
buzbeeb48819d2013-09-14 16:15:25 -0700947 }
948 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949}
950
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700951void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700952 /*
953 * Sparse switch data format:
954 * ushort ident = 0x0200 magic value
955 * ushort size number of entries in the table; > 0
956 * int keys[size] keys, sorted low-to-high; 32-bit aligned
957 * int targets[size] branch targets, relative to switch opcode
958 *
959 * Total size is (2+size*4) 16-bit code units.
960 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 uint16_t ident = table[0];
962 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700963 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
964 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
966 << ", entries: " << std::dec << entries;
967 for (int i = 0; i < entries; i++) {
968 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
969 }
970}
971
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700972void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973 /*
974 * Packed switch data format:
975 * ushort ident = 0x0100 magic value
976 * ushort size number of entries in the table
977 * int first_key first (and lowest) switch case value
978 * int targets[size] branch targets, relative to switch opcode
979 *
980 * Total size is (4+size*2) 16-bit code units.
981 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700983 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 int entries = table[1];
985 int low_key = s4FromSwitchData(&table[2]);
986 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
987 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
988 for (int i = 0; i < entries; i++) {
989 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
990 << targets[i];
991 }
992}
993
buzbee252254b2013-09-08 16:20:53 -0700994/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700995void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700996 UNUSED(offset);
buzbee0d829482013-10-11 15:24:55 -0700997 // NOTE: only used for debug listings.
998 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999}
1000
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001// Convert relation of src1/src2 to src2/src1
1002ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
1003 ConditionCode res;
1004 switch (before) {
1005 case kCondEq: res = kCondEq; break;
1006 case kCondNe: res = kCondNe; break;
1007 case kCondLt: res = kCondGt; break;
1008 case kCondGt: res = kCondLt; break;
1009 case kCondLe: res = kCondGe; break;
1010 case kCondGe: res = kCondLe; break;
1011 default:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001013 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 }
1015 return res;
1016}
1017
Vladimir Markoa1a70742014-03-03 10:28:05 +00001018ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
1019 ConditionCode res;
1020 switch (before) {
1021 case kCondEq: res = kCondNe; break;
1022 case kCondNe: res = kCondEq; break;
1023 case kCondLt: res = kCondGe; break;
1024 case kCondGt: res = kCondLe; break;
1025 case kCondLe: res = kCondGt; break;
1026 case kCondGe: res = kCondLt; break;
1027 default:
Vladimir Markoa1a70742014-03-03 10:28:05 +00001028 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001029 UNREACHABLE();
Vladimir Markoa1a70742014-03-03 10:28:05 +00001030 }
1031 return res;
1032}
1033
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034// TODO: move to mir_to_lir.cc
1035Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Andreas Gampe9c462082015-01-27 14:31:40 -08001036 : literal_list_(nullptr),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001037 method_literal_list_(nullptr),
1038 class_literal_list_(nullptr),
1039 code_literal_list_(nullptr),
1040 first_fixup_(nullptr),
Andreas Gampe9c462082015-01-27 14:31:40 -08001041 arena_(arena),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 cu_(cu),
1043 mir_graph_(mir_graph),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001044 switch_tables_(arena->Adapter(kArenaAllocSwitchTable)),
1045 fill_array_data_(arena->Adapter(kArenaAllocFillArrayData)),
1046 tempreg_info_(arena->Adapter()),
1047 reginfo_map_(arena->Adapter()),
1048 pointer_storage_(arena->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001049 data_offset_(0),
1050 total_size_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001051 block_label_list_(nullptr),
1052 promotion_map_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 current_dalvik_offset_(0),
Vladimir Marko767c7522015-03-20 12:47:30 +00001054 current_mir_(nullptr),
buzbeeb48819d2013-09-14 16:15:25 -07001055 estimated_native_code_size_(0),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001056 reg_pool_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 live_sreg_(0),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001058 code_buffer_(mir_graph->GetArena()->Adapter()),
1059 encoded_mapping_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko8081d2b2014-07-31 15:33:43 +01001060 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1061 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001062 native_gc_map_(mir_graph->GetArena()->Adapter()),
Vladimir Markof4da6752014-08-01 19:04:18 +01001063 patches_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001064 num_core_spills_(0),
1065 num_fp_spills_(0),
1066 frame_size_(0),
1067 core_spill_mask_(0),
1068 fp_spill_mask_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001069 first_lir_insn_(nullptr),
1070 last_lir_insn_(nullptr),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001071 slow_paths_(arena->Adapter(kArenaAllocSlowPaths)),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001072 mem_ref_type_(ResourceMask::kHeapRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001073 mask_cache_(arena),
Vladimir Marko767c7522015-03-20 12:47:30 +00001074 safepoints_(arena->Adapter()),
Vladimir Marko20f85592015-03-19 10:07:02 +00001075 dex_cache_arrays_layout_(cu->compiler_driver->GetDexCacheArraysLayout(cu->dex_file)),
Vladimir Markocc234812015-04-07 09:36:09 +01001076 pc_rel_temp_(nullptr),
1077 dex_cache_arrays_min_offset_(std::numeric_limits<uint32_t>::max()),
David Srbecky1109fb32015-04-07 20:21:06 +01001078 cfi_(&last_lir_insn_,
David Srbecky8363c772015-05-28 16:12:43 +01001079 cu->compiler_driver->GetCompilerOptions().GetGenerateDebugInfo(),
David Srbecky1109fb32015-04-07 20:21:06 +01001080 arena),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001081 in_to_reg_storage_mapping_(arena) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001082 switch_tables_.reserve(4);
1083 fill_array_data_.reserve(4);
1084 tempreg_info_.reserve(20);
1085 reginfo_map_.reserve(RegStorage::kMaxRegs);
1086 pointer_storage_.reserve(128);
1087 slow_paths_.reserve(32);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001088 // Reserve pointer id 0 for null.
Vladimir Markof6737f72015-03-23 17:05:14 +00001089 size_t null_idx = WrapPointer<void>(nullptr);
buzbee0d829482013-10-11 15:24:55 -07001090 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091}
1092
1093void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001094 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1096
1097 /* Allocate Registers using simple local allocation scheme */
1098 SimpleRegAlloc();
1099
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001100 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001101 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001102 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001103 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001105 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1106 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 MethodMIR2LIR();
1108 }
1109
1110 /* Method is not empty */
1111 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 /* Convert LIR into machine code. */
1113 AssembleLIR();
1114
buzbeeb01bf152014-05-13 15:59:07 -07001115 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 CodegenDump();
1117 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119}
1120
1121CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001122 // Combine vmap tables - core regs, then fp regs - into vmap_table.
Vladimir Markof9f64412015-09-02 14:05:49 +01001123 Leb128EncodingVector<> vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001125 // Prefix the encoded data with its size.
1126 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1127 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1128 vmap_encoder.PushBackUnsigned(size);
1129 // Core regs may have been inserted out of order - sort first.
1130 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1131 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1132 // Copy, stripping out the phys register sort key.
1133 vmap_encoder.PushBackUnsigned(
Vladimir Marko97a87ec2015-09-29 11:25:48 +01001134 ~(~0u << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001135 }
1136 // Push a marker to take place of lr.
1137 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001138 if (cu_->instruction_set == kThumb2) {
1139 // fp regs already sorted.
1140 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1141 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1142 }
1143 } else {
1144 // For other platforms regs may have been inserted out of order - sort first.
1145 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1146 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1147 // Copy, stripping out the phys register sort key.
1148 vmap_encoder.PushBackUnsigned(
Vladimir Marko97a87ec2015-09-29 11:25:48 +01001149 ~(~0u << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
Serguei Katkovc3801912014-07-08 17:21:53 +07001150 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001151 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001153 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1154 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001155 DCHECK_EQ(core_vmap_table_.size(), 0u);
1156 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1157 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001159
Vladimir Marko58155012015-08-19 12:49:41 +00001160 // Sort patches by literal offset. Required for .oat_patches encoding.
Vladimir Markof4da6752014-08-01 19:04:18 +01001161 std::sort(patches_.begin(), patches_.end(), [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
1162 return lhs.LiteralOffset() < rhs.LiteralOffset();
1163 });
1164
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001165 return CompiledMethod::SwapAllocCompiledMethod(
1166 cu_->compiler_driver, cu_->instruction_set,
1167 ArrayRef<const uint8_t>(code_buffer_),
1168 frame_size_, core_spill_mask_, fp_spill_mask_,
1169 &src_mapping_table_,
1170 ArrayRef<const uint8_t>(encoded_mapping_table_),
1171 ArrayRef<const uint8_t>(vmap_encoder.GetData()),
1172 ArrayRef<const uint8_t>(native_gc_map_),
David Srbecky1109fb32015-04-07 20:21:06 +01001173 ArrayRef<const uint8_t>(*cfi_.Patch(code_buffer_.size())),
Vladimir Markob207e142015-04-02 21:25:21 +01001174 ArrayRef<const LinkerPatch>(patches_));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175}
1176
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001177size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1178 // Chose a reasonably small value in order to contain stack growth.
1179 // Backends that are smarter about spill region can return larger values.
1180 const size_t max_compiler_temps = 10;
1181 return max_compiler_temps;
1182}
1183
1184size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1185 // By default assume that the Mir2Lir will need one slot for each temporary.
1186 // If the backend can better determine temps that have non-overlapping ranges and
1187 // temps that do not need spilled, it can actually provide a small region.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001188 mir_graph_->CommitCompilerTemps();
1189 return mir_graph_->GetNumBytesForSpecialTemps() + mir_graph_->GetMaximumBytesForNonSpecialTemps();
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001190}
1191
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192int Mir2Lir::ComputeFrameSize() {
1193 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001194 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1195 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1196 + sizeof(uint32_t) // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001197 + mir_graph_->GetNumOfLocalCodeVRs() * sizeof(uint32_t)
1198 + mir_graph_->GetNumOfOutVRs() * sizeof(uint32_t)
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001199 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001201 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202}
1203
1204/*
1205 * Append an LIR instruction to the LIR list maintained by a compilation
1206 * unit
1207 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001208void Mir2Lir::AppendLIR(LIR* lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001209 if (first_lir_insn_ == nullptr) {
1210 DCHECK(last_lir_insn_ == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211 last_lir_insn_ = first_lir_insn_ = lir;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001212 lir->prev = lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 } else {
1214 last_lir_insn_->next = lir;
1215 lir->prev = last_lir_insn_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001216 lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 last_lir_insn_ = lir;
1218 }
1219}
1220
1221/*
1222 * Insert an LIR instruction before the current instruction, which cannot be the
1223 * first instruction.
1224 *
1225 * prev_lir <-> new_lir <-> current_lir
1226 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001227void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001228 DCHECK(current_lir->prev != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 LIR *prev_lir = current_lir->prev;
1230
1231 prev_lir->next = new_lir;
1232 new_lir->prev = prev_lir;
1233 new_lir->next = current_lir;
1234 current_lir->prev = new_lir;
1235}
1236
1237/*
1238 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001239 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 *
1241 * current_lir -> new_lir -> old_next
1242 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001243void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 new_lir->prev = current_lir;
1245 new_lir->next = current_lir->next;
1246 current_lir->next = new_lir;
1247 new_lir->next->prev = new_lir;
1248}
1249
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001250bool Mir2Lir::PartiallyIntersects(RegLocation rl_src, RegLocation rl_dest) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001251 DCHECK(rl_src.wide);
1252 DCHECK(rl_dest.wide);
1253 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1254}
1255
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001256bool Mir2Lir::Intersects(RegLocation rl_src, RegLocation rl_dest) {
1257 DCHECK(rl_src.wide);
1258 DCHECK(rl_dest.wide);
1259 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) <= 1);
1260}
1261
buzbee2700f7e2014-03-07 09:46:20 -08001262LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001263 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001264 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001265 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1266 if (compare != nullptr) {
1267 *compare = inst;
1268 }
Mark Mendell766e9292014-01-27 07:55:47 -08001269 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1270 return branch;
1271}
1272
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001273void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001274 slow_paths_.push_back(slowpath);
Serguei Katkov589e0462014-09-05 18:37:22 +07001275 ResetDefTracking();
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001276}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001277
Jeff Hao49161ce2014-03-12 11:05:25 -07001278void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1279 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001280 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001281 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001282 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001283 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001284 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1285 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1286 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001287 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001288 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001289 // Loads a code pointer. Code from oat file can be mapped anywhere.
Vladimir Markof6737f72015-03-23 17:05:14 +00001290 OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001291 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001292 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001293}
1294
Jeff Hao49161ce2014-03-12 11:05:25 -07001295void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1296 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001297 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001298 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001299 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001300 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001301 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1302 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1303 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001304 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001305 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001306 // Loads an ArtMethod pointer, which is not a reference.
1307 OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001308 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001309 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001310}
1311
Fred Shihe7f82e22014-08-06 10:46:37 -07001312void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1313 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001314 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001315 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001316 if (data_target == nullptr) {
1317 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001318 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001319 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001320 // Loads a Class pointer, which is a reference as it lives in the heap.
Vladimir Markof6737f72015-03-23 17:05:14 +00001321 OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001322}
1323
Vladimir Marko20f85592015-03-19 10:07:02 +00001324bool Mir2Lir::CanUseOpPcRelDexCacheArrayLoad() const {
1325 return false;
1326}
1327
1328void Mir2Lir::OpPcRelDexCacheArrayLoad(const DexFile* dex_file ATTRIBUTE_UNUSED,
1329 int offset ATTRIBUTE_UNUSED,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001330 RegStorage r_dest ATTRIBUTE_UNUSED,
1331 bool wide ATTRIBUTE_UNUSED) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001332 LOG(FATAL) << "No generic implementation.";
1333 UNREACHABLE();
1334}
1335
buzbee2700f7e2014-03-07 09:46:20 -08001336RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001337 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001338 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001339 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001340 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1341 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1342 info_lo->SetIsWide(false);
1343 info_hi->SetIsWide(false);
1344 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001345 } else {
buzbee85089dd2014-05-25 15:10:52 -07001346 RegisterInfo* info = GetRegInfo(loc.reg);
1347 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1348 DCHECK(info_new != nullptr);
1349 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1350 info->MarkDead();
1351 info_new->MarkLive(loc.s_reg_low);
1352 }
1353 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001354 }
buzbee85089dd2014-05-25 15:10:52 -07001355 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001356 }
buzbee85089dd2014-05-25 15:10:52 -07001357 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001358 return loc;
1359}
1360
Mark Mendelld65c51a2014-04-29 16:55:20 -04001361void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001362 UNUSED(bb, mir);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001363 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001364 UNREACHABLE();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001365}
1366
Vladimir Marko767c7522015-03-20 12:47:30 +00001367void Mir2Lir::InitReferenceVRegs(BasicBlock* bb, BitVector* references) {
1368 // Mark the references coming from the first predecessor.
1369 DCHECK(bb != nullptr);
1370 DCHECK(bb->block_type == kEntryBlock || !bb->predecessors.empty());
1371 BasicBlock* first_bb =
1372 (bb->block_type == kEntryBlock) ? bb : mir_graph_->GetBasicBlock(bb->predecessors[0]);
1373 DCHECK(first_bb != nullptr);
1374 DCHECK(first_bb->data_flow_info != nullptr);
1375 DCHECK(first_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1376 const int32_t* first_vreg_to_ssa_map = first_bb->data_flow_info->vreg_to_ssa_map_exit;
1377 references->ClearAllBits();
Pavel Vyssotski41f9cc22015-06-16 17:57:37 +06001378 for (uint32_t vreg = 0,
1379 num_vregs = mir_graph_->GetNumOfCodeVRs() + mir_graph_->GetNumUsedCompilerTemps();
1380 vreg != num_vregs; ++vreg) {
Vladimir Marko767c7522015-03-20 12:47:30 +00001381 int32_t sreg = first_vreg_to_ssa_map[vreg];
1382 if (sreg != INVALID_SREG && mir_graph_->reg_location_[sreg].ref &&
1383 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[sreg])) {
1384 references->SetBit(vreg);
1385 }
1386 }
1387 // Unmark the references that are merging with a different value.
1388 for (size_t i = 1u, num_pred = bb->predecessors.size(); i < num_pred; ++i) {
1389 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(bb->predecessors[i]);
1390 DCHECK(pred_bb != nullptr);
1391 DCHECK(pred_bb->data_flow_info != nullptr);
1392 DCHECK(pred_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1393 const int32_t* pred_vreg_to_ssa_map = pred_bb->data_flow_info->vreg_to_ssa_map_exit;
1394 for (uint32_t vreg : references->Indexes()) {
1395 if (first_vreg_to_ssa_map[vreg] != pred_vreg_to_ssa_map[vreg]) {
1396 // NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
1397 // so clearing the bit has no effect on the iterator.
1398 references->ClearBit(vreg);
1399 }
1400 }
1401 }
Vladimir Marko767c7522015-03-20 12:47:30 +00001402}
1403
1404bool Mir2Lir::UpdateReferenceVRegsLocal(MIR* mir, MIR* prev_mir, BitVector* references) {
1405 DCHECK(mir == nullptr || mir->bb == prev_mir->bb);
1406 DCHECK(prev_mir != nullptr);
1407 while (prev_mir != nullptr) {
1408 if (prev_mir == mir) {
1409 return true;
1410 }
1411 const size_t num_defs = prev_mir->ssa_rep->num_defs;
1412 const int32_t* defs = prev_mir->ssa_rep->defs;
1413 if (num_defs == 1u && mir_graph_->reg_location_[defs[0]].ref &&
1414 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[defs[0]])) {
1415 references->SetBit(mir_graph_->SRegToVReg(defs[0]));
1416 } else {
1417 for (size_t i = 0u; i != num_defs; ++i) {
1418 references->ClearBit(mir_graph_->SRegToVReg(defs[i]));
1419 }
1420 }
1421 prev_mir = prev_mir->next;
1422 }
1423 return false;
1424}
1425
1426void Mir2Lir::UpdateReferenceVRegs(MIR* mir, MIR* prev_mir, BitVector* references) {
1427 if (mir == nullptr) {
1428 // Safepoint in entry sequence.
1429 InitReferenceVRegs(mir_graph_->GetEntryBlock(), references);
1430 return;
1431 }
1432 if (IsInstructionReturn(mir->dalvikInsn.opcode) ||
1433 mir->dalvikInsn.opcode == Instruction::RETURN_VOID_NO_BARRIER) {
1434 references->ClearAllBits();
1435 if (mir->dalvikInsn.opcode == Instruction::RETURN_OBJECT) {
1436 references->SetBit(mir_graph_->SRegToVReg(mir->ssa_rep->uses[0]));
1437 }
1438 return;
1439 }
1440 if (prev_mir != nullptr && mir->bb == prev_mir->bb &&
1441 UpdateReferenceVRegsLocal(mir, prev_mir, references)) {
1442 return;
1443 }
1444 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
1445 DCHECK(bb != nullptr);
1446 InitReferenceVRegs(bb, references);
1447 bool success = UpdateReferenceVRegsLocal(mir, bb->first_mir_insn, references);
1448 DCHECK(success) << "MIR @0x" << std::hex << mir->offset << " not in BB#" << std::dec << mir->bb;
1449}
1450
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001451} // namespace art