blob: 08e1c1aa5a53800bc6a736e808920af71bb90c11 [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) {
60 if (rl_src.fp) {
61 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
62 } else {
63 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
64 }
65 } else {
66 if (rl_src.fp) {
67 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
68 } else {
69 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
70 }
71 }
72 }
73 return res;
74}
75
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070076void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070077 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010078 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010080 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -070081}
82
Andreas Gampe3c12c512014-06-24 18:46:29 +000083void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
84 DCHECK(!after->flags.use_def_invalid);
85 after->u.m.def_mask = &kEncodeAll;
86 // As NewLIR0 uses Append, we need to create the LIR by hand.
87 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
88 if (after->next == nullptr) {
89 DCHECK_EQ(after, last_lir_insn_);
90 AppendLIR(safepoint_pc);
91 } else {
92 InsertLIRAfter(after, safepoint_pc);
93 }
94 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
95}
96
buzbee252254b2013-09-08 16:20:53 -070097/* Remove a LIR from the list. */
98void Mir2Lir::UnlinkLIR(LIR* lir) {
99 if (UNLIKELY(lir == first_lir_insn_)) {
100 first_lir_insn_ = lir->next;
101 if (lir->next != NULL) {
102 lir->next->prev = NULL;
103 } else {
104 DCHECK(lir->next == NULL);
105 DCHECK(lir == last_lir_insn_);
106 last_lir_insn_ = NULL;
107 }
108 } else if (lir == last_lir_insn_) {
109 last_lir_insn_ = lir->prev;
110 lir->prev->next = NULL;
111 } else if ((lir->prev != NULL) && (lir->next != NULL)) {
112 lir->prev->next = lir->next;
113 lir->next->prev = lir->prev;
114 }
115}
116
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700118void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700120 if (!cu_->verbose) {
121 UnlinkLIR(lir);
122 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123}
124
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700125void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700127 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100128 // TODO: Avoid the extra Arena allocation!
129 const ResourceMask** mask_ptr;
130 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700132 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700134 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100136 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100138 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 /* ..and then add back the one we need */
140 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100141 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100143 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100145 case ResourceMask::kDalvikReg:
146 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100148 case ResourceMask::kHeapRef:
149 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100151 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 /* Currently only loads can be marked as kMustNotAlias */
153 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100154 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 break;
156 default:
157 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
158 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100159 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160}
161
162/*
163 * Mark load/store instructions that access Dalvik registers through the stack.
164 */
165void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700166 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100167 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
168 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169
170 /*
171 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
172 * access.
173 */
buzbeeb48819d2013-09-14 16:15:25 -0700174 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175}
176
177/*
178 * Debugging macros
179 */
180#define DUMP_RESOURCE_MASK(X)
181
182/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700183void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 int offset = lir->offset;
185 int dest = lir->operands[0];
186 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
187
188 /* Handle pseudo-ops individually, and all regular insns as a group */
189 switch (lir->opcode) {
190 case kPseudoMethodEntry:
191 LOG(INFO) << "-------- method entry "
192 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
193 break;
194 case kPseudoMethodExit:
195 LOG(INFO) << "-------- Method_Exit";
196 break;
197 case kPseudoBarrier:
198 LOG(INFO) << "-------- BARRIER";
199 break;
200 case kPseudoEntryBlock:
201 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
202 break;
203 case kPseudoDalvikByteCodeBoundary:
204 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700205 // NOTE: only used for debug listings.
206 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 }
208 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000209 << lir->dalvik_offset << " @ "
210 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 break;
212 case kPseudoExitBlock:
213 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
214 break;
215 case kPseudoPseudoAlign4:
216 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
217 << offset << "): .align4";
218 break;
219 case kPseudoEHBlockLabel:
220 LOG(INFO) << "Exception_Handling:";
221 break;
222 case kPseudoTargetLabel:
223 case kPseudoNormalBlockLabel:
224 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
225 break;
226 case kPseudoThrowTarget:
227 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
228 break;
229 case kPseudoIntrinsicRetry:
230 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
231 break;
232 case kPseudoSuspendTarget:
233 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
234 break;
235 case kPseudoSafepointPC:
236 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
237 break;
238 case kPseudoExportedPC:
239 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
240 break;
241 case kPseudoCaseLabel:
242 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
243 << std::hex << lir->operands[0] << "|" << std::dec <<
244 lir->operands[0];
245 break;
246 default:
247 if (lir->flags.is_nop && !dump_nop) {
248 break;
249 } else {
250 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
251 lir, base_addr));
252 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
253 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800254 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
255 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 op_name.c_str(), op_operands.c_str(),
257 lir->flags.is_nop ? "(nop)" : "");
258 }
259 break;
260 }
261
buzbeeb48819d2013-09-14 16:15:25 -0700262 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100263 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 }
buzbeeb48819d2013-09-14 16:15:25 -0700265 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100266 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 }
268}
269
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700270void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700271 uint32_t num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
272 for (uint32_t i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 PromotionMap v_reg_map = promotion_map_[i];
274 std::string buf;
275 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700276 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 }
278
279 std::string buf3;
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700280 if (i < mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 StringAppendF(&buf3, "%02d", i);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700282 } else if (i == mir_graph_->GetNumOfCodeVRs()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 buf3 = "Method*";
284 } else {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700285 uint32_t diff = i - mir_graph_->GetNumOfCodeVRs();
286 StringAppendF(&buf3, "ct%d", diff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 }
288
289 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
290 v_reg_map.core_location == kLocPhysReg ?
291 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
292 v_reg_map.core_reg : SRegOffset(i),
293 buf.c_str());
294 }
295}
296
buzbee7a11ab02014-04-28 20:02:38 -0700297void Mir2Lir::UpdateLIROffsets() {
298 // Only used for code listings.
299 size_t offset = 0;
300 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
301 lir->offset = offset;
302 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
303 offset += GetInsnSize(lir);
304 } else if (lir->opcode == kPseudoPseudoAlign4) {
305 offset += (offset & 0x2);
306 }
307 }
308}
309
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700311void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 LOG(INFO) << "Dumping LIR insns for "
313 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
314 LIR* lir_insn;
315 int insns_size = cu_->code_item->insns_size_in_code_units_;
316
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700317 LOG(INFO) << "Regs (excluding ins) : " << mir_graph_->GetNumOfLocalCodeVRs();
318 LOG(INFO) << "Ins : " << mir_graph_->GetNumOfInVRs();
319 LOG(INFO) << "Outs : " << mir_graph_->GetNumOfOutVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 LOG(INFO) << "CoreSpills : " << num_core_spills_;
321 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800322 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 LOG(INFO) << "Frame size : " << frame_size_;
324 LOG(INFO) << "code size is " << total_size_ <<
325 " bytes, Dalvik size is " << insns_size * 2;
326 LOG(INFO) << "expansion factor: "
327 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
328 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700329 UpdateLIROffsets();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) {
331 DumpLIRInsn(lir_insn, 0);
332 }
333 for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) {
334 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
335 lir_insn->operands[0]);
336 }
337
338 const DexFile::MethodId& method_id =
339 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700340 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
341 const char* name = cu_->dex_file->GetMethodName(method_id);
342 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343
344 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000345 if (!encoded_mapping_table_.empty()) {
346 MappingTable table(&encoded_mapping_table_[0]);
347 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
348 table.PcToDexSize(), table.PcToDexBegin());
349 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
350 table.DexToPcSize(), table.DexToPcBegin());
351 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352}
353
354/*
355 * Search the existing constants in the literal pool for an exact or close match
356 * within specified delta (greater or equal to 0).
357 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700358LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359 while (data_target) {
360 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
361 return data_target;
362 data_target = data_target->next;
363 }
364 return NULL;
365}
366
367/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700368LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 bool lo_match = false;
370 LIR* lo_target = NULL;
371 while (data_target) {
372 if (lo_match && (data_target->operands[0] == val_hi)) {
373 // Record high word in case we need to expand this later.
374 lo_target->operands[1] = val_hi;
375 return lo_target;
376 }
377 lo_match = false;
378 if (data_target->operands[0] == val_lo) {
379 lo_match = true;
380 lo_target = data_target;
381 }
382 data_target = data_target->next;
383 }
384 return NULL;
385}
386
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100387/* Search the existing constants in the literal pool for an exact method match */
388LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
389 while (data_target) {
390 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
391 UnwrapPointer(data_target->operands[1]) == method.dex_file) {
392 return data_target;
393 }
394 data_target = data_target->next;
395 }
396 return nullptr;
397}
398
Fred Shihe7f82e22014-08-06 10:46:37 -0700399/* Search the existing constants in the literal pool for an exact class match */
400LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
401 while (data_target) {
402 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
403 UnwrapPointer(data_target->operands[1]) == &dex_file) {
404 return data_target;
405 }
406 data_target = data_target->next;
407 }
408 return nullptr;
409}
410
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411/*
412 * The following are building blocks to insert constants into the pool or
413 * instruction streams.
414 */
415
416/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700417LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 /* Add the constant to the literal pool */
419 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000420 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 new_value->operands[0] = value;
422 new_value->next = *constant_list_p;
423 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700424 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 return new_value;
426 }
427 return NULL;
428}
429
430/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700431LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 AddWordData(constant_list_p, val_hi);
433 return AddWordData(constant_list_p, val_lo);
434}
435
Andreas Gampe2da88232014-02-27 12:26:20 -0800436static void Push32(std::vector<uint8_t>&buf, int data) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700437 buf.push_back(data & 0xff);
438 buf.push_back((data >> 8) & 0xff);
439 buf.push_back((data >> 16) & 0xff);
440 buf.push_back((data >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441}
442
Andreas Gampe2da88232014-02-27 12:26:20 -0800443// Push 8 bytes on 64-bit target systems; 4 on 32-bit target systems.
444static void PushPointer(std::vector<uint8_t>&buf, const void* pointer, bool target64) {
445 uint64_t data = reinterpret_cast<uintptr_t>(pointer);
446 if (target64) {
447 Push32(buf, data & 0xFFFFFFFF);
448 Push32(buf, (data >> 32) & 0xFFFFFFFF);
buzbee0d829482013-10-11 15:24:55 -0700449 } else {
Andreas Gampe2da88232014-02-27 12:26:20 -0800450 Push32(buf, static_cast<uint32_t>(data));
buzbee0d829482013-10-11 15:24:55 -0700451 }
452}
453
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
455 while (buf.size() < offset) {
456 buf.push_back(0);
457 }
458}
459
460/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700461void Mir2Lir::InstallLiteralPools() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 AlignBuffer(code_buffer_, data_offset_);
463 LIR* data_lir = literal_list_;
464 while (data_lir != NULL) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800465 Push32(code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 data_lir = NEXT_LIR(data_lir);
467 }
468 // Push code and method literals, record offsets for the compiler to patch.
469 data_lir = code_literal_list_;
470 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700471 uint32_t target_method_idx = data_lir->operands[0];
472 const DexFile* target_dex_file =
473 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 cu_->compiler_driver->AddCodePatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700475 cu_->class_def_idx,
476 cu_->method_idx,
477 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700478 target_method_idx,
479 target_dex_file,
480 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700481 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700482 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700483 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700484 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 data_lir = NEXT_LIR(data_lir);
486 }
487 data_lir = method_literal_list_;
488 while (data_lir != NULL) {
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]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 cu_->compiler_driver->AddMethodPatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700493 cu_->class_def_idx,
494 cu_->method_idx,
495 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700496 target_method_idx,
497 target_dex_file,
498 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700499 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700500 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700501 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700502 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 data_lir = NEXT_LIR(data_lir);
504 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800505 // Push class literals.
506 data_lir = class_literal_list_;
507 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700508 uint32_t target_method_idx = data_lir->operands[0];
Fred Shihe7f82e22014-08-06 10:46:37 -0700509 const DexFile* class_dex_file =
510 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800511 cu_->compiler_driver->AddClassPatch(cu_->dex_file,
512 cu_->class_def_idx,
513 cu_->method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -0700514 target_method_idx,
Fred Shihe7f82e22014-08-06 10:46:37 -0700515 class_dex_file,
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800516 code_buffer_.size());
Fred Shih4fc78532014-08-06 16:44:22 -0700517 const DexFile::TypeId& target_method_id = class_dex_file->GetTypeId(target_method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800518 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700519 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800520 data_lir = NEXT_LIR(data_lir);
521 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522}
523
524/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700525void Mir2Lir::InstallSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
527 while (true) {
528 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
529 if (tab_rec == NULL) break;
530 AlignBuffer(code_buffer_, tab_rec->offset);
531 /*
532 * For Arm, our reference point is the address of the bx
533 * instruction that does the launch, so we have to subtract
534 * the auto pc-advance. For other targets the reference point
535 * is a label, so we can use the offset as-is.
536 */
537 int bx_offset = INVALID_OFFSET;
538 switch (cu_->instruction_set) {
539 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700540 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 bx_offset = tab_rec->anchor->offset + 4;
542 break;
543 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700544 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 bx_offset = 0;
546 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100547 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 case kMips:
549 bx_offset = tab_rec->anchor->offset;
550 break;
551 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
552 }
553 if (cu_->verbose) {
554 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
555 }
556 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee0d829482013-10-11 15:24:55 -0700557 const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
559 int disp = tab_rec->targets[elems]->offset - bx_offset;
560 if (cu_->verbose) {
561 LOG(INFO) << " Case[" << elems << "] key: 0x"
562 << std::hex << keys[elems] << ", disp: 0x"
563 << std::hex << disp;
564 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800565 Push32(code_buffer_, keys[elems]);
566 Push32(code_buffer_,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 tab_rec->targets[elems]->offset - bx_offset);
568 }
569 } else {
570 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
571 static_cast<int>(Instruction::kPackedSwitchSignature));
572 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
573 int disp = tab_rec->targets[elems]->offset - bx_offset;
574 if (cu_->verbose) {
575 LOG(INFO) << " Case[" << elems << "] disp: 0x"
576 << std::hex << disp;
577 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800578 Push32(code_buffer_, tab_rec->targets[elems]->offset - bx_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 }
580 }
581 }
582}
583
584/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700585void Mir2Lir::InstallFillArrayData() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
587 while (true) {
588 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
589 if (tab_rec == NULL) break;
590 AlignBuffer(code_buffer_, tab_rec->offset);
591 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700592 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
593 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 }
595 }
596}
597
buzbee0d829482013-10-11 15:24:55 -0700598static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700599 for (; lir != NULL; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 lir->offset = offset;
601 offset += 4;
602 }
603 return offset;
604}
605
Ian Rogersff093b32014-04-30 19:04:27 -0700606static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
607 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700608 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700609 offset = RoundUp(offset, element_size);
buzbee0d829482013-10-11 15:24:55 -0700610 for (; lir != NULL; lir = lir->next) {
611 lir->offset = offset;
612 offset += element_size;
613 }
614 return offset;
615}
616
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700618bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000619 MappingTable table(&encoded_mapping_table_[0]);
620 std::vector<uint32_t> dex_pcs;
621 dex_pcs.reserve(table.DexToPcSize());
622 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
623 dex_pcs.push_back(it.DexPc());
624 }
625 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
626 std::sort(dex_pcs.begin(), dex_pcs.end());
627
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000629 auto it = dex_pcs.begin(), end = dex_pcs.end();
630 for (uint32_t dex_pc : mir_graph_->catches_) {
631 while (it != end && *it < dex_pc) {
632 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
633 ++it;
634 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000636 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
638 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000639 } else {
640 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 }
642 }
643 if (!success) {
644 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
645 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000646 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 }
648 return success;
649}
650
651
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700652void Mir2Lir::CreateMappingTables() {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700653 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
654
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000655 uint32_t pc2dex_data_size = 0u;
656 uint32_t pc2dex_entries = 0u;
657 uint32_t pc2dex_offset = 0u;
658 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700659 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000660 uint32_t dex2pc_data_size = 0u;
661 uint32_t dex2pc_entries = 0u;
662 uint32_t dex2pc_offset = 0u;
663 uint32_t dex2pc_dalvik_offset = 0u;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700665 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000667 pc2dex_entries += 1;
668 DCHECK(pc2dex_offset <= tgt_lir->offset);
669 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
670 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
671 static_cast<int32_t>(pc2dex_dalvik_offset));
672 pc2dex_offset = tgt_lir->offset;
673 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 }
675 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000676 dex2pc_entries += 1;
677 DCHECK(dex2pc_offset <= tgt_lir->offset);
678 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
679 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
680 static_cast<int32_t>(dex2pc_dalvik_offset));
681 dex2pc_offset = tgt_lir->offset;
682 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 }
684 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000685
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700686 if (generate_src_map) {
687 src_mapping_table_.reserve(pc2dex_src_entries);
688 }
689
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000690 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
691 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
692 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000693 encoded_mapping_table_.resize(data_size);
694 uint8_t* write_pos = &encoded_mapping_table_[0];
695 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
696 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
697 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
698 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000699
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000700 pc2dex_offset = 0u;
701 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000702 dex2pc_offset = 0u;
703 dex2pc_dalvik_offset = 0u;
704 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700705 if (generate_src_map && !tgt_lir->flags.is_nop) {
706 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
707 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
708 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000709 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
710 DCHECK(pc2dex_offset <= tgt_lir->offset);
711 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
712 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
713 static_cast<int32_t>(pc2dex_dalvik_offset));
714 pc2dex_offset = tgt_lir->offset;
715 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
716 }
717 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
718 DCHECK(dex2pc_offset <= tgt_lir->offset);
719 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
720 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
721 static_cast<int32_t>(dex2pc_dalvik_offset));
722 dex2pc_offset = tgt_lir->offset;
723 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
724 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000725 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000726 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
727 hdr_data_size + pc2dex_data_size);
728 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000729
Ian Rogers96faf5b2013-08-09 22:05:32 -0700730 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000731 CHECK(VerifyCatchEntries());
732
Ian Rogers96faf5b2013-08-09 22:05:32 -0700733 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000734 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700735 CHECK_EQ(table.TotalSize(), total_entries);
736 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000737 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000738 auto it2 = table.DexToPcBegin();
739 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
740 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
741 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
742 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
743 ++it;
744 }
745 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
746 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
747 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
748 ++it2;
749 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700750 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000751 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000752 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700753 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754}
755
Brian Carlstrom7940e442013-07-12 13:46:57 -0700756void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000757 DCHECK(!encoded_mapping_table_.empty());
758 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000760 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
761 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 if (native_offset > max_native_offset) {
763 max_native_offset = native_offset;
764 }
765 }
766 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000767 const std::vector<uint8_t>& gc_map_raw =
768 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
769 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
770 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000772 GcMapBuilder native_gc_map_builder(&native_gc_map_,
773 mapping_table.PcToDexSize(),
774 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775
Vladimir Marko06606b92013-12-02 15:31:08 +0000776 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
777 uint32_t native_offset = it.NativePcOffset();
778 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Dave Allisonf9439142014-03-27 15:10:22 -0700780 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
781 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 native_gc_map_builder.AddEntry(native_offset, references);
783 }
784}
785
786/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700787int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Ian Rogersff093b32014-04-30 19:04:27 -0700789 unsigned int ptr_size = GetInstructionSetPointerSize(cu_->instruction_set);
790 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
791 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
792 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 return offset;
794}
795
buzbee0d829482013-10-11 15:24:55 -0700796int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
798 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700799 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 if (tab_rec == NULL) break;
801 tab_rec->offset = offset;
802 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
803 offset += tab_rec->table[1] * (sizeof(int) * 2);
804 } else {
805 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
806 static_cast<int>(Instruction::kPackedSwitchSignature));
807 offset += tab_rec->table[1] * sizeof(int);
808 }
809 }
810 return offset;
811}
812
buzbee0d829482013-10-11 15:24:55 -0700813int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
815 while (true) {
816 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
817 if (tab_rec == NULL) break;
818 tab_rec->offset = offset;
819 offset += tab_rec->size;
820 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700821 offset = RoundUp(offset, 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 }
823 return offset;
824}
825
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826/*
827 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700828 * offset vaddr if pretty-printing, otherise use the standard block
829 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700830 * branch table during the assembly phase. All resource flags
831 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 */
buzbee0d829482013-10-11 15:24:55 -0700833LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) {
buzbee252254b2013-09-08 16:20:53 -0700834 LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id];
buzbeeb48819d2013-09-14 16:15:25 -0700835 LIR* res = boundary_lir;
836 if (cu_->verbose) {
837 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000838 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
buzbeeb48819d2013-09-14 16:15:25 -0700839 new_label->dalvik_offset = vaddr;
840 new_label->opcode = kPseudoCaseLabel;
841 new_label->operands[0] = keyVal;
842 new_label->flags.fixup = kFixupLabel;
843 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100844 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700845 InsertLIRAfter(boundary_lir, new_label);
846 res = new_label;
847 }
848 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849}
850
buzbee0d829482013-10-11 15:24:55 -0700851void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700853 DexOffset base_vaddr = tab_rec->vaddr;
854 const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 int entries = table[1];
856 int low_key = s4FromSwitchData(&table[2]);
857 for (int i = 0; i < entries; i++) {
858 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key);
859 }
860}
861
buzbee0d829482013-10-11 15:24:55 -0700862void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700864 DexOffset base_vaddr = tab_rec->vaddr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700866 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
867 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 for (int i = 0; i < entries; i++) {
869 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]);
870 }
871}
872
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700873void Mir2Lir::ProcessSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
875 while (true) {
876 Mir2Lir::SwitchTable *tab_rec = iterator.Next();
877 if (tab_rec == NULL) break;
878 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
879 MarkPackedCaseLabels(tab_rec);
880 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
881 MarkSparseCaseLabels(tab_rec);
882 } else {
883 LOG(FATAL) << "Invalid switch table";
884 }
885 }
886}
887
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700888void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889 /*
890 * Sparse switch data format:
891 * ushort ident = 0x0200 magic value
892 * ushort size number of entries in the table; > 0
893 * int keys[size] keys, sorted low-to-high; 32-bit aligned
894 * int targets[size] branch targets, relative to switch opcode
895 *
896 * Total size is (2+size*4) 16-bit code units.
897 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 uint16_t ident = table[0];
899 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700900 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
901 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
903 << ", entries: " << std::dec << entries;
904 for (int i = 0; i < entries; i++) {
905 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
906 }
907}
908
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700909void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 /*
911 * Packed switch data format:
912 * ushort ident = 0x0100 magic value
913 * ushort size number of entries in the table
914 * int first_key first (and lowest) switch case value
915 * int targets[size] branch targets, relative to switch opcode
916 *
917 * Total size is (4+size*2) 16-bit code units.
918 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700920 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 int entries = table[1];
922 int low_key = s4FromSwitchData(&table[2]);
923 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
924 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
925 for (int i = 0; i < entries; i++) {
926 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
927 << targets[i];
928 }
929}
930
buzbee252254b2013-09-08 16:20:53 -0700931/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700932void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
933 // NOTE: only used for debug listings.
934 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700935}
936
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700937bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700938 bool is_taken;
939 switch (opcode) {
940 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
941 case Instruction::IF_NE: is_taken = (src1 != src2); break;
942 case Instruction::IF_LT: is_taken = (src1 < src2); break;
943 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
944 case Instruction::IF_GT: is_taken = (src1 > src2); break;
945 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
946 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
947 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
948 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
949 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
950 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
951 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
952 default:
953 LOG(FATAL) << "Unexpected opcode " << opcode;
954 is_taken = false;
955 }
956 return is_taken;
957}
958
959// Convert relation of src1/src2 to src2/src1
960ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
961 ConditionCode res;
962 switch (before) {
963 case kCondEq: res = kCondEq; break;
964 case kCondNe: res = kCondNe; break;
965 case kCondLt: res = kCondGt; break;
966 case kCondGt: res = kCondLt; break;
967 case kCondLe: res = kCondGe; break;
968 case kCondGe: res = kCondLe; break;
969 default:
970 res = static_cast<ConditionCode>(0);
971 LOG(FATAL) << "Unexpected ccode " << before;
972 }
973 return res;
974}
975
Vladimir Markoa1a70742014-03-03 10:28:05 +0000976ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
977 ConditionCode res;
978 switch (before) {
979 case kCondEq: res = kCondNe; break;
980 case kCondNe: res = kCondEq; break;
981 case kCondLt: res = kCondGe; break;
982 case kCondGt: res = kCondLe; break;
983 case kCondLe: res = kCondGt; break;
984 case kCondGe: res = kCondLt; break;
985 default:
986 res = static_cast<ConditionCode>(0);
987 LOG(FATAL) << "Unexpected ccode " << before;
988 }
989 return res;
990}
991
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992// TODO: move to mir_to_lir.cc
993Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
994 : Backend(arena),
995 literal_list_(NULL),
996 method_literal_list_(NULL),
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800997 class_literal_list_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 code_literal_list_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -0700999 first_fixup_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000 cu_(cu),
1001 mir_graph_(mir_graph),
1002 switch_tables_(arena, 4, kGrowableArraySwitchTables),
1003 fill_array_data_(arena, 4, kGrowableArrayFillArrayData),
buzbeebd663de2013-09-10 15:41:31 -07001004 tempreg_info_(arena, 20, kGrowableArrayMisc),
buzbee091cc402014-03-31 10:14:40 -07001005 reginfo_map_(arena, RegStorage::kMaxRegs, kGrowableArrayMisc),
buzbee0d829482013-10-11 15:24:55 -07001006 pointer_storage_(arena, 128, kGrowableArrayMisc),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 data_offset_(0),
1008 total_size_(0),
1009 block_label_list_(NULL),
buzbeed69835d2014-02-03 14:40:27 -08001010 promotion_map_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 current_dalvik_offset_(0),
buzbeeb48819d2013-09-14 16:15:25 -07001012 estimated_native_code_size_(0),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 reg_pool_(NULL),
1014 live_sreg_(0),
Vladimir Marko8081d2b2014-07-31 15:33:43 +01001015 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1016 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 num_core_spills_(0),
1018 num_fp_spills_(0),
1019 frame_size_(0),
1020 core_spill_mask_(0),
1021 fp_spill_mask_(0),
1022 first_lir_insn_(NULL),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001023 last_lir_insn_(NULL),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001024 slow_paths_(arena, 32, kGrowableArraySlowPaths),
1025 mem_ref_type_(ResourceMask::kHeapRef),
1026 mask_cache_(arena) {
buzbee0d829482013-10-11 15:24:55 -07001027 // Reserve pointer id 0 for NULL.
1028 size_t null_idx = WrapPointer(NULL);
1029 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030}
1031
1032void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001033 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1035
1036 /* Allocate Registers using simple local allocation scheme */
1037 SimpleRegAlloc();
1038
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001039 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001040 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001041 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001042 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001044 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1045 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 MethodMIR2LIR();
1047 }
1048
1049 /* Method is not empty */
1050 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 // mark the targets of switch statement case labels
1052 ProcessSwitchTables();
1053
1054 /* Convert LIR into machine code. */
1055 AssembleLIR();
1056
buzbeeb01bf152014-05-13 15:59:07 -07001057 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 CodegenDump();
1059 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061}
1062
1063CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001064 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1065 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001067 // Prefix the encoded data with its size.
1068 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1069 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1070 vmap_encoder.PushBackUnsigned(size);
1071 // Core regs may have been inserted out of order - sort first.
1072 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1073 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1074 // Copy, stripping out the phys register sort key.
1075 vmap_encoder.PushBackUnsigned(
1076 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1077 }
1078 // Push a marker to take place of lr.
1079 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001080 if (cu_->instruction_set == kThumb2) {
1081 // fp regs already sorted.
1082 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1083 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1084 }
1085 } else {
1086 // For other platforms regs may have been inserted out of order - sort first.
1087 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1088 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1089 // Copy, stripping out the phys register sort key.
1090 vmap_encoder.PushBackUnsigned(
1091 ~(-1 << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
1092 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001093 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001095 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1096 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001097 DCHECK_EQ(core_vmap_table_.size(), 0u);
1098 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1099 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001101
Tong Shen547cdfd2014-08-05 01:54:19 -07001102 std::unique_ptr<std::vector<uint8_t>> cfi_info(ReturnFrameDescriptionEntry());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 CompiledMethod* result =
Ian Rogers72d32622014-05-06 16:20:11 -07001104 new CompiledMethod(cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_,
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001105 core_spill_mask_, fp_spill_mask_, &src_mapping_table_, encoded_mapping_table_,
Dave Allisond6ed6422014-04-09 23:36:15 +00001106 vmap_encoder.GetData(), native_gc_map_, cfi_info.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 return result;
1108}
1109
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001110size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1111 // Chose a reasonably small value in order to contain stack growth.
1112 // Backends that are smarter about spill region can return larger values.
1113 const size_t max_compiler_temps = 10;
1114 return max_compiler_temps;
1115}
1116
1117size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1118 // By default assume that the Mir2Lir will need one slot for each temporary.
1119 // If the backend can better determine temps that have non-overlapping ranges and
1120 // temps that do not need spilled, it can actually provide a small region.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001121 mir_graph_->CommitCompilerTemps();
1122 return mir_graph_->GetNumBytesForSpecialTemps() + mir_graph_->GetMaximumBytesForNonSpecialTemps();
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001123}
1124
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125int Mir2Lir::ComputeFrameSize() {
1126 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001127 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1128 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1129 + sizeof(uint32_t) // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001130 + mir_graph_->GetNumOfLocalCodeVRs() * sizeof(uint32_t)
1131 + mir_graph_->GetNumOfOutVRs() * sizeof(uint32_t)
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001132 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001134 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135}
1136
1137/*
1138 * Append an LIR instruction to the LIR list maintained by a compilation
1139 * unit
1140 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001141void Mir2Lir::AppendLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 if (first_lir_insn_ == NULL) {
1143 DCHECK(last_lir_insn_ == NULL);
1144 last_lir_insn_ = first_lir_insn_ = lir;
1145 lir->prev = lir->next = NULL;
1146 } else {
1147 last_lir_insn_->next = lir;
1148 lir->prev = last_lir_insn_;
1149 lir->next = NULL;
1150 last_lir_insn_ = lir;
1151 }
1152}
1153
1154/*
1155 * Insert an LIR instruction before the current instruction, which cannot be the
1156 * first instruction.
1157 *
1158 * prev_lir <-> new_lir <-> current_lir
1159 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001160void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 DCHECK(current_lir->prev != NULL);
1162 LIR *prev_lir = current_lir->prev;
1163
1164 prev_lir->next = new_lir;
1165 new_lir->prev = prev_lir;
1166 new_lir->next = current_lir;
1167 current_lir->prev = new_lir;
1168}
1169
1170/*
1171 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001172 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 *
1174 * current_lir -> new_lir -> old_next
1175 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001176void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 new_lir->prev = current_lir;
1178 new_lir->next = current_lir->next;
1179 current_lir->next = new_lir;
1180 new_lir->next->prev = new_lir;
1181}
1182
Mark Mendell4708dcd2014-01-22 09:05:18 -08001183bool Mir2Lir::IsPowerOfTwo(uint64_t x) {
1184 return (x & (x - 1)) == 0;
1185}
1186
1187// Returns the index of the lowest set bit in 'x'.
1188int32_t Mir2Lir::LowestSetBit(uint64_t x) {
1189 int bit_posn = 0;
1190 while ((x & 0xf) == 0) {
1191 bit_posn += 4;
1192 x >>= 4;
1193 }
1194 while ((x & 1) == 0) {
1195 bit_posn++;
1196 x >>= 1;
1197 }
1198 return bit_posn;
1199}
1200
1201bool Mir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
1202 DCHECK(rl_src.wide);
1203 DCHECK(rl_dest.wide);
1204 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1205}
1206
buzbee2700f7e2014-03-07 09:46:20 -08001207LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001208 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001209 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001210 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1211 if (compare != nullptr) {
1212 *compare = inst;
1213 }
Mark Mendell766e9292014-01-27 07:55:47 -08001214 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1215 return branch;
1216}
1217
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001218void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
1219 slow_paths_.Insert(slowpath);
1220}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001221
Jeff Hao49161ce2014-03-12 11:05:25 -07001222void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1223 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001224 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001225 if (data_target == NULL) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001226 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001227 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001228 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1229 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1230 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001231 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001232 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001233 // Loads a code pointer. Code from oat file can be mapped anywhere.
1234 LIR* load_pc_rel = OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001235 AppendLIR(load_pc_rel);
1236 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1237}
1238
Jeff Hao49161ce2014-03-12 11:05:25 -07001239void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1240 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001241 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001242 if (data_target == NULL) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001243 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001244 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001245 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1246 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1247 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001248 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001249 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001250 // Loads an ArtMethod pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001251 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001252 AppendLIR(load_pc_rel);
1253 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1254}
1255
Fred Shihe7f82e22014-08-06 10:46:37 -07001256void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1257 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001258 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001259 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001260 if (data_target == nullptr) {
1261 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001262 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001263 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001264 // Loads a Class pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001265 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001266 AppendLIR(load_pc_rel);
1267}
1268
Tong Shen547cdfd2014-08-05 01:54:19 -07001269std::vector<uint8_t>* Mir2Lir::ReturnFrameDescriptionEntry() {
Mark Mendellae9fd932014-02-10 16:14:35 -08001270 // Default case is to do nothing.
1271 return nullptr;
1272}
1273
buzbee2700f7e2014-03-07 09:46:20 -08001274RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001275 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001276 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001277 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001278 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1279 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1280 info_lo->SetIsWide(false);
1281 info_hi->SetIsWide(false);
1282 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001283 } else {
buzbee85089dd2014-05-25 15:10:52 -07001284 RegisterInfo* info = GetRegInfo(loc.reg);
1285 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1286 DCHECK(info_new != nullptr);
1287 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1288 info->MarkDead();
1289 info_new->MarkLive(loc.s_reg_low);
1290 }
1291 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001292 }
buzbee85089dd2014-05-25 15:10:52 -07001293 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001294 }
buzbee85089dd2014-05-25 15:10:52 -07001295 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001296 return loc;
1297}
1298
Mark Mendelld65c51a2014-04-29 16:55:20 -04001299void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1300 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
1301}
1302
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001303} // namespace art