blob: 80cb535307ce746a2b5cc3ad6f64415e1eac0f4f [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
17#include "dex/compiler_internals.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070018#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "dex_file-inl.h"
20#include "gc_map.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000021#include "gc_map_builder.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070022#include "mapping_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "mir_to_lir-inl.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000024#include "dex/quick/dex_file_method_inliner.h"
25#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000026#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000027#include "dex/verified_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "verifier/dex_gc_map.h"
29#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000030#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031
32namespace art {
33
Vladimir Marko06606b92013-12-02 15:31:08 +000034namespace {
35
36/* Dump a mapping table */
37template <typename It>
38void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
39 const Signature& signature, uint32_t size, It first) {
40 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080041 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000042 descriptor, name, signature.ToString().c_str(), size));
43 std::replace(line.begin(), line.end(), ';', '_');
44 LOG(INFO) << line;
45 for (uint32_t i = 0; i != size; ++i) {
46 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
47 ++first;
48 LOG(INFO) << line;
49 }
50 LOG(INFO) <<" };\n\n";
51 }
52}
53
54} // anonymous namespace
55
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070056bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 bool res = false;
58 if (rl_src.is_const) {
59 if (rl_src.wide) {
Andreas Gampede0b9962014-08-27 14:24:42 -070060 // For wide registers, check whether we're the high partner. In that case we need to switch
61 // to the lower one for the correct value.
62 if (rl_src.high_word) {
63 rl_src.high_word = false;
64 rl_src.s_reg_low--;
65 rl_src.orig_sreg--;
66 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070068 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070070 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 }
72 } else {
73 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070074 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070076 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 }
78 }
79 }
80 return res;
81}
82
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070083void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070084 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010085 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010087 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -070088}
89
Andreas Gampe3c12c512014-06-24 18:46:29 +000090void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
91 DCHECK(!after->flags.use_def_invalid);
92 after->u.m.def_mask = &kEncodeAll;
93 // As NewLIR0 uses Append, we need to create the LIR by hand.
94 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
95 if (after->next == nullptr) {
96 DCHECK_EQ(after, last_lir_insn_);
97 AppendLIR(safepoint_pc);
98 } else {
99 InsertLIRAfter(after, safepoint_pc);
100 }
101 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
102}
103
buzbee252254b2013-09-08 16:20:53 -0700104/* Remove a LIR from the list. */
105void Mir2Lir::UnlinkLIR(LIR* lir) {
106 if (UNLIKELY(lir == first_lir_insn_)) {
107 first_lir_insn_ = lir->next;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700108 if (lir->next != nullptr) {
109 lir->next->prev = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700110 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700111 DCHECK(lir->next == nullptr);
buzbee252254b2013-09-08 16:20:53 -0700112 DCHECK(lir == last_lir_insn_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700113 last_lir_insn_ = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700114 }
115 } else if (lir == last_lir_insn_) {
116 last_lir_insn_ = lir->prev;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117 lir->prev->next = nullptr;
118 } else if ((lir->prev != nullptr) && (lir->next != nullptr)) {
buzbee252254b2013-09-08 16:20:53 -0700119 lir->prev->next = lir->next;
120 lir->next->prev = lir->prev;
121 }
122}
123
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700125void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700127 if (!cu_->verbose) {
128 UnlinkLIR(lir);
129 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130}
131
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700132void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700134 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100135 // TODO: Avoid the extra Arena allocation!
136 const ResourceMask** mask_ptr;
137 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700139 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700141 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100143 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100145 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 /* ..and then add back the one we need */
147 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100148 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100150 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100152 case ResourceMask::kDalvikReg:
153 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100155 case ResourceMask::kHeapRef:
156 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100158 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 /* Currently only loads can be marked as kMustNotAlias */
160 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100161 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 break;
163 default:
164 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
165 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100166 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167}
168
169/*
170 * Mark load/store instructions that access Dalvik registers through the stack.
171 */
172void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700173 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100174 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
175 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176
177 /*
178 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
179 * access.
180 */
buzbeeb48819d2013-09-14 16:15:25 -0700181 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182}
183
184/*
185 * Debugging macros
186 */
187#define DUMP_RESOURCE_MASK(X)
188
189/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700190void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 int offset = lir->offset;
192 int dest = lir->operands[0];
193 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
194
195 /* Handle pseudo-ops individually, and all regular insns as a group */
196 switch (lir->opcode) {
197 case kPseudoMethodEntry:
198 LOG(INFO) << "-------- method entry "
199 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
200 break;
201 case kPseudoMethodExit:
202 LOG(INFO) << "-------- Method_Exit";
203 break;
204 case kPseudoBarrier:
205 LOG(INFO) << "-------- BARRIER";
206 break;
207 case kPseudoEntryBlock:
208 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
209 break;
210 case kPseudoDalvikByteCodeBoundary:
211 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700212 // NOTE: only used for debug listings.
213 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 }
215 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000216 << lir->dalvik_offset << " @ "
217 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 break;
219 case kPseudoExitBlock:
220 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
221 break;
222 case kPseudoPseudoAlign4:
223 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
224 << offset << "): .align4";
225 break;
226 case kPseudoEHBlockLabel:
227 LOG(INFO) << "Exception_Handling:";
228 break;
229 case kPseudoTargetLabel:
230 case kPseudoNormalBlockLabel:
231 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
232 break;
233 case kPseudoThrowTarget:
234 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
235 break;
236 case kPseudoIntrinsicRetry:
237 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
238 break;
239 case kPseudoSuspendTarget:
240 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
241 break;
242 case kPseudoSafepointPC:
243 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
244 break;
245 case kPseudoExportedPC:
246 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
247 break;
248 case kPseudoCaseLabel:
249 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
250 << std::hex << lir->operands[0] << "|" << std::dec <<
251 lir->operands[0];
252 break;
253 default:
254 if (lir->flags.is_nop && !dump_nop) {
255 break;
256 } else {
257 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
258 lir, base_addr));
259 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
260 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800261 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
262 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 op_name.c_str(), op_operands.c_str(),
264 lir->flags.is_nop ? "(nop)" : "");
265 }
266 break;
267 }
268
buzbeeb48819d2013-09-14 16:15:25 -0700269 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100270 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 }
buzbeeb48819d2013-09-14 16:15:25 -0700272 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100273 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 }
275}
276
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700277void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700278 uint32_t num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
279 for (uint32_t i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 PromotionMap v_reg_map = promotion_map_[i];
281 std::string buf;
282 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700283 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 }
285
286 std::string buf3;
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700287 if (i < mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 StringAppendF(&buf3, "%02d", i);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700289 } else if (i == mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 buf3 = "Method*";
291 } else {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700292 uint32_t diff = i - mir_graph_->GetNumOfCodeVRs();
293 StringAppendF(&buf3, "ct%d", diff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 }
295
296 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
297 v_reg_map.core_location == kLocPhysReg ?
298 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
299 v_reg_map.core_reg : SRegOffset(i),
300 buf.c_str());
301 }
302}
303
buzbee7a11ab02014-04-28 20:02:38 -0700304void Mir2Lir::UpdateLIROffsets() {
305 // Only used for code listings.
306 size_t offset = 0;
307 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
308 lir->offset = offset;
309 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
310 offset += GetInsnSize(lir);
311 } else if (lir->opcode == kPseudoPseudoAlign4) {
312 offset += (offset & 0x2);
313 }
314 }
315}
316
Vladimir Markobf535be2014-11-19 18:52:35 +0000317void Mir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
318 DCHECK(val_reg.Valid());
319 DCHECK_EQ(val_reg.Is64Bit(), cu_->target64);
320 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, nullptr);
321 UnconditionallyMarkGCCard(tgt_addr_reg);
322 LIR* target = NewLIR0(kPseudoTargetLabel);
323 branch_over->target = target;
324}
325
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700327void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 LOG(INFO) << "Dumping LIR insns for "
329 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
330 LIR* lir_insn;
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700331 int insns_size = mir_graph_->GetNumDalvikInsns();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700333 LOG(INFO) << "Regs (excluding ins) : " << mir_graph_->GetNumOfLocalCodeVRs();
334 LOG(INFO) << "Ins : " << mir_graph_->GetNumOfInVRs();
335 LOG(INFO) << "Outs : " << mir_graph_->GetNumOfOutVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 LOG(INFO) << "CoreSpills : " << num_core_spills_;
337 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800338 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 LOG(INFO) << "Frame size : " << frame_size_;
340 LOG(INFO) << "code size is " << total_size_ <<
341 " bytes, Dalvik size is " << insns_size * 2;
342 LOG(INFO) << "expansion factor: "
343 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
344 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700345 UpdateLIROffsets();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700346 for (lir_insn = first_lir_insn_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347 DumpLIRInsn(lir_insn, 0);
348 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700349 for (lir_insn = literal_list_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
351 lir_insn->operands[0]);
352 }
353
354 const DexFile::MethodId& method_id =
355 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700356 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
357 const char* name = cu_->dex_file->GetMethodName(method_id);
358 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359
360 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000361 if (!encoded_mapping_table_.empty()) {
362 MappingTable table(&encoded_mapping_table_[0]);
363 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
364 table.PcToDexSize(), table.PcToDexBegin());
365 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
366 table.DexToPcSize(), table.DexToPcBegin());
367 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368}
369
370/*
371 * Search the existing constants in the literal pool for an exact or close match
372 * within specified delta (greater or equal to 0).
373 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700374LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 while (data_target) {
376 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
377 return data_target;
378 data_target = data_target->next;
379 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700380 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381}
382
383/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700384LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 bool lo_match = false;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700386 LIR* lo_target = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 while (data_target) {
388 if (lo_match && (data_target->operands[0] == val_hi)) {
389 // Record high word in case we need to expand this later.
390 lo_target->operands[1] = val_hi;
391 return lo_target;
392 }
393 lo_match = false;
394 if (data_target->operands[0] == val_lo) {
395 lo_match = true;
396 lo_target = data_target;
397 }
398 data_target = data_target->next;
399 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700400 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401}
402
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100403/* Search the existing constants in the literal pool for an exact method match */
404LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
405 while (data_target) {
406 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
407 UnwrapPointer(data_target->operands[1]) == method.dex_file) {
408 return data_target;
409 }
410 data_target = data_target->next;
411 }
412 return nullptr;
413}
414
Fred Shihe7f82e22014-08-06 10:46:37 -0700415/* Search the existing constants in the literal pool for an exact class match */
416LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
417 while (data_target) {
418 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
419 UnwrapPointer(data_target->operands[1]) == &dex_file) {
420 return data_target;
421 }
422 data_target = data_target->next;
423 }
424 return nullptr;
425}
426
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427/*
428 * The following are building blocks to insert constants into the pool or
429 * instruction streams.
430 */
431
432/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700433LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 /* Add the constant to the literal pool */
435 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000436 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 new_value->operands[0] = value;
438 new_value->next = *constant_list_p;
439 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700440 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 return new_value;
442 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700443 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444}
445
446/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700447LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 AddWordData(constant_list_p, val_hi);
449 return AddWordData(constant_list_p, val_lo);
450}
451
Andreas Gampe2da88232014-02-27 12:26:20 -0800452static void Push32(std::vector<uint8_t>&buf, int data) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700453 buf.push_back(data & 0xff);
454 buf.push_back((data >> 8) & 0xff);
455 buf.push_back((data >> 16) & 0xff);
456 buf.push_back((data >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457}
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 */
464static void PushUnpatchedReference(std::vector<uint8_t>&buf) {
465 // 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
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
472 while (buf.size() < offset) {
473 buf.push_back(0);
474 }
475}
476
477/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700478void Mir2Lir::InstallLiteralPools() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 AlignBuffer(code_buffer_, data_offset_);
480 LIR* data_lir = literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700481 while (data_lir != nullptr) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800482 Push32(code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 data_lir = NEXT_LIR(data_lir);
484 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100485 // TODO: patches_.reserve() as needed.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 // Push code and method literals, record offsets for the compiler to patch.
487 data_lir = code_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700488 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700489 uint32_t target_method_idx = data_lir->operands[0];
490 const DexFile* target_dex_file =
491 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Vladimir Markof4da6752014-08-01 19:04:18 +0100492 patches_.push_back(LinkerPatch::CodePatch(code_buffer_.size(),
493 target_dex_file, target_method_idx));
Matteo Franchin27cc0932014-09-08 18:29:24 +0100494 PushUnpatchedReference(code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 data_lir = NEXT_LIR(data_lir);
496 }
497 data_lir = method_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700498 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700499 uint32_t target_method_idx = data_lir->operands[0];
500 const DexFile* target_dex_file =
501 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Vladimir Markof4da6752014-08-01 19:04:18 +0100502 patches_.push_back(LinkerPatch::MethodPatch(code_buffer_.size(),
503 target_dex_file, target_method_idx));
Matteo Franchin27cc0932014-09-08 18:29:24 +0100504 PushUnpatchedReference(code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 data_lir = NEXT_LIR(data_lir);
506 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800507 // Push class literals.
508 data_lir = class_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700509 while (data_lir != nullptr) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100510 uint32_t target_type_idx = data_lir->operands[0];
Fred Shihe7f82e22014-08-06 10:46:37 -0700511 const DexFile* class_dex_file =
512 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Vladimir Markof4da6752014-08-01 19:04:18 +0100513 patches_.push_back(LinkerPatch::TypePatch(code_buffer_.size(),
514 class_dex_file, target_type_idx));
Matteo Franchin27cc0932014-09-08 18:29:24 +0100515 PushUnpatchedReference(code_buffer_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800516 data_lir = NEXT_LIR(data_lir);
517 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518}
519
520/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700521void Mir2Lir::InstallSwitchTables() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100522 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 AlignBuffer(code_buffer_, tab_rec->offset);
524 /*
525 * For Arm, our reference point is the address of the bx
526 * instruction that does the launch, so we have to subtract
527 * the auto pc-advance. For other targets the reference point
528 * is a label, so we can use the offset as-is.
529 */
530 int bx_offset = INVALID_OFFSET;
531 switch (cu_->instruction_set) {
532 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700533 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 bx_offset = tab_rec->anchor->offset + 4;
535 break;
536 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700537 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 bx_offset = 0;
539 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100540 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 case kMips:
542 bx_offset = tab_rec->anchor->offset;
543 break;
544 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
545 }
546 if (cu_->verbose) {
547 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
548 }
549 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee0d829482013-10-11 15:24:55 -0700550 const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
552 int disp = tab_rec->targets[elems]->offset - bx_offset;
553 if (cu_->verbose) {
554 LOG(INFO) << " Case[" << elems << "] key: 0x"
555 << std::hex << keys[elems] << ", disp: 0x"
556 << std::hex << disp;
557 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800558 Push32(code_buffer_, keys[elems]);
559 Push32(code_buffer_,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 tab_rec->targets[elems]->offset - bx_offset);
561 }
562 } else {
563 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
564 static_cast<int>(Instruction::kPackedSwitchSignature));
565 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
566 int disp = tab_rec->targets[elems]->offset - bx_offset;
567 if (cu_->verbose) {
568 LOG(INFO) << " Case[" << elems << "] disp: 0x"
569 << std::hex << disp;
570 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800571 Push32(code_buffer_, tab_rec->targets[elems]->offset - bx_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 }
573 }
574 }
575}
576
577/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700578void Mir2Lir::InstallFillArrayData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100579 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 AlignBuffer(code_buffer_, tab_rec->offset);
581 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700582 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
583 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 }
585 }
586}
587
buzbee0d829482013-10-11 15:24:55 -0700588static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700589 for (; lir != nullptr; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 lir->offset = offset;
591 offset += 4;
592 }
593 return offset;
594}
595
Ian Rogersff093b32014-04-30 19:04:27 -0700596static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
597 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700598 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700599 offset = RoundUp(offset, element_size);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700600 for (; lir != nullptr; lir = lir->next) {
buzbee0d829482013-10-11 15:24:55 -0700601 lir->offset = offset;
602 offset += element_size;
603 }
604 return offset;
605}
606
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700608bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000609 MappingTable table(&encoded_mapping_table_[0]);
610 std::vector<uint32_t> dex_pcs;
611 dex_pcs.reserve(table.DexToPcSize());
612 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
613 dex_pcs.push_back(it.DexPc());
614 }
615 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
616 std::sort(dex_pcs.begin(), dex_pcs.end());
617
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000619 auto it = dex_pcs.begin(), end = dex_pcs.end();
620 for (uint32_t dex_pc : mir_graph_->catches_) {
621 while (it != end && *it < dex_pc) {
622 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
623 ++it;
624 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000626 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
628 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000629 } else {
630 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 }
632 }
633 if (!success) {
634 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
635 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000636 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 }
638 return success;
639}
640
641
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700642void Mir2Lir::CreateMappingTables() {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700643 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
644
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000645 uint32_t pc2dex_data_size = 0u;
646 uint32_t pc2dex_entries = 0u;
647 uint32_t pc2dex_offset = 0u;
648 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700649 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000650 uint32_t dex2pc_data_size = 0u;
651 uint32_t dex2pc_entries = 0u;
652 uint32_t dex2pc_offset = 0u;
653 uint32_t dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700654 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700655 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000657 pc2dex_entries += 1;
658 DCHECK(pc2dex_offset <= tgt_lir->offset);
659 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
660 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
661 static_cast<int32_t>(pc2dex_dalvik_offset));
662 pc2dex_offset = tgt_lir->offset;
663 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 }
665 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000666 dex2pc_entries += 1;
667 DCHECK(dex2pc_offset <= tgt_lir->offset);
668 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
669 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
670 static_cast<int32_t>(dex2pc_dalvik_offset));
671 dex2pc_offset = tgt_lir->offset;
672 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 }
674 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000675
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700676 if (generate_src_map) {
677 src_mapping_table_.reserve(pc2dex_src_entries);
678 }
679
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000680 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
681 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
682 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000683 encoded_mapping_table_.resize(data_size);
684 uint8_t* write_pos = &encoded_mapping_table_[0];
685 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
686 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
687 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
688 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000689
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000690 pc2dex_offset = 0u;
691 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000692 dex2pc_offset = 0u;
693 dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700694 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700695 if (generate_src_map && !tgt_lir->flags.is_nop) {
696 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
697 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
698 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000699 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
700 DCHECK(pc2dex_offset <= tgt_lir->offset);
701 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
702 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
703 static_cast<int32_t>(pc2dex_dalvik_offset));
704 pc2dex_offset = tgt_lir->offset;
705 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
706 }
707 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
708 DCHECK(dex2pc_offset <= tgt_lir->offset);
709 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
710 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
711 static_cast<int32_t>(dex2pc_dalvik_offset));
712 dex2pc_offset = tgt_lir->offset;
713 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
714 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000715 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000716 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
717 hdr_data_size + pc2dex_data_size);
718 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000719
Ian Rogers96faf5b2013-08-09 22:05:32 -0700720 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000721 CHECK(VerifyCatchEntries());
722
Ian Rogers96faf5b2013-08-09 22:05:32 -0700723 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000724 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700725 CHECK_EQ(table.TotalSize(), total_entries);
726 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000727 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000728 auto it2 = table.DexToPcBegin();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700729 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000730 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
731 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
732 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
733 ++it;
734 }
735 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
736 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
737 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
738 ++it2;
739 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700740 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000741 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000742 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700743 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744}
745
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000747 DCHECK(!encoded_mapping_table_.empty());
748 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000750 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
751 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 if (native_offset > max_native_offset) {
753 max_native_offset = native_offset;
754 }
755 }
756 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000757 const std::vector<uint8_t>& gc_map_raw =
758 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
759 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
760 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000762 GcMapBuilder native_gc_map_builder(&native_gc_map_,
763 mapping_table.PcToDexSize(),
764 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765
Vladimir Marko06606b92013-12-02 15:31:08 +0000766 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
767 uint32_t native_offset = it.NativePcOffset();
768 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700770 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
Dave Allisonf9439142014-03-27 15:10:22 -0700771 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 native_gc_map_builder.AddEntry(native_offset, references);
773 }
774}
775
776/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700777int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100779 constexpr unsigned int ptr_size = sizeof(uint32_t);
Andreas Gampe785d2f22014-11-03 22:57:30 -0800780 static_assert(ptr_size >= sizeof(mirror::HeapReference<mirror::Object>),
781 "Pointer size cannot hold a heap reference");
Ian Rogersff093b32014-04-30 19:04:27 -0700782 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
783 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
784 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 return offset;
786}
787
buzbee0d829482013-10-11 15:24:55 -0700788int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100789 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 tab_rec->offset = offset;
791 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
792 offset += tab_rec->table[1] * (sizeof(int) * 2);
793 } else {
794 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
795 static_cast<int>(Instruction::kPackedSwitchSignature));
796 offset += tab_rec->table[1] * sizeof(int);
797 }
798 }
799 return offset;
800}
801
buzbee0d829482013-10-11 15:24:55 -0700802int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100803 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700804 tab_rec->offset = offset;
805 offset += tab_rec->size;
806 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700807 offset = RoundUp(offset, 4);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100808 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 return offset;
810}
811
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812/*
813 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700814 * offset vaddr if pretty-printing, otherise use the standard block
815 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700816 * branch table during the assembly phase. All resource flags
817 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 */
buzbee0d829482013-10-11 15:24:55 -0700819LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) {
buzbee252254b2013-09-08 16:20:53 -0700820 LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id];
buzbeeb48819d2013-09-14 16:15:25 -0700821 LIR* res = boundary_lir;
822 if (cu_->verbose) {
823 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000824 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
buzbeeb48819d2013-09-14 16:15:25 -0700825 new_label->dalvik_offset = vaddr;
826 new_label->opcode = kPseudoCaseLabel;
827 new_label->operands[0] = keyVal;
828 new_label->flags.fixup = kFixupLabel;
829 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100830 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700831 InsertLIRAfter(boundary_lir, new_label);
832 res = new_label;
833 }
834 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835}
836
buzbee0d829482013-10-11 15:24:55 -0700837void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700839 DexOffset base_vaddr = tab_rec->vaddr;
840 const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 int entries = table[1];
842 int low_key = s4FromSwitchData(&table[2]);
843 for (int i = 0; i < entries; i++) {
844 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key);
845 }
846}
847
buzbee0d829482013-10-11 15:24:55 -0700848void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700850 DexOffset base_vaddr = tab_rec->vaddr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700852 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
853 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 for (int i = 0; i < entries; i++) {
855 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]);
856 }
857}
858
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700859void Mir2Lir::ProcessSwitchTables() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100860 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
862 MarkPackedCaseLabels(tab_rec);
863 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
864 MarkSparseCaseLabels(tab_rec);
865 } else {
866 LOG(FATAL) << "Invalid switch table";
867 }
868 }
869}
870
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700871void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 /*
873 * Sparse switch data format:
874 * ushort ident = 0x0200 magic value
875 * ushort size number of entries in the table; > 0
876 * int keys[size] keys, sorted low-to-high; 32-bit aligned
877 * int targets[size] branch targets, relative to switch opcode
878 *
879 * Total size is (2+size*4) 16-bit code units.
880 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 uint16_t ident = table[0];
882 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700883 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
884 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
886 << ", entries: " << std::dec << entries;
887 for (int i = 0; i < entries; i++) {
888 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
889 }
890}
891
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700892void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 /*
894 * Packed switch data format:
895 * ushort ident = 0x0100 magic value
896 * ushort size number of entries in the table
897 * int first_key first (and lowest) switch case value
898 * int targets[size] branch targets, relative to switch opcode
899 *
900 * Total size is (4+size*2) 16-bit code units.
901 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700903 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 int entries = table[1];
905 int low_key = s4FromSwitchData(&table[2]);
906 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
907 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
908 for (int i = 0; i < entries; i++) {
909 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
910 << targets[i];
911 }
912}
913
buzbee252254b2013-09-08 16:20:53 -0700914/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700915void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700916 UNUSED(offset);
buzbee0d829482013-10-11 15:24:55 -0700917 // NOTE: only used for debug listings.
918 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919}
920
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700921bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 bool is_taken;
923 switch (opcode) {
924 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
925 case Instruction::IF_NE: is_taken = (src1 != src2); break;
926 case Instruction::IF_LT: is_taken = (src1 < src2); break;
927 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
928 case Instruction::IF_GT: is_taken = (src1 > src2); break;
929 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
930 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
931 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
932 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
933 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
934 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
935 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
936 default:
937 LOG(FATAL) << "Unexpected opcode " << opcode;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700938 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 }
940 return is_taken;
941}
942
943// Convert relation of src1/src2 to src2/src1
944ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
945 ConditionCode res;
946 switch (before) {
947 case kCondEq: res = kCondEq; break;
948 case kCondNe: res = kCondNe; break;
949 case kCondLt: res = kCondGt; break;
950 case kCondGt: res = kCondLt; break;
951 case kCondLe: res = kCondGe; break;
952 case kCondGe: res = kCondLe; break;
953 default:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700955 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 }
957 return res;
958}
959
Vladimir Markoa1a70742014-03-03 10:28:05 +0000960ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
961 ConditionCode res;
962 switch (before) {
963 case kCondEq: res = kCondNe; break;
964 case kCondNe: res = kCondEq; break;
965 case kCondLt: res = kCondGe; break;
966 case kCondGt: res = kCondLe; break;
967 case kCondLe: res = kCondGt; break;
968 case kCondGe: res = kCondLt; break;
969 default:
Vladimir Markoa1a70742014-03-03 10:28:05 +0000970 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700971 UNREACHABLE();
Vladimir Markoa1a70742014-03-03 10:28:05 +0000972 }
973 return res;
974}
975
Brian Carlstrom7940e442013-07-12 13:46:57 -0700976// TODO: move to mir_to_lir.cc
977Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
978 : Backend(arena),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700979 literal_list_(nullptr),
980 method_literal_list_(nullptr),
981 class_literal_list_(nullptr),
982 code_literal_list_(nullptr),
983 first_fixup_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 cu_(cu),
985 mir_graph_(mir_graph),
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100986 switch_tables_(arena->Adapter(kArenaAllocSwitchTable)),
987 fill_array_data_(arena->Adapter(kArenaAllocFillArrayData)),
988 tempreg_info_(arena->Adapter()),
989 reginfo_map_(arena->Adapter()),
990 pointer_storage_(arena->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991 data_offset_(0),
992 total_size_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700993 block_label_list_(nullptr),
994 promotion_map_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 current_dalvik_offset_(0),
buzbeeb48819d2013-09-14 16:15:25 -0700996 estimated_native_code_size_(0),
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100997 reg_pool_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 live_sreg_(0),
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100999 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1000 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Vladimir Markof4da6752014-08-01 19:04:18 +01001001 patches_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 num_core_spills_(0),
1003 num_fp_spills_(0),
1004 frame_size_(0),
1005 core_spill_mask_(0),
1006 fp_spill_mask_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001007 first_lir_insn_(nullptr),
1008 last_lir_insn_(nullptr),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001009 slow_paths_(arena->Adapter(kArenaAllocSlowPaths)),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001010 mem_ref_type_(ResourceMask::kHeapRef),
1011 mask_cache_(arena) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001012 switch_tables_.reserve(4);
1013 fill_array_data_.reserve(4);
1014 tempreg_info_.reserve(20);
1015 reginfo_map_.reserve(RegStorage::kMaxRegs);
1016 pointer_storage_.reserve(128);
1017 slow_paths_.reserve(32);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001018 // Reserve pointer id 0 for nullptr.
1019 size_t null_idx = WrapPointer(nullptr);
buzbee0d829482013-10-11 15:24:55 -07001020 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021}
1022
1023void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001024 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1026
1027 /* Allocate Registers using simple local allocation scheme */
1028 SimpleRegAlloc();
1029
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001030 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001031 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001032 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001033 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001035 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1036 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037 MethodMIR2LIR();
1038 }
1039
1040 /* Method is not empty */
1041 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 // mark the targets of switch statement case labels
1043 ProcessSwitchTables();
1044
1045 /* Convert LIR into machine code. */
1046 AssembleLIR();
1047
buzbeeb01bf152014-05-13 15:59:07 -07001048 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001049 CodegenDump();
1050 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052}
1053
1054CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001055 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1056 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001058 // Prefix the encoded data with its size.
1059 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1060 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1061 vmap_encoder.PushBackUnsigned(size);
1062 // Core regs may have been inserted out of order - sort first.
1063 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1064 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1065 // Copy, stripping out the phys register sort key.
1066 vmap_encoder.PushBackUnsigned(
1067 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1068 }
1069 // Push a marker to take place of lr.
1070 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001071 if (cu_->instruction_set == kThumb2) {
1072 // fp regs already sorted.
1073 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1074 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1075 }
1076 } else {
1077 // For other platforms regs may have been inserted out of order - sort first.
1078 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1079 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1080 // Copy, stripping out the phys register sort key.
1081 vmap_encoder.PushBackUnsigned(
1082 ~(-1 << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
1083 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001084 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001086 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1087 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001088 DCHECK_EQ(core_vmap_table_.size(), 0u);
1089 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1090 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001092
Vladimir Markof4da6752014-08-01 19:04:18 +01001093 // Sort patches by literal offset for better deduplication.
1094 std::sort(patches_.begin(), patches_.end(), [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
1095 return lhs.LiteralOffset() < rhs.LiteralOffset();
1096 });
1097
Tong Shen547cdfd2014-08-05 01:54:19 -07001098 std::unique_ptr<std::vector<uint8_t>> cfi_info(ReturnFrameDescriptionEntry());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 CompiledMethod* result =
Ian Rogers72d32622014-05-06 16:20:11 -07001100 new CompiledMethod(cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_,
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001101 core_spill_mask_, fp_spill_mask_, &src_mapping_table_, encoded_mapping_table_,
Vladimir Markof4da6752014-08-01 19:04:18 +01001102 vmap_encoder.GetData(), native_gc_map_, cfi_info.get(),
1103 ArrayRef<LinkerPatch>(patches_));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 return result;
1105}
1106
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001107size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1108 // Chose a reasonably small value in order to contain stack growth.
1109 // Backends that are smarter about spill region can return larger values.
1110 const size_t max_compiler_temps = 10;
1111 return max_compiler_temps;
1112}
1113
1114size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1115 // By default assume that the Mir2Lir will need one slot for each temporary.
1116 // If the backend can better determine temps that have non-overlapping ranges and
1117 // temps that do not need spilled, it can actually provide a small region.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001118 mir_graph_->CommitCompilerTemps();
1119 return mir_graph_->GetNumBytesForSpecialTemps() + mir_graph_->GetMaximumBytesForNonSpecialTemps();
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001120}
1121
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122int Mir2Lir::ComputeFrameSize() {
1123 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001124 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1125 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1126 + sizeof(uint32_t) // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001127 + mir_graph_->GetNumOfLocalCodeVRs() * sizeof(uint32_t)
1128 + mir_graph_->GetNumOfOutVRs() * sizeof(uint32_t)
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001129 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001131 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132}
1133
1134/*
1135 * Append an LIR instruction to the LIR list maintained by a compilation
1136 * unit
1137 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001138void Mir2Lir::AppendLIR(LIR* lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001139 if (first_lir_insn_ == nullptr) {
1140 DCHECK(last_lir_insn_ == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 last_lir_insn_ = first_lir_insn_ = lir;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001142 lir->prev = lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 } else {
1144 last_lir_insn_->next = lir;
1145 lir->prev = last_lir_insn_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001146 lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 last_lir_insn_ = lir;
1148 }
1149}
1150
1151/*
1152 * Insert an LIR instruction before the current instruction, which cannot be the
1153 * first instruction.
1154 *
1155 * prev_lir <-> new_lir <-> current_lir
1156 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001157void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001158 DCHECK(current_lir->prev != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 LIR *prev_lir = current_lir->prev;
1160
1161 prev_lir->next = new_lir;
1162 new_lir->prev = prev_lir;
1163 new_lir->next = current_lir;
1164 current_lir->prev = new_lir;
1165}
1166
1167/*
1168 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001169 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 *
1171 * current_lir -> new_lir -> old_next
1172 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001173void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 new_lir->prev = current_lir;
1175 new_lir->next = current_lir->next;
1176 current_lir->next = new_lir;
1177 new_lir->next->prev = new_lir;
1178}
1179
Mark Mendell4708dcd2014-01-22 09:05:18 -08001180bool Mir2Lir::IsPowerOfTwo(uint64_t x) {
1181 return (x & (x - 1)) == 0;
1182}
1183
1184// Returns the index of the lowest set bit in 'x'.
1185int32_t Mir2Lir::LowestSetBit(uint64_t x) {
1186 int bit_posn = 0;
1187 while ((x & 0xf) == 0) {
1188 bit_posn += 4;
1189 x >>= 4;
1190 }
1191 while ((x & 1) == 0) {
1192 bit_posn++;
1193 x >>= 1;
1194 }
1195 return bit_posn;
1196}
1197
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001198bool Mir2Lir::PartiallyIntersects(RegLocation rl_src, RegLocation rl_dest) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001199 DCHECK(rl_src.wide);
1200 DCHECK(rl_dest.wide);
1201 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1202}
1203
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001204bool Mir2Lir::Intersects(RegLocation rl_src, RegLocation rl_dest) {
1205 DCHECK(rl_src.wide);
1206 DCHECK(rl_dest.wide);
1207 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) <= 1);
1208}
1209
buzbee2700f7e2014-03-07 09:46:20 -08001210LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001211 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001212 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001213 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1214 if (compare != nullptr) {
1215 *compare = inst;
1216 }
Mark Mendell766e9292014-01-27 07:55:47 -08001217 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1218 return branch;
1219}
1220
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001221void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001222 slow_paths_.push_back(slowpath);
Serguei Katkov589e0462014-09-05 18:37:22 +07001223 ResetDefTracking();
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001224}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001225
Jeff Hao49161ce2014-03-12 11:05:25 -07001226void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1227 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001228 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001229 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001230 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001231 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001232 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1233 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1234 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001235 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001236 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001237 // Loads a code pointer. Code from oat file can be mapped anywhere.
1238 LIR* load_pc_rel = OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001239 AppendLIR(load_pc_rel);
1240 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1241}
1242
Jeff Hao49161ce2014-03-12 11:05:25 -07001243void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1244 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001245 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001246 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001247 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001248 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001249 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1250 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1251 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001252 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001253 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001254 // Loads an ArtMethod pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001255 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001256 AppendLIR(load_pc_rel);
1257 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1258}
1259
Fred Shihe7f82e22014-08-06 10:46:37 -07001260void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1261 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001262 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001263 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001264 if (data_target == nullptr) {
1265 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001266 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001267 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001268 // Loads a Class pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001269 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001270 AppendLIR(load_pc_rel);
1271}
1272
Tong Shen547cdfd2014-08-05 01:54:19 -07001273std::vector<uint8_t>* Mir2Lir::ReturnFrameDescriptionEntry() {
Mark Mendellae9fd932014-02-10 16:14:35 -08001274 // Default case is to do nothing.
1275 return nullptr;
1276}
1277
buzbee2700f7e2014-03-07 09:46:20 -08001278RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001279 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001280 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001281 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001282 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1283 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1284 info_lo->SetIsWide(false);
1285 info_hi->SetIsWide(false);
1286 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001287 } else {
buzbee85089dd2014-05-25 15:10:52 -07001288 RegisterInfo* info = GetRegInfo(loc.reg);
1289 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1290 DCHECK(info_new != nullptr);
1291 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1292 info->MarkDead();
1293 info_new->MarkLive(loc.s_reg_low);
1294 }
1295 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001296 }
buzbee85089dd2014-05-25 15:10:52 -07001297 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001298 }
buzbee85089dd2014-05-25 15:10:52 -07001299 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001300 return loc;
1301}
1302
Mark Mendelld65c51a2014-04-29 16:55:20 -04001303void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001304 UNUSED(bb, mir);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001305 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001306 UNREACHABLE();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001307}
1308
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001309} // namespace art