blob: 4e7919b6d2def99826ea3a8ab883a8deb724eff5 [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"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "verifier/dex_gc_map.h"
33#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000034#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035
36namespace art {
37
Vladimir Marko06606b92013-12-02 15:31:08 +000038namespace {
39
40/* Dump a mapping table */
41template <typename It>
42void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
43 const Signature& signature, uint32_t size, It first) {
44 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080045 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000046 descriptor, name, signature.ToString().c_str(), size));
47 std::replace(line.begin(), line.end(), ';', '_');
48 LOG(INFO) << line;
49 for (uint32_t i = 0; i != size; ++i) {
50 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
51 ++first;
52 LOG(INFO) << line;
53 }
54 LOG(INFO) <<" };\n\n";
55 }
56}
57
58} // anonymous namespace
59
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070060bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 bool res = false;
62 if (rl_src.is_const) {
63 if (rl_src.wide) {
Andreas Gampede0b9962014-08-27 14:24:42 -070064 // For wide registers, check whether we're the high partner. In that case we need to switch
65 // to the lower one for the correct value.
66 if (rl_src.high_word) {
67 rl_src.high_word = false;
68 rl_src.s_reg_low--;
69 rl_src.orig_sreg--;
70 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070072 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070074 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 }
76 } else {
77 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070078 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070080 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 }
82 }
83 }
84 return res;
85}
86
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070087void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070088 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010089 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010091 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +000092 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
93 safepoints_.emplace_back(safepoint_pc, current_mir_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070094}
95
Andreas Gampe3c12c512014-06-24 18:46:29 +000096void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
97 DCHECK(!after->flags.use_def_invalid);
98 after->u.m.def_mask = &kEncodeAll;
99 // As NewLIR0 uses Append, we need to create the LIR by hand.
100 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
101 if (after->next == nullptr) {
102 DCHECK_EQ(after, last_lir_insn_);
103 AppendLIR(safepoint_pc);
104 } else {
105 InsertLIRAfter(after, safepoint_pc);
106 }
107 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +0000108 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
109 safepoints_.emplace_back(safepoint_pc, current_mir_);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000110}
111
buzbee252254b2013-09-08 16:20:53 -0700112/* Remove a LIR from the list. */
113void Mir2Lir::UnlinkLIR(LIR* lir) {
114 if (UNLIKELY(lir == first_lir_insn_)) {
115 first_lir_insn_ = lir->next;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700116 if (lir->next != nullptr) {
117 lir->next->prev = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700118 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700119 DCHECK(lir->next == nullptr);
buzbee252254b2013-09-08 16:20:53 -0700120 DCHECK(lir == last_lir_insn_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700121 last_lir_insn_ = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700122 }
123 } else if (lir == last_lir_insn_) {
124 last_lir_insn_ = lir->prev;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700125 lir->prev->next = nullptr;
126 } else if ((lir->prev != nullptr) && (lir->next != nullptr)) {
buzbee252254b2013-09-08 16:20:53 -0700127 lir->prev->next = lir->next;
128 lir->next->prev = lir->prev;
129 }
130}
131
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700133void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700135 if (!cu_->verbose) {
136 UnlinkLIR(lir);
137 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138}
139
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700140void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700142 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100143 // TODO: Avoid the extra Arena allocation!
144 const ResourceMask** mask_ptr;
145 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700147 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700149 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100151 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100153 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 /* ..and then add back the one we need */
155 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100156 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100158 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100160 case ResourceMask::kDalvikReg:
161 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100163 case ResourceMask::kHeapRef:
164 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100166 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 /* Currently only loads can be marked as kMustNotAlias */
168 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100169 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 break;
171 default:
172 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
173 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100174 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175}
176
177/*
178 * Mark load/store instructions that access Dalvik registers through the stack.
179 */
180void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700181 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100182 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
183 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184
185 /*
186 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
187 * access.
188 */
buzbeeb48819d2013-09-14 16:15:25 -0700189 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190}
191
192/*
193 * Debugging macros
194 */
195#define DUMP_RESOURCE_MASK(X)
196
197/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700198void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 int offset = lir->offset;
200 int dest = lir->operands[0];
201 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
202
203 /* Handle pseudo-ops individually, and all regular insns as a group */
204 switch (lir->opcode) {
205 case kPseudoMethodEntry:
206 LOG(INFO) << "-------- method entry "
207 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
208 break;
209 case kPseudoMethodExit:
210 LOG(INFO) << "-------- Method_Exit";
211 break;
212 case kPseudoBarrier:
213 LOG(INFO) << "-------- BARRIER";
214 break;
215 case kPseudoEntryBlock:
216 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
217 break;
218 case kPseudoDalvikByteCodeBoundary:
219 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700220 // NOTE: only used for debug listings.
221 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 }
223 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000224 << lir->dalvik_offset << " @ "
Vladimir Markof6737f72015-03-23 17:05:14 +0000225 << UnwrapPointer<char>(lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226 break;
227 case kPseudoExitBlock:
228 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
229 break;
230 case kPseudoPseudoAlign4:
231 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
232 << offset << "): .align4";
233 break;
234 case kPseudoEHBlockLabel:
235 LOG(INFO) << "Exception_Handling:";
236 break;
237 case kPseudoTargetLabel:
238 case kPseudoNormalBlockLabel:
239 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
240 break;
241 case kPseudoThrowTarget:
242 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
243 break;
244 case kPseudoIntrinsicRetry:
245 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
246 break;
247 case kPseudoSuspendTarget:
248 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
249 break;
250 case kPseudoSafepointPC:
251 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
252 break;
253 case kPseudoExportedPC:
254 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
255 break;
256 case kPseudoCaseLabel:
257 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
258 << std::hex << lir->operands[0] << "|" << std::dec <<
259 lir->operands[0];
260 break;
261 default:
262 if (lir->flags.is_nop && !dump_nop) {
263 break;
264 } else {
265 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
266 lir, base_addr));
267 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
268 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800269 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
270 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 op_name.c_str(), op_operands.c_str(),
272 lir->flags.is_nop ? "(nop)" : "");
273 }
274 break;
275 }
276
buzbeeb48819d2013-09-14 16:15:25 -0700277 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100278 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 }
buzbeeb48819d2013-09-14 16:15:25 -0700280 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100281 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 }
283}
284
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700285void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700286 uint32_t num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
287 for (uint32_t i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 PromotionMap v_reg_map = promotion_map_[i];
289 std::string buf;
290 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700291 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 }
293
294 std::string buf3;
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700295 if (i < mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 StringAppendF(&buf3, "%02d", i);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700297 } else if (i == mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 buf3 = "Method*";
299 } else {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700300 uint32_t diff = i - mir_graph_->GetNumOfCodeVRs();
301 StringAppendF(&buf3, "ct%d", diff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 }
303
304 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
305 v_reg_map.core_location == kLocPhysReg ?
306 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
307 v_reg_map.core_reg : SRegOffset(i),
308 buf.c_str());
309 }
310}
311
buzbee7a11ab02014-04-28 20:02:38 -0700312void Mir2Lir::UpdateLIROffsets() {
313 // Only used for code listings.
314 size_t offset = 0;
315 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
316 lir->offset = offset;
317 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
318 offset += GetInsnSize(lir);
319 } else if (lir->opcode == kPseudoPseudoAlign4) {
320 offset += (offset & 0x2);
321 }
322 }
323}
324
Vladimir Marko743b98c2014-11-24 19:45:41 +0000325void Mir2Lir::MarkGCCard(int opt_flags, RegStorage val_reg, RegStorage tgt_addr_reg) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000326 DCHECK(val_reg.Valid());
327 DCHECK_EQ(val_reg.Is64Bit(), cu_->target64);
Vladimir Marko743b98c2014-11-24 19:45:41 +0000328 if ((opt_flags & MIR_STORE_NON_NULL_VALUE) != 0) {
329 UnconditionallyMarkGCCard(tgt_addr_reg);
330 } else {
331 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, nullptr);
332 UnconditionallyMarkGCCard(tgt_addr_reg);
333 LIR* target = NewLIR0(kPseudoTargetLabel);
334 branch_over->target = target;
335 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000336}
337
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700339void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 LOG(INFO) << "Dumping LIR insns for "
341 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
342 LIR* lir_insn;
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700343 int insns_size = mir_graph_->GetNumDalvikInsns();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700345 LOG(INFO) << "Regs (excluding ins) : " << mir_graph_->GetNumOfLocalCodeVRs();
346 LOG(INFO) << "Ins : " << mir_graph_->GetNumOfInVRs();
347 LOG(INFO) << "Outs : " << mir_graph_->GetNumOfOutVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 LOG(INFO) << "CoreSpills : " << num_core_spills_;
349 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800350 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 LOG(INFO) << "Frame size : " << frame_size_;
352 LOG(INFO) << "code size is " << total_size_ <<
353 " bytes, Dalvik size is " << insns_size * 2;
354 LOG(INFO) << "expansion factor: "
355 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
356 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700357 UpdateLIROffsets();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700358 for (lir_insn = first_lir_insn_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359 DumpLIRInsn(lir_insn, 0);
360 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700361 for (lir_insn = literal_list_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
363 lir_insn->operands[0]);
364 }
365
366 const DexFile::MethodId& method_id =
367 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700368 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
369 const char* name = cu_->dex_file->GetMethodName(method_id);
370 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371
372 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000373 if (!encoded_mapping_table_.empty()) {
374 MappingTable table(&encoded_mapping_table_[0]);
375 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
376 table.PcToDexSize(), table.PcToDexBegin());
377 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
378 table.DexToPcSize(), table.DexToPcBegin());
379 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380}
381
382/*
383 * Search the existing constants in the literal pool for an exact or close match
384 * within specified delta (greater or equal to 0).
385 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700386LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 while (data_target) {
388 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
389 return data_target;
390 data_target = data_target->next;
391 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700392 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393}
394
395/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700396LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 bool lo_match = false;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700398 LIR* lo_target = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 while (data_target) {
400 if (lo_match && (data_target->operands[0] == val_hi)) {
401 // Record high word in case we need to expand this later.
402 lo_target->operands[1] = val_hi;
403 return lo_target;
404 }
405 lo_match = false;
406 if (data_target->operands[0] == val_lo) {
407 lo_match = true;
408 lo_target = data_target;
409 }
410 data_target = data_target->next;
411 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700412 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413}
414
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100415/* Search the existing constants in the literal pool for an exact method match */
416LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
417 while (data_target) {
418 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000419 UnwrapPointer<DexFile>(data_target->operands[1]) == method.dex_file) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100420 return data_target;
421 }
422 data_target = data_target->next;
423 }
424 return nullptr;
425}
426
Fred Shihe7f82e22014-08-06 10:46:37 -0700427/* Search the existing constants in the literal pool for an exact class match */
428LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
429 while (data_target) {
430 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000431 UnwrapPointer<DexFile>(data_target->operands[1]) == &dex_file) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700432 return data_target;
433 }
434 data_target = data_target->next;
435 }
436 return nullptr;
437}
438
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439/*
440 * The following are building blocks to insert constants into the pool or
441 * instruction streams.
442 */
443
444/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700445LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 /* Add the constant to the literal pool */
447 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000448 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 new_value->operands[0] = value;
450 new_value->next = *constant_list_p;
451 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700452 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 return new_value;
454 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700455 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456}
457
458/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700459LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 AddWordData(constant_list_p, val_hi);
461 return AddWordData(constant_list_p, val_lo);
462}
463
Matteo Franchin27cc0932014-09-08 18:29:24 +0100464/**
465 * @brief Push a compressed reference which needs patching at link/patchoat-time.
466 * @details This needs to be kept consistent with the code which actually does the patching in
467 * oat_writer.cc and in the patchoat tool.
468 */
Vladimir Marko80b96d12015-02-19 15:50:28 +0000469static void PushUnpatchedReference(CodeBuffer* buf) {
Matteo Franchin27cc0932014-09-08 18:29:24 +0100470 // Note that we can safely initialize the patches to zero. The code deduplication mechanism takes
471 // the patches into account when determining whether two pieces of codes are functionally
472 // equivalent.
473 Push32(buf, UINT32_C(0));
buzbee0d829482013-10-11 15:24:55 -0700474}
475
Vladimir Marko80b96d12015-02-19 15:50:28 +0000476static void AlignBuffer(CodeBuffer* buf, size_t offset) {
477 DCHECK_LE(buf->size(), offset);
478 buf->insert(buf->end(), offset - buf->size(), 0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479}
480
481/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700482void Mir2Lir::InstallLiteralPools() {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000483 AlignBuffer(&code_buffer_, data_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 LIR* data_lir = literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700485 while (data_lir != nullptr) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000486 Push32(&code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 data_lir = NEXT_LIR(data_lir);
488 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100489 // TODO: patches_.reserve() as needed.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 // Push code and method literals, record offsets for the compiler to patch.
491 data_lir = code_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700492 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700493 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000494 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100495 patches_.push_back(LinkerPatch::CodePatch(code_buffer_.size(),
496 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000497 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 data_lir = NEXT_LIR(data_lir);
499 }
500 data_lir = method_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700501 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700502 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000503 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100504 patches_.push_back(LinkerPatch::MethodPatch(code_buffer_.size(),
505 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000506 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 data_lir = NEXT_LIR(data_lir);
508 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800509 // Push class literals.
510 data_lir = class_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700511 while (data_lir != nullptr) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100512 uint32_t target_type_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000513 const DexFile* class_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100514 patches_.push_back(LinkerPatch::TypePatch(code_buffer_.size(),
515 class_dex_file, target_type_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000516 PushUnpatchedReference(&code_buffer_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800517 data_lir = NEXT_LIR(data_lir);
518 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519}
520
521/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700522void Mir2Lir::InstallSwitchTables() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100523 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000524 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 /*
526 * For Arm, our reference point is the address of the bx
527 * instruction that does the launch, so we have to subtract
528 * the auto pc-advance. For other targets the reference point
529 * is a label, so we can use the offset as-is.
530 */
531 int bx_offset = INVALID_OFFSET;
532 switch (cu_->instruction_set) {
533 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700534 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 bx_offset = tab_rec->anchor->offset + 4;
536 break;
537 case kX86:
538 bx_offset = 0;
539 break;
Mark Mendell27dee8b2014-12-01 19:06:12 -0500540 case kX86_64:
541 // RIP relative to switch table.
542 bx_offset = tab_rec->offset;
543 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100544 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100546 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 bx_offset = tab_rec->anchor->offset;
548 break;
549 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
550 }
551 if (cu_->verbose) {
552 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
553 }
554 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800555 DCHECK(tab_rec->switch_mir != nullptr);
556 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
557 DCHECK(bb != nullptr);
558 int elems = 0;
559 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
560 int key = successor_block_info->key;
561 int target = successor_block_info->block;
562 LIR* boundary_lir = InsertCaseLabel(target, key);
563 DCHECK(boundary_lir != nullptr);
564 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000565 Push32(&code_buffer_, key);
566 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 if (cu_->verbose) {
568 LOG(INFO) << " Case[" << elems << "] key: 0x"
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800569 << std::hex << key << ", disp: 0x"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 << std::hex << disp;
571 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800572 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800574 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 } else {
576 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
577 static_cast<int>(Instruction::kPackedSwitchSignature));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800578 DCHECK(tab_rec->switch_mir != nullptr);
579 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
580 DCHECK(bb != nullptr);
581 int elems = 0;
582 int low_key = s4FromSwitchData(&tab_rec->table[2]);
583 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
584 int key = successor_block_info->key;
585 DCHECK_EQ(elems + low_key, key);
586 int target = successor_block_info->block;
587 LIR* boundary_lir = InsertCaseLabel(target, key);
588 DCHECK(boundary_lir != nullptr);
589 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000590 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 if (cu_->verbose) {
592 LOG(INFO) << " Case[" << elems << "] disp: 0x"
593 << std::hex << disp;
594 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800595 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800597 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 }
599 }
600}
601
602/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700603void Mir2Lir::InstallFillArrayData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100604 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000605 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700607 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
608 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 }
610 }
611}
612
buzbee0d829482013-10-11 15:24:55 -0700613static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700614 for (; lir != nullptr; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615 lir->offset = offset;
616 offset += 4;
617 }
618 return offset;
619}
620
Ian Rogersff093b32014-04-30 19:04:27 -0700621static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
622 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700623 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700624 offset = RoundUp(offset, element_size);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700625 for (; lir != nullptr; lir = lir->next) {
buzbee0d829482013-10-11 15:24:55 -0700626 lir->offset = offset;
627 offset += element_size;
628 }
629 return offset;
630}
631
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700633bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000634 MappingTable table(&encoded_mapping_table_[0]);
635 std::vector<uint32_t> dex_pcs;
636 dex_pcs.reserve(table.DexToPcSize());
637 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
638 dex_pcs.push_back(it.DexPc());
639 }
640 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
641 std::sort(dex_pcs.begin(), dex_pcs.end());
642
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000644 auto it = dex_pcs.begin(), end = dex_pcs.end();
645 for (uint32_t dex_pc : mir_graph_->catches_) {
646 while (it != end && *it < dex_pc) {
647 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
648 ++it;
649 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000651 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
653 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000654 } else {
655 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 }
657 }
658 if (!success) {
659 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
660 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000661 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 }
663 return success;
664}
665
666
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700667void Mir2Lir::CreateMappingTables() {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700668 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
669
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000670 uint32_t pc2dex_data_size = 0u;
671 uint32_t pc2dex_entries = 0u;
672 uint32_t pc2dex_offset = 0u;
673 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700674 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000675 uint32_t dex2pc_data_size = 0u;
676 uint32_t dex2pc_entries = 0u;
677 uint32_t dex2pc_offset = 0u;
678 uint32_t dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700679 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700680 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000682 pc2dex_entries += 1;
683 DCHECK(pc2dex_offset <= tgt_lir->offset);
684 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
685 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
686 static_cast<int32_t>(pc2dex_dalvik_offset));
687 pc2dex_offset = tgt_lir->offset;
688 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 }
690 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000691 dex2pc_entries += 1;
692 DCHECK(dex2pc_offset <= tgt_lir->offset);
693 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
694 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
695 static_cast<int32_t>(dex2pc_dalvik_offset));
696 dex2pc_offset = tgt_lir->offset;
697 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 }
699 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000700
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700701 if (generate_src_map) {
702 src_mapping_table_.reserve(pc2dex_src_entries);
703 }
704
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000705 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
706 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
707 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000708 encoded_mapping_table_.resize(data_size);
709 uint8_t* write_pos = &encoded_mapping_table_[0];
710 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
711 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
712 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
713 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000714
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000715 pc2dex_offset = 0u;
716 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000717 dex2pc_offset = 0u;
718 dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700719 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700720 if (generate_src_map && !tgt_lir->flags.is_nop) {
721 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
722 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
723 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000724 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
725 DCHECK(pc2dex_offset <= tgt_lir->offset);
726 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
727 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
728 static_cast<int32_t>(pc2dex_dalvik_offset));
729 pc2dex_offset = tgt_lir->offset;
730 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
731 }
732 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
733 DCHECK(dex2pc_offset <= tgt_lir->offset);
734 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
735 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
736 static_cast<int32_t>(dex2pc_dalvik_offset));
737 dex2pc_offset = tgt_lir->offset;
738 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
739 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000740 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000741 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
742 hdr_data_size + pc2dex_data_size);
743 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000744
Ian Rogers96faf5b2013-08-09 22:05:32 -0700745 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000746 CHECK(VerifyCatchEntries());
747
Ian Rogers96faf5b2013-08-09 22:05:32 -0700748 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000749 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700750 CHECK_EQ(table.TotalSize(), total_entries);
751 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000752 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000753 auto it2 = table.DexToPcBegin();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700754 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000755 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
756 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
757 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
758 ++it;
759 }
760 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
761 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
762 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
763 ++it2;
764 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700765 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000766 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000767 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700768 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769}
770
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko767c7522015-03-20 12:47:30 +0000772 if (UNLIKELY((cu_->disable_opt & (1u << kPromoteRegs)) != 0u)) {
773 // If we're not promoting to physical registers, it's safe to use the verifier's notion of
774 // references. (We disable register promotion when type inference finds a type conflict and
775 // in that the case we defer to the verifier to avoid using the compiler's conflicting info.)
776 CreateNativeGcMapWithoutRegisterPromotion();
777 return;
778 }
779
780 ArenaBitVector* references = new (arena_) ArenaBitVector(arena_, mir_graph_->GetNumSSARegs(),
781 false);
782
783 // Calculate max native offset and max reference vreg.
784 MIR* prev_mir = nullptr;
785 int max_ref_vreg = -1;
786 CodeOffset max_native_offset = 0u;
787 for (const auto& entry : safepoints_) {
788 uint32_t native_offset = entry.first->offset;
789 max_native_offset = std::max(max_native_offset, native_offset);
790 MIR* mir = entry.second;
791 UpdateReferenceVRegs(mir, prev_mir, references);
792 max_ref_vreg = std::max(max_ref_vreg, references->GetHighestBitSet());
793 prev_mir = mir;
794 }
795
796 // Build the GC map.
797 uint32_t reg_width = static_cast<uint32_t>((max_ref_vreg + 8) / 8);
798 GcMapBuilder native_gc_map_builder(&native_gc_map_,
799 safepoints_.size(),
800 max_native_offset, reg_width);
801#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN)
802 ArenaVector<uint8_t> references_buffer(arena_->Adapter());
803 references_buffer.resize(reg_width);
804#endif
805 for (const auto& entry : safepoints_) {
806 uint32_t native_offset = entry.first->offset;
807 MIR* mir = entry.second;
808 UpdateReferenceVRegs(mir, prev_mir, references);
809#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN)
810 // Big-endian or unknown endianness, manually translate the bit vector data.
811 const auto* raw_storage = references->GetRawStorage();
812 for (size_t i = 0; i != reg_width; ++i) {
813 references_buffer[i] = static_cast<uint8_t>(
814 raw_storage[i / sizeof(raw_storage[0])] >> (8u * (i % sizeof(raw_storage[0]))));
815 }
816 native_gc_map_builder.AddEntry(native_offset, &references_buffer[0]);
817#else
818 // For little-endian, the bytes comprising the bit vector's raw storage are what we need.
819 native_gc_map_builder.AddEntry(native_offset,
820 reinterpret_cast<const uint8_t*>(references->GetRawStorage()));
821#endif
822 prev_mir = mir;
823 }
824}
825
826void Mir2Lir::CreateNativeGcMapWithoutRegisterPromotion() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000827 DCHECK(!encoded_mapping_table_.empty());
828 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000830 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
831 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 if (native_offset > max_native_offset) {
833 max_native_offset = native_offset;
834 }
835 }
836 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000837 const std::vector<uint8_t>& gc_map_raw =
838 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
839 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
840 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000842 GcMapBuilder native_gc_map_builder(&native_gc_map_,
843 mapping_table.PcToDexSize(),
844 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845
Vladimir Marko06606b92013-12-02 15:31:08 +0000846 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
847 uint32_t native_offset = it.NativePcOffset();
848 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700850 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
Dave Allisonf9439142014-03-27 15:10:22 -0700851 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 native_gc_map_builder.AddEntry(native_offset, references);
853 }
Mathieu Chartierab972ef2014-12-03 17:38:22 -0800854
855 // Maybe not necessary, but this could help prevent errors where we access the verified method
856 // after it has been deleted.
857 mir_graph_->GetCurrentDexCompilationUnit()->ClearVerifiedMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858}
859
860/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700861int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100863 constexpr unsigned int ptr_size = sizeof(uint32_t);
Andreas Gampe785d2f22014-11-03 22:57:30 -0800864 static_assert(ptr_size >= sizeof(mirror::HeapReference<mirror::Object>),
865 "Pointer size cannot hold a heap reference");
Ian Rogersff093b32014-04-30 19:04:27 -0700866 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
867 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
868 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869 return offset;
870}
871
buzbee0d829482013-10-11 15:24:55 -0700872int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100873 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 tab_rec->offset = offset;
875 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
876 offset += tab_rec->table[1] * (sizeof(int) * 2);
877 } else {
878 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
879 static_cast<int>(Instruction::kPackedSwitchSignature));
880 offset += tab_rec->table[1] * sizeof(int);
881 }
882 }
883 return offset;
884}
885
buzbee0d829482013-10-11 15:24:55 -0700886int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100887 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 tab_rec->offset = offset;
889 offset += tab_rec->size;
890 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700891 offset = RoundUp(offset, 4);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100892 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 return offset;
894}
895
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896/*
897 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700898 * offset vaddr if pretty-printing, otherise use the standard block
899 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700900 * branch table during the assembly phase. All resource flags
901 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 */
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800903LIR* Mir2Lir::InsertCaseLabel(uint32_t bbid, int keyVal) {
904 LIR* boundary_lir = &block_label_list_[bbid];
buzbeeb48819d2013-09-14 16:15:25 -0700905 LIR* res = boundary_lir;
906 if (cu_->verbose) {
907 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000908 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800909 BasicBlock* bb = mir_graph_->GetBasicBlock(bbid);
910 DCHECK(bb != nullptr);
911 new_label->dalvik_offset = bb->start_offset;
buzbeeb48819d2013-09-14 16:15:25 -0700912 new_label->opcode = kPseudoCaseLabel;
913 new_label->operands[0] = keyVal;
914 new_label->flags.fixup = kFixupLabel;
915 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100916 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700917 InsertLIRAfter(boundary_lir, new_label);
buzbeeb48819d2013-09-14 16:15:25 -0700918 }
919 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920}
921
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700922void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923 /*
924 * Sparse switch data format:
925 * ushort ident = 0x0200 magic value
926 * ushort size number of entries in the table; > 0
927 * int keys[size] keys, sorted low-to-high; 32-bit aligned
928 * int targets[size] branch targets, relative to switch opcode
929 *
930 * Total size is (2+size*4) 16-bit code units.
931 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 uint16_t ident = table[0];
933 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700934 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
935 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700936 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
937 << ", entries: " << std::dec << entries;
938 for (int i = 0; i < entries; i++) {
939 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
940 }
941}
942
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700943void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944 /*
945 * Packed switch data format:
946 * ushort ident = 0x0100 magic value
947 * ushort size number of entries in the table
948 * int first_key first (and lowest) switch case value
949 * int targets[size] branch targets, relative to switch opcode
950 *
951 * Total size is (4+size*2) 16-bit code units.
952 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700954 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955 int entries = table[1];
956 int low_key = s4FromSwitchData(&table[2]);
957 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
958 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
959 for (int i = 0; i < entries; i++) {
960 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
961 << targets[i];
962 }
963}
964
buzbee252254b2013-09-08 16:20:53 -0700965/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700966void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700967 UNUSED(offset);
buzbee0d829482013-10-11 15:24:55 -0700968 // NOTE: only used for debug listings.
969 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970}
971
Brian Carlstrom7940e442013-07-12 13:46:57 -0700972// Convert relation of src1/src2 to src2/src1
973ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
974 ConditionCode res;
975 switch (before) {
976 case kCondEq: res = kCondEq; break;
977 case kCondNe: res = kCondNe; break;
978 case kCondLt: res = kCondGt; break;
979 case kCondGt: res = kCondLt; break;
980 case kCondLe: res = kCondGe; break;
981 case kCondGe: res = kCondLe; break;
982 default:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700984 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985 }
986 return res;
987}
988
Vladimir Markoa1a70742014-03-03 10:28:05 +0000989ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
990 ConditionCode res;
991 switch (before) {
992 case kCondEq: res = kCondNe; break;
993 case kCondNe: res = kCondEq; break;
994 case kCondLt: res = kCondGe; break;
995 case kCondGt: res = kCondLe; break;
996 case kCondLe: res = kCondGt; break;
997 case kCondGe: res = kCondLt; break;
998 default:
Vladimir Markoa1a70742014-03-03 10:28:05 +0000999 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001000 UNREACHABLE();
Vladimir Markoa1a70742014-03-03 10:28:05 +00001001 }
1002 return res;
1003}
1004
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005// TODO: move to mir_to_lir.cc
1006Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Andreas Gampe9c462082015-01-27 14:31:40 -08001007 : literal_list_(nullptr),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001008 method_literal_list_(nullptr),
1009 class_literal_list_(nullptr),
1010 code_literal_list_(nullptr),
1011 first_fixup_(nullptr),
Andreas Gampe9c462082015-01-27 14:31:40 -08001012 arena_(arena),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 cu_(cu),
1014 mir_graph_(mir_graph),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001015 switch_tables_(arena->Adapter(kArenaAllocSwitchTable)),
1016 fill_array_data_(arena->Adapter(kArenaAllocFillArrayData)),
1017 tempreg_info_(arena->Adapter()),
1018 reginfo_map_(arena->Adapter()),
1019 pointer_storage_(arena->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 data_offset_(0),
1021 total_size_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001022 block_label_list_(nullptr),
1023 promotion_map_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 current_dalvik_offset_(0),
Vladimir Marko767c7522015-03-20 12:47:30 +00001025 current_mir_(nullptr),
buzbeeb48819d2013-09-14 16:15:25 -07001026 estimated_native_code_size_(0),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001027 reg_pool_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 live_sreg_(0),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001029 code_buffer_(mir_graph->GetArena()->Adapter()),
1030 encoded_mapping_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko8081d2b2014-07-31 15:33:43 +01001031 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1032 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001033 native_gc_map_(mir_graph->GetArena()->Adapter()),
Vladimir Markof4da6752014-08-01 19:04:18 +01001034 patches_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 num_core_spills_(0),
1036 num_fp_spills_(0),
1037 frame_size_(0),
1038 core_spill_mask_(0),
1039 fp_spill_mask_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001040 first_lir_insn_(nullptr),
1041 last_lir_insn_(nullptr),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001042 slow_paths_(arena->Adapter(kArenaAllocSlowPaths)),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001043 mem_ref_type_(ResourceMask::kHeapRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001044 mask_cache_(arena),
Vladimir Marko767c7522015-03-20 12:47:30 +00001045 safepoints_(arena->Adapter()),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001046 in_to_reg_storage_mapping_(arena) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001047 switch_tables_.reserve(4);
1048 fill_array_data_.reserve(4);
1049 tempreg_info_.reserve(20);
1050 reginfo_map_.reserve(RegStorage::kMaxRegs);
1051 pointer_storage_.reserve(128);
1052 slow_paths_.reserve(32);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001053 // Reserve pointer id 0 for nullptr.
Vladimir Markof6737f72015-03-23 17:05:14 +00001054 size_t null_idx = WrapPointer<void>(nullptr);
buzbee0d829482013-10-11 15:24:55 -07001055 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056}
1057
1058void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001059 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1061
1062 /* Allocate Registers using simple local allocation scheme */
1063 SimpleRegAlloc();
1064
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001065 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001066 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001067 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001068 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001069
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001070 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1071 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 MethodMIR2LIR();
1073 }
1074
1075 /* Method is not empty */
1076 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 /* Convert LIR into machine code. */
1078 AssembleLIR();
1079
buzbeeb01bf152014-05-13 15:59:07 -07001080 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 CodegenDump();
1082 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084}
1085
1086CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001087 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1088 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001090 // Prefix the encoded data with its size.
1091 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1092 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1093 vmap_encoder.PushBackUnsigned(size);
1094 // Core regs may have been inserted out of order - sort first.
1095 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1096 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1097 // Copy, stripping out the phys register sort key.
1098 vmap_encoder.PushBackUnsigned(
1099 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1100 }
1101 // Push a marker to take place of lr.
1102 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001103 if (cu_->instruction_set == kThumb2) {
1104 // fp regs already sorted.
1105 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1106 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1107 }
1108 } else {
1109 // For other platforms regs may have been inserted out of order - sort first.
1110 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1111 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1112 // Copy, stripping out the phys register sort key.
1113 vmap_encoder.PushBackUnsigned(
1114 ~(-1 << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
1115 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001116 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001118 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1119 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001120 DCHECK_EQ(core_vmap_table_.size(), 0u);
1121 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1122 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001124
Vladimir Markof4da6752014-08-01 19:04:18 +01001125 // Sort patches by literal offset for better deduplication.
1126 std::sort(patches_.begin(), patches_.end(), [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
1127 return lhs.LiteralOffset() < rhs.LiteralOffset();
1128 });
1129
Andreas Gamped37f9192015-03-04 14:00:56 -08001130 std::unique_ptr<std::vector<uint8_t>> cfi_info(
1131 cu_->compiler_driver->GetCompilerOptions().GetGenerateGDBInformation() ?
1132 ReturnFrameDescriptionEntry() :
1133 nullptr);
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001134 ArrayRef<const uint8_t> cfi_ref;
1135 if (cfi_info.get() != nullptr) {
1136 cfi_ref = ArrayRef<const uint8_t>(*cfi_info);
1137 }
1138 return CompiledMethod::SwapAllocCompiledMethod(
1139 cu_->compiler_driver, cu_->instruction_set,
1140 ArrayRef<const uint8_t>(code_buffer_),
1141 frame_size_, core_spill_mask_, fp_spill_mask_,
1142 &src_mapping_table_,
1143 ArrayRef<const uint8_t>(encoded_mapping_table_),
1144 ArrayRef<const uint8_t>(vmap_encoder.GetData()),
1145 ArrayRef<const uint8_t>(native_gc_map_),
1146 cfi_ref,
1147 ArrayRef<LinkerPatch>(patches_));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148}
1149
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001150size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1151 // Chose a reasonably small value in order to contain stack growth.
1152 // Backends that are smarter about spill region can return larger values.
1153 const size_t max_compiler_temps = 10;
1154 return max_compiler_temps;
1155}
1156
1157size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1158 // By default assume that the Mir2Lir will need one slot for each temporary.
1159 // If the backend can better determine temps that have non-overlapping ranges and
1160 // temps that do not need spilled, it can actually provide a small region.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001161 mir_graph_->CommitCompilerTemps();
1162 return mir_graph_->GetNumBytesForSpecialTemps() + mir_graph_->GetMaximumBytesForNonSpecialTemps();
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001163}
1164
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165int Mir2Lir::ComputeFrameSize() {
1166 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001167 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1168 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1169 + sizeof(uint32_t) // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001170 + mir_graph_->GetNumOfLocalCodeVRs() * sizeof(uint32_t)
1171 + mir_graph_->GetNumOfOutVRs() * sizeof(uint32_t)
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001172 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001174 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175}
1176
1177/*
1178 * Append an LIR instruction to the LIR list maintained by a compilation
1179 * unit
1180 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001181void Mir2Lir::AppendLIR(LIR* lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001182 if (first_lir_insn_ == nullptr) {
1183 DCHECK(last_lir_insn_ == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 last_lir_insn_ = first_lir_insn_ = lir;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001185 lir->prev = lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001186 } else {
1187 last_lir_insn_->next = lir;
1188 lir->prev = last_lir_insn_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001189 lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 last_lir_insn_ = lir;
1191 }
1192}
1193
1194/*
1195 * Insert an LIR instruction before the current instruction, which cannot be the
1196 * first instruction.
1197 *
1198 * prev_lir <-> new_lir <-> current_lir
1199 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001200void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001201 DCHECK(current_lir->prev != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 LIR *prev_lir = current_lir->prev;
1203
1204 prev_lir->next = new_lir;
1205 new_lir->prev = prev_lir;
1206 new_lir->next = current_lir;
1207 current_lir->prev = new_lir;
1208}
1209
1210/*
1211 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001212 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 *
1214 * current_lir -> new_lir -> old_next
1215 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001216void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 new_lir->prev = current_lir;
1218 new_lir->next = current_lir->next;
1219 current_lir->next = new_lir;
1220 new_lir->next->prev = new_lir;
1221}
1222
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001223bool Mir2Lir::PartiallyIntersects(RegLocation rl_src, RegLocation rl_dest) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001224 DCHECK(rl_src.wide);
1225 DCHECK(rl_dest.wide);
1226 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1227}
1228
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001229bool Mir2Lir::Intersects(RegLocation rl_src, RegLocation rl_dest) {
1230 DCHECK(rl_src.wide);
1231 DCHECK(rl_dest.wide);
1232 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) <= 1);
1233}
1234
buzbee2700f7e2014-03-07 09:46:20 -08001235LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001236 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001237 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001238 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1239 if (compare != nullptr) {
1240 *compare = inst;
1241 }
Mark Mendell766e9292014-01-27 07:55:47 -08001242 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1243 return branch;
1244}
1245
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001246void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001247 slow_paths_.push_back(slowpath);
Serguei Katkov589e0462014-09-05 18:37:22 +07001248 ResetDefTracking();
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001249}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001250
Jeff Hao49161ce2014-03-12 11:05:25 -07001251void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1252 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001253 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001254 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001255 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001256 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001257 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1258 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1259 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001260 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001261 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001262 // Loads a code pointer. Code from oat file can be mapped anywhere.
Vladimir Markof6737f72015-03-23 17:05:14 +00001263 OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001264 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001265 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001266}
1267
Jeff Hao49161ce2014-03-12 11:05:25 -07001268void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1269 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001270 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001271 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001272 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001273 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001274 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1275 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1276 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001277 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001278 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001279 // Loads an ArtMethod pointer, which is a reference as it lives in the heap.
Vladimir Markof6737f72015-03-23 17:05:14 +00001280 OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001281 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001282 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001283}
1284
Fred Shihe7f82e22014-08-06 10:46:37 -07001285void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1286 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001287 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001288 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001289 if (data_target == nullptr) {
1290 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001291 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001292 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001293 // Loads a Class pointer, which is a reference as it lives in the heap.
Vladimir Markof6737f72015-03-23 17:05:14 +00001294 OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001295}
1296
Tong Shen547cdfd2014-08-05 01:54:19 -07001297std::vector<uint8_t>* Mir2Lir::ReturnFrameDescriptionEntry() {
Mark Mendellae9fd932014-02-10 16:14:35 -08001298 // Default case is to do nothing.
1299 return nullptr;
1300}
1301
buzbee2700f7e2014-03-07 09:46:20 -08001302RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001303 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001304 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001305 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001306 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1307 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1308 info_lo->SetIsWide(false);
1309 info_hi->SetIsWide(false);
1310 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001311 } else {
buzbee85089dd2014-05-25 15:10:52 -07001312 RegisterInfo* info = GetRegInfo(loc.reg);
1313 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1314 DCHECK(info_new != nullptr);
1315 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1316 info->MarkDead();
1317 info_new->MarkLive(loc.s_reg_low);
1318 }
1319 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001320 }
buzbee85089dd2014-05-25 15:10:52 -07001321 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001322 }
buzbee85089dd2014-05-25 15:10:52 -07001323 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001324 return loc;
1325}
1326
Mark Mendelld65c51a2014-04-29 16:55:20 -04001327void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001328 UNUSED(bb, mir);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001329 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001330 UNREACHABLE();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001331}
1332
Vladimir Marko767c7522015-03-20 12:47:30 +00001333void Mir2Lir::InitReferenceVRegs(BasicBlock* bb, BitVector* references) {
1334 // Mark the references coming from the first predecessor.
1335 DCHECK(bb != nullptr);
1336 DCHECK(bb->block_type == kEntryBlock || !bb->predecessors.empty());
1337 BasicBlock* first_bb =
1338 (bb->block_type == kEntryBlock) ? bb : mir_graph_->GetBasicBlock(bb->predecessors[0]);
1339 DCHECK(first_bb != nullptr);
1340 DCHECK(first_bb->data_flow_info != nullptr);
1341 DCHECK(first_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1342 const int32_t* first_vreg_to_ssa_map = first_bb->data_flow_info->vreg_to_ssa_map_exit;
1343 references->ClearAllBits();
1344 for (uint32_t vreg = 0, num_vregs = mir_graph_->GetNumOfCodeVRs(); vreg != num_vregs; ++vreg) {
1345 int32_t sreg = first_vreg_to_ssa_map[vreg];
1346 if (sreg != INVALID_SREG && mir_graph_->reg_location_[sreg].ref &&
1347 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[sreg])) {
1348 references->SetBit(vreg);
1349 }
1350 }
1351 // Unmark the references that are merging with a different value.
1352 for (size_t i = 1u, num_pred = bb->predecessors.size(); i < num_pred; ++i) {
1353 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(bb->predecessors[i]);
1354 DCHECK(pred_bb != nullptr);
1355 DCHECK(pred_bb->data_flow_info != nullptr);
1356 DCHECK(pred_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1357 const int32_t* pred_vreg_to_ssa_map = pred_bb->data_flow_info->vreg_to_ssa_map_exit;
1358 for (uint32_t vreg : references->Indexes()) {
1359 if (first_vreg_to_ssa_map[vreg] != pred_vreg_to_ssa_map[vreg]) {
1360 // NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
1361 // so clearing the bit has no effect on the iterator.
1362 references->ClearBit(vreg);
1363 }
1364 }
1365 }
1366 if (bb->block_type != kEntryBlock && bb->first_mir_insn != nullptr &&
1367 static_cast<int>(bb->first_mir_insn->dalvikInsn.opcode) == kMirOpCheckPart2) {
1368 // In Mir2Lir::MethodBlockCodeGen() we have artificially moved the throwing
1369 // instruction to the previous block. However, the MIRGraph data used above
1370 // doesn't reflect that, so we still need to process that MIR insn here.
1371 DCHECK_EQ(bb->predecessors.size(), 1u);
1372 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(bb->predecessors[0]);
1373 DCHECK(pred_bb != nullptr);
1374 DCHECK(pred_bb->last_mir_insn != nullptr);
1375 UpdateReferenceVRegsLocal(nullptr, pred_bb->last_mir_insn, references);
1376 }
1377}
1378
1379bool Mir2Lir::UpdateReferenceVRegsLocal(MIR* mir, MIR* prev_mir, BitVector* references) {
1380 DCHECK(mir == nullptr || mir->bb == prev_mir->bb);
1381 DCHECK(prev_mir != nullptr);
1382 while (prev_mir != nullptr) {
1383 if (prev_mir == mir) {
1384 return true;
1385 }
1386 const size_t num_defs = prev_mir->ssa_rep->num_defs;
1387 const int32_t* defs = prev_mir->ssa_rep->defs;
1388 if (num_defs == 1u && mir_graph_->reg_location_[defs[0]].ref &&
1389 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[defs[0]])) {
1390 references->SetBit(mir_graph_->SRegToVReg(defs[0]));
1391 } else {
1392 for (size_t i = 0u; i != num_defs; ++i) {
1393 references->ClearBit(mir_graph_->SRegToVReg(defs[i]));
1394 }
1395 }
1396 prev_mir = prev_mir->next;
1397 }
1398 return false;
1399}
1400
1401void Mir2Lir::UpdateReferenceVRegs(MIR* mir, MIR* prev_mir, BitVector* references) {
1402 if (mir == nullptr) {
1403 // Safepoint in entry sequence.
1404 InitReferenceVRegs(mir_graph_->GetEntryBlock(), references);
1405 return;
1406 }
1407 if (IsInstructionReturn(mir->dalvikInsn.opcode) ||
1408 mir->dalvikInsn.opcode == Instruction::RETURN_VOID_NO_BARRIER) {
1409 references->ClearAllBits();
1410 if (mir->dalvikInsn.opcode == Instruction::RETURN_OBJECT) {
1411 references->SetBit(mir_graph_->SRegToVReg(mir->ssa_rep->uses[0]));
1412 }
1413 return;
1414 }
1415 if (prev_mir != nullptr && mir->bb == prev_mir->bb &&
1416 UpdateReferenceVRegsLocal(mir, prev_mir, references)) {
1417 return;
1418 }
1419 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
1420 DCHECK(bb != nullptr);
1421 InitReferenceVRegs(bb, references);
1422 bool success = UpdateReferenceVRegsLocal(mir, bb->first_mir_insn, references);
1423 DCHECK(success) << "MIR @0x" << std::hex << mir->offset << " not in BB#" << std::dec << mir->bb;
1424}
1425
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001426} // namespace art