blob: 5e0fed7fd9229de611df2a8e73fd5de2a8fb01c4 [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"
28
29namespace art {
30
Vladimir Marko06606b92013-12-02 15:31:08 +000031namespace {
32
33/* Dump a mapping table */
34template <typename It>
35void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
36 const Signature& signature, uint32_t size, It first) {
37 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080038 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000039 descriptor, name, signature.ToString().c_str(), size));
40 std::replace(line.begin(), line.end(), ';', '_');
41 LOG(INFO) << line;
42 for (uint32_t i = 0; i != size; ++i) {
43 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
44 ++first;
45 LOG(INFO) << line;
46 }
47 LOG(INFO) <<" };\n\n";
48 }
49}
50
51} // anonymous namespace
52
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070053bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 bool res = false;
55 if (rl_src.is_const) {
56 if (rl_src.wide) {
57 if (rl_src.fp) {
58 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
59 } else {
60 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
61 }
62 } else {
63 if (rl_src.fp) {
64 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
65 } else {
66 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
67 }
68 }
69 }
70 return res;
71}
72
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070073void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070074 DCHECK(!inst->flags.use_def_invalid);
75 inst->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
buzbeeb48819d2013-09-14 16:15:25 -070077 DCHECK_EQ(safepoint_pc->u.m.def_mask, ENCODE_ALL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070078}
79
Ian Rogers9b297bf2013-09-06 11:11:25 -070080bool Mir2Lir::FastInstance(uint32_t field_idx, bool is_put, int* field_offset, bool* is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 return cu_->compiler_driver->ComputeInstanceFieldInfo(
Ian Rogers9b297bf2013-09-06 11:11:25 -070082 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), is_put, field_offset, is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -070083}
84
buzbee252254b2013-09-08 16:20:53 -070085/* Remove a LIR from the list. */
86void Mir2Lir::UnlinkLIR(LIR* lir) {
87 if (UNLIKELY(lir == first_lir_insn_)) {
88 first_lir_insn_ = lir->next;
89 if (lir->next != NULL) {
90 lir->next->prev = NULL;
91 } else {
92 DCHECK(lir->next == NULL);
93 DCHECK(lir == last_lir_insn_);
94 last_lir_insn_ = NULL;
95 }
96 } else if (lir == last_lir_insn_) {
97 last_lir_insn_ = lir->prev;
98 lir->prev->next = NULL;
99 } else if ((lir->prev != NULL) && (lir->next != NULL)) {
100 lir->prev->next = lir->next;
101 lir->next->prev = lir->prev;
102 }
103}
104
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700106void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700108 if (!cu_->verbose) {
109 UnlinkLIR(lir);
110 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111}
112
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700113void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 uint64_t *mask_ptr;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700115 uint64_t mask = ENCODE_MEM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700117 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700119 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700121 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 }
123 /* Clear out the memref flags */
124 *mask_ptr &= ~mask;
125 /* ..and then add back the one we need */
126 switch (mem_type) {
127 case kLiteral:
128 DCHECK(is_load);
129 *mask_ptr |= ENCODE_LITERAL;
130 break;
131 case kDalvikReg:
132 *mask_ptr |= ENCODE_DALVIK_REG;
133 break;
134 case kHeapRef:
135 *mask_ptr |= ENCODE_HEAP_REF;
136 break;
137 case kMustNotAlias:
138 /* Currently only loads can be marked as kMustNotAlias */
139 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
140 *mask_ptr |= ENCODE_MUST_NOT_ALIAS;
141 break;
142 default:
143 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
144 }
145}
146
147/*
148 * Mark load/store instructions that access Dalvik registers through the stack.
149 */
150void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700151 bool is64bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 SetMemRefType(lir, is_load, kDalvikReg);
153
154 /*
155 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
156 * access.
157 */
buzbeeb48819d2013-09-14 16:15:25 -0700158 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159}
160
161/*
162 * Debugging macros
163 */
164#define DUMP_RESOURCE_MASK(X)
165
166/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700167void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 int offset = lir->offset;
169 int dest = lir->operands[0];
170 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
171
172 /* Handle pseudo-ops individually, and all regular insns as a group */
173 switch (lir->opcode) {
174 case kPseudoMethodEntry:
175 LOG(INFO) << "-------- method entry "
176 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
177 break;
178 case kPseudoMethodExit:
179 LOG(INFO) << "-------- Method_Exit";
180 break;
181 case kPseudoBarrier:
182 LOG(INFO) << "-------- BARRIER";
183 break;
184 case kPseudoEntryBlock:
185 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
186 break;
187 case kPseudoDalvikByteCodeBoundary:
188 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700189 // NOTE: only used for debug listings.
190 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 }
192 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000193 << lir->dalvik_offset << " @ "
194 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195 break;
196 case kPseudoExitBlock:
197 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
198 break;
199 case kPseudoPseudoAlign4:
200 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
201 << offset << "): .align4";
202 break;
203 case kPseudoEHBlockLabel:
204 LOG(INFO) << "Exception_Handling:";
205 break;
206 case kPseudoTargetLabel:
207 case kPseudoNormalBlockLabel:
208 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
209 break;
210 case kPseudoThrowTarget:
211 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
212 break;
213 case kPseudoIntrinsicRetry:
214 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
215 break;
216 case kPseudoSuspendTarget:
217 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
218 break;
219 case kPseudoSafepointPC:
220 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
221 break;
222 case kPseudoExportedPC:
223 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
224 break;
225 case kPseudoCaseLabel:
226 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
227 << std::hex << lir->operands[0] << "|" << std::dec <<
228 lir->operands[0];
229 break;
230 default:
231 if (lir->flags.is_nop && !dump_nop) {
232 break;
233 } else {
234 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
235 lir, base_addr));
236 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
237 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800238 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
239 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 op_name.c_str(), op_operands.c_str(),
241 lir->flags.is_nop ? "(nop)" : "");
242 }
243 break;
244 }
245
buzbeeb48819d2013-09-14 16:15:25 -0700246 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
247 DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 }
buzbeeb48819d2013-09-14 16:15:25 -0700249 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
250 DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 }
252}
253
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700254void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800255 int num_regs = cu_->num_dalvik_registers + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 for (int i = 0; i < num_regs; i++) {
257 PromotionMap v_reg_map = promotion_map_[i];
258 std::string buf;
259 if (v_reg_map.fp_location == kLocPhysReg) {
260 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & FpRegMask());
261 }
262
263 std::string buf3;
264 if (i < cu_->num_dalvik_registers) {
265 StringAppendF(&buf3, "%02d", i);
266 } else if (i == mir_graph_->GetMethodSReg()) {
267 buf3 = "Method*";
268 } else {
269 StringAppendF(&buf3, "ct%d", i - cu_->num_dalvik_registers);
270 }
271
272 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
273 v_reg_map.core_location == kLocPhysReg ?
274 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
275 v_reg_map.core_reg : SRegOffset(i),
276 buf.c_str());
277 }
278}
279
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700281void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 LOG(INFO) << "Dumping LIR insns for "
283 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
284 LIR* lir_insn;
285 int insns_size = cu_->code_item->insns_size_in_code_units_;
286
287 LOG(INFO) << "Regs (excluding ins) : " << cu_->num_regs;
288 LOG(INFO) << "Ins : " << cu_->num_ins;
289 LOG(INFO) << "Outs : " << cu_->num_outs;
290 LOG(INFO) << "CoreSpills : " << num_core_spills_;
291 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800292 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 LOG(INFO) << "Frame size : " << frame_size_;
294 LOG(INFO) << "code size is " << total_size_ <<
295 " bytes, Dalvik size is " << insns_size * 2;
296 LOG(INFO) << "expansion factor: "
297 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
298 DumpPromotionMap();
299 for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) {
300 DumpLIRInsn(lir_insn, 0);
301 }
302 for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) {
303 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
304 lir_insn->operands[0]);
305 }
306
307 const DexFile::MethodId& method_id =
308 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700309 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
310 const char* name = cu_->dex_file->GetMethodName(method_id);
311 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312
313 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000314 if (!encoded_mapping_table_.empty()) {
315 MappingTable table(&encoded_mapping_table_[0]);
316 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
317 table.PcToDexSize(), table.PcToDexBegin());
318 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
319 table.DexToPcSize(), table.DexToPcBegin());
320 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321}
322
323/*
324 * Search the existing constants in the literal pool for an exact or close match
325 * within specified delta (greater or equal to 0).
326 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700327LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 while (data_target) {
329 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
330 return data_target;
331 data_target = data_target->next;
332 }
333 return NULL;
334}
335
336/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700337LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 bool lo_match = false;
339 LIR* lo_target = NULL;
340 while (data_target) {
341 if (lo_match && (data_target->operands[0] == val_hi)) {
342 // Record high word in case we need to expand this later.
343 lo_target->operands[1] = val_hi;
344 return lo_target;
345 }
346 lo_match = false;
347 if (data_target->operands[0] == val_lo) {
348 lo_match = true;
349 lo_target = data_target;
350 }
351 data_target = data_target->next;
352 }
353 return NULL;
354}
355
356/*
357 * The following are building blocks to insert constants into the pool or
358 * instruction streams.
359 */
360
361/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700362LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 /* Add the constant to the literal pool */
364 if (constant_list_p) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700365 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 new_value->operands[0] = value;
367 new_value->next = *constant_list_p;
368 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700369 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 return new_value;
371 }
372 return NULL;
373}
374
375/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700376LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 AddWordData(constant_list_p, val_hi);
378 return AddWordData(constant_list_p, val_lo);
379}
380
381static void PushWord(std::vector<uint8_t>&buf, int data) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700382 buf.push_back(data & 0xff);
383 buf.push_back((data >> 8) & 0xff);
384 buf.push_back((data >> 16) & 0xff);
385 buf.push_back((data >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386}
387
buzbee0d829482013-10-11 15:24:55 -0700388// Push 8 bytes on 64-bit systems; 4 on 32-bit systems.
389static void PushPointer(std::vector<uint8_t>&buf, void const* pointer) {
390 uintptr_t data = reinterpret_cast<uintptr_t>(pointer);
391 if (sizeof(void*) == sizeof(uint64_t)) {
392 PushWord(buf, (data >> (sizeof(void*) * 4)) & 0xFFFFFFFF);
393 PushWord(buf, data & 0xFFFFFFFF);
394 } else {
395 PushWord(buf, data);
396 }
397}
398
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
400 while (buf.size() < offset) {
401 buf.push_back(0);
402 }
403}
404
405/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700406void Mir2Lir::InstallLiteralPools() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 AlignBuffer(code_buffer_, data_offset_);
408 LIR* data_lir = literal_list_;
409 while (data_lir != NULL) {
410 PushWord(code_buffer_, data_lir->operands[0]);
411 data_lir = NEXT_LIR(data_lir);
412 }
413 // Push code and method literals, record offsets for the compiler to patch.
414 data_lir = code_literal_list_;
415 while (data_lir != NULL) {
416 uint32_t target = data_lir->operands[0];
417 cu_->compiler_driver->AddCodePatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700418 cu_->class_def_idx,
419 cu_->method_idx,
420 cu_->invoke_type,
421 target,
422 static_cast<InvokeType>(data_lir->operands[1]),
423 code_buffer_.size());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 const DexFile::MethodId& id = cu_->dex_file->GetMethodId(target);
buzbee0d829482013-10-11 15:24:55 -0700425 // unique value based on target to ensure code deduplication works
426 PushPointer(code_buffer_, &id);
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) {
431 uint32_t target = data_lir->operands[0];
432 cu_->compiler_driver->AddMethodPatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700433 cu_->class_def_idx,
434 cu_->method_idx,
435 cu_->invoke_type,
436 target,
437 static_cast<InvokeType>(data_lir->operands[1]),
438 code_buffer_.size());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 const DexFile::MethodId& id = cu_->dex_file->GetMethodId(target);
buzbee0d829482013-10-11 15:24:55 -0700440 // unique value based on target to ensure code deduplication works
441 PushPointer(code_buffer_, &id);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 data_lir = NEXT_LIR(data_lir);
443 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800444 // Push class literals.
445 data_lir = class_literal_list_;
446 while (data_lir != NULL) {
447 uint32_t target = data_lir->operands[0];
448 cu_->compiler_driver->AddClassPatch(cu_->dex_file,
449 cu_->class_def_idx,
450 cu_->method_idx,
451 target,
452 code_buffer_.size());
453 const DexFile::TypeId& id = cu_->dex_file->GetTypeId(target);
454 // unique value based on target to ensure code deduplication works
455 PushPointer(code_buffer_, &id);
456 data_lir = NEXT_LIR(data_lir);
457 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458}
459
460/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700461void Mir2Lir::InstallSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
463 while (true) {
464 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
465 if (tab_rec == NULL) break;
466 AlignBuffer(code_buffer_, tab_rec->offset);
467 /*
468 * For Arm, our reference point is the address of the bx
469 * instruction that does the launch, so we have to subtract
470 * the auto pc-advance. For other targets the reference point
471 * is a label, so we can use the offset as-is.
472 */
473 int bx_offset = INVALID_OFFSET;
474 switch (cu_->instruction_set) {
475 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700476 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 bx_offset = tab_rec->anchor->offset + 4;
478 break;
479 case kX86:
480 bx_offset = 0;
481 break;
482 case kMips:
483 bx_offset = tab_rec->anchor->offset;
484 break;
485 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
486 }
487 if (cu_->verbose) {
488 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
489 }
490 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee0d829482013-10-11 15:24:55 -0700491 const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
493 int disp = tab_rec->targets[elems]->offset - bx_offset;
494 if (cu_->verbose) {
495 LOG(INFO) << " Case[" << elems << "] key: 0x"
496 << std::hex << keys[elems] << ", disp: 0x"
497 << std::hex << disp;
498 }
499 PushWord(code_buffer_, keys[elems]);
500 PushWord(code_buffer_,
501 tab_rec->targets[elems]->offset - bx_offset);
502 }
503 } else {
504 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
505 static_cast<int>(Instruction::kPackedSwitchSignature));
506 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
507 int disp = tab_rec->targets[elems]->offset - bx_offset;
508 if (cu_->verbose) {
509 LOG(INFO) << " Case[" << elems << "] disp: 0x"
510 << std::hex << disp;
511 }
512 PushWord(code_buffer_, tab_rec->targets[elems]->offset - bx_offset);
513 }
514 }
515 }
516}
517
518/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700519void Mir2Lir::InstallFillArrayData() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
521 while (true) {
522 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
523 if (tab_rec == NULL) break;
524 AlignBuffer(code_buffer_, tab_rec->offset);
525 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700526 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
527 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 }
529 }
530}
531
buzbee0d829482013-10-11 15:24:55 -0700532static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700533 for (; lir != NULL; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 lir->offset = offset;
535 offset += 4;
536 }
537 return offset;
538}
539
buzbee0d829482013-10-11 15:24:55 -0700540static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset) {
541 unsigned int element_size = sizeof(void*);
542 // Align to natural pointer size.
543 offset = (offset + (element_size - 1)) & ~(element_size - 1);
544 for (; lir != NULL; lir = lir->next) {
545 lir->offset = offset;
546 offset += element_size;
547 }
548 return offset;
549}
550
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700552bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000553 MappingTable table(&encoded_mapping_table_[0]);
554 std::vector<uint32_t> dex_pcs;
555 dex_pcs.reserve(table.DexToPcSize());
556 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
557 dex_pcs.push_back(it.DexPc());
558 }
559 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
560 std::sort(dex_pcs.begin(), dex_pcs.end());
561
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000563 auto it = dex_pcs.begin(), end = dex_pcs.end();
564 for (uint32_t dex_pc : mir_graph_->catches_) {
565 while (it != end && *it < dex_pc) {
566 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
567 ++it;
568 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000570 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
572 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000573 } else {
574 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 }
576 }
577 if (!success) {
578 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
579 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000580 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 }
582 return success;
583}
584
585
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700586void Mir2Lir::CreateMappingTables() {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000587 uint32_t pc2dex_data_size = 0u;
588 uint32_t pc2dex_entries = 0u;
589 uint32_t pc2dex_offset = 0u;
590 uint32_t pc2dex_dalvik_offset = 0u;
591 uint32_t dex2pc_data_size = 0u;
592 uint32_t dex2pc_entries = 0u;
593 uint32_t dex2pc_offset = 0u;
594 uint32_t dex2pc_dalvik_offset = 0u;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
596 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000597 pc2dex_entries += 1;
598 DCHECK(pc2dex_offset <= tgt_lir->offset);
599 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
600 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
601 static_cast<int32_t>(pc2dex_dalvik_offset));
602 pc2dex_offset = tgt_lir->offset;
603 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 }
605 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000606 dex2pc_entries += 1;
607 DCHECK(dex2pc_offset <= tgt_lir->offset);
608 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
609 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
610 static_cast<int32_t>(dex2pc_dalvik_offset));
611 dex2pc_offset = tgt_lir->offset;
612 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 }
614 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000615
616 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
617 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
618 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000619 encoded_mapping_table_.resize(data_size);
620 uint8_t* write_pos = &encoded_mapping_table_[0];
621 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
622 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
623 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
624 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000625
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000626 pc2dex_offset = 0u;
627 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000628 dex2pc_offset = 0u;
629 dex2pc_dalvik_offset = 0u;
630 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
631 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
632 DCHECK(pc2dex_offset <= tgt_lir->offset);
633 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
634 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
635 static_cast<int32_t>(pc2dex_dalvik_offset));
636 pc2dex_offset = tgt_lir->offset;
637 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
638 }
639 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
640 DCHECK(dex2pc_offset <= tgt_lir->offset);
641 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
642 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
643 static_cast<int32_t>(dex2pc_dalvik_offset));
644 dex2pc_offset = tgt_lir->offset;
645 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
646 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000647 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000648 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
649 hdr_data_size + pc2dex_data_size);
650 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000651
Ian Rogers96faf5b2013-08-09 22:05:32 -0700652 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000653 CHECK(VerifyCatchEntries());
654
Ian Rogers96faf5b2013-08-09 22:05:32 -0700655 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000656 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700657 CHECK_EQ(table.TotalSize(), total_entries);
658 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000659 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000660 auto it2 = table.DexToPcBegin();
661 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
662 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
663 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
664 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
665 ++it;
666 }
667 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
668 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
669 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
670 ++it2;
671 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700672 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000673 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000674 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700675 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676}
677
678class NativePcToReferenceMapBuilder {
679 public:
680 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
681 size_t entries, uint32_t max_native_offset,
682 size_t references_width) : entries_(entries),
683 references_width_(references_width), in_use_(entries),
684 table_(table) {
685 // Compute width in bytes needed to hold max_native_offset.
686 native_offset_width_ = 0;
687 while (max_native_offset != 0) {
688 native_offset_width_++;
689 max_native_offset >>= 8;
690 }
691 // Resize table and set up header.
692 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
693 CHECK_LT(native_offset_width_, 1U << 3);
694 (*table)[0] = native_offset_width_ & 7;
695 CHECK_LT(references_width_, 1U << 13);
696 (*table)[0] |= (references_width_ << 3) & 0xFF;
697 (*table)[1] = (references_width_ >> 5) & 0xFF;
698 CHECK_LT(entries, 1U << 16);
699 (*table)[2] = entries & 0xFF;
700 (*table)[3] = (entries >> 8) & 0xFF;
701 }
702
703 void AddEntry(uint32_t native_offset, const uint8_t* references) {
704 size_t table_index = TableIndex(native_offset);
705 while (in_use_[table_index]) {
706 table_index = (table_index + 1) % entries_;
707 }
708 in_use_[table_index] = true;
buzbee0d829482013-10-11 15:24:55 -0700709 SetCodeOffset(table_index, native_offset);
710 DCHECK_EQ(native_offset, GetCodeOffset(table_index));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 SetReferences(table_index, references);
712 }
713
714 private:
715 size_t TableIndex(uint32_t native_offset) {
716 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
717 }
718
buzbee0d829482013-10-11 15:24:55 -0700719 uint32_t GetCodeOffset(size_t table_index) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 uint32_t native_offset = 0;
721 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
722 for (size_t i = 0; i < native_offset_width_; i++) {
723 native_offset |= (*table_)[table_offset + i] << (i * 8);
724 }
725 return native_offset;
726 }
727
buzbee0d829482013-10-11 15:24:55 -0700728 void SetCodeOffset(size_t table_index, uint32_t native_offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
730 for (size_t i = 0; i < native_offset_width_; i++) {
731 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
732 }
733 }
734
735 void SetReferences(size_t table_index, const uint8_t* references) {
736 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
737 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
738 }
739
740 size_t EntryWidth() const {
741 return native_offset_width_ + references_width_;
742 }
743
744 // Number of entries in the table.
745 const size_t entries_;
746 // Number of bytes used to encode the reference bitmap.
747 const size_t references_width_;
748 // Number of bytes used to encode a native offset.
749 size_t native_offset_width_;
750 // Entries that are in use.
751 std::vector<bool> in_use_;
752 // The table we're building.
753 std::vector<uint8_t>* const table_;
754};
755
756void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000757 DCHECK(!encoded_mapping_table_.empty());
758 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000760 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
761 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 if (native_offset > max_native_offset) {
763 max_native_offset = native_offset;
764 }
765 }
766 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000767 const std::vector<uint8_t>& gc_map_raw =
768 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
769 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
770 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 // Compute native offset to references size.
772 NativePcToReferenceMapBuilder native_gc_map_builder(&native_gc_map_,
Vladimir Marko06606b92013-12-02 15:31:08 +0000773 mapping_table.PcToDexSize(),
774 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775
Vladimir Marko06606b92013-12-02 15:31:08 +0000776 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
777 uint32_t native_offset = it.NativePcOffset();
778 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
780 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
781 native_gc_map_builder.AddEntry(native_offset, references);
782 }
783}
784
785/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700786int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 offset = AssignLiteralOffsetCommon(literal_list_, offset);
buzbee0d829482013-10-11 15:24:55 -0700788 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset);
789 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800790 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 return offset;
792}
793
buzbee0d829482013-10-11 15:24:55 -0700794int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
796 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700797 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 if (tab_rec == NULL) break;
799 tab_rec->offset = offset;
800 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
801 offset += tab_rec->table[1] * (sizeof(int) * 2);
802 } else {
803 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
804 static_cast<int>(Instruction::kPackedSwitchSignature));
805 offset += tab_rec->table[1] * sizeof(int);
806 }
807 }
808 return offset;
809}
810
buzbee0d829482013-10-11 15:24:55 -0700811int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
813 while (true) {
814 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
815 if (tab_rec == NULL) break;
816 tab_rec->offset = offset;
817 offset += tab_rec->size;
818 // word align
819 offset = (offset + 3) & ~3;
820 }
821 return offset;
822}
823
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824/*
825 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700826 * offset vaddr if pretty-printing, otherise use the standard block
827 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700828 * branch table during the assembly phase. All resource flags
829 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 */
buzbee0d829482013-10-11 15:24:55 -0700831LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) {
buzbee252254b2013-09-08 16:20:53 -0700832 LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id];
buzbeeb48819d2013-09-14 16:15:25 -0700833 LIR* res = boundary_lir;
834 if (cu_->verbose) {
835 // Only pay the expense if we're pretty-printing.
836 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocLIR));
837 new_label->dalvik_offset = vaddr;
838 new_label->opcode = kPseudoCaseLabel;
839 new_label->operands[0] = keyVal;
840 new_label->flags.fixup = kFixupLabel;
841 DCHECK(!new_label->flags.use_def_invalid);
842 new_label->u.m.def_mask = ENCODE_ALL;
843 InsertLIRAfter(boundary_lir, new_label);
844 res = new_label;
845 }
846 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847}
848
buzbee0d829482013-10-11 15:24:55 -0700849void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700851 DexOffset base_vaddr = tab_rec->vaddr;
852 const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 int entries = table[1];
854 int low_key = s4FromSwitchData(&table[2]);
855 for (int i = 0; i < entries; i++) {
856 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key);
857 }
858}
859
buzbee0d829482013-10-11 15:24:55 -0700860void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700862 DexOffset base_vaddr = tab_rec->vaddr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700864 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
865 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 for (int i = 0; i < entries; i++) {
867 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]);
868 }
869}
870
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700871void Mir2Lir::ProcessSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
873 while (true) {
874 Mir2Lir::SwitchTable *tab_rec = iterator.Next();
875 if (tab_rec == NULL) break;
876 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
877 MarkPackedCaseLabels(tab_rec);
878 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
879 MarkSparseCaseLabels(tab_rec);
880 } else {
881 LOG(FATAL) << "Invalid switch table";
882 }
883 }
884}
885
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700886void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887 /*
888 * Sparse switch data format:
889 * ushort ident = 0x0200 magic value
890 * ushort size number of entries in the table; > 0
891 * int keys[size] keys, sorted low-to-high; 32-bit aligned
892 * int targets[size] branch targets, relative to switch opcode
893 *
894 * Total size is (2+size*4) 16-bit code units.
895 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 uint16_t ident = table[0];
897 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700898 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
899 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
901 << ", entries: " << std::dec << entries;
902 for (int i = 0; i < entries; i++) {
903 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
904 }
905}
906
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700907void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 /*
909 * Packed switch data format:
910 * ushort ident = 0x0100 magic value
911 * ushort size number of entries in the table
912 * int first_key first (and lowest) switch case value
913 * int targets[size] branch targets, relative to switch opcode
914 *
915 * Total size is (4+size*2) 16-bit code units.
916 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700917 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700918 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 int entries = table[1];
920 int low_key = s4FromSwitchData(&table[2]);
921 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
922 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
923 for (int i = 0; i < entries; i++) {
924 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
925 << targets[i];
926 }
927}
928
buzbee252254b2013-09-08 16:20:53 -0700929/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700930void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
931 // NOTE: only used for debug listings.
932 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933}
934
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700935bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700936 bool is_taken;
937 switch (opcode) {
938 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
939 case Instruction::IF_NE: is_taken = (src1 != src2); break;
940 case Instruction::IF_LT: is_taken = (src1 < src2); break;
941 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
942 case Instruction::IF_GT: is_taken = (src1 > src2); break;
943 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
944 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
945 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
946 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
947 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
948 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
949 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
950 default:
951 LOG(FATAL) << "Unexpected opcode " << opcode;
952 is_taken = false;
953 }
954 return is_taken;
955}
956
957// Convert relation of src1/src2 to src2/src1
958ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
959 ConditionCode res;
960 switch (before) {
961 case kCondEq: res = kCondEq; break;
962 case kCondNe: res = kCondNe; break;
963 case kCondLt: res = kCondGt; break;
964 case kCondGt: res = kCondLt; break;
965 case kCondLe: res = kCondGe; break;
966 case kCondGe: res = kCondLe; break;
967 default:
968 res = static_cast<ConditionCode>(0);
969 LOG(FATAL) << "Unexpected ccode " << before;
970 }
971 return res;
972}
973
974// TODO: move to mir_to_lir.cc
975Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
976 : Backend(arena),
977 literal_list_(NULL),
978 method_literal_list_(NULL),
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800979 class_literal_list_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980 code_literal_list_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -0700981 first_fixup_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 cu_(cu),
983 mir_graph_(mir_graph),
984 switch_tables_(arena, 4, kGrowableArraySwitchTables),
985 fill_array_data_(arena, 4, kGrowableArrayFillArrayData),
986 throw_launchpads_(arena, 2048, kGrowableArrayThrowLaunchPads),
987 suspend_launchpads_(arena, 4, kGrowableArraySuspendLaunchPads),
988 intrinsic_launchpads_(arena, 2048, kGrowableArrayMisc),
buzbeebd663de2013-09-10 15:41:31 -0700989 tempreg_info_(arena, 20, kGrowableArrayMisc),
990 reginfo_map_(arena, 64, kGrowableArrayMisc),
buzbee0d829482013-10-11 15:24:55 -0700991 pointer_storage_(arena, 128, kGrowableArrayMisc),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 data_offset_(0),
993 total_size_(0),
994 block_label_list_(NULL),
buzbeed69835d2014-02-03 14:40:27 -0800995 promotion_map_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 current_dalvik_offset_(0),
buzbeeb48819d2013-09-14 16:15:25 -0700997 estimated_native_code_size_(0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 reg_pool_(NULL),
999 live_sreg_(0),
1000 num_core_spills_(0),
1001 num_fp_spills_(0),
1002 frame_size_(0),
1003 core_spill_mask_(0),
1004 fp_spill_mask_(0),
1005 first_lir_insn_(NULL),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001006 last_lir_insn_(NULL),
1007 slow_paths_(arena, 32, kGrowableArraySlowPaths) {
buzbee0d829482013-10-11 15:24:55 -07001008 // Reserve pointer id 0 for NULL.
1009 size_t null_idx = WrapPointer(NULL);
1010 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011}
1012
1013void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001014 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1016
1017 /* Allocate Registers using simple local allocation scheme */
1018 SimpleRegAlloc();
1019
Vladimir Marko5816ed42013-11-27 17:04:20 +00001020 /*
1021 * Custom codegen for special cases. If for any reason the
Vladimir Marko51154732014-01-02 09:44:23 +00001022 * special codegen doesn't succeed, first_lir_insn_ will be
Vladimir Marko5816ed42013-11-27 17:04:20 +00001023 * set to NULL;
1024 */
1025 // TODO: Clean up GenSpecial() and return true only if special implementation is emitted.
1026 // Currently, GenSpecial() returns IsSpecial() but doesn't check after SpecialMIR2LIR().
1027 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1028 cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1029 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030
1031 /* Convert MIR to LIR, etc. */
1032 if (first_lir_insn_ == NULL) {
1033 MethodMIR2LIR();
1034 }
1035
1036 /* Method is not empty */
1037 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 // mark the targets of switch statement case labels
1039 ProcessSwitchTables();
1040
1041 /* Convert LIR into machine code. */
1042 AssembleLIR();
1043
1044 if (cu_->verbose) {
1045 CodegenDump();
1046 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048}
1049
1050CompiledMethod* Mir2Lir::GetCompiledMethod() {
1051 // Combine vmap tables - core regs, then fp regs - into vmap_table
Ian Rogers96faf5b2013-08-09 22:05:32 -07001052 std::vector<uint16_t> raw_vmap_table;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 // Core regs may have been inserted out of order - sort first
1054 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
Mathieu Chartier193bad92013-08-29 18:46:00 -07001055 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 // Copy, stripping out the phys register sort key
Ian Rogers96faf5b2013-08-09 22:05:32 -07001057 raw_vmap_table.push_back(~(-1 << VREG_NUM_WIDTH) & core_vmap_table_[i]);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 }
1059 // If we have a frame, push a marker to take place of lr
1060 if (frame_size_ > 0) {
Ian Rogers96faf5b2013-08-09 22:05:32 -07001061 raw_vmap_table.push_back(INVALID_VREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 } else {
1063 DCHECK_EQ(__builtin_popcount(core_spill_mask_), 0);
1064 DCHECK_EQ(__builtin_popcount(fp_spill_mask_), 0);
1065 }
1066 // Combine vmap tables - core regs, then fp regs. fp regs already sorted
1067 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
Ian Rogers96faf5b2013-08-09 22:05:32 -07001068 raw_vmap_table.push_back(fp_vmap_table_[i]);
1069 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +00001070 Leb128EncodingVector vmap_encoder;
Ian Rogers96faf5b2013-08-09 22:05:32 -07001071 // Prefix the encoded data with its size.
Vladimir Marko1e6cb632013-11-28 16:27:29 +00001072 vmap_encoder.PushBackUnsigned(raw_vmap_table.size());
Mathieu Chartier193bad92013-08-29 18:46:00 -07001073 for (uint16_t cur : raw_vmap_table) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +00001074 vmap_encoder.PushBackUnsigned(cur);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 }
1076 CompiledMethod* result =
Mathieu Chartier193bad92013-08-29 18:46:00 -07001077 new CompiledMethod(*cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_,
Vladimir Marko06606b92013-12-02 15:31:08 +00001078 core_spill_mask_, fp_spill_mask_, encoded_mapping_table_,
Mathieu Chartier193bad92013-08-29 18:46:00 -07001079 vmap_encoder.GetData(), native_gc_map_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 return result;
1081}
1082
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001083size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1084 // Chose a reasonably small value in order to contain stack growth.
1085 // Backends that are smarter about spill region can return larger values.
1086 const size_t max_compiler_temps = 10;
1087 return max_compiler_temps;
1088}
1089
1090size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1091 // By default assume that the Mir2Lir will need one slot for each temporary.
1092 // If the backend can better determine temps that have non-overlapping ranges and
1093 // temps that do not need spilled, it can actually provide a small region.
1094 return (mir_graph_->GetNumUsedCompilerTemps() * sizeof(uint32_t));
1095}
1096
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097int Mir2Lir::ComputeFrameSize() {
1098 /* Figure out the frame size */
1099 static const uint32_t kAlignMask = kStackAlignment - 1;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001100 uint32_t size = ((num_core_spills_ + num_fp_spills_ +
1101 1 /* filler word */ + cu_->num_regs + cu_->num_outs)
1102 * sizeof(uint32_t)) +
1103 GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 /* Align and set */
1105 return (size + kAlignMask) & ~(kAlignMask);
1106}
1107
1108/*
1109 * Append an LIR instruction to the LIR list maintained by a compilation
1110 * unit
1111 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001112void Mir2Lir::AppendLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 if (first_lir_insn_ == NULL) {
1114 DCHECK(last_lir_insn_ == NULL);
1115 last_lir_insn_ = first_lir_insn_ = lir;
1116 lir->prev = lir->next = NULL;
1117 } else {
1118 last_lir_insn_->next = lir;
1119 lir->prev = last_lir_insn_;
1120 lir->next = NULL;
1121 last_lir_insn_ = lir;
1122 }
1123}
1124
1125/*
1126 * Insert an LIR instruction before the current instruction, which cannot be the
1127 * first instruction.
1128 *
1129 * prev_lir <-> new_lir <-> current_lir
1130 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001131void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 DCHECK(current_lir->prev != NULL);
1133 LIR *prev_lir = current_lir->prev;
1134
1135 prev_lir->next = new_lir;
1136 new_lir->prev = prev_lir;
1137 new_lir->next = current_lir;
1138 current_lir->prev = new_lir;
1139}
1140
1141/*
1142 * Insert an LIR instruction after the current instruction, which cannot be the
1143 * first instruction.
1144 *
1145 * current_lir -> new_lir -> old_next
1146 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001147void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 new_lir->prev = current_lir;
1149 new_lir->next = current_lir->next;
1150 current_lir->next = new_lir;
1151 new_lir->next->prev = new_lir;
1152}
1153
Mark Mendell4708dcd2014-01-22 09:05:18 -08001154bool Mir2Lir::IsPowerOfTwo(uint64_t x) {
1155 return (x & (x - 1)) == 0;
1156}
1157
1158// Returns the index of the lowest set bit in 'x'.
1159int32_t Mir2Lir::LowestSetBit(uint64_t x) {
1160 int bit_posn = 0;
1161 while ((x & 0xf) == 0) {
1162 bit_posn += 4;
1163 x >>= 4;
1164 }
1165 while ((x & 1) == 0) {
1166 bit_posn++;
1167 x >>= 1;
1168 }
1169 return bit_posn;
1170}
1171
1172bool Mir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
1173 DCHECK(rl_src.wide);
1174 DCHECK(rl_dest.wide);
1175 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1176}
1177
Mark Mendell766e9292014-01-27 07:55:47 -08001178LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, int temp_reg, int base_reg,
1179 int offset, int check_value, LIR* target) {
1180 // Handle this for architectures that can't compare to memory.
1181 LoadWordDisp(base_reg, offset, temp_reg);
1182 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1183 return branch;
1184}
1185
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001186void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
1187 slow_paths_.Insert(slowpath);
1188}
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001189} // namespace art