blob: c1457fc71e70815ae0b11360c83b42bf5fe973d9 [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 Beyler2469e602014-05-06 20:36:55 -070024#include "pass_driver_me_opts.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) |
Vladimir Marko95a05972014-05-30 10:01:32 +010048 // (1 << kGlobalValueNumbering) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070049 // (1 << kPromoteRegs) |
buzbee30adc732014-05-09 15:10:18 -070050 // (1 << kTrackLiveTemps) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070051 // (1 << kSafeOptimizations) |
52 // (1 << kBBOpt) |
53 // (1 << kMatch) |
54 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080055 // (1 << kSuppressExceptionEdges) |
Vladimir Marko9820b7c2014-01-02 16:40:37 +000056 // (1 << kSuppressMethodInlining) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 0;
58
59static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070060 // (1 << kDebugDisplayMissingTargets) |
61 // (1 << kDebugVerbose) |
62 // (1 << kDebugDumpCFG) |
63 // (1 << kDebugSlowFieldPath) |
64 // (1 << kDebugSlowInvokePath) |
65 // (1 << kDebugSlowStringPath) |
66 // (1 << kDebugSlowestFieldPath) |
67 // (1 << kDebugSlowestStringPath) |
68 // (1 << kDebugExerciseResolveMethod) |
69 // (1 << kDebugVerifyDataflow) |
70 // (1 << kDebugShowMemoryUsage) |
71 // (1 << kDebugShowNops) |
72 // (1 << kDebugCountOpcodes) |
73 // (1 << kDebugDumpCheckStats) |
74 // (1 << kDebugDumpBitcodeFile) |
75 // (1 << kDebugVerifyBitcode) |
76 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -070077 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -070078 // (1 << kDebugTimings) |
buzbeeb01bf152014-05-13 15:59:07 -070079 // (1 << kDebugCodegenDump) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 0;
81
Vladimir Marko25724ef2013-11-12 15:09:20 +000082CompilationUnit::CompilationUnit(ArenaPool* pool)
Ian Rogers93dcff32014-05-15 09:11:23 -070083 : compiler_driver(nullptr),
84 class_linker(nullptr),
85 dex_file(nullptr),
86 class_loader(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000087 class_def_idx(0),
88 method_idx(0),
Ian Rogers93dcff32014-05-15 09:11:23 -070089 code_item(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000090 access_flags(0),
91 invoke_type(kDirect),
Ian Rogers93dcff32014-05-15 09:11:23 -070092 shorty(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000093 disable_opt(0),
94 enable_debug(0),
95 verbose(false),
Ian Rogers93dcff32014-05-15 09:11:23 -070096 compiler(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000097 instruction_set(kNone),
Ian Rogers93dcff32014-05-15 09:11:23 -070098 target64(false),
Vladimir Marko25724ef2013-11-12 15:09:20 +000099 num_dalvik_registers(0),
Ian Rogers93dcff32014-05-15 09:11:23 -0700100 insns(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000101 num_ins(0),
102 num_outs(0),
103 num_regs(0),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000104 compiler_flip_match(false),
105 arena(pool),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000106 arena_stack(pool),
Ian Rogers93dcff32014-05-15 09:11:23 -0700107 mir_graph(nullptr),
108 cg(nullptr),
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700109 timings("QuickCompiler", true, false),
110 print_pass(false) {
Vladimir Marko25724ef2013-11-12 15:09:20 +0000111}
112
113CompilationUnit::~CompilationUnit() {
114}
115
buzbeea61f4952013-08-23 14:27:06 -0700116void CompilationUnit::StartTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000117 if (compiler_driver->GetDumpPasses()) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700118 timings.StartTiming(label);
buzbeea61f4952013-08-23 14:27:06 -0700119 }
120}
121
122void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000123 if (compiler_driver->GetDumpPasses()) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700124 timings.EndTiming();
125 timings.StartTiming(label);
buzbeea61f4952013-08-23 14:27:06 -0700126 }
127}
128
129void CompilationUnit::EndTiming() {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000130 if (compiler_driver->GetDumpPasses()) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700131 timings.EndTiming();
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000132 if (enable_debug & (1 << kDebugTimings)) {
133 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
134 LOG(INFO) << Dumpable<TimingLogger>(timings);
135 }
buzbeea61f4952013-08-23 14:27:06 -0700136 }
137}
138
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100139// TODO: Remove this when we are able to compile everything.
140int arm64_support_list[] = {
141 Instruction::NOP,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100142 Instruction::MOVE,
143 Instruction::MOVE_FROM16,
144 Instruction::MOVE_16,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800145 Instruction::MOVE_WIDE,
146 Instruction::MOVE_WIDE_FROM16,
147 Instruction::MOVE_WIDE_16,
148 Instruction::MOVE_OBJECT,
149 Instruction::MOVE_OBJECT_FROM16,
150 Instruction::MOVE_OBJECT_16,
buzbee54ee4442014-06-19 15:04:12 -0700151 Instruction::MOVE_RESULT,
152 Instruction::MOVE_RESULT_WIDE,
153 Instruction::MOVE_RESULT_OBJECT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100154 Instruction::MOVE_EXCEPTION,
155 Instruction::RETURN_VOID,
156 Instruction::RETURN,
157 Instruction::RETURN_WIDE,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100158 Instruction::RETURN_OBJECT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100159 Instruction::CONST_4,
160 Instruction::CONST_16,
161 Instruction::CONST,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800162 Instruction::CONST_HIGH16,
163 Instruction::CONST_WIDE_16,
164 Instruction::CONST_WIDE_32,
165 Instruction::CONST_WIDE,
166 Instruction::CONST_WIDE_HIGH16,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100167 Instruction::CONST_STRING,
buzbee54ee4442014-06-19 15:04:12 -0700168 Instruction::CONST_STRING_JUMBO,
169 Instruction::CONST_CLASS,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100170 Instruction::MONITOR_ENTER,
171 Instruction::MONITOR_EXIT,
buzbee54ee4442014-06-19 15:04:12 -0700172 Instruction::CHECK_CAST,
173 Instruction::INSTANCE_OF,
174 Instruction::ARRAY_LENGTH,
175 Instruction::NEW_INSTANCE,
176 Instruction::NEW_ARRAY,
177 Instruction::FILLED_NEW_ARRAY,
178 Instruction::FILLED_NEW_ARRAY_RANGE,
179 Instruction::FILL_ARRAY_DATA,
180 Instruction::THROW,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100181 Instruction::GOTO,
182 Instruction::GOTO_16,
183 Instruction::GOTO_32,
Matteo Franchin5acc8b02014-06-05 15:10:35 +0100184 Instruction::PACKED_SWITCH,
185 Instruction::SPARSE_SWITCH,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800186 Instruction::CMPL_FLOAT,
187 Instruction::CMPG_FLOAT,
188 Instruction::CMPL_DOUBLE,
189 Instruction::CMPG_DOUBLE,
190 Instruction::CMP_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100191 Instruction::IF_EQ,
192 Instruction::IF_NE,
193 Instruction::IF_LT,
194 Instruction::IF_GE,
195 Instruction::IF_GT,
196 Instruction::IF_LE,
197 Instruction::IF_EQZ,
198 Instruction::IF_NEZ,
199 Instruction::IF_LTZ,
200 Instruction::IF_GEZ,
201 Instruction::IF_GTZ,
202 Instruction::IF_LEZ,
buzbee54ee4442014-06-19 15:04:12 -0700203 Instruction::UNUSED_3E,
204 Instruction::UNUSED_3F,
205 Instruction::UNUSED_40,
206 Instruction::UNUSED_41,
207 Instruction::UNUSED_42,
208 Instruction::UNUSED_43,
209 Instruction::AGET,
210 Instruction::AGET_WIDE,
211 Instruction::AGET_OBJECT,
212 Instruction::AGET_BOOLEAN,
213 Instruction::AGET_BYTE,
214 Instruction::AGET_CHAR,
215 Instruction::AGET_SHORT,
216 Instruction::APUT,
217 Instruction::APUT_WIDE,
218 Instruction::APUT_OBJECT,
219 Instruction::APUT_BOOLEAN,
220 Instruction::APUT_BYTE,
221 Instruction::APUT_CHAR,
222 Instruction::APUT_SHORT,
223 Instruction::IGET,
224 Instruction::IGET_WIDE,
225 Instruction::IGET_OBJECT,
226 Instruction::IGET_BOOLEAN,
227 Instruction::IGET_BYTE,
228 Instruction::IGET_CHAR,
229 Instruction::IGET_SHORT,
230 Instruction::IPUT,
231 Instruction::IPUT_WIDE,
232 Instruction::IPUT_OBJECT,
233 Instruction::IPUT_BOOLEAN,
234 Instruction::IPUT_BYTE,
235 Instruction::IPUT_CHAR,
236 Instruction::IPUT_SHORT,
237 Instruction::SGET,
238 Instruction::SGET_WIDE,
239 Instruction::SGET_OBJECT,
240 Instruction::SGET_BOOLEAN,
241 Instruction::SGET_BYTE,
242 Instruction::SGET_CHAR,
243 Instruction::SGET_SHORT,
244 Instruction::SPUT,
245 Instruction::SPUT_WIDE,
246 Instruction::SPUT_OBJECT,
247 Instruction::SPUT_BOOLEAN,
248 Instruction::SPUT_BYTE,
249 Instruction::SPUT_CHAR,
250 Instruction::SPUT_SHORT,
251 Instruction::INVOKE_VIRTUAL,
252 Instruction::INVOKE_SUPER,
253 Instruction::INVOKE_DIRECT,
254 Instruction::INVOKE_STATIC,
255 Instruction::INVOKE_INTERFACE,
256 Instruction::RETURN_VOID_BARRIER,
257 Instruction::INVOKE_VIRTUAL_RANGE,
258 Instruction::INVOKE_SUPER_RANGE,
259 Instruction::INVOKE_DIRECT_RANGE,
260 Instruction::INVOKE_STATIC_RANGE,
261 Instruction::INVOKE_INTERFACE_RANGE,
262 Instruction::UNUSED_79,
263 Instruction::UNUSED_7A,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100264 Instruction::NEG_INT,
265 Instruction::NOT_INT,
buzbee54ee4442014-06-19 15:04:12 -0700266 Instruction::NEG_LONG,
267 Instruction::NOT_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100268 Instruction::NEG_FLOAT,
buzbee54ee4442014-06-19 15:04:12 -0700269 Instruction::NEG_DOUBLE,
270 Instruction::INT_TO_LONG,
271 Instruction::INT_TO_FLOAT,
272 Instruction::INT_TO_DOUBLE,
273 Instruction::LONG_TO_INT,
274 Instruction::LONG_TO_FLOAT,
275 Instruction::LONG_TO_DOUBLE,
276 Instruction::FLOAT_TO_INT,
277 Instruction::FLOAT_TO_LONG,
278 Instruction::FLOAT_TO_DOUBLE,
279 Instruction::DOUBLE_TO_INT,
280 Instruction::DOUBLE_TO_LONG,
281 Instruction::DOUBLE_TO_FLOAT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100282 Instruction::INT_TO_BYTE,
283 Instruction::INT_TO_CHAR,
284 Instruction::INT_TO_SHORT,
285 Instruction::ADD_INT,
286 Instruction::SUB_INT,
287 Instruction::MUL_INT,
288 Instruction::DIV_INT,
289 Instruction::REM_INT,
290 Instruction::AND_INT,
291 Instruction::OR_INT,
292 Instruction::XOR_INT,
293 Instruction::SHL_INT,
294 Instruction::SHR_INT,
295 Instruction::USHR_INT,
buzbee54ee4442014-06-19 15:04:12 -0700296 Instruction::ADD_LONG,
297 Instruction::SUB_LONG,
298 Instruction::MUL_LONG,
299 Instruction::DIV_LONG,
300 Instruction::REM_LONG,
301 Instruction::AND_LONG,
302 Instruction::OR_LONG,
303 Instruction::XOR_LONG,
304 Instruction::SHL_LONG,
305 Instruction::SHR_LONG,
306 Instruction::USHR_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100307 Instruction::ADD_FLOAT,
308 Instruction::SUB_FLOAT,
309 Instruction::MUL_FLOAT,
310 Instruction::DIV_FLOAT,
buzbee54ee4442014-06-19 15:04:12 -0700311 Instruction::REM_FLOAT,
312 Instruction::ADD_DOUBLE,
313 Instruction::SUB_DOUBLE,
314 Instruction::MUL_DOUBLE,
315 Instruction::DIV_DOUBLE,
316 Instruction::REM_DOUBLE,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100317 Instruction::ADD_INT_2ADDR,
318 Instruction::SUB_INT_2ADDR,
319 Instruction::MUL_INT_2ADDR,
320 Instruction::DIV_INT_2ADDR,
321 Instruction::REM_INT_2ADDR,
322 Instruction::AND_INT_2ADDR,
323 Instruction::OR_INT_2ADDR,
324 Instruction::XOR_INT_2ADDR,
325 Instruction::SHL_INT_2ADDR,
326 Instruction::SHR_INT_2ADDR,
327 Instruction::USHR_INT_2ADDR,
buzbee54ee4442014-06-19 15:04:12 -0700328 Instruction::ADD_LONG_2ADDR,
329 Instruction::SUB_LONG_2ADDR,
330 Instruction::MUL_LONG_2ADDR,
331 Instruction::DIV_LONG_2ADDR,
332 Instruction::REM_LONG_2ADDR,
333 Instruction::AND_LONG_2ADDR,
334 Instruction::OR_LONG_2ADDR,
335 Instruction::XOR_LONG_2ADDR,
336 Instruction::SHL_LONG_2ADDR,
337 Instruction::SHR_LONG_2ADDR,
338 Instruction::USHR_LONG_2ADDR,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100339 Instruction::ADD_FLOAT_2ADDR,
340 Instruction::SUB_FLOAT_2ADDR,
341 Instruction::MUL_FLOAT_2ADDR,
342 Instruction::DIV_FLOAT_2ADDR,
buzbee54ee4442014-06-19 15:04:12 -0700343 Instruction::REM_FLOAT_2ADDR,
344 Instruction::ADD_DOUBLE_2ADDR,
345 Instruction::SUB_DOUBLE_2ADDR,
346 Instruction::MUL_DOUBLE_2ADDR,
347 Instruction::DIV_DOUBLE_2ADDR,
348 Instruction::REM_DOUBLE_2ADDR,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100349 Instruction::ADD_INT_LIT16,
350 Instruction::RSUB_INT,
351 Instruction::MUL_INT_LIT16,
352 Instruction::DIV_INT_LIT16,
353 Instruction::REM_INT_LIT16,
354 Instruction::AND_INT_LIT16,
355 Instruction::OR_INT_LIT16,
356 Instruction::XOR_INT_LIT16,
357 Instruction::ADD_INT_LIT8,
358 Instruction::RSUB_INT_LIT8,
359 Instruction::MUL_INT_LIT8,
360 Instruction::DIV_INT_LIT8,
361 Instruction::REM_INT_LIT8,
362 Instruction::AND_INT_LIT8,
363 Instruction::OR_INT_LIT8,
364 Instruction::XOR_INT_LIT8,
365 Instruction::SHL_INT_LIT8,
366 Instruction::SHR_INT_LIT8,
367 Instruction::USHR_INT_LIT8,
buzbee54ee4442014-06-19 15:04:12 -0700368 Instruction::IGET_QUICK,
369 Instruction::IGET_WIDE_QUICK,
370 Instruction::IGET_OBJECT_QUICK,
371 Instruction::IPUT_QUICK,
372 Instruction::IPUT_WIDE_QUICK,
373 Instruction::IPUT_OBJECT_QUICK,
374 Instruction::INVOKE_VIRTUAL_QUICK,
375 Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
376 Instruction::UNUSED_EB,
377 Instruction::UNUSED_EC,
378 Instruction::UNUSED_ED,
379 Instruction::UNUSED_EE,
380 Instruction::UNUSED_EF,
381 Instruction::UNUSED_F0,
382 Instruction::UNUSED_F1,
383 Instruction::UNUSED_F2,
384 Instruction::UNUSED_F3,
385 Instruction::UNUSED_F4,
386 Instruction::UNUSED_F5,
387 Instruction::UNUSED_F6,
388 Instruction::UNUSED_F7,
389 Instruction::UNUSED_F8,
390 Instruction::UNUSED_F9,
391 Instruction::UNUSED_FA,
392 Instruction::UNUSED_FB,
393 Instruction::UNUSED_FC,
394 Instruction::UNUSED_FD,
395 Instruction::UNUSED_FE,
396 Instruction::UNUSED_FF,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100397 // ----- ExtendedMIROpcode -----
398 kMirOpPhi,
399 kMirOpCopy,
400 kMirOpFusedCmplFloat,
401 kMirOpFusedCmpgFloat,
402 kMirOpFusedCmplDouble,
403 kMirOpFusedCmpgDouble,
404 kMirOpFusedCmpLong,
405 kMirOpNop,
406 kMirOpNullCheck,
407 kMirOpRangeCheck,
408 kMirOpDivZeroCheck,
409 kMirOpCheck,
410 kMirOpCheckPart2,
411 kMirOpSelect,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100412};
413
414// TODO: Remove this when we are able to compile everything.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700415int x86_64_support_list[] = {
416 Instruction::NOP,
417 // Instruction::MOVE,
418 // Instruction::MOVE_FROM16,
419 // Instruction::MOVE_16,
420 // Instruction::MOVE_WIDE,
421 // Instruction::MOVE_WIDE_FROM16,
422 // Instruction::MOVE_WIDE_16,
423 // Instruction::MOVE_OBJECT,
424 // Instruction::MOVE_OBJECT_FROM16,
425 // Instruction::MOVE_OBJECT_16,
426 // Instruction::MOVE_RESULT,
427 // Instruction::MOVE_RESULT_WIDE,
428 // Instruction::MOVE_RESULT_OBJECT,
429 // Instruction::MOVE_EXCEPTION,
430 Instruction::RETURN_VOID,
431 Instruction::RETURN,
432 // Instruction::RETURN_WIDE,
433 Instruction::RETURN_OBJECT,
434 // Instruction::CONST_4,
435 // Instruction::CONST_16,
436 // Instruction::CONST,
437 // Instruction::CONST_HIGH16,
438 // Instruction::CONST_WIDE_16,
439 // Instruction::CONST_WIDE_32,
440 // Instruction::CONST_WIDE,
441 // Instruction::CONST_WIDE_HIGH16,
442 // Instruction::CONST_STRING,
443 // Instruction::CONST_STRING_JUMBO,
444 // Instruction::CONST_CLASS,
445 // Instruction::MONITOR_ENTER,
446 // Instruction::MONITOR_EXIT,
447 // Instruction::CHECK_CAST,
448 // Instruction::INSTANCE_OF,
449 // Instruction::ARRAY_LENGTH,
450 // Instruction::NEW_INSTANCE,
451 // Instruction::NEW_ARRAY,
452 // Instruction::FILLED_NEW_ARRAY,
453 // Instruction::FILLED_NEW_ARRAY_RANGE,
454 // Instruction::FILL_ARRAY_DATA,
455 // Instruction::THROW,
456 // Instruction::GOTO,
457 // Instruction::GOTO_16,
458 // Instruction::GOTO_32,
459 // Instruction::PACKED_SWITCH,
460 // Instruction::SPARSE_SWITCH,
461 // Instruction::CMPL_FLOAT,
462 // Instruction::CMPG_FLOAT,
463 // Instruction::CMPL_DOUBLE,
464 // Instruction::CMPG_DOUBLE,
465 // Instruction::CMP_LONG,
466 // Instruction::IF_EQ,
467 // Instruction::IF_NE,
468 // Instruction::IF_LT,
469 // Instruction::IF_GE,
470 // Instruction::IF_GT,
471 // Instruction::IF_LE,
472 // Instruction::IF_EQZ,
473 // Instruction::IF_NEZ,
474 // Instruction::IF_LTZ,
475 // Instruction::IF_GEZ,
476 // Instruction::IF_GTZ,
477 // Instruction::IF_LEZ,
478 // Instruction::UNUSED_3E,
479 // Instruction::UNUSED_3F,
480 // Instruction::UNUSED_40,
481 // Instruction::UNUSED_41,
482 // Instruction::UNUSED_42,
483 // Instruction::UNUSED_43,
484 // Instruction::AGET,
485 // Instruction::AGET_WIDE,
486 // Instruction::AGET_OBJECT,
487 // Instruction::AGET_BOOLEAN,
488 // Instruction::AGET_BYTE,
489 // Instruction::AGET_CHAR,
490 // Instruction::AGET_SHORT,
491 // Instruction::APUT,
492 // Instruction::APUT_WIDE,
493 // Instruction::APUT_OBJECT,
494 // Instruction::APUT_BOOLEAN,
495 // Instruction::APUT_BYTE,
496 // Instruction::APUT_CHAR,
497 // Instruction::APUT_SHORT,
498 // Instruction::IGET,
499 // Instruction::IGET_WIDE,
500 // Instruction::IGET_OBJECT,
501 // Instruction::IGET_BOOLEAN,
502 // Instruction::IGET_BYTE,
503 // Instruction::IGET_CHAR,
504 // Instruction::IGET_SHORT,
505 // Instruction::IPUT,
506 // Instruction::IPUT_WIDE,
507 // Instruction::IPUT_OBJECT,
508 // Instruction::IPUT_BOOLEAN,
509 // Instruction::IPUT_BYTE,
510 // Instruction::IPUT_CHAR,
511 // Instruction::IPUT_SHORT,
512 Instruction::SGET,
513 // Instruction::SGET_WIDE,
514 Instruction::SGET_OBJECT,
515 Instruction::SGET_BOOLEAN,
516 Instruction::SGET_BYTE,
517 Instruction::SGET_CHAR,
518 Instruction::SGET_SHORT,
519 Instruction::SPUT,
520 // Instruction::SPUT_WIDE,
521 Instruction::SPUT_OBJECT,
522 Instruction::SPUT_BOOLEAN,
523 Instruction::SPUT_BYTE,
524 Instruction::SPUT_CHAR,
525 Instruction::SPUT_SHORT,
526 Instruction::INVOKE_VIRTUAL,
527 Instruction::INVOKE_SUPER,
528 Instruction::INVOKE_DIRECT,
529 Instruction::INVOKE_STATIC,
530 Instruction::INVOKE_INTERFACE,
531 // Instruction::RETURN_VOID_BARRIER,
532 // Instruction::INVOKE_VIRTUAL_RANGE,
533 // Instruction::INVOKE_SUPER_RANGE,
534 // Instruction::INVOKE_DIRECT_RANGE,
535 // Instruction::INVOKE_STATIC_RANGE,
536 // Instruction::INVOKE_INTERFACE_RANGE,
537 // Instruction::UNUSED_79,
538 // Instruction::UNUSED_7A,
539 // Instruction::NEG_INT,
540 // Instruction::NOT_INT,
541 // Instruction::NEG_LONG,
542 // Instruction::NOT_LONG,
543 // Instruction::NEG_FLOAT,
544 // Instruction::NEG_DOUBLE,
545 // Instruction::INT_TO_LONG,
546 // Instruction::INT_TO_FLOAT,
547 // Instruction::INT_TO_DOUBLE,
548 // Instruction::LONG_TO_INT,
549 // Instruction::LONG_TO_FLOAT,
550 // Instruction::LONG_TO_DOUBLE,
551 // Instruction::FLOAT_TO_INT,
552 // Instruction::FLOAT_TO_LONG,
553 // Instruction::FLOAT_TO_DOUBLE,
554 // Instruction::DOUBLE_TO_INT,
555 // Instruction::DOUBLE_TO_LONG,
556 // Instruction::DOUBLE_TO_FLOAT,
557 // Instruction::INT_TO_BYTE,
558 // Instruction::INT_TO_CHAR,
559 // Instruction::INT_TO_SHORT,
560 // Instruction::ADD_INT,
561 // Instruction::SUB_INT,
562 // Instruction::MUL_INT,
563 // Instruction::DIV_INT,
564 // Instruction::REM_INT,
565 // Instruction::AND_INT,
566 // Instruction::OR_INT,
567 // Instruction::XOR_INT,
568 // Instruction::SHL_INT,
569 // Instruction::SHR_INT,
570 // Instruction::USHR_INT,
571 // Instruction::ADD_LONG,
572 // Instruction::SUB_LONG,
573 // Instruction::MUL_LONG,
574 // Instruction::DIV_LONG,
575 // Instruction::REM_LONG,
576 // Instruction::AND_LONG,
577 // Instruction::OR_LONG,
578 // Instruction::XOR_LONG,
579 // Instruction::SHL_LONG,
580 // Instruction::SHR_LONG,
581 // Instruction::USHR_LONG,
582 // Instruction::ADD_FLOAT,
583 // Instruction::SUB_FLOAT,
584 // Instruction::MUL_FLOAT,
585 // Instruction::DIV_FLOAT,
586 // Instruction::REM_FLOAT,
587 // Instruction::ADD_DOUBLE,
588 // Instruction::SUB_DOUBLE,
589 // Instruction::MUL_DOUBLE,
590 // Instruction::DIV_DOUBLE,
591 // Instruction::REM_DOUBLE,
592 // Instruction::ADD_INT_2ADDR,
593 // Instruction::SUB_INT_2ADDR,
594 // Instruction::MUL_INT_2ADDR,
595 // Instruction::DIV_INT_2ADDR,
596 // Instruction::REM_INT_2ADDR,
597 // Instruction::AND_INT_2ADDR,
598 // Instruction::OR_INT_2ADDR,
599 // Instruction::XOR_INT_2ADDR,
600 // Instruction::SHL_INT_2ADDR,
601 // Instruction::SHR_INT_2ADDR,
602 // Instruction::USHR_INT_2ADDR,
603 // Instruction::ADD_LONG_2ADDR,
604 // Instruction::SUB_LONG_2ADDR,
605 // Instruction::MUL_LONG_2ADDR,
606 // Instruction::DIV_LONG_2ADDR,
607 // Instruction::REM_LONG_2ADDR,
608 // Instruction::AND_LONG_2ADDR,
609 // Instruction::OR_LONG_2ADDR,
610 // Instruction::XOR_LONG_2ADDR,
611 // Instruction::SHL_LONG_2ADDR,
612 // Instruction::SHR_LONG_2ADDR,
613 // Instruction::USHR_LONG_2ADDR,
614 // Instruction::ADD_FLOAT_2ADDR,
615 // Instruction::SUB_FLOAT_2ADDR,
616 // Instruction::MUL_FLOAT_2ADDR,
617 // Instruction::DIV_FLOAT_2ADDR,
618 // Instruction::REM_FLOAT_2ADDR,
619 // Instruction::ADD_DOUBLE_2ADDR,
620 // Instruction::SUB_DOUBLE_2ADDR,
621 // Instruction::MUL_DOUBLE_2ADDR,
622 // Instruction::DIV_DOUBLE_2ADDR,
623 // Instruction::REM_DOUBLE_2ADDR,
624 // Instruction::ADD_INT_LIT16,
625 // Instruction::RSUB_INT,
626 // Instruction::MUL_INT_LIT16,
627 // Instruction::DIV_INT_LIT16,
628 // Instruction::REM_INT_LIT16,
629 // Instruction::AND_INT_LIT16,
630 // Instruction::OR_INT_LIT16,
631 // Instruction::XOR_INT_LIT16,
632 // Instruction::ADD_INT_LIT8,
633 // Instruction::RSUB_INT_LIT8,
634 // Instruction::MUL_INT_LIT8,
635 // Instruction::DIV_INT_LIT8,
636 // Instruction::REM_INT_LIT8,
637 // Instruction::AND_INT_LIT8,
638 // Instruction::OR_INT_LIT8,
639 // Instruction::XOR_INT_LIT8,
640 // Instruction::SHL_INT_LIT8,
641 // Instruction::SHR_INT_LIT8,
642 // Instruction::USHR_INT_LIT8,
643 // Instruction::IGET_QUICK,
644 // Instruction::IGET_WIDE_QUICK,
645 // Instruction::IGET_OBJECT_QUICK,
646 // Instruction::IPUT_QUICK,
647 // Instruction::IPUT_WIDE_QUICK,
648 // Instruction::IPUT_OBJECT_QUICK,
649 // Instruction::INVOKE_VIRTUAL_QUICK,
650 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
651 // Instruction::UNUSED_EB,
652 // Instruction::UNUSED_EC,
653 // Instruction::UNUSED_ED,
654 // Instruction::UNUSED_EE,
655 // Instruction::UNUSED_EF,
656 // Instruction::UNUSED_F0,
657 // Instruction::UNUSED_F1,
658 // Instruction::UNUSED_F2,
659 // Instruction::UNUSED_F3,
660 // Instruction::UNUSED_F4,
661 // Instruction::UNUSED_F5,
662 // Instruction::UNUSED_F6,
663 // Instruction::UNUSED_F7,
664 // Instruction::UNUSED_F8,
665 // Instruction::UNUSED_F9,
666 // Instruction::UNUSED_FA,
667 // Instruction::UNUSED_FB,
668 // Instruction::UNUSED_FC,
669 // Instruction::UNUSED_FD,
670 // Instruction::UNUSED_FE,
671 // Instruction::UNUSED_FF,
672
673 // ----- ExtendedMIROpcode -----
674 // kMirOpPhi,
675 // kMirOpCopy,
676 // kMirOpFusedCmplFloat,
677 // kMirOpFusedCmpgFloat,
678 // kMirOpFusedCmplDouble,
679 // kMirOpFusedCmpgDouble,
680 // kMirOpFusedCmpLong,
681 // kMirOpNop,
682 // kMirOpNullCheck,
683 // kMirOpRangeCheck,
684 // kMirOpDivZeroCheck,
685 // kMirOpCheck,
686 // kMirOpCheckPart2,
687 // kMirOpSelect,
688 // kMirOpLast,
689};
690
691// Z : boolean
692// B : byte
693// S : short
694// C : char
695// I : int
Zheng Xu48241e72014-05-23 11:52:42 +0800696// J : long
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700697// F : float
698// D : double
699// L : reference(object, array)
700// V : void
701// (ARM64) Current calling conversion only support 32bit softfp
702// which has problems with long, float, double
Serban Constantinescu032d3772014-05-23 17:38:18 +0100703constexpr char arm64_supported_types[] = "ZBSCILVJFD";
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700704constexpr char x86_64_supported_types[] = "ZBSCILVJFD";
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700705
706// TODO: Remove this when we are able to compile everything.
707static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100708 uint32_t shorty_size = strlen(shorty);
709 CHECK_GE(shorty_size, 1u);
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700710
buzbee33ae5582014-06-12 14:56:32 -0700711 const char* supported_types =
712 (instruction_set == kX86_64) ? x86_64_supported_types : arm64_supported_types;
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100713 for (uint32_t i = 0; i < shorty_size; i++) {
714 if (strchr(supported_types, shorty[i]) == nullptr) {
715 return false;
716 }
717 }
718 return true;
719};
720
721// TODO: Remove this when we are able to compile everything.
722// Skip the method that we do not support currently.
723static bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
724 CompilationUnit& cu) {
725 // There is some limitation with current ARM 64 backend.
Dmitry Petrochenko136aaee2014-06-06 15:18:14 +0700726 if (cu.instruction_set == kArm64) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100727 // Check if we can compile the prototype.
728 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700729 if (!CanCompileShorty(shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100730 VLOG(compiler) << "Unsupported shorty : " << shorty;
731 return false;
732 }
733
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700734 const int *support_list = arm64_support_list;
735 int support_list_size = arraysize(arm64_support_list);
736 if (cu.instruction_set == kX86_64) {
737 support_list = x86_64_support_list;
738 support_list_size = arraysize(x86_64_support_list);
739 }
740
Andreas Gampe44395962014-06-13 13:44:40 -0700741 for (unsigned int idx = 0; idx < cu.mir_graph->GetNumBlocks(); idx++) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700742 BasicBlock* bb = cu.mir_graph->GetBasicBlock(idx);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100743 if (bb == NULL) continue;
744 if (bb->block_type == kDead) continue;
745 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
746 int opcode = mir->dalvikInsn.opcode;
747 // Check if we support the byte code.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700748 if (std::find(support_list, support_list + support_list_size,
749 opcode) == support_list + support_list_size) {
buzbee35ba7f32014-05-31 08:59:01 -0700750 if (!cu.mir_graph->IsPseudoMirOp(opcode)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100751 VLOG(compiler) << "Unsupported dalvik byte code : "
752 << mir->dalvikInsn.opcode;
753 } else {
754 VLOG(compiler) << "Unsupported extended MIR opcode : "
755 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
756 }
757 return false;
758 }
759 // Check if it invokes a prototype that we cannot support.
760 if (Instruction::INVOKE_VIRTUAL == opcode ||
761 Instruction::INVOKE_SUPER == opcode ||
762 Instruction::INVOKE_DIRECT == opcode ||
763 Instruction::INVOKE_STATIC == opcode ||
764 Instruction::INVOKE_INTERFACE == opcode) {
765 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
766 const char* invoke_method_shorty = dex_file.GetMethodShorty(
767 dex_file.GetMethodId(invoke_method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700768 if (!CanCompileShorty(invoke_method_shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100769 VLOG(compiler) << "Unsupported to invoke '"
770 << PrettyMethod(invoke_method_idx, dex_file)
771 << "' with shorty : " << invoke_method_shorty;
772 return false;
773 }
774 }
775 }
776 }
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100777 }
778 return true;
779}
780
Ian Rogers3d504072014-03-01 09:16:49 -0800781static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000782 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783 const DexFile::CodeItem* code_item,
784 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700785 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000786 jobject class_loader, const DexFile& dex_file,
787 void* llvm_compilation_unit) {
Calin Juravle38a09042014-06-23 14:40:31 +0100788 std::string method_name = PrettyMethod(method_idx, dex_file);
789 VLOG(compiler) << "Compiling " << method_name << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700790 if (code_item->insns_size_in_code_units_ >= 0x10000) {
791 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
Calin Juravle38a09042014-06-23 14:40:31 +0100792 << " in " << method_name;
buzbeeb48819d2013-09-14 16:15:25 -0700793 return NULL;
794 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795
Jeff Hao4a200f52014-04-01 14:58:49 -0700796 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000797 return nullptr;
798 }
799
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800801 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802
Ian Rogers3d504072014-03-01 09:16:49 -0800803 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700804 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800805 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -0700806 if (cu.instruction_set == kArm) {
807 cu.instruction_set = kThumb2;
808 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700809 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000810 cu.compiler = compiler;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000811 // TODO: x86_64 & arm64 are not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700812 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100813 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700814 (cu.instruction_set == kX86) ||
815 (cu.instruction_set == kX86_64) ||
816 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700819 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700821 cu.compiler_flip_match = false;
822 bool use_match = !cu.compiler_method_match.empty();
823 bool match = use_match && (cu.compiler_flip_match ^
Calin Juravle38a09042014-06-23 14:40:31 +0100824 (method_name.find(cu.compiler_method_match) != std::string::npos));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700826 cu.disable_opt = kCompilerOptimizerDisableFlags;
827 cu.enable_debug = kCompilerDebugFlags;
828 cu.verbose = VLOG_IS_ON(compiler) ||
829 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 }
831
Mingyao Yang42d65c52014-04-18 16:49:39 -0700832 if (gVerboseMethods.size() != 0) {
833 cu.verbose = false;
834 for (size_t i = 0; i < gVerboseMethods.size(); ++i) {
Calin Juravle38a09042014-06-23 14:40:31 +0100835 if (method_name.find(gVerboseMethods[i])
Mingyao Yang42d65c52014-04-18 16:49:39 -0700836 != std::string::npos) {
837 cu.verbose = true;
838 break;
839 }
840 }
841 }
842
buzbeeb01bf152014-05-13 15:59:07 -0700843 if (cu.verbose) {
844 cu.enable_debug |= (1 << kDebugCodegenDump);
845 }
846
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 /*
848 * TODO: rework handling of optimization and debug flags. Should we split out
849 * MIR and backend flags? Need command-line setting as well.
850 */
851
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000852 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700854 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700856 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700857 (1 << kLoadStoreElimination) |
858 (1 << kLoadHoisting) |
859 (1 << kSuppressLoads) |
860 (1 << kNullCheckElimination) |
861 (1 << kPromoteRegs) |
862 (1 << kTrackLiveTemps) |
863 (1 << kSafeOptimizations) |
864 (1 << kBBOpt) |
865 (1 << kMatch) |
866 (1 << kPromoteCompilerTemps));
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700867 } else if (cu.instruction_set == kX86_64) {
Dmitry Petrochenkoba279d92014-05-16 18:36:39 +0700868 // TODO(X86_64): enable optimizations once backend is mature enough.
Dmitry Petrochenko699c04a2014-06-28 10:59:19 +0700869 cu.disable_opt |= (1 << kLoadStoreElimination);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700870 } else if (cu.instruction_set == kArm64) {
871 // TODO(Arm64): enable optimizations once backend is mature enough.
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100872 cu.disable_opt = ~((1 << kSuppressMethodInlining) |
Matteo Franchinf1013192014-07-07 13:35:14 +0100873 (1 << kNullCheckElimination) |
874 (1 << kPromoteRegs));
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100875 }
876
buzbeea61f4952013-08-23 14:27:06 -0700877 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700878 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800880 /*
881 * After creation of the MIR graph, also create the code generator.
882 * The reason we do this is that optimizations on the MIR graph may need to get information
883 * that is only available if a CG exists.
884 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000885 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800886
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887 /* Gathering opcode stats? */
888 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700889 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700890 }
891
892 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700893 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700894 class_loader, dex_file);
895
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100896 // TODO(Arm64): Remove this when we are able to compile everything.
897 if (!CanCompileMethod(method_idx, dex_file, cu)) {
Calin Juravle38a09042014-06-23 14:40:31 +0100898 VLOG(compiler) << cu.instruction_set << ": Cannot compile method : " << method_name;
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100899 return nullptr;
900 }
901
buzbee1da1e2f2013-11-15 13:37:01 -0800902 cu.NewTimingSplit("MIROpt:CheckFilters");
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700903 std::string skip_message;
904 if (cu.mir_graph->SkipCompilation(&skip_message)) {
905 VLOG(compiler) << cu.instruction_set << ": Skipping method : "
Calin Juravle38a09042014-06-23 14:40:31 +0100906 << method_name << " Reason = " << skip_message;
buzbee33ae5582014-06-12 14:56:32 -0700907 return nullptr;
buzbeeee17e0a2013-07-31 10:47:37 -0700908 }
909
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800910 /* Create the pass driver and launch it */
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700911 PassDriverMEOpts pass_driver(&cu);
Ian Rogers3d504072014-03-01 09:16:49 -0800912 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913
Calin Juravle38a09042014-06-23 14:40:31 +0100914 /* For non-leaf methods check if we should skip compilation when the profiler is enabled. */
915 if (cu.compiler_driver->ProfilePresent()
916 && !cu.mir_graph->MethodIsLeaf()
917 && cu.mir_graph->SkipCompilationByName(method_name)) {
918 return nullptr;
919 }
920
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700921 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
922 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923 }
924
925 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700926 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 }
928
buzbee1da1e2f2013-11-15 13:37:01 -0800929 /* Reassociate sreg names with original Dalvik vreg names. */
930 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000932 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
933 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
934 if (cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
935 MemStats stack_stats(cu.arena_stack.GetPeakStats());
Calin Juravle38a09042014-06-23 14:40:31 +0100936 LOG(INFO) << method_name << " " << Dumpable<MemStats>(stack_stats);
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000937 }
938 }
939 cu.arena_stack.Reset();
940
Brian Carlstrom7940e442013-07-12 13:46:57 -0700941 CompiledMethod* result = NULL;
942
buzbee8c7a02a2014-06-14 12:33:09 -0700943 if (cu.mir_graph->PuntToInterpreter()) {
Calin Juravle38a09042014-06-23 14:40:31 +0100944 VLOG(compiler) << cu.instruction_set << ": Punted method to interpreter: " << method_name;
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700945 return nullptr;
buzbee8c7a02a2014-06-14 12:33:09 -0700946 }
947
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700948 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949
buzbee1da1e2f2013-11-15 13:37:01 -0800950 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700951 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800952 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953
954 if (result) {
Calin Juravle38a09042014-06-23 14:40:31 +0100955 VLOG(compiler) << cu.instruction_set << ": Compiled " << method_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 } else {
Calin Juravle38a09042014-06-23 14:40:31 +0100957 VLOG(compiler) << cu.instruction_set << ": Deferred " << method_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 }
959
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700960 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000961 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000962 MemStats mem_stats(cu.arena.GetMemStats());
Calin Juravle38a09042014-06-23 14:40:31 +0100963 LOG(INFO) << method_name << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 }
965 }
966
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700967 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
968 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Calin Juravle38a09042014-06-23 14:40:31 +0100969 << " " << method_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 }
971
buzbeea61f4952013-08-23 14:27:06 -0700972 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800973 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 return result;
975}
976
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000977CompiledMethod* CompileOneMethod(CompilerDriver& driver,
978 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 const DexFile::CodeItem* code_item,
980 uint32_t access_flags,
981 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700982 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 uint32_t method_idx,
984 jobject class_loader,
985 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000986 void* compilation_unit) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000987 return CompileMethod(driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000988 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989}
990
991} // namespace art
992
993extern "C" art::CompiledMethod*
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000994 ArtQuickCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 const art::DexFile::CodeItem* code_item,
996 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700997 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 const art::DexFile& dex_file) {
999 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001000 art::Compiler* compiler = driver.GetCompiler();
1001 return art::CompileOneMethod(driver, compiler, code_item, access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 class_def_idx, method_idx, class_loader, dex_file,
1003 NULL /* use thread llvm_info */);
1004}