blob: bd55fa64e663f7fdb7316f50de66becc1e8046c0 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe0b9203e2015-01-22 20:39:27 -080017#include "mir_to_lir-inl.h"
18
Vladimir Markoec7802a2015-10-01 20:57:57 +010019#include <endian.h>
20
Vladimir Marko767c7522015-03-20 12:47:30 +000021#include "base/bit_vector-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080022#include "dex/mir_graph.h"
23#include "driver/compiler_driver.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070024#include "driver/compiler_options.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080025#include "driver/dex_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "dex_file-inl.h"
27#include "gc_map.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000028#include "gc_map_builder.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070029#include "mapping_table.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000030#include "dex/quick/dex_file_method_inliner.h"
31#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000032#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000033#include "dex/verified_method.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000034#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "verifier/dex_gc_map.h"
36#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000037#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070038
39namespace art {
40
Vladimir Marko06606b92013-12-02 15:31:08 +000041namespace {
42
43/* Dump a mapping table */
44template <typename It>
45void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
46 const Signature& signature, uint32_t size, It first) {
47 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080048 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000049 descriptor, name, signature.ToString().c_str(), size));
50 std::replace(line.begin(), line.end(), ';', '_');
51 LOG(INFO) << line;
52 for (uint32_t i = 0; i != size; ++i) {
53 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
54 ++first;
55 LOG(INFO) << line;
56 }
57 LOG(INFO) <<" };\n\n";
58 }
59}
60
61} // anonymous namespace
62
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070063bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 bool res = false;
65 if (rl_src.is_const) {
66 if (rl_src.wide) {
Andreas Gampede0b9962014-08-27 14:24:42 -070067 // For wide registers, check whether we're the high partner. In that case we need to switch
68 // to the lower one for the correct value.
69 if (rl_src.high_word) {
70 rl_src.high_word = false;
71 rl_src.s_reg_low--;
72 rl_src.orig_sreg--;
73 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070075 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070077 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 }
79 } else {
80 if (rl_src.fp) {
Andreas Gampede0b9962014-08-27 14:24:42 -070081 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 } else {
Andreas Gampede0b9962014-08-27 14:24:42 -070083 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 }
85 }
86 }
87 return res;
88}
89
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070090void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070091 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010092 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010094 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +000095 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
96 safepoints_.emplace_back(safepoint_pc, current_mir_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070097}
98
Andreas Gampe3c12c512014-06-24 18:46:29 +000099void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
100 DCHECK(!after->flags.use_def_invalid);
101 after->u.m.def_mask = &kEncodeAll;
102 // As NewLIR0 uses Append, we need to create the LIR by hand.
103 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
104 if (after->next == nullptr) {
105 DCHECK_EQ(after, last_lir_insn_);
106 AppendLIR(safepoint_pc);
107 } else {
108 InsertLIRAfter(after, safepoint_pc);
109 }
110 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Vladimir Marko767c7522015-03-20 12:47:30 +0000111 DCHECK(current_mir_ != nullptr || (current_dalvik_offset_ == 0 && safepoints_.empty()));
112 safepoints_.emplace_back(safepoint_pc, current_mir_);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000113}
114
buzbee252254b2013-09-08 16:20:53 -0700115/* Remove a LIR from the list. */
116void Mir2Lir::UnlinkLIR(LIR* lir) {
117 if (UNLIKELY(lir == first_lir_insn_)) {
118 first_lir_insn_ = lir->next;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700119 if (lir->next != nullptr) {
120 lir->next->prev = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700121 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700122 DCHECK(lir->next == nullptr);
buzbee252254b2013-09-08 16:20:53 -0700123 DCHECK(lir == last_lir_insn_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700124 last_lir_insn_ = nullptr;
buzbee252254b2013-09-08 16:20:53 -0700125 }
126 } else if (lir == last_lir_insn_) {
127 last_lir_insn_ = lir->prev;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700128 lir->prev->next = nullptr;
129 } else if ((lir->prev != nullptr) && (lir->next != nullptr)) {
buzbee252254b2013-09-08 16:20:53 -0700130 lir->prev->next = lir->next;
131 lir->next->prev = lir->prev;
132 }
133}
134
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700136void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700138 if (!cu_->verbose) {
139 UnlinkLIR(lir);
140 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141}
142
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700143void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700145 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100146 // TODO: Avoid the extra Arena allocation!
147 const ResourceMask** mask_ptr;
148 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700150 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700152 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100154 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100156 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 /* ..and then add back the one we need */
158 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100159 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100161 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100163 case ResourceMask::kDalvikReg:
164 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100166 case ResourceMask::kHeapRef:
167 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100169 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 /* Currently only loads can be marked as kMustNotAlias */
171 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100172 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 break;
174 default:
175 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
176 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100177 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178}
179
180/*
181 * Mark load/store instructions that access Dalvik registers through the stack.
182 */
183void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700184 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100185 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
186 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187
188 /*
189 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
190 * access.
191 */
buzbeeb48819d2013-09-14 16:15:25 -0700192 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193}
194
195/*
196 * Debugging macros
197 */
198#define DUMP_RESOURCE_MASK(X)
199
200/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700201void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 int offset = lir->offset;
203 int dest = lir->operands[0];
204 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
205
206 /* Handle pseudo-ops individually, and all regular insns as a group */
207 switch (lir->opcode) {
David Srbecky6f715892015-03-30 14:21:42 +0100208 case kPseudoPrologueBegin:
209 LOG(INFO) << "-------- PrologueBegin";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 break;
David Srbecky6f715892015-03-30 14:21:42 +0100211 case kPseudoPrologueEnd:
212 LOG(INFO) << "-------- PrologueEnd";
213 break;
214 case kPseudoEpilogueBegin:
215 LOG(INFO) << "-------- EpilogueBegin";
216 break;
217 case kPseudoEpilogueEnd:
218 LOG(INFO) << "-------- EpilogueEnd";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 break;
220 case kPseudoBarrier:
221 LOG(INFO) << "-------- BARRIER";
222 break;
223 case kPseudoEntryBlock:
224 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
225 break;
226 case kPseudoDalvikByteCodeBoundary:
227 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700228 // NOTE: only used for debug listings.
229 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 }
231 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000232 << lir->dalvik_offset << " @ "
Vladimir Markof6737f72015-03-23 17:05:14 +0000233 << UnwrapPointer<char>(lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 break;
235 case kPseudoExitBlock:
236 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
237 break;
238 case kPseudoPseudoAlign4:
239 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
240 << offset << "): .align4";
241 break;
242 case kPseudoEHBlockLabel:
243 LOG(INFO) << "Exception_Handling:";
244 break;
245 case kPseudoTargetLabel:
246 case kPseudoNormalBlockLabel:
247 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
248 break;
249 case kPseudoThrowTarget:
250 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
251 break;
252 case kPseudoIntrinsicRetry:
253 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
254 break;
255 case kPseudoSuspendTarget:
256 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
257 break;
258 case kPseudoSafepointPC:
259 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
260 break;
261 case kPseudoExportedPC:
262 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
263 break;
264 case kPseudoCaseLabel:
265 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
266 << std::hex << lir->operands[0] << "|" << std::dec <<
267 lir->operands[0];
268 break;
269 default:
270 if (lir->flags.is_nop && !dump_nop) {
271 break;
272 } else {
273 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
274 lir, base_addr));
275 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
276 lir, base_addr));
David Srbecky6f715892015-03-30 14:21:42 +0100277 LOG(INFO) << StringPrintf("%5p|0x%02x: %-9s%s%s",
Ian Rogers107c31e2014-01-23 20:55:29 -0800278 base_addr + offset,
David Srbecky6f715892015-03-30 14:21:42 +0100279 lir->dalvik_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 op_name.c_str(), op_operands.c_str(),
281 lir->flags.is_nop ? "(nop)" : "");
282 }
283 break;
284 }
285
buzbeeb48819d2013-09-14 16:15:25 -0700286 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100287 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 }
buzbeeb48819d2013-09-14 16:15:25 -0700289 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100290 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 }
292}
293
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700294void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700295 uint32_t num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
296 for (uint32_t i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 PromotionMap v_reg_map = promotion_map_[i];
298 std::string buf;
299 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700300 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 }
302
303 std::string buf3;
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700304 if (i < mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 StringAppendF(&buf3, "%02d", i);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700306 } else if (i == mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 buf3 = "Method*";
308 } else {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700309 uint32_t diff = i - mir_graph_->GetNumOfCodeVRs();
310 StringAppendF(&buf3, "ct%d", diff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 }
312
313 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
314 v_reg_map.core_location == kLocPhysReg ?
315 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
316 v_reg_map.core_reg : SRegOffset(i),
317 buf.c_str());
318 }
319}
320
buzbee7a11ab02014-04-28 20:02:38 -0700321void Mir2Lir::UpdateLIROffsets() {
322 // Only used for code listings.
323 size_t offset = 0;
324 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
325 lir->offset = offset;
326 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
327 offset += GetInsnSize(lir);
328 } else if (lir->opcode == kPseudoPseudoAlign4) {
329 offset += (offset & 0x2);
330 }
331 }
332}
333
Vladimir Marko743b98c2014-11-24 19:45:41 +0000334void Mir2Lir::MarkGCCard(int opt_flags, RegStorage val_reg, RegStorage tgt_addr_reg) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000335 DCHECK(val_reg.Valid());
336 DCHECK_EQ(val_reg.Is64Bit(), cu_->target64);
Vladimir Marko743b98c2014-11-24 19:45:41 +0000337 if ((opt_flags & MIR_STORE_NON_NULL_VALUE) != 0) {
338 UnconditionallyMarkGCCard(tgt_addr_reg);
339 } else {
340 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, nullptr);
341 UnconditionallyMarkGCCard(tgt_addr_reg);
342 LIR* target = NewLIR0(kPseudoTargetLabel);
343 branch_over->target = target;
344 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000345}
346
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700348void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 LOG(INFO) << "Dumping LIR insns for "
350 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
351 LIR* lir_insn;
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700352 int insns_size = mir_graph_->GetNumDalvikInsns();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700354 LOG(INFO) << "Regs (excluding ins) : " << mir_graph_->GetNumOfLocalCodeVRs();
355 LOG(INFO) << "Ins : " << mir_graph_->GetNumOfInVRs();
356 LOG(INFO) << "Outs : " << mir_graph_->GetNumOfOutVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 LOG(INFO) << "CoreSpills : " << num_core_spills_;
358 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800359 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 LOG(INFO) << "Frame size : " << frame_size_;
361 LOG(INFO) << "code size is " << total_size_ <<
362 " bytes, Dalvik size is " << insns_size * 2;
363 LOG(INFO) << "expansion factor: "
364 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
365 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700366 UpdateLIROffsets();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700367 for (lir_insn = first_lir_insn_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 DumpLIRInsn(lir_insn, 0);
369 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700370 for (lir_insn = literal_list_; lir_insn != nullptr; lir_insn = lir_insn->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
372 lir_insn->operands[0]);
373 }
374
375 const DexFile::MethodId& method_id =
376 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700377 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
378 const char* name = cu_->dex_file->GetMethodName(method_id);
379 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380
381 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000382 if (!encoded_mapping_table_.empty()) {
383 MappingTable table(&encoded_mapping_table_[0]);
384 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
385 table.PcToDexSize(), table.PcToDexBegin());
386 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
387 table.DexToPcSize(), table.DexToPcBegin());
388 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389}
390
391/*
392 * Search the existing constants in the literal pool for an exact or close match
393 * within specified delta (greater or equal to 0).
394 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700395LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 while (data_target) {
397 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
398 return data_target;
399 data_target = data_target->next;
400 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700401 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402}
403
404/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700405LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 bool lo_match = false;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700407 LIR* lo_target = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 while (data_target) {
409 if (lo_match && (data_target->operands[0] == val_hi)) {
410 // Record high word in case we need to expand this later.
411 lo_target->operands[1] = val_hi;
412 return lo_target;
413 }
414 lo_match = false;
415 if (data_target->operands[0] == val_lo) {
416 lo_match = true;
417 lo_target = data_target;
418 }
419 data_target = data_target->next;
420 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700421 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422}
423
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100424/* Search the existing constants in the literal pool for an exact method match */
425LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
426 while (data_target) {
427 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000428 UnwrapPointer<DexFile>(data_target->operands[1]) == method.dex_file) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100429 return data_target;
430 }
431 data_target = data_target->next;
432 }
433 return nullptr;
434}
435
Fred Shihe7f82e22014-08-06 10:46:37 -0700436/* Search the existing constants in the literal pool for an exact class match */
437LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
438 while (data_target) {
439 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
Vladimir Markof6737f72015-03-23 17:05:14 +0000440 UnwrapPointer<DexFile>(data_target->operands[1]) == &dex_file) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700441 return data_target;
442 }
443 data_target = data_target->next;
444 }
445 return nullptr;
446}
447
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448/*
449 * The following are building blocks to insert constants into the pool or
450 * instruction streams.
451 */
452
453/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700454LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 /* Add the constant to the literal pool */
456 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000457 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 new_value->operands[0] = value;
459 new_value->next = *constant_list_p;
460 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700461 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 return new_value;
463 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700464 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465}
466
467/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700468LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 AddWordData(constant_list_p, val_hi);
470 return AddWordData(constant_list_p, val_lo);
471}
472
Matteo Franchin27cc0932014-09-08 18:29:24 +0100473/**
474 * @brief Push a compressed reference which needs patching at link/patchoat-time.
475 * @details This needs to be kept consistent with the code which actually does the patching in
476 * oat_writer.cc and in the patchoat tool.
477 */
Vladimir Marko80b96d12015-02-19 15:50:28 +0000478static void PushUnpatchedReference(CodeBuffer* buf) {
Matteo Franchin27cc0932014-09-08 18:29:24 +0100479 // Note that we can safely initialize the patches to zero. The code deduplication mechanism takes
480 // the patches into account when determining whether two pieces of codes are functionally
481 // equivalent.
482 Push32(buf, UINT32_C(0));
buzbee0d829482013-10-11 15:24:55 -0700483}
484
Vladimir Marko80b96d12015-02-19 15:50:28 +0000485static void AlignBuffer(CodeBuffer* buf, size_t offset) {
486 DCHECK_LE(buf->size(), offset);
487 buf->insert(buf->end(), offset - buf->size(), 0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488}
489
490/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700491void Mir2Lir::InstallLiteralPools() {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000492 AlignBuffer(&code_buffer_, data_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 LIR* data_lir = literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700494 while (data_lir != nullptr) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000495 Push32(&code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 data_lir = NEXT_LIR(data_lir);
497 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100498 // TODO: patches_.reserve() as needed.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 // Push code and method literals, record offsets for the compiler to patch.
500 data_lir = code_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700501 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700502 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000503 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100504 patches_.push_back(LinkerPatch::CodePatch(code_buffer_.size(),
505 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000506 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 data_lir = NEXT_LIR(data_lir);
508 }
509 data_lir = method_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700510 while (data_lir != nullptr) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700511 uint32_t target_method_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000512 const DexFile* target_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100513 patches_.push_back(LinkerPatch::MethodPatch(code_buffer_.size(),
514 target_dex_file, target_method_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000515 PushUnpatchedReference(&code_buffer_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 data_lir = NEXT_LIR(data_lir);
517 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800518 // Push class literals.
519 data_lir = class_literal_list_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700520 while (data_lir != nullptr) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100521 uint32_t target_type_idx = data_lir->operands[0];
Vladimir Markof6737f72015-03-23 17:05:14 +0000522 const DexFile* class_dex_file = UnwrapPointer<DexFile>(data_lir->operands[1]);
Vladimir Markof4da6752014-08-01 19:04:18 +0100523 patches_.push_back(LinkerPatch::TypePatch(code_buffer_.size(),
524 class_dex_file, target_type_idx));
Vladimir Marko80b96d12015-02-19 15:50:28 +0000525 PushUnpatchedReference(&code_buffer_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800526 data_lir = NEXT_LIR(data_lir);
527 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528}
529
530/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700531void Mir2Lir::InstallSwitchTables() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100532 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000533 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 /*
535 * For Arm, our reference point is the address of the bx
536 * instruction that does the launch, so we have to subtract
537 * the auto pc-advance. For other targets the reference point
538 * is a label, so we can use the offset as-is.
539 */
540 int bx_offset = INVALID_OFFSET;
541 switch (cu_->instruction_set) {
542 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700543 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 bx_offset = tab_rec->anchor->offset + 4;
545 break;
Mark Mendell27dee8b2014-12-01 19:06:12 -0500546 case kX86_64:
547 // RIP relative to switch table.
548 bx_offset = tab_rec->offset;
549 break;
Vladimir Marko1961b602015-04-08 20:51:48 +0100550 case kX86:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100551 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100553 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 bx_offset = tab_rec->anchor->offset;
555 break;
556 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
557 }
558 if (cu_->verbose) {
559 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
560 }
561 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800562 DCHECK(tab_rec->switch_mir != nullptr);
563 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
564 DCHECK(bb != nullptr);
565 int elems = 0;
566 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
567 int key = successor_block_info->key;
568 int target = successor_block_info->block;
569 LIR* boundary_lir = InsertCaseLabel(target, key);
570 DCHECK(boundary_lir != nullptr);
571 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000572 Push32(&code_buffer_, key);
573 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 if (cu_->verbose) {
575 LOG(INFO) << " Case[" << elems << "] key: 0x"
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800576 << std::hex << key << ", disp: 0x"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 << std::hex << disp;
578 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800579 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800581 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 } else {
583 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
584 static_cast<int>(Instruction::kPackedSwitchSignature));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800585 DCHECK(tab_rec->switch_mir != nullptr);
586 BasicBlock* bb = mir_graph_->GetBasicBlock(tab_rec->switch_mir->bb);
587 DCHECK(bb != nullptr);
588 int elems = 0;
589 int low_key = s4FromSwitchData(&tab_rec->table[2]);
590 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
591 int key = successor_block_info->key;
592 DCHECK_EQ(elems + low_key, key);
593 int target = successor_block_info->block;
594 LIR* boundary_lir = InsertCaseLabel(target, key);
595 DCHECK(boundary_lir != nullptr);
596 int disp = boundary_lir->offset - bx_offset;
Vladimir Marko80b96d12015-02-19 15:50:28 +0000597 Push32(&code_buffer_, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 if (cu_->verbose) {
599 LOG(INFO) << " Case[" << elems << "] disp: 0x"
600 << std::hex << disp;
601 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800602 elems++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 }
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800604 DCHECK_EQ(elems, tab_rec->table[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 }
606 }
607}
608
609/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700610void Mir2Lir::InstallFillArrayData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100611 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Vladimir Marko80b96d12015-02-19 15:50:28 +0000612 AlignBuffer(&code_buffer_, tab_rec->offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700614 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
615 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 }
617 }
618}
619
buzbee0d829482013-10-11 15:24:55 -0700620static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700621 for (; lir != nullptr; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 lir->offset = offset;
623 offset += 4;
624 }
625 return offset;
626}
627
Ian Rogersff093b32014-04-30 19:04:27 -0700628static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
629 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700630 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700631 offset = RoundUp(offset, element_size);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700632 for (; lir != nullptr; lir = lir->next) {
buzbee0d829482013-10-11 15:24:55 -0700633 lir->offset = offset;
634 offset += element_size;
635 }
636 return offset;
637}
638
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700640bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000641 MappingTable table(&encoded_mapping_table_[0]);
642 std::vector<uint32_t> dex_pcs;
643 dex_pcs.reserve(table.DexToPcSize());
644 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
645 dex_pcs.push_back(it.DexPc());
646 }
647 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
648 std::sort(dex_pcs.begin(), dex_pcs.end());
649
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000651 auto it = dex_pcs.begin(), end = dex_pcs.end();
652 for (uint32_t dex_pc : mir_graph_->catches_) {
653 while (it != end && *it < dex_pc) {
654 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
655 ++it;
656 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000658 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
660 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000661 } else {
662 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 }
664 }
665 if (!success) {
666 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
667 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000668 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 }
670 return success;
671}
672
673
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700674void Mir2Lir::CreateMappingTables() {
David Srbecky8363c772015-05-28 16:12:43 +0100675 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetGenerateDebugInfo();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700676
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000677 uint32_t pc2dex_data_size = 0u;
678 uint32_t pc2dex_entries = 0u;
679 uint32_t pc2dex_offset = 0u;
680 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700681 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000682 uint32_t dex2pc_data_size = 0u;
683 uint32_t dex2pc_entries = 0u;
684 uint32_t dex2pc_offset = 0u;
685 uint32_t dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700686 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700687 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000689 pc2dex_entries += 1;
690 DCHECK(pc2dex_offset <= tgt_lir->offset);
691 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
692 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
693 static_cast<int32_t>(pc2dex_dalvik_offset));
694 pc2dex_offset = tgt_lir->offset;
695 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 }
697 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000698 dex2pc_entries += 1;
699 DCHECK(dex2pc_offset <= tgt_lir->offset);
700 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
701 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
702 static_cast<int32_t>(dex2pc_dalvik_offset));
703 dex2pc_offset = tgt_lir->offset;
704 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 }
706 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000707
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700708 if (generate_src_map) {
709 src_mapping_table_.reserve(pc2dex_src_entries);
710 }
711
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000712 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
713 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
714 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000715 encoded_mapping_table_.resize(data_size);
716 uint8_t* write_pos = &encoded_mapping_table_[0];
717 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
718 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
719 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
720 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000721
David Srbecky6f715892015-03-30 14:21:42 +0100722 bool is_in_prologue_or_epilogue = false;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000723 pc2dex_offset = 0u;
724 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000725 dex2pc_offset = 0u;
726 dex2pc_dalvik_offset = 0u;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700727 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
David Srbecky6f715892015-03-30 14:21:42 +0100728 if (generate_src_map && !tgt_lir->flags.is_nop && tgt_lir->opcode >= 0) {
729 if (!is_in_prologue_or_epilogue) {
730 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
731 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
732 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700733 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000734 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
735 DCHECK(pc2dex_offset <= tgt_lir->offset);
736 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
737 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
738 static_cast<int32_t>(pc2dex_dalvik_offset));
739 pc2dex_offset = tgt_lir->offset;
740 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
741 }
742 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
743 DCHECK(dex2pc_offset <= tgt_lir->offset);
744 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
745 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
746 static_cast<int32_t>(dex2pc_dalvik_offset));
747 dex2pc_offset = tgt_lir->offset;
748 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
749 }
David Srbecky6f715892015-03-30 14:21:42 +0100750 if (tgt_lir->opcode == kPseudoPrologueBegin || tgt_lir->opcode == kPseudoEpilogueBegin) {
751 is_in_prologue_or_epilogue = true;
752 }
753 if (tgt_lir->opcode == kPseudoPrologueEnd || tgt_lir->opcode == kPseudoEpilogueEnd) {
754 is_in_prologue_or_epilogue = false;
755 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000756 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000757 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
758 hdr_data_size + pc2dex_data_size);
759 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000760
Ian Rogers96faf5b2013-08-09 22:05:32 -0700761 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000762 CHECK(VerifyCatchEntries());
763
Ian Rogers96faf5b2013-08-09 22:05:32 -0700764 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000765 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700766 CHECK_EQ(table.TotalSize(), total_entries);
767 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000768 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000769 auto it2 = table.DexToPcBegin();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700770 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != nullptr; tgt_lir = NEXT_LIR(tgt_lir)) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000771 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
772 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
773 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
774 ++it;
775 }
776 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
777 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
778 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
779 ++it2;
780 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700781 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000782 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000783 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700784 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785}
786
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko767c7522015-03-20 12:47:30 +0000788 if (UNLIKELY((cu_->disable_opt & (1u << kPromoteRegs)) != 0u)) {
789 // If we're not promoting to physical registers, it's safe to use the verifier's notion of
790 // references. (We disable register promotion when type inference finds a type conflict and
791 // in that the case we defer to the verifier to avoid using the compiler's conflicting info.)
792 CreateNativeGcMapWithoutRegisterPromotion();
793 return;
794 }
795
796 ArenaBitVector* references = new (arena_) ArenaBitVector(arena_, mir_graph_->GetNumSSARegs(),
797 false);
798
799 // Calculate max native offset and max reference vreg.
800 MIR* prev_mir = nullptr;
801 int max_ref_vreg = -1;
802 CodeOffset max_native_offset = 0u;
803 for (const auto& entry : safepoints_) {
804 uint32_t native_offset = entry.first->offset;
805 max_native_offset = std::max(max_native_offset, native_offset);
806 MIR* mir = entry.second;
807 UpdateReferenceVRegs(mir, prev_mir, references);
808 max_ref_vreg = std::max(max_ref_vreg, references->GetHighestBitSet());
809 prev_mir = mir;
810 }
811
Vladimir Marko6e071832015-03-25 11:13:39 +0000812#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
813 static constexpr bool kLittleEndian = true;
814#else
815 static constexpr bool kLittleEndian = false;
816#endif
817
Vladimir Marko767c7522015-03-20 12:47:30 +0000818 // Build the GC map.
819 uint32_t reg_width = static_cast<uint32_t>((max_ref_vreg + 8) / 8);
820 GcMapBuilder native_gc_map_builder(&native_gc_map_,
821 safepoints_.size(),
822 max_native_offset, reg_width);
Vladimir Marko6e071832015-03-25 11:13:39 +0000823 if (kLittleEndian) {
824 for (const auto& entry : safepoints_) {
825 uint32_t native_offset = entry.first->offset;
826 MIR* mir = entry.second;
827 UpdateReferenceVRegs(mir, prev_mir, references);
828 // For little-endian, the bytes comprising the bit vector's raw storage are what we need.
829 native_gc_map_builder.AddEntry(native_offset,
830 reinterpret_cast<const uint8_t*>(references->GetRawStorage()));
831 prev_mir = mir;
Vladimir Marko767c7522015-03-20 12:47:30 +0000832 }
Vladimir Marko6e071832015-03-25 11:13:39 +0000833 } else {
834 ArenaVector<uint8_t> references_buffer(arena_->Adapter());
835 references_buffer.resize(reg_width);
836 for (const auto& entry : safepoints_) {
837 uint32_t native_offset = entry.first->offset;
838 MIR* mir = entry.second;
839 UpdateReferenceVRegs(mir, prev_mir, references);
840 // Big-endian or unknown endianness, manually translate the bit vector data.
841 const auto* raw_storage = references->GetRawStorage();
842 for (size_t i = 0; i != reg_width; ++i) {
843 references_buffer[i] = static_cast<uint8_t>(
844 raw_storage[i / sizeof(raw_storage[0])] >> (8u * (i % sizeof(raw_storage[0]))));
845 }
Vladimir Markoec7802a2015-10-01 20:57:57 +0100846 native_gc_map_builder.AddEntry(native_offset, references_buffer.data());
Vladimir Marko6e071832015-03-25 11:13:39 +0000847 prev_mir = mir;
848 }
Vladimir Marko767c7522015-03-20 12:47:30 +0000849 }
850}
851
852void Mir2Lir::CreateNativeGcMapWithoutRegisterPromotion() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000853 DCHECK(!encoded_mapping_table_.empty());
854 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000856 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
857 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858 if (native_offset > max_native_offset) {
859 max_native_offset = native_offset;
860 }
861 }
862 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000863 const std::vector<uint8_t>& gc_map_raw =
864 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
865 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
866 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000868 GcMapBuilder native_gc_map_builder(&native_gc_map_,
869 mapping_table.PcToDexSize(),
870 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871
Vladimir Marko06606b92013-12-02 15:31:08 +0000872 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
873 uint32_t native_offset = it.NativePcOffset();
874 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700876 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
Dave Allisonf9439142014-03-27 15:10:22 -0700877 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 native_gc_map_builder.AddEntry(native_offset, references);
879 }
Mathieu Chartierab972ef2014-12-03 17:38:22 -0800880
881 // Maybe not necessary, but this could help prevent errors where we access the verified method
882 // after it has been deleted.
883 mir_graph_->GetCurrentDexCompilationUnit()->ClearVerifiedMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884}
885
886/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700887int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100889 constexpr unsigned int ptr_size = sizeof(uint32_t);
Andreas Gampe785d2f22014-11-03 22:57:30 -0800890 static_assert(ptr_size >= sizeof(mirror::HeapReference<mirror::Object>),
891 "Pointer size cannot hold a heap reference");
Ian Rogersff093b32014-04-30 19:04:27 -0700892 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
893 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
894 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 return offset;
896}
897
buzbee0d829482013-10-11 15:24:55 -0700898int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100899 for (Mir2Lir::SwitchTable* tab_rec : switch_tables_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 tab_rec->offset = offset;
901 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
902 offset += tab_rec->table[1] * (sizeof(int) * 2);
903 } else {
904 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
905 static_cast<int>(Instruction::kPackedSwitchSignature));
906 offset += tab_rec->table[1] * sizeof(int);
907 }
908 }
909 return offset;
910}
911
buzbee0d829482013-10-11 15:24:55 -0700912int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100913 for (Mir2Lir::FillArrayData* tab_rec : fill_array_data_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 tab_rec->offset = offset;
915 offset += tab_rec->size;
916 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700917 offset = RoundUp(offset, 4);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100918 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 return offset;
920}
921
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922/*
923 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700924 * offset vaddr if pretty-printing, otherise use the standard block
925 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700926 * branch table during the assembly phase. All resource flags
927 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928 */
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800929LIR* Mir2Lir::InsertCaseLabel(uint32_t bbid, int keyVal) {
930 LIR* boundary_lir = &block_label_list_[bbid];
buzbeeb48819d2013-09-14 16:15:25 -0700931 LIR* res = boundary_lir;
932 if (cu_->verbose) {
933 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000934 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800935 BasicBlock* bb = mir_graph_->GetBasicBlock(bbid);
936 DCHECK(bb != nullptr);
937 new_label->dalvik_offset = bb->start_offset;
buzbeeb48819d2013-09-14 16:15:25 -0700938 new_label->opcode = kPseudoCaseLabel;
939 new_label->operands[0] = keyVal;
940 new_label->flags.fixup = kFixupLabel;
941 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100942 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700943 InsertLIRAfter(boundary_lir, new_label);
buzbeeb48819d2013-09-14 16:15:25 -0700944 }
945 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946}
947
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700948void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949 /*
950 * Sparse switch data format:
951 * ushort ident = 0x0200 magic value
952 * ushort size number of entries in the table; > 0
953 * int keys[size] keys, sorted low-to-high; 32-bit aligned
954 * int targets[size] branch targets, relative to switch opcode
955 *
956 * Total size is (2+size*4) 16-bit code units.
957 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 uint16_t ident = table[0];
959 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700960 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
961 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
963 << ", entries: " << std::dec << entries;
964 for (int i = 0; i < entries; i++) {
965 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
966 }
967}
968
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700969void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 /*
971 * Packed switch data format:
972 * ushort ident = 0x0100 magic value
973 * ushort size number of entries in the table
974 * int first_key first (and lowest) switch case value
975 * int targets[size] branch targets, relative to switch opcode
976 *
977 * Total size is (4+size*2) 16-bit code units.
978 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700980 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 int entries = table[1];
982 int low_key = s4FromSwitchData(&table[2]);
983 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
984 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
985 for (int i = 0; i < entries; i++) {
986 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
987 << targets[i];
988 }
989}
990
buzbee252254b2013-09-08 16:20:53 -0700991/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700992void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700993 UNUSED(offset);
buzbee0d829482013-10-11 15:24:55 -0700994 // NOTE: only used for debug listings.
995 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996}
997
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998// Convert relation of src1/src2 to src2/src1
999ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
1000 ConditionCode res;
1001 switch (before) {
1002 case kCondEq: res = kCondEq; break;
1003 case kCondNe: res = kCondNe; break;
1004 case kCondLt: res = kCondGt; break;
1005 case kCondGt: res = kCondLt; break;
1006 case kCondLe: res = kCondGe; break;
1007 case kCondGe: res = kCondLe; break;
1008 default:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001010 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 }
1012 return res;
1013}
1014
Vladimir Markoa1a70742014-03-03 10:28:05 +00001015ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
1016 ConditionCode res;
1017 switch (before) {
1018 case kCondEq: res = kCondNe; break;
1019 case kCondNe: res = kCondEq; break;
1020 case kCondLt: res = kCondGe; break;
1021 case kCondGt: res = kCondLe; break;
1022 case kCondLe: res = kCondGt; break;
1023 case kCondGe: res = kCondLt; break;
1024 default:
Vladimir Markoa1a70742014-03-03 10:28:05 +00001025 LOG(FATAL) << "Unexpected ccode " << before;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001026 UNREACHABLE();
Vladimir Markoa1a70742014-03-03 10:28:05 +00001027 }
1028 return res;
1029}
1030
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031// TODO: move to mir_to_lir.cc
1032Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Andreas Gampe9c462082015-01-27 14:31:40 -08001033 : literal_list_(nullptr),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001034 method_literal_list_(nullptr),
1035 class_literal_list_(nullptr),
1036 code_literal_list_(nullptr),
1037 first_fixup_(nullptr),
Andreas Gampe9c462082015-01-27 14:31:40 -08001038 arena_(arena),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 cu_(cu),
1040 mir_graph_(mir_graph),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001041 switch_tables_(arena->Adapter(kArenaAllocSwitchTable)),
1042 fill_array_data_(arena->Adapter(kArenaAllocFillArrayData)),
1043 tempreg_info_(arena->Adapter()),
1044 reginfo_map_(arena->Adapter()),
1045 pointer_storage_(arena->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 data_offset_(0),
1047 total_size_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001048 block_label_list_(nullptr),
1049 promotion_map_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 current_dalvik_offset_(0),
Vladimir Marko767c7522015-03-20 12:47:30 +00001051 current_mir_(nullptr),
buzbeeb48819d2013-09-14 16:15:25 -07001052 estimated_native_code_size_(0),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001053 reg_pool_(nullptr),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 live_sreg_(0),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001055 code_buffer_(mir_graph->GetArena()->Adapter()),
1056 encoded_mapping_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko8081d2b2014-07-31 15:33:43 +01001057 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1058 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Vladimir Marko80b96d12015-02-19 15:50:28 +00001059 native_gc_map_(mir_graph->GetArena()->Adapter()),
Vladimir Markof4da6752014-08-01 19:04:18 +01001060 patches_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 num_core_spills_(0),
1062 num_fp_spills_(0),
1063 frame_size_(0),
1064 core_spill_mask_(0),
1065 fp_spill_mask_(0),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001066 first_lir_insn_(nullptr),
1067 last_lir_insn_(nullptr),
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001068 slow_paths_(arena->Adapter(kArenaAllocSlowPaths)),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001069 mem_ref_type_(ResourceMask::kHeapRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001070 mask_cache_(arena),
Vladimir Marko767c7522015-03-20 12:47:30 +00001071 safepoints_(arena->Adapter()),
Vladimir Marko20f85592015-03-19 10:07:02 +00001072 dex_cache_arrays_layout_(cu->compiler_driver->GetDexCacheArraysLayout(cu->dex_file)),
Vladimir Markocc234812015-04-07 09:36:09 +01001073 pc_rel_temp_(nullptr),
1074 dex_cache_arrays_min_offset_(std::numeric_limits<uint32_t>::max()),
David Srbecky1109fb32015-04-07 20:21:06 +01001075 cfi_(&last_lir_insn_,
David Srbecky8363c772015-05-28 16:12:43 +01001076 cu->compiler_driver->GetCompilerOptions().GetGenerateDebugInfo(),
David Srbecky1109fb32015-04-07 20:21:06 +01001077 arena),
Serguei Katkov717a3e42014-11-13 17:19:42 +06001078 in_to_reg_storage_mapping_(arena) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001079 switch_tables_.reserve(4);
1080 fill_array_data_.reserve(4);
1081 tempreg_info_.reserve(20);
1082 reginfo_map_.reserve(RegStorage::kMaxRegs);
1083 pointer_storage_.reserve(128);
1084 slow_paths_.reserve(32);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001085 // Reserve pointer id 0 for null.
Vladimir Markof6737f72015-03-23 17:05:14 +00001086 size_t null_idx = WrapPointer<void>(nullptr);
buzbee0d829482013-10-11 15:24:55 -07001087 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088}
1089
1090void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001091 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1093
1094 /* Allocate Registers using simple local allocation scheme */
1095 SimpleRegAlloc();
1096
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001097 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001098 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001099 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001100 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001102 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1103 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 MethodMIR2LIR();
1105 }
1106
1107 /* Method is not empty */
1108 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109 /* Convert LIR into machine code. */
1110 AssembleLIR();
1111
buzbeeb01bf152014-05-13 15:59:07 -07001112 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 CodegenDump();
1114 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116}
1117
1118CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001119 // Combine vmap tables - core regs, then fp regs - into vmap_table.
Vladimir Markof9f64412015-09-02 14:05:49 +01001120 Leb128EncodingVector<> vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001122 // Prefix the encoded data with its size.
1123 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1124 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1125 vmap_encoder.PushBackUnsigned(size);
1126 // Core regs may have been inserted out of order - sort first.
1127 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1128 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1129 // Copy, stripping out the phys register sort key.
1130 vmap_encoder.PushBackUnsigned(
Vladimir Marko97a87ec2015-09-29 11:25:48 +01001131 ~(~0u << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001132 }
1133 // Push a marker to take place of lr.
1134 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001135 if (cu_->instruction_set == kThumb2) {
1136 // fp regs already sorted.
1137 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1138 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1139 }
1140 } else {
1141 // For other platforms regs may have been inserted out of order - sort first.
1142 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1143 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1144 // Copy, stripping out the phys register sort key.
1145 vmap_encoder.PushBackUnsigned(
Vladimir Marko97a87ec2015-09-29 11:25:48 +01001146 ~(~0u << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
Serguei Katkovc3801912014-07-08 17:21:53 +07001147 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001148 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001150 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1151 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001152 DCHECK_EQ(core_vmap_table_.size(), 0u);
1153 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1154 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001156
Vladimir Marko58155012015-08-19 12:49:41 +00001157 // Sort patches by literal offset. Required for .oat_patches encoding.
Vladimir Markof4da6752014-08-01 19:04:18 +01001158 std::sort(patches_.begin(), patches_.end(), [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
1159 return lhs.LiteralOffset() < rhs.LiteralOffset();
1160 });
1161
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001162 return CompiledMethod::SwapAllocCompiledMethod(
1163 cu_->compiler_driver, cu_->instruction_set,
1164 ArrayRef<const uint8_t>(code_buffer_),
1165 frame_size_, core_spill_mask_, fp_spill_mask_,
1166 &src_mapping_table_,
1167 ArrayRef<const uint8_t>(encoded_mapping_table_),
1168 ArrayRef<const uint8_t>(vmap_encoder.GetData()),
1169 ArrayRef<const uint8_t>(native_gc_map_),
David Srbecky1109fb32015-04-07 20:21:06 +01001170 ArrayRef<const uint8_t>(*cfi_.Patch(code_buffer_.size())),
Vladimir Markob207e142015-04-02 21:25:21 +01001171 ArrayRef<const LinkerPatch>(patches_));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172}
1173
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001174size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1175 // Chose a reasonably small value in order to contain stack growth.
1176 // Backends that are smarter about spill region can return larger values.
1177 const size_t max_compiler_temps = 10;
1178 return max_compiler_temps;
1179}
1180
1181size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1182 // By default assume that the Mir2Lir will need one slot for each temporary.
1183 // If the backend can better determine temps that have non-overlapping ranges and
1184 // temps that do not need spilled, it can actually provide a small region.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001185 mir_graph_->CommitCompilerTemps();
1186 return mir_graph_->GetNumBytesForSpecialTemps() + mir_graph_->GetMaximumBytesForNonSpecialTemps();
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001187}
1188
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189int Mir2Lir::ComputeFrameSize() {
1190 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001191 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1192 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1193 + sizeof(uint32_t) // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001194 + mir_graph_->GetNumOfLocalCodeVRs() * sizeof(uint32_t)
1195 + mir_graph_->GetNumOfOutVRs() * sizeof(uint32_t)
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001196 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001198 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199}
1200
1201/*
1202 * Append an LIR instruction to the LIR list maintained by a compilation
1203 * unit
1204 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001205void Mir2Lir::AppendLIR(LIR* lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001206 if (first_lir_insn_ == nullptr) {
1207 DCHECK(last_lir_insn_ == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 last_lir_insn_ = first_lir_insn_ = lir;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001209 lir->prev = lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 } else {
1211 last_lir_insn_->next = lir;
1212 lir->prev = last_lir_insn_;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001213 lir->next = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 last_lir_insn_ = lir;
1215 }
1216}
1217
1218/*
1219 * Insert an LIR instruction before the current instruction, which cannot be the
1220 * first instruction.
1221 *
1222 * prev_lir <-> new_lir <-> current_lir
1223 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001224void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001225 DCHECK(current_lir->prev != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 LIR *prev_lir = current_lir->prev;
1227
1228 prev_lir->next = new_lir;
1229 new_lir->prev = prev_lir;
1230 new_lir->next = current_lir;
1231 current_lir->prev = new_lir;
1232}
1233
1234/*
1235 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001236 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 *
1238 * current_lir -> new_lir -> old_next
1239 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001240void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 new_lir->prev = current_lir;
1242 new_lir->next = current_lir->next;
1243 current_lir->next = new_lir;
1244 new_lir->next->prev = new_lir;
1245}
1246
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001247bool Mir2Lir::PartiallyIntersects(RegLocation rl_src, RegLocation rl_dest) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001248 DCHECK(rl_src.wide);
1249 DCHECK(rl_dest.wide);
1250 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1251}
1252
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001253bool Mir2Lir::Intersects(RegLocation rl_src, RegLocation rl_dest) {
1254 DCHECK(rl_src.wide);
1255 DCHECK(rl_dest.wide);
1256 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) <= 1);
1257}
1258
buzbee2700f7e2014-03-07 09:46:20 -08001259LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001260 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001261 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001262 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1263 if (compare != nullptr) {
1264 *compare = inst;
1265 }
Mark Mendell766e9292014-01-27 07:55:47 -08001266 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1267 return branch;
1268}
1269
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001270void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001271 slow_paths_.push_back(slowpath);
Serguei Katkov589e0462014-09-05 18:37:22 +07001272 ResetDefTracking();
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001273}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001274
Jeff Hao49161ce2014-03-12 11:05:25 -07001275void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1276 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001277 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001278 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001279 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001280 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001281 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1282 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1283 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001284 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001285 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001286 // Loads a code pointer. Code from oat file can be mapped anywhere.
Vladimir Markof6737f72015-03-23 17:05:14 +00001287 OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001288 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001289 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001290}
1291
Jeff Hao49161ce2014-03-12 11:05:25 -07001292void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1293 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001294 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001295 if (data_target == nullptr) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001296 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001297 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001298 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1299 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1300 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001301 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001302 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001303 // Loads an ArtMethod pointer, which is not a reference.
1304 OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001305 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001306 DCHECK_NE(cu_->instruction_set, kMips64) << reinterpret_cast<void*>(data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001307}
1308
Fred Shihe7f82e22014-08-06 10:46:37 -07001309void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1310 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001311 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001312 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001313 if (data_target == nullptr) {
1314 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001315 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001316 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001317 // Loads a Class pointer, which is a reference as it lives in the heap.
Vladimir Markof6737f72015-03-23 17:05:14 +00001318 OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001319}
1320
Vladimir Marko20f85592015-03-19 10:07:02 +00001321bool Mir2Lir::CanUseOpPcRelDexCacheArrayLoad() const {
1322 return false;
1323}
1324
1325void Mir2Lir::OpPcRelDexCacheArrayLoad(const DexFile* dex_file ATTRIBUTE_UNUSED,
1326 int offset ATTRIBUTE_UNUSED,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001327 RegStorage r_dest ATTRIBUTE_UNUSED,
1328 bool wide ATTRIBUTE_UNUSED) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001329 LOG(FATAL) << "No generic implementation.";
1330 UNREACHABLE();
1331}
1332
buzbee2700f7e2014-03-07 09:46:20 -08001333RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001334 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001335 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001336 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001337 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1338 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1339 info_lo->SetIsWide(false);
1340 info_hi->SetIsWide(false);
1341 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001342 } else {
buzbee85089dd2014-05-25 15:10:52 -07001343 RegisterInfo* info = GetRegInfo(loc.reg);
1344 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1345 DCHECK(info_new != nullptr);
1346 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1347 info->MarkDead();
1348 info_new->MarkLive(loc.s_reg_low);
1349 }
1350 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001351 }
buzbee85089dd2014-05-25 15:10:52 -07001352 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001353 }
buzbee85089dd2014-05-25 15:10:52 -07001354 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001355 return loc;
1356}
1357
Mark Mendelld65c51a2014-04-29 16:55:20 -04001358void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001359 UNUSED(bb, mir);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001360 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001361 UNREACHABLE();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001362}
1363
Vladimir Marko767c7522015-03-20 12:47:30 +00001364void Mir2Lir::InitReferenceVRegs(BasicBlock* bb, BitVector* references) {
1365 // Mark the references coming from the first predecessor.
1366 DCHECK(bb != nullptr);
1367 DCHECK(bb->block_type == kEntryBlock || !bb->predecessors.empty());
1368 BasicBlock* first_bb =
1369 (bb->block_type == kEntryBlock) ? bb : mir_graph_->GetBasicBlock(bb->predecessors[0]);
1370 DCHECK(first_bb != nullptr);
1371 DCHECK(first_bb->data_flow_info != nullptr);
1372 DCHECK(first_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1373 const int32_t* first_vreg_to_ssa_map = first_bb->data_flow_info->vreg_to_ssa_map_exit;
1374 references->ClearAllBits();
Pavel Vyssotski41f9cc22015-06-16 17:57:37 +06001375 for (uint32_t vreg = 0,
1376 num_vregs = mir_graph_->GetNumOfCodeVRs() + mir_graph_->GetNumUsedCompilerTemps();
1377 vreg != num_vregs; ++vreg) {
Vladimir Marko767c7522015-03-20 12:47:30 +00001378 int32_t sreg = first_vreg_to_ssa_map[vreg];
1379 if (sreg != INVALID_SREG && mir_graph_->reg_location_[sreg].ref &&
1380 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[sreg])) {
1381 references->SetBit(vreg);
1382 }
1383 }
1384 // Unmark the references that are merging with a different value.
1385 for (size_t i = 1u, num_pred = bb->predecessors.size(); i < num_pred; ++i) {
1386 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(bb->predecessors[i]);
1387 DCHECK(pred_bb != nullptr);
1388 DCHECK(pred_bb->data_flow_info != nullptr);
1389 DCHECK(pred_bb->data_flow_info->vreg_to_ssa_map_exit != nullptr);
1390 const int32_t* pred_vreg_to_ssa_map = pred_bb->data_flow_info->vreg_to_ssa_map_exit;
1391 for (uint32_t vreg : references->Indexes()) {
1392 if (first_vreg_to_ssa_map[vreg] != pred_vreg_to_ssa_map[vreg]) {
1393 // NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
1394 // so clearing the bit has no effect on the iterator.
1395 references->ClearBit(vreg);
1396 }
1397 }
1398 }
Vladimir Marko767c7522015-03-20 12:47:30 +00001399}
1400
1401bool Mir2Lir::UpdateReferenceVRegsLocal(MIR* mir, MIR* prev_mir, BitVector* references) {
1402 DCHECK(mir == nullptr || mir->bb == prev_mir->bb);
1403 DCHECK(prev_mir != nullptr);
1404 while (prev_mir != nullptr) {
1405 if (prev_mir == mir) {
1406 return true;
1407 }
1408 const size_t num_defs = prev_mir->ssa_rep->num_defs;
1409 const int32_t* defs = prev_mir->ssa_rep->defs;
1410 if (num_defs == 1u && mir_graph_->reg_location_[defs[0]].ref &&
1411 !mir_graph_->IsConstantNullRef(mir_graph_->reg_location_[defs[0]])) {
1412 references->SetBit(mir_graph_->SRegToVReg(defs[0]));
1413 } else {
1414 for (size_t i = 0u; i != num_defs; ++i) {
1415 references->ClearBit(mir_graph_->SRegToVReg(defs[i]));
1416 }
1417 }
1418 prev_mir = prev_mir->next;
1419 }
1420 return false;
1421}
1422
1423void Mir2Lir::UpdateReferenceVRegs(MIR* mir, MIR* prev_mir, BitVector* references) {
1424 if (mir == nullptr) {
1425 // Safepoint in entry sequence.
1426 InitReferenceVRegs(mir_graph_->GetEntryBlock(), references);
1427 return;
1428 }
1429 if (IsInstructionReturn(mir->dalvikInsn.opcode) ||
1430 mir->dalvikInsn.opcode == Instruction::RETURN_VOID_NO_BARRIER) {
1431 references->ClearAllBits();
1432 if (mir->dalvikInsn.opcode == Instruction::RETURN_OBJECT) {
1433 references->SetBit(mir_graph_->SRegToVReg(mir->ssa_rep->uses[0]));
1434 }
1435 return;
1436 }
1437 if (prev_mir != nullptr && mir->bb == prev_mir->bb &&
1438 UpdateReferenceVRegsLocal(mir, prev_mir, references)) {
1439 return;
1440 }
1441 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
1442 DCHECK(bb != nullptr);
1443 InitReferenceVRegs(bb, references);
1444 bool success = UpdateReferenceVRegsLocal(mir, bb->first_mir_insn, references);
1445 DCHECK(success) << "MIR @0x" << std::hex << mir->offset << " not in BB#" << std::dec << mir->bb;
1446}
1447
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001448} // namespace art