blob: 89c642d21ebae89c99925b08e1ce1a2b4ce4e8d9 [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
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000017#include "compiler.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070018#include "compiler_internals.h"
19#include "driver/compiler_driver.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080020#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "dataflow_iterator-inl.h"
22#include "leb128.h"
23#include "mirror/object.h"
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080024#include "pass_driver.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "runtime.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "base/logging.h"
buzbeea61f4952013-08-23 14:27:06 -070027#include "base/timing_logger.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080028#include "driver/compiler_options.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000029#include "dex/quick/dex_file_to_method_inliner_map.h"
30
Brian Carlstrom7940e442013-07-12 13:46:57 -070031namespace art {
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
Ian Rogers72d32622014-05-06 16:20:11 -070033extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver* driver) {
34 CHECK(driver->GetCompilerContext() == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -070035}
36
Ian Rogers72d32622014-05-06 16:20:11 -070037extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver* driver) {
38 CHECK(driver->GetCompilerContext() == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -070039}
40
41/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070042static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
buzbee091cc402014-03-31 10:14:40 -070043 (1 << kLoadStoreElimination) | // TODO: this pass has been broken for awhile - fix or delete.
Brian Carlstrom7934ac22013-07-26 10:54:15 -070044 // (1 << kLoadHoisting) |
45 // (1 << kSuppressLoads) |
46 // (1 << kNullCheckElimination) |
Vladimir Markobfea9c22014-01-17 17:49:33 +000047 // (1 << kClassInitCheckElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070048 // (1 << kPromoteRegs) |
buzbee091cc402014-03-31 10:14:40 -070049 (1 << kTrackLiveTemps) | // FIXME: disable until liveness issue fixed.
Brian Carlstrom7934ac22013-07-26 10:54:15 -070050 // (1 << kSafeOptimizations) |
51 // (1 << kBBOpt) |
52 // (1 << kMatch) |
53 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080054 // (1 << kSuppressExceptionEdges) |
Vladimir Marko9820b7c2014-01-02 16:40:37 +000055 // (1 << kSuppressMethodInlining) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 0;
57
58static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070059 // (1 << kDebugDisplayMissingTargets) |
60 // (1 << kDebugVerbose) |
61 // (1 << kDebugDumpCFG) |
62 // (1 << kDebugSlowFieldPath) |
63 // (1 << kDebugSlowInvokePath) |
64 // (1 << kDebugSlowStringPath) |
65 // (1 << kDebugSlowestFieldPath) |
66 // (1 << kDebugSlowestStringPath) |
67 // (1 << kDebugExerciseResolveMethod) |
68 // (1 << kDebugVerifyDataflow) |
69 // (1 << kDebugShowMemoryUsage) |
70 // (1 << kDebugShowNops) |
71 // (1 << kDebugCountOpcodes) |
72 // (1 << kDebugDumpCheckStats) |
73 // (1 << kDebugDumpBitcodeFile) |
74 // (1 << kDebugVerifyBitcode) |
75 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -070076 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -070077 // (1 << kDebugTimings) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 0;
79
Vladimir Marko25724ef2013-11-12 15:09:20 +000080CompilationUnit::CompilationUnit(ArenaPool* pool)
81 : compiler_driver(NULL),
82 class_linker(NULL),
83 dex_file(NULL),
84 class_loader(NULL),
85 class_def_idx(0),
86 method_idx(0),
87 code_item(NULL),
88 access_flags(0),
89 invoke_type(kDirect),
90 shorty(NULL),
91 disable_opt(0),
92 enable_debug(0),
93 verbose(false),
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000094 compiler(NULL),
Vladimir Marko25724ef2013-11-12 15:09:20 +000095 instruction_set(kNone),
96 num_dalvik_registers(0),
97 insns(NULL),
98 num_ins(0),
99 num_outs(0),
100 num_regs(0),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000101 compiler_flip_match(false),
102 arena(pool),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000103 arena_stack(pool),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000104 mir_graph(NULL),
105 cg(NULL),
106 timings("QuickCompiler", true, false) {
107}
108
109CompilationUnit::~CompilationUnit() {
110}
111
buzbeea61f4952013-08-23 14:27:06 -0700112void CompilationUnit::StartTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000113 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700114 timings.StartSplit(label);
115 }
116}
117
118void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000119 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700120 timings.NewSplit(label);
121 }
122}
123
124void CompilationUnit::EndTiming() {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000125 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700126 timings.EndSplit();
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000127 if (enable_debug & (1 << kDebugTimings)) {
128 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
129 LOG(INFO) << Dumpable<TimingLogger>(timings);
130 }
buzbeea61f4952013-08-23 14:27:06 -0700131 }
132}
133
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100134// TODO: Remove this when we are able to compile everything.
135int arm64_support_list[] = {
136 Instruction::NOP,
137 // Instruction::MOVE,
138 // Instruction::MOVE_FROM16,
139 // Instruction::MOVE_16,
140 // Instruction::MOVE_WIDE,
141 // Instruction::MOVE_WIDE_FROM16,
142 // Instruction::MOVE_WIDE_16,
143 // Instruction::MOVE_OBJECT,
144 // Instruction::MOVE_OBJECT_FROM16,
145 // Instruction::MOVE_OBJECT_16,
146 // Instruction::MOVE_RESULT,
147 // Instruction::MOVE_RESULT_WIDE,
148 // Instruction::MOVE_RESULT_OBJECT,
149 // Instruction::MOVE_EXCEPTION,
150 // Instruction::RETURN_VOID,
151 // Instruction::RETURN,
152 // Instruction::RETURN_WIDE,
153 // Instruction::RETURN_OBJECT,
154 // Instruction::CONST_4,
155 // Instruction::CONST_16,
156 // Instruction::CONST,
157 // Instruction::CONST_HIGH16,
158 // Instruction::CONST_WIDE_16,
159 // Instruction::CONST_WIDE_32,
160 // Instruction::CONST_WIDE,
161 // Instruction::CONST_WIDE_HIGH16,
162 // Instruction::CONST_STRING,
163 // Instruction::CONST_STRING_JUMBO,
164 // Instruction::CONST_CLASS,
165 // Instruction::MONITOR_ENTER,
166 // Instruction::MONITOR_EXIT,
167 // Instruction::CHECK_CAST,
168 // Instruction::INSTANCE_OF,
169 // Instruction::ARRAY_LENGTH,
170 // Instruction::NEW_INSTANCE,
171 // Instruction::NEW_ARRAY,
172 // Instruction::FILLED_NEW_ARRAY,
173 // Instruction::FILLED_NEW_ARRAY_RANGE,
174 // Instruction::FILL_ARRAY_DATA,
175 // Instruction::THROW,
176 // Instruction::GOTO,
177 // Instruction::GOTO_16,
178 // Instruction::GOTO_32,
179 // Instruction::PACKED_SWITCH,
180 // Instruction::SPARSE_SWITCH,
181 // Instruction::CMPL_FLOAT,
182 // Instruction::CMPG_FLOAT,
183 // Instruction::CMPL_DOUBLE,
184 // Instruction::CMPG_DOUBLE,
185 // Instruction::CMP_LONG,
186 // Instruction::IF_EQ,
187 // Instruction::IF_NE,
188 // Instruction::IF_LT,
189 // Instruction::IF_GE,
190 // Instruction::IF_GT,
191 // Instruction::IF_LE,
192 // Instruction::IF_EQZ,
193 // Instruction::IF_NEZ,
194 // Instruction::IF_LTZ,
195 // Instruction::IF_GEZ,
196 // Instruction::IF_GTZ,
197 // Instruction::IF_LEZ,
198 // Instruction::UNUSED_3E,
199 // Instruction::UNUSED_3F,
200 // Instruction::UNUSED_40,
201 // Instruction::UNUSED_41,
202 // Instruction::UNUSED_42,
203 // Instruction::UNUSED_43,
204 // Instruction::AGET,
205 // Instruction::AGET_WIDE,
206 // Instruction::AGET_OBJECT,
207 // Instruction::AGET_BOOLEAN,
208 // Instruction::AGET_BYTE,
209 // Instruction::AGET_CHAR,
210 // Instruction::AGET_SHORT,
211 // Instruction::APUT,
212 // Instruction::APUT_WIDE,
213 // Instruction::APUT_OBJECT,
214 // Instruction::APUT_BOOLEAN,
215 // Instruction::APUT_BYTE,
216 // Instruction::APUT_CHAR,
217 // Instruction::APUT_SHORT,
218 // Instruction::IGET,
219 // Instruction::IGET_WIDE,
220 // Instruction::IGET_OBJECT,
221 // Instruction::IGET_BOOLEAN,
222 // Instruction::IGET_BYTE,
223 // Instruction::IGET_CHAR,
224 // Instruction::IGET_SHORT,
225 // Instruction::IPUT,
226 // Instruction::IPUT_WIDE,
227 // Instruction::IPUT_OBJECT,
228 // Instruction::IPUT_BOOLEAN,
229 // Instruction::IPUT_BYTE,
230 // Instruction::IPUT_CHAR,
231 // Instruction::IPUT_SHORT,
232 // Instruction::SGET,
233 // Instruction::SGET_WIDE,
234 // Instruction::SGET_OBJECT,
235 // Instruction::SGET_BOOLEAN,
236 // Instruction::SGET_BYTE,
237 // Instruction::SGET_CHAR,
238 // Instruction::SGET_SHORT,
239 // Instruction::SPUT,
240 // Instruction::SPUT_WIDE,
241 // Instruction::SPUT_OBJECT,
242 // Instruction::SPUT_BOOLEAN,
243 // Instruction::SPUT_BYTE,
244 // Instruction::SPUT_CHAR,
245 // Instruction::SPUT_SHORT,
246 Instruction::INVOKE_VIRTUAL,
247 Instruction::INVOKE_SUPER,
248 Instruction::INVOKE_DIRECT,
249 Instruction::INVOKE_STATIC,
250 Instruction::INVOKE_INTERFACE,
251 // Instruction::RETURN_VOID_BARRIER,
252 // Instruction::INVOKE_VIRTUAL_RANGE,
253 // Instruction::INVOKE_SUPER_RANGE,
254 // Instruction::INVOKE_DIRECT_RANGE,
255 // Instruction::INVOKE_STATIC_RANGE,
256 // Instruction::INVOKE_INTERFACE_RANGE,
257 // Instruction::UNUSED_79,
258 // Instruction::UNUSED_7A,
259 // Instruction::NEG_INT,
260 // Instruction::NOT_INT,
261 // Instruction::NEG_LONG,
262 // Instruction::NOT_LONG,
263 // Instruction::NEG_FLOAT,
264 // Instruction::NEG_DOUBLE,
265 // Instruction::INT_TO_LONG,
266 // Instruction::INT_TO_FLOAT,
267 // Instruction::INT_TO_DOUBLE,
268 // Instruction::LONG_TO_INT,
269 // Instruction::LONG_TO_FLOAT,
270 // Instruction::LONG_TO_DOUBLE,
271 // Instruction::FLOAT_TO_INT,
272 // Instruction::FLOAT_TO_LONG,
273 // Instruction::FLOAT_TO_DOUBLE,
274 // Instruction::DOUBLE_TO_INT,
275 // Instruction::DOUBLE_TO_LONG,
276 // Instruction::DOUBLE_TO_FLOAT,
277 // Instruction::INT_TO_BYTE,
278 // Instruction::INT_TO_CHAR,
279 // Instruction::INT_TO_SHORT,
280 // Instruction::ADD_INT,
281 // Instruction::SUB_INT,
282 // Instruction::MUL_INT,
283 // Instruction::DIV_INT,
284 // Instruction::REM_INT,
285 // Instruction::AND_INT,
286 // Instruction::OR_INT,
287 // Instruction::XOR_INT,
288 // Instruction::SHL_INT,
289 // Instruction::SHR_INT,
290 // Instruction::USHR_INT,
291 // Instruction::ADD_LONG,
292 // Instruction::SUB_LONG,
293 // Instruction::MUL_LONG,
294 // Instruction::DIV_LONG,
295 // Instruction::REM_LONG,
296 // Instruction::AND_LONG,
297 // Instruction::OR_LONG,
298 // Instruction::XOR_LONG,
299 // Instruction::SHL_LONG,
300 // Instruction::SHR_LONG,
301 // Instruction::USHR_LONG,
302 // Instruction::ADD_FLOAT,
303 // Instruction::SUB_FLOAT,
304 // Instruction::MUL_FLOAT,
305 // Instruction::DIV_FLOAT,
306 // Instruction::REM_FLOAT,
307 // Instruction::ADD_DOUBLE,
308 // Instruction::SUB_DOUBLE,
309 // Instruction::MUL_DOUBLE,
310 // Instruction::DIV_DOUBLE,
311 // Instruction::REM_DOUBLE,
312 // Instruction::ADD_INT_2ADDR,
313 // Instruction::SUB_INT_2ADDR,
314 // Instruction::MUL_INT_2ADDR,
315 // Instruction::DIV_INT_2ADDR,
316 // Instruction::REM_INT_2ADDR,
317 // Instruction::AND_INT_2ADDR,
318 // Instruction::OR_INT_2ADDR,
319 // Instruction::XOR_INT_2ADDR,
320 // Instruction::SHL_INT_2ADDR,
321 // Instruction::SHR_INT_2ADDR,
322 // Instruction::USHR_INT_2ADDR,
323 // Instruction::ADD_LONG_2ADDR,
324 // Instruction::SUB_LONG_2ADDR,
325 // Instruction::MUL_LONG_2ADDR,
326 // Instruction::DIV_LONG_2ADDR,
327 // Instruction::REM_LONG_2ADDR,
328 // Instruction::AND_LONG_2ADDR,
329 // Instruction::OR_LONG_2ADDR,
330 // Instruction::XOR_LONG_2ADDR,
331 // Instruction::SHL_LONG_2ADDR,
332 // Instruction::SHR_LONG_2ADDR,
333 // Instruction::USHR_LONG_2ADDR,
334 // Instruction::ADD_FLOAT_2ADDR,
335 // Instruction::SUB_FLOAT_2ADDR,
336 // Instruction::MUL_FLOAT_2ADDR,
337 // Instruction::DIV_FLOAT_2ADDR,
338 // Instruction::REM_FLOAT_2ADDR,
339 // Instruction::ADD_DOUBLE_2ADDR,
340 // Instruction::SUB_DOUBLE_2ADDR,
341 // Instruction::MUL_DOUBLE_2ADDR,
342 // Instruction::DIV_DOUBLE_2ADDR,
343 // Instruction::REM_DOUBLE_2ADDR,
344 // Instruction::ADD_INT_LIT16,
345 // Instruction::RSUB_INT,
346 // Instruction::MUL_INT_LIT16,
347 // Instruction::DIV_INT_LIT16,
348 // Instruction::REM_INT_LIT16,
349 // Instruction::AND_INT_LIT16,
350 // Instruction::OR_INT_LIT16,
351 // Instruction::XOR_INT_LIT16,
352 // Instruction::ADD_INT_LIT8,
353 // Instruction::RSUB_INT_LIT8,
354 // Instruction::MUL_INT_LIT8,
355 // Instruction::DIV_INT_LIT8,
356 // Instruction::REM_INT_LIT8,
357 // Instruction::AND_INT_LIT8,
358 // Instruction::OR_INT_LIT8,
359 // Instruction::XOR_INT_LIT8,
360 // Instruction::SHL_INT_LIT8,
361 // Instruction::SHR_INT_LIT8,
362 // Instruction::USHR_INT_LIT8,
363 // Instruction::IGET_QUICK,
364 // Instruction::IGET_WIDE_QUICK,
365 // Instruction::IGET_OBJECT_QUICK,
366 // Instruction::IPUT_QUICK,
367 // Instruction::IPUT_WIDE_QUICK,
368 // Instruction::IPUT_OBJECT_QUICK,
369 // Instruction::INVOKE_VIRTUAL_QUICK,
370 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
371 // Instruction::UNUSED_EB,
372 // Instruction::UNUSED_EC,
373 // Instruction::UNUSED_ED,
374 // Instruction::UNUSED_EE,
375 // Instruction::UNUSED_EF,
376 // Instruction::UNUSED_F0,
377 // Instruction::UNUSED_F1,
378 // Instruction::UNUSED_F2,
379 // Instruction::UNUSED_F3,
380 // Instruction::UNUSED_F4,
381 // Instruction::UNUSED_F5,
382 // Instruction::UNUSED_F6,
383 // Instruction::UNUSED_F7,
384 // Instruction::UNUSED_F8,
385 // Instruction::UNUSED_F9,
386 // Instruction::UNUSED_FA,
387 // Instruction::UNUSED_FB,
388 // Instruction::UNUSED_FC,
389 // Instruction::UNUSED_FD,
390 // Instruction::UNUSED_FE,
391 // Instruction::UNUSED_FF,
392
393 // ----- ExtendedMIROpcode -----
394 // kMirOpPhi,
395 // kMirOpCopy,
396 // kMirOpFusedCmplFloat,
397 // kMirOpFusedCmpgFloat,
398 // kMirOpFusedCmplDouble,
399 // kMirOpFusedCmpgDouble,
400 // kMirOpFusedCmpLong,
401 // kMirOpNop,
402 // kMirOpNullCheck,
403 // kMirOpRangeCheck,
404 // kMirOpDivZeroCheck,
405 // kMirOpCheck,
406 // kMirOpCheckPart2,
407 // kMirOpSelect,
408 // kMirOpLast,
409};
410
411// TODO: Remove this when we are able to compile everything.
412static bool CanCompileShorty(const char* shorty) {
413 uint32_t shorty_size = strlen(shorty);
414 CHECK_GE(shorty_size, 1u);
415 // Set a limitation on maximum number of parameters.
416 // Note : there is an implied "method*" parameter, and probably "this" as well.
417 // 1 is for the return type. Currently, we only accept 2 parameters at the most.
418 if (shorty_size > (1 + 2)) {
419 return false;
420 }
421 // Z : boolean
422 // B : byte
423 // S : short
424 // C : char
425 // I : int
426 // L : long
427 // F : float
428 // D : double
429 // L : reference(object, array)
430 // V : void
431 // Current calling conversion only support 32bit softfp
432 // which has problems with long, float, double
433 constexpr char supported_types[] = "ZBSCILV";
434 for (uint32_t i = 0; i < shorty_size; i++) {
435 if (strchr(supported_types, shorty[i]) == nullptr) {
436 return false;
437 }
438 }
439 return true;
440};
441
442// TODO: Remove this when we are able to compile everything.
443// Skip the method that we do not support currently.
444static bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
445 CompilationUnit& cu) {
446 // There is some limitation with current ARM 64 backend.
447 if (cu.instruction_set == kArm64) {
448 // Check if we can compile the prototype.
449 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
450 if (!CanCompileShorty(shorty)) {
451 VLOG(compiler) << "Unsupported shorty : " << shorty;
452 return false;
453 }
454
455 for (int idx = 0; idx < cu.mir_graph->GetNumBlocks(); idx++) {
456 BasicBlock *bb = cu.mir_graph->GetBasicBlock(idx);
457 if (bb == NULL) continue;
458 if (bb->block_type == kDead) continue;
459 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
460 int opcode = mir->dalvikInsn.opcode;
461 // Check if we support the byte code.
462 if (std::find(arm64_support_list, arm64_support_list + arraysize(arm64_support_list),
463 opcode) == arm64_support_list + arraysize(arm64_support_list)) {
464 if (opcode < kMirOpFirst) {
465 VLOG(compiler) << "Unsupported dalvik byte code : "
466 << mir->dalvikInsn.opcode;
467 } else {
468 VLOG(compiler) << "Unsupported extended MIR opcode : "
469 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
470 }
471 return false;
472 }
473 // Check if it invokes a prototype that we cannot support.
474 if (Instruction::INVOKE_VIRTUAL == opcode ||
475 Instruction::INVOKE_SUPER == opcode ||
476 Instruction::INVOKE_DIRECT == opcode ||
477 Instruction::INVOKE_STATIC == opcode ||
478 Instruction::INVOKE_INTERFACE == opcode) {
479 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
480 const char* invoke_method_shorty = dex_file.GetMethodShorty(
481 dex_file.GetMethodId(invoke_method_idx));
482 if (!CanCompileShorty(invoke_method_shorty)) {
483 VLOG(compiler) << "Unsupported to invoke '"
484 << PrettyMethod(invoke_method_idx, dex_file)
485 << "' with shorty : " << invoke_method_shorty;
486 return false;
487 }
488 }
489 }
490 }
491
492 LOG(INFO) << "Using experimental instruction set A64 for "
493 << PrettyMethod(method_idx, dex_file);
494 }
495 return true;
496}
497
Ian Rogers3d504072014-03-01 09:16:49 -0800498static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000499 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 const DexFile::CodeItem* code_item,
501 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700502 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000503 jobject class_loader, const DexFile& dex_file,
504 void* llvm_compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700506 if (code_item->insns_size_in_code_units_ >= 0x10000) {
507 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
508 << " in " << PrettyMethod(method_idx, dex_file);
509 return NULL;
510 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511
Jeff Hao4a200f52014-04-01 14:58:49 -0700512 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000513 return nullptr;
514 }
515
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800517 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518
Ian Rogers3d504072014-03-01 09:16:49 -0800519 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700520 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800521 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -0700522 if (cu.instruction_set == kArm) {
523 cu.instruction_set = kThumb2;
524 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700525 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000526 cu.compiler = compiler;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000527 // TODO: x86_64 & arm64 are not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700528 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100529 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700530 (cu.instruction_set == kX86) ||
531 (cu.instruction_set == kX86_64) ||
532 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700535 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700537 cu.compiler_flip_match = false;
538 bool use_match = !cu.compiler_method_match.empty();
539 bool match = use_match && (cu.compiler_flip_match ^
540 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 std::string::npos));
542 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700543 cu.disable_opt = kCompilerOptimizerDisableFlags;
544 cu.enable_debug = kCompilerDebugFlags;
545 cu.verbose = VLOG_IS_ON(compiler) ||
546 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 }
548
Mingyao Yang42d65c52014-04-18 16:49:39 -0700549 if (gVerboseMethods.size() != 0) {
550 cu.verbose = false;
551 for (size_t i = 0; i < gVerboseMethods.size(); ++i) {
552 if (PrettyMethod(method_idx, dex_file).find(gVerboseMethods[i])
553 != std::string::npos) {
554 cu.verbose = true;
555 break;
556 }
557 }
558 }
559
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 /*
561 * TODO: rework handling of optimization and debug flags. Should we split out
562 * MIR and backend flags? Need command-line setting as well.
563 */
564
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000565 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700567 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700569 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 (1 << kLoadStoreElimination) |
571 (1 << kLoadHoisting) |
572 (1 << kSuppressLoads) |
573 (1 << kNullCheckElimination) |
574 (1 << kPromoteRegs) |
575 (1 << kTrackLiveTemps) |
576 (1 << kSafeOptimizations) |
577 (1 << kBBOpt) |
578 (1 << kMatch) |
579 (1 << kPromoteCompilerTemps));
580 }
581
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100582 if (cu.instruction_set == kArm64) {
583 // TODO(Arm64): enable optimizations once backend is mature enough.
584 cu.disable_opt = ~(uint32_t)0;
585 }
586
buzbeea61f4952013-08-23 14:27:06 -0700587 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700588 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800590 /*
591 * After creation of the MIR graph, also create the code generator.
592 * The reason we do this is that optimizations on the MIR graph may need to get information
593 * that is only available if a CG exists.
594 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000595 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800596
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 /* Gathering opcode stats? */
598 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700599 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 }
601
Dave Allison39c3bfb2014-01-28 18:33:52 -0800602 // Check early if we should skip this compilation if using the profiled filter.
603 if (cu.compiler_driver->ProfilePresent()) {
604 std::string methodname = PrettyMethod(method_idx, dex_file);
605 if (cu.mir_graph->SkipCompilation(methodname)) {
606 return NULL;
607 }
608 }
609
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700611 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 class_loader, dex_file);
613
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100614 // TODO(Arm64): Remove this when we are able to compile everything.
615 if (!CanCompileMethod(method_idx, dex_file, cu)) {
616 VLOG(compiler) << "Cannot compile method : " << PrettyMethod(method_idx, dex_file);
617 return nullptr;
618 }
619
buzbee1da1e2f2013-11-15 13:37:01 -0800620 cu.NewTimingSplit("MIROpt:CheckFilters");
Jeff Hao4a200f52014-04-01 14:58:49 -0700621 if (cu.mir_graph->SkipCompilation()) {
622 return NULL;
buzbeeee17e0a2013-07-31 10:47:37 -0700623 }
624
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800625 /* Create the pass driver and launch it */
Ian Rogers3d504072014-03-01 09:16:49 -0800626 PassDriver pass_driver(&cu);
627 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700629 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
630 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 }
632
633 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700634 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 }
636
buzbee1da1e2f2013-11-15 13:37:01 -0800637 /* Reassociate sreg names with original Dalvik vreg names. */
638 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000640 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
641 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
642 if (cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
643 MemStats stack_stats(cu.arena_stack.GetPeakStats());
644 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
645 }
646 }
647 cu.arena_stack.Reset();
648
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 CompiledMethod* result = NULL;
650
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700651 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652
buzbee1da1e2f2013-11-15 13:37:01 -0800653 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700654 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800655 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656
657 if (result) {
658 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
659 } else {
660 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
661 }
662
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700663 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000664 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000665 MemStats mem_stats(cu.arena.GetMemStats());
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000666 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 }
668 }
669
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700670 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
671 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 << " " << PrettyMethod(method_idx, dex_file);
673 }
674
buzbeea61f4952013-08-23 14:27:06 -0700675 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800676 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 return result;
678}
679
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000680CompiledMethod* CompileOneMethod(CompilerDriver& driver,
681 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 const DexFile::CodeItem* code_item,
683 uint32_t access_flags,
684 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700685 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 uint32_t method_idx,
687 jobject class_loader,
688 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000689 void* compilation_unit) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000690 return CompileMethod(driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000691 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692}
693
694} // namespace art
695
696extern "C" art::CompiledMethod*
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000697 ArtQuickCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 const art::DexFile::CodeItem* code_item,
699 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700700 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 const art::DexFile& dex_file) {
702 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000703 art::Compiler* compiler = driver.GetCompiler();
704 return art::CompileOneMethod(driver, compiler, code_item, access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 class_def_idx, method_idx, class_loader, dex_file,
706 NULL /* use thread llvm_info */);
707}