blob: a224de2c09412ce163382fdaea286d121e9fea41 [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) |
buzbee30adc732014-05-09 15:10:18 -070049 // (1 << kTrackLiveTemps) |
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)
Ian Rogers93dcff32014-05-15 09:11:23 -070081 : compiler_driver(nullptr),
82 class_linker(nullptr),
83 dex_file(nullptr),
84 class_loader(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000085 class_def_idx(0),
86 method_idx(0),
Ian Rogers93dcff32014-05-15 09:11:23 -070087 code_item(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000088 access_flags(0),
89 invoke_type(kDirect),
Ian Rogers93dcff32014-05-15 09:11:23 -070090 shorty(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000091 disable_opt(0),
92 enable_debug(0),
93 verbose(false),
Ian Rogers93dcff32014-05-15 09:11:23 -070094 compiler(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000095 instruction_set(kNone),
Ian Rogers93dcff32014-05-15 09:11:23 -070096 target64(false),
Vladimir Marko25724ef2013-11-12 15:09:20 +000097 num_dalvik_registers(0),
Ian Rogers93dcff32014-05-15 09:11:23 -070098 insns(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000099 num_ins(0),
100 num_outs(0),
101 num_regs(0),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000102 compiler_flip_match(false),
103 arena(pool),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000104 arena_stack(pool),
Ian Rogers93dcff32014-05-15 09:11:23 -0700105 mir_graph(nullptr),
106 cg(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000107 timings("QuickCompiler", true, false) {
108}
109
110CompilationUnit::~CompilationUnit() {
111}
112
buzbeea61f4952013-08-23 14:27:06 -0700113void CompilationUnit::StartTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000114 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700115 timings.StartSplit(label);
116 }
117}
118
119void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000120 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700121 timings.NewSplit(label);
122 }
123}
124
125void CompilationUnit::EndTiming() {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000126 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700127 timings.EndSplit();
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000128 if (enable_debug & (1 << kDebugTimings)) {
129 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
130 LOG(INFO) << Dumpable<TimingLogger>(timings);
131 }
buzbeea61f4952013-08-23 14:27:06 -0700132 }
133}
134
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100135// TODO: Remove this when we are able to compile everything.
136int arm64_support_list[] = {
137 Instruction::NOP,
138 // Instruction::MOVE,
139 // Instruction::MOVE_FROM16,
140 // Instruction::MOVE_16,
141 // Instruction::MOVE_WIDE,
142 // Instruction::MOVE_WIDE_FROM16,
143 // Instruction::MOVE_WIDE_16,
144 // Instruction::MOVE_OBJECT,
145 // Instruction::MOVE_OBJECT_FROM16,
146 // Instruction::MOVE_OBJECT_16,
147 // Instruction::MOVE_RESULT,
148 // Instruction::MOVE_RESULT_WIDE,
149 // Instruction::MOVE_RESULT_OBJECT,
150 // Instruction::MOVE_EXCEPTION,
151 // Instruction::RETURN_VOID,
152 // Instruction::RETURN,
153 // Instruction::RETURN_WIDE,
154 // Instruction::RETURN_OBJECT,
155 // Instruction::CONST_4,
156 // Instruction::CONST_16,
157 // Instruction::CONST,
158 // Instruction::CONST_HIGH16,
159 // Instruction::CONST_WIDE_16,
160 // Instruction::CONST_WIDE_32,
161 // Instruction::CONST_WIDE,
162 // Instruction::CONST_WIDE_HIGH16,
163 // Instruction::CONST_STRING,
164 // Instruction::CONST_STRING_JUMBO,
165 // Instruction::CONST_CLASS,
166 // Instruction::MONITOR_ENTER,
167 // Instruction::MONITOR_EXIT,
168 // Instruction::CHECK_CAST,
169 // Instruction::INSTANCE_OF,
170 // Instruction::ARRAY_LENGTH,
171 // Instruction::NEW_INSTANCE,
172 // Instruction::NEW_ARRAY,
173 // Instruction::FILLED_NEW_ARRAY,
174 // Instruction::FILLED_NEW_ARRAY_RANGE,
175 // Instruction::FILL_ARRAY_DATA,
176 // Instruction::THROW,
177 // Instruction::GOTO,
178 // Instruction::GOTO_16,
179 // Instruction::GOTO_32,
180 // Instruction::PACKED_SWITCH,
181 // Instruction::SPARSE_SWITCH,
182 // Instruction::CMPL_FLOAT,
183 // Instruction::CMPG_FLOAT,
184 // Instruction::CMPL_DOUBLE,
185 // Instruction::CMPG_DOUBLE,
186 // Instruction::CMP_LONG,
187 // Instruction::IF_EQ,
188 // Instruction::IF_NE,
189 // Instruction::IF_LT,
190 // Instruction::IF_GE,
191 // Instruction::IF_GT,
192 // Instruction::IF_LE,
193 // Instruction::IF_EQZ,
194 // Instruction::IF_NEZ,
195 // Instruction::IF_LTZ,
196 // Instruction::IF_GEZ,
197 // Instruction::IF_GTZ,
198 // Instruction::IF_LEZ,
199 // Instruction::UNUSED_3E,
200 // Instruction::UNUSED_3F,
201 // Instruction::UNUSED_40,
202 // Instruction::UNUSED_41,
203 // Instruction::UNUSED_42,
204 // Instruction::UNUSED_43,
205 // Instruction::AGET,
206 // Instruction::AGET_WIDE,
207 // Instruction::AGET_OBJECT,
208 // Instruction::AGET_BOOLEAN,
209 // Instruction::AGET_BYTE,
210 // Instruction::AGET_CHAR,
211 // Instruction::AGET_SHORT,
212 // Instruction::APUT,
213 // Instruction::APUT_WIDE,
214 // Instruction::APUT_OBJECT,
215 // Instruction::APUT_BOOLEAN,
216 // Instruction::APUT_BYTE,
217 // Instruction::APUT_CHAR,
218 // Instruction::APUT_SHORT,
219 // Instruction::IGET,
220 // Instruction::IGET_WIDE,
221 // Instruction::IGET_OBJECT,
222 // Instruction::IGET_BOOLEAN,
223 // Instruction::IGET_BYTE,
224 // Instruction::IGET_CHAR,
225 // Instruction::IGET_SHORT,
226 // Instruction::IPUT,
227 // Instruction::IPUT_WIDE,
228 // Instruction::IPUT_OBJECT,
229 // Instruction::IPUT_BOOLEAN,
230 // Instruction::IPUT_BYTE,
231 // Instruction::IPUT_CHAR,
232 // Instruction::IPUT_SHORT,
233 // Instruction::SGET,
234 // Instruction::SGET_WIDE,
235 // Instruction::SGET_OBJECT,
236 // Instruction::SGET_BOOLEAN,
237 // Instruction::SGET_BYTE,
238 // Instruction::SGET_CHAR,
239 // Instruction::SGET_SHORT,
240 // Instruction::SPUT,
241 // Instruction::SPUT_WIDE,
242 // Instruction::SPUT_OBJECT,
243 // Instruction::SPUT_BOOLEAN,
244 // Instruction::SPUT_BYTE,
245 // Instruction::SPUT_CHAR,
246 // Instruction::SPUT_SHORT,
247 Instruction::INVOKE_VIRTUAL,
248 Instruction::INVOKE_SUPER,
249 Instruction::INVOKE_DIRECT,
250 Instruction::INVOKE_STATIC,
251 Instruction::INVOKE_INTERFACE,
252 // Instruction::RETURN_VOID_BARRIER,
253 // Instruction::INVOKE_VIRTUAL_RANGE,
254 // Instruction::INVOKE_SUPER_RANGE,
255 // Instruction::INVOKE_DIRECT_RANGE,
256 // Instruction::INVOKE_STATIC_RANGE,
257 // Instruction::INVOKE_INTERFACE_RANGE,
258 // Instruction::UNUSED_79,
259 // Instruction::UNUSED_7A,
260 // Instruction::NEG_INT,
261 // Instruction::NOT_INT,
262 // Instruction::NEG_LONG,
263 // Instruction::NOT_LONG,
264 // Instruction::NEG_FLOAT,
265 // Instruction::NEG_DOUBLE,
266 // Instruction::INT_TO_LONG,
267 // Instruction::INT_TO_FLOAT,
268 // Instruction::INT_TO_DOUBLE,
269 // Instruction::LONG_TO_INT,
270 // Instruction::LONG_TO_FLOAT,
271 // Instruction::LONG_TO_DOUBLE,
272 // Instruction::FLOAT_TO_INT,
273 // Instruction::FLOAT_TO_LONG,
274 // Instruction::FLOAT_TO_DOUBLE,
275 // Instruction::DOUBLE_TO_INT,
276 // Instruction::DOUBLE_TO_LONG,
277 // Instruction::DOUBLE_TO_FLOAT,
278 // Instruction::INT_TO_BYTE,
279 // Instruction::INT_TO_CHAR,
280 // Instruction::INT_TO_SHORT,
281 // Instruction::ADD_INT,
282 // Instruction::SUB_INT,
283 // Instruction::MUL_INT,
284 // Instruction::DIV_INT,
285 // Instruction::REM_INT,
286 // Instruction::AND_INT,
287 // Instruction::OR_INT,
288 // Instruction::XOR_INT,
289 // Instruction::SHL_INT,
290 // Instruction::SHR_INT,
291 // Instruction::USHR_INT,
292 // Instruction::ADD_LONG,
293 // Instruction::SUB_LONG,
294 // Instruction::MUL_LONG,
295 // Instruction::DIV_LONG,
296 // Instruction::REM_LONG,
297 // Instruction::AND_LONG,
298 // Instruction::OR_LONG,
299 // Instruction::XOR_LONG,
300 // Instruction::SHL_LONG,
301 // Instruction::SHR_LONG,
302 // Instruction::USHR_LONG,
303 // Instruction::ADD_FLOAT,
304 // Instruction::SUB_FLOAT,
305 // Instruction::MUL_FLOAT,
306 // Instruction::DIV_FLOAT,
307 // Instruction::REM_FLOAT,
308 // Instruction::ADD_DOUBLE,
309 // Instruction::SUB_DOUBLE,
310 // Instruction::MUL_DOUBLE,
311 // Instruction::DIV_DOUBLE,
312 // Instruction::REM_DOUBLE,
313 // Instruction::ADD_INT_2ADDR,
314 // Instruction::SUB_INT_2ADDR,
315 // Instruction::MUL_INT_2ADDR,
316 // Instruction::DIV_INT_2ADDR,
317 // Instruction::REM_INT_2ADDR,
318 // Instruction::AND_INT_2ADDR,
319 // Instruction::OR_INT_2ADDR,
320 // Instruction::XOR_INT_2ADDR,
321 // Instruction::SHL_INT_2ADDR,
322 // Instruction::SHR_INT_2ADDR,
323 // Instruction::USHR_INT_2ADDR,
324 // Instruction::ADD_LONG_2ADDR,
325 // Instruction::SUB_LONG_2ADDR,
326 // Instruction::MUL_LONG_2ADDR,
327 // Instruction::DIV_LONG_2ADDR,
328 // Instruction::REM_LONG_2ADDR,
329 // Instruction::AND_LONG_2ADDR,
330 // Instruction::OR_LONG_2ADDR,
331 // Instruction::XOR_LONG_2ADDR,
332 // Instruction::SHL_LONG_2ADDR,
333 // Instruction::SHR_LONG_2ADDR,
334 // Instruction::USHR_LONG_2ADDR,
335 // Instruction::ADD_FLOAT_2ADDR,
336 // Instruction::SUB_FLOAT_2ADDR,
337 // Instruction::MUL_FLOAT_2ADDR,
338 // Instruction::DIV_FLOAT_2ADDR,
339 // Instruction::REM_FLOAT_2ADDR,
340 // Instruction::ADD_DOUBLE_2ADDR,
341 // Instruction::SUB_DOUBLE_2ADDR,
342 // Instruction::MUL_DOUBLE_2ADDR,
343 // Instruction::DIV_DOUBLE_2ADDR,
344 // Instruction::REM_DOUBLE_2ADDR,
345 // Instruction::ADD_INT_LIT16,
346 // Instruction::RSUB_INT,
347 // Instruction::MUL_INT_LIT16,
348 // Instruction::DIV_INT_LIT16,
349 // Instruction::REM_INT_LIT16,
350 // Instruction::AND_INT_LIT16,
351 // Instruction::OR_INT_LIT16,
352 // Instruction::XOR_INT_LIT16,
353 // Instruction::ADD_INT_LIT8,
354 // Instruction::RSUB_INT_LIT8,
355 // Instruction::MUL_INT_LIT8,
356 // Instruction::DIV_INT_LIT8,
357 // Instruction::REM_INT_LIT8,
358 // Instruction::AND_INT_LIT8,
359 // Instruction::OR_INT_LIT8,
360 // Instruction::XOR_INT_LIT8,
361 // Instruction::SHL_INT_LIT8,
362 // Instruction::SHR_INT_LIT8,
363 // Instruction::USHR_INT_LIT8,
364 // Instruction::IGET_QUICK,
365 // Instruction::IGET_WIDE_QUICK,
366 // Instruction::IGET_OBJECT_QUICK,
367 // Instruction::IPUT_QUICK,
368 // Instruction::IPUT_WIDE_QUICK,
369 // Instruction::IPUT_OBJECT_QUICK,
370 // Instruction::INVOKE_VIRTUAL_QUICK,
371 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
372 // Instruction::UNUSED_EB,
373 // Instruction::UNUSED_EC,
374 // Instruction::UNUSED_ED,
375 // Instruction::UNUSED_EE,
376 // Instruction::UNUSED_EF,
377 // Instruction::UNUSED_F0,
378 // Instruction::UNUSED_F1,
379 // Instruction::UNUSED_F2,
380 // Instruction::UNUSED_F3,
381 // Instruction::UNUSED_F4,
382 // Instruction::UNUSED_F5,
383 // Instruction::UNUSED_F6,
384 // Instruction::UNUSED_F7,
385 // Instruction::UNUSED_F8,
386 // Instruction::UNUSED_F9,
387 // Instruction::UNUSED_FA,
388 // Instruction::UNUSED_FB,
389 // Instruction::UNUSED_FC,
390 // Instruction::UNUSED_FD,
391 // Instruction::UNUSED_FE,
392 // Instruction::UNUSED_FF,
393
394 // ----- ExtendedMIROpcode -----
395 // kMirOpPhi,
396 // kMirOpCopy,
397 // kMirOpFusedCmplFloat,
398 // kMirOpFusedCmpgFloat,
399 // kMirOpFusedCmplDouble,
400 // kMirOpFusedCmpgDouble,
401 // kMirOpFusedCmpLong,
402 // kMirOpNop,
403 // kMirOpNullCheck,
404 // kMirOpRangeCheck,
405 // kMirOpDivZeroCheck,
406 // kMirOpCheck,
407 // kMirOpCheckPart2,
408 // kMirOpSelect,
409 // kMirOpLast,
410};
411
412// TODO: Remove this when we are able to compile everything.
413static bool CanCompileShorty(const char* shorty) {
414 uint32_t shorty_size = strlen(shorty);
415 CHECK_GE(shorty_size, 1u);
416 // Set a limitation on maximum number of parameters.
417 // Note : there is an implied "method*" parameter, and probably "this" as well.
418 // 1 is for the return type. Currently, we only accept 2 parameters at the most.
419 if (shorty_size > (1 + 2)) {
420 return false;
421 }
422 // Z : boolean
423 // B : byte
424 // S : short
425 // C : char
426 // I : int
427 // L : long
428 // F : float
429 // D : double
430 // L : reference(object, array)
431 // V : void
432 // Current calling conversion only support 32bit softfp
433 // which has problems with long, float, double
434 constexpr char supported_types[] = "ZBSCILV";
435 for (uint32_t i = 0; i < shorty_size; i++) {
436 if (strchr(supported_types, shorty[i]) == nullptr) {
437 return false;
438 }
439 }
440 return true;
441};
442
443// TODO: Remove this when we are able to compile everything.
444// Skip the method that we do not support currently.
445static bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
446 CompilationUnit& cu) {
447 // There is some limitation with current ARM 64 backend.
448 if (cu.instruction_set == kArm64) {
449 // Check if we can compile the prototype.
450 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
451 if (!CanCompileShorty(shorty)) {
452 VLOG(compiler) << "Unsupported shorty : " << shorty;
453 return false;
454 }
455
456 for (int idx = 0; idx < cu.mir_graph->GetNumBlocks(); idx++) {
457 BasicBlock *bb = cu.mir_graph->GetBasicBlock(idx);
458 if (bb == NULL) continue;
459 if (bb->block_type == kDead) continue;
460 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
461 int opcode = mir->dalvikInsn.opcode;
462 // Check if we support the byte code.
463 if (std::find(arm64_support_list, arm64_support_list + arraysize(arm64_support_list),
464 opcode) == arm64_support_list + arraysize(arm64_support_list)) {
465 if (opcode < kMirOpFirst) {
466 VLOG(compiler) << "Unsupported dalvik byte code : "
467 << mir->dalvikInsn.opcode;
468 } else {
469 VLOG(compiler) << "Unsupported extended MIR opcode : "
470 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
471 }
472 return false;
473 }
474 // Check if it invokes a prototype that we cannot support.
475 if (Instruction::INVOKE_VIRTUAL == opcode ||
476 Instruction::INVOKE_SUPER == opcode ||
477 Instruction::INVOKE_DIRECT == opcode ||
478 Instruction::INVOKE_STATIC == opcode ||
479 Instruction::INVOKE_INTERFACE == opcode) {
480 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
481 const char* invoke_method_shorty = dex_file.GetMethodShorty(
482 dex_file.GetMethodId(invoke_method_idx));
483 if (!CanCompileShorty(invoke_method_shorty)) {
484 VLOG(compiler) << "Unsupported to invoke '"
485 << PrettyMethod(invoke_method_idx, dex_file)
486 << "' with shorty : " << invoke_method_shorty;
487 return false;
488 }
489 }
490 }
491 }
492
493 LOG(INFO) << "Using experimental instruction set A64 for "
494 << PrettyMethod(method_idx, dex_file);
495 }
496 return true;
497}
498
Ian Rogers3d504072014-03-01 09:16:49 -0800499static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000500 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 const DexFile::CodeItem* code_item,
502 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700503 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000504 jobject class_loader, const DexFile& dex_file,
505 void* llvm_compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700507 if (code_item->insns_size_in_code_units_ >= 0x10000) {
508 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
509 << " in " << PrettyMethod(method_idx, dex_file);
510 return NULL;
511 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512
Jeff Hao4a200f52014-04-01 14:58:49 -0700513 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000514 return nullptr;
515 }
516
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800518 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519
Ian Rogers3d504072014-03-01 09:16:49 -0800520 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700521 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800522 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -0700523 if (cu.instruction_set == kArm) {
524 cu.instruction_set = kThumb2;
525 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700526 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000527 cu.compiler = compiler;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000528 // TODO: x86_64 & arm64 are not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700529 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100530 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700531 (cu.instruction_set == kX86) ||
532 (cu.instruction_set == kX86_64) ||
533 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700536 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700538 cu.compiler_flip_match = false;
539 bool use_match = !cu.compiler_method_match.empty();
540 bool match = use_match && (cu.compiler_flip_match ^
541 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 std::string::npos));
543 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700544 cu.disable_opt = kCompilerOptimizerDisableFlags;
545 cu.enable_debug = kCompilerDebugFlags;
546 cu.verbose = VLOG_IS_ON(compiler) ||
547 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 }
549
Mingyao Yang42d65c52014-04-18 16:49:39 -0700550 if (gVerboseMethods.size() != 0) {
551 cu.verbose = false;
552 for (size_t i = 0; i < gVerboseMethods.size(); ++i) {
553 if (PrettyMethod(method_idx, dex_file).find(gVerboseMethods[i])
554 != std::string::npos) {
555 cu.verbose = true;
556 break;
557 }
558 }
559 }
560
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 /*
562 * TODO: rework handling of optimization and debug flags. Should we split out
563 * MIR and backend flags? Need command-line setting as well.
564 */
565
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000566 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700568 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700570 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 (1 << kLoadStoreElimination) |
572 (1 << kLoadHoisting) |
573 (1 << kSuppressLoads) |
574 (1 << kNullCheckElimination) |
575 (1 << kPromoteRegs) |
576 (1 << kTrackLiveTemps) |
577 (1 << kSafeOptimizations) |
578 (1 << kBBOpt) |
579 (1 << kMatch) |
580 (1 << kPromoteCompilerTemps));
581 }
582
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100583 if (cu.instruction_set == kArm64) {
584 // TODO(Arm64): enable optimizations once backend is mature enough.
585 cu.disable_opt = ~(uint32_t)0;
586 }
587
buzbeea61f4952013-08-23 14:27:06 -0700588 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700589 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800591 /*
592 * After creation of the MIR graph, also create the code generator.
593 * The reason we do this is that optimizations on the MIR graph may need to get information
594 * that is only available if a CG exists.
595 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000596 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800597
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 /* Gathering opcode stats? */
599 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700600 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 }
602
Dave Allison39c3bfb2014-01-28 18:33:52 -0800603 // Check early if we should skip this compilation if using the profiled filter.
604 if (cu.compiler_driver->ProfilePresent()) {
605 std::string methodname = PrettyMethod(method_idx, dex_file);
606 if (cu.mir_graph->SkipCompilation(methodname)) {
607 return NULL;
608 }
609 }
610
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700612 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 class_loader, dex_file);
614
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100615 // TODO(Arm64): Remove this when we are able to compile everything.
616 if (!CanCompileMethod(method_idx, dex_file, cu)) {
617 VLOG(compiler) << "Cannot compile method : " << PrettyMethod(method_idx, dex_file);
618 return nullptr;
619 }
620
buzbee1da1e2f2013-11-15 13:37:01 -0800621 cu.NewTimingSplit("MIROpt:CheckFilters");
Jeff Hao4a200f52014-04-01 14:58:49 -0700622 if (cu.mir_graph->SkipCompilation()) {
623 return NULL;
buzbeeee17e0a2013-07-31 10:47:37 -0700624 }
625
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800626 /* Create the pass driver and launch it */
Ian Rogers3d504072014-03-01 09:16:49 -0800627 PassDriver pass_driver(&cu);
628 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700630 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
631 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 }
633
634 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700635 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 }
637
buzbee1da1e2f2013-11-15 13:37:01 -0800638 /* Reassociate sreg names with original Dalvik vreg names. */
639 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000641 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
642 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
643 if (cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
644 MemStats stack_stats(cu.arena_stack.GetPeakStats());
645 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
646 }
647 }
648 cu.arena_stack.Reset();
649
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 CompiledMethod* result = NULL;
651
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700652 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653
buzbee1da1e2f2013-11-15 13:37:01 -0800654 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700655 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800656 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657
658 if (result) {
659 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
660 } else {
661 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
662 }
663
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700664 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000665 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000666 MemStats mem_stats(cu.arena.GetMemStats());
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000667 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 }
669 }
670
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700671 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
672 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 << " " << PrettyMethod(method_idx, dex_file);
674 }
675
buzbeea61f4952013-08-23 14:27:06 -0700676 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800677 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 return result;
679}
680
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000681CompiledMethod* CompileOneMethod(CompilerDriver& driver,
682 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 const DexFile::CodeItem* code_item,
684 uint32_t access_flags,
685 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700686 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 uint32_t method_idx,
688 jobject class_loader,
689 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000690 void* compilation_unit) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000691 return CompileMethod(driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000692 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693}
694
695} // namespace art
696
697extern "C" art::CompiledMethod*
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000698 ArtQuickCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 const art::DexFile::CodeItem* code_item,
700 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700701 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 const art::DexFile& dex_file) {
703 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000704 art::Compiler* compiler = driver.GetCompiler();
705 return art::CompileOneMethod(driver, compiler, code_item, access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 class_def_idx, method_idx, class_loader, dex_file,
707 NULL /* use thread llvm_info */);
708}