blob: 509d4487ad76703c6612f33812c2e013fb9c86b5 [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 Marko767c7522015-03-20 12:47:30 +000019#include "base/bit_vector-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020#include "dex/mir_graph.h"
21#include "driver/compiler_driver.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070022#include "driver/compiler_options.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080023#include "driver/dex_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "dex_file-inl.h"
25#include "gc_map.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000026#include "gc_map_builder.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070027#include "mapping_table.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000028#include "dex/quick/dex_file_method_inliner.h"
29#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000030#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000031#include "dex/verified_method.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000032#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#include "verifier/dex_gc_map.h"
34#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000035#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036
37namespace art {
38
Vladimir Marko06606b92013-12-02 15:31:08 +000039namespace {
40
41/* Dump a mapping table */
42template <typename It>
43void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
44 const Signature& signature, uint32_t size, It first) {
45 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080046 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000047 descriptor, name, signature.ToString().c_str(), size));
48 std::replace(line.begin(), line.end(), ';', '_');
49 LOG(INFO) << line;
50 for (uint32_t i = 0; i != size; ++i) {
51 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
52 ++first;
53 LOG(INFO) << line;
54 }
55 LOG(INFO) <<" };\n\n";
56 }
57}
58
59} // anonymous namespace
60
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070061bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 bool res = false;
63 if (rl_src.is_const) {
64 if (rl_src.wide) {
Andreas Gampede0b9962014-08-27 14:24:42 -070065 // For wide registers, check whether we're the high partner. In that case we need to switch
66 // to the lower one for the correct value.
67 if (rl_src.high_word) {
68 rl_src.high_word = false;
69 rl_src.s_reg_low--;
70 rl_src.orig_sreg--;
71 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070073 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070075 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 }
77 } else {
78 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070079 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070081 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 }
83 }
84 }
85 return res;
86}
87
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070088void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070089 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010090 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010092 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +000093 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
94 safepoints_.emplace_back(safepoint_pc, current_mir_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070095}
96
Andreas Gampe3c12c512014-06-24 18:46:29 +000097void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
98 DCHECK(!after->flags.use_def_invalid);
99 after->u.m.def_mask = &kEncodeAll;
100 // As NewLIR0 uses Append, we need to create the LIR by hand.
101 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
102 if (after->next == nullptr) {
103 DCHECK_EQ(after, last_lir_insn_);
104 AppendLIR(safepoint_pc);
105 } else {
106 InsertLIRAfter(after, safepoint_pc);
107 }
108 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +0000109 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
110 safepoints_.emplace_back(safepoint_pc, current_mir_);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000111}
112
buzbee252254b2013-09-08 16:20:53 -0700113/* Remove a LIR from the list. */
114void Mir2Lir::UnlinkLIR(LIR* lir) {
115 if (UNLIKELY(lir == first_lir_insn_)) {
116 first_lir_insn_ = lir->next;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117 if (lir->next != nullptr) {
118 lir->next->prev = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700119 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700120 DCHECK(lir->next == nullptr);
buzbee252254b2013-09-08 16:20:53 -0700121 DCHECK(lir == last_lir_insn_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700122 last_lir_insn_ = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700123 }
124 } else if (lir == last_lir_insn_) {
125 last_lir_insn_ = lir->prev;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700126 lir->prev->next = nullptr;
127 } else if ((lir->prev != nullptr) && (lir->next != nullptr)) {
buzbee252254b2013-09-08 16:20:53 -0700128 lir->prev->next = lir->next;
129 lir->next->prev = lir->prev;
130 }
131}
132
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700134void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700136 if (!cu_->verbose) {
137 UnlinkLIR(lir);
138 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139}
140
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700141void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700143 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100144 // TODO: Avoid the extra Arena allocation!
145 const ResourceMask** mask_ptr;
146 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700148 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700150 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100152 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100154 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 /* ..and then add back the one we need */
156 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100157 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100159 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100161 case ResourceMask::kDalvikReg:
162 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100164 case ResourceMask::kHeapRef:
165 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100167 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 /* Currently only loads can be marked as kMustNotAlias */
169 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100170 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 break;
172 default:
173 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
174 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100175 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176}
177
178/*
179 * Mark load/store instructions that access Dalvik registers through the stack.
180 */
181void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700182 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100183 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
184 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185
186 /*
187 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
188 * access.
189 */
buzbeeb48819d2013-09-14 16:15:25 -0700190 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191}
192
193/*
194 * Debugging macros
195 */
196#define DUMP_RESOURCE_MASK(X)
197
198/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700199void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 int offset = lir->offset;
201 int dest = lir->operands[0];
202 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
203
204 /* Handle pseudo-ops individually, and all regular insns as a group */
205 switch (lir->opcode) {
206 case kPseudoMethodEntry:
207 LOG(INFO) << "-------- method entry "
208 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
209 break;
210 case kPseudoMethodExit:
211 LOG(INFO) << "-------- Method_Exit";
212 break;
213 case kPseudoBarrier:
214 LOG(INFO) << "-------- BARRIER";
215 break;
216 case kPseudoEntryBlock:
217 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
218 break;
219 case kPseudoDalvikByteCodeBoundary:
220 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700221 // NOTE: only used for debug listings.
222 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 }
224 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000225 << lir->dalvik_offset << " @ "
Vladimir Markof6737f72015-03-23 17:05:14 +0000226 << UnwrapPointer<char>(lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 break;
228 case kPseudoExitBlock:
229 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
230 break;
231 case kPseudoPseudoAlign4:
232 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
233 << offset << "): .align4";
234 break;
235 case kPseudoEHBlockLabel:
236 LOG(INFO) << "Exception_Handling:";
237 break;
238 case kPseudoTargetLabel:
239 case kPseudoNormalBlockLabel:
240 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
241 break;
242 case kPseudoThrowTarget:
243 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
244 break;
245 case kPseudoIntrinsicRetry:
246 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
247 break;
248 case kPseudoSuspendTarget:
249 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
250 break;
251 case kPseudoSafepointPC:
252 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
253 break;
254 case kPseudoExportedPC:
255 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
256 break;
257 case kPseudoCaseLabel:
258 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
259 << std::hex << lir->operands[0] << "|" << std::dec <<
260 lir->operands[0];
261 break;
262 default:
263 if (lir->flags.is_nop && !dump_nop) {
264 break;
265 } else {
266 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
267 lir, base_addr));
268 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
269 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800270 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
271 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 op_name.c_str(), op_operands.c_str(),
273 lir->flags.is_nop ? "(nop)" : "");
274 }
275 break;
276 }
277
buzbeeb48819d2013-09-14 16:15:25 -0700278 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100279 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 }
buzbeeb48819d2013-09-14 16:15:25 -0700281 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100282 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 }
284}
285
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700286void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700287 uint32_t num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
288 for (uint32_t i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 PromotionMap v_reg_map = promotion_map_[i];
290 std::string buf;
291 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700292 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 }
294
295 std::string buf3;
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700296 if (i < mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 StringAppendF(&buf3, "%02d", i);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700298 } else if (i == mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 buf3 = "Method*";
300 } else {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700301 uint32_t diff = i - mir_graph_->GetNumOfCodeVRs();
302 StringAppendF(&buf3, "ct%d", diff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 }
304
305 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
306 v_reg_map.core_location == kLocPhysReg ?
307 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
308 v_reg_map.core_reg : SRegOffset(i),
309 buf.c_str());
310 }
311}
312
buzbee7a11ab02014-04-28 20:02:38 -0700313void Mir2Lir::UpdateLIROffsets() {
314 // Only used for code listings.
315 size_t offset = 0;
316 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
317 lir->offset = offset;
318 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
319 offset += GetInsnSize(lir);
320 } else if (lir->opcode == kPseudoPseudoAlign4) {
321 offset += (offset & 0x2);
322 }
323 }
324}
325
Vladimir Marko743b98c2014-11-24 19:45:41 +0000326void Mir2Lir::MarkGCCard(int opt_flags, RegStorage val_reg, RegStorage tgt_addr_reg) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000327 DCHECK(val_reg.Valid());
328 DCHECK_EQ(val_reg.Is64Bit(), cu_->target64);
Vladimir Marko743b98c2014-11-24 19:45:41 +0000329 if ((opt_flags & MIR_STORE_NON_NULL_VALUE) != 0) {
330 UnconditionallyMarkGCCard(tgt_addr_reg);
331 } else {
332 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, nullptr);
333 UnconditionallyMarkGCCard(tgt_addr_reg);
334 LIR* target = NewLIR0(kPseudoTargetLabel);
335 branch_over->target = target;
336 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000337}
338
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700340void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 LOG(INFO) << "Dumping LIR insns for "
342 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
343 LIR* lir_insn;
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700344 int insns_size = mir_graph_->GetNumDalvikInsns();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700346 LOG(INFO) << "Regs (excluding ins) : " << mir_graph_->GetNumOfLocalCodeVRs();
347 LOG(INFO) << "Ins : " << mir_graph_->GetNumOfInVRs();
348 LOG(INFO) << "Outs : " << mir_graph_->GetNumOfOutVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 LOG(INFO) << "CoreSpills : " << num_core_spills_;
350 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800351 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 LOG(INFO) << "Frame size : " << frame_size_;
353 LOG(INFO) << "code size is " << total_size_ <<
354 " bytes, Dalvik size is " << insns_size * 2;
355 LOG(INFO) << "expansion factor: "
356 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
357 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700358 UpdateLIROffsets();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700359 for (lir_insn = first_lir_insn_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 DumpLIRInsn(lir_insn, 0);
361 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700362 for (lir_insn = literal_list_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
364 lir_insn->operands[0]);
365 }
366
367 const DexFile::MethodId& method_id =
368 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700369 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
370 const char* name = cu_->dex_file->GetMethodName(method_id);
371 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372
373 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000374 if (!encoded_mapping_table_.empty()) {
375 MappingTable table(&encoded_mapping_table_[0]);
376 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
377 table.PcToDexSize(), table.PcToDexBegin());
378 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
379 table.DexToPcSize(), table.DexToPcBegin());
380 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381}
382
383/*
384 * Search the existing constants in the literal pool for an exact or close match
385 * within specified delta (greater or equal to 0).
386 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700387LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 while (data_target) {
389 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
390 return data_target;
391 data_target = data_target->next;
392 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700393 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394}
395
396/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700397LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 bool lo_match = false;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700399 LIR* lo_target = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 while (data_target) {
401 if (lo_match && (data_target->operands[0] == val_hi)) {
402 // Record high word in case we need to expand this later.
403 lo_target->operands[1] = val_hi;
404 return lo_target;
405 }
406 lo_match = false;
407 if (data_target->operands[0] == val_lo) {
408 lo_match = true;
409 lo_target = data_target;
410 }
411 data_target = data_target->next;
412 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700413 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414}
415
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100416/* Search the existing constants in the literal pool for an exact method match */
417LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
418 while (data_target) {
419 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000420 UnwrapPointer<DexFile>(data_target->operands[1]) == method.dex_file) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100421 return data_target;
422 }
423 data_target = data_target->next;
424 }
425 return nullptr;
426}
427
Fred Shihe7f82e22014-08-06 10:46:37 -0700428/* Search the existing constants in the literal pool for an exact class match */
429LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
430 while (data_target) {
431 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000432 UnwrapPointer<DexFile>(data_target->operands[1]) == &dex_file) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700433 return data_target;
434 }
435 data_target = data_target->next;
436 }
437 return nullptr;
438}
439
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440/*
441 * The following are building blocks to insert constants into the pool or
442 * instruction streams.
443 */
444
445/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700446LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 /* Add the constant to the literal pool */
448 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000449 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 new_value->operands[0] = value;
451 new_value->next = *constant_list_p;
452 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700453 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 return new_value;
455 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700456 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457}
458
459/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700460LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 AddWordData(constant_list_p, val_hi);
462 return AddWordData(constant_list_p, val_lo);
463}
464
Matteo Franchin27cc0932014-09-08 18:29:24 +0100465/**
466 * @brief Push a compressed reference which needs patching at link/patchoat-time.
467 * @details This needs to be kept consistent with the code which actually does the patching in
468 * oat_writer.cc and in the patchoat tool.
469 */
Vladimir Marko80b96d12015-02-19 15:50:28 +0000470static void PushUnpatchedReference(CodeBuffer* buf) {
Matteo Franchin27cc0932014-09-08 18:29:24 +0100471 // Note that we can safely initialize the patches to zero. The code deduplication mechanism takes
472 // the patches into account when determining whether two pieces of codes are functionally
473 // equivalent.
474 Push32(buf, UINT32_C(0));
buzbee0d829482013-10-11 15:24:55 -0700475}
476
Vladimir Marko80b96d12015-02-19 15:50:28 +0000477static void AlignBuffer(CodeBuffer* buf, size_t offset) {
478 DCHECK_LE(buf->size(), offset);
479 buf->insert(buf->end(), offset - buf->size(), 0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480}
481
482/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700483void Mir2Lir::InstallLiteralPools() {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000484 AlignBuffer(&code_buffer_, data_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 LIR* data_lir = literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700486 while (data_lir != nullptr) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000487 Push32(&code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 data_lir = NEXT_LIR(data_lir);
489 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100490 // TODO: patches_.reserve() as needed.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 // Push code and method literals, record offsets for the compiler to patch.
492 data_lir = code_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700493 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700494 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000495 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100496 patches_.push_back(LinkerPatch::CodePatch(code_buffer_.size(),
497 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000498 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 data_lir = NEXT_LIR(data_lir);
500 }
501 data_lir = method_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700502 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700503 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000504 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100505 patches_.push_back(LinkerPatch::MethodPatch(code_buffer_.size(),
506 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000507 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 data_lir = NEXT_LIR(data_lir);
509 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800510 // Push class literals.
511 data_lir = class_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700512 while (data_lir != nullptr) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100513 uint32_t target_type_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000514 const DexFile* class_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100515 patches_.push_back(LinkerPatch::TypePatch(code_buffer_.size(),
516 class_dex_file, target_type_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000517 PushUnpatchedReference(&code_buffer_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800518 data_lir = NEXT_LIR(data_lir);
519 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520}
521
522/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700523void Mir2Lir::InstallSwitchTables() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100524 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000525 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 /*
527 * For Arm, our reference point is the address of the bx
528 * instruction that does the launch, so we have to subtract
529 * the auto pc-advance. For other targets the reference point
530 * is a label, so we can use the offset as-is.
531 */
532 int bx_offset = INVALID_OFFSET;
533 switch (cu_->instruction_set) {
534 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700535 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 bx_offset = tab_rec->anchor->offset + 4;
537 break;
538 case kX86:
539 bx_offset = 0;
540 break;
Mark Mendell27dee8b2014-12-01 19:06:12 -0500541 case kX86_64:
542 // RIP relative to switch table.
543 bx_offset = tab_rec->offset;
544 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100545 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100547 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 bx_offset = tab_rec->anchor->offset;
549 break;
550 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
551 }
552 if (cu_->verbose) {
553 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
554 }
555 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800556 DCHECK(tab_rec->switch_mir != nullptr);
557 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
558 DCHECK(bb != nullptr);
559 int elems = 0;
560 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
561 int key = successor_block_info->key;
562 int target = successor_block_info->block;
563 LIR* boundary_lir = InsertCaseLabel(target, key);
564 DCHECK(boundary_lir != nullptr);
565 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000566 Push32(&code_buffer_, key);
567 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 if (cu_->verbose) {
569 LOG(INFO) << " Case[" << elems << "] key: 0x"
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800570 << std::hex << key << ", disp: 0x"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 << std::hex << disp;
572 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800573 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800575 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 } else {
577 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
578 static_cast<int>(Instruction::kPackedSwitchSignature));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800579 DCHECK(tab_rec->switch_mir != nullptr);
580 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
581 DCHECK(bb != nullptr);
582 int elems = 0;
583 int low_key = s4FromSwitchData(&tab_rec->table[2]);
584 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
585 int key = successor_block_info->key;
586 DCHECK_EQ(elems + low_key, key);
587 int target = successor_block_info->block;
588 LIR* boundary_lir = InsertCaseLabel(target, key);
589 DCHECK(boundary_lir != nullptr);
590 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000591 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 if (cu_->verbose) {
593 LOG(INFO) << " Case[" << elems << "] disp: 0x"
594 << std::hex << disp;
595 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800596 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800598 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 }
600 }
601}
602
603/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700604void Mir2Lir::InstallFillArrayData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100605 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000606 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700608 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
609 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 }
611 }
612}
613
buzbee0d829482013-10-11 15:24:55 -0700614static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700615 for (; lir != nullptr; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 lir->offset = offset;
617 offset += 4;
618 }
619 return offset;
620}
621
Ian Rogersff093b32014-04-30 19:04:27 -0700622static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
623 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700624 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700625 offset = RoundUp(offset, element_size);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700626 for (; lir != nullptr; lir = lir->next) {
buzbee0d829482013-10-11 15:24:55 -0700627 lir->offset = offset;
628 offset += element_size;
629 }
630 return offset;
631}
632
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700634bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000635 MappingTable table(&encoded_mapping_table_[0]);
636 std::vector<uint32_t> dex_pcs;
637 dex_pcs.reserve(table.DexToPcSize());
638 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
639 dex_pcs.push_back(it.DexPc());
640 }
641 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
642 std::sort(dex_pcs.begin(), dex_pcs.end());
643
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000645 auto it = dex_pcs.begin(), end = dex_pcs.end();
646 for (uint32_t dex_pc : mir_graph_->catches_) {
647 while (it != end && *it < dex_pc) {
648 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
649 ++it;
650 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000652 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
654 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000655 } else {
656 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 }
658 }
659 if (!success) {
660 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
661 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000662 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 }
664 return success;
665}
666
667
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700668void Mir2Lir::CreateMappingTables() {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700669 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
670
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000671 uint32_t pc2dex_data_size = 0u;
672 uint32_t pc2dex_entries = 0u;
673 uint32_t pc2dex_offset = 0u;
674 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700675 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000676 uint32_t dex2pc_data_size = 0u;
677 uint32_t dex2pc_entries = 0u;
678 uint32_t dex2pc_offset = 0u;
679 uint32_t dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700680 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700681 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000683 pc2dex_entries += 1;
684 DCHECK(pc2dex_offset <= tgt_lir->offset);
685 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
686 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
687 static_cast<int32_t>(pc2dex_dalvik_offset));
688 pc2dex_offset = tgt_lir->offset;
689 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 }
691 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000692 dex2pc_entries += 1;
693 DCHECK(dex2pc_offset <= tgt_lir->offset);
694 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
695 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
696 static_cast<int32_t>(dex2pc_dalvik_offset));
697 dex2pc_offset = tgt_lir->offset;
698 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 }
700 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000701
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700702 if (generate_src_map) {
703 src_mapping_table_.reserve(pc2dex_src_entries);
704 }
705
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000706 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
707 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
708 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000709 encoded_mapping_table_.resize(data_size);
710 uint8_t* write_pos = &encoded_mapping_table_[0];
711 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
712 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
713 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
714 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000715
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000716 pc2dex_offset = 0u;
717 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000718 dex2pc_offset = 0u;
719 dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700720 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700721 if (generate_src_map && !tgt_lir->flags.is_nop) {
722 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
723 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
724 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000725 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
726 DCHECK(pc2dex_offset <= tgt_lir->offset);
727 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
728 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
729 static_cast<int32_t>(pc2dex_dalvik_offset));
730 pc2dex_offset = tgt_lir->offset;
731 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
732 }
733 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
734 DCHECK(dex2pc_offset <= tgt_lir->offset);
735 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
736 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
737 static_cast<int32_t>(dex2pc_dalvik_offset));
738 dex2pc_offset = tgt_lir->offset;
739 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
740 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000741 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000742 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
743 hdr_data_size + pc2dex_data_size);
744 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000745
Ian Rogers96faf5b2013-08-09 22:05:32 -0700746 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000747 CHECK(VerifyCatchEntries());
748
Ian Rogers96faf5b2013-08-09 22:05:32 -0700749 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000750 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700751 CHECK_EQ(table.TotalSize(), total_entries);
752 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000753 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000754 auto it2 = table.DexToPcBegin();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700755 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000756 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
757 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
758 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
759 ++it;
760 }
761 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
762 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
763 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
764 ++it2;
765 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700766 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000767 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000768 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700769 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770}
771
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko767c7522015-03-20 12:47:30 +0000773 if (UNLIKELY((cu_->disable_opt & (1u << kPromoteRegs)) != 0u)) {
774 // If we're not promoting to physical registers, it's safe to use the verifier's notion of
775 // references. (We disable register promotion when type inference finds a type conflict and
776 // in that the case we defer to the verifier to avoid using the compiler's conflicting info.)
777 CreateNativeGcMapWithoutRegisterPromotion();
778 return;
779 }
780
781 ArenaBitVector* references = new (arena_) ArenaBitVector(arena_, mir_graph_->GetNumSSARegs(),
782 false);
783
784 // Calculate max native offset and max reference vreg.
785 MIR* prev_mir = nullptr;
786 int max_ref_vreg = -1;
787 CodeOffset max_native_offset = 0u;
788 for (const auto& entry : safepoints_) {
789 uint32_t native_offset = entry.first->offset;
790 max_native_offset = std::max(max_native_offset, native_offset);
791 MIR* mir = entry.second;
792 UpdateReferenceVRegs(mir, prev_mir, references);
793 max_ref_vreg = std::max(max_ref_vreg, references->GetHighestBitSet());
794 prev_mir = mir;
795 }
796
Vladimir Marko6e071832015-03-25 11:13:39 +0000797#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
798 static constexpr bool kLittleEndian = true;
799#else
800 static constexpr bool kLittleEndian = false;
801#endif
802
Vladimir Marko767c7522015-03-20 12:47:30 +0000803 // Build the GC map.
804 uint32_t reg_width = static_cast<uint32_t>((max_ref_vreg + 8) / 8);
805 GcMapBuilder native_gc_map_builder(&native_gc_map_,
806 safepoints_.size(),
807 max_native_offset, reg_width);
Vladimir Marko6e071832015-03-25 11:13:39 +0000808 if (kLittleEndian) {
809 for (const auto& entry : safepoints_) {
810 uint32_t native_offset = entry.first->offset;
811 MIR* mir = entry.second;
812 UpdateReferenceVRegs(mir, prev_mir, references);
813 // For little-endian, the bytes comprising the bit vector's raw storage are what we need.
814 native_gc_map_builder.AddEntry(native_offset,
815 reinterpret_cast<const uint8_t*>(references->GetRawStorage()));
816 prev_mir = mir;
Vladimir Marko767c7522015-03-20 12:47:30 +0000817 }
Vladimir Marko6e071832015-03-25 11:13:39 +0000818 } else {
819 ArenaVector<uint8_t> references_buffer(arena_->Adapter());
820 references_buffer.resize(reg_width);
821 for (const auto& entry : safepoints_) {
822 uint32_t native_offset = entry.first->offset;
823 MIR* mir = entry.second;
824 UpdateReferenceVRegs(mir, prev_mir, references);
825 // Big-endian or unknown endianness, manually translate the bit vector data.
826 const auto* raw_storage = references->GetRawStorage();
827 for (size_t i = 0; i != reg_width; ++i) {
828 references_buffer[i] = static_cast<uint8_t>(
829 raw_storage[i / sizeof(raw_storage[0])] >> (8u * (i % sizeof(raw_storage[0]))));
830 }
831 native_gc_map_builder.AddEntry(native_offset, &references_buffer[0]);
832 prev_mir = mir;
833 }
Vladimir Marko767c7522015-03-20 12:47:30 +0000834 }
835}
836
837void Mir2Lir::CreateNativeGcMapWithoutRegisterPromotion() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000838 DCHECK(!encoded_mapping_table_.empty());
839 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000841 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
842 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 if (native_offset > max_native_offset) {
844 max_native_offset = native_offset;
845 }
846 }
847 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000848 const std::vector<uint8_t>& gc_map_raw =
849 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
850 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
851 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000853 GcMapBuilder native_gc_map_builder(&native_gc_map_,
854 mapping_table.PcToDexSize(),
855 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856
Vladimir Marko06606b92013-12-02 15:31:08 +0000857 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
858 uint32_t native_offset = it.NativePcOffset();
859 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700861 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
Dave Allisonf9439142014-03-27 15:10:22 -0700862 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 native_gc_map_builder.AddEntry(native_offset, references);
864 }
Mathieu Chartierab972ef2014-12-03 17:38:22 -0800865
866 // Maybe not necessary, but this could help prevent errors where we access the verified method
867 // after it has been deleted.
868 mir_graph_->GetCurrentDexCompilationUnit()->ClearVerifiedMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869}
870
871/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700872int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100874 constexpr unsigned int ptr_size = sizeof(uint32_t);
Andreas Gampe785d2f22014-11-03 22:57:30 -0800875 static_assert(ptr_size >= sizeof(mirror::HeapReference<mirror::Object>),
876 "Pointer size cannot hold a heap reference");
Ian Rogersff093b32014-04-30 19:04:27 -0700877 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
878 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
879 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 return offset;
881}
882
buzbee0d829482013-10-11 15:24:55 -0700883int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100884 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 tab_rec->offset = offset;
886 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
887 offset += tab_rec->table[1] * (sizeof(int) * 2);
888 } else {
889 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
890 static_cast<int>(Instruction::kPackedSwitchSignature));
891 offset += tab_rec->table[1] * sizeof(int);
892 }
893 }
894 return offset;
895}
896
buzbee0d829482013-10-11 15:24:55 -0700897int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100898 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899 tab_rec->offset = offset;
900 offset += tab_rec->size;
901 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700902 offset = RoundUp(offset, 4);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100903 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 return offset;
905}
906
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907/*
908 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700909 * offset vaddr if pretty-printing, otherise use the standard block
910 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700911 * branch table during the assembly phase. All resource flags
912 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 */
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800914LIR* Mir2Lir::InsertCaseLabel(uint32_t bbid, int keyVal) {
915 LIR* boundary_lir = &block_label_list_[bbid];
buzbeeb48819d2013-09-14 16:15:25 -0700916 LIR* res = boundary_lir;
917 if (cu_->verbose) {
918 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000919 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800920 BasicBlock* bb = mir_graph_->GetBasicBlock(bbid);
921 DCHECK(bb != nullptr);
922 new_label->dalvik_offset = bb->start_offset;
buzbeeb48819d2013-09-14 16:15:25 -0700923 new_label->opcode = kPseudoCaseLabel;
924 new_label->operands[0] = keyVal;
925 new_label->flags.fixup = kFixupLabel;
926 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100927 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700928 InsertLIRAfter(boundary_lir, new_label);
buzbeeb48819d2013-09-14 16:15:25 -0700929 }
930 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931}
932
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700933void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 /*
935 * Sparse switch data format:
936 * ushort ident = 0x0200 magic value
937 * ushort size number of entries in the table; > 0
938 * int keys[size] keys, sorted low-to-high; 32-bit aligned
939 * int targets[size] branch targets, relative to switch opcode
940 *
941 * Total size is (2+size*4) 16-bit code units.
942 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700943 uint16_t ident = table[0];
944 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700945 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
946 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700947 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
948 << ", entries: " << std::dec << entries;
949 for (int i = 0; i < entries; i++) {
950 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
951 }
952}
953
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700954void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955 /*
956 * Packed switch data format:
957 * ushort ident = 0x0100 magic value
958 * ushort size number of entries in the table
959 * int first_key first (and lowest) switch case value
960 * int targets[size] branch targets, relative to switch opcode
961 *
962 * Total size is (4+size*2) 16-bit code units.
963 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700965 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 int entries = table[1];
967 int low_key = s4FromSwitchData(&table[2]);
968 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
969 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
970 for (int i = 0; i < entries; i++) {
971 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
972 << targets[i];
973 }
974}
975
buzbee252254b2013-09-08 16:20:53 -0700976/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700977void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700978 UNUSED(offset);
buzbee0d829482013-10-11 15:24:55 -0700979 // NOTE: only used for debug listings.
980 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981}
982
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983// Convert relation of src1/src2 to src2/src1
984ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
985 ConditionCode res;
986 switch (before) {
987 case kCondEq: res = kCondEq; break;
988 case kCondNe: res = kCondNe; break;
989 case kCondLt: res = kCondGt; break;
990 case kCondGt: res = kCondLt; break;
991 case kCondLe: res = kCondGe; break;
992 case kCondGe: res = kCondLe; break;
993 default:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700995 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 }
997 return res;
998}
999
Vladimir Markoa1a70742014-03-03 10:28:05 +00001000ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
1001 ConditionCode res;
1002 switch (before) {
1003 case kCondEq: res = kCondNe; break;
1004 case kCondNe: res = kCondEq; break;
1005 case kCondLt: res = kCondGe; break;
1006 case kCondGt: res = kCondLe; break;
1007 case kCondLe: res = kCondGt; break;
1008 case kCondGe: res = kCondLt; break;
1009 default:
Vladimir Markoa1a70742014-03-03 10:28:05 +00001010 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011 UNREACHABLE();
Vladimir Markoa1a70742014-03-03 10:28:05 +00001012 }
1013 return res;
1014}
1015
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016// TODO: move to mir_to_lir.cc
1017Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Andreas Gampe9c462082015-01-27 14:31:40 -08001018 : literal_list_(nullptr),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001019 method_literal_list_(nullptr),
1020 class_literal_list_(nullptr),
1021 code_literal_list_(nullptr),
1022 first_fixup_(nullptr),
Andreas Gampe9c462082015-01-27 14:31:40 -08001023 arena_(arena),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 cu_(cu),
1025 mir_graph_(mir_graph),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001026 switch_tables_(arena->Adapter(kArenaAllocSwitchTable)),
1027 fill_array_data_(arena->Adapter(kArenaAllocFillArrayData)),
1028 tempreg_info_(arena->Adapter()),
1029 reginfo_map_(arena->Adapter()),
1030 pointer_storage_(arena->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 data_offset_(0),
1032 total_size_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001033 block_label_list_(nullptr),
1034 promotion_map_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 current_dalvik_offset_(0),
Vladimir Marko767c7522015-03-20 12:47:30 +00001036 current_mir_(nullptr),
buzbeeb48819d2013-09-14 16:15:25 -07001037 estimated_native_code_size_(0),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001038 reg_pool_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 live_sreg_(0),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001040 code_buffer_(mir_graph->GetArena()->Adapter()),
1041 encoded_mapping_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko8081d2b2014-07-31 15:33:43 +01001042 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1043 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001044 native_gc_map_(mir_graph->GetArena()->Adapter()),
Vladimir Markof4da6752014-08-01 19:04:18 +01001045 patches_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 num_core_spills_(0),
1047 num_fp_spills_(0),
1048 frame_size_(0),
1049 core_spill_mask_(0),
1050 fp_spill_mask_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001051 first_lir_insn_(nullptr),
1052 last_lir_insn_(nullptr),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001053 slow_paths_(arena->Adapter(kArenaAllocSlowPaths)),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001054 mem_ref_type_(ResourceMask::kHeapRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001055 mask_cache_(arena),
Vladimir Marko767c7522015-03-20 12:47:30 +00001056 safepoints_(arena->Adapter()),
Vladimir Marko20f85592015-03-19 10:07:02 +00001057 dex_cache_arrays_layout_(cu->compiler_driver->GetDexCacheArraysLayout(cu->dex_file)),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001058 in_to_reg_storage_mapping_(arena) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001059 switch_tables_.reserve(4);
1060 fill_array_data_.reserve(4);
1061 tempreg_info_.reserve(20);
1062 reginfo_map_.reserve(RegStorage::kMaxRegs);
1063 pointer_storage_.reserve(128);
1064 slow_paths_.reserve(32);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001065 // Reserve pointer id 0 for nullptr.
Vladimir Markof6737f72015-03-23 17:05:14 +00001066 size_t null_idx = WrapPointer<void>(nullptr);
buzbee0d829482013-10-11 15:24:55 -07001067 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068}
1069
1070void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001071 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1073
1074 /* Allocate Registers using simple local allocation scheme */
1075 SimpleRegAlloc();
1076
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001077 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001078 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001079 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001080 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001082 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1083 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 MethodMIR2LIR();
1085 }
1086
1087 /* Method is not empty */
1088 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 /* Convert LIR into machine code. */
1090 AssembleLIR();
1091
buzbeeb01bf152014-05-13 15:59:07 -07001092 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 CodegenDump();
1094 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096}
1097
1098CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001099 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1100 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001102 // Prefix the encoded data with its size.
1103 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1104 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1105 vmap_encoder.PushBackUnsigned(size);
1106 // Core regs may have been inserted out of order - sort first.
1107 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1108 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1109 // Copy, stripping out the phys register sort key.
1110 vmap_encoder.PushBackUnsigned(
1111 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1112 }
1113 // Push a marker to take place of lr.
1114 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001115 if (cu_->instruction_set == kThumb2) {
1116 // fp regs already sorted.
1117 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1118 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1119 }
1120 } else {
1121 // For other platforms regs may have been inserted out of order - sort first.
1122 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1123 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1124 // Copy, stripping out the phys register sort key.
1125 vmap_encoder.PushBackUnsigned(
1126 ~(-1 << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
1127 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001128 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001130 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1131 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001132 DCHECK_EQ(core_vmap_table_.size(), 0u);
1133 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1134 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001136
Vladimir Markof4da6752014-08-01 19:04:18 +01001137 // Sort patches by literal offset for better deduplication.
1138 std::sort(patches_.begin(), patches_.end(), [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
1139 return lhs.LiteralOffset() < rhs.LiteralOffset();
1140 });
1141
Andreas Gamped37f9192015-03-04 14:00:56 -08001142 std::unique_ptr<std::vector<uint8_t>> cfi_info(
1143 cu_->compiler_driver->GetCompilerOptions().GetGenerateGDBInformation() ?
1144 ReturnFrameDescriptionEntry() :
1145 nullptr);
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001146 ArrayRef<const uint8_t> cfi_ref;
1147 if (cfi_info.get() != nullptr) {
1148 cfi_ref = ArrayRef<const uint8_t>(*cfi_info);
1149 }
1150 return CompiledMethod::SwapAllocCompiledMethod(
1151 cu_->compiler_driver, cu_->instruction_set,
1152 ArrayRef<const uint8_t>(code_buffer_),
1153 frame_size_, core_spill_mask_, fp_spill_mask_,
1154 &src_mapping_table_,
1155 ArrayRef<const uint8_t>(encoded_mapping_table_),
1156 ArrayRef<const uint8_t>(vmap_encoder.GetData()),
1157 ArrayRef<const uint8_t>(native_gc_map_),
1158 cfi_ref,
1159 ArrayRef<LinkerPatch>(patches_));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160}
1161
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001162size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1163 // Chose a reasonably small value in order to contain stack growth.
1164 // Backends that are smarter about spill region can return larger values.
1165 const size_t max_compiler_temps = 10;
1166 return max_compiler_temps;
1167}
1168
1169size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1170 // By default assume that the Mir2Lir will need one slot for each temporary.
1171 // If the backend can better determine temps that have non-overlapping ranges and
1172 // temps that do not need spilled, it can actually provide a small region.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001173 mir_graph_->CommitCompilerTemps();
1174 return mir_graph_->GetNumBytesForSpecialTemps() + mir_graph_->GetMaximumBytesForNonSpecialTemps();
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001175}
1176
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177int Mir2Lir::ComputeFrameSize() {
1178 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001179 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1180 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1181 + sizeof(uint32_t) // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001182 + mir_graph_->GetNumOfLocalCodeVRs() * sizeof(uint32_t)
1183 + mir_graph_->GetNumOfOutVRs() * sizeof(uint32_t)
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001184 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001186 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187}
1188
1189/*
1190 * Append an LIR instruction to the LIR list maintained by a compilation
1191 * unit
1192 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001193void Mir2Lir::AppendLIR(LIR* lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001194 if (first_lir_insn_ == nullptr) {
1195 DCHECK(last_lir_insn_ == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 last_lir_insn_ = first_lir_insn_ = lir;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001197 lir->prev = lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 } else {
1199 last_lir_insn_->next = lir;
1200 lir->prev = last_lir_insn_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001201 lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 last_lir_insn_ = lir;
1203 }
1204}
1205
1206/*
1207 * Insert an LIR instruction before the current instruction, which cannot be the
1208 * first instruction.
1209 *
1210 * prev_lir <-> new_lir <-> current_lir
1211 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001212void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001213 DCHECK(current_lir->prev != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 LIR *prev_lir = current_lir->prev;
1215
1216 prev_lir->next = new_lir;
1217 new_lir->prev = prev_lir;
1218 new_lir->next = current_lir;
1219 current_lir->prev = new_lir;
1220}
1221
1222/*
1223 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001224 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 *
1226 * current_lir -> new_lir -> old_next
1227 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001228void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 new_lir->prev = current_lir;
1230 new_lir->next = current_lir->next;
1231 current_lir->next = new_lir;
1232 new_lir->next->prev = new_lir;
1233}
1234
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001235bool Mir2Lir::PartiallyIntersects(RegLocation rl_src, RegLocation rl_dest) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001236 DCHECK(rl_src.wide);
1237 DCHECK(rl_dest.wide);
1238 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1239}
1240
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001241bool Mir2Lir::Intersects(RegLocation rl_src, RegLocation rl_dest) {
1242 DCHECK(rl_src.wide);
1243 DCHECK(rl_dest.wide);
1244 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) <= 1);
1245}
1246
buzbee2700f7e2014-03-07 09:46:20 -08001247LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001248 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001249 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001250 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1251 if (compare != nullptr) {
1252 *compare = inst;
1253 }
Mark Mendell766e9292014-01-27 07:55:47 -08001254 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1255 return branch;
1256}
1257
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001258void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001259 slow_paths_.push_back(slowpath);
Serguei Katkov589e0462014-09-05 18:37:22 +07001260 ResetDefTracking();
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001261}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001262
Jeff Hao49161ce2014-03-12 11:05:25 -07001263void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1264 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001265 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001266 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001267 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001268 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001269 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1270 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1271 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001272 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001273 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001274 // Loads a code pointer. Code from oat file can be mapped anywhere.
Vladimir Markof6737f72015-03-23 17:05:14 +00001275 OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001276 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001277 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001278}
1279
Jeff Hao49161ce2014-03-12 11:05:25 -07001280void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1281 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001282 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001283 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001284 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001285 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001286 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1287 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1288 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001289 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001290 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001291 // Loads an ArtMethod pointer, which is a reference as it lives in the heap.
Vladimir Markof6737f72015-03-23 17:05:14 +00001292 OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001293 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001294 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001295}
1296
Fred Shihe7f82e22014-08-06 10:46:37 -07001297void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1298 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001299 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001300 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001301 if (data_target == nullptr) {
1302 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001303 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001304 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001305 // Loads a Class pointer, which is a reference as it lives in the heap.
Vladimir Markof6737f72015-03-23 17:05:14 +00001306 OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001307}
1308
Vladimir Marko20f85592015-03-19 10:07:02 +00001309bool Mir2Lir::CanUseOpPcRelDexCacheArrayLoad() const {
1310 return false;
1311}
1312
1313void Mir2Lir::OpPcRelDexCacheArrayLoad(const DexFile* dex_file ATTRIBUTE_UNUSED,
1314 int offset ATTRIBUTE_UNUSED,
1315 RegStorage r_dest ATTRIBUTE_UNUSED) {
1316 LOG(FATAL) << "No generic implementation.";
1317 UNREACHABLE();
1318}
1319
Tong Shen547cdfd2014-08-05 01:54:19 -07001320std::vector<uint8_t>* Mir2Lir::ReturnFrameDescriptionEntry() {
Mark Mendellae9fd932014-02-10 16:14:35 -08001321 // Default case is to do nothing.
1322 return nullptr;
1323}
1324
buzbee2700f7e2014-03-07 09:46:20 -08001325RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001326 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001327 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001328 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001329 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1330 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1331 info_lo->SetIsWide(false);
1332 info_hi->SetIsWide(false);
1333 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001334 } else {
buzbee85089dd2014-05-25 15:10:52 -07001335 RegisterInfo* info = GetRegInfo(loc.reg);
1336 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1337 DCHECK(info_new != nullptr);
1338 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1339 info->MarkDead();
1340 info_new->MarkLive(loc.s_reg_low);
1341 }
1342 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001343 }
buzbee85089dd2014-05-25 15:10:52 -07001344 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001345 }
buzbee85089dd2014-05-25 15:10:52 -07001346 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001347 return loc;
1348}
1349
Mark Mendelld65c51a2014-04-29 16:55:20 -04001350void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001351 UNUSED(bb, mir);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001352 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001353 UNREACHABLE();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001354}
1355
Vladimir Marko767c7522015-03-20 12:47:30 +00001356void Mir2Lir::InitReferenceVRegs(BasicBlock* bb, BitVector* references) {
1357 // Mark the references coming from the first predecessor.
1358 DCHECK(bb != nullptr);
1359 DCHECK(bb->block_type == kEntryBlock || !bb->predecessors.empty());
1360 BasicBlock* first_bb =
1361 (bb->block_type == kEntryBlock) ? bb : mir_graph_->GetBasicBlock(bb->predecessors[0]);
1362 DCHECK(first_bb != nullptr);
1363 DCHECK(first_bb->data_flow_info != nullptr);
1364 DCHECK(first_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1365 const int32_t* first_vreg_to_ssa_map = first_bb->data_flow_info->vreg_to_ssa_map_exit;
1366 references->ClearAllBits();
1367 for (uint32_t vreg = 0, num_vregs = mir_graph_->GetNumOfCodeVRs(); vreg != num_vregs; ++vreg) {
1368 int32_t sreg = first_vreg_to_ssa_map[vreg];
1369 if (sreg != INVALID_SREG && mir_graph_->reg_location_[sreg].ref &&
1370 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[sreg])) {
1371 references->SetBit(vreg);
1372 }
1373 }
1374 // Unmark the references that are merging with a different value.
1375 for (size_t i = 1u, num_pred = bb->predecessors.size(); i < num_pred; ++i) {
1376 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(bb->predecessors[i]);
1377 DCHECK(pred_bb != nullptr);
1378 DCHECK(pred_bb->data_flow_info != nullptr);
1379 DCHECK(pred_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1380 const int32_t* pred_vreg_to_ssa_map = pred_bb->data_flow_info->vreg_to_ssa_map_exit;
1381 for (uint32_t vreg : references->Indexes()) {
1382 if (first_vreg_to_ssa_map[vreg] != pred_vreg_to_ssa_map[vreg]) {
1383 // NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
1384 // so clearing the bit has no effect on the iterator.
1385 references->ClearBit(vreg);
1386 }
1387 }
1388 }
1389 if (bb->block_type != kEntryBlock && bb->first_mir_insn != nullptr &&
1390 static_cast<int>(bb->first_mir_insn->dalvikInsn.opcode) == kMirOpCheckPart2) {
1391 // In Mir2Lir::MethodBlockCodeGen() we have artificially moved the throwing
1392 // instruction to the previous block. However, the MIRGraph data used above
1393 // doesn't reflect that, so we still need to process that MIR insn here.
Pavel Vyssotski356a1812015-03-27 15:23:02 +06001394 MIR* mir = nullptr;
1395 BasicBlock* pred_bb = bb;
1396 // Traverse empty blocks.
1397 while (mir == nullptr && pred_bb->predecessors.size() == 1u) {
1398 pred_bb = mir_graph_->GetBasicBlock(bb->predecessors[0]);
1399 DCHECK(pred_bb != nullptr);
1400 mir = pred_bb->last_mir_insn;
1401 }
1402 DCHECK(mir != nullptr);
1403 UpdateReferenceVRegsLocal(nullptr, mir, references);
Vladimir Marko767c7522015-03-20 12:47:30 +00001404 }
1405}
1406
1407bool Mir2Lir::UpdateReferenceVRegsLocal(MIR* mir, MIR* prev_mir, BitVector* references) {
1408 DCHECK(mir == nullptr || mir->bb == prev_mir->bb);
1409 DCHECK(prev_mir != nullptr);
1410 while (prev_mir != nullptr) {
1411 if (prev_mir == mir) {
1412 return true;
1413 }
1414 const size_t num_defs = prev_mir->ssa_rep->num_defs;
1415 const int32_t* defs = prev_mir->ssa_rep->defs;
1416 if (num_defs == 1u && mir_graph_->reg_location_[defs[0]].ref &&
1417 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[defs[0]])) {
1418 references->SetBit(mir_graph_->SRegToVReg(defs[0]));
1419 } else {
1420 for (size_t i = 0u; i != num_defs; ++i) {
1421 references->ClearBit(mir_graph_->SRegToVReg(defs[i]));
1422 }
1423 }
1424 prev_mir = prev_mir->next;
1425 }
1426 return false;
1427}
1428
1429void Mir2Lir::UpdateReferenceVRegs(MIR* mir, MIR* prev_mir, BitVector* references) {
1430 if (mir == nullptr) {
1431 // Safepoint in entry sequence.
1432 InitReferenceVRegs(mir_graph_->GetEntryBlock(), references);
1433 return;
1434 }
1435 if (IsInstructionReturn(mir->dalvikInsn.opcode) ||
1436 mir->dalvikInsn.opcode == Instruction::RETURN_VOID_NO_BARRIER) {
1437 references->ClearAllBits();
1438 if (mir->dalvikInsn.opcode == Instruction::RETURN_OBJECT) {
1439 references->SetBit(mir_graph_->SRegToVReg(mir->ssa_rep->uses[0]));
1440 }
1441 return;
1442 }
1443 if (prev_mir != nullptr && mir->bb == prev_mir->bb &&
1444 UpdateReferenceVRegsLocal(mir, prev_mir, references)) {
1445 return;
1446 }
1447 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
1448 DCHECK(bb != nullptr);
1449 InitReferenceVRegs(bb, references);
1450 bool success = UpdateReferenceVRegsLocal(mir, bb->first_mir_insn, references);
1451 DCHECK(success) << "MIR @0x" << std::hex << mir->offset << " not in BB#" << std::dec << mir->bb;
1452}
1453
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001454} // namespace art