blob: 029c0ca8c006f42e95e3e3ffd53c835dc5a816c2 [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
19#include "dex/mir_graph.h"
20#include "driver/compiler_driver.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070021#include "driver/compiler_options.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080022#include "driver/dex_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "dex_file-inl.h"
24#include "gc_map.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000025#include "gc_map_builder.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070026#include "mapping_table.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000027#include "dex/quick/dex_file_method_inliner.h"
28#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000029#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000030#include "dex/verified_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "verifier/dex_gc_map.h"
32#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000033#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034
35namespace art {
36
Vladimir Marko06606b92013-12-02 15:31:08 +000037namespace {
38
39/* Dump a mapping table */
40template <typename It>
41void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
42 const Signature& signature, uint32_t size, It first) {
43 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080044 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000045 descriptor, name, signature.ToString().c_str(), size));
46 std::replace(line.begin(), line.end(), ';', '_');
47 LOG(INFO) << line;
48 for (uint32_t i = 0; i != size; ++i) {
49 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
50 ++first;
51 LOG(INFO) << line;
52 }
53 LOG(INFO) <<" };\n\n";
54 }
55}
56
57} // anonymous namespace
58
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070059bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070060 bool res = false;
61 if (rl_src.is_const) {
62 if (rl_src.wide) {
Andreas Gampede0b9962014-08-27 14:24:42 -070063 // For wide registers, check whether we're the high partner. In that case we need to switch
64 // to the lower one for the correct value.
65 if (rl_src.high_word) {
66 rl_src.high_word = false;
67 rl_src.s_reg_low--;
68 rl_src.orig_sreg--;
69 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070071 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070073 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 }
75 } else {
76 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070077 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070079 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 }
81 }
82 }
83 return res;
84}
85
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070086void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070087 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010088 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010090 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -070091}
92
Andreas Gampe3c12c512014-06-24 18:46:29 +000093void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
94 DCHECK(!after->flags.use_def_invalid);
95 after->u.m.def_mask = &kEncodeAll;
96 // As NewLIR0 uses Append, we need to create the LIR by hand.
97 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
98 if (after->next == nullptr) {
99 DCHECK_EQ(after, last_lir_insn_);
100 AppendLIR(safepoint_pc);
101 } else {
102 InsertLIRAfter(after, safepoint_pc);
103 }
104 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
105}
106
buzbee252254b2013-09-08 16:20:53 -0700107/* Remove a LIR from the list. */
108void Mir2Lir::UnlinkLIR(LIR* lir) {
109 if (UNLIKELY(lir == first_lir_insn_)) {
110 first_lir_insn_ = lir->next;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700111 if (lir->next != nullptr) {
112 lir->next->prev = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700113 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700114 DCHECK(lir->next == nullptr);
buzbee252254b2013-09-08 16:20:53 -0700115 DCHECK(lir == last_lir_insn_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700116 last_lir_insn_ = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700117 }
118 } else if (lir == last_lir_insn_) {
119 last_lir_insn_ = lir->prev;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700120 lir->prev->next = nullptr;
121 } else if ((lir->prev != nullptr) && (lir->next != nullptr)) {
buzbee252254b2013-09-08 16:20:53 -0700122 lir->prev->next = lir->next;
123 lir->next->prev = lir->prev;
124 }
125}
126
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700128void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700130 if (!cu_->verbose) {
131 UnlinkLIR(lir);
132 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133}
134
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700135void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700137 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100138 // TODO: Avoid the extra Arena allocation!
139 const ResourceMask** mask_ptr;
140 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700142 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700144 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100146 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100148 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 /* ..and then add back the one we need */
150 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100151 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100153 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100155 case ResourceMask::kDalvikReg:
156 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100158 case ResourceMask::kHeapRef:
159 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100161 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 /* Currently only loads can be marked as kMustNotAlias */
163 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100164 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 break;
166 default:
167 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
168 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100169 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170}
171
172/*
173 * Mark load/store instructions that access Dalvik registers through the stack.
174 */
175void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700176 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100177 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
178 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179
180 /*
181 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
182 * access.
183 */
buzbeeb48819d2013-09-14 16:15:25 -0700184 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185}
186
187/*
188 * Debugging macros
189 */
190#define DUMP_RESOURCE_MASK(X)
191
192/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700193void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 int offset = lir->offset;
195 int dest = lir->operands[0];
196 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
197
198 /* Handle pseudo-ops individually, and all regular insns as a group */
199 switch (lir->opcode) {
200 case kPseudoMethodEntry:
201 LOG(INFO) << "-------- method entry "
202 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
203 break;
204 case kPseudoMethodExit:
205 LOG(INFO) << "-------- Method_Exit";
206 break;
207 case kPseudoBarrier:
208 LOG(INFO) << "-------- BARRIER";
209 break;
210 case kPseudoEntryBlock:
211 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
212 break;
213 case kPseudoDalvikByteCodeBoundary:
214 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700215 // NOTE: only used for debug listings.
216 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 }
218 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000219 << lir->dalvik_offset << " @ "
220 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 break;
222 case kPseudoExitBlock:
223 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
224 break;
225 case kPseudoPseudoAlign4:
226 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
227 << offset << "): .align4";
228 break;
229 case kPseudoEHBlockLabel:
230 LOG(INFO) << "Exception_Handling:";
231 break;
232 case kPseudoTargetLabel:
233 case kPseudoNormalBlockLabel:
234 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
235 break;
236 case kPseudoThrowTarget:
237 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
238 break;
239 case kPseudoIntrinsicRetry:
240 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
241 break;
242 case kPseudoSuspendTarget:
243 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
244 break;
245 case kPseudoSafepointPC:
246 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
247 break;
248 case kPseudoExportedPC:
249 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
250 break;
251 case kPseudoCaseLabel:
252 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
253 << std::hex << lir->operands[0] << "|" << std::dec <<
254 lir->operands[0];
255 break;
256 default:
257 if (lir->flags.is_nop && !dump_nop) {
258 break;
259 } else {
260 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
261 lir, base_addr));
262 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
263 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800264 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
265 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 op_name.c_str(), op_operands.c_str(),
267 lir->flags.is_nop ? "(nop)" : "");
268 }
269 break;
270 }
271
buzbeeb48819d2013-09-14 16:15:25 -0700272 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100273 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 }
buzbeeb48819d2013-09-14 16:15:25 -0700275 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100276 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 }
278}
279
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700280void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700281 uint32_t num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
282 for (uint32_t i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 PromotionMap v_reg_map = promotion_map_[i];
284 std::string buf;
285 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700286 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 }
288
289 std::string buf3;
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700290 if (i < mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 StringAppendF(&buf3, "%02d", i);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700292 } else if (i == mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 buf3 = "Method*";
294 } else {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700295 uint32_t diff = i - mir_graph_->GetNumOfCodeVRs();
296 StringAppendF(&buf3, "ct%d", diff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 }
298
299 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
300 v_reg_map.core_location == kLocPhysReg ?
301 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
302 v_reg_map.core_reg : SRegOffset(i),
303 buf.c_str());
304 }
305}
306
buzbee7a11ab02014-04-28 20:02:38 -0700307void Mir2Lir::UpdateLIROffsets() {
308 // Only used for code listings.
309 size_t offset = 0;
310 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
311 lir->offset = offset;
312 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
313 offset += GetInsnSize(lir);
314 } else if (lir->opcode == kPseudoPseudoAlign4) {
315 offset += (offset & 0x2);
316 }
317 }
318}
319
Vladimir Marko743b98c2014-11-24 19:45:41 +0000320void Mir2Lir::MarkGCCard(int opt_flags, RegStorage val_reg, RegStorage tgt_addr_reg) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000321 DCHECK(val_reg.Valid());
322 DCHECK_EQ(val_reg.Is64Bit(), cu_->target64);
Vladimir Marko743b98c2014-11-24 19:45:41 +0000323 if ((opt_flags & MIR_STORE_NON_NULL_VALUE) != 0) {
324 UnconditionallyMarkGCCard(tgt_addr_reg);
325 } else {
326 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, nullptr);
327 UnconditionallyMarkGCCard(tgt_addr_reg);
328 LIR* target = NewLIR0(kPseudoTargetLabel);
329 branch_over->target = target;
330 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000331}
332
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700334void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 LOG(INFO) << "Dumping LIR insns for "
336 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
337 LIR* lir_insn;
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700338 int insns_size = mir_graph_->GetNumDalvikInsns();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700340 LOG(INFO) << "Regs (excluding ins) : " << mir_graph_->GetNumOfLocalCodeVRs();
341 LOG(INFO) << "Ins : " << mir_graph_->GetNumOfInVRs();
342 LOG(INFO) << "Outs : " << mir_graph_->GetNumOfOutVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 LOG(INFO) << "CoreSpills : " << num_core_spills_;
344 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800345 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 LOG(INFO) << "Frame size : " << frame_size_;
347 LOG(INFO) << "code size is " << total_size_ <<
348 " bytes, Dalvik size is " << insns_size * 2;
349 LOG(INFO) << "expansion factor: "
350 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
351 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700352 UpdateLIROffsets();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700353 for (lir_insn = first_lir_insn_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 DumpLIRInsn(lir_insn, 0);
355 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700356 for (lir_insn = literal_list_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
358 lir_insn->operands[0]);
359 }
360
361 const DexFile::MethodId& method_id =
362 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700363 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
364 const char* name = cu_->dex_file->GetMethodName(method_id);
365 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366
367 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000368 if (!encoded_mapping_table_.empty()) {
369 MappingTable table(&encoded_mapping_table_[0]);
370 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
371 table.PcToDexSize(), table.PcToDexBegin());
372 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
373 table.DexToPcSize(), table.DexToPcBegin());
374 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375}
376
377/*
378 * Search the existing constants in the literal pool for an exact or close match
379 * within specified delta (greater or equal to 0).
380 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700381LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 while (data_target) {
383 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
384 return data_target;
385 data_target = data_target->next;
386 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700387 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388}
389
390/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700391LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 bool lo_match = false;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700393 LIR* lo_target = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 while (data_target) {
395 if (lo_match && (data_target->operands[0] == val_hi)) {
396 // Record high word in case we need to expand this later.
397 lo_target->operands[1] = val_hi;
398 return lo_target;
399 }
400 lo_match = false;
401 if (data_target->operands[0] == val_lo) {
402 lo_match = true;
403 lo_target = data_target;
404 }
405 data_target = data_target->next;
406 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700407 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408}
409
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100410/* Search the existing constants in the literal pool for an exact method match */
411LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
412 while (data_target) {
413 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
414 UnwrapPointer(data_target->operands[1]) == method.dex_file) {
415 return data_target;
416 }
417 data_target = data_target->next;
418 }
419 return nullptr;
420}
421
Fred Shihe7f82e22014-08-06 10:46:37 -0700422/* Search the existing constants in the literal pool for an exact class match */
423LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
424 while (data_target) {
425 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
426 UnwrapPointer(data_target->operands[1]) == &dex_file) {
427 return data_target;
428 }
429 data_target = data_target->next;
430 }
431 return nullptr;
432}
433
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434/*
435 * The following are building blocks to insert constants into the pool or
436 * instruction streams.
437 */
438
439/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700440LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 /* Add the constant to the literal pool */
442 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000443 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 new_value->operands[0] = value;
445 new_value->next = *constant_list_p;
446 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700447 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 return new_value;
449 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700450 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451}
452
453/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700454LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 AddWordData(constant_list_p, val_hi);
456 return AddWordData(constant_list_p, val_lo);
457}
458
Matteo Franchin27cc0932014-09-08 18:29:24 +0100459/**
460 * @brief Push a compressed reference which needs patching at link/patchoat-time.
461 * @details This needs to be kept consistent with the code which actually does the patching in
462 * oat_writer.cc and in the patchoat tool.
463 */
Vladimir Marko80b96d12015-02-19 15:50:28 +0000464static void PushUnpatchedReference(CodeBuffer* buf) {
Matteo Franchin27cc0932014-09-08 18:29:24 +0100465 // Note that we can safely initialize the patches to zero. The code deduplication mechanism takes
466 // the patches into account when determining whether two pieces of codes are functionally
467 // equivalent.
468 Push32(buf, UINT32_C(0));
buzbee0d829482013-10-11 15:24:55 -0700469}
470
Vladimir Marko80b96d12015-02-19 15:50:28 +0000471static void AlignBuffer(CodeBuffer* buf, size_t offset) {
472 DCHECK_LE(buf->size(), offset);
473 buf->insert(buf->end(), offset - buf->size(), 0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474}
475
476/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700477void Mir2Lir::InstallLiteralPools() {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000478 AlignBuffer(&code_buffer_, data_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 LIR* data_lir = literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700480 while (data_lir != nullptr) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000481 Push32(&code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 data_lir = NEXT_LIR(data_lir);
483 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100484 // TODO: patches_.reserve() as needed.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 // Push code and method literals, record offsets for the compiler to patch.
486 data_lir = code_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700487 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700488 uint32_t target_method_idx = data_lir->operands[0];
489 const DexFile* target_dex_file =
490 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Vladimir Markof4da6752014-08-01 19:04:18 +0100491 patches_.push_back(LinkerPatch::CodePatch(code_buffer_.size(),
492 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000493 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 data_lir = NEXT_LIR(data_lir);
495 }
496 data_lir = method_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700497 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700498 uint32_t target_method_idx = data_lir->operands[0];
499 const DexFile* target_dex_file =
500 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Vladimir Markof4da6752014-08-01 19:04:18 +0100501 patches_.push_back(LinkerPatch::MethodPatch(code_buffer_.size(),
502 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000503 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 data_lir = NEXT_LIR(data_lir);
505 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800506 // Push class literals.
507 data_lir = class_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700508 while (data_lir != nullptr) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100509 uint32_t target_type_idx = data_lir->operands[0];
Fred Shihe7f82e22014-08-06 10:46:37 -0700510 const DexFile* class_dex_file =
511 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Vladimir Markof4da6752014-08-01 19:04:18 +0100512 patches_.push_back(LinkerPatch::TypePatch(code_buffer_.size(),
513 class_dex_file, target_type_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000514 PushUnpatchedReference(&code_buffer_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800515 data_lir = NEXT_LIR(data_lir);
516 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517}
518
519/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700520void Mir2Lir::InstallSwitchTables() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100521 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000522 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 /*
524 * For Arm, our reference point is the address of the bx
525 * instruction that does the launch, so we have to subtract
526 * the auto pc-advance. For other targets the reference point
527 * is a label, so we can use the offset as-is.
528 */
529 int bx_offset = INVALID_OFFSET;
530 switch (cu_->instruction_set) {
531 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700532 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 bx_offset = tab_rec->anchor->offset + 4;
534 break;
535 case kX86:
536 bx_offset = 0;
537 break;
Mark Mendell27dee8b2014-12-01 19:06:12 -0500538 case kX86_64:
539 // RIP relative to switch table.
540 bx_offset = tab_rec->offset;
541 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100542 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100544 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 bx_offset = tab_rec->anchor->offset;
546 break;
547 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
548 }
549 if (cu_->verbose) {
550 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
551 }
552 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800553 DCHECK(tab_rec->switch_mir != nullptr);
554 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
555 DCHECK(bb != nullptr);
556 int elems = 0;
557 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
558 int key = successor_block_info->key;
559 int target = successor_block_info->block;
560 LIR* boundary_lir = InsertCaseLabel(target, key);
561 DCHECK(boundary_lir != nullptr);
562 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000563 Push32(&code_buffer_, key);
564 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 if (cu_->verbose) {
566 LOG(INFO) << " Case[" << elems << "] key: 0x"
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800567 << std::hex << key << ", disp: 0x"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 << std::hex << disp;
569 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800570 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800572 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 } else {
574 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
575 static_cast<int>(Instruction::kPackedSwitchSignature));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800576 DCHECK(tab_rec->switch_mir != nullptr);
577 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
578 DCHECK(bb != nullptr);
579 int elems = 0;
580 int low_key = s4FromSwitchData(&tab_rec->table[2]);
581 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
582 int key = successor_block_info->key;
583 DCHECK_EQ(elems + low_key, key);
584 int target = successor_block_info->block;
585 LIR* boundary_lir = InsertCaseLabel(target, key);
586 DCHECK(boundary_lir != nullptr);
587 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000588 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 if (cu_->verbose) {
590 LOG(INFO) << " Case[" << elems << "] disp: 0x"
591 << std::hex << disp;
592 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800593 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800595 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 }
597 }
598}
599
600/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700601void Mir2Lir::InstallFillArrayData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100602 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000603 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700605 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
606 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 }
608 }
609}
610
buzbee0d829482013-10-11 15:24:55 -0700611static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700612 for (; lir != nullptr; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 lir->offset = offset;
614 offset += 4;
615 }
616 return offset;
617}
618
Ian Rogersff093b32014-04-30 19:04:27 -0700619static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
620 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700621 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700622 offset = RoundUp(offset, element_size);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700623 for (; lir != nullptr; lir = lir->next) {
buzbee0d829482013-10-11 15:24:55 -0700624 lir->offset = offset;
625 offset += element_size;
626 }
627 return offset;
628}
629
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700631bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000632 MappingTable table(&encoded_mapping_table_[0]);
633 std::vector<uint32_t> dex_pcs;
634 dex_pcs.reserve(table.DexToPcSize());
635 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
636 dex_pcs.push_back(it.DexPc());
637 }
638 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
639 std::sort(dex_pcs.begin(), dex_pcs.end());
640
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000642 auto it = dex_pcs.begin(), end = dex_pcs.end();
643 for (uint32_t dex_pc : mir_graph_->catches_) {
644 while (it != end && *it < dex_pc) {
645 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
646 ++it;
647 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000649 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
651 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000652 } else {
653 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 }
655 }
656 if (!success) {
657 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
658 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000659 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 }
661 return success;
662}
663
664
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700665void Mir2Lir::CreateMappingTables() {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700666 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
667
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000668 uint32_t pc2dex_data_size = 0u;
669 uint32_t pc2dex_entries = 0u;
670 uint32_t pc2dex_offset = 0u;
671 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700672 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000673 uint32_t dex2pc_data_size = 0u;
674 uint32_t dex2pc_entries = 0u;
675 uint32_t dex2pc_offset = 0u;
676 uint32_t dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700677 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700678 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000680 pc2dex_entries += 1;
681 DCHECK(pc2dex_offset <= tgt_lir->offset);
682 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
683 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
684 static_cast<int32_t>(pc2dex_dalvik_offset));
685 pc2dex_offset = tgt_lir->offset;
686 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 }
688 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000689 dex2pc_entries += 1;
690 DCHECK(dex2pc_offset <= tgt_lir->offset);
691 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
692 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
693 static_cast<int32_t>(dex2pc_dalvik_offset));
694 dex2pc_offset = tgt_lir->offset;
695 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 }
697 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000698
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700699 if (generate_src_map) {
700 src_mapping_table_.reserve(pc2dex_src_entries);
701 }
702
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000703 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
704 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
705 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000706 encoded_mapping_table_.resize(data_size);
707 uint8_t* write_pos = &encoded_mapping_table_[0];
708 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
709 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
710 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
711 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000712
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000713 pc2dex_offset = 0u;
714 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000715 dex2pc_offset = 0u;
716 dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700717 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700718 if (generate_src_map && !tgt_lir->flags.is_nop) {
719 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
720 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
721 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000722 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
723 DCHECK(pc2dex_offset <= tgt_lir->offset);
724 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
725 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
726 static_cast<int32_t>(pc2dex_dalvik_offset));
727 pc2dex_offset = tgt_lir->offset;
728 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
729 }
730 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
731 DCHECK(dex2pc_offset <= tgt_lir->offset);
732 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
733 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
734 static_cast<int32_t>(dex2pc_dalvik_offset));
735 dex2pc_offset = tgt_lir->offset;
736 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
737 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000738 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000739 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
740 hdr_data_size + pc2dex_data_size);
741 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000742
Ian Rogers96faf5b2013-08-09 22:05:32 -0700743 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000744 CHECK(VerifyCatchEntries());
745
Ian Rogers96faf5b2013-08-09 22:05:32 -0700746 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000747 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700748 CHECK_EQ(table.TotalSize(), total_entries);
749 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000750 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000751 auto it2 = table.DexToPcBegin();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700752 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000753 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
754 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
755 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
756 ++it;
757 }
758 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
759 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
760 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
761 ++it2;
762 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700763 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000764 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000765 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700766 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767}
768
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000770 DCHECK(!encoded_mapping_table_.empty());
771 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000773 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
774 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 if (native_offset > max_native_offset) {
776 max_native_offset = native_offset;
777 }
778 }
779 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000780 const std::vector<uint8_t>& gc_map_raw =
781 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
782 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
783 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000785 GcMapBuilder native_gc_map_builder(&native_gc_map_,
786 mapping_table.PcToDexSize(),
787 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788
Vladimir Marko06606b92013-12-02 15:31:08 +0000789 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
790 uint32_t native_offset = it.NativePcOffset();
791 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700793 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
Dave Allisonf9439142014-03-27 15:10:22 -0700794 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 native_gc_map_builder.AddEntry(native_offset, references);
796 }
Mathieu Chartierab972ef2014-12-03 17:38:22 -0800797
798 // Maybe not necessary, but this could help prevent errors where we access the verified method
799 // after it has been deleted.
800 mir_graph_->GetCurrentDexCompilationUnit()->ClearVerifiedMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801}
802
803/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700804int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100806 constexpr unsigned int ptr_size = sizeof(uint32_t);
Andreas Gampe785d2f22014-11-03 22:57:30 -0800807 static_assert(ptr_size >= sizeof(mirror::HeapReference<mirror::Object>),
808 "Pointer size cannot hold a heap reference");
Ian Rogersff093b32014-04-30 19:04:27 -0700809 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
810 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
811 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 return offset;
813}
814
buzbee0d829482013-10-11 15:24:55 -0700815int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100816 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 tab_rec->offset = offset;
818 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
819 offset += tab_rec->table[1] * (sizeof(int) * 2);
820 } else {
821 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
822 static_cast<int>(Instruction::kPackedSwitchSignature));
823 offset += tab_rec->table[1] * sizeof(int);
824 }
825 }
826 return offset;
827}
828
buzbee0d829482013-10-11 15:24:55 -0700829int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100830 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 tab_rec->offset = offset;
832 offset += tab_rec->size;
833 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700834 offset = RoundUp(offset, 4);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100835 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 return offset;
837}
838
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839/*
840 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700841 * offset vaddr if pretty-printing, otherise use the standard block
842 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700843 * branch table during the assembly phase. All resource flags
844 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 */
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800846LIR* Mir2Lir::InsertCaseLabel(uint32_t bbid, int keyVal) {
847 LIR* boundary_lir = &block_label_list_[bbid];
buzbeeb48819d2013-09-14 16:15:25 -0700848 LIR* res = boundary_lir;
849 if (cu_->verbose) {
850 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000851 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800852 BasicBlock* bb = mir_graph_->GetBasicBlock(bbid);
853 DCHECK(bb != nullptr);
854 new_label->dalvik_offset = bb->start_offset;
buzbeeb48819d2013-09-14 16:15:25 -0700855 new_label->opcode = kPseudoCaseLabel;
856 new_label->operands[0] = keyVal;
857 new_label->flags.fixup = kFixupLabel;
858 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100859 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700860 InsertLIRAfter(boundary_lir, new_label);
buzbeeb48819d2013-09-14 16:15:25 -0700861 }
862 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863}
864
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700865void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 /*
867 * Sparse switch data format:
868 * ushort ident = 0x0200 magic value
869 * ushort size number of entries in the table; > 0
870 * int keys[size] keys, sorted low-to-high; 32-bit aligned
871 * int targets[size] branch targets, relative to switch opcode
872 *
873 * Total size is (2+size*4) 16-bit code units.
874 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 uint16_t ident = table[0];
876 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700877 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
878 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
880 << ", entries: " << std::dec << entries;
881 for (int i = 0; i < entries; i++) {
882 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
883 }
884}
885
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700886void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887 /*
888 * Packed switch data format:
889 * ushort ident = 0x0100 magic value
890 * ushort size number of entries in the table
891 * int first_key first (and lowest) switch case value
892 * int targets[size] branch targets, relative to switch opcode
893 *
894 * Total size is (4+size*2) 16-bit code units.
895 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700897 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 int entries = table[1];
899 int low_key = s4FromSwitchData(&table[2]);
900 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
901 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
902 for (int i = 0; i < entries; i++) {
903 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
904 << targets[i];
905 }
906}
907
buzbee252254b2013-09-08 16:20:53 -0700908/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700909void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700910 UNUSED(offset);
buzbee0d829482013-10-11 15:24:55 -0700911 // NOTE: only used for debug listings.
912 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913}
914
Brian Carlstrom7940e442013-07-12 13:46:57 -0700915// Convert relation of src1/src2 to src2/src1
916ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
917 ConditionCode res;
918 switch (before) {
919 case kCondEq: res = kCondEq; break;
920 case kCondNe: res = kCondNe; break;
921 case kCondLt: res = kCondGt; break;
922 case kCondGt: res = kCondLt; break;
923 case kCondLe: res = kCondGe; break;
924 case kCondGe: res = kCondLe; break;
925 default:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700927 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928 }
929 return res;
930}
931
Vladimir Markoa1a70742014-03-03 10:28:05 +0000932ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
933 ConditionCode res;
934 switch (before) {
935 case kCondEq: res = kCondNe; break;
936 case kCondNe: res = kCondEq; break;
937 case kCondLt: res = kCondGe; break;
938 case kCondGt: res = kCondLe; break;
939 case kCondLe: res = kCondGt; break;
940 case kCondGe: res = kCondLt; break;
941 default:
Vladimir Markoa1a70742014-03-03 10:28:05 +0000942 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700943 UNREACHABLE();
Vladimir Markoa1a70742014-03-03 10:28:05 +0000944 }
945 return res;
946}
947
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948// TODO: move to mir_to_lir.cc
949Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Andreas Gampe9c462082015-01-27 14:31:40 -0800950 : literal_list_(nullptr),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700951 method_literal_list_(nullptr),
952 class_literal_list_(nullptr),
953 code_literal_list_(nullptr),
954 first_fixup_(nullptr),
Andreas Gampe9c462082015-01-27 14:31:40 -0800955 arena_(arena),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 cu_(cu),
957 mir_graph_(mir_graph),
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100958 switch_tables_(arena->Adapter(kArenaAllocSwitchTable)),
959 fill_array_data_(arena->Adapter(kArenaAllocFillArrayData)),
960 tempreg_info_(arena->Adapter()),
961 reginfo_map_(arena->Adapter()),
962 pointer_storage_(arena->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 data_offset_(0),
964 total_size_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700965 block_label_list_(nullptr),
966 promotion_map_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700967 current_dalvik_offset_(0),
buzbeeb48819d2013-09-14 16:15:25 -0700968 estimated_native_code_size_(0),
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100969 reg_pool_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 live_sreg_(0),
Vladimir Marko80b96d12015-02-19 15:50:28 +0000971 code_buffer_(mir_graph->GetArena()->Adapter()),
972 encoded_mapping_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100973 core_vmap_table_(mir_graph->GetArena()->Adapter()),
974 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko80b96d12015-02-19 15:50:28 +0000975 native_gc_map_(mir_graph->GetArena()->Adapter()),
Vladimir Markof4da6752014-08-01 19:04:18 +0100976 patches_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 num_core_spills_(0),
978 num_fp_spills_(0),
979 frame_size_(0),
980 core_spill_mask_(0),
981 fp_spill_mask_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700982 first_lir_insn_(nullptr),
983 last_lir_insn_(nullptr),
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100984 slow_paths_(arena->Adapter(kArenaAllocSlowPaths)),
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100985 mem_ref_type_(ResourceMask::kHeapRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +0600986 mask_cache_(arena),
987 in_to_reg_storage_mapping_(arena) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100988 switch_tables_.reserve(4);
989 fill_array_data_.reserve(4);
990 tempreg_info_.reserve(20);
991 reginfo_map_.reserve(RegStorage::kMaxRegs);
992 pointer_storage_.reserve(128);
993 slow_paths_.reserve(32);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700994 // Reserve pointer id 0 for nullptr.
995 size_t null_idx = WrapPointer(nullptr);
buzbee0d829482013-10-11 15:24:55 -0700996 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997}
998
999void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001000 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1002
1003 /* Allocate Registers using simple local allocation scheme */
1004 SimpleRegAlloc();
1005
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001006 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001007 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001008 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001009 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001011 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1012 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 MethodMIR2LIR();
1014 }
1015
1016 /* Method is not empty */
1017 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 /* Convert LIR into machine code. */
1019 AssembleLIR();
1020
buzbeeb01bf152014-05-13 15:59:07 -07001021 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 CodegenDump();
1023 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025}
1026
1027CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001028 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1029 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001031 // Prefix the encoded data with its size.
1032 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1033 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1034 vmap_encoder.PushBackUnsigned(size);
1035 // Core regs may have been inserted out of order - sort first.
1036 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1037 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1038 // Copy, stripping out the phys register sort key.
1039 vmap_encoder.PushBackUnsigned(
1040 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1041 }
1042 // Push a marker to take place of lr.
1043 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001044 if (cu_->instruction_set == kThumb2) {
1045 // fp regs already sorted.
1046 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1047 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1048 }
1049 } else {
1050 // For other platforms regs may have been inserted out of order - sort first.
1051 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1052 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1053 // Copy, stripping out the phys register sort key.
1054 vmap_encoder.PushBackUnsigned(
1055 ~(-1 << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
1056 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001057 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001059 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1060 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001061 DCHECK_EQ(core_vmap_table_.size(), 0u);
1062 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1063 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001064 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001065
Vladimir Markof4da6752014-08-01 19:04:18 +01001066 // Sort patches by literal offset for better deduplication.
1067 std::sort(patches_.begin(), patches_.end(), [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
1068 return lhs.LiteralOffset() < rhs.LiteralOffset();
1069 });
1070
Andreas Gamped37f9192015-03-04 14:00:56 -08001071 std::unique_ptr<std::vector<uint8_t>> cfi_info(
1072 cu_->compiler_driver->GetCompilerOptions().GetGenerateGDBInformation() ?
1073 ReturnFrameDescriptionEntry() :
1074 nullptr);
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001075 ArrayRef<const uint8_t> cfi_ref;
1076 if (cfi_info.get() != nullptr) {
1077 cfi_ref = ArrayRef<const uint8_t>(*cfi_info);
1078 }
1079 return CompiledMethod::SwapAllocCompiledMethod(
1080 cu_->compiler_driver, cu_->instruction_set,
1081 ArrayRef<const uint8_t>(code_buffer_),
1082 frame_size_, core_spill_mask_, fp_spill_mask_,
1083 &src_mapping_table_,
1084 ArrayRef<const uint8_t>(encoded_mapping_table_),
1085 ArrayRef<const uint8_t>(vmap_encoder.GetData()),
1086 ArrayRef<const uint8_t>(native_gc_map_),
1087 cfi_ref,
1088 ArrayRef<LinkerPatch>(patches_));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089}
1090
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001091size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1092 // Chose a reasonably small value in order to contain stack growth.
1093 // Backends that are smarter about spill region can return larger values.
1094 const size_t max_compiler_temps = 10;
1095 return max_compiler_temps;
1096}
1097
1098size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1099 // By default assume that the Mir2Lir will need one slot for each temporary.
1100 // If the backend can better determine temps that have non-overlapping ranges and
1101 // temps that do not need spilled, it can actually provide a small region.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001102 mir_graph_->CommitCompilerTemps();
1103 return mir_graph_->GetNumBytesForSpecialTemps() + mir_graph_->GetMaximumBytesForNonSpecialTemps();
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001104}
1105
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106int Mir2Lir::ComputeFrameSize() {
1107 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001108 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1109 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1110 + sizeof(uint32_t) // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001111 + mir_graph_->GetNumOfLocalCodeVRs() * sizeof(uint32_t)
1112 + mir_graph_->GetNumOfOutVRs() * sizeof(uint32_t)
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001113 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001115 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116}
1117
1118/*
1119 * Append an LIR instruction to the LIR list maintained by a compilation
1120 * unit
1121 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001122void Mir2Lir::AppendLIR(LIR* lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001123 if (first_lir_insn_ == nullptr) {
1124 DCHECK(last_lir_insn_ == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 last_lir_insn_ = first_lir_insn_ = lir;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001126 lir->prev = lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 } else {
1128 last_lir_insn_->next = lir;
1129 lir->prev = last_lir_insn_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001130 lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001131 last_lir_insn_ = lir;
1132 }
1133}
1134
1135/*
1136 * Insert an LIR instruction before the current instruction, which cannot be the
1137 * first instruction.
1138 *
1139 * prev_lir <-> new_lir <-> current_lir
1140 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001141void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001142 DCHECK(current_lir->prev != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 LIR *prev_lir = current_lir->prev;
1144
1145 prev_lir->next = new_lir;
1146 new_lir->prev = prev_lir;
1147 new_lir->next = current_lir;
1148 current_lir->prev = new_lir;
1149}
1150
1151/*
1152 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001153 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 *
1155 * current_lir -> new_lir -> old_next
1156 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001157void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 new_lir->prev = current_lir;
1159 new_lir->next = current_lir->next;
1160 current_lir->next = new_lir;
1161 new_lir->next->prev = new_lir;
1162}
1163
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001164bool Mir2Lir::PartiallyIntersects(RegLocation rl_src, RegLocation rl_dest) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001165 DCHECK(rl_src.wide);
1166 DCHECK(rl_dest.wide);
1167 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1168}
1169
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001170bool Mir2Lir::Intersects(RegLocation rl_src, RegLocation rl_dest) {
1171 DCHECK(rl_src.wide);
1172 DCHECK(rl_dest.wide);
1173 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) <= 1);
1174}
1175
buzbee2700f7e2014-03-07 09:46:20 -08001176LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001177 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001178 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001179 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1180 if (compare != nullptr) {
1181 *compare = inst;
1182 }
Mark Mendell766e9292014-01-27 07:55:47 -08001183 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1184 return branch;
1185}
1186
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001187void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001188 slow_paths_.push_back(slowpath);
Serguei Katkov589e0462014-09-05 18:37:22 +07001189 ResetDefTracking();
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001190}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001191
Jeff Hao49161ce2014-03-12 11:05:25 -07001192void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1193 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001194 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001195 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001196 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001197 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001198 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1199 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1200 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001201 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001202 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001203 // Loads a code pointer. Code from oat file can be mapped anywhere.
1204 LIR* load_pc_rel = OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001205 AppendLIR(load_pc_rel);
1206 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001207 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001208}
1209
Jeff Hao49161ce2014-03-12 11:05:25 -07001210void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1211 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001212 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001213 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001214 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001215 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001216 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1217 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1218 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001219 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001220 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001221 // Loads an ArtMethod pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001222 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001223 AppendLIR(load_pc_rel);
1224 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001225 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001226}
1227
Fred Shihe7f82e22014-08-06 10:46:37 -07001228void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1229 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001230 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001231 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001232 if (data_target == nullptr) {
1233 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001234 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001235 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001236 // Loads a Class pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001237 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001238 AppendLIR(load_pc_rel);
1239}
1240
Tong Shen547cdfd2014-08-05 01:54:19 -07001241std::vector<uint8_t>* Mir2Lir::ReturnFrameDescriptionEntry() {
Mark Mendellae9fd932014-02-10 16:14:35 -08001242 // Default case is to do nothing.
1243 return nullptr;
1244}
1245
buzbee2700f7e2014-03-07 09:46:20 -08001246RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001247 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001248 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001249 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001250 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1251 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1252 info_lo->SetIsWide(false);
1253 info_hi->SetIsWide(false);
1254 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001255 } else {
buzbee85089dd2014-05-25 15:10:52 -07001256 RegisterInfo* info = GetRegInfo(loc.reg);
1257 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1258 DCHECK(info_new != nullptr);
1259 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1260 info->MarkDead();
1261 info_new->MarkLive(loc.s_reg_low);
1262 }
1263 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001264 }
buzbee85089dd2014-05-25 15:10:52 -07001265 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001266 }
buzbee85089dd2014-05-25 15:10:52 -07001267 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001268 return loc;
1269}
1270
Mark Mendelld65c51a2014-04-29 16:55:20 -04001271void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001272 UNUSED(bb, mir);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001273 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001274 UNREACHABLE();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001275}
1276
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001277} // namespace art