blob: 6e6b8f0a30f6edf1f83831cebe5af045be58098d [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);
77 inst->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
buzbeeb48819d2013-09-14 16:15:25 -070079 DCHECK_EQ(safepoint_pc->u.m.def_mask, ENCODE_ALL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070080}
81
buzbee252254b2013-09-08 16:20:53 -070082/* Remove a LIR from the list. */
83void Mir2Lir::UnlinkLIR(LIR* lir) {
84 if (UNLIKELY(lir == first_lir_insn_)) {
85 first_lir_insn_ = lir->next;
86 if (lir->next != NULL) {
87 lir->next->prev = NULL;
88 } else {
89 DCHECK(lir->next == NULL);
90 DCHECK(lir == last_lir_insn_);
91 last_lir_insn_ = NULL;
92 }
93 } else if (lir == last_lir_insn_) {
94 last_lir_insn_ = lir->prev;
95 lir->prev->next = NULL;
96 } else if ((lir->prev != NULL) && (lir->next != NULL)) {
97 lir->prev->next = lir->next;
98 lir->next->prev = lir->prev;
99 }
100}
101
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700103void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700105 if (!cu_->verbose) {
106 UnlinkLIR(lir);
107 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108}
109
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700110void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 uint64_t *mask_ptr;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700112 uint64_t mask = ENCODE_MEM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700114 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700116 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700118 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 }
120 /* Clear out the memref flags */
121 *mask_ptr &= ~mask;
122 /* ..and then add back the one we need */
123 switch (mem_type) {
124 case kLiteral:
125 DCHECK(is_load);
126 *mask_ptr |= ENCODE_LITERAL;
127 break;
128 case kDalvikReg:
129 *mask_ptr |= ENCODE_DALVIK_REG;
130 break;
131 case kHeapRef:
132 *mask_ptr |= ENCODE_HEAP_REF;
133 break;
134 case kMustNotAlias:
135 /* Currently only loads can be marked as kMustNotAlias */
136 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
137 *mask_ptr |= ENCODE_MUST_NOT_ALIAS;
138 break;
139 default:
140 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
141 }
142}
143
144/*
145 * Mark load/store instructions that access Dalvik registers through the stack.
146 */
147void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700148 bool is64bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 SetMemRefType(lir, is_load, kDalvikReg);
150
151 /*
152 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
153 * access.
154 */
buzbeeb48819d2013-09-14 16:15:25 -0700155 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156}
157
158/*
159 * Debugging macros
160 */
161#define DUMP_RESOURCE_MASK(X)
162
163/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700164void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 int offset = lir->offset;
166 int dest = lir->operands[0];
167 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
168
169 /* Handle pseudo-ops individually, and all regular insns as a group */
170 switch (lir->opcode) {
171 case kPseudoMethodEntry:
172 LOG(INFO) << "-------- method entry "
173 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
174 break;
175 case kPseudoMethodExit:
176 LOG(INFO) << "-------- Method_Exit";
177 break;
178 case kPseudoBarrier:
179 LOG(INFO) << "-------- BARRIER";
180 break;
181 case kPseudoEntryBlock:
182 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
183 break;
184 case kPseudoDalvikByteCodeBoundary:
185 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700186 // NOTE: only used for debug listings.
187 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 }
189 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000190 << lir->dalvik_offset << " @ "
191 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 break;
193 case kPseudoExitBlock:
194 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
195 break;
196 case kPseudoPseudoAlign4:
197 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
198 << offset << "): .align4";
199 break;
200 case kPseudoEHBlockLabel:
201 LOG(INFO) << "Exception_Handling:";
202 break;
203 case kPseudoTargetLabel:
204 case kPseudoNormalBlockLabel:
205 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
206 break;
207 case kPseudoThrowTarget:
208 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
209 break;
210 case kPseudoIntrinsicRetry:
211 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
212 break;
213 case kPseudoSuspendTarget:
214 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
215 break;
216 case kPseudoSafepointPC:
217 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
218 break;
219 case kPseudoExportedPC:
220 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
221 break;
222 case kPseudoCaseLabel:
223 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
224 << std::hex << lir->operands[0] << "|" << std::dec <<
225 lir->operands[0];
226 break;
227 default:
228 if (lir->flags.is_nop && !dump_nop) {
229 break;
230 } else {
231 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
232 lir, base_addr));
233 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
234 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800235 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
236 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 op_name.c_str(), op_operands.c_str(),
238 lir->flags.is_nop ? "(nop)" : "");
239 }
240 break;
241 }
242
buzbeeb48819d2013-09-14 16:15:25 -0700243 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
244 DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 }
buzbeeb48819d2013-09-14 16:15:25 -0700246 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
247 DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 }
249}
250
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700251void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800252 int num_regs = cu_->num_dalvik_registers + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 for (int i = 0; i < num_regs; i++) {
254 PromotionMap v_reg_map = promotion_map_[i];
255 std::string buf;
256 if (v_reg_map.fp_location == kLocPhysReg) {
257 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & FpRegMask());
258 }
259
260 std::string buf3;
261 if (i < cu_->num_dalvik_registers) {
262 StringAppendF(&buf3, "%02d", i);
263 } else if (i == mir_graph_->GetMethodSReg()) {
264 buf3 = "Method*";
265 } else {
266 StringAppendF(&buf3, "ct%d", i - cu_->num_dalvik_registers);
267 }
268
269 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
270 v_reg_map.core_location == kLocPhysReg ?
271 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
272 v_reg_map.core_reg : SRegOffset(i),
273 buf.c_str());
274 }
275}
276
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700278void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 LOG(INFO) << "Dumping LIR insns for "
280 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
281 LIR* lir_insn;
282 int insns_size = cu_->code_item->insns_size_in_code_units_;
283
284 LOG(INFO) << "Regs (excluding ins) : " << cu_->num_regs;
285 LOG(INFO) << "Ins : " << cu_->num_ins;
286 LOG(INFO) << "Outs : " << cu_->num_outs;
287 LOG(INFO) << "CoreSpills : " << num_core_spills_;
288 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800289 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 LOG(INFO) << "Frame size : " << frame_size_;
291 LOG(INFO) << "code size is " << total_size_ <<
292 " bytes, Dalvik size is " << insns_size * 2;
293 LOG(INFO) << "expansion factor: "
294 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
295 DumpPromotionMap();
296 for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) {
297 DumpLIRInsn(lir_insn, 0);
298 }
299 for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) {
300 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
301 lir_insn->operands[0]);
302 }
303
304 const DexFile::MethodId& method_id =
305 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700306 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
307 const char* name = cu_->dex_file->GetMethodName(method_id);
308 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309
310 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000311 if (!encoded_mapping_table_.empty()) {
312 MappingTable table(&encoded_mapping_table_[0]);
313 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
314 table.PcToDexSize(), table.PcToDexBegin());
315 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
316 table.DexToPcSize(), table.DexToPcBegin());
317 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318}
319
320/*
321 * Search the existing constants in the literal pool for an exact or close match
322 * within specified delta (greater or equal to 0).
323 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700324LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 while (data_target) {
326 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
327 return data_target;
328 data_target = data_target->next;
329 }
330 return NULL;
331}
332
333/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700334LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 bool lo_match = false;
336 LIR* lo_target = NULL;
337 while (data_target) {
338 if (lo_match && (data_target->operands[0] == val_hi)) {
339 // Record high word in case we need to expand this later.
340 lo_target->operands[1] = val_hi;
341 return lo_target;
342 }
343 lo_match = false;
344 if (data_target->operands[0] == val_lo) {
345 lo_match = true;
346 lo_target = data_target;
347 }
348 data_target = data_target->next;
349 }
350 return NULL;
351}
352
353/*
354 * The following are building blocks to insert constants into the pool or
355 * instruction streams.
356 */
357
358/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700359LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 /* Add the constant to the literal pool */
361 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000362 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 new_value->operands[0] = value;
364 new_value->next = *constant_list_p;
365 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700366 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 return new_value;
368 }
369 return NULL;
370}
371
372/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700373LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 AddWordData(constant_list_p, val_hi);
375 return AddWordData(constant_list_p, val_lo);
376}
377
Andreas Gampe2da88232014-02-27 12:26:20 -0800378static void Push32(std::vector<uint8_t>&buf, int data) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700379 buf.push_back(data & 0xff);
380 buf.push_back((data >> 8) & 0xff);
381 buf.push_back((data >> 16) & 0xff);
382 buf.push_back((data >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383}
384
Andreas Gampe2da88232014-02-27 12:26:20 -0800385// Push 8 bytes on 64-bit target systems; 4 on 32-bit target systems.
386static void PushPointer(std::vector<uint8_t>&buf, const void* pointer, bool target64) {
387 uint64_t data = reinterpret_cast<uintptr_t>(pointer);
388 if (target64) {
389 Push32(buf, data & 0xFFFFFFFF);
390 Push32(buf, (data >> 32) & 0xFFFFFFFF);
buzbee0d829482013-10-11 15:24:55 -0700391 } else {
Andreas Gampe2da88232014-02-27 12:26:20 -0800392 Push32(buf, static_cast<uint32_t>(data));
buzbee0d829482013-10-11 15:24:55 -0700393 }
394}
395
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
397 while (buf.size() < offset) {
398 buf.push_back(0);
399 }
400}
401
402/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700403void Mir2Lir::InstallLiteralPools() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 AlignBuffer(code_buffer_, data_offset_);
405 LIR* data_lir = literal_list_;
406 while (data_lir != NULL) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800407 Push32(code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 data_lir = NEXT_LIR(data_lir);
409 }
410 // Push code and method literals, record offsets for the compiler to patch.
411 data_lir = code_literal_list_;
412 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700413 uint32_t target_method_idx = data_lir->operands[0];
414 const DexFile* target_dex_file =
415 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 cu_->compiler_driver->AddCodePatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700417 cu_->class_def_idx,
418 cu_->method_idx,
419 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700420 target_method_idx,
421 target_dex_file,
422 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700423 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700424 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700425 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700426 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 data_lir = NEXT_LIR(data_lir);
428 }
429 data_lir = method_literal_list_;
430 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700431 uint32_t target_method_idx = data_lir->operands[0];
432 const DexFile* target_dex_file =
433 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 cu_->compiler_driver->AddMethodPatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700435 cu_->class_def_idx,
436 cu_->method_idx,
437 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700438 target_method_idx,
439 target_dex_file,
440 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700441 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700442 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700443 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700444 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 data_lir = NEXT_LIR(data_lir);
446 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800447 // Push class literals.
448 data_lir = class_literal_list_;
449 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700450 uint32_t target_method_idx = data_lir->operands[0];
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800451 cu_->compiler_driver->AddClassPatch(cu_->dex_file,
452 cu_->class_def_idx,
453 cu_->method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -0700454 target_method_idx,
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800455 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700456 const DexFile::TypeId& target_method_id = cu_->dex_file->GetTypeId(target_method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800457 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700458 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800459 data_lir = NEXT_LIR(data_lir);
460 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461}
462
463/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700464void Mir2Lir::InstallSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
466 while (true) {
467 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
468 if (tab_rec == NULL) break;
469 AlignBuffer(code_buffer_, tab_rec->offset);
470 /*
471 * For Arm, our reference point is the address of the bx
472 * instruction that does the launch, so we have to subtract
473 * the auto pc-advance. For other targets the reference point
474 * is a label, so we can use the offset as-is.
475 */
476 int bx_offset = INVALID_OFFSET;
477 switch (cu_->instruction_set) {
478 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700479 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 bx_offset = tab_rec->anchor->offset + 4;
481 break;
482 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700483 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 bx_offset = 0;
485 break;
486 case kMips:
487 bx_offset = tab_rec->anchor->offset;
488 break;
489 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
490 }
491 if (cu_->verbose) {
492 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
493 }
494 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee0d829482013-10-11 15:24:55 -0700495 const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
497 int disp = tab_rec->targets[elems]->offset - bx_offset;
498 if (cu_->verbose) {
499 LOG(INFO) << " Case[" << elems << "] key: 0x"
500 << std::hex << keys[elems] << ", disp: 0x"
501 << std::hex << disp;
502 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800503 Push32(code_buffer_, keys[elems]);
504 Push32(code_buffer_,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 tab_rec->targets[elems]->offset - bx_offset);
506 }
507 } else {
508 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
509 static_cast<int>(Instruction::kPackedSwitchSignature));
510 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
511 int disp = tab_rec->targets[elems]->offset - bx_offset;
512 if (cu_->verbose) {
513 LOG(INFO) << " Case[" << elems << "] disp: 0x"
514 << std::hex << disp;
515 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800516 Push32(code_buffer_, tab_rec->targets[elems]->offset - bx_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 }
518 }
519 }
520}
521
522/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700523void Mir2Lir::InstallFillArrayData() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
525 while (true) {
526 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
527 if (tab_rec == NULL) break;
528 AlignBuffer(code_buffer_, tab_rec->offset);
529 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700530 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
531 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 }
533 }
534}
535
buzbee0d829482013-10-11 15:24:55 -0700536static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700537 for (; lir != NULL; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 lir->offset = offset;
539 offset += 4;
540 }
541 return offset;
542}
543
buzbee0d829482013-10-11 15:24:55 -0700544static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset) {
545 unsigned int element_size = sizeof(void*);
546 // Align to natural pointer size.
547 offset = (offset + (element_size - 1)) & ~(element_size - 1);
548 for (; lir != NULL; lir = lir->next) {
549 lir->offset = offset;
550 offset += element_size;
551 }
552 return offset;
553}
554
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700556bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000557 MappingTable table(&encoded_mapping_table_[0]);
558 std::vector<uint32_t> dex_pcs;
559 dex_pcs.reserve(table.DexToPcSize());
560 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
561 dex_pcs.push_back(it.DexPc());
562 }
563 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
564 std::sort(dex_pcs.begin(), dex_pcs.end());
565
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000567 auto it = dex_pcs.begin(), end = dex_pcs.end();
568 for (uint32_t dex_pc : mir_graph_->catches_) {
569 while (it != end && *it < dex_pc) {
570 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
571 ++it;
572 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000574 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
576 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000577 } else {
578 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 }
580 }
581 if (!success) {
582 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
583 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000584 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 }
586 return success;
587}
588
589
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700590void Mir2Lir::CreateMappingTables() {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000591 uint32_t pc2dex_data_size = 0u;
592 uint32_t pc2dex_entries = 0u;
593 uint32_t pc2dex_offset = 0u;
594 uint32_t pc2dex_dalvik_offset = 0u;
595 uint32_t dex2pc_data_size = 0u;
596 uint32_t dex2pc_entries = 0u;
597 uint32_t dex2pc_offset = 0u;
598 uint32_t dex2pc_dalvik_offset = 0u;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
600 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000601 pc2dex_entries += 1;
602 DCHECK(pc2dex_offset <= tgt_lir->offset);
603 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
604 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
605 static_cast<int32_t>(pc2dex_dalvik_offset));
606 pc2dex_offset = tgt_lir->offset;
607 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 }
609 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000610 dex2pc_entries += 1;
611 DCHECK(dex2pc_offset <= tgt_lir->offset);
612 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
613 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
614 static_cast<int32_t>(dex2pc_dalvik_offset));
615 dex2pc_offset = tgt_lir->offset;
616 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 }
618 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000619
620 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
621 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
622 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000623 encoded_mapping_table_.resize(data_size);
624 uint8_t* write_pos = &encoded_mapping_table_[0];
625 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
626 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
627 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
628 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000629
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000630 pc2dex_offset = 0u;
631 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000632 dex2pc_offset = 0u;
633 dex2pc_dalvik_offset = 0u;
634 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
635 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
636 DCHECK(pc2dex_offset <= tgt_lir->offset);
637 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
638 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
639 static_cast<int32_t>(pc2dex_dalvik_offset));
640 pc2dex_offset = tgt_lir->offset;
641 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
642 }
643 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
644 DCHECK(dex2pc_offset <= tgt_lir->offset);
645 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
646 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
647 static_cast<int32_t>(dex2pc_dalvik_offset));
648 dex2pc_offset = tgt_lir->offset;
649 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
650 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000651 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000652 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
653 hdr_data_size + pc2dex_data_size);
654 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000655
Ian Rogers96faf5b2013-08-09 22:05:32 -0700656 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000657 CHECK(VerifyCatchEntries());
658
Ian Rogers96faf5b2013-08-09 22:05:32 -0700659 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000660 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700661 CHECK_EQ(table.TotalSize(), total_entries);
662 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000663 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000664 auto it2 = table.DexToPcBegin();
665 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
666 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
667 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
668 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
669 ++it;
670 }
671 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
672 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
673 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
674 ++it2;
675 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700676 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000677 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000678 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700679 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680}
681
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000683 DCHECK(!encoded_mapping_table_.empty());
684 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000686 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
687 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 if (native_offset > max_native_offset) {
689 max_native_offset = native_offset;
690 }
691 }
692 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000693 const std::vector<uint8_t>& gc_map_raw =
694 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
695 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
696 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000698 GcMapBuilder native_gc_map_builder(&native_gc_map_,
699 mapping_table.PcToDexSize(),
700 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701
Vladimir Marko06606b92013-12-02 15:31:08 +0000702 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
703 uint32_t native_offset = it.NativePcOffset();
704 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Dave Allisonf9439142014-03-27 15:10:22 -0700706 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
707 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 native_gc_map_builder.AddEntry(native_offset, references);
709 }
710}
711
712/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700713int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 offset = AssignLiteralOffsetCommon(literal_list_, offset);
buzbee0d829482013-10-11 15:24:55 -0700715 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset);
716 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800717 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 return offset;
719}
720
buzbee0d829482013-10-11 15:24:55 -0700721int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
723 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700724 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 if (tab_rec == NULL) break;
726 tab_rec->offset = offset;
727 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
728 offset += tab_rec->table[1] * (sizeof(int) * 2);
729 } else {
730 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
731 static_cast<int>(Instruction::kPackedSwitchSignature));
732 offset += tab_rec->table[1] * sizeof(int);
733 }
734 }
735 return offset;
736}
737
buzbee0d829482013-10-11 15:24:55 -0700738int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700739 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
740 while (true) {
741 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
742 if (tab_rec == NULL) break;
743 tab_rec->offset = offset;
744 offset += tab_rec->size;
745 // word align
746 offset = (offset + 3) & ~3;
747 }
748 return offset;
749}
750
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751/*
752 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700753 * offset vaddr if pretty-printing, otherise use the standard block
754 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700755 * branch table during the assembly phase. All resource flags
756 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 */
buzbee0d829482013-10-11 15:24:55 -0700758LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) {
buzbee252254b2013-09-08 16:20:53 -0700759 LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id];
buzbeeb48819d2013-09-14 16:15:25 -0700760 LIR* res = boundary_lir;
761 if (cu_->verbose) {
762 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000763 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
buzbeeb48819d2013-09-14 16:15:25 -0700764 new_label->dalvik_offset = vaddr;
765 new_label->opcode = kPseudoCaseLabel;
766 new_label->operands[0] = keyVal;
767 new_label->flags.fixup = kFixupLabel;
768 DCHECK(!new_label->flags.use_def_invalid);
769 new_label->u.m.def_mask = ENCODE_ALL;
770 InsertLIRAfter(boundary_lir, new_label);
771 res = new_label;
772 }
773 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774}
775
buzbee0d829482013-10-11 15:24:55 -0700776void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700778 DexOffset base_vaddr = tab_rec->vaddr;
779 const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 int entries = table[1];
781 int low_key = s4FromSwitchData(&table[2]);
782 for (int i = 0; i < entries; i++) {
783 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key);
784 }
785}
786
buzbee0d829482013-10-11 15:24:55 -0700787void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700789 DexOffset base_vaddr = tab_rec->vaddr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700791 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
792 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 for (int i = 0; i < entries; i++) {
794 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]);
795 }
796}
797
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700798void Mir2Lir::ProcessSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
800 while (true) {
801 Mir2Lir::SwitchTable *tab_rec = iterator.Next();
802 if (tab_rec == NULL) break;
803 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
804 MarkPackedCaseLabels(tab_rec);
805 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
806 MarkSparseCaseLabels(tab_rec);
807 } else {
808 LOG(FATAL) << "Invalid switch table";
809 }
810 }
811}
812
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700813void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 /*
815 * Sparse switch data format:
816 * ushort ident = 0x0200 magic value
817 * ushort size number of entries in the table; > 0
818 * int keys[size] keys, sorted low-to-high; 32-bit aligned
819 * int targets[size] branch targets, relative to switch opcode
820 *
821 * Total size is (2+size*4) 16-bit code units.
822 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 uint16_t ident = table[0];
824 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700825 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
826 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700827 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
828 << ", entries: " << std::dec << entries;
829 for (int i = 0; i < entries; i++) {
830 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
831 }
832}
833
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700834void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 /*
836 * Packed switch data format:
837 * ushort ident = 0x0100 magic value
838 * ushort size number of entries in the table
839 * int first_key first (and lowest) switch case value
840 * int targets[size] branch targets, relative to switch opcode
841 *
842 * Total size is (4+size*2) 16-bit code units.
843 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700845 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 int entries = table[1];
847 int low_key = s4FromSwitchData(&table[2]);
848 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
849 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
850 for (int i = 0; i < entries; i++) {
851 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
852 << targets[i];
853 }
854}
855
buzbee252254b2013-09-08 16:20:53 -0700856/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700857void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
858 // NOTE: only used for debug listings.
859 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860}
861
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700862bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 bool is_taken;
864 switch (opcode) {
865 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
866 case Instruction::IF_NE: is_taken = (src1 != src2); break;
867 case Instruction::IF_LT: is_taken = (src1 < src2); break;
868 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
869 case Instruction::IF_GT: is_taken = (src1 > src2); break;
870 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
871 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
872 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
873 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
874 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
875 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
876 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
877 default:
878 LOG(FATAL) << "Unexpected opcode " << opcode;
879 is_taken = false;
880 }
881 return is_taken;
882}
883
884// Convert relation of src1/src2 to src2/src1
885ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
886 ConditionCode res;
887 switch (before) {
888 case kCondEq: res = kCondEq; break;
889 case kCondNe: res = kCondNe; break;
890 case kCondLt: res = kCondGt; break;
891 case kCondGt: res = kCondLt; break;
892 case kCondLe: res = kCondGe; break;
893 case kCondGe: res = kCondLe; break;
894 default:
895 res = static_cast<ConditionCode>(0);
896 LOG(FATAL) << "Unexpected ccode " << before;
897 }
898 return res;
899}
900
Vladimir Markoa1a70742014-03-03 10:28:05 +0000901ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
902 ConditionCode res;
903 switch (before) {
904 case kCondEq: res = kCondNe; break;
905 case kCondNe: res = kCondEq; break;
906 case kCondLt: res = kCondGe; break;
907 case kCondGt: res = kCondLe; break;
908 case kCondLe: res = kCondGt; break;
909 case kCondGe: res = kCondLt; break;
910 default:
911 res = static_cast<ConditionCode>(0);
912 LOG(FATAL) << "Unexpected ccode " << before;
913 }
914 return res;
915}
916
Brian Carlstrom7940e442013-07-12 13:46:57 -0700917// TODO: move to mir_to_lir.cc
918Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
919 : Backend(arena),
920 literal_list_(NULL),
921 method_literal_list_(NULL),
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800922 class_literal_list_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923 code_literal_list_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -0700924 first_fixup_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700925 cu_(cu),
926 mir_graph_(mir_graph),
927 switch_tables_(arena, 4, kGrowableArraySwitchTables),
928 fill_array_data_(arena, 4, kGrowableArrayFillArrayData),
929 throw_launchpads_(arena, 2048, kGrowableArrayThrowLaunchPads),
930 suspend_launchpads_(arena, 4, kGrowableArraySuspendLaunchPads),
buzbeebd663de2013-09-10 15:41:31 -0700931 tempreg_info_(arena, 20, kGrowableArrayMisc),
932 reginfo_map_(arena, 64, kGrowableArrayMisc),
buzbee0d829482013-10-11 15:24:55 -0700933 pointer_storage_(arena, 128, kGrowableArrayMisc),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 data_offset_(0),
935 total_size_(0),
936 block_label_list_(NULL),
buzbeed69835d2014-02-03 14:40:27 -0800937 promotion_map_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700938 current_dalvik_offset_(0),
buzbeeb48819d2013-09-14 16:15:25 -0700939 estimated_native_code_size_(0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 reg_pool_(NULL),
941 live_sreg_(0),
942 num_core_spills_(0),
943 num_fp_spills_(0),
944 frame_size_(0),
945 core_spill_mask_(0),
946 fp_spill_mask_(0),
947 first_lir_insn_(NULL),
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800948 last_lir_insn_(NULL),
949 slow_paths_(arena, 32, kGrowableArraySlowPaths) {
buzbee0d829482013-10-11 15:24:55 -0700950 // Reserve pointer id 0 for NULL.
951 size_t null_idx = WrapPointer(NULL);
952 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953}
954
955void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -0700956 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
958
959 /* Allocate Registers using simple local allocation scheme */
960 SimpleRegAlloc();
961
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800962 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +0000963 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800964 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +0000965 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800967 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
968 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 MethodMIR2LIR();
970 }
971
972 /* Method is not empty */
973 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 // mark the targets of switch statement case labels
975 ProcessSwitchTables();
976
977 /* Convert LIR into machine code. */
978 AssembleLIR();
979
980 if (cu_->verbose) {
981 CodegenDump();
982 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984}
985
986CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +0000987 // Combine vmap tables - core regs, then fp regs - into vmap_table.
988 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +0000990 // Prefix the encoded data with its size.
991 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
992 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
993 vmap_encoder.PushBackUnsigned(size);
994 // Core regs may have been inserted out of order - sort first.
995 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
996 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
997 // Copy, stripping out the phys register sort key.
998 vmap_encoder.PushBackUnsigned(
999 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1000 }
1001 // Push a marker to take place of lr.
1002 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
1003 // fp regs already sorted.
1004 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1005 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1006 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 } else {
1008 DCHECK_EQ(__builtin_popcount(core_spill_mask_), 0);
1009 DCHECK_EQ(__builtin_popcount(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001010 DCHECK_EQ(core_vmap_table_.size(), 0u);
1011 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1012 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001014
1015 UniquePtr<std::vector<uint8_t> > cfi_info(ReturnCallFrameInformation());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 CompiledMethod* result =
Mathieu Chartier193bad92013-08-29 18:46:00 -07001017 new CompiledMethod(*cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_,
Vladimir Marko06606b92013-12-02 15:31:08 +00001018 core_spill_mask_, fp_spill_mask_, encoded_mapping_table_,
Dave Allisond6ed6422014-04-09 23:36:15 +00001019 vmap_encoder.GetData(), native_gc_map_, cfi_info.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 return result;
1021}
1022
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001023size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1024 // Chose a reasonably small value in order to contain stack growth.
1025 // Backends that are smarter about spill region can return larger values.
1026 const size_t max_compiler_temps = 10;
1027 return max_compiler_temps;
1028}
1029
1030size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1031 // By default assume that the Mir2Lir will need one slot for each temporary.
1032 // If the backend can better determine temps that have non-overlapping ranges and
1033 // temps that do not need spilled, it can actually provide a small region.
1034 return (mir_graph_->GetNumUsedCompilerTemps() * sizeof(uint32_t));
1035}
1036
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037int Mir2Lir::ComputeFrameSize() {
1038 /* Figure out the frame size */
1039 static const uint32_t kAlignMask = kStackAlignment - 1;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001040 uint32_t size = ((num_core_spills_ + num_fp_spills_ +
1041 1 /* filler word */ + cu_->num_regs + cu_->num_outs)
1042 * sizeof(uint32_t)) +
1043 GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 /* Align and set */
1045 return (size + kAlignMask) & ~(kAlignMask);
1046}
1047
1048/*
1049 * Append an LIR instruction to the LIR list maintained by a compilation
1050 * unit
1051 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001052void Mir2Lir::AppendLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 if (first_lir_insn_ == NULL) {
1054 DCHECK(last_lir_insn_ == NULL);
1055 last_lir_insn_ = first_lir_insn_ = lir;
1056 lir->prev = lir->next = NULL;
1057 } else {
1058 last_lir_insn_->next = lir;
1059 lir->prev = last_lir_insn_;
1060 lir->next = NULL;
1061 last_lir_insn_ = lir;
1062 }
1063}
1064
1065/*
1066 * Insert an LIR instruction before the current instruction, which cannot be the
1067 * first instruction.
1068 *
1069 * prev_lir <-> new_lir <-> current_lir
1070 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001071void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 DCHECK(current_lir->prev != NULL);
1073 LIR *prev_lir = current_lir->prev;
1074
1075 prev_lir->next = new_lir;
1076 new_lir->prev = prev_lir;
1077 new_lir->next = current_lir;
1078 current_lir->prev = new_lir;
1079}
1080
1081/*
1082 * Insert an LIR instruction after the current instruction, which cannot be the
1083 * first instruction.
1084 *
1085 * current_lir -> new_lir -> old_next
1086 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001087void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 new_lir->prev = current_lir;
1089 new_lir->next = current_lir->next;
1090 current_lir->next = new_lir;
1091 new_lir->next->prev = new_lir;
1092}
1093
Mark Mendell4708dcd2014-01-22 09:05:18 -08001094bool Mir2Lir::IsPowerOfTwo(uint64_t x) {
1095 return (x & (x - 1)) == 0;
1096}
1097
1098// Returns the index of the lowest set bit in 'x'.
1099int32_t Mir2Lir::LowestSetBit(uint64_t x) {
1100 int bit_posn = 0;
1101 while ((x & 0xf) == 0) {
1102 bit_posn += 4;
1103 x >>= 4;
1104 }
1105 while ((x & 1) == 0) {
1106 bit_posn++;
1107 x >>= 1;
1108 }
1109 return bit_posn;
1110}
1111
1112bool Mir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
1113 DCHECK(rl_src.wide);
1114 DCHECK(rl_dest.wide);
1115 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1116}
1117
buzbee2700f7e2014-03-07 09:46:20 -08001118LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Mark Mendell766e9292014-01-27 07:55:47 -08001119 int offset, int check_value, LIR* target) {
1120 // Handle this for architectures that can't compare to memory.
1121 LoadWordDisp(base_reg, offset, temp_reg);
1122 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1123 return branch;
1124}
1125
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001126void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
1127 slow_paths_.Insert(slowpath);
1128}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001129
Jeff Hao49161ce2014-03-12 11:05:25 -07001130void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1131 SpecialTargetRegister symbolic_reg) {
1132 int target_method_idx = target_method.dex_method_index;
1133 LIR* data_target = ScanLiteralPool(code_literal_list_, target_method_idx, 0);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001134 if (data_target == NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -07001135 data_target = AddWordData(&code_literal_list_, target_method_idx);
1136 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
1137 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001138 }
1139 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg), data_target);
1140 AppendLIR(load_pc_rel);
1141 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1142}
1143
Jeff Hao49161ce2014-03-12 11:05:25 -07001144void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1145 SpecialTargetRegister symbolic_reg) {
1146 int target_method_idx = target_method.dex_method_index;
1147 LIR* data_target = ScanLiteralPool(method_literal_list_, target_method_idx, 0);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001148 if (data_target == NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -07001149 data_target = AddWordData(&method_literal_list_, target_method_idx);
1150 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
1151 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001152 }
1153 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg), data_target);
1154 AppendLIR(load_pc_rel);
1155 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1156}
1157
1158void Mir2Lir::LoadClassType(uint32_t type_idx, SpecialTargetRegister symbolic_reg) {
1159 // Use the literal pool and a PC-relative load from a data word.
1160 LIR* data_target = ScanLiteralPool(class_literal_list_, type_idx, 0);
1161 if (data_target == nullptr) {
1162 data_target = AddWordData(&class_literal_list_, type_idx);
1163 }
1164 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg), data_target);
1165 AppendLIR(load_pc_rel);
1166}
1167
Mark Mendellae9fd932014-02-10 16:14:35 -08001168std::vector<uint8_t>* Mir2Lir::ReturnCallFrameInformation() {
1169 // Default case is to do nothing.
1170 return nullptr;
1171}
1172
buzbee2700f7e2014-03-07 09:46:20 -08001173RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
1174 loc.wide = false;
1175 if (loc.reg.IsPair()) {
1176 loc.reg = loc.reg.GetLow();
1177 }
1178 return loc;
1179}
1180
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001181} // namespace art