blob: 3285195b40ad6abc018556b51b32cfc3f11a9dae [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 << " @ "
225 << reinterpret_cast<char*>(UnwrapPointer(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 &&
419 UnwrapPointer(data_target->operands[1]) == method.dex_file) {
420 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 &&
431 UnwrapPointer(data_target->operands[1]) == &dex_file) {
432 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];
494 const DexFile* target_dex_file =
495 reinterpret_cast<const DexFile*>(UnwrapPointer(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];
504 const DexFile* target_dex_file =
505 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Vladimir Markof4da6752014-08-01 19:04:18 +0100506 patches_.push_back(LinkerPatch::MethodPatch(code_buffer_.size(),
507 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000508 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 data_lir = NEXT_LIR(data_lir);
510 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800511 // Push class literals.
512 data_lir = class_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700513 while (data_lir != nullptr) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100514 uint32_t target_type_idx = data_lir->operands[0];
Fred Shihe7f82e22014-08-06 10:46:37 -0700515 const DexFile* class_dex_file =
516 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Vladimir Markof4da6752014-08-01 19:04:18 +0100517 patches_.push_back(LinkerPatch::TypePatch(code_buffer_.size(),
518 class_dex_file, target_type_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000519 PushUnpatchedReference(&code_buffer_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800520 data_lir = NEXT_LIR(data_lir);
521 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522}
523
524/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700525void Mir2Lir::InstallSwitchTables() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100526 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000527 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 /*
529 * For Arm, our reference point is the address of the bx
530 * instruction that does the launch, so we have to subtract
531 * the auto pc-advance. For other targets the reference point
532 * is a label, so we can use the offset as-is.
533 */
534 int bx_offset = INVALID_OFFSET;
535 switch (cu_->instruction_set) {
536 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700537 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 bx_offset = tab_rec->anchor->offset + 4;
539 break;
540 case kX86:
541 bx_offset = 0;
542 break;
Mark Mendell27dee8b2014-12-01 19:06:12 -0500543 case kX86_64:
544 // RIP relative to switch table.
545 bx_offset = tab_rec->offset;
546 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100547 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100549 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 bx_offset = tab_rec->anchor->offset;
551 break;
552 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
553 }
554 if (cu_->verbose) {
555 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
556 }
557 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800558 DCHECK(tab_rec->switch_mir != nullptr);
559 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
560 DCHECK(bb != nullptr);
561 int elems = 0;
562 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
563 int key = successor_block_info->key;
564 int target = successor_block_info->block;
565 LIR* boundary_lir = InsertCaseLabel(target, key);
566 DCHECK(boundary_lir != nullptr);
567 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000568 Push32(&code_buffer_, key);
569 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 if (cu_->verbose) {
571 LOG(INFO) << " Case[" << elems << "] key: 0x"
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800572 << std::hex << key << ", disp: 0x"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 << std::hex << disp;
574 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800575 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800577 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 } else {
579 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
580 static_cast<int>(Instruction::kPackedSwitchSignature));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800581 DCHECK(tab_rec->switch_mir != nullptr);
582 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
583 DCHECK(bb != nullptr);
584 int elems = 0;
585 int low_key = s4FromSwitchData(&tab_rec->table[2]);
586 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
587 int key = successor_block_info->key;
588 DCHECK_EQ(elems + low_key, key);
589 int target = successor_block_info->block;
590 LIR* boundary_lir = InsertCaseLabel(target, key);
591 DCHECK(boundary_lir != nullptr);
592 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000593 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 if (cu_->verbose) {
595 LOG(INFO) << " Case[" << elems << "] disp: 0x"
596 << std::hex << disp;
597 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800598 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800600 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 }
602 }
603}
604
605/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700606void Mir2Lir::InstallFillArrayData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100607 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000608 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700610 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
611 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 }
613 }
614}
615
buzbee0d829482013-10-11 15:24:55 -0700616static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700617 for (; lir != nullptr; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 lir->offset = offset;
619 offset += 4;
620 }
621 return offset;
622}
623
Ian Rogersff093b32014-04-30 19:04:27 -0700624static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
625 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700626 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700627 offset = RoundUp(offset, element_size);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700628 for (; lir != nullptr; lir = lir->next) {
buzbee0d829482013-10-11 15:24:55 -0700629 lir->offset = offset;
630 offset += element_size;
631 }
632 return offset;
633}
634
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700636bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000637 MappingTable table(&encoded_mapping_table_[0]);
638 std::vector<uint32_t> dex_pcs;
639 dex_pcs.reserve(table.DexToPcSize());
640 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
641 dex_pcs.push_back(it.DexPc());
642 }
643 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
644 std::sort(dex_pcs.begin(), dex_pcs.end());
645
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000647 auto it = dex_pcs.begin(), end = dex_pcs.end();
648 for (uint32_t dex_pc : mir_graph_->catches_) {
649 while (it != end && *it < dex_pc) {
650 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
651 ++it;
652 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000654 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
656 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000657 } else {
658 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 }
660 }
661 if (!success) {
662 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
663 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000664 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 }
666 return success;
667}
668
669
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700670void Mir2Lir::CreateMappingTables() {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700671 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
672
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000673 uint32_t pc2dex_data_size = 0u;
674 uint32_t pc2dex_entries = 0u;
675 uint32_t pc2dex_offset = 0u;
676 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700677 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000678 uint32_t dex2pc_data_size = 0u;
679 uint32_t dex2pc_entries = 0u;
680 uint32_t dex2pc_offset = 0u;
681 uint32_t dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700682 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700683 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000685 pc2dex_entries += 1;
686 DCHECK(pc2dex_offset <= tgt_lir->offset);
687 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
688 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
689 static_cast<int32_t>(pc2dex_dalvik_offset));
690 pc2dex_offset = tgt_lir->offset;
691 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 }
693 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000694 dex2pc_entries += 1;
695 DCHECK(dex2pc_offset <= tgt_lir->offset);
696 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
697 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
698 static_cast<int32_t>(dex2pc_dalvik_offset));
699 dex2pc_offset = tgt_lir->offset;
700 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 }
702 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000703
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700704 if (generate_src_map) {
705 src_mapping_table_.reserve(pc2dex_src_entries);
706 }
707
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000708 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
709 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
710 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000711 encoded_mapping_table_.resize(data_size);
712 uint8_t* write_pos = &encoded_mapping_table_[0];
713 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
714 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
715 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
716 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000717
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000718 pc2dex_offset = 0u;
719 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000720 dex2pc_offset = 0u;
721 dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700722 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700723 if (generate_src_map && !tgt_lir->flags.is_nop) {
724 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
725 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
726 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000727 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
728 DCHECK(pc2dex_offset <= tgt_lir->offset);
729 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
730 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
731 static_cast<int32_t>(pc2dex_dalvik_offset));
732 pc2dex_offset = tgt_lir->offset;
733 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
734 }
735 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
736 DCHECK(dex2pc_offset <= tgt_lir->offset);
737 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
738 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
739 static_cast<int32_t>(dex2pc_dalvik_offset));
740 dex2pc_offset = tgt_lir->offset;
741 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
742 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000743 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000744 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
745 hdr_data_size + pc2dex_data_size);
746 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000747
Ian Rogers96faf5b2013-08-09 22:05:32 -0700748 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000749 CHECK(VerifyCatchEntries());
750
Ian Rogers96faf5b2013-08-09 22:05:32 -0700751 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000752 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700753 CHECK_EQ(table.TotalSize(), total_entries);
754 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000755 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000756 auto it2 = table.DexToPcBegin();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700757 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000758 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
759 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
760 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
761 ++it;
762 }
763 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
764 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
765 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
766 ++it2;
767 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700768 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000769 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000770 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700771 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772}
773
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko767c7522015-03-20 12:47:30 +0000775 if (UNLIKELY((cu_->disable_opt & (1u << kPromoteRegs)) != 0u)) {
776 // If we're not promoting to physical registers, it's safe to use the verifier's notion of
777 // references. (We disable register promotion when type inference finds a type conflict and
778 // in that the case we defer to the verifier to avoid using the compiler's conflicting info.)
779 CreateNativeGcMapWithoutRegisterPromotion();
780 return;
781 }
782
783 ArenaBitVector* references = new (arena_) ArenaBitVector(arena_, mir_graph_->GetNumSSARegs(),
784 false);
785
786 // Calculate max native offset and max reference vreg.
787 MIR* prev_mir = nullptr;
788 int max_ref_vreg = -1;
789 CodeOffset max_native_offset = 0u;
790 for (const auto& entry : safepoints_) {
791 uint32_t native_offset = entry.first->offset;
792 max_native_offset = std::max(max_native_offset, native_offset);
793 MIR* mir = entry.second;
794 UpdateReferenceVRegs(mir, prev_mir, references);
795 max_ref_vreg = std::max(max_ref_vreg, references->GetHighestBitSet());
796 prev_mir = mir;
797 }
798
799 // Build the GC map.
800 uint32_t reg_width = static_cast<uint32_t>((max_ref_vreg + 8) / 8);
801 GcMapBuilder native_gc_map_builder(&native_gc_map_,
802 safepoints_.size(),
803 max_native_offset, reg_width);
804#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN)
805 ArenaVector<uint8_t> references_buffer(arena_->Adapter());
806 references_buffer.resize(reg_width);
807#endif
808 for (const auto& entry : safepoints_) {
809 uint32_t native_offset = entry.first->offset;
810 MIR* mir = entry.second;
811 UpdateReferenceVRegs(mir, prev_mir, references);
812#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN)
813 // Big-endian or unknown endianness, manually translate the bit vector data.
814 const auto* raw_storage = references->GetRawStorage();
815 for (size_t i = 0; i != reg_width; ++i) {
816 references_buffer[i] = static_cast<uint8_t>(
817 raw_storage[i / sizeof(raw_storage[0])] >> (8u * (i % sizeof(raw_storage[0]))));
818 }
819 native_gc_map_builder.AddEntry(native_offset, &references_buffer[0]);
820#else
821 // For little-endian, the bytes comprising the bit vector's raw storage are what we need.
822 native_gc_map_builder.AddEntry(native_offset,
823 reinterpret_cast<const uint8_t*>(references->GetRawStorage()));
824#endif
825 prev_mir = mir;
826 }
827}
828
829void Mir2Lir::CreateNativeGcMapWithoutRegisterPromotion() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000830 DCHECK(!encoded_mapping_table_.empty());
831 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000833 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
834 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 if (native_offset > max_native_offset) {
836 max_native_offset = native_offset;
837 }
838 }
839 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000840 const std::vector<uint8_t>& gc_map_raw =
841 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
842 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
843 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000845 GcMapBuilder native_gc_map_builder(&native_gc_map_,
846 mapping_table.PcToDexSize(),
847 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848
Vladimir Marko06606b92013-12-02 15:31:08 +0000849 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
850 uint32_t native_offset = it.NativePcOffset();
851 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700853 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
Dave Allisonf9439142014-03-27 15:10:22 -0700854 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 native_gc_map_builder.AddEntry(native_offset, references);
856 }
Mathieu Chartierab972ef2014-12-03 17:38:22 -0800857
858 // Maybe not necessary, but this could help prevent errors where we access the verified method
859 // after it has been deleted.
860 mir_graph_->GetCurrentDexCompilationUnit()->ClearVerifiedMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861}
862
863/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700864int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100866 constexpr unsigned int ptr_size = sizeof(uint32_t);
Andreas Gampe785d2f22014-11-03 22:57:30 -0800867 static_assert(ptr_size >= sizeof(mirror::HeapReference<mirror::Object>),
868 "Pointer size cannot hold a heap reference");
Ian Rogersff093b32014-04-30 19:04:27 -0700869 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
870 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
871 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 return offset;
873}
874
buzbee0d829482013-10-11 15:24:55 -0700875int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100876 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 tab_rec->offset = offset;
878 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
879 offset += tab_rec->table[1] * (sizeof(int) * 2);
880 } else {
881 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
882 static_cast<int>(Instruction::kPackedSwitchSignature));
883 offset += tab_rec->table[1] * sizeof(int);
884 }
885 }
886 return offset;
887}
888
buzbee0d829482013-10-11 15:24:55 -0700889int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100890 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 tab_rec->offset = offset;
892 offset += tab_rec->size;
893 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700894 offset = RoundUp(offset, 4);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100895 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 return offset;
897}
898
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899/*
900 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700901 * offset vaddr if pretty-printing, otherise use the standard block
902 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700903 * branch table during the assembly phase. All resource flags
904 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 */
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800906LIR* Mir2Lir::InsertCaseLabel(uint32_t bbid, int keyVal) {
907 LIR* boundary_lir = &block_label_list_[bbid];
buzbeeb48819d2013-09-14 16:15:25 -0700908 LIR* res = boundary_lir;
909 if (cu_->verbose) {
910 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000911 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800912 BasicBlock* bb = mir_graph_->GetBasicBlock(bbid);
913 DCHECK(bb != nullptr);
914 new_label->dalvik_offset = bb->start_offset;
buzbeeb48819d2013-09-14 16:15:25 -0700915 new_label->opcode = kPseudoCaseLabel;
916 new_label->operands[0] = keyVal;
917 new_label->flags.fixup = kFixupLabel;
918 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100919 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700920 InsertLIRAfter(boundary_lir, new_label);
buzbeeb48819d2013-09-14 16:15:25 -0700921 }
922 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923}
924
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700925void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 /*
927 * Sparse switch data format:
928 * ushort ident = 0x0200 magic value
929 * ushort size number of entries in the table; > 0
930 * int keys[size] keys, sorted low-to-high; 32-bit aligned
931 * int targets[size] branch targets, relative to switch opcode
932 *
933 * Total size is (2+size*4) 16-bit code units.
934 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700935 uint16_t ident = table[0];
936 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700937 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
938 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
940 << ", entries: " << std::dec << entries;
941 for (int i = 0; i < entries; i++) {
942 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
943 }
944}
945
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700946void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700947 /*
948 * Packed switch data format:
949 * ushort ident = 0x0100 magic value
950 * ushort size number of entries in the table
951 * int first_key first (and lowest) switch case value
952 * int targets[size] branch targets, relative to switch opcode
953 *
954 * Total size is (4+size*2) 16-bit code units.
955 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700957 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 int entries = table[1];
959 int low_key = s4FromSwitchData(&table[2]);
960 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
961 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
962 for (int i = 0; i < entries; i++) {
963 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
964 << targets[i];
965 }
966}
967
buzbee252254b2013-09-08 16:20:53 -0700968/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700969void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700970 UNUSED(offset);
buzbee0d829482013-10-11 15:24:55 -0700971 // NOTE: only used for debug listings.
972 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973}
974
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975// Convert relation of src1/src2 to src2/src1
976ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
977 ConditionCode res;
978 switch (before) {
979 case kCondEq: res = kCondEq; break;
980 case kCondNe: res = kCondNe; break;
981 case kCondLt: res = kCondGt; break;
982 case kCondGt: res = kCondLt; break;
983 case kCondLe: res = kCondGe; break;
984 case kCondGe: res = kCondLe; break;
985 default:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700987 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 }
989 return res;
990}
991
Vladimir Markoa1a70742014-03-03 10:28:05 +0000992ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
993 ConditionCode res;
994 switch (before) {
995 case kCondEq: res = kCondNe; break;
996 case kCondNe: res = kCondEq; break;
997 case kCondLt: res = kCondGe; break;
998 case kCondGt: res = kCondLe; break;
999 case kCondLe: res = kCondGt; break;
1000 case kCondGe: res = kCondLt; break;
1001 default:
Vladimir Markoa1a70742014-03-03 10:28:05 +00001002 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001003 UNREACHABLE();
Vladimir Markoa1a70742014-03-03 10:28:05 +00001004 }
1005 return res;
1006}
1007
Brian Carlstrom7940e442013-07-12 13:46:57 -07001008// TODO: move to mir_to_lir.cc
1009Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Andreas Gampe9c462082015-01-27 14:31:40 -08001010 : literal_list_(nullptr),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011 method_literal_list_(nullptr),
1012 class_literal_list_(nullptr),
1013 code_literal_list_(nullptr),
1014 first_fixup_(nullptr),
Andreas Gampe9c462082015-01-27 14:31:40 -08001015 arena_(arena),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 cu_(cu),
1017 mir_graph_(mir_graph),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001018 switch_tables_(arena->Adapter(kArenaAllocSwitchTable)),
1019 fill_array_data_(arena->Adapter(kArenaAllocFillArrayData)),
1020 tempreg_info_(arena->Adapter()),
1021 reginfo_map_(arena->Adapter()),
1022 pointer_storage_(arena->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 data_offset_(0),
1024 total_size_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001025 block_label_list_(nullptr),
1026 promotion_map_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 current_dalvik_offset_(0),
Vladimir Marko767c7522015-03-20 12:47:30 +00001028 current_mir_(nullptr),
buzbeeb48819d2013-09-14 16:15:25 -07001029 estimated_native_code_size_(0),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001030 reg_pool_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 live_sreg_(0),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001032 code_buffer_(mir_graph->GetArena()->Adapter()),
1033 encoded_mapping_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko8081d2b2014-07-31 15:33:43 +01001034 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1035 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001036 native_gc_map_(mir_graph->GetArena()->Adapter()),
Vladimir Markof4da6752014-08-01 19:04:18 +01001037 patches_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 num_core_spills_(0),
1039 num_fp_spills_(0),
1040 frame_size_(0),
1041 core_spill_mask_(0),
1042 fp_spill_mask_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001043 first_lir_insn_(nullptr),
1044 last_lir_insn_(nullptr),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001045 slow_paths_(arena->Adapter(kArenaAllocSlowPaths)),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001046 mem_ref_type_(ResourceMask::kHeapRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001047 mask_cache_(arena),
Vladimir Marko767c7522015-03-20 12:47:30 +00001048 safepoints_(arena->Adapter()),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001049 in_to_reg_storage_mapping_(arena) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001050 switch_tables_.reserve(4);
1051 fill_array_data_.reserve(4);
1052 tempreg_info_.reserve(20);
1053 reginfo_map_.reserve(RegStorage::kMaxRegs);
1054 pointer_storage_.reserve(128);
1055 slow_paths_.reserve(32);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001056 // Reserve pointer id 0 for nullptr.
1057 size_t null_idx = WrapPointer(nullptr);
buzbee0d829482013-10-11 15:24:55 -07001058 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059}
1060
1061void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001062 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1064
1065 /* Allocate Registers using simple local allocation scheme */
1066 SimpleRegAlloc();
1067
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001068 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001069 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001070 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001071 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001073 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1074 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 MethodMIR2LIR();
1076 }
1077
1078 /* Method is not empty */
1079 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 /* Convert LIR into machine code. */
1081 AssembleLIR();
1082
buzbeeb01bf152014-05-13 15:59:07 -07001083 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 CodegenDump();
1085 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087}
1088
1089CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001090 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1091 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001093 // Prefix the encoded data with its size.
1094 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1095 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1096 vmap_encoder.PushBackUnsigned(size);
1097 // Core regs may have been inserted out of order - sort first.
1098 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1099 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1100 // Copy, stripping out the phys register sort key.
1101 vmap_encoder.PushBackUnsigned(
1102 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1103 }
1104 // Push a marker to take place of lr.
1105 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001106 if (cu_->instruction_set == kThumb2) {
1107 // fp regs already sorted.
1108 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1109 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1110 }
1111 } else {
1112 // For other platforms regs may have been inserted out of order - sort first.
1113 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1114 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1115 // Copy, stripping out the phys register sort key.
1116 vmap_encoder.PushBackUnsigned(
1117 ~(-1 << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
1118 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001119 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001121 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1122 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001123 DCHECK_EQ(core_vmap_table_.size(), 0u);
1124 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1125 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001127
Vladimir Markof4da6752014-08-01 19:04:18 +01001128 // Sort patches by literal offset for better deduplication.
1129 std::sort(patches_.begin(), patches_.end(), [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
1130 return lhs.LiteralOffset() < rhs.LiteralOffset();
1131 });
1132
Andreas Gamped37f9192015-03-04 14:00:56 -08001133 std::unique_ptr<std::vector<uint8_t>> cfi_info(
1134 cu_->compiler_driver->GetCompilerOptions().GetGenerateGDBInformation() ?
1135 ReturnFrameDescriptionEntry() :
1136 nullptr);
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001137 ArrayRef<const uint8_t> cfi_ref;
1138 if (cfi_info.get() != nullptr) {
1139 cfi_ref = ArrayRef<const uint8_t>(*cfi_info);
1140 }
1141 return CompiledMethod::SwapAllocCompiledMethod(
1142 cu_->compiler_driver, cu_->instruction_set,
1143 ArrayRef<const uint8_t>(code_buffer_),
1144 frame_size_, core_spill_mask_, fp_spill_mask_,
1145 &src_mapping_table_,
1146 ArrayRef<const uint8_t>(encoded_mapping_table_),
1147 ArrayRef<const uint8_t>(vmap_encoder.GetData()),
1148 ArrayRef<const uint8_t>(native_gc_map_),
1149 cfi_ref,
1150 ArrayRef<LinkerPatch>(patches_));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151}
1152
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001153size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1154 // Chose a reasonably small value in order to contain stack growth.
1155 // Backends that are smarter about spill region can return larger values.
1156 const size_t max_compiler_temps = 10;
1157 return max_compiler_temps;
1158}
1159
1160size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1161 // By default assume that the Mir2Lir will need one slot for each temporary.
1162 // If the backend can better determine temps that have non-overlapping ranges and
1163 // temps that do not need spilled, it can actually provide a small region.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001164 mir_graph_->CommitCompilerTemps();
1165 return mir_graph_->GetNumBytesForSpecialTemps() + mir_graph_->GetMaximumBytesForNonSpecialTemps();
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001166}
1167
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168int Mir2Lir::ComputeFrameSize() {
1169 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001170 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1171 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1172 + sizeof(uint32_t) // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001173 + mir_graph_->GetNumOfLocalCodeVRs() * sizeof(uint32_t)
1174 + mir_graph_->GetNumOfOutVRs() * sizeof(uint32_t)
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001175 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001177 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178}
1179
1180/*
1181 * Append an LIR instruction to the LIR list maintained by a compilation
1182 * unit
1183 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001184void Mir2Lir::AppendLIR(LIR* lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001185 if (first_lir_insn_ == nullptr) {
1186 DCHECK(last_lir_insn_ == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 last_lir_insn_ = first_lir_insn_ = lir;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001188 lir->prev = lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 } else {
1190 last_lir_insn_->next = lir;
1191 lir->prev = last_lir_insn_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001192 lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 last_lir_insn_ = lir;
1194 }
1195}
1196
1197/*
1198 * Insert an LIR instruction before the current instruction, which cannot be the
1199 * first instruction.
1200 *
1201 * prev_lir <-> new_lir <-> current_lir
1202 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001203void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001204 DCHECK(current_lir->prev != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 LIR *prev_lir = current_lir->prev;
1206
1207 prev_lir->next = new_lir;
1208 new_lir->prev = prev_lir;
1209 new_lir->next = current_lir;
1210 current_lir->prev = new_lir;
1211}
1212
1213/*
1214 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001215 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 *
1217 * current_lir -> new_lir -> old_next
1218 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001219void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 new_lir->prev = current_lir;
1221 new_lir->next = current_lir->next;
1222 current_lir->next = new_lir;
1223 new_lir->next->prev = new_lir;
1224}
1225
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001226bool Mir2Lir::PartiallyIntersects(RegLocation rl_src, RegLocation rl_dest) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001227 DCHECK(rl_src.wide);
1228 DCHECK(rl_dest.wide);
1229 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1230}
1231
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001232bool Mir2Lir::Intersects(RegLocation rl_src, RegLocation rl_dest) {
1233 DCHECK(rl_src.wide);
1234 DCHECK(rl_dest.wide);
1235 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) <= 1);
1236}
1237
buzbee2700f7e2014-03-07 09:46:20 -08001238LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001239 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001240 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001241 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1242 if (compare != nullptr) {
1243 *compare = inst;
1244 }
Mark Mendell766e9292014-01-27 07:55:47 -08001245 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1246 return branch;
1247}
1248
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001249void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001250 slow_paths_.push_back(slowpath);
Serguei Katkov589e0462014-09-05 18:37:22 +07001251 ResetDefTracking();
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001252}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001253
Jeff Hao49161ce2014-03-12 11:05:25 -07001254void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1255 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001256 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001257 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001258 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001259 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001260 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1261 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1262 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001263 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001264 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001265 // Loads a code pointer. Code from oat file can be mapped anywhere.
1266 LIR* load_pc_rel = OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001267 AppendLIR(load_pc_rel);
1268 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001269 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001270}
1271
Jeff Hao49161ce2014-03-12 11:05:25 -07001272void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1273 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001274 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001275 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001276 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001277 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001278 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1279 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1280 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001281 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001282 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001283 // Loads an ArtMethod pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001284 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001285 AppendLIR(load_pc_rel);
1286 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001287 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001288}
1289
Fred Shihe7f82e22014-08-06 10:46:37 -07001290void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1291 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001292 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001293 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001294 if (data_target == nullptr) {
1295 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001296 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001297 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001298 // Loads a Class pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001299 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001300 AppendLIR(load_pc_rel);
1301}
1302
Tong Shen547cdfd2014-08-05 01:54:19 -07001303std::vector<uint8_t>* Mir2Lir::ReturnFrameDescriptionEntry() {
Mark Mendellae9fd932014-02-10 16:14:35 -08001304 // Default case is to do nothing.
1305 return nullptr;
1306}
1307
buzbee2700f7e2014-03-07 09:46:20 -08001308RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001309 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001310 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001311 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001312 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1313 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1314 info_lo->SetIsWide(false);
1315 info_hi->SetIsWide(false);
1316 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001317 } else {
buzbee85089dd2014-05-25 15:10:52 -07001318 RegisterInfo* info = GetRegInfo(loc.reg);
1319 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1320 DCHECK(info_new != nullptr);
1321 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1322 info->MarkDead();
1323 info_new->MarkLive(loc.s_reg_low);
1324 }
1325 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001326 }
buzbee85089dd2014-05-25 15:10:52 -07001327 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001328 }
buzbee85089dd2014-05-25 15:10:52 -07001329 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001330 return loc;
1331}
1332
Mark Mendelld65c51a2014-04-29 16:55:20 -04001333void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001334 UNUSED(bb, mir);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001335 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001336 UNREACHABLE();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001337}
1338
Vladimir Marko767c7522015-03-20 12:47:30 +00001339void Mir2Lir::InitReferenceVRegs(BasicBlock* bb, BitVector* references) {
1340 // Mark the references coming from the first predecessor.
1341 DCHECK(bb != nullptr);
1342 DCHECK(bb->block_type == kEntryBlock || !bb->predecessors.empty());
1343 BasicBlock* first_bb =
1344 (bb->block_type == kEntryBlock) ? bb : mir_graph_->GetBasicBlock(bb->predecessors[0]);
1345 DCHECK(first_bb != nullptr);
1346 DCHECK(first_bb->data_flow_info != nullptr);
1347 DCHECK(first_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1348 const int32_t* first_vreg_to_ssa_map = first_bb->data_flow_info->vreg_to_ssa_map_exit;
1349 references->ClearAllBits();
1350 for (uint32_t vreg = 0, num_vregs = mir_graph_->GetNumOfCodeVRs(); vreg != num_vregs; ++vreg) {
1351 int32_t sreg = first_vreg_to_ssa_map[vreg];
1352 if (sreg != INVALID_SREG && mir_graph_->reg_location_[sreg].ref &&
1353 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[sreg])) {
1354 references->SetBit(vreg);
1355 }
1356 }
1357 // Unmark the references that are merging with a different value.
1358 for (size_t i = 1u, num_pred = bb->predecessors.size(); i < num_pred; ++i) {
1359 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(bb->predecessors[i]);
1360 DCHECK(pred_bb != nullptr);
1361 DCHECK(pred_bb->data_flow_info != nullptr);
1362 DCHECK(pred_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1363 const int32_t* pred_vreg_to_ssa_map = pred_bb->data_flow_info->vreg_to_ssa_map_exit;
1364 for (uint32_t vreg : references->Indexes()) {
1365 if (first_vreg_to_ssa_map[vreg] != pred_vreg_to_ssa_map[vreg]) {
1366 // NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
1367 // so clearing the bit has no effect on the iterator.
1368 references->ClearBit(vreg);
1369 }
1370 }
1371 }
1372 if (bb->block_type != kEntryBlock && bb->first_mir_insn != nullptr &&
1373 static_cast<int>(bb->first_mir_insn->dalvikInsn.opcode) == kMirOpCheckPart2) {
1374 // In Mir2Lir::MethodBlockCodeGen() we have artificially moved the throwing
1375 // instruction to the previous block. However, the MIRGraph data used above
1376 // doesn't reflect that, so we still need to process that MIR insn here.
1377 DCHECK_EQ(bb->predecessors.size(), 1u);
1378 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(bb->predecessors[0]);
1379 DCHECK(pred_bb != nullptr);
1380 DCHECK(pred_bb->last_mir_insn != nullptr);
1381 UpdateReferenceVRegsLocal(nullptr, pred_bb->last_mir_insn, references);
1382 }
1383}
1384
1385bool Mir2Lir::UpdateReferenceVRegsLocal(MIR* mir, MIR* prev_mir, BitVector* references) {
1386 DCHECK(mir == nullptr || mir->bb == prev_mir->bb);
1387 DCHECK(prev_mir != nullptr);
1388 while (prev_mir != nullptr) {
1389 if (prev_mir == mir) {
1390 return true;
1391 }
1392 const size_t num_defs = prev_mir->ssa_rep->num_defs;
1393 const int32_t* defs = prev_mir->ssa_rep->defs;
1394 if (num_defs == 1u && mir_graph_->reg_location_[defs[0]].ref &&
1395 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[defs[0]])) {
1396 references->SetBit(mir_graph_->SRegToVReg(defs[0]));
1397 } else {
1398 for (size_t i = 0u; i != num_defs; ++i) {
1399 references->ClearBit(mir_graph_->SRegToVReg(defs[i]));
1400 }
1401 }
1402 prev_mir = prev_mir->next;
1403 }
1404 return false;
1405}
1406
1407void Mir2Lir::UpdateReferenceVRegs(MIR* mir, MIR* prev_mir, BitVector* references) {
1408 if (mir == nullptr) {
1409 // Safepoint in entry sequence.
1410 InitReferenceVRegs(mir_graph_->GetEntryBlock(), references);
1411 return;
1412 }
1413 if (IsInstructionReturn(mir->dalvikInsn.opcode) ||
1414 mir->dalvikInsn.opcode == Instruction::RETURN_VOID_NO_BARRIER) {
1415 references->ClearAllBits();
1416 if (mir->dalvikInsn.opcode == Instruction::RETURN_OBJECT) {
1417 references->SetBit(mir_graph_->SRegToVReg(mir->ssa_rep->uses[0]));
1418 }
1419 return;
1420 }
1421 if (prev_mir != nullptr && mir->bb == prev_mir->bb &&
1422 UpdateReferenceVRegsLocal(mir, prev_mir, references)) {
1423 return;
1424 }
1425 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
1426 DCHECK(bb != nullptr);
1427 InitReferenceVRegs(bb, references);
1428 bool success = UpdateReferenceVRegsLocal(mir, bb->first_mir_insn, references);
1429 DCHECK(success) << "MIR @0x" << std::hex << mir->offset << " not in BB#" << std::dec << mir->bb;
1430}
1431
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001432} // namespace art