blob: be79b639310a02405d6ec4f282eaf7ab66da0f8c [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"
18#include "dex_file-inl.h"
19#include "gc_map.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000020#include "gc_map_builder.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070021#include "mapping_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "mir_to_lir-inl.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000023#include "dex/quick/dex_file_method_inliner.h"
24#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000025#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000026#include "dex/verified_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "verifier/dex_gc_map.h"
28#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000029#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030
31namespace art {
32
Vladimir Marko06606b92013-12-02 15:31:08 +000033namespace {
34
35/* Dump a mapping table */
36template <typename It>
37void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
38 const Signature& signature, uint32_t size, It first) {
39 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080040 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000041 descriptor, name, signature.ToString().c_str(), size));
42 std::replace(line.begin(), line.end(), ';', '_');
43 LOG(INFO) << line;
44 for (uint32_t i = 0; i != size; ++i) {
45 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
46 ++first;
47 LOG(INFO) << line;
48 }
49 LOG(INFO) <<" };\n\n";
50 }
51}
52
53} // anonymous namespace
54
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070055bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 bool res = false;
57 if (rl_src.is_const) {
58 if (rl_src.wide) {
59 if (rl_src.fp) {
60 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
61 } else {
62 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
63 }
64 } else {
65 if (rl_src.fp) {
66 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
67 } else {
68 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
69 }
70 }
71 }
72 return res;
73}
74
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070075void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070076 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010077 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010079 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -070080}
81
Andreas Gampe3c12c512014-06-24 18:46:29 +000082void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
83 DCHECK(!after->flags.use_def_invalid);
84 after->u.m.def_mask = &kEncodeAll;
85 // As NewLIR0 uses Append, we need to create the LIR by hand.
86 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
87 if (after->next == nullptr) {
88 DCHECK_EQ(after, last_lir_insn_);
89 AppendLIR(safepoint_pc);
90 } else {
91 InsertLIRAfter(after, safepoint_pc);
92 }
93 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
94}
95
buzbee252254b2013-09-08 16:20:53 -070096/* Remove a LIR from the list. */
97void Mir2Lir::UnlinkLIR(LIR* lir) {
98 if (UNLIKELY(lir == first_lir_insn_)) {
99 first_lir_insn_ = lir->next;
100 if (lir->next != NULL) {
101 lir->next->prev = NULL;
102 } else {
103 DCHECK(lir->next == NULL);
104 DCHECK(lir == last_lir_insn_);
105 last_lir_insn_ = NULL;
106 }
107 } else if (lir == last_lir_insn_) {
108 last_lir_insn_ = lir->prev;
109 lir->prev->next = NULL;
110 } else if ((lir->prev != NULL) && (lir->next != NULL)) {
111 lir->prev->next = lir->next;
112 lir->next->prev = lir->prev;
113 }
114}
115
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700117void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700119 if (!cu_->verbose) {
120 UnlinkLIR(lir);
121 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122}
123
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700124void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700126 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100127 // TODO: Avoid the extra Arena allocation!
128 const ResourceMask** mask_ptr;
129 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700131 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700133 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100135 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100137 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 /* ..and then add back the one we need */
139 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100140 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100142 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100144 case ResourceMask::kDalvikReg:
145 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100147 case ResourceMask::kHeapRef:
148 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100150 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 /* Currently only loads can be marked as kMustNotAlias */
152 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100153 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 break;
155 default:
156 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
157 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100158 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159}
160
161/*
162 * Mark load/store instructions that access Dalvik registers through the stack.
163 */
164void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700165 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100166 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
167 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168
169 /*
170 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
171 * access.
172 */
buzbeeb48819d2013-09-14 16:15:25 -0700173 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174}
175
176/*
177 * Debugging macros
178 */
179#define DUMP_RESOURCE_MASK(X)
180
181/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700182void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 int offset = lir->offset;
184 int dest = lir->operands[0];
185 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
186
187 /* Handle pseudo-ops individually, and all regular insns as a group */
188 switch (lir->opcode) {
189 case kPseudoMethodEntry:
190 LOG(INFO) << "-------- method entry "
191 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
192 break;
193 case kPseudoMethodExit:
194 LOG(INFO) << "-------- Method_Exit";
195 break;
196 case kPseudoBarrier:
197 LOG(INFO) << "-------- BARRIER";
198 break;
199 case kPseudoEntryBlock:
200 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
201 break;
202 case kPseudoDalvikByteCodeBoundary:
203 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700204 // NOTE: only used for debug listings.
205 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 }
207 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000208 << lir->dalvik_offset << " @ "
209 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 break;
211 case kPseudoExitBlock:
212 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
213 break;
214 case kPseudoPseudoAlign4:
215 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
216 << offset << "): .align4";
217 break;
218 case kPseudoEHBlockLabel:
219 LOG(INFO) << "Exception_Handling:";
220 break;
221 case kPseudoTargetLabel:
222 case kPseudoNormalBlockLabel:
223 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
224 break;
225 case kPseudoThrowTarget:
226 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
227 break;
228 case kPseudoIntrinsicRetry:
229 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
230 break;
231 case kPseudoSuspendTarget:
232 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
233 break;
234 case kPseudoSafepointPC:
235 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
236 break;
237 case kPseudoExportedPC:
238 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
239 break;
240 case kPseudoCaseLabel:
241 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
242 << std::hex << lir->operands[0] << "|" << std::dec <<
243 lir->operands[0];
244 break;
245 default:
246 if (lir->flags.is_nop && !dump_nop) {
247 break;
248 } else {
249 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
250 lir, base_addr));
251 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
252 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800253 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
254 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 op_name.c_str(), op_operands.c_str(),
256 lir->flags.is_nop ? "(nop)" : "");
257 }
258 break;
259 }
260
buzbeeb48819d2013-09-14 16:15:25 -0700261 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100262 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 }
buzbeeb48819d2013-09-14 16:15:25 -0700264 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100265 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 }
267}
268
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700269void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800270 int num_regs = cu_->num_dalvik_registers + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 for (int i = 0; i < num_regs; i++) {
272 PromotionMap v_reg_map = promotion_map_[i];
273 std::string buf;
274 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700275 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 }
277
278 std::string buf3;
279 if (i < cu_->num_dalvik_registers) {
280 StringAppendF(&buf3, "%02d", i);
281 } else if (i == mir_graph_->GetMethodSReg()) {
282 buf3 = "Method*";
283 } else {
284 StringAppendF(&buf3, "ct%d", i - cu_->num_dalvik_registers);
285 }
286
287 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
288 v_reg_map.core_location == kLocPhysReg ?
289 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
290 v_reg_map.core_reg : SRegOffset(i),
291 buf.c_str());
292 }
293}
294
buzbee7a11ab02014-04-28 20:02:38 -0700295void Mir2Lir::UpdateLIROffsets() {
296 // Only used for code listings.
297 size_t offset = 0;
298 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
299 lir->offset = offset;
300 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
301 offset += GetInsnSize(lir);
302 } else if (lir->opcode == kPseudoPseudoAlign4) {
303 offset += (offset & 0x2);
304 }
305 }
306}
307
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700309void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 LOG(INFO) << "Dumping LIR insns for "
311 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
312 LIR* lir_insn;
313 int insns_size = cu_->code_item->insns_size_in_code_units_;
314
315 LOG(INFO) << "Regs (excluding ins) : " << cu_->num_regs;
316 LOG(INFO) << "Ins : " << cu_->num_ins;
317 LOG(INFO) << "Outs : " << cu_->num_outs;
318 LOG(INFO) << "CoreSpills : " << num_core_spills_;
319 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800320 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 LOG(INFO) << "Frame size : " << frame_size_;
322 LOG(INFO) << "code size is " << total_size_ <<
323 " bytes, Dalvik size is " << insns_size * 2;
324 LOG(INFO) << "expansion factor: "
325 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
326 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700327 UpdateLIROffsets();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) {
329 DumpLIRInsn(lir_insn, 0);
330 }
331 for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) {
332 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
333 lir_insn->operands[0]);
334 }
335
336 const DexFile::MethodId& method_id =
337 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700338 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
339 const char* name = cu_->dex_file->GetMethodName(method_id);
340 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341
342 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000343 if (!encoded_mapping_table_.empty()) {
344 MappingTable table(&encoded_mapping_table_[0]);
345 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
346 table.PcToDexSize(), table.PcToDexBegin());
347 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
348 table.DexToPcSize(), table.DexToPcBegin());
349 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350}
351
352/*
353 * Search the existing constants in the literal pool for an exact or close match
354 * within specified delta (greater or equal to 0).
355 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700356LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 while (data_target) {
358 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
359 return data_target;
360 data_target = data_target->next;
361 }
362 return NULL;
363}
364
365/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700366LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 bool lo_match = false;
368 LIR* lo_target = NULL;
369 while (data_target) {
370 if (lo_match && (data_target->operands[0] == val_hi)) {
371 // Record high word in case we need to expand this later.
372 lo_target->operands[1] = val_hi;
373 return lo_target;
374 }
375 lo_match = false;
376 if (data_target->operands[0] == val_lo) {
377 lo_match = true;
378 lo_target = data_target;
379 }
380 data_target = data_target->next;
381 }
382 return NULL;
383}
384
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100385/* Search the existing constants in the literal pool for an exact method match */
386LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
387 while (data_target) {
388 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
389 UnwrapPointer(data_target->operands[1]) == method.dex_file) {
390 return data_target;
391 }
392 data_target = data_target->next;
393 }
394 return nullptr;
395}
396
Fred Shihe7f82e22014-08-06 10:46:37 -0700397/* Search the existing constants in the literal pool for an exact class match */
398LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
399 while (data_target) {
400 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
401 UnwrapPointer(data_target->operands[1]) == &dex_file) {
402 return data_target;
403 }
404 data_target = data_target->next;
405 }
406 return nullptr;
407}
408
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409/*
410 * The following are building blocks to insert constants into the pool or
411 * instruction streams.
412 */
413
414/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700415LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 /* Add the constant to the literal pool */
417 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000418 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 new_value->operands[0] = value;
420 new_value->next = *constant_list_p;
421 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700422 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 return new_value;
424 }
425 return NULL;
426}
427
428/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700429LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 AddWordData(constant_list_p, val_hi);
431 return AddWordData(constant_list_p, val_lo);
432}
433
Andreas Gampe2da88232014-02-27 12:26:20 -0800434static void Push32(std::vector<uint8_t>&buf, int data) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700435 buf.push_back(data & 0xff);
436 buf.push_back((data >> 8) & 0xff);
437 buf.push_back((data >> 16) & 0xff);
438 buf.push_back((data >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439}
440
Andreas Gampe2da88232014-02-27 12:26:20 -0800441// Push 8 bytes on 64-bit target systems; 4 on 32-bit target systems.
442static void PushPointer(std::vector<uint8_t>&buf, const void* pointer, bool target64) {
443 uint64_t data = reinterpret_cast<uintptr_t>(pointer);
444 if (target64) {
445 Push32(buf, data & 0xFFFFFFFF);
446 Push32(buf, (data >> 32) & 0xFFFFFFFF);
buzbee0d829482013-10-11 15:24:55 -0700447 } else {
Andreas Gampe2da88232014-02-27 12:26:20 -0800448 Push32(buf, static_cast<uint32_t>(data));
buzbee0d829482013-10-11 15:24:55 -0700449 }
450}
451
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
453 while (buf.size() < offset) {
454 buf.push_back(0);
455 }
456}
457
458/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700459void Mir2Lir::InstallLiteralPools() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 AlignBuffer(code_buffer_, data_offset_);
461 LIR* data_lir = literal_list_;
462 while (data_lir != NULL) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800463 Push32(code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 data_lir = NEXT_LIR(data_lir);
465 }
466 // Push code and method literals, record offsets for the compiler to patch.
467 data_lir = code_literal_list_;
468 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700469 uint32_t target_method_idx = data_lir->operands[0];
470 const DexFile* target_dex_file =
471 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 cu_->compiler_driver->AddCodePatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700473 cu_->class_def_idx,
474 cu_->method_idx,
475 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700476 target_method_idx,
477 target_dex_file,
478 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700479 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700480 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700481 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700482 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 data_lir = NEXT_LIR(data_lir);
484 }
485 data_lir = method_literal_list_;
486 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700487 uint32_t target_method_idx = data_lir->operands[0];
488 const DexFile* target_dex_file =
489 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 cu_->compiler_driver->AddMethodPatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700491 cu_->class_def_idx,
492 cu_->method_idx,
493 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700494 target_method_idx,
495 target_dex_file,
496 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700497 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700498 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700499 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700500 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 data_lir = NEXT_LIR(data_lir);
502 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800503 // Push class literals.
504 data_lir = class_literal_list_;
505 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700506 uint32_t target_method_idx = data_lir->operands[0];
Fred Shihe7f82e22014-08-06 10:46:37 -0700507 const DexFile* class_dex_file =
508 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800509 cu_->compiler_driver->AddClassPatch(cu_->dex_file,
510 cu_->class_def_idx,
511 cu_->method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -0700512 target_method_idx,
Fred Shihe7f82e22014-08-06 10:46:37 -0700513 class_dex_file,
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800514 code_buffer_.size());
Fred Shih4fc78532014-08-06 16:44:22 -0700515 const DexFile::TypeId& target_method_id = class_dex_file->GetTypeId(target_method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800516 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700517 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800518 data_lir = NEXT_LIR(data_lir);
519 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520}
521
522/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700523void Mir2Lir::InstallSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
525 while (true) {
526 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
527 if (tab_rec == NULL) break;
528 AlignBuffer(code_buffer_, tab_rec->offset);
529 /*
530 * For Arm, our reference point is the address of the bx
531 * instruction that does the launch, so we have to subtract
532 * the auto pc-advance. For other targets the reference point
533 * is a label, so we can use the offset as-is.
534 */
535 int bx_offset = INVALID_OFFSET;
536 switch (cu_->instruction_set) {
537 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700538 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539 bx_offset = tab_rec->anchor->offset + 4;
540 break;
541 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700542 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 bx_offset = 0;
544 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100545 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 case kMips:
547 bx_offset = tab_rec->anchor->offset;
548 break;
549 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
550 }
551 if (cu_->verbose) {
552 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
553 }
554 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee0d829482013-10-11 15:24:55 -0700555 const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
557 int disp = tab_rec->targets[elems]->offset - bx_offset;
558 if (cu_->verbose) {
559 LOG(INFO) << " Case[" << elems << "] key: 0x"
560 << std::hex << keys[elems] << ", disp: 0x"
561 << std::hex << disp;
562 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800563 Push32(code_buffer_, keys[elems]);
564 Push32(code_buffer_,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 tab_rec->targets[elems]->offset - bx_offset);
566 }
567 } else {
568 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
569 static_cast<int>(Instruction::kPackedSwitchSignature));
570 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
571 int disp = tab_rec->targets[elems]->offset - bx_offset;
572 if (cu_->verbose) {
573 LOG(INFO) << " Case[" << elems << "] disp: 0x"
574 << std::hex << disp;
575 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800576 Push32(code_buffer_, tab_rec->targets[elems]->offset - bx_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 }
578 }
579 }
580}
581
582/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700583void Mir2Lir::InstallFillArrayData() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
585 while (true) {
586 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
587 if (tab_rec == NULL) break;
588 AlignBuffer(code_buffer_, tab_rec->offset);
589 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700590 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
591 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 }
593 }
594}
595
buzbee0d829482013-10-11 15:24:55 -0700596static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700597 for (; lir != NULL; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 lir->offset = offset;
599 offset += 4;
600 }
601 return offset;
602}
603
Ian Rogersff093b32014-04-30 19:04:27 -0700604static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
605 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700606 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700607 offset = RoundUp(offset, element_size);
buzbee0d829482013-10-11 15:24:55 -0700608 for (; lir != NULL; lir = lir->next) {
609 lir->offset = offset;
610 offset += element_size;
611 }
612 return offset;
613}
614
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700616bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000617 MappingTable table(&encoded_mapping_table_[0]);
618 std::vector<uint32_t> dex_pcs;
619 dex_pcs.reserve(table.DexToPcSize());
620 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
621 dex_pcs.push_back(it.DexPc());
622 }
623 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
624 std::sort(dex_pcs.begin(), dex_pcs.end());
625
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000627 auto it = dex_pcs.begin(), end = dex_pcs.end();
628 for (uint32_t dex_pc : mir_graph_->catches_) {
629 while (it != end && *it < dex_pc) {
630 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
631 ++it;
632 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000634 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
636 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000637 } else {
638 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
640 }
641 if (!success) {
642 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
643 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000644 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 }
646 return success;
647}
648
649
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700650void Mir2Lir::CreateMappingTables() {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000651 uint32_t pc2dex_data_size = 0u;
652 uint32_t pc2dex_entries = 0u;
653 uint32_t pc2dex_offset = 0u;
654 uint32_t pc2dex_dalvik_offset = 0u;
655 uint32_t dex2pc_data_size = 0u;
656 uint32_t dex2pc_entries = 0u;
657 uint32_t dex2pc_offset = 0u;
658 uint32_t dex2pc_dalvik_offset = 0u;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
660 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000661 pc2dex_entries += 1;
662 DCHECK(pc2dex_offset <= tgt_lir->offset);
663 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
664 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
665 static_cast<int32_t>(pc2dex_dalvik_offset));
666 pc2dex_offset = tgt_lir->offset;
667 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 }
669 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000670 dex2pc_entries += 1;
671 DCHECK(dex2pc_offset <= tgt_lir->offset);
672 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
673 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
674 static_cast<int32_t>(dex2pc_dalvik_offset));
675 dex2pc_offset = tgt_lir->offset;
676 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 }
678 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000679
680 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
681 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
682 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000683 encoded_mapping_table_.resize(data_size);
684 uint8_t* write_pos = &encoded_mapping_table_[0];
685 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
686 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
687 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
688 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000689
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000690 pc2dex_offset = 0u;
691 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000692 dex2pc_offset = 0u;
693 dex2pc_dalvik_offset = 0u;
694 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
695 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
696 DCHECK(pc2dex_offset <= tgt_lir->offset);
697 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
698 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
699 static_cast<int32_t>(pc2dex_dalvik_offset));
700 pc2dex_offset = tgt_lir->offset;
701 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
702 }
703 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
704 DCHECK(dex2pc_offset <= tgt_lir->offset);
705 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
706 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
707 static_cast<int32_t>(dex2pc_dalvik_offset));
708 dex2pc_offset = tgt_lir->offset;
709 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
710 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000711 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000712 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
713 hdr_data_size + pc2dex_data_size);
714 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000715
Ian Rogers96faf5b2013-08-09 22:05:32 -0700716 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000717 CHECK(VerifyCatchEntries());
718
Ian Rogers96faf5b2013-08-09 22:05:32 -0700719 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000720 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700721 CHECK_EQ(table.TotalSize(), total_entries);
722 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000723 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000724 auto it2 = table.DexToPcBegin();
725 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
726 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
727 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
728 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
729 ++it;
730 }
731 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
732 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
733 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
734 ++it2;
735 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700736 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000737 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000738 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700739 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740}
741
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000743 DCHECK(!encoded_mapping_table_.empty());
744 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000746 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
747 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 if (native_offset > max_native_offset) {
749 max_native_offset = native_offset;
750 }
751 }
752 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000753 const std::vector<uint8_t>& gc_map_raw =
754 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
755 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
756 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000758 GcMapBuilder native_gc_map_builder(&native_gc_map_,
759 mapping_table.PcToDexSize(),
760 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761
Vladimir Marko06606b92013-12-02 15:31:08 +0000762 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
763 uint32_t native_offset = it.NativePcOffset();
764 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Dave Allisonf9439142014-03-27 15:10:22 -0700766 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
767 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 native_gc_map_builder.AddEntry(native_offset, references);
769 }
770}
771
772/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700773int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Ian Rogersff093b32014-04-30 19:04:27 -0700775 unsigned int ptr_size = GetInstructionSetPointerSize(cu_->instruction_set);
776 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
777 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
778 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 return offset;
780}
781
buzbee0d829482013-10-11 15:24:55 -0700782int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
784 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700785 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 if (tab_rec == NULL) break;
787 tab_rec->offset = offset;
788 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
789 offset += tab_rec->table[1] * (sizeof(int) * 2);
790 } else {
791 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
792 static_cast<int>(Instruction::kPackedSwitchSignature));
793 offset += tab_rec->table[1] * sizeof(int);
794 }
795 }
796 return offset;
797}
798
buzbee0d829482013-10-11 15:24:55 -0700799int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
801 while (true) {
802 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
803 if (tab_rec == NULL) break;
804 tab_rec->offset = offset;
805 offset += tab_rec->size;
806 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700807 offset = RoundUp(offset, 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 }
809 return offset;
810}
811
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812/*
813 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700814 * offset vaddr if pretty-printing, otherise use the standard block
815 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700816 * branch table during the assembly phase. All resource flags
817 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 */
buzbee0d829482013-10-11 15:24:55 -0700819LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) {
buzbee252254b2013-09-08 16:20:53 -0700820 LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id];
buzbeeb48819d2013-09-14 16:15:25 -0700821 LIR* res = boundary_lir;
822 if (cu_->verbose) {
823 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000824 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
buzbeeb48819d2013-09-14 16:15:25 -0700825 new_label->dalvik_offset = vaddr;
826 new_label->opcode = kPseudoCaseLabel;
827 new_label->operands[0] = keyVal;
828 new_label->flags.fixup = kFixupLabel;
829 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100830 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700831 InsertLIRAfter(boundary_lir, new_label);
832 res = new_label;
833 }
834 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835}
836
buzbee0d829482013-10-11 15:24:55 -0700837void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700839 DexOffset base_vaddr = tab_rec->vaddr;
840 const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 int entries = table[1];
842 int low_key = s4FromSwitchData(&table[2]);
843 for (int i = 0; i < entries; i++) {
844 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key);
845 }
846}
847
buzbee0d829482013-10-11 15:24:55 -0700848void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700850 DexOffset base_vaddr = tab_rec->vaddr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700852 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
853 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 for (int i = 0; i < entries; i++) {
855 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]);
856 }
857}
858
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700859void Mir2Lir::ProcessSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
861 while (true) {
862 Mir2Lir::SwitchTable *tab_rec = iterator.Next();
863 if (tab_rec == NULL) break;
864 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
865 MarkPackedCaseLabels(tab_rec);
866 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
867 MarkSparseCaseLabels(tab_rec);
868 } else {
869 LOG(FATAL) << "Invalid switch table";
870 }
871 }
872}
873
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700874void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 /*
876 * Sparse switch data format:
877 * ushort ident = 0x0200 magic value
878 * ushort size number of entries in the table; > 0
879 * int keys[size] keys, sorted low-to-high; 32-bit aligned
880 * int targets[size] branch targets, relative to switch opcode
881 *
882 * Total size is (2+size*4) 16-bit code units.
883 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884 uint16_t ident = table[0];
885 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700886 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
887 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
889 << ", entries: " << std::dec << entries;
890 for (int i = 0; i < entries; i++) {
891 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
892 }
893}
894
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700895void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 /*
897 * Packed switch data format:
898 * ushort ident = 0x0100 magic value
899 * ushort size number of entries in the table
900 * int first_key first (and lowest) switch case value
901 * int targets[size] branch targets, relative to switch opcode
902 *
903 * Total size is (4+size*2) 16-bit code units.
904 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700906 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 int entries = table[1];
908 int low_key = s4FromSwitchData(&table[2]);
909 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
910 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
911 for (int i = 0; i < entries; i++) {
912 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
913 << targets[i];
914 }
915}
916
buzbee252254b2013-09-08 16:20:53 -0700917/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700918void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
919 // NOTE: only used for debug listings.
920 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921}
922
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700923bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 bool is_taken;
925 switch (opcode) {
926 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
927 case Instruction::IF_NE: is_taken = (src1 != src2); break;
928 case Instruction::IF_LT: is_taken = (src1 < src2); break;
929 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
930 case Instruction::IF_GT: is_taken = (src1 > src2); break;
931 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
932 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
933 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
934 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
935 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
936 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
937 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
938 default:
939 LOG(FATAL) << "Unexpected opcode " << opcode;
940 is_taken = false;
941 }
942 return is_taken;
943}
944
945// Convert relation of src1/src2 to src2/src1
946ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
947 ConditionCode res;
948 switch (before) {
949 case kCondEq: res = kCondEq; break;
950 case kCondNe: res = kCondNe; break;
951 case kCondLt: res = kCondGt; break;
952 case kCondGt: res = kCondLt; break;
953 case kCondLe: res = kCondGe; break;
954 case kCondGe: res = kCondLe; break;
955 default:
956 res = static_cast<ConditionCode>(0);
957 LOG(FATAL) << "Unexpected ccode " << before;
958 }
959 return res;
960}
961
Vladimir Markoa1a70742014-03-03 10:28:05 +0000962ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
963 ConditionCode res;
964 switch (before) {
965 case kCondEq: res = kCondNe; break;
966 case kCondNe: res = kCondEq; break;
967 case kCondLt: res = kCondGe; break;
968 case kCondGt: res = kCondLe; break;
969 case kCondLe: res = kCondGt; break;
970 case kCondGe: res = kCondLt; break;
971 default:
972 res = static_cast<ConditionCode>(0);
973 LOG(FATAL) << "Unexpected ccode " << before;
974 }
975 return res;
976}
977
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978// TODO: move to mir_to_lir.cc
979Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
980 : Backend(arena),
981 literal_list_(NULL),
982 method_literal_list_(NULL),
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800983 class_literal_list_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 code_literal_list_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -0700985 first_fixup_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 cu_(cu),
987 mir_graph_(mir_graph),
988 switch_tables_(arena, 4, kGrowableArraySwitchTables),
989 fill_array_data_(arena, 4, kGrowableArrayFillArrayData),
buzbeebd663de2013-09-10 15:41:31 -0700990 tempreg_info_(arena, 20, kGrowableArrayMisc),
buzbee091cc402014-03-31 10:14:40 -0700991 reginfo_map_(arena, RegStorage::kMaxRegs, kGrowableArrayMisc),
buzbee0d829482013-10-11 15:24:55 -0700992 pointer_storage_(arena, 128, kGrowableArrayMisc),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 data_offset_(0),
994 total_size_(0),
995 block_label_list_(NULL),
buzbeed69835d2014-02-03 14:40:27 -0800996 promotion_map_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997 current_dalvik_offset_(0),
buzbeeb48819d2013-09-14 16:15:25 -0700998 estimated_native_code_size_(0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 reg_pool_(NULL),
1000 live_sreg_(0),
Vladimir Marko8081d2b2014-07-31 15:33:43 +01001001 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1002 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003 num_core_spills_(0),
1004 num_fp_spills_(0),
1005 frame_size_(0),
1006 core_spill_mask_(0),
1007 fp_spill_mask_(0),
1008 first_lir_insn_(NULL),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001009 last_lir_insn_(NULL),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001010 slow_paths_(arena, 32, kGrowableArraySlowPaths),
1011 mem_ref_type_(ResourceMask::kHeapRef),
1012 mask_cache_(arena) {
buzbee0d829482013-10-11 15:24:55 -07001013 // Reserve pointer id 0 for NULL.
1014 size_t null_idx = WrapPointer(NULL);
1015 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016}
1017
1018void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001019 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1021
1022 /* Allocate Registers using simple local allocation scheme */
1023 SimpleRegAlloc();
1024
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001025 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001026 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001027 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001028 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001030 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1031 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 MethodMIR2LIR();
1033 }
1034
1035 /* Method is not empty */
1036 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037 // mark the targets of switch statement case labels
1038 ProcessSwitchTables();
1039
1040 /* Convert LIR into machine code. */
1041 AssembleLIR();
1042
buzbeeb01bf152014-05-13 15:59:07 -07001043 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 CodegenDump();
1045 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047}
1048
1049CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001050 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1051 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001053 // Prefix the encoded data with its size.
1054 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1055 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1056 vmap_encoder.PushBackUnsigned(size);
1057 // Core regs may have been inserted out of order - sort first.
1058 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1059 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1060 // Copy, stripping out the phys register sort key.
1061 vmap_encoder.PushBackUnsigned(
1062 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1063 }
1064 // Push a marker to take place of lr.
1065 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001066 if (cu_->instruction_set == kThumb2) {
1067 // fp regs already sorted.
1068 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1069 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1070 }
1071 } else {
1072 // For other platforms regs may have been inserted out of order - sort first.
1073 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1074 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1075 // Copy, stripping out the phys register sort key.
1076 vmap_encoder.PushBackUnsigned(
1077 ~(-1 << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
1078 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001079 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001081 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1082 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001083 DCHECK_EQ(core_vmap_table_.size(), 0u);
1084 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1085 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001087
Tong Shen547cdfd2014-08-05 01:54:19 -07001088 std::unique_ptr<std::vector<uint8_t>> cfi_info(ReturnFrameDescriptionEntry());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 CompiledMethod* result =
Ian Rogers72d32622014-05-06 16:20:11 -07001090 new CompiledMethod(cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_,
Vladimir Marko06606b92013-12-02 15:31:08 +00001091 core_spill_mask_, fp_spill_mask_, encoded_mapping_table_,
Dave Allisond6ed6422014-04-09 23:36:15 +00001092 vmap_encoder.GetData(), native_gc_map_, cfi_info.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 return result;
1094}
1095
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001096size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1097 // Chose a reasonably small value in order to contain stack growth.
1098 // Backends that are smarter about spill region can return larger values.
1099 const size_t max_compiler_temps = 10;
1100 return max_compiler_temps;
1101}
1102
1103size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1104 // By default assume that the Mir2Lir will need one slot for each temporary.
1105 // If the backend can better determine temps that have non-overlapping ranges and
1106 // temps that do not need spilled, it can actually provide a small region.
1107 return (mir_graph_->GetNumUsedCompilerTemps() * sizeof(uint32_t));
1108}
1109
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110int Mir2Lir::ComputeFrameSize() {
1111 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001112 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1113 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1114 + sizeof(uint32_t) // Filler.
1115 + (cu_->num_regs + cu_->num_outs) * sizeof(uint32_t)
1116 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001118 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119}
1120
1121/*
1122 * Append an LIR instruction to the LIR list maintained by a compilation
1123 * unit
1124 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001125void Mir2Lir::AppendLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 if (first_lir_insn_ == NULL) {
1127 DCHECK(last_lir_insn_ == NULL);
1128 last_lir_insn_ = first_lir_insn_ = lir;
1129 lir->prev = lir->next = NULL;
1130 } else {
1131 last_lir_insn_->next = lir;
1132 lir->prev = last_lir_insn_;
1133 lir->next = NULL;
1134 last_lir_insn_ = lir;
1135 }
1136}
1137
1138/*
1139 * Insert an LIR instruction before the current instruction, which cannot be the
1140 * first instruction.
1141 *
1142 * prev_lir <-> new_lir <-> current_lir
1143 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001144void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 DCHECK(current_lir->prev != NULL);
1146 LIR *prev_lir = current_lir->prev;
1147
1148 prev_lir->next = new_lir;
1149 new_lir->prev = prev_lir;
1150 new_lir->next = current_lir;
1151 current_lir->prev = new_lir;
1152}
1153
1154/*
1155 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001156 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 *
1158 * current_lir -> new_lir -> old_next
1159 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001160void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 new_lir->prev = current_lir;
1162 new_lir->next = current_lir->next;
1163 current_lir->next = new_lir;
1164 new_lir->next->prev = new_lir;
1165}
1166
Mark Mendell4708dcd2014-01-22 09:05:18 -08001167bool Mir2Lir::IsPowerOfTwo(uint64_t x) {
1168 return (x & (x - 1)) == 0;
1169}
1170
1171// Returns the index of the lowest set bit in 'x'.
1172int32_t Mir2Lir::LowestSetBit(uint64_t x) {
1173 int bit_posn = 0;
1174 while ((x & 0xf) == 0) {
1175 bit_posn += 4;
1176 x >>= 4;
1177 }
1178 while ((x & 1) == 0) {
1179 bit_posn++;
1180 x >>= 1;
1181 }
1182 return bit_posn;
1183}
1184
1185bool Mir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
1186 DCHECK(rl_src.wide);
1187 DCHECK(rl_dest.wide);
1188 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1189}
1190
buzbee2700f7e2014-03-07 09:46:20 -08001191LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001192 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001193 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001194 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1195 if (compare != nullptr) {
1196 *compare = inst;
1197 }
Mark Mendell766e9292014-01-27 07:55:47 -08001198 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1199 return branch;
1200}
1201
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001202void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
1203 slow_paths_.Insert(slowpath);
1204}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001205
Jeff Hao49161ce2014-03-12 11:05:25 -07001206void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1207 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001208 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001209 if (data_target == NULL) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001210 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001211 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001212 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1213 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1214 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001215 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001216 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001217 // Loads a code pointer. Code from oat file can be mapped anywhere.
1218 LIR* load_pc_rel = OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001219 AppendLIR(load_pc_rel);
1220 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1221}
1222
Jeff Hao49161ce2014-03-12 11:05:25 -07001223void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1224 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001225 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001226 if (data_target == NULL) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001227 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001228 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001229 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1230 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1231 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001232 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001233 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001234 // Loads an ArtMethod pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001235 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001236 AppendLIR(load_pc_rel);
1237 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1238}
1239
Fred Shihe7f82e22014-08-06 10:46:37 -07001240void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1241 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001242 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001243 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001244 if (data_target == nullptr) {
1245 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001246 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001247 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001248 // Loads a Class pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001249 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001250 AppendLIR(load_pc_rel);
1251}
1252
Tong Shen547cdfd2014-08-05 01:54:19 -07001253std::vector<uint8_t>* Mir2Lir::ReturnFrameDescriptionEntry() {
Mark Mendellae9fd932014-02-10 16:14:35 -08001254 // Default case is to do nothing.
1255 return nullptr;
1256}
1257
buzbee2700f7e2014-03-07 09:46:20 -08001258RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001259 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001260 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001261 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001262 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1263 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1264 info_lo->SetIsWide(false);
1265 info_hi->SetIsWide(false);
1266 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001267 } else {
buzbee85089dd2014-05-25 15:10:52 -07001268 RegisterInfo* info = GetRegInfo(loc.reg);
1269 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1270 DCHECK(info_new != nullptr);
1271 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1272 info->MarkDead();
1273 info_new->MarkLive(loc.s_reg_low);
1274 }
1275 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001276 }
buzbee85089dd2014-05-25 15:10:52 -07001277 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001278 }
buzbee85089dd2014-05-25 15:10:52 -07001279 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001280 return loc;
1281}
1282
Mark Mendelld65c51a2014-04-29 16:55:20 -04001283void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1284 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
1285}
1286
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001287} // namespace art