blob: ebebe70462742f7dc0fad9f7e3085d3bca59f85c [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "dex/compiler_internals.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070018#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "dex_file-inl.h"
20#include "gc_map.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000021#include "gc_map_builder.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070022#include "mapping_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "mir_to_lir-inl.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000024#include "dex/quick/dex_file_method_inliner.h"
25#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000026#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000027#include "dex/verified_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "verifier/dex_gc_map.h"
29#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000030#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031
32namespace art {
33
Vladimir Marko06606b92013-12-02 15:31:08 +000034namespace {
35
36/* Dump a mapping table */
37template <typename It>
38void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
39 const Signature& signature, uint32_t size, It first) {
40 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080041 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000042 descriptor, name, signature.ToString().c_str(), size));
43 std::replace(line.begin(), line.end(), ';', '_');
44 LOG(INFO) << line;
45 for (uint32_t i = 0; i != size; ++i) {
46 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
47 ++first;
48 LOG(INFO) << line;
49 }
50 LOG(INFO) <<" };\n\n";
51 }
52}
53
54} // anonymous namespace
55
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070056bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 bool res = false;
58 if (rl_src.is_const) {
59 if (rl_src.wide) {
60 if (rl_src.fp) {
61 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
62 } else {
63 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
64 }
65 } else {
66 if (rl_src.fp) {
67 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
68 } else {
69 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
70 }
71 }
72 }
73 return res;
74}
75
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070076void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070077 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010078 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010080 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -070081}
82
Andreas Gampe3c12c512014-06-24 18:46:29 +000083void Mir2Lir::MarkSafepointPCAfter(LIR* after) {
84 DCHECK(!after->flags.use_def_invalid);
85 after->u.m.def_mask = &kEncodeAll;
86 // As NewLIR0 uses Append, we need to create the LIR by hand.
87 LIR* safepoint_pc = RawLIR(current_dalvik_offset_, kPseudoSafepointPC);
88 if (after->next == nullptr) {
89 DCHECK_EQ(after, last_lir_insn_);
90 AppendLIR(safepoint_pc);
91 } else {
92 InsertLIRAfter(after, safepoint_pc);
93 }
94 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
95}
96
buzbee252254b2013-09-08 16:20:53 -070097/* Remove a LIR from the list. */
98void Mir2Lir::UnlinkLIR(LIR* lir) {
99 if (UNLIKELY(lir == first_lir_insn_)) {
100 first_lir_insn_ = lir->next;
101 if (lir->next != NULL) {
102 lir->next->prev = NULL;
103 } else {
104 DCHECK(lir->next == NULL);
105 DCHECK(lir == last_lir_insn_);
106 last_lir_insn_ = NULL;
107 }
108 } else if (lir == last_lir_insn_) {
109 last_lir_insn_ = lir->prev;
110 lir->prev->next = NULL;
111 } else if ((lir->prev != NULL) && (lir->next != NULL)) {
112 lir->prev->next = lir->next;
113 lir->next->prev = lir->prev;
114 }
115}
116
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700118void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700120 if (!cu_->verbose) {
121 UnlinkLIR(lir);
122 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123}
124
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700125void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700127 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100128 // TODO: Avoid the extra Arena allocation!
129 const ResourceMask** mask_ptr;
130 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700132 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700134 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100136 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100138 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 /* ..and then add back the one we need */
140 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100141 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100143 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100145 case ResourceMask::kDalvikReg:
146 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100148 case ResourceMask::kHeapRef:
149 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100151 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 /* Currently only loads can be marked as kMustNotAlias */
153 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100154 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 break;
156 default:
157 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
158 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100159 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160}
161
162/*
163 * Mark load/store instructions that access Dalvik registers through the stack.
164 */
165void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700166 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100167 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
168 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169
170 /*
171 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
172 * access.
173 */
buzbeeb48819d2013-09-14 16:15:25 -0700174 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175}
176
177/*
178 * Debugging macros
179 */
180#define DUMP_RESOURCE_MASK(X)
181
182/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700183void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 int offset = lir->offset;
185 int dest = lir->operands[0];
186 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
187
188 /* Handle pseudo-ops individually, and all regular insns as a group */
189 switch (lir->opcode) {
190 case kPseudoMethodEntry:
191 LOG(INFO) << "-------- method entry "
192 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
193 break;
194 case kPseudoMethodExit:
195 LOG(INFO) << "-------- Method_Exit";
196 break;
197 case kPseudoBarrier:
198 LOG(INFO) << "-------- BARRIER";
199 break;
200 case kPseudoEntryBlock:
201 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
202 break;
203 case kPseudoDalvikByteCodeBoundary:
204 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700205 // NOTE: only used for debug listings.
206 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 }
208 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000209 << lir->dalvik_offset << " @ "
210 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 break;
212 case kPseudoExitBlock:
213 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
214 break;
215 case kPseudoPseudoAlign4:
216 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
217 << offset << "): .align4";
218 break;
219 case kPseudoEHBlockLabel:
220 LOG(INFO) << "Exception_Handling:";
221 break;
222 case kPseudoTargetLabel:
223 case kPseudoNormalBlockLabel:
224 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
225 break;
226 case kPseudoThrowTarget:
227 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
228 break;
229 case kPseudoIntrinsicRetry:
230 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
231 break;
232 case kPseudoSuspendTarget:
233 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
234 break;
235 case kPseudoSafepointPC:
236 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
237 break;
238 case kPseudoExportedPC:
239 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
240 break;
241 case kPseudoCaseLabel:
242 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
243 << std::hex << lir->operands[0] << "|" << std::dec <<
244 lir->operands[0];
245 break;
246 default:
247 if (lir->flags.is_nop && !dump_nop) {
248 break;
249 } else {
250 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
251 lir, base_addr));
252 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
253 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800254 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
255 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 op_name.c_str(), op_operands.c_str(),
257 lir->flags.is_nop ? "(nop)" : "");
258 }
259 break;
260 }
261
buzbeeb48819d2013-09-14 16:15:25 -0700262 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100263 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 }
buzbeeb48819d2013-09-14 16:15:25 -0700265 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100266 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 }
268}
269
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700270void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800271 int num_regs = cu_->num_dalvik_registers + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 for (int i = 0; i < num_regs; i++) {
273 PromotionMap v_reg_map = promotion_map_[i];
274 std::string buf;
275 if (v_reg_map.fp_location == kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700276 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 }
278
279 std::string buf3;
280 if (i < cu_->num_dalvik_registers) {
281 StringAppendF(&buf3, "%02d", i);
282 } else if (i == mir_graph_->GetMethodSReg()) {
283 buf3 = "Method*";
284 } else {
285 StringAppendF(&buf3, "ct%d", i - cu_->num_dalvik_registers);
286 }
287
288 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
289 v_reg_map.core_location == kLocPhysReg ?
290 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
291 v_reg_map.core_reg : SRegOffset(i),
292 buf.c_str());
293 }
294}
295
buzbee7a11ab02014-04-28 20:02:38 -0700296void Mir2Lir::UpdateLIROffsets() {
297 // Only used for code listings.
298 size_t offset = 0;
299 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
300 lir->offset = offset;
301 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
302 offset += GetInsnSize(lir);
303 } else if (lir->opcode == kPseudoPseudoAlign4) {
304 offset += (offset & 0x2);
305 }
306 }
307}
308
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700310void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 LOG(INFO) << "Dumping LIR insns for "
312 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
313 LIR* lir_insn;
314 int insns_size = cu_->code_item->insns_size_in_code_units_;
315
316 LOG(INFO) << "Regs (excluding ins) : " << cu_->num_regs;
317 LOG(INFO) << "Ins : " << cu_->num_ins;
318 LOG(INFO) << "Outs : " << cu_->num_outs;
319 LOG(INFO) << "CoreSpills : " << num_core_spills_;
320 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800321 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 LOG(INFO) << "Frame size : " << frame_size_;
323 LOG(INFO) << "code size is " << total_size_ <<
324 " bytes, Dalvik size is " << insns_size * 2;
325 LOG(INFO) << "expansion factor: "
326 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
327 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700328 UpdateLIROffsets();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329 for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) {
330 DumpLIRInsn(lir_insn, 0);
331 }
332 for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) {
333 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
334 lir_insn->operands[0]);
335 }
336
337 const DexFile::MethodId& method_id =
338 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700339 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
340 const char* name = cu_->dex_file->GetMethodName(method_id);
341 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342
343 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000344 if (!encoded_mapping_table_.empty()) {
345 MappingTable table(&encoded_mapping_table_[0]);
346 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
347 table.PcToDexSize(), table.PcToDexBegin());
348 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
349 table.DexToPcSize(), table.DexToPcBegin());
350 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351}
352
353/*
354 * Search the existing constants in the literal pool for an exact or close match
355 * within specified delta (greater or equal to 0).
356 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700357LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 while (data_target) {
359 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
360 return data_target;
361 data_target = data_target->next;
362 }
363 return NULL;
364}
365
366/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700367LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 bool lo_match = false;
369 LIR* lo_target = NULL;
370 while (data_target) {
371 if (lo_match && (data_target->operands[0] == val_hi)) {
372 // Record high word in case we need to expand this later.
373 lo_target->operands[1] = val_hi;
374 return lo_target;
375 }
376 lo_match = false;
377 if (data_target->operands[0] == val_lo) {
378 lo_match = true;
379 lo_target = data_target;
380 }
381 data_target = data_target->next;
382 }
383 return NULL;
384}
385
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100386/* Search the existing constants in the literal pool for an exact method match */
387LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
388 while (data_target) {
389 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
390 UnwrapPointer(data_target->operands[1]) == method.dex_file) {
391 return data_target;
392 }
393 data_target = data_target->next;
394 }
395 return nullptr;
396}
397
Fred Shihe7f82e22014-08-06 10:46:37 -0700398/* Search the existing constants in the literal pool for an exact class match */
399LIR* Mir2Lir::ScanLiteralPoolClass(LIR* data_target, const DexFile& dex_file, uint32_t type_idx) {
400 while (data_target) {
401 if (static_cast<uint32_t>(data_target->operands[0]) == type_idx &&
402 UnwrapPointer(data_target->operands[1]) == &dex_file) {
403 return data_target;
404 }
405 data_target = data_target->next;
406 }
407 return nullptr;
408}
409
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410/*
411 * The following are building blocks to insert constants into the pool or
412 * instruction streams.
413 */
414
415/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700416LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 /* Add the constant to the literal pool */
418 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000419 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 new_value->operands[0] = value;
421 new_value->next = *constant_list_p;
422 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700423 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 return new_value;
425 }
426 return NULL;
427}
428
429/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700430LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 AddWordData(constant_list_p, val_hi);
432 return AddWordData(constant_list_p, val_lo);
433}
434
Andreas Gampe2da88232014-02-27 12:26:20 -0800435static void Push32(std::vector<uint8_t>&buf, int data) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700436 buf.push_back(data & 0xff);
437 buf.push_back((data >> 8) & 0xff);
438 buf.push_back((data >> 16) & 0xff);
439 buf.push_back((data >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440}
441
Andreas Gampe2da88232014-02-27 12:26:20 -0800442// Push 8 bytes on 64-bit target systems; 4 on 32-bit target systems.
443static void PushPointer(std::vector<uint8_t>&buf, const void* pointer, bool target64) {
444 uint64_t data = reinterpret_cast<uintptr_t>(pointer);
445 if (target64) {
446 Push32(buf, data & 0xFFFFFFFF);
447 Push32(buf, (data >> 32) & 0xFFFFFFFF);
buzbee0d829482013-10-11 15:24:55 -0700448 } else {
Andreas Gampe2da88232014-02-27 12:26:20 -0800449 Push32(buf, static_cast<uint32_t>(data));
buzbee0d829482013-10-11 15:24:55 -0700450 }
451}
452
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
454 while (buf.size() < offset) {
455 buf.push_back(0);
456 }
457}
458
459/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700460void Mir2Lir::InstallLiteralPools() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 AlignBuffer(code_buffer_, data_offset_);
462 LIR* data_lir = literal_list_;
463 while (data_lir != NULL) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800464 Push32(code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 data_lir = NEXT_LIR(data_lir);
466 }
467 // Push code and method literals, record offsets for the compiler to patch.
468 data_lir = code_literal_list_;
469 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700470 uint32_t target_method_idx = data_lir->operands[0];
471 const DexFile* target_dex_file =
472 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 cu_->compiler_driver->AddCodePatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700474 cu_->class_def_idx,
475 cu_->method_idx,
476 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700477 target_method_idx,
478 target_dex_file,
479 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700480 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700481 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700482 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700483 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 data_lir = NEXT_LIR(data_lir);
485 }
486 data_lir = method_literal_list_;
487 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700488 uint32_t target_method_idx = data_lir->operands[0];
489 const DexFile* target_dex_file =
490 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 cu_->compiler_driver->AddMethodPatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700492 cu_->class_def_idx,
493 cu_->method_idx,
494 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700495 target_method_idx,
496 target_dex_file,
497 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700498 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700499 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700500 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700501 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 data_lir = NEXT_LIR(data_lir);
503 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800504 // Push class literals.
505 data_lir = class_literal_list_;
506 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700507 uint32_t target_method_idx = data_lir->operands[0];
Fred Shihe7f82e22014-08-06 10:46:37 -0700508 const DexFile* class_dex_file =
509 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800510 cu_->compiler_driver->AddClassPatch(cu_->dex_file,
511 cu_->class_def_idx,
512 cu_->method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -0700513 target_method_idx,
Fred Shihe7f82e22014-08-06 10:46:37 -0700514 class_dex_file,
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800515 code_buffer_.size());
Fred Shih4fc78532014-08-06 16:44:22 -0700516 const DexFile::TypeId& target_method_id = class_dex_file->GetTypeId(target_method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800517 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700518 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800519 data_lir = NEXT_LIR(data_lir);
520 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521}
522
523/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700524void Mir2Lir::InstallSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
526 while (true) {
527 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
528 if (tab_rec == NULL) break;
529 AlignBuffer(code_buffer_, tab_rec->offset);
530 /*
531 * For Arm, our reference point is the address of the bx
532 * instruction that does the launch, so we have to subtract
533 * the auto pc-advance. For other targets the reference point
534 * is a label, so we can use the offset as-is.
535 */
536 int bx_offset = INVALID_OFFSET;
537 switch (cu_->instruction_set) {
538 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700539 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 bx_offset = tab_rec->anchor->offset + 4;
541 break;
542 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700543 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 bx_offset = 0;
545 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100546 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 case kMips:
548 bx_offset = tab_rec->anchor->offset;
549 break;
550 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
551 }
552 if (cu_->verbose) {
553 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
554 }
555 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee0d829482013-10-11 15:24:55 -0700556 const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
558 int disp = tab_rec->targets[elems]->offset - bx_offset;
559 if (cu_->verbose) {
560 LOG(INFO) << " Case[" << elems << "] key: 0x"
561 << std::hex << keys[elems] << ", disp: 0x"
562 << std::hex << disp;
563 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800564 Push32(code_buffer_, keys[elems]);
565 Push32(code_buffer_,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 tab_rec->targets[elems]->offset - bx_offset);
567 }
568 } else {
569 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
570 static_cast<int>(Instruction::kPackedSwitchSignature));
571 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
572 int disp = tab_rec->targets[elems]->offset - bx_offset;
573 if (cu_->verbose) {
574 LOG(INFO) << " Case[" << elems << "] disp: 0x"
575 << std::hex << disp;
576 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800577 Push32(code_buffer_, tab_rec->targets[elems]->offset - bx_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 }
579 }
580 }
581}
582
583/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700584void Mir2Lir::InstallFillArrayData() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
586 while (true) {
587 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
588 if (tab_rec == NULL) break;
589 AlignBuffer(code_buffer_, tab_rec->offset);
590 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700591 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
592 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 }
594 }
595}
596
buzbee0d829482013-10-11 15:24:55 -0700597static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700598 for (; lir != NULL; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 lir->offset = offset;
600 offset += 4;
601 }
602 return offset;
603}
604
Ian Rogersff093b32014-04-30 19:04:27 -0700605static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
606 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700607 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700608 offset = RoundUp(offset, element_size);
buzbee0d829482013-10-11 15:24:55 -0700609 for (; lir != NULL; lir = lir->next) {
610 lir->offset = offset;
611 offset += element_size;
612 }
613 return offset;
614}
615
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700617bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000618 MappingTable table(&encoded_mapping_table_[0]);
619 std::vector<uint32_t> dex_pcs;
620 dex_pcs.reserve(table.DexToPcSize());
621 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
622 dex_pcs.push_back(it.DexPc());
623 }
624 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
625 std::sort(dex_pcs.begin(), dex_pcs.end());
626
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000628 auto it = dex_pcs.begin(), end = dex_pcs.end();
629 for (uint32_t dex_pc : mir_graph_->catches_) {
630 while (it != end && *it < dex_pc) {
631 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
632 ++it;
633 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000635 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
637 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000638 } else {
639 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 }
641 }
642 if (!success) {
643 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
644 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000645 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 }
647 return success;
648}
649
650
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700651void Mir2Lir::CreateMappingTables() {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700652 bool generate_src_map = cu_->compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
653
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000654 uint32_t pc2dex_data_size = 0u;
655 uint32_t pc2dex_entries = 0u;
656 uint32_t pc2dex_offset = 0u;
657 uint32_t pc2dex_dalvik_offset = 0u;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700658 uint32_t pc2dex_src_entries = 0u;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000659 uint32_t dex2pc_data_size = 0u;
660 uint32_t dex2pc_entries = 0u;
661 uint32_t dex2pc_offset = 0u;
662 uint32_t dex2pc_dalvik_offset = 0u;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700664 pc2dex_src_entries++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000666 pc2dex_entries += 1;
667 DCHECK(pc2dex_offset <= tgt_lir->offset);
668 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
669 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
670 static_cast<int32_t>(pc2dex_dalvik_offset));
671 pc2dex_offset = tgt_lir->offset;
672 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 }
674 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000675 dex2pc_entries += 1;
676 DCHECK(dex2pc_offset <= tgt_lir->offset);
677 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
678 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
679 static_cast<int32_t>(dex2pc_dalvik_offset));
680 dex2pc_offset = tgt_lir->offset;
681 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 }
683 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000684
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700685 if (generate_src_map) {
686 src_mapping_table_.reserve(pc2dex_src_entries);
687 }
688
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000689 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
690 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
691 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000692 encoded_mapping_table_.resize(data_size);
693 uint8_t* write_pos = &encoded_mapping_table_[0];
694 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
695 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
696 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
697 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000698
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000699 pc2dex_offset = 0u;
700 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000701 dex2pc_offset = 0u;
702 dex2pc_dalvik_offset = 0u;
703 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700704 if (generate_src_map && !tgt_lir->flags.is_nop) {
705 src_mapping_table_.push_back(SrcMapElem({tgt_lir->offset,
706 static_cast<int32_t>(tgt_lir->dalvik_offset)}));
707 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000708 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
709 DCHECK(pc2dex_offset <= tgt_lir->offset);
710 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
711 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
712 static_cast<int32_t>(pc2dex_dalvik_offset));
713 pc2dex_offset = tgt_lir->offset;
714 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
715 }
716 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
717 DCHECK(dex2pc_offset <= tgt_lir->offset);
718 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
719 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
720 static_cast<int32_t>(dex2pc_dalvik_offset));
721 dex2pc_offset = tgt_lir->offset;
722 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
723 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000724 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000725 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
726 hdr_data_size + pc2dex_data_size);
727 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000728
Ian Rogers96faf5b2013-08-09 22:05:32 -0700729 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000730 CHECK(VerifyCatchEntries());
731
Ian Rogers96faf5b2013-08-09 22:05:32 -0700732 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000733 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700734 CHECK_EQ(table.TotalSize(), total_entries);
735 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000736 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000737 auto it2 = table.DexToPcBegin();
738 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
739 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
740 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
741 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
742 ++it;
743 }
744 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
745 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
746 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
747 ++it2;
748 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700749 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000750 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000751 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700752 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753}
754
Brian Carlstrom7940e442013-07-12 13:46:57 -0700755void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000756 DCHECK(!encoded_mapping_table_.empty());
757 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000759 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
760 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 if (native_offset > max_native_offset) {
762 max_native_offset = native_offset;
763 }
764 }
765 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000766 const std::vector<uint8_t>& gc_map_raw =
767 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
768 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
769 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000771 GcMapBuilder native_gc_map_builder(&native_gc_map_,
772 mapping_table.PcToDexSize(),
773 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774
Vladimir Marko06606b92013-12-02 15:31:08 +0000775 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
776 uint32_t native_offset = it.NativePcOffset();
777 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Dave Allisonf9439142014-03-27 15:10:22 -0700779 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
780 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 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);
Ian Rogersff093b32014-04-30 19:04:27 -0700788 unsigned int ptr_size = GetInstructionSetPointerSize(cu_->instruction_set);
789 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
790 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
791 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
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
Andreas Gampe66018822014-05-05 20:47:19 -0700820 offset = RoundUp(offset, 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 }
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.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000837 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
buzbeeb48819d2013-09-14 16:15:25 -0700838 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);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100843 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700844 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
Vladimir Markoa1a70742014-03-03 10:28:05 +0000975ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
976 ConditionCode res;
977 switch (before) {
978 case kCondEq: res = kCondNe; break;
979 case kCondNe: res = kCondEq; break;
980 case kCondLt: res = kCondGe; break;
981 case kCondGt: res = kCondLe; break;
982 case kCondLe: res = kCondGt; break;
983 case kCondGe: res = kCondLt; break;
984 default:
985 res = static_cast<ConditionCode>(0);
986 LOG(FATAL) << "Unexpected ccode " << before;
987 }
988 return res;
989}
990
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991// TODO: move to mir_to_lir.cc
992Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
993 : Backend(arena),
994 literal_list_(NULL),
995 method_literal_list_(NULL),
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800996 class_literal_list_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997 code_literal_list_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -0700998 first_fixup_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 cu_(cu),
1000 mir_graph_(mir_graph),
1001 switch_tables_(arena, 4, kGrowableArraySwitchTables),
1002 fill_array_data_(arena, 4, kGrowableArrayFillArrayData),
buzbeebd663de2013-09-10 15:41:31 -07001003 tempreg_info_(arena, 20, kGrowableArrayMisc),
buzbee091cc402014-03-31 10:14:40 -07001004 reginfo_map_(arena, RegStorage::kMaxRegs, kGrowableArrayMisc),
buzbee0d829482013-10-11 15:24:55 -07001005 pointer_storage_(arena, 128, kGrowableArrayMisc),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 data_offset_(0),
1007 total_size_(0),
1008 block_label_list_(NULL),
buzbeed69835d2014-02-03 14:40:27 -08001009 promotion_map_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 current_dalvik_offset_(0),
buzbeeb48819d2013-09-14 16:15:25 -07001011 estimated_native_code_size_(0),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 reg_pool_(NULL),
1013 live_sreg_(0),
Vladimir Marko8081d2b2014-07-31 15:33:43 +01001014 core_vmap_table_(mir_graph->GetArena()->Adapter()),
1015 fp_vmap_table_(mir_graph->GetArena()->Adapter()),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 num_core_spills_(0),
1017 num_fp_spills_(0),
1018 frame_size_(0),
1019 core_spill_mask_(0),
1020 fp_spill_mask_(0),
1021 first_lir_insn_(NULL),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001022 last_lir_insn_(NULL),
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001023 slow_paths_(arena, 32, kGrowableArraySlowPaths),
1024 mem_ref_type_(ResourceMask::kHeapRef),
1025 mask_cache_(arena) {
buzbee0d829482013-10-11 15:24:55 -07001026 // Reserve pointer id 0 for NULL.
1027 size_t null_idx = WrapPointer(NULL);
1028 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029}
1030
1031void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -07001032 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
1034
1035 /* Allocate Registers using simple local allocation scheme */
1036 SimpleRegAlloc();
1037
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001038 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +00001039 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001040 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +00001041 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001043 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1044 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 MethodMIR2LIR();
1046 }
1047
1048 /* Method is not empty */
1049 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 // mark the targets of switch statement case labels
1051 ProcessSwitchTables();
1052
1053 /* Convert LIR into machine code. */
1054 AssembleLIR();
1055
buzbeeb01bf152014-05-13 15:59:07 -07001056 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 CodegenDump();
1058 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060}
1061
1062CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001063 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1064 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001066 // Prefix the encoded data with its size.
1067 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1068 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1069 vmap_encoder.PushBackUnsigned(size);
1070 // Core regs may have been inserted out of order - sort first.
1071 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1072 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1073 // Copy, stripping out the phys register sort key.
1074 vmap_encoder.PushBackUnsigned(
1075 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1076 }
1077 // Push a marker to take place of lr.
1078 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Serguei Katkovc3801912014-07-08 17:21:53 +07001079 if (cu_->instruction_set == kThumb2) {
1080 // fp regs already sorted.
1081 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1082 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1083 }
1084 } else {
1085 // For other platforms regs may have been inserted out of order - sort first.
1086 std::sort(fp_vmap_table_.begin(), fp_vmap_table_.end());
1087 for (size_t i = 0 ; i < fp_vmap_table_.size(); ++i) {
1088 // Copy, stripping out the phys register sort key.
1089 vmap_encoder.PushBackUnsigned(
1090 ~(-1 << VREG_NUM_WIDTH) & (fp_vmap_table_[i] + VmapTable::kEntryAdjustment));
1091 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001092 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001094 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1095 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001096 DCHECK_EQ(core_vmap_table_.size(), 0u);
1097 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1098 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001100
Tong Shen547cdfd2014-08-05 01:54:19 -07001101 std::unique_ptr<std::vector<uint8_t>> cfi_info(ReturnFrameDescriptionEntry());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 CompiledMethod* result =
Ian Rogers72d32622014-05-06 16:20:11 -07001103 new CompiledMethod(cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_,
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001104 core_spill_mask_, fp_spill_mask_, &src_mapping_table_, encoded_mapping_table_,
Dave Allisond6ed6422014-04-09 23:36:15 +00001105 vmap_encoder.GetData(), native_gc_map_, cfi_info.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 return result;
1107}
1108
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001109size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1110 // Chose a reasonably small value in order to contain stack growth.
1111 // Backends that are smarter about spill region can return larger values.
1112 const size_t max_compiler_temps = 10;
1113 return max_compiler_temps;
1114}
1115
1116size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1117 // By default assume that the Mir2Lir will need one slot for each temporary.
1118 // If the backend can better determine temps that have non-overlapping ranges and
1119 // temps that do not need spilled, it can actually provide a small region.
1120 return (mir_graph_->GetNumUsedCompilerTemps() * sizeof(uint32_t));
1121}
1122
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123int Mir2Lir::ComputeFrameSize() {
1124 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001125 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1126 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1127 + sizeof(uint32_t) // Filler.
1128 + (cu_->num_regs + cu_->num_outs) * sizeof(uint32_t)
1129 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001131 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132}
1133
1134/*
1135 * Append an LIR instruction to the LIR list maintained by a compilation
1136 * unit
1137 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001138void Mir2Lir::AppendLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 if (first_lir_insn_ == NULL) {
1140 DCHECK(last_lir_insn_ == NULL);
1141 last_lir_insn_ = first_lir_insn_ = lir;
1142 lir->prev = lir->next = NULL;
1143 } else {
1144 last_lir_insn_->next = lir;
1145 lir->prev = last_lir_insn_;
1146 lir->next = NULL;
1147 last_lir_insn_ = lir;
1148 }
1149}
1150
1151/*
1152 * Insert an LIR instruction before the current instruction, which cannot be the
1153 * first instruction.
1154 *
1155 * prev_lir <-> new_lir <-> current_lir
1156 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001157void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 DCHECK(current_lir->prev != NULL);
1159 LIR *prev_lir = current_lir->prev;
1160
1161 prev_lir->next = new_lir;
1162 new_lir->prev = prev_lir;
1163 new_lir->next = current_lir;
1164 current_lir->prev = new_lir;
1165}
1166
1167/*
1168 * Insert an LIR instruction after the current instruction, which cannot be the
Andreas Gampe3c12c512014-06-24 18:46:29 +00001169 * last instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 *
1171 * current_lir -> new_lir -> old_next
1172 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001173void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 new_lir->prev = current_lir;
1175 new_lir->next = current_lir->next;
1176 current_lir->next = new_lir;
1177 new_lir->next->prev = new_lir;
1178}
1179
Mark Mendell4708dcd2014-01-22 09:05:18 -08001180bool Mir2Lir::IsPowerOfTwo(uint64_t x) {
1181 return (x & (x - 1)) == 0;
1182}
1183
1184// Returns the index of the lowest set bit in 'x'.
1185int32_t Mir2Lir::LowestSetBit(uint64_t x) {
1186 int bit_posn = 0;
1187 while ((x & 0xf) == 0) {
1188 bit_posn += 4;
1189 x >>= 4;
1190 }
1191 while ((x & 1) == 0) {
1192 bit_posn++;
1193 x >>= 1;
1194 }
1195 return bit_posn;
1196}
1197
1198bool Mir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
1199 DCHECK(rl_src.wide);
1200 DCHECK(rl_dest.wide);
1201 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1202}
1203
buzbee2700f7e2014-03-07 09:46:20 -08001204LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +00001205 int offset, int check_value, LIR* target, LIR** compare) {
Mark Mendell766e9292014-01-27 07:55:47 -08001206 // Handle this for architectures that can't compare to memory.
Dave Allison69dfe512014-07-11 17:11:58 +00001207 LIR* inst = Load32Disp(base_reg, offset, temp_reg);
1208 if (compare != nullptr) {
1209 *compare = inst;
1210 }
Mark Mendell766e9292014-01-27 07:55:47 -08001211 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1212 return branch;
1213}
1214
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001215void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
1216 slow_paths_.Insert(slowpath);
1217}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001218
Jeff Hao49161ce2014-03-12 11:05:25 -07001219void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1220 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001221 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001222 if (data_target == NULL) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001223 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001224 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001225 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1226 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1227 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001228 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001229 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001230 // Loads a code pointer. Code from oat file can be mapped anywhere.
1231 LIR* load_pc_rel = OpPcRelLoad(TargetPtrReg(symbolic_reg), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001232 AppendLIR(load_pc_rel);
1233 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1234}
1235
Jeff Hao49161ce2014-03-12 11:05:25 -07001236void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1237 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001238 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001239 if (data_target == NULL) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001240 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001241 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001242 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1243 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1244 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001245 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001246 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001247 // Loads an ArtMethod pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001248 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001249 AppendLIR(load_pc_rel);
1250 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1251}
1252
Fred Shihe7f82e22014-08-06 10:46:37 -07001253void Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
1254 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001255 // Use the literal pool and a PC-relative load from a data word.
Fred Shihe7f82e22014-08-06 10:46:37 -07001256 LIR* data_target = ScanLiteralPoolClass(class_literal_list_, dex_file, type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001257 if (data_target == nullptr) {
1258 data_target = AddWordData(&class_literal_list_, type_idx);
Fred Shih4fc78532014-08-06 16:44:22 -07001259 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(&dex_file));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001260 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001261 // Loads a Class pointer, which is a reference as it lives in the heap.
Andreas Gampeccc60262014-07-04 18:02:38 -07001262 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg, kRef), data_target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001263 AppendLIR(load_pc_rel);
1264}
1265
Tong Shen547cdfd2014-08-05 01:54:19 -07001266std::vector<uint8_t>* Mir2Lir::ReturnFrameDescriptionEntry() {
Mark Mendellae9fd932014-02-10 16:14:35 -08001267 // Default case is to do nothing.
1268 return nullptr;
1269}
1270
buzbee2700f7e2014-03-07 09:46:20 -08001271RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001272 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001273 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001274 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001275 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1276 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1277 info_lo->SetIsWide(false);
1278 info_hi->SetIsWide(false);
1279 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001280 } else {
buzbee85089dd2014-05-25 15:10:52 -07001281 RegisterInfo* info = GetRegInfo(loc.reg);
1282 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1283 DCHECK(info_new != nullptr);
1284 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1285 info->MarkDead();
1286 info_new->MarkLive(loc.s_reg_low);
1287 }
1288 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001289 }
buzbee85089dd2014-05-25 15:10:52 -07001290 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001291 }
buzbee85089dd2014-05-25 15:10:52 -07001292 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001293 return loc;
1294}
1295
Mark Mendelld65c51a2014-04-29 16:55:20 -04001296void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1297 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
1298}
1299
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001300} // namespace art