blob: 86bb69d01eb210f015bfc89c53f385c26eacd82b [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe0b9203e2015-01-22 20:39:27 -080017#include "mir_to_lir-inl.h"
18
Vladimir Marko767c7522015-03-20 12:47:30 +000019#include "base/bit_vector-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020#include "dex/mir_graph.h"
21#include "driver/compiler_driver.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070022#include "driver/compiler_options.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080023#include "driver/dex_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "dex_file-inl.h"
25#include "gc_map.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000026#include "gc_map_builder.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070027#include "mapping_table.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000028#include "dex/quick/dex_file_method_inliner.h"
29#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000030#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000031#include "dex/verified_method.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000032#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#include "verifier/dex_gc_map.h"
34#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000035#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036
37namespace art {
38
Vladimir Marko06606b92013-12-02 15:31:08 +000039namespace {
40
41/* Dump a mapping table */
42template <typename It>
43void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
44 const Signature& signature, uint32_t size, It first) {
45 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080046 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000047 descriptor, name, signature.ToString().c_str(), size));
48 std::replace(line.begin(), line.end(), ';', '_');
49 LOG(INFO) << line;
50 for (uint32_t i = 0; i != size; ++i) {
51 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
52 ++first;
53 LOG(INFO) << line;
54 }
55 LOG(INFO) <<" };\n\n";
56 }
57}
58
59} // anonymous namespace
60
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070061bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 bool res = false;
63 if (rl_src.is_const) {
64 if (rl_src.wide) {
Andreas Gampede0b9962014-08-27 14:24:42 -070065 // For wide registers, check whether we're the high partner. In that case we need to switch
66 // to the lower one for the correct value.
67 if (rl_src.high_word) {
68 rl_src.high_word = false;
69 rl_src.s_reg_low--;
70 rl_src.orig_sreg--;
71 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070073 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070075 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 }
77 } else {
78 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070079 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070081 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 }
83 }
84 }
85 return res;
86}
87
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070088void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070089 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010090 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010092 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +000093 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
94 safepoints_.emplace_back(safepoint_pc, current_mir_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070095}
96
Andreas Gampe3c12c512014-06-24 18:46:29 +000097void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
98 DCHECK(!after->flags.use_def_invalid);
99 after->u.m.def_mask = &kEncodeAll;
100 // As NewLIR0 uses Append, we need to create the LIR by hand.
101 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
102 if (after->next == nullptr) {
103 DCHECK_EQ(after, last_lir_insn_);
104 AppendLIR(safepoint_pc);
105 } else {
106 InsertLIRAfter(after, safepoint_pc);
107 }
108 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +0000109 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
110 safepoints_.emplace_back(safepoint_pc, current_mir_);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000111}
112
buzbee252254b2013-09-08 16:20:53 -0700113/* Remove a LIR from the list. */
114void Mir2Lir::UnlinkLIR(LIR* lir) {
115 if (UNLIKELY(lir == first_lir_insn_)) {
116 first_lir_insn_ = lir->next;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117 if (lir->next != nullptr) {
118 lir->next->prev = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700119 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700120 DCHECK(lir->next == nullptr);
buzbee252254b2013-09-08 16:20:53 -0700121 DCHECK(lir == last_lir_insn_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700122 last_lir_insn_ = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700123 }
124 } else if (lir == last_lir_insn_) {
125 last_lir_insn_ = lir->prev;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700126 lir->prev->next = nullptr;
127 } else if ((lir->prev != nullptr) && (lir->next != nullptr)) {
buzbee252254b2013-09-08 16:20:53 -0700128 lir->prev->next = lir->next;
129 lir->next->prev = lir->prev;
130 }
131}
132
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700134void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700136 if (!cu_->verbose) {
137 UnlinkLIR(lir);
138 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139}
140
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700141void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700143 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100144 // TODO: Avoid the extra Arena allocation!
145 const ResourceMask** mask_ptr;
146 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700148 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700150 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100152 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100154 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 /* ..and then add back the one we need */
156 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100157 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100159 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100161 case ResourceMask::kDalvikReg:
162 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100164 case ResourceMask::kHeapRef:
165 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100167 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 /* Currently only loads can be marked as kMustNotAlias */
169 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100170 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 break;
172 default:
173 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
174 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100175 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176}
177
178/*
179 * Mark load/store instructions that access Dalvik registers through the stack.
180 */
181void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700182 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100183 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
184 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185
186 /*
187 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
188 * access.
189 */
buzbeeb48819d2013-09-14 16:15:25 -0700190 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191}
192
193/*
194 * Debugging macros
195 */
196#define DUMP_RESOURCE_MASK(X)
197
198/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700199void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 int offset = lir->offset;
201 int dest = lir->operands[0];
202 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
203
204 /* Handle pseudo-ops individually, and all regular insns as a group */
205 switch (lir->opcode) {
David Srbecky6f715892015-03-30 14:21:42 +0100206 case kPseudoPrologueBegin:
207 LOG(INFO) << "-------- PrologueBegin";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 break;
David Srbecky6f715892015-03-30 14:21:42 +0100209 case kPseudoPrologueEnd:
210 LOG(INFO) << "-------- PrologueEnd";
211 break;
212 case kPseudoEpilogueBegin:
213 LOG(INFO) << "-------- EpilogueBegin";
214 break;
215 case kPseudoEpilogueEnd:
216 LOG(INFO) << "-------- EpilogueEnd";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 break;
218 case kPseudoBarrier:
219 LOG(INFO) << "-------- BARRIER";
220 break;
221 case kPseudoEntryBlock:
222 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
223 break;
224 case kPseudoDalvikByteCodeBoundary:
225 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700226 // NOTE: only used for debug listings.
227 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 }
229 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000230 << lir->dalvik_offset << " @ "
Vladimir Markof6737f72015-03-23 17:05:14 +0000231 << UnwrapPointer<char>(lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 break;
233 case kPseudoExitBlock:
234 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
235 break;
236 case kPseudoPseudoAlign4:
237 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
238 << offset << "): .align4";
239 break;
240 case kPseudoEHBlockLabel:
241 LOG(INFO) << "Exception_Handling:";
242 break;
243 case kPseudoTargetLabel:
244 case kPseudoNormalBlockLabel:
245 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
246 break;
247 case kPseudoThrowTarget:
248 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
249 break;
250 case kPseudoIntrinsicRetry:
251 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
252 break;
253 case kPseudoSuspendTarget:
254 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
255 break;
256 case kPseudoSafepointPC:
257 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
258 break;
259 case kPseudoExportedPC:
260 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
261 break;
262 case kPseudoCaseLabel:
263 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
264 << std::hex << lir->operands[0] << "|" << std::dec <<
265 lir->operands[0];
266 break;
267 default:
268 if (lir->flags.is_nop && !dump_nop) {
269 break;
270 } else {
271 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
272 lir, base_addr));
273 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
274 lir, base_addr));
David Srbecky6f715892015-03-30 14:21:42 +0100275 LOG(INFO) << StringPrintf("%5p|0x%02x: %-9s%s%s",
Ian Rogers107c31e2014-01-23 20:55:29 -0800276 base_addr + offset,
David Srbecky6f715892015-03-30 14:21:42 +0100277 lir->dalvik_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 op_name.c_str(), op_operands.c_str(),
279 lir->flags.is_nop ? "(nop)" : "");
280 }
281 break;
282 }
283
buzbeeb48819d2013-09-14 16:15:25 -0700284 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100285 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 }
buzbeeb48819d2013-09-14 16:15:25 -0700287 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100288 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 }
290}
291
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700292void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700293 uint32_t num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
294 for (uint32_t i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 PromotionMap v_reg_map = promotion_map_[i];
296 std::string buf;
297 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700298 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 }
300
301 std::string buf3;
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700302 if (i < mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 StringAppendF(&buf3, "%02d", i);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700304 } else if (i == mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 buf3 = "Method*";
306 } else {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700307 uint32_t diff = i - mir_graph_->GetNumOfCodeVRs();
308 StringAppendF(&buf3, "ct%d", diff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 }
310
311 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
312 v_reg_map.core_location == kLocPhysReg ?
313 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
314 v_reg_map.core_reg : SRegOffset(i),
315 buf.c_str());
316 }
317}
318
buzbee7a11ab02014-04-28 20:02:38 -0700319void Mir2Lir::UpdateLIROffsets() {
320 // Only used for code listings.
321 size_t offset = 0;
322 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
323 lir->offset = offset;
324 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
325 offset += GetInsnSize(lir);
326 } else if (lir->opcode == kPseudoPseudoAlign4) {
327 offset += (offset & 0x2);
328 }
329 }
330}
331
Vladimir Marko743b98c2014-11-24 19:45:41 +0000332void Mir2Lir::MarkGCCard(int opt_flags, RegStorage val_reg, RegStorage tgt_addr_reg) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000333 DCHECK(val_reg.Valid());
334 DCHECK_EQ(val_reg.Is64Bit(), cu_->target64);
Vladimir Marko743b98c2014-11-24 19:45:41 +0000335 if ((opt_flags & MIR_STORE_NON_NULL_VALUE) != 0) {
336 UnconditionallyMarkGCCard(tgt_addr_reg);
337 } else {
338 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, nullptr);
339 UnconditionallyMarkGCCard(tgt_addr_reg);
340 LIR* target = NewLIR0(kPseudoTargetLabel);
341 branch_over->target = target;
342 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000343}
344
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700346void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347 LOG(INFO) << "Dumping LIR insns for "
348 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
349 LIR* lir_insn;
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700350 int insns_size = mir_graph_->GetNumDalvikInsns();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700352 LOG(INFO) << "Regs (excluding ins) : " << mir_graph_->GetNumOfLocalCodeVRs();
353 LOG(INFO) << "Ins : " << mir_graph_->GetNumOfInVRs();
354 LOG(INFO) << "Outs : " << mir_graph_->GetNumOfOutVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 LOG(INFO) << "CoreSpills : " << num_core_spills_;
356 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800357 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 LOG(INFO) << "Frame size : " << frame_size_;
359 LOG(INFO) << "code size is " << total_size_ <<
360 " bytes, Dalvik size is " << insns_size * 2;
361 LOG(INFO) << "expansion factor: "
362 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
363 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700364 UpdateLIROffsets();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700365 for (lir_insn = first_lir_insn_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 DumpLIRInsn(lir_insn, 0);
367 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700368 for (lir_insn = literal_list_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
370 lir_insn->operands[0]);
371 }
372
373 const DexFile::MethodId& method_id =
374 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700375 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
376 const char* name = cu_->dex_file->GetMethodName(method_id);
377 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378
379 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000380 if (!encoded_mapping_table_.empty()) {
381 MappingTable table(&encoded_mapping_table_[0]);
382 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
383 table.PcToDexSize(), table.PcToDexBegin());
384 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
385 table.DexToPcSize(), table.DexToPcBegin());
386 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387}
388
389/*
390 * Search the existing constants in the literal pool for an exact or close match
391 * within specified delta (greater or equal to 0).
392 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700393LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 while (data_target) {
395 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
396 return data_target;
397 data_target = data_target->next;
398 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700399 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400}
401
402/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700403LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 bool lo_match = false;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700405 LIR* lo_target = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 while (data_target) {
407 if (lo_match && (data_target->operands[0] == val_hi)) {
408 // Record high word in case we need to expand this later.
409 lo_target->operands[1] = val_hi;
410 return lo_target;
411 }
412 lo_match = false;
413 if (data_target->operands[0] == val_lo) {
414 lo_match = true;
415 lo_target = data_target;
416 }
417 data_target = data_target->next;
418 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700419 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420}
421
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100422/* Search the existing constants in the literal pool for an exact method match */
423LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
424 while (data_target) {
425 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000426 UnwrapPointer<DexFile>(data_target->operands[1]) == method.dex_file) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100427 return data_target;
428 }
429 data_target = data_target->next;
430 }
431 return nullptr;
432}
433
Fred Shihe7f82e22014-08-06 10:46:37 -0700434/* Search the existing constants in the literal pool for an exact class match */
435LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
436 while (data_target) {
437 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000438 UnwrapPointer<DexFile>(data_target->operands[1]) == &dex_file) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700439 return data_target;
440 }
441 data_target = data_target->next;
442 }
443 return nullptr;
444}
445
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446/*
447 * The following are building blocks to insert constants into the pool or
448 * instruction streams.
449 */
450
451/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700452LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 /* Add the constant to the literal pool */
454 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000455 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 new_value->operands[0] = value;
457 new_value->next = *constant_list_p;
458 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700459 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 return new_value;
461 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700462 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463}
464
465/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700466LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 AddWordData(constant_list_p, val_hi);
468 return AddWordData(constant_list_p, val_lo);
469}
470
Matteo Franchin27cc0932014-09-08 18:29:24 +0100471/**
472 * @brief Push a compressed reference which needs patching at link/patchoat-time.
473 * @details This needs to be kept consistent with the code which actually does the patching in
474 * oat_writer.cc and in the patchoat tool.
475 */
Vladimir Marko80b96d12015-02-19 15:50:28 +0000476static void PushUnpatchedReference(CodeBuffer* buf) {
Matteo Franchin27cc0932014-09-08 18:29:24 +0100477 // Note that we can safely initialize the patches to zero. The code deduplication mechanism takes
478 // the patches into account when determining whether two pieces of codes are functionally
479 // equivalent.
480 Push32(buf, UINT32_C(0));
buzbee0d829482013-10-11 15:24:55 -0700481}
482
Vladimir Marko80b96d12015-02-19 15:50:28 +0000483static void AlignBuffer(CodeBuffer* buf, size_t offset) {
484 DCHECK_LE(buf->size(), offset);
485 buf->insert(buf->end(), offset - buf->size(), 0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486}
487
488/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700489void Mir2Lir::InstallLiteralPools() {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000490 AlignBuffer(&code_buffer_, data_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 LIR* data_lir = literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700492 while (data_lir != nullptr) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000493 Push32(&code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 data_lir = NEXT_LIR(data_lir);
495 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100496 // TODO: patches_.reserve() as needed.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 // Push code and method literals, record offsets for the compiler to patch.
498 data_lir = code_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700499 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700500 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000501 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100502 patches_.push_back(LinkerPatch::CodePatch(code_buffer_.size(),
503 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000504 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 data_lir = NEXT_LIR(data_lir);
506 }
507 data_lir = method_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700508 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700509 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000510 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100511 patches_.push_back(LinkerPatch::MethodPatch(code_buffer_.size(),
512 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000513 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 data_lir = NEXT_LIR(data_lir);
515 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800516 // Push class literals.
517 data_lir = class_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700518 while (data_lir != nullptr) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100519 uint32_t target_type_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000520 const DexFile* class_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100521 patches_.push_back(LinkerPatch::TypePatch(code_buffer_.size(),
522 class_dex_file, target_type_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000523 PushUnpatchedReference(&code_buffer_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800524 data_lir = NEXT_LIR(data_lir);
525 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526}
527
528/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700529void Mir2Lir::InstallSwitchTables() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100530 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000531 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 /*
533 * For Arm, our reference point is the address of the bx
534 * instruction that does the launch, so we have to subtract
535 * the auto pc-advance. For other targets the reference point
536 * is a label, so we can use the offset as-is.
537 */
538 int bx_offset = INVALID_OFFSET;
539 switch (cu_->instruction_set) {
540 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700541 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 bx_offset = tab_rec->anchor->offset + 4;
543 break;
Mark Mendell27dee8b2014-12-01 19:06:12 -0500544 case kX86_64:
545 // RIP relative to switch table.
546 bx_offset = tab_rec->offset;
547 break;
Vladimir Marko1961b602015-04-08 20:51:48 +0100548 case kX86:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100549 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100551 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 bx_offset = tab_rec->anchor->offset;
553 break;
554 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
555 }
556 if (cu_->verbose) {
557 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
558 }
559 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800560 DCHECK(tab_rec->switch_mir != nullptr);
561 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
562 DCHECK(bb != nullptr);
563 int elems = 0;
564 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
565 int key = successor_block_info->key;
566 int target = successor_block_info->block;
567 LIR* boundary_lir = InsertCaseLabel(target, key);
568 DCHECK(boundary_lir != nullptr);
569 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000570 Push32(&code_buffer_, key);
571 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 if (cu_->verbose) {
573 LOG(INFO) << " Case[" << elems << "] key: 0x"
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800574 << std::hex << key << ", disp: 0x"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 << std::hex << disp;
576 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800577 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800579 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 } else {
581 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
582 static_cast<int>(Instruction::kPackedSwitchSignature));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800583 DCHECK(tab_rec->switch_mir != nullptr);
584 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
585 DCHECK(bb != nullptr);
586 int elems = 0;
587 int low_key = s4FromSwitchData(&tab_rec->table[2]);
588 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
589 int key = successor_block_info->key;
590 DCHECK_EQ(elems + low_key, key);
591 int target = successor_block_info->block;
592 LIR* boundary_lir = InsertCaseLabel(target, key);
593 DCHECK(boundary_lir != nullptr);
594 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000595 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 if (cu_->verbose) {
597 LOG(INFO) << " Case[" << elems << "] disp: 0x"
598 << std::hex << disp;
599 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800600 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800602 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 }
604 }
605}
606
607/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700608void Mir2Lir::InstallFillArrayData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100609 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000610 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700612 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
613 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 }
615 }
616}
617
buzbee0d829482013-10-11 15:24:55 -0700618static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700619 for (; lir != nullptr; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 lir->offset = offset;
621 offset += 4;
622 }
623 return offset;
624}
625
Ian Rogersff093b32014-04-30 19:04:27 -0700626static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
627 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700628 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700629 offset = RoundUp(offset, element_size);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700630 for (; lir != nullptr; lir = lir->next) {
buzbee0d829482013-10-11 15:24:55 -0700631 lir->offset = offset;
632 offset += element_size;
633 }
634 return offset;
635}
636
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700638bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000639 MappingTable table(&encoded_mapping_table_[0]);
640 std::vector<uint32_t> dex_pcs;
641 dex_pcs.reserve(table.DexToPcSize());
642 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
643 dex_pcs.push_back(it.DexPc());
644 }
645 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
646 std::sort(dex_pcs.begin(), dex_pcs.end());
647
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000649 auto it = dex_pcs.begin(), end = dex_pcs.end();
650 for (uint32_t dex_pc : mir_graph_->catches_) {
651 while (it != end && *it < dex_pc) {
652 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
653 ++it;
654 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000656 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
658 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000659 } else {
660 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 }
662 }
663 if (!success) {
664 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
665 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000666 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 }
668 return success;
669}
670
671
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700672void Mir2Lir::CreateMappingTables() {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700673 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
674
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000675 uint32_t pc2dex_data_size = 0u;
676 uint32_t pc2dex_entries = 0u;
677 uint32_t pc2dex_offset = 0u;
678 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700679 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000680 uint32_t dex2pc_data_size = 0u;
681 uint32_t dex2pc_entries = 0u;
682 uint32_t dex2pc_offset = 0u;
683 uint32_t dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700684 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700685 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000687 pc2dex_entries += 1;
688 DCHECK(pc2dex_offset <= tgt_lir->offset);
689 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
690 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
691 static_cast<int32_t>(pc2dex_dalvik_offset));
692 pc2dex_offset = tgt_lir->offset;
693 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 }
695 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000696 dex2pc_entries += 1;
697 DCHECK(dex2pc_offset <= tgt_lir->offset);
698 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
699 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
700 static_cast<int32_t>(dex2pc_dalvik_offset));
701 dex2pc_offset = tgt_lir->offset;
702 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 }
704 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000705
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700706 if (generate_src_map) {
707 src_mapping_table_.reserve(pc2dex_src_entries);
708 }
709
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000710 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
711 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
712 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000713 encoded_mapping_table_.resize(data_size);
714 uint8_t* write_pos = &encoded_mapping_table_[0];
715 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
716 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
717 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
718 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000719
David Srbecky6f715892015-03-30 14:21:42 +0100720 bool is_in_prologue_or_epilogue = false;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000721 pc2dex_offset = 0u;
722 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000723 dex2pc_offset = 0u;
724 dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700725 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
David Srbecky6f715892015-03-30 14:21:42 +0100726 if (generate_src_map && !tgt_lir->flags.is_nop && tgt_lir->opcode >= 0) {
727 if (!is_in_prologue_or_epilogue) {
728 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
729 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
730 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700731 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000732 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
733 DCHECK(pc2dex_offset <= tgt_lir->offset);
734 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
735 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
736 static_cast<int32_t>(pc2dex_dalvik_offset));
737 pc2dex_offset = tgt_lir->offset;
738 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
739 }
740 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
741 DCHECK(dex2pc_offset <= tgt_lir->offset);
742 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
743 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
744 static_cast<int32_t>(dex2pc_dalvik_offset));
745 dex2pc_offset = tgt_lir->offset;
746 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
747 }
David Srbecky6f715892015-03-30 14:21:42 +0100748 if (tgt_lir->opcode == kPseudoPrologueBegin || tgt_lir->opcode == kPseudoEpilogueBegin) {
749 is_in_prologue_or_epilogue = true;
750 }
751 if (tgt_lir->opcode == kPseudoPrologueEnd || tgt_lir->opcode == kPseudoEpilogueEnd) {
752 is_in_prologue_or_epilogue = false;
753 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000754 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000755 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
756 hdr_data_size + pc2dex_data_size);
757 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000758
Ian Rogers96faf5b2013-08-09 22:05:32 -0700759 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000760 CHECK(VerifyCatchEntries());
761
Ian Rogers96faf5b2013-08-09 22:05:32 -0700762 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000763 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700764 CHECK_EQ(table.TotalSize(), total_entries);
765 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000766 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000767 auto it2 = table.DexToPcBegin();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700768 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000769 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
770 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
771 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
772 ++it;
773 }
774 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
775 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
776 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
777 ++it2;
778 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700779 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000780 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000781 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700782 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783}
784
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko767c7522015-03-20 12:47:30 +0000786 if (UNLIKELY((cu_->disable_opt & (1u << kPromoteRegs)) != 0u)) {
787 // If we're not promoting to physical registers, it's safe to use the verifier's notion of
788 // references. (We disable register promotion when type inference finds a type conflict and
789 // in that the case we defer to the verifier to avoid using the compiler's conflicting info.)
790 CreateNativeGcMapWithoutRegisterPromotion();
791 return;
792 }
793
794 ArenaBitVector* references = new (arena_) ArenaBitVector(arena_, mir_graph_->GetNumSSARegs(),
795 false);
796
797 // Calculate max native offset and max reference vreg.
798 MIR* prev_mir = nullptr;
799 int max_ref_vreg = -1;
800 CodeOffset max_native_offset = 0u;
801 for (const auto& entry : safepoints_) {
802 uint32_t native_offset = entry.first->offset;
803 max_native_offset = std::max(max_native_offset, native_offset);
804 MIR* mir = entry.second;
805 UpdateReferenceVRegs(mir, prev_mir, references);
806 max_ref_vreg = std::max(max_ref_vreg, references->GetHighestBitSet());
807 prev_mir = mir;
808 }
809
Vladimir Marko6e071832015-03-25 11:13:39 +0000810#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
811 static constexpr bool kLittleEndian = true;
812#else
813 static constexpr bool kLittleEndian = false;
814#endif
815
Vladimir Marko767c7522015-03-20 12:47:30 +0000816 // Build the GC map.
817 uint32_t reg_width = static_cast<uint32_t>((max_ref_vreg + 8) / 8);
818 GcMapBuilder native_gc_map_builder(&native_gc_map_,
819 safepoints_.size(),
820 max_native_offset, reg_width);
Vladimir Marko6e071832015-03-25 11:13:39 +0000821 if (kLittleEndian) {
822 for (const auto& entry : safepoints_) {
823 uint32_t native_offset = entry.first->offset;
824 MIR* mir = entry.second;
825 UpdateReferenceVRegs(mir, prev_mir, references);
826 // For little-endian, the bytes comprising the bit vector's raw storage are what we need.
827 native_gc_map_builder.AddEntry(native_offset,
828 reinterpret_cast<const uint8_t*>(references->GetRawStorage()));
829 prev_mir = mir;
Vladimir Marko767c7522015-03-20 12:47:30 +0000830 }
Vladimir Marko6e071832015-03-25 11:13:39 +0000831 } else {
832 ArenaVector<uint8_t> references_buffer(arena_->Adapter());
833 references_buffer.resize(reg_width);
834 for (const auto& entry : safepoints_) {
835 uint32_t native_offset = entry.first->offset;
836 MIR* mir = entry.second;
837 UpdateReferenceVRegs(mir, prev_mir, references);
838 // Big-endian or unknown endianness, manually translate the bit vector data.
839 const auto* raw_storage = references->GetRawStorage();
840 for (size_t i = 0; i != reg_width; ++i) {
841 references_buffer[i] = static_cast<uint8_t>(
842 raw_storage[i / sizeof(raw_storage[0])] >> (8u * (i % sizeof(raw_storage[0]))));
843 }
844 native_gc_map_builder.AddEntry(native_offset, &references_buffer[0]);
845 prev_mir = mir;
846 }
Vladimir Marko767c7522015-03-20 12:47:30 +0000847 }
848}
849
850void Mir2Lir::CreateNativeGcMapWithoutRegisterPromotion() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000851 DCHECK(!encoded_mapping_table_.empty());
852 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000854 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
855 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 if (native_offset > max_native_offset) {
857 max_native_offset = native_offset;
858 }
859 }
860 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000861 const std::vector<uint8_t>& gc_map_raw =
862 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
863 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
864 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000866 GcMapBuilder native_gc_map_builder(&native_gc_map_,
867 mapping_table.PcToDexSize(),
868 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869
Vladimir Marko06606b92013-12-02 15:31:08 +0000870 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
871 uint32_t native_offset = it.NativePcOffset();
872 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700874 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
Dave Allisonf9439142014-03-27 15:10:22 -0700875 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 native_gc_map_builder.AddEntry(native_offset, references);
877 }
Mathieu Chartierab972ef2014-12-03 17:38:22 -0800878
879 // Maybe not necessary, but this could help prevent errors where we access the verified method
880 // after it has been deleted.
881 mir_graph_->GetCurrentDexCompilationUnit()->ClearVerifiedMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882}
883
884/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700885int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100887 constexpr unsigned int ptr_size = sizeof(uint32_t);
Andreas Gampe785d2f22014-11-03 22:57:30 -0800888 static_assert(ptr_size >= sizeof(mirror::HeapReference<mirror::Object>),
889 "Pointer size cannot hold a heap reference");
Ian Rogersff093b32014-04-30 19:04:27 -0700890 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
891 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
892 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 return offset;
894}
895
buzbee0d829482013-10-11 15:24:55 -0700896int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100897 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 tab_rec->offset = offset;
899 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
900 offset += tab_rec->table[1] * (sizeof(int) * 2);
901 } else {
902 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
903 static_cast<int>(Instruction::kPackedSwitchSignature));
904 offset += tab_rec->table[1] * sizeof(int);
905 }
906 }
907 return offset;
908}
909
buzbee0d829482013-10-11 15:24:55 -0700910int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100911 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 tab_rec->offset = offset;
913 offset += tab_rec->size;
914 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700915 offset = RoundUp(offset, 4);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100916 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700917 return offset;
918}
919
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920/*
921 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700922 * offset vaddr if pretty-printing, otherise use the standard block
923 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700924 * branch table during the assembly phase. All resource flags
925 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 */
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800927LIR* Mir2Lir::InsertCaseLabel(uint32_t bbid, int keyVal) {
928 LIR* boundary_lir = &block_label_list_[bbid];
buzbeeb48819d2013-09-14 16:15:25 -0700929 LIR* res = boundary_lir;
930 if (cu_->verbose) {
931 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000932 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800933 BasicBlock* bb = mir_graph_->GetBasicBlock(bbid);
934 DCHECK(bb != nullptr);
935 new_label->dalvik_offset = bb->start_offset;
buzbeeb48819d2013-09-14 16:15:25 -0700936 new_label->opcode = kPseudoCaseLabel;
937 new_label->operands[0] = keyVal;
938 new_label->flags.fixup = kFixupLabel;
939 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100940 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700941 InsertLIRAfter(boundary_lir, new_label);
buzbeeb48819d2013-09-14 16:15:25 -0700942 }
943 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944}
945
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700946void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700947 /*
948 * Sparse switch data format:
949 * ushort ident = 0x0200 magic value
950 * ushort size number of entries in the table; > 0
951 * int keys[size] keys, sorted low-to-high; 32-bit aligned
952 * int targets[size] branch targets, relative to switch opcode
953 *
954 * Total size is (2+size*4) 16-bit code units.
955 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 uint16_t ident = table[0];
957 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700958 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
959 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700960 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
961 << ", entries: " << std::dec << entries;
962 for (int i = 0; i < entries; i++) {
963 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
964 }
965}
966
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700967void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 /*
969 * Packed switch data format:
970 * ushort ident = 0x0100 magic value
971 * ushort size number of entries in the table
972 * int first_key first (and lowest) switch case value
973 * int targets[size] branch targets, relative to switch opcode
974 *
975 * Total size is (4+size*2) 16-bit code units.
976 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700978 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 int entries = table[1];
980 int low_key = s4FromSwitchData(&table[2]);
981 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
982 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
983 for (int i = 0; i < entries; i++) {
984 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
985 << targets[i];
986 }
987}
988
buzbee252254b2013-09-08 16:20:53 -0700989/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700990void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700991 UNUSED(offset);
buzbee0d829482013-10-11 15:24:55 -0700992 // NOTE: only used for debug listings.
993 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994}
995
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996// Convert relation of src1/src2 to src2/src1
997ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
998 ConditionCode res;
999 switch (before) {
1000 case kCondEq: res = kCondEq; break;
1001 case kCondNe: res = kCondNe; break;
1002 case kCondLt: res = kCondGt; break;
1003 case kCondGt: res = kCondLt; break;
1004 case kCondLe: res = kCondGe; break;
1005 case kCondGe: res = kCondLe; break;
1006 default:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001008 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 }
1010 return res;
1011}
1012
Vladimir Markoa1a70742014-03-03 10:28:05 +00001013ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
1014 ConditionCode res;
1015 switch (before) {
1016 case kCondEq: res = kCondNe; break;
1017 case kCondNe: res = kCondEq; break;
1018 case kCondLt: res = kCondGe; break;
1019 case kCondGt: res = kCondLe; break;
1020 case kCondLe: res = kCondGt; break;
1021 case kCondGe: res = kCondLt; break;
1022 default:
Vladimir Markoa1a70742014-03-03 10:28:05 +00001023 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001024 UNREACHABLE();
Vladimir Markoa1a70742014-03-03 10:28:05 +00001025 }
1026 return res;
1027}
1028
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029// TODO: move to mir_to_lir.cc
1030Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Andreas Gampe9c462082015-01-27 14:31:40 -08001031 : literal_list_(nullptr),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001032 method_literal_list_(nullptr),
1033 class_literal_list_(nullptr),
1034 code_literal_list_(nullptr),
1035 first_fixup_(nullptr),
Andreas Gampe9c462082015-01-27 14:31:40 -08001036 arena_(arena),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037 cu_(cu),
1038 mir_graph_(mir_graph),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001039 switch_tables_(arena->Adapter(kArenaAllocSwitchTable)),
1040 fill_array_data_(arena->Adapter(kArenaAllocFillArrayData)),
1041 tempreg_info_(arena->Adapter()),
1042 reginfo_map_(arena->Adapter()),
1043 pointer_storage_(arena->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 data_offset_(0),
1045 total_size_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001046 block_label_list_(nullptr),
1047 promotion_map_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 current_dalvik_offset_(0),
Vladimir Marko767c7522015-03-20 12:47:30 +00001049 current_mir_(nullptr),
buzbeeb48819d2013-09-14 16:15:25 -07001050 estimated_native_code_size_(0),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001051 reg_pool_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 live_sreg_(0),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001053 code_buffer_(mir_graph->GetArena()->Adapter()),
1054 encoded_mapping_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko8081d2b2014-07-31 15:33:43 +01001055 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1056 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001057 native_gc_map_(mir_graph->GetArena()->Adapter()),
Vladimir Markof4da6752014-08-01 19:04:18 +01001058 patches_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 num_core_spills_(0),
1060 num_fp_spills_(0),
1061 frame_size_(0),
1062 core_spill_mask_(0),
1063 fp_spill_mask_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001064 first_lir_insn_(nullptr),
1065 last_lir_insn_(nullptr),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001066 slow_paths_(arena->Adapter(kArenaAllocSlowPaths)),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001067 mem_ref_type_(ResourceMask::kHeapRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001068 mask_cache_(arena),
Vladimir Marko767c7522015-03-20 12:47:30 +00001069 safepoints_(arena->Adapter()),
Vladimir Marko20f85592015-03-19 10:07:02 +00001070 dex_cache_arrays_layout_(cu->compiler_driver->GetDexCacheArraysLayout(cu->dex_file)),
Vladimir Markocc234812015-04-07 09:36:09 +01001071 pc_rel_temp_(nullptr),
1072 dex_cache_arrays_min_offset_(std::numeric_limits<uint32_t>::max()),
David Srbecky1109fb32015-04-07 20:21:06 +01001073 cfi_(&last_lir_insn_,
David Srbecky8dc73242015-04-12 11:40:39 +01001074 cu->compiler_driver->GetCompilerOptions().GetIncludeCFI(),
David Srbecky1109fb32015-04-07 20:21:06 +01001075 arena),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001076 in_to_reg_storage_mapping_(arena) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001077 switch_tables_.reserve(4);
1078 fill_array_data_.reserve(4);
1079 tempreg_info_.reserve(20);
1080 reginfo_map_.reserve(RegStorage::kMaxRegs);
1081 pointer_storage_.reserve(128);
1082 slow_paths_.reserve(32);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001083 // Reserve pointer id 0 for null.
Vladimir Markof6737f72015-03-23 17:05:14 +00001084 size_t null_idx = WrapPointer<void>(nullptr);
buzbee0d829482013-10-11 15:24:55 -07001085 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086}
1087
1088void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001089 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1091
1092 /* Allocate Registers using simple local allocation scheme */
1093 SimpleRegAlloc();
1094
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001095 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001096 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001097 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001098 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001100 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1101 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 MethodMIR2LIR();
1103 }
1104
1105 /* Method is not empty */
1106 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 /* Convert LIR into machine code. */
1108 AssembleLIR();
1109
buzbeeb01bf152014-05-13 15:59:07 -07001110 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 CodegenDump();
1112 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114}
1115
1116CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001117 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1118 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001120 // Prefix the encoded data with its size.
1121 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1122 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1123 vmap_encoder.PushBackUnsigned(size);
1124 // Core regs may have been inserted out of order - sort first.
1125 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1126 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1127 // Copy, stripping out the phys register sort key.
1128 vmap_encoder.PushBackUnsigned(
1129 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1130 }
1131 // Push a marker to take place of lr.
1132 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001133 if (cu_->instruction_set == kThumb2) {
1134 // fp regs already sorted.
1135 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1136 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1137 }
1138 } else {
1139 // For other platforms regs may have been inserted out of order - sort first.
1140 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1141 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1142 // Copy, stripping out the phys register sort key.
1143 vmap_encoder.PushBackUnsigned(
1144 ~(-1 << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
1145 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001146 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001148 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1149 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001150 DCHECK_EQ(core_vmap_table_.size(), 0u);
1151 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1152 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001154
Vladimir Markof4da6752014-08-01 19:04:18 +01001155 // Sort patches by literal offset for better deduplication.
1156 std::sort(patches_.begin(), patches_.end(), [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
1157 return lhs.LiteralOffset() < rhs.LiteralOffset();
1158 });
1159
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001160 return CompiledMethod::SwapAllocCompiledMethod(
1161 cu_->compiler_driver, cu_->instruction_set,
1162 ArrayRef<const uint8_t>(code_buffer_),
1163 frame_size_, core_spill_mask_, fp_spill_mask_,
1164 &src_mapping_table_,
1165 ArrayRef<const uint8_t>(encoded_mapping_table_),
1166 ArrayRef<const uint8_t>(vmap_encoder.GetData()),
1167 ArrayRef<const uint8_t>(native_gc_map_),
David Srbecky1109fb32015-04-07 20:21:06 +01001168 ArrayRef<const uint8_t>(*cfi_.Patch(code_buffer_.size())),
Vladimir Markob207e142015-04-02 21:25:21 +01001169 ArrayRef<const LinkerPatch>(patches_));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170}
1171
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001172size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1173 // Chose a reasonably small value in order to contain stack growth.
1174 // Backends that are smarter about spill region can return larger values.
1175 const size_t max_compiler_temps = 10;
1176 return max_compiler_temps;
1177}
1178
1179size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1180 // By default assume that the Mir2Lir will need one slot for each temporary.
1181 // If the backend can better determine temps that have non-overlapping ranges and
1182 // temps that do not need spilled, it can actually provide a small region.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001183 mir_graph_->CommitCompilerTemps();
1184 return mir_graph_->GetNumBytesForSpecialTemps() + mir_graph_->GetMaximumBytesForNonSpecialTemps();
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001185}
1186
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187int Mir2Lir::ComputeFrameSize() {
1188 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001189 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1190 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1191 + sizeof(uint32_t) // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001192 + mir_graph_->GetNumOfLocalCodeVRs() * sizeof(uint32_t)
1193 + mir_graph_->GetNumOfOutVRs() * sizeof(uint32_t)
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001194 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001196 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197}
1198
1199/*
1200 * Append an LIR instruction to the LIR list maintained by a compilation
1201 * unit
1202 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001203void Mir2Lir::AppendLIR(LIR* lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001204 if (first_lir_insn_ == nullptr) {
1205 DCHECK(last_lir_insn_ == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 last_lir_insn_ = first_lir_insn_ = lir;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001207 lir->prev = lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 } else {
1209 last_lir_insn_->next = lir;
1210 lir->prev = last_lir_insn_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001211 lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 last_lir_insn_ = lir;
1213 }
1214}
1215
1216/*
1217 * Insert an LIR instruction before the current instruction, which cannot be the
1218 * first instruction.
1219 *
1220 * prev_lir <-> new_lir <-> current_lir
1221 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001222void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001223 DCHECK(current_lir->prev != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 LIR *prev_lir = current_lir->prev;
1225
1226 prev_lir->next = new_lir;
1227 new_lir->prev = prev_lir;
1228 new_lir->next = current_lir;
1229 current_lir->prev = new_lir;
1230}
1231
1232/*
1233 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001234 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 *
1236 * current_lir -> new_lir -> old_next
1237 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001238void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 new_lir->prev = current_lir;
1240 new_lir->next = current_lir->next;
1241 current_lir->next = new_lir;
1242 new_lir->next->prev = new_lir;
1243}
1244
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001245bool Mir2Lir::PartiallyIntersects(RegLocation rl_src, RegLocation rl_dest) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001246 DCHECK(rl_src.wide);
1247 DCHECK(rl_dest.wide);
1248 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1249}
1250
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001251bool Mir2Lir::Intersects(RegLocation rl_src, RegLocation rl_dest) {
1252 DCHECK(rl_src.wide);
1253 DCHECK(rl_dest.wide);
1254 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) <= 1);
1255}
1256
buzbee2700f7e2014-03-07 09:46:20 -08001257LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001258 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001259 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001260 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1261 if (compare != nullptr) {
1262 *compare = inst;
1263 }
Mark Mendell766e9292014-01-27 07:55:47 -08001264 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1265 return branch;
1266}
1267
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001268void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001269 slow_paths_.push_back(slowpath);
Serguei Katkov589e0462014-09-05 18:37:22 +07001270 ResetDefTracking();
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001271}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001272
Jeff Hao49161ce2014-03-12 11:05:25 -07001273void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1274 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001275 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001276 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001277 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001278 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001279 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1280 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1281 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001282 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001283 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001284 // Loads a code pointer. Code from oat file can be mapped anywhere.
Vladimir Markof6737f72015-03-23 17:05:14 +00001285 OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001286 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
Jeff Hao49161ce2014-03-12 11:05:25 -07001290void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1291 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001292 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001293 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001294 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001295 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001296 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1297 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1298 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001299 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001300 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001301 // Loads an ArtMethod pointer, which is a reference as it lives in the heap.
Vladimir Markof6737f72015-03-23 17:05:14 +00001302 OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001303 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001304 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001305}
1306
Fred Shihe7f82e22014-08-06 10:46:37 -07001307void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1308 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001309 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001310 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001311 if (data_target == nullptr) {
1312 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001313 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001314 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001315 // Loads a Class pointer, which is a reference as it lives in the heap.
Vladimir Markof6737f72015-03-23 17:05:14 +00001316 OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001317}
1318
Vladimir Marko20f85592015-03-19 10:07:02 +00001319bool Mir2Lir::CanUseOpPcRelDexCacheArrayLoad() const {
1320 return false;
1321}
1322
1323void Mir2Lir::OpPcRelDexCacheArrayLoad(const DexFile* dex_file ATTRIBUTE_UNUSED,
1324 int offset ATTRIBUTE_UNUSED,
1325 RegStorage r_dest ATTRIBUTE_UNUSED) {
1326 LOG(FATAL) << "No generic implementation.";
1327 UNREACHABLE();
1328}
1329
buzbee2700f7e2014-03-07 09:46:20 -08001330RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001331 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001332 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001333 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001334 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1335 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1336 info_lo->SetIsWide(false);
1337 info_hi->SetIsWide(false);
1338 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001339 } else {
buzbee85089dd2014-05-25 15:10:52 -07001340 RegisterInfo* info = GetRegInfo(loc.reg);
1341 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1342 DCHECK(info_new != nullptr);
1343 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1344 info->MarkDead();
1345 info_new->MarkLive(loc.s_reg_low);
1346 }
1347 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001348 }
buzbee85089dd2014-05-25 15:10:52 -07001349 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001350 }
buzbee85089dd2014-05-25 15:10:52 -07001351 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001352 return loc;
1353}
1354
Mark Mendelld65c51a2014-04-29 16:55:20 -04001355void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001356 UNUSED(bb, mir);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001357 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001358 UNREACHABLE();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001359}
1360
Vladimir Marko767c7522015-03-20 12:47:30 +00001361void Mir2Lir::InitReferenceVRegs(BasicBlock* bb, BitVector* references) {
1362 // Mark the references coming from the first predecessor.
1363 DCHECK(bb != nullptr);
1364 DCHECK(bb->block_type == kEntryBlock || !bb->predecessors.empty());
1365 BasicBlock* first_bb =
1366 (bb->block_type == kEntryBlock) ? bb : mir_graph_->GetBasicBlock(bb->predecessors[0]);
1367 DCHECK(first_bb != nullptr);
1368 DCHECK(first_bb->data_flow_info != nullptr);
1369 DCHECK(first_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1370 const int32_t* first_vreg_to_ssa_map = first_bb->data_flow_info->vreg_to_ssa_map_exit;
1371 references->ClearAllBits();
1372 for (uint32_t vreg = 0, num_vregs = mir_graph_->GetNumOfCodeVRs(); vreg != num_vregs; ++vreg) {
1373 int32_t sreg = first_vreg_to_ssa_map[vreg];
1374 if (sreg != INVALID_SREG && mir_graph_->reg_location_[sreg].ref &&
1375 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[sreg])) {
1376 references->SetBit(vreg);
1377 }
1378 }
1379 // Unmark the references that are merging with a different value.
1380 for (size_t i = 1u, num_pred = bb->predecessors.size(); i < num_pred; ++i) {
1381 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(bb->predecessors[i]);
1382 DCHECK(pred_bb != nullptr);
1383 DCHECK(pred_bb->data_flow_info != nullptr);
1384 DCHECK(pred_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1385 const int32_t* pred_vreg_to_ssa_map = pred_bb->data_flow_info->vreg_to_ssa_map_exit;
1386 for (uint32_t vreg : references->Indexes()) {
1387 if (first_vreg_to_ssa_map[vreg] != pred_vreg_to_ssa_map[vreg]) {
1388 // NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
1389 // so clearing the bit has no effect on the iterator.
1390 references->ClearBit(vreg);
1391 }
1392 }
1393 }
Vladimir Marko767c7522015-03-20 12:47:30 +00001394}
1395
1396bool Mir2Lir::UpdateReferenceVRegsLocal(MIR* mir, MIR* prev_mir, BitVector* references) {
1397 DCHECK(mir == nullptr || mir->bb == prev_mir->bb);
1398 DCHECK(prev_mir != nullptr);
1399 while (prev_mir != nullptr) {
1400 if (prev_mir == mir) {
1401 return true;
1402 }
1403 const size_t num_defs = prev_mir->ssa_rep->num_defs;
1404 const int32_t* defs = prev_mir->ssa_rep->defs;
1405 if (num_defs == 1u && mir_graph_->reg_location_[defs[0]].ref &&
1406 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[defs[0]])) {
1407 references->SetBit(mir_graph_->SRegToVReg(defs[0]));
1408 } else {
1409 for (size_t i = 0u; i != num_defs; ++i) {
1410 references->ClearBit(mir_graph_->SRegToVReg(defs[i]));
1411 }
1412 }
1413 prev_mir = prev_mir->next;
1414 }
1415 return false;
1416}
1417
1418void Mir2Lir::UpdateReferenceVRegs(MIR* mir, MIR* prev_mir, BitVector* references) {
1419 if (mir == nullptr) {
1420 // Safepoint in entry sequence.
1421 InitReferenceVRegs(mir_graph_->GetEntryBlock(), references);
1422 return;
1423 }
1424 if (IsInstructionReturn(mir->dalvikInsn.opcode) ||
1425 mir->dalvikInsn.opcode == Instruction::RETURN_VOID_NO_BARRIER) {
1426 references->ClearAllBits();
1427 if (mir->dalvikInsn.opcode == Instruction::RETURN_OBJECT) {
1428 references->SetBit(mir_graph_->SRegToVReg(mir->ssa_rep->uses[0]));
1429 }
1430 return;
1431 }
1432 if (prev_mir != nullptr && mir->bb == prev_mir->bb &&
1433 UpdateReferenceVRegsLocal(mir, prev_mir, references)) {
1434 return;
1435 }
1436 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
1437 DCHECK(bb != nullptr);
1438 InitReferenceVRegs(bb, references);
1439 bool success = UpdateReferenceVRegsLocal(mir, bb->first_mir_insn, references);
1440 DCHECK(success) << "MIR @0x" << std::hex << mir->offset << " not in BB#" << std::dec << mir->bb;
1441}
1442
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001443} // namespace art