blob: 31854496ab206335f78391d89db7724b3e738249 [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"
Ian Rogers96faf5b2013-08-09 22:05:32 -070020#include "mapping_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "mir_to_lir-inl.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000022#include "dex/quick/dex_file_method_inliner.h"
23#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000024#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000025#include "dex/verified_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "verifier/dex_gc_map.h"
27#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000028#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029
30namespace art {
31
Vladimir Marko06606b92013-12-02 15:31:08 +000032namespace {
33
34/* Dump a mapping table */
35template <typename It>
36void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
37 const Signature& signature, uint32_t size, It first) {
38 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080039 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000040 descriptor, name, signature.ToString().c_str(), size));
41 std::replace(line.begin(), line.end(), ';', '_');
42 LOG(INFO) << line;
43 for (uint32_t i = 0; i != size; ++i) {
44 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
45 ++first;
46 LOG(INFO) << line;
47 }
48 LOG(INFO) <<" };\n\n";
49 }
50}
51
52} // anonymous namespace
53
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070054bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 bool res = false;
56 if (rl_src.is_const) {
57 if (rl_src.wide) {
58 if (rl_src.fp) {
59 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
60 } else {
61 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
62 }
63 } else {
64 if (rl_src.fp) {
65 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
66 } else {
67 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
68 }
69 }
70 }
71 return res;
72}
73
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070074void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070075 DCHECK(!inst->flags.use_def_invalid);
76 inst->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
buzbeeb48819d2013-09-14 16:15:25 -070078 DCHECK_EQ(safepoint_pc->u.m.def_mask, ENCODE_ALL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070079}
80
Ian Rogers9b297bf2013-09-06 11:11:25 -070081bool Mir2Lir::FastInstance(uint32_t field_idx, bool is_put, int* field_offset, bool* is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 return cu_->compiler_driver->ComputeInstanceFieldInfo(
Ian Rogers9b297bf2013-09-06 11:11:25 -070083 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), is_put, field_offset, is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -070084}
85
buzbee252254b2013-09-08 16:20:53 -070086/* Remove a LIR from the list. */
87void Mir2Lir::UnlinkLIR(LIR* lir) {
88 if (UNLIKELY(lir == first_lir_insn_)) {
89 first_lir_insn_ = lir->next;
90 if (lir->next != NULL) {
91 lir->next->prev = NULL;
92 } else {
93 DCHECK(lir->next == NULL);
94 DCHECK(lir == last_lir_insn_);
95 last_lir_insn_ = NULL;
96 }
97 } else if (lir == last_lir_insn_) {
98 last_lir_insn_ = lir->prev;
99 lir->prev->next = NULL;
100 } else if ((lir->prev != NULL) && (lir->next != NULL)) {
101 lir->prev->next = lir->next;
102 lir->next->prev = lir->prev;
103 }
104}
105
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700107void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700109 if (!cu_->verbose) {
110 UnlinkLIR(lir);
111 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112}
113
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700114void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 uint64_t *mask_ptr;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700116 uint64_t mask = ENCODE_MEM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700118 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700120 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700122 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 }
124 /* Clear out the memref flags */
125 *mask_ptr &= ~mask;
126 /* ..and then add back the one we need */
127 switch (mem_type) {
128 case kLiteral:
129 DCHECK(is_load);
130 *mask_ptr |= ENCODE_LITERAL;
131 break;
132 case kDalvikReg:
133 *mask_ptr |= ENCODE_DALVIK_REG;
134 break;
135 case kHeapRef:
136 *mask_ptr |= ENCODE_HEAP_REF;
137 break;
138 case kMustNotAlias:
139 /* Currently only loads can be marked as kMustNotAlias */
140 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
141 *mask_ptr |= ENCODE_MUST_NOT_ALIAS;
142 break;
143 default:
144 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
145 }
146}
147
148/*
149 * Mark load/store instructions that access Dalvik registers through the stack.
150 */
151void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700152 bool is64bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 SetMemRefType(lir, is_load, kDalvikReg);
154
155 /*
156 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
157 * access.
158 */
buzbeeb48819d2013-09-14 16:15:25 -0700159 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160}
161
162/*
163 * Debugging macros
164 */
165#define DUMP_RESOURCE_MASK(X)
166
167/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700168void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 int offset = lir->offset;
170 int dest = lir->operands[0];
171 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
172
173 /* Handle pseudo-ops individually, and all regular insns as a group */
174 switch (lir->opcode) {
175 case kPseudoMethodEntry:
176 LOG(INFO) << "-------- method entry "
177 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
178 break;
179 case kPseudoMethodExit:
180 LOG(INFO) << "-------- Method_Exit";
181 break;
182 case kPseudoBarrier:
183 LOG(INFO) << "-------- BARRIER";
184 break;
185 case kPseudoEntryBlock:
186 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
187 break;
188 case kPseudoDalvikByteCodeBoundary:
189 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700190 // NOTE: only used for debug listings.
191 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 }
193 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000194 << lir->dalvik_offset << " @ "
195 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 break;
197 case kPseudoExitBlock:
198 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
199 break;
200 case kPseudoPseudoAlign4:
201 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
202 << offset << "): .align4";
203 break;
204 case kPseudoEHBlockLabel:
205 LOG(INFO) << "Exception_Handling:";
206 break;
207 case kPseudoTargetLabel:
208 case kPseudoNormalBlockLabel:
209 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
210 break;
211 case kPseudoThrowTarget:
212 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
213 break;
214 case kPseudoIntrinsicRetry:
215 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
216 break;
217 case kPseudoSuspendTarget:
218 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
219 break;
220 case kPseudoSafepointPC:
221 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
222 break;
223 case kPseudoExportedPC:
224 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
225 break;
226 case kPseudoCaseLabel:
227 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
228 << std::hex << lir->operands[0] << "|" << std::dec <<
229 lir->operands[0];
230 break;
231 default:
232 if (lir->flags.is_nop && !dump_nop) {
233 break;
234 } else {
235 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
236 lir, base_addr));
237 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
238 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800239 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
240 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 op_name.c_str(), op_operands.c_str(),
242 lir->flags.is_nop ? "(nop)" : "");
243 }
244 break;
245 }
246
buzbeeb48819d2013-09-14 16:15:25 -0700247 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
248 DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 }
buzbeeb48819d2013-09-14 16:15:25 -0700250 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
251 DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 }
253}
254
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700255void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800256 int num_regs = cu_->num_dalvik_registers + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 for (int i = 0; i < num_regs; i++) {
258 PromotionMap v_reg_map = promotion_map_[i];
259 std::string buf;
260 if (v_reg_map.fp_location == kLocPhysReg) {
261 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & FpRegMask());
262 }
263
264 std::string buf3;
265 if (i < cu_->num_dalvik_registers) {
266 StringAppendF(&buf3, "%02d", i);
267 } else if (i == mir_graph_->GetMethodSReg()) {
268 buf3 = "Method*";
269 } else {
270 StringAppendF(&buf3, "ct%d", i - cu_->num_dalvik_registers);
271 }
272
273 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
274 v_reg_map.core_location == kLocPhysReg ?
275 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
276 v_reg_map.core_reg : SRegOffset(i),
277 buf.c_str());
278 }
279}
280
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700282void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 LOG(INFO) << "Dumping LIR insns for "
284 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
285 LIR* lir_insn;
286 int insns_size = cu_->code_item->insns_size_in_code_units_;
287
288 LOG(INFO) << "Regs (excluding ins) : " << cu_->num_regs;
289 LOG(INFO) << "Ins : " << cu_->num_ins;
290 LOG(INFO) << "Outs : " << cu_->num_outs;
291 LOG(INFO) << "CoreSpills : " << num_core_spills_;
292 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800293 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 LOG(INFO) << "Frame size : " << frame_size_;
295 LOG(INFO) << "code size is " << total_size_ <<
296 " bytes, Dalvik size is " << insns_size * 2;
297 LOG(INFO) << "expansion factor: "
298 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
299 DumpPromotionMap();
300 for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) {
301 DumpLIRInsn(lir_insn, 0);
302 }
303 for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) {
304 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
305 lir_insn->operands[0]);
306 }
307
308 const DexFile::MethodId& method_id =
309 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700310 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
311 const char* name = cu_->dex_file->GetMethodName(method_id);
312 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313
314 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000315 if (!encoded_mapping_table_.empty()) {
316 MappingTable table(&encoded_mapping_table_[0]);
317 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
318 table.PcToDexSize(), table.PcToDexBegin());
319 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
320 table.DexToPcSize(), table.DexToPcBegin());
321 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322}
323
324/*
325 * Search the existing constants in the literal pool for an exact or close match
326 * within specified delta (greater or equal to 0).
327 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700328LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329 while (data_target) {
330 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
331 return data_target;
332 data_target = data_target->next;
333 }
334 return NULL;
335}
336
337/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700338LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 bool lo_match = false;
340 LIR* lo_target = NULL;
341 while (data_target) {
342 if (lo_match && (data_target->operands[0] == val_hi)) {
343 // Record high word in case we need to expand this later.
344 lo_target->operands[1] = val_hi;
345 return lo_target;
346 }
347 lo_match = false;
348 if (data_target->operands[0] == val_lo) {
349 lo_match = true;
350 lo_target = data_target;
351 }
352 data_target = data_target->next;
353 }
354 return NULL;
355}
356
357/*
358 * The following are building blocks to insert constants into the pool or
359 * instruction streams.
360 */
361
362/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700363LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 /* Add the constant to the literal pool */
365 if (constant_list_p) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700366 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 new_value->operands[0] = value;
368 new_value->next = *constant_list_p;
369 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700370 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 return new_value;
372 }
373 return NULL;
374}
375
376/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700377LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 AddWordData(constant_list_p, val_hi);
379 return AddWordData(constant_list_p, val_lo);
380}
381
382static void PushWord(std::vector<uint8_t>&buf, int data) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700383 buf.push_back(data & 0xff);
384 buf.push_back((data >> 8) & 0xff);
385 buf.push_back((data >> 16) & 0xff);
386 buf.push_back((data >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387}
388
buzbee0d829482013-10-11 15:24:55 -0700389// Push 8 bytes on 64-bit systems; 4 on 32-bit systems.
390static void PushPointer(std::vector<uint8_t>&buf, void const* pointer) {
391 uintptr_t data = reinterpret_cast<uintptr_t>(pointer);
392 if (sizeof(void*) == sizeof(uint64_t)) {
393 PushWord(buf, (data >> (sizeof(void*) * 4)) & 0xFFFFFFFF);
394 PushWord(buf, data & 0xFFFFFFFF);
395 } else {
396 PushWord(buf, data);
397 }
398}
399
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
401 while (buf.size() < offset) {
402 buf.push_back(0);
403 }
404}
405
406/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700407void Mir2Lir::InstallLiteralPools() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 AlignBuffer(code_buffer_, data_offset_);
409 LIR* data_lir = literal_list_;
410 while (data_lir != NULL) {
411 PushWord(code_buffer_, data_lir->operands[0]);
412 data_lir = NEXT_LIR(data_lir);
413 }
414 // Push code and method literals, record offsets for the compiler to patch.
415 data_lir = code_literal_list_;
416 while (data_lir != NULL) {
417 uint32_t target = data_lir->operands[0];
418 cu_->compiler_driver->AddCodePatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700419 cu_->class_def_idx,
420 cu_->method_idx,
421 cu_->invoke_type,
422 target,
423 static_cast<InvokeType>(data_lir->operands[1]),
424 code_buffer_.size());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 const DexFile::MethodId& id = cu_->dex_file->GetMethodId(target);
buzbee0d829482013-10-11 15:24:55 -0700426 // unique value based on target to ensure code deduplication works
427 PushPointer(code_buffer_, &id);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 data_lir = NEXT_LIR(data_lir);
429 }
430 data_lir = method_literal_list_;
431 while (data_lir != NULL) {
432 uint32_t target = data_lir->operands[0];
433 cu_->compiler_driver->AddMethodPatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700434 cu_->class_def_idx,
435 cu_->method_idx,
436 cu_->invoke_type,
437 target,
438 static_cast<InvokeType>(data_lir->operands[1]),
439 code_buffer_.size());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 const DexFile::MethodId& id = cu_->dex_file->GetMethodId(target);
buzbee0d829482013-10-11 15:24:55 -0700441 // unique value based on target to ensure code deduplication works
442 PushPointer(code_buffer_, &id);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443 data_lir = NEXT_LIR(data_lir);
444 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800445 // Push class literals.
446 data_lir = class_literal_list_;
447 while (data_lir != NULL) {
448 uint32_t target = data_lir->operands[0];
449 cu_->compiler_driver->AddClassPatch(cu_->dex_file,
450 cu_->class_def_idx,
451 cu_->method_idx,
452 target,
453 code_buffer_.size());
454 const DexFile::TypeId& id = cu_->dex_file->GetTypeId(target);
455 // unique value based on target to ensure code deduplication works
456 PushPointer(code_buffer_, &id);
457 data_lir = NEXT_LIR(data_lir);
458 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459}
460
461/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700462void Mir2Lir::InstallSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
464 while (true) {
465 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
466 if (tab_rec == NULL) break;
467 AlignBuffer(code_buffer_, tab_rec->offset);
468 /*
469 * For Arm, our reference point is the address of the bx
470 * instruction that does the launch, so we have to subtract
471 * the auto pc-advance. For other targets the reference point
472 * is a label, so we can use the offset as-is.
473 */
474 int bx_offset = INVALID_OFFSET;
475 switch (cu_->instruction_set) {
476 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700477 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 bx_offset = tab_rec->anchor->offset + 4;
479 break;
480 case kX86:
481 bx_offset = 0;
482 break;
483 case kMips:
484 bx_offset = tab_rec->anchor->offset;
485 break;
486 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
487 }
488 if (cu_->verbose) {
489 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
490 }
491 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee0d829482013-10-11 15:24:55 -0700492 const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
494 int disp = tab_rec->targets[elems]->offset - bx_offset;
495 if (cu_->verbose) {
496 LOG(INFO) << " Case[" << elems << "] key: 0x"
497 << std::hex << keys[elems] << ", disp: 0x"
498 << std::hex << disp;
499 }
500 PushWord(code_buffer_, keys[elems]);
501 PushWord(code_buffer_,
502 tab_rec->targets[elems]->offset - bx_offset);
503 }
504 } else {
505 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
506 static_cast<int>(Instruction::kPackedSwitchSignature));
507 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
508 int disp = tab_rec->targets[elems]->offset - bx_offset;
509 if (cu_->verbose) {
510 LOG(INFO) << " Case[" << elems << "] disp: 0x"
511 << std::hex << disp;
512 }
513 PushWord(code_buffer_, tab_rec->targets[elems]->offset - bx_offset);
514 }
515 }
516 }
517}
518
519/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700520void Mir2Lir::InstallFillArrayData() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
522 while (true) {
523 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
524 if (tab_rec == NULL) break;
525 AlignBuffer(code_buffer_, tab_rec->offset);
526 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700527 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
528 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 }
530 }
531}
532
buzbee0d829482013-10-11 15:24:55 -0700533static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700534 for (; lir != NULL; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 lir->offset = offset;
536 offset += 4;
537 }
538 return offset;
539}
540
buzbee0d829482013-10-11 15:24:55 -0700541static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset) {
542 unsigned int element_size = sizeof(void*);
543 // Align to natural pointer size.
544 offset = (offset + (element_size - 1)) & ~(element_size - 1);
545 for (; lir != NULL; lir = lir->next) {
546 lir->offset = offset;
547 offset += element_size;
548 }
549 return offset;
550}
551
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700553bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000554 MappingTable table(&encoded_mapping_table_[0]);
555 std::vector<uint32_t> dex_pcs;
556 dex_pcs.reserve(table.DexToPcSize());
557 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
558 dex_pcs.push_back(it.DexPc());
559 }
560 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
561 std::sort(dex_pcs.begin(), dex_pcs.end());
562
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000564 auto it = dex_pcs.begin(), end = dex_pcs.end();
565 for (uint32_t dex_pc : mir_graph_->catches_) {
566 while (it != end && *it < dex_pc) {
567 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
568 ++it;
569 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000571 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
573 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000574 } else {
575 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 }
577 }
578 if (!success) {
579 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
580 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000581 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 }
583 return success;
584}
585
586
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700587void Mir2Lir::CreateMappingTables() {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000588 uint32_t pc2dex_data_size = 0u;
589 uint32_t pc2dex_entries = 0u;
590 uint32_t pc2dex_offset = 0u;
591 uint32_t pc2dex_dalvik_offset = 0u;
592 uint32_t dex2pc_data_size = 0u;
593 uint32_t dex2pc_entries = 0u;
594 uint32_t dex2pc_offset = 0u;
595 uint32_t dex2pc_dalvik_offset = 0u;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
597 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000598 pc2dex_entries += 1;
599 DCHECK(pc2dex_offset <= tgt_lir->offset);
600 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
601 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
602 static_cast<int32_t>(pc2dex_dalvik_offset));
603 pc2dex_offset = tgt_lir->offset;
604 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 }
606 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000607 dex2pc_entries += 1;
608 DCHECK(dex2pc_offset <= tgt_lir->offset);
609 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
610 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
611 static_cast<int32_t>(dex2pc_dalvik_offset));
612 dex2pc_offset = tgt_lir->offset;
613 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 }
615 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000616
617 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
618 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
619 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000620 encoded_mapping_table_.resize(data_size);
621 uint8_t* write_pos = &encoded_mapping_table_[0];
622 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
623 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
624 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
625 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000626
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000627 pc2dex_offset = 0u;
628 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000629 dex2pc_offset = 0u;
630 dex2pc_dalvik_offset = 0u;
631 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
632 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
633 DCHECK(pc2dex_offset <= tgt_lir->offset);
634 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
635 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
636 static_cast<int32_t>(pc2dex_dalvik_offset));
637 pc2dex_offset = tgt_lir->offset;
638 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
639 }
640 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
641 DCHECK(dex2pc_offset <= tgt_lir->offset);
642 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
643 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
644 static_cast<int32_t>(dex2pc_dalvik_offset));
645 dex2pc_offset = tgt_lir->offset;
646 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
647 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000648 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000649 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
650 hdr_data_size + pc2dex_data_size);
651 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000652
Ian Rogers96faf5b2013-08-09 22:05:32 -0700653 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000654 CHECK(VerifyCatchEntries());
655
Ian Rogers96faf5b2013-08-09 22:05:32 -0700656 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000657 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700658 CHECK_EQ(table.TotalSize(), total_entries);
659 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000660 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000661 auto it2 = table.DexToPcBegin();
662 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
663 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
664 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
665 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
666 ++it;
667 }
668 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
669 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
670 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
671 ++it2;
672 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700673 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000674 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000675 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700676 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677}
678
679class NativePcToReferenceMapBuilder {
680 public:
681 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
682 size_t entries, uint32_t max_native_offset,
683 size_t references_width) : entries_(entries),
684 references_width_(references_width), in_use_(entries),
685 table_(table) {
686 // Compute width in bytes needed to hold max_native_offset.
687 native_offset_width_ = 0;
688 while (max_native_offset != 0) {
689 native_offset_width_++;
690 max_native_offset >>= 8;
691 }
692 // Resize table and set up header.
693 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
694 CHECK_LT(native_offset_width_, 1U << 3);
695 (*table)[0] = native_offset_width_ & 7;
696 CHECK_LT(references_width_, 1U << 13);
697 (*table)[0] |= (references_width_ << 3) & 0xFF;
698 (*table)[1] = (references_width_ >> 5) & 0xFF;
699 CHECK_LT(entries, 1U << 16);
700 (*table)[2] = entries & 0xFF;
701 (*table)[3] = (entries >> 8) & 0xFF;
702 }
703
704 void AddEntry(uint32_t native_offset, const uint8_t* references) {
705 size_t table_index = TableIndex(native_offset);
706 while (in_use_[table_index]) {
707 table_index = (table_index + 1) % entries_;
708 }
709 in_use_[table_index] = true;
buzbee0d829482013-10-11 15:24:55 -0700710 SetCodeOffset(table_index, native_offset);
711 DCHECK_EQ(native_offset, GetCodeOffset(table_index));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 SetReferences(table_index, references);
713 }
714
715 private:
716 size_t TableIndex(uint32_t native_offset) {
717 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
718 }
719
buzbee0d829482013-10-11 15:24:55 -0700720 uint32_t GetCodeOffset(size_t table_index) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 uint32_t native_offset = 0;
722 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
723 for (size_t i = 0; i < native_offset_width_; i++) {
724 native_offset |= (*table_)[table_offset + i] << (i * 8);
725 }
726 return native_offset;
727 }
728
buzbee0d829482013-10-11 15:24:55 -0700729 void SetCodeOffset(size_t table_index, uint32_t native_offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
731 for (size_t i = 0; i < native_offset_width_; i++) {
732 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
733 }
734 }
735
736 void SetReferences(size_t table_index, const uint8_t* references) {
737 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
738 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
739 }
740
741 size_t EntryWidth() const {
742 return native_offset_width_ + references_width_;
743 }
744
745 // Number of entries in the table.
746 const size_t entries_;
747 // Number of bytes used to encode the reference bitmap.
748 const size_t references_width_;
749 // Number of bytes used to encode a native offset.
750 size_t native_offset_width_;
751 // Entries that are in use.
752 std::vector<bool> in_use_;
753 // The table we're building.
754 std::vector<uint8_t>* const table_;
755};
756
757void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000758 DCHECK(!encoded_mapping_table_.empty());
759 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000761 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
762 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 if (native_offset > max_native_offset) {
764 max_native_offset = native_offset;
765 }
766 }
767 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000768 const std::vector<uint8_t>& gc_map_raw =
769 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
770 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
771 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 // Compute native offset to references size.
773 NativePcToReferenceMapBuilder native_gc_map_builder(&native_gc_map_,
Vladimir Marko06606b92013-12-02 15:31:08 +0000774 mapping_table.PcToDexSize(),
775 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776
Vladimir Marko06606b92013-12-02 15:31:08 +0000777 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
778 uint32_t native_offset = it.NativePcOffset();
779 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
781 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
782 native_gc_map_builder.AddEntry(native_offset, references);
783 }
784}
785
786/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700787int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 offset = AssignLiteralOffsetCommon(literal_list_, offset);
buzbee0d829482013-10-11 15:24:55 -0700789 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset);
790 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800791 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 return offset;
793}
794
buzbee0d829482013-10-11 15:24:55 -0700795int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
797 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700798 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 if (tab_rec == NULL) break;
800 tab_rec->offset = offset;
801 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
802 offset += tab_rec->table[1] * (sizeof(int) * 2);
803 } else {
804 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
805 static_cast<int>(Instruction::kPackedSwitchSignature));
806 offset += tab_rec->table[1] * sizeof(int);
807 }
808 }
809 return offset;
810}
811
buzbee0d829482013-10-11 15:24:55 -0700812int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
814 while (true) {
815 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
816 if (tab_rec == NULL) break;
817 tab_rec->offset = offset;
818 offset += tab_rec->size;
819 // word align
820 offset = (offset + 3) & ~3;
821 }
822 return offset;
823}
824
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825/*
826 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700827 * offset vaddr if pretty-printing, otherise use the standard block
828 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700829 * branch table during the assembly phase. All resource flags
830 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 */
buzbee0d829482013-10-11 15:24:55 -0700832LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) {
buzbee252254b2013-09-08 16:20:53 -0700833 LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id];
buzbeeb48819d2013-09-14 16:15:25 -0700834 LIR* res = boundary_lir;
835 if (cu_->verbose) {
836 // Only pay the expense if we're pretty-printing.
837 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocLIR));
838 new_label->dalvik_offset = vaddr;
839 new_label->opcode = kPseudoCaseLabel;
840 new_label->operands[0] = keyVal;
841 new_label->flags.fixup = kFixupLabel;
842 DCHECK(!new_label->flags.use_def_invalid);
843 new_label->u.m.def_mask = ENCODE_ALL;
844 InsertLIRAfter(boundary_lir, new_label);
845 res = new_label;
846 }
847 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848}
849
buzbee0d829482013-10-11 15:24:55 -0700850void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700852 DexOffset base_vaddr = tab_rec->vaddr;
853 const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 int entries = table[1];
855 int low_key = s4FromSwitchData(&table[2]);
856 for (int i = 0; i < entries; i++) {
857 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key);
858 }
859}
860
buzbee0d829482013-10-11 15:24:55 -0700861void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700863 DexOffset base_vaddr = tab_rec->vaddr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700865 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
866 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 for (int i = 0; i < entries; i++) {
868 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]);
869 }
870}
871
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700872void Mir2Lir::ProcessSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
874 while (true) {
875 Mir2Lir::SwitchTable *tab_rec = iterator.Next();
876 if (tab_rec == NULL) break;
877 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
878 MarkPackedCaseLabels(tab_rec);
879 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
880 MarkSparseCaseLabels(tab_rec);
881 } else {
882 LOG(FATAL) << "Invalid switch table";
883 }
884 }
885}
886
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700887void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 /*
889 * Sparse switch data format:
890 * ushort ident = 0x0200 magic value
891 * ushort size number of entries in the table; > 0
892 * int keys[size] keys, sorted low-to-high; 32-bit aligned
893 * int targets[size] branch targets, relative to switch opcode
894 *
895 * Total size is (2+size*4) 16-bit code units.
896 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 uint16_t ident = table[0];
898 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700899 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
900 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
902 << ", entries: " << std::dec << entries;
903 for (int i = 0; i < entries; i++) {
904 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
905 }
906}
907
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700908void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909 /*
910 * Packed switch data format:
911 * ushort ident = 0x0100 magic value
912 * ushort size number of entries in the table
913 * int first_key first (and lowest) switch case value
914 * int targets[size] branch targets, relative to switch opcode
915 *
916 * Total size is (4+size*2) 16-bit code units.
917 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700919 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 int entries = table[1];
921 int low_key = s4FromSwitchData(&table[2]);
922 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
923 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
924 for (int i = 0; i < entries; i++) {
925 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
926 << targets[i];
927 }
928}
929
buzbee252254b2013-09-08 16:20:53 -0700930/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700931void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
932 // NOTE: only used for debug listings.
933 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934}
935
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700936bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700937 bool is_taken;
938 switch (opcode) {
939 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
940 case Instruction::IF_NE: is_taken = (src1 != src2); break;
941 case Instruction::IF_LT: is_taken = (src1 < src2); break;
942 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
943 case Instruction::IF_GT: is_taken = (src1 > src2); break;
944 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
945 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
946 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
947 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
948 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
949 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
950 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
951 default:
952 LOG(FATAL) << "Unexpected opcode " << opcode;
953 is_taken = false;
954 }
955 return is_taken;
956}
957
958// Convert relation of src1/src2 to src2/src1
959ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
960 ConditionCode res;
961 switch (before) {
962 case kCondEq: res = kCondEq; break;
963 case kCondNe: res = kCondNe; break;
964 case kCondLt: res = kCondGt; break;
965 case kCondGt: res = kCondLt; break;
966 case kCondLe: res = kCondGe; break;
967 case kCondGe: res = kCondLe; break;
968 default:
969 res = static_cast<ConditionCode>(0);
970 LOG(FATAL) << "Unexpected ccode " << before;
971 }
972 return res;
973}
974
975// TODO: move to mir_to_lir.cc
976Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
977 : Backend(arena),
978 literal_list_(NULL),
979 method_literal_list_(NULL),
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800980 class_literal_list_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 code_literal_list_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -0700982 first_fixup_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 cu_(cu),
984 mir_graph_(mir_graph),
985 switch_tables_(arena, 4, kGrowableArraySwitchTables),
986 fill_array_data_(arena, 4, kGrowableArrayFillArrayData),
987 throw_launchpads_(arena, 2048, kGrowableArrayThrowLaunchPads),
988 suspend_launchpads_(arena, 4, kGrowableArraySuspendLaunchPads),
989 intrinsic_launchpads_(arena, 2048, kGrowableArrayMisc),
buzbeebd663de2013-09-10 15:41:31 -0700990 tempreg_info_(arena, 20, kGrowableArrayMisc),
991 reginfo_map_(arena, 64, 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),
1001 num_core_spills_(0),
1002 num_fp_spills_(0),
1003 frame_size_(0),
1004 core_spill_mask_(0),
1005 fp_spill_mask_(0),
1006 first_lir_insn_(NULL),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001007 last_lir_insn_(NULL),
1008 slow_paths_(arena, 32, kGrowableArraySlowPaths) {
buzbee0d829482013-10-11 15:24:55 -07001009 // Reserve pointer id 0 for NULL.
1010 size_t null_idx = WrapPointer(NULL);
1011 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012}
1013
1014void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001015 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1017
1018 /* Allocate Registers using simple local allocation scheme */
1019 SimpleRegAlloc();
1020
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001021 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001022 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001023 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001024 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001026 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1027 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 MethodMIR2LIR();
1029 }
1030
1031 /* Method is not empty */
1032 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 // mark the targets of switch statement case labels
1034 ProcessSwitchTables();
1035
1036 /* Convert LIR into machine code. */
1037 AssembleLIR();
1038
1039 if (cu_->verbose) {
1040 CodegenDump();
1041 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043}
1044
1045CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001046 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1047 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001049 // Prefix the encoded data with its size.
1050 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1051 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1052 vmap_encoder.PushBackUnsigned(size);
1053 // Core regs may have been inserted out of order - sort first.
1054 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1055 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1056 // Copy, stripping out the phys register sort key.
1057 vmap_encoder.PushBackUnsigned(
1058 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1059 }
1060 // Push a marker to take place of lr.
1061 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
1062 // fp regs already sorted.
1063 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1064 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1065 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 } else {
1067 DCHECK_EQ(__builtin_popcount(core_spill_mask_), 0);
1068 DCHECK_EQ(__builtin_popcount(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001069 DCHECK_EQ(core_vmap_table_.size(), 0u);
1070 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1071 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 }
1073 CompiledMethod* result =
Mathieu Chartier193bad92013-08-29 18:46:00 -07001074 new CompiledMethod(*cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_,
Vladimir Marko06606b92013-12-02 15:31:08 +00001075 core_spill_mask_, fp_spill_mask_, encoded_mapping_table_,
Mathieu Chartier193bad92013-08-29 18:46:00 -07001076 vmap_encoder.GetData(), native_gc_map_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 return result;
1078}
1079
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001080size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1081 // Chose a reasonably small value in order to contain stack growth.
1082 // Backends that are smarter about spill region can return larger values.
1083 const size_t max_compiler_temps = 10;
1084 return max_compiler_temps;
1085}
1086
1087size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1088 // By default assume that the Mir2Lir will need one slot for each temporary.
1089 // If the backend can better determine temps that have non-overlapping ranges and
1090 // temps that do not need spilled, it can actually provide a small region.
1091 return (mir_graph_->GetNumUsedCompilerTemps() * sizeof(uint32_t));
1092}
1093
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094int Mir2Lir::ComputeFrameSize() {
1095 /* Figure out the frame size */
1096 static const uint32_t kAlignMask = kStackAlignment - 1;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001097 uint32_t size = ((num_core_spills_ + num_fp_spills_ +
1098 1 /* filler word */ + cu_->num_regs + cu_->num_outs)
1099 * sizeof(uint32_t)) +
1100 GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 /* Align and set */
1102 return (size + kAlignMask) & ~(kAlignMask);
1103}
1104
1105/*
1106 * Append an LIR instruction to the LIR list maintained by a compilation
1107 * unit
1108 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001109void Mir2Lir::AppendLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 if (first_lir_insn_ == NULL) {
1111 DCHECK(last_lir_insn_ == NULL);
1112 last_lir_insn_ = first_lir_insn_ = lir;
1113 lir->prev = lir->next = NULL;
1114 } else {
1115 last_lir_insn_->next = lir;
1116 lir->prev = last_lir_insn_;
1117 lir->next = NULL;
1118 last_lir_insn_ = lir;
1119 }
1120}
1121
1122/*
1123 * Insert an LIR instruction before the current instruction, which cannot be the
1124 * first instruction.
1125 *
1126 * prev_lir <-> new_lir <-> current_lir
1127 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001128void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 DCHECK(current_lir->prev != NULL);
1130 LIR *prev_lir = current_lir->prev;
1131
1132 prev_lir->next = new_lir;
1133 new_lir->prev = prev_lir;
1134 new_lir->next = current_lir;
1135 current_lir->prev = new_lir;
1136}
1137
1138/*
1139 * Insert an LIR instruction after the current instruction, which cannot be the
1140 * first instruction.
1141 *
1142 * current_lir -> new_lir -> old_next
1143 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001144void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 new_lir->prev = current_lir;
1146 new_lir->next = current_lir->next;
1147 current_lir->next = new_lir;
1148 new_lir->next->prev = new_lir;
1149}
1150
Mark Mendell4708dcd2014-01-22 09:05:18 -08001151bool Mir2Lir::IsPowerOfTwo(uint64_t x) {
1152 return (x & (x - 1)) == 0;
1153}
1154
1155// Returns the index of the lowest set bit in 'x'.
1156int32_t Mir2Lir::LowestSetBit(uint64_t x) {
1157 int bit_posn = 0;
1158 while ((x & 0xf) == 0) {
1159 bit_posn += 4;
1160 x >>= 4;
1161 }
1162 while ((x & 1) == 0) {
1163 bit_posn++;
1164 x >>= 1;
1165 }
1166 return bit_posn;
1167}
1168
1169bool Mir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
1170 DCHECK(rl_src.wide);
1171 DCHECK(rl_dest.wide);
1172 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1173}
1174
Mark Mendell766e9292014-01-27 07:55:47 -08001175LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, int temp_reg, int base_reg,
1176 int offset, int check_value, LIR* target) {
1177 // Handle this for architectures that can't compare to memory.
1178 LoadWordDisp(base_reg, offset, temp_reg);
1179 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1180 return branch;
1181}
1182
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001183void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
1184 slow_paths_.Insert(slowpath);
1185}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001186
1187void Mir2Lir::LoadCodeAddress(int dex_method_index, InvokeType type, SpecialTargetRegister symbolic_reg) {
1188 LIR* data_target = ScanLiteralPool(code_literal_list_, dex_method_index, 0);
1189 if (data_target == NULL) {
1190 data_target = AddWordData(&code_literal_list_, dex_method_index);
1191 data_target->operands[1] = type;
1192 }
1193 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg), data_target);
1194 AppendLIR(load_pc_rel);
1195 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1196}
1197
1198void Mir2Lir::LoadMethodAddress(int dex_method_index, InvokeType type, SpecialTargetRegister symbolic_reg) {
1199 LIR* data_target = ScanLiteralPool(method_literal_list_, dex_method_index, 0);
1200 if (data_target == NULL) {
1201 data_target = AddWordData(&method_literal_list_, dex_method_index);
1202 data_target->operands[1] = type;
1203 }
1204 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg), data_target);
1205 AppendLIR(load_pc_rel);
1206 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1207}
1208
1209void Mir2Lir::LoadClassType(uint32_t type_idx, SpecialTargetRegister symbolic_reg) {
1210 // Use the literal pool and a PC-relative load from a data word.
1211 LIR* data_target = ScanLiteralPool(class_literal_list_, type_idx, 0);
1212 if (data_target == nullptr) {
1213 data_target = AddWordData(&class_literal_list_, type_idx);
1214 }
1215 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg), data_target);
1216 AppendLIR(load_pc_rel);
1217}
1218
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001219} // namespace art