blob: dc6043dd65a00600e54cdd36639b0720875fc5fc [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) |
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) |
buzbeeb01bf152014-05-13 15:59:07 -070078 // (1 << kDebugCodegenDump) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 0;
80
Vladimir Marko25724ef2013-11-12 15:09:20 +000081CompilationUnit::CompilationUnit(ArenaPool* pool)
Ian Rogers93dcff32014-05-15 09:11:23 -070082 : compiler_driver(nullptr),
83 class_linker(nullptr),
84 dex_file(nullptr),
85 class_loader(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000086 class_def_idx(0),
87 method_idx(0),
Ian Rogers93dcff32014-05-15 09:11:23 -070088 code_item(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000089 access_flags(0),
90 invoke_type(kDirect),
Ian Rogers93dcff32014-05-15 09:11:23 -070091 shorty(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000092 disable_opt(0),
93 enable_debug(0),
94 verbose(false),
Ian Rogers93dcff32014-05-15 09:11:23 -070095 compiler(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000096 instruction_set(kNone),
Ian Rogers93dcff32014-05-15 09:11:23 -070097 target64(false),
Vladimir Marko25724ef2013-11-12 15:09:20 +000098 num_dalvik_registers(0),
Ian Rogers93dcff32014-05-15 09:11:23 -070099 insns(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000100 num_ins(0),
101 num_outs(0),
102 num_regs(0),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000103 compiler_flip_match(false),
104 arena(pool),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000105 arena_stack(pool),
Ian Rogers93dcff32014-05-15 09:11:23 -0700106 mir_graph(nullptr),
107 cg(nullptr),
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700108 timings("QuickCompiler", true, false),
109 print_pass(false) {
Vladimir Marko25724ef2013-11-12 15:09:20 +0000110}
111
112CompilationUnit::~CompilationUnit() {
113}
114
buzbeea61f4952013-08-23 14:27:06 -0700115void CompilationUnit::StartTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000116 if (compiler_driver->GetDumpPasses()) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700117 timings.StartTiming(label);
buzbeea61f4952013-08-23 14:27:06 -0700118 }
119}
120
121void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000122 if (compiler_driver->GetDumpPasses()) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700123 timings.EndTiming();
124 timings.StartTiming(label);
buzbeea61f4952013-08-23 14:27:06 -0700125 }
126}
127
128void CompilationUnit::EndTiming() {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000129 if (compiler_driver->GetDumpPasses()) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700130 timings.EndTiming();
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000131 if (enable_debug & (1 << kDebugTimings)) {
132 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
133 LOG(INFO) << Dumpable<TimingLogger>(timings);
134 }
buzbeea61f4952013-08-23 14:27:06 -0700135 }
136}
137
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100138// TODO: Remove this when we are able to compile everything.
139int arm64_support_list[] = {
140 Instruction::NOP,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100141 Instruction::MOVE,
142 Instruction::MOVE_FROM16,
143 Instruction::MOVE_16,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800144 Instruction::MOVE_WIDE,
145 Instruction::MOVE_WIDE_FROM16,
146 Instruction::MOVE_WIDE_16,
147 Instruction::MOVE_OBJECT,
148 Instruction::MOVE_OBJECT_FROM16,
149 Instruction::MOVE_OBJECT_16,
buzbee54ee4442014-06-19 15:04:12 -0700150 Instruction::MOVE_RESULT,
151 Instruction::MOVE_RESULT_WIDE,
152 Instruction::MOVE_RESULT_OBJECT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100153 Instruction::MOVE_EXCEPTION,
154 Instruction::RETURN_VOID,
155 Instruction::RETURN,
156 Instruction::RETURN_WIDE,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100157 Instruction::RETURN_OBJECT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100158 Instruction::CONST_4,
159 Instruction::CONST_16,
160 Instruction::CONST,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800161 Instruction::CONST_HIGH16,
162 Instruction::CONST_WIDE_16,
163 Instruction::CONST_WIDE_32,
164 Instruction::CONST_WIDE,
165 Instruction::CONST_WIDE_HIGH16,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100166 Instruction::CONST_STRING,
buzbee54ee4442014-06-19 15:04:12 -0700167 Instruction::CONST_STRING_JUMBO,
168 Instruction::CONST_CLASS,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100169 Instruction::MONITOR_ENTER,
170 Instruction::MONITOR_EXIT,
buzbee54ee4442014-06-19 15:04:12 -0700171 Instruction::CHECK_CAST,
172 Instruction::INSTANCE_OF,
173 Instruction::ARRAY_LENGTH,
174 Instruction::NEW_INSTANCE,
175 Instruction::NEW_ARRAY,
176 Instruction::FILLED_NEW_ARRAY,
177 Instruction::FILLED_NEW_ARRAY_RANGE,
178 Instruction::FILL_ARRAY_DATA,
179 Instruction::THROW,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100180 Instruction::GOTO,
181 Instruction::GOTO_16,
182 Instruction::GOTO_32,
Matteo Franchin5acc8b02014-06-05 15:10:35 +0100183 Instruction::PACKED_SWITCH,
184 Instruction::SPARSE_SWITCH,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800185 Instruction::CMPL_FLOAT,
186 Instruction::CMPG_FLOAT,
187 Instruction::CMPL_DOUBLE,
188 Instruction::CMPG_DOUBLE,
189 Instruction::CMP_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100190 Instruction::IF_EQ,
191 Instruction::IF_NE,
192 Instruction::IF_LT,
193 Instruction::IF_GE,
194 Instruction::IF_GT,
195 Instruction::IF_LE,
196 Instruction::IF_EQZ,
197 Instruction::IF_NEZ,
198 Instruction::IF_LTZ,
199 Instruction::IF_GEZ,
200 Instruction::IF_GTZ,
201 Instruction::IF_LEZ,
buzbee54ee4442014-06-19 15:04:12 -0700202 Instruction::UNUSED_3E,
203 Instruction::UNUSED_3F,
204 Instruction::UNUSED_40,
205 Instruction::UNUSED_41,
206 Instruction::UNUSED_42,
207 Instruction::UNUSED_43,
208 Instruction::AGET,
209 Instruction::AGET_WIDE,
210 Instruction::AGET_OBJECT,
211 Instruction::AGET_BOOLEAN,
212 Instruction::AGET_BYTE,
213 Instruction::AGET_CHAR,
214 Instruction::AGET_SHORT,
215 Instruction::APUT,
216 Instruction::APUT_WIDE,
217 Instruction::APUT_OBJECT,
218 Instruction::APUT_BOOLEAN,
219 Instruction::APUT_BYTE,
220 Instruction::APUT_CHAR,
221 Instruction::APUT_SHORT,
222 Instruction::IGET,
223 Instruction::IGET_WIDE,
224 Instruction::IGET_OBJECT,
225 Instruction::IGET_BOOLEAN,
226 Instruction::IGET_BYTE,
227 Instruction::IGET_CHAR,
228 Instruction::IGET_SHORT,
229 Instruction::IPUT,
230 Instruction::IPUT_WIDE,
231 Instruction::IPUT_OBJECT,
232 Instruction::IPUT_BOOLEAN,
233 Instruction::IPUT_BYTE,
234 Instruction::IPUT_CHAR,
235 Instruction::IPUT_SHORT,
236 Instruction::SGET,
237 Instruction::SGET_WIDE,
238 Instruction::SGET_OBJECT,
239 Instruction::SGET_BOOLEAN,
240 Instruction::SGET_BYTE,
241 Instruction::SGET_CHAR,
242 Instruction::SGET_SHORT,
243 Instruction::SPUT,
244 Instruction::SPUT_WIDE,
245 Instruction::SPUT_OBJECT,
246 Instruction::SPUT_BOOLEAN,
247 Instruction::SPUT_BYTE,
248 Instruction::SPUT_CHAR,
249 Instruction::SPUT_SHORT,
250 Instruction::INVOKE_VIRTUAL,
251 Instruction::INVOKE_SUPER,
252 Instruction::INVOKE_DIRECT,
253 Instruction::INVOKE_STATIC,
254 Instruction::INVOKE_INTERFACE,
255 Instruction::RETURN_VOID_BARRIER,
256 Instruction::INVOKE_VIRTUAL_RANGE,
257 Instruction::INVOKE_SUPER_RANGE,
258 Instruction::INVOKE_DIRECT_RANGE,
259 Instruction::INVOKE_STATIC_RANGE,
260 Instruction::INVOKE_INTERFACE_RANGE,
261 Instruction::UNUSED_79,
262 Instruction::UNUSED_7A,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100263 Instruction::NEG_INT,
264 Instruction::NOT_INT,
buzbee54ee4442014-06-19 15:04:12 -0700265 Instruction::NEG_LONG,
266 Instruction::NOT_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100267 Instruction::NEG_FLOAT,
buzbee54ee4442014-06-19 15:04:12 -0700268 Instruction::NEG_DOUBLE,
269 Instruction::INT_TO_LONG,
270 Instruction::INT_TO_FLOAT,
271 Instruction::INT_TO_DOUBLE,
272 Instruction::LONG_TO_INT,
273 Instruction::LONG_TO_FLOAT,
274 Instruction::LONG_TO_DOUBLE,
275 Instruction::FLOAT_TO_INT,
276 Instruction::FLOAT_TO_LONG,
277 Instruction::FLOAT_TO_DOUBLE,
278 Instruction::DOUBLE_TO_INT,
279 Instruction::DOUBLE_TO_LONG,
280 Instruction::DOUBLE_TO_FLOAT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100281 Instruction::INT_TO_BYTE,
282 Instruction::INT_TO_CHAR,
283 Instruction::INT_TO_SHORT,
284 Instruction::ADD_INT,
285 Instruction::SUB_INT,
286 Instruction::MUL_INT,
287 Instruction::DIV_INT,
288 Instruction::REM_INT,
289 Instruction::AND_INT,
290 Instruction::OR_INT,
291 Instruction::XOR_INT,
292 Instruction::SHL_INT,
293 Instruction::SHR_INT,
294 Instruction::USHR_INT,
buzbee54ee4442014-06-19 15:04:12 -0700295 Instruction::ADD_LONG,
296 Instruction::SUB_LONG,
297 Instruction::MUL_LONG,
298 Instruction::DIV_LONG,
299 Instruction::REM_LONG,
300 Instruction::AND_LONG,
301 Instruction::OR_LONG,
302 Instruction::XOR_LONG,
303 Instruction::SHL_LONG,
304 Instruction::SHR_LONG,
305 Instruction::USHR_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100306 Instruction::ADD_FLOAT,
307 Instruction::SUB_FLOAT,
308 Instruction::MUL_FLOAT,
309 Instruction::DIV_FLOAT,
buzbee54ee4442014-06-19 15:04:12 -0700310 Instruction::REM_FLOAT,
311 Instruction::ADD_DOUBLE,
312 Instruction::SUB_DOUBLE,
313 Instruction::MUL_DOUBLE,
314 Instruction::DIV_DOUBLE,
315 Instruction::REM_DOUBLE,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100316 Instruction::ADD_INT_2ADDR,
317 Instruction::SUB_INT_2ADDR,
318 Instruction::MUL_INT_2ADDR,
319 Instruction::DIV_INT_2ADDR,
320 Instruction::REM_INT_2ADDR,
321 Instruction::AND_INT_2ADDR,
322 Instruction::OR_INT_2ADDR,
323 Instruction::XOR_INT_2ADDR,
324 Instruction::SHL_INT_2ADDR,
325 Instruction::SHR_INT_2ADDR,
326 Instruction::USHR_INT_2ADDR,
buzbee54ee4442014-06-19 15:04:12 -0700327 Instruction::ADD_LONG_2ADDR,
328 Instruction::SUB_LONG_2ADDR,
329 Instruction::MUL_LONG_2ADDR,
330 Instruction::DIV_LONG_2ADDR,
331 Instruction::REM_LONG_2ADDR,
332 Instruction::AND_LONG_2ADDR,
333 Instruction::OR_LONG_2ADDR,
334 Instruction::XOR_LONG_2ADDR,
335 Instruction::SHL_LONG_2ADDR,
336 Instruction::SHR_LONG_2ADDR,
337 Instruction::USHR_LONG_2ADDR,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100338 Instruction::ADD_FLOAT_2ADDR,
339 Instruction::SUB_FLOAT_2ADDR,
340 Instruction::MUL_FLOAT_2ADDR,
341 Instruction::DIV_FLOAT_2ADDR,
buzbee54ee4442014-06-19 15:04:12 -0700342 Instruction::REM_FLOAT_2ADDR,
343 Instruction::ADD_DOUBLE_2ADDR,
344 Instruction::SUB_DOUBLE_2ADDR,
345 Instruction::MUL_DOUBLE_2ADDR,
346 Instruction::DIV_DOUBLE_2ADDR,
347 Instruction::REM_DOUBLE_2ADDR,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100348 Instruction::ADD_INT_LIT16,
349 Instruction::RSUB_INT,
350 Instruction::MUL_INT_LIT16,
351 Instruction::DIV_INT_LIT16,
352 Instruction::REM_INT_LIT16,
353 Instruction::AND_INT_LIT16,
354 Instruction::OR_INT_LIT16,
355 Instruction::XOR_INT_LIT16,
356 Instruction::ADD_INT_LIT8,
357 Instruction::RSUB_INT_LIT8,
358 Instruction::MUL_INT_LIT8,
359 Instruction::DIV_INT_LIT8,
360 Instruction::REM_INT_LIT8,
361 Instruction::AND_INT_LIT8,
362 Instruction::OR_INT_LIT8,
363 Instruction::XOR_INT_LIT8,
364 Instruction::SHL_INT_LIT8,
365 Instruction::SHR_INT_LIT8,
366 Instruction::USHR_INT_LIT8,
buzbee54ee4442014-06-19 15:04:12 -0700367 Instruction::IGET_QUICK,
368 Instruction::IGET_WIDE_QUICK,
369 Instruction::IGET_OBJECT_QUICK,
370 Instruction::IPUT_QUICK,
371 Instruction::IPUT_WIDE_QUICK,
372 Instruction::IPUT_OBJECT_QUICK,
373 Instruction::INVOKE_VIRTUAL_QUICK,
374 Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
375 Instruction::UNUSED_EB,
376 Instruction::UNUSED_EC,
377 Instruction::UNUSED_ED,
378 Instruction::UNUSED_EE,
379 Instruction::UNUSED_EF,
380 Instruction::UNUSED_F0,
381 Instruction::UNUSED_F1,
382 Instruction::UNUSED_F2,
383 Instruction::UNUSED_F3,
384 Instruction::UNUSED_F4,
385 Instruction::UNUSED_F5,
386 Instruction::UNUSED_F6,
387 Instruction::UNUSED_F7,
388 Instruction::UNUSED_F8,
389 Instruction::UNUSED_F9,
390 Instruction::UNUSED_FA,
391 Instruction::UNUSED_FB,
392 Instruction::UNUSED_FC,
393 Instruction::UNUSED_FD,
394 Instruction::UNUSED_FE,
395 Instruction::UNUSED_FF,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100396 // ----- ExtendedMIROpcode -----
397 kMirOpPhi,
398 kMirOpCopy,
399 kMirOpFusedCmplFloat,
400 kMirOpFusedCmpgFloat,
401 kMirOpFusedCmplDouble,
402 kMirOpFusedCmpgDouble,
403 kMirOpFusedCmpLong,
404 kMirOpNop,
405 kMirOpNullCheck,
406 kMirOpRangeCheck,
407 kMirOpDivZeroCheck,
408 kMirOpCheck,
409 kMirOpCheckPart2,
410 kMirOpSelect,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100411};
412
413// TODO: Remove this when we are able to compile everything.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700414int x86_64_support_list[] = {
415 Instruction::NOP,
416 // Instruction::MOVE,
417 // Instruction::MOVE_FROM16,
418 // Instruction::MOVE_16,
419 // Instruction::MOVE_WIDE,
420 // Instruction::MOVE_WIDE_FROM16,
421 // Instruction::MOVE_WIDE_16,
422 // Instruction::MOVE_OBJECT,
423 // Instruction::MOVE_OBJECT_FROM16,
424 // Instruction::MOVE_OBJECT_16,
425 // Instruction::MOVE_RESULT,
426 // Instruction::MOVE_RESULT_WIDE,
427 // Instruction::MOVE_RESULT_OBJECT,
428 // Instruction::MOVE_EXCEPTION,
429 Instruction::RETURN_VOID,
430 Instruction::RETURN,
431 // Instruction::RETURN_WIDE,
432 Instruction::RETURN_OBJECT,
433 // Instruction::CONST_4,
434 // Instruction::CONST_16,
435 // Instruction::CONST,
436 // Instruction::CONST_HIGH16,
437 // Instruction::CONST_WIDE_16,
438 // Instruction::CONST_WIDE_32,
439 // Instruction::CONST_WIDE,
440 // Instruction::CONST_WIDE_HIGH16,
441 // Instruction::CONST_STRING,
442 // Instruction::CONST_STRING_JUMBO,
443 // Instruction::CONST_CLASS,
444 // Instruction::MONITOR_ENTER,
445 // Instruction::MONITOR_EXIT,
446 // Instruction::CHECK_CAST,
447 // Instruction::INSTANCE_OF,
448 // Instruction::ARRAY_LENGTH,
449 // Instruction::NEW_INSTANCE,
450 // Instruction::NEW_ARRAY,
451 // Instruction::FILLED_NEW_ARRAY,
452 // Instruction::FILLED_NEW_ARRAY_RANGE,
453 // Instruction::FILL_ARRAY_DATA,
454 // Instruction::THROW,
455 // Instruction::GOTO,
456 // Instruction::GOTO_16,
457 // Instruction::GOTO_32,
458 // Instruction::PACKED_SWITCH,
459 // Instruction::SPARSE_SWITCH,
460 // Instruction::CMPL_FLOAT,
461 // Instruction::CMPG_FLOAT,
462 // Instruction::CMPL_DOUBLE,
463 // Instruction::CMPG_DOUBLE,
464 // Instruction::CMP_LONG,
465 // Instruction::IF_EQ,
466 // Instruction::IF_NE,
467 // Instruction::IF_LT,
468 // Instruction::IF_GE,
469 // Instruction::IF_GT,
470 // Instruction::IF_LE,
471 // Instruction::IF_EQZ,
472 // Instruction::IF_NEZ,
473 // Instruction::IF_LTZ,
474 // Instruction::IF_GEZ,
475 // Instruction::IF_GTZ,
476 // Instruction::IF_LEZ,
477 // Instruction::UNUSED_3E,
478 // Instruction::UNUSED_3F,
479 // Instruction::UNUSED_40,
480 // Instruction::UNUSED_41,
481 // Instruction::UNUSED_42,
482 // Instruction::UNUSED_43,
483 // Instruction::AGET,
484 // Instruction::AGET_WIDE,
485 // Instruction::AGET_OBJECT,
486 // Instruction::AGET_BOOLEAN,
487 // Instruction::AGET_BYTE,
488 // Instruction::AGET_CHAR,
489 // Instruction::AGET_SHORT,
490 // Instruction::APUT,
491 // Instruction::APUT_WIDE,
492 // Instruction::APUT_OBJECT,
493 // Instruction::APUT_BOOLEAN,
494 // Instruction::APUT_BYTE,
495 // Instruction::APUT_CHAR,
496 // Instruction::APUT_SHORT,
497 // Instruction::IGET,
498 // Instruction::IGET_WIDE,
499 // Instruction::IGET_OBJECT,
500 // Instruction::IGET_BOOLEAN,
501 // Instruction::IGET_BYTE,
502 // Instruction::IGET_CHAR,
503 // Instruction::IGET_SHORT,
504 // Instruction::IPUT,
505 // Instruction::IPUT_WIDE,
506 // Instruction::IPUT_OBJECT,
507 // Instruction::IPUT_BOOLEAN,
508 // Instruction::IPUT_BYTE,
509 // Instruction::IPUT_CHAR,
510 // Instruction::IPUT_SHORT,
511 Instruction::SGET,
512 // Instruction::SGET_WIDE,
513 Instruction::SGET_OBJECT,
514 Instruction::SGET_BOOLEAN,
515 Instruction::SGET_BYTE,
516 Instruction::SGET_CHAR,
517 Instruction::SGET_SHORT,
518 Instruction::SPUT,
519 // Instruction::SPUT_WIDE,
520 Instruction::SPUT_OBJECT,
521 Instruction::SPUT_BOOLEAN,
522 Instruction::SPUT_BYTE,
523 Instruction::SPUT_CHAR,
524 Instruction::SPUT_SHORT,
525 Instruction::INVOKE_VIRTUAL,
526 Instruction::INVOKE_SUPER,
527 Instruction::INVOKE_DIRECT,
528 Instruction::INVOKE_STATIC,
529 Instruction::INVOKE_INTERFACE,
530 // Instruction::RETURN_VOID_BARRIER,
531 // Instruction::INVOKE_VIRTUAL_RANGE,
532 // Instruction::INVOKE_SUPER_RANGE,
533 // Instruction::INVOKE_DIRECT_RANGE,
534 // Instruction::INVOKE_STATIC_RANGE,
535 // Instruction::INVOKE_INTERFACE_RANGE,
536 // Instruction::UNUSED_79,
537 // Instruction::UNUSED_7A,
538 // Instruction::NEG_INT,
539 // Instruction::NOT_INT,
540 // Instruction::NEG_LONG,
541 // Instruction::NOT_LONG,
542 // Instruction::NEG_FLOAT,
543 // Instruction::NEG_DOUBLE,
544 // Instruction::INT_TO_LONG,
545 // Instruction::INT_TO_FLOAT,
546 // Instruction::INT_TO_DOUBLE,
547 // Instruction::LONG_TO_INT,
548 // Instruction::LONG_TO_FLOAT,
549 // Instruction::LONG_TO_DOUBLE,
550 // Instruction::FLOAT_TO_INT,
551 // Instruction::FLOAT_TO_LONG,
552 // Instruction::FLOAT_TO_DOUBLE,
553 // Instruction::DOUBLE_TO_INT,
554 // Instruction::DOUBLE_TO_LONG,
555 // Instruction::DOUBLE_TO_FLOAT,
556 // Instruction::INT_TO_BYTE,
557 // Instruction::INT_TO_CHAR,
558 // Instruction::INT_TO_SHORT,
559 // Instruction::ADD_INT,
560 // Instruction::SUB_INT,
561 // Instruction::MUL_INT,
562 // Instruction::DIV_INT,
563 // Instruction::REM_INT,
564 // Instruction::AND_INT,
565 // Instruction::OR_INT,
566 // Instruction::XOR_INT,
567 // Instruction::SHL_INT,
568 // Instruction::SHR_INT,
569 // Instruction::USHR_INT,
570 // Instruction::ADD_LONG,
571 // Instruction::SUB_LONG,
572 // Instruction::MUL_LONG,
573 // Instruction::DIV_LONG,
574 // Instruction::REM_LONG,
575 // Instruction::AND_LONG,
576 // Instruction::OR_LONG,
577 // Instruction::XOR_LONG,
578 // Instruction::SHL_LONG,
579 // Instruction::SHR_LONG,
580 // Instruction::USHR_LONG,
581 // Instruction::ADD_FLOAT,
582 // Instruction::SUB_FLOAT,
583 // Instruction::MUL_FLOAT,
584 // Instruction::DIV_FLOAT,
585 // Instruction::REM_FLOAT,
586 // Instruction::ADD_DOUBLE,
587 // Instruction::SUB_DOUBLE,
588 // Instruction::MUL_DOUBLE,
589 // Instruction::DIV_DOUBLE,
590 // Instruction::REM_DOUBLE,
591 // Instruction::ADD_INT_2ADDR,
592 // Instruction::SUB_INT_2ADDR,
593 // Instruction::MUL_INT_2ADDR,
594 // Instruction::DIV_INT_2ADDR,
595 // Instruction::REM_INT_2ADDR,
596 // Instruction::AND_INT_2ADDR,
597 // Instruction::OR_INT_2ADDR,
598 // Instruction::XOR_INT_2ADDR,
599 // Instruction::SHL_INT_2ADDR,
600 // Instruction::SHR_INT_2ADDR,
601 // Instruction::USHR_INT_2ADDR,
602 // Instruction::ADD_LONG_2ADDR,
603 // Instruction::SUB_LONG_2ADDR,
604 // Instruction::MUL_LONG_2ADDR,
605 // Instruction::DIV_LONG_2ADDR,
606 // Instruction::REM_LONG_2ADDR,
607 // Instruction::AND_LONG_2ADDR,
608 // Instruction::OR_LONG_2ADDR,
609 // Instruction::XOR_LONG_2ADDR,
610 // Instruction::SHL_LONG_2ADDR,
611 // Instruction::SHR_LONG_2ADDR,
612 // Instruction::USHR_LONG_2ADDR,
613 // Instruction::ADD_FLOAT_2ADDR,
614 // Instruction::SUB_FLOAT_2ADDR,
615 // Instruction::MUL_FLOAT_2ADDR,
616 // Instruction::DIV_FLOAT_2ADDR,
617 // Instruction::REM_FLOAT_2ADDR,
618 // Instruction::ADD_DOUBLE_2ADDR,
619 // Instruction::SUB_DOUBLE_2ADDR,
620 // Instruction::MUL_DOUBLE_2ADDR,
621 // Instruction::DIV_DOUBLE_2ADDR,
622 // Instruction::REM_DOUBLE_2ADDR,
623 // Instruction::ADD_INT_LIT16,
624 // Instruction::RSUB_INT,
625 // Instruction::MUL_INT_LIT16,
626 // Instruction::DIV_INT_LIT16,
627 // Instruction::REM_INT_LIT16,
628 // Instruction::AND_INT_LIT16,
629 // Instruction::OR_INT_LIT16,
630 // Instruction::XOR_INT_LIT16,
631 // Instruction::ADD_INT_LIT8,
632 // Instruction::RSUB_INT_LIT8,
633 // Instruction::MUL_INT_LIT8,
634 // Instruction::DIV_INT_LIT8,
635 // Instruction::REM_INT_LIT8,
636 // Instruction::AND_INT_LIT8,
637 // Instruction::OR_INT_LIT8,
638 // Instruction::XOR_INT_LIT8,
639 // Instruction::SHL_INT_LIT8,
640 // Instruction::SHR_INT_LIT8,
641 // Instruction::USHR_INT_LIT8,
642 // Instruction::IGET_QUICK,
643 // Instruction::IGET_WIDE_QUICK,
644 // Instruction::IGET_OBJECT_QUICK,
645 // Instruction::IPUT_QUICK,
646 // Instruction::IPUT_WIDE_QUICK,
647 // Instruction::IPUT_OBJECT_QUICK,
648 // Instruction::INVOKE_VIRTUAL_QUICK,
649 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
650 // Instruction::UNUSED_EB,
651 // Instruction::UNUSED_EC,
652 // Instruction::UNUSED_ED,
653 // Instruction::UNUSED_EE,
654 // Instruction::UNUSED_EF,
655 // Instruction::UNUSED_F0,
656 // Instruction::UNUSED_F1,
657 // Instruction::UNUSED_F2,
658 // Instruction::UNUSED_F3,
659 // Instruction::UNUSED_F4,
660 // Instruction::UNUSED_F5,
661 // Instruction::UNUSED_F6,
662 // Instruction::UNUSED_F7,
663 // Instruction::UNUSED_F8,
664 // Instruction::UNUSED_F9,
665 // Instruction::UNUSED_FA,
666 // Instruction::UNUSED_FB,
667 // Instruction::UNUSED_FC,
668 // Instruction::UNUSED_FD,
669 // Instruction::UNUSED_FE,
670 // Instruction::UNUSED_FF,
671
672 // ----- ExtendedMIROpcode -----
673 // kMirOpPhi,
674 // kMirOpCopy,
675 // kMirOpFusedCmplFloat,
676 // kMirOpFusedCmpgFloat,
677 // kMirOpFusedCmplDouble,
678 // kMirOpFusedCmpgDouble,
679 // kMirOpFusedCmpLong,
680 // kMirOpNop,
681 // kMirOpNullCheck,
682 // kMirOpRangeCheck,
683 // kMirOpDivZeroCheck,
684 // kMirOpCheck,
685 // kMirOpCheckPart2,
686 // kMirOpSelect,
687 // kMirOpLast,
688};
689
690// Z : boolean
691// B : byte
692// S : short
693// C : char
694// I : int
Zheng Xu48241e72014-05-23 11:52:42 +0800695// J : long
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700696// F : float
697// D : double
698// L : reference(object, array)
699// V : void
700// (ARM64) Current calling conversion only support 32bit softfp
701// which has problems with long, float, double
Serban Constantinescu032d3772014-05-23 17:38:18 +0100702constexpr char arm64_supported_types[] = "ZBSCILVJFD";
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700703constexpr char x86_64_supported_types[] = "ZBSCILVJFD";
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700704
705// TODO: Remove this when we are able to compile everything.
706static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100707 uint32_t shorty_size = strlen(shorty);
708 CHECK_GE(shorty_size, 1u);
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700709
buzbee33ae5582014-06-12 14:56:32 -0700710 const char* supported_types =
711 (instruction_set == kX86_64) ? x86_64_supported_types : arm64_supported_types;
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100712 for (uint32_t i = 0; i < shorty_size; i++) {
713 if (strchr(supported_types, shorty[i]) == nullptr) {
714 return false;
715 }
716 }
717 return true;
718};
719
720// TODO: Remove this when we are able to compile everything.
721// Skip the method that we do not support currently.
722static bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
723 CompilationUnit& cu) {
724 // There is some limitation with current ARM 64 backend.
Dmitry Petrochenko136aaee2014-06-06 15:18:14 +0700725 if (cu.instruction_set == kArm64) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100726 // Check if we can compile the prototype.
727 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700728 if (!CanCompileShorty(shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100729 VLOG(compiler) << "Unsupported shorty : " << shorty;
730 return false;
731 }
732
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700733 const int *support_list = arm64_support_list;
734 int support_list_size = arraysize(arm64_support_list);
735 if (cu.instruction_set == kX86_64) {
736 support_list = x86_64_support_list;
737 support_list_size = arraysize(x86_64_support_list);
738 }
739
Andreas Gampe44395962014-06-13 13:44:40 -0700740 for (unsigned int idx = 0; idx < cu.mir_graph->GetNumBlocks(); idx++) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700741 BasicBlock* bb = cu.mir_graph->GetBasicBlock(idx);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100742 if (bb == NULL) continue;
743 if (bb->block_type == kDead) continue;
744 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
745 int opcode = mir->dalvikInsn.opcode;
746 // Check if we support the byte code.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700747 if (std::find(support_list, support_list + support_list_size,
748 opcode) == support_list + support_list_size) {
buzbee35ba7f32014-05-31 08:59:01 -0700749 if (!cu.mir_graph->IsPseudoMirOp(opcode)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100750 VLOG(compiler) << "Unsupported dalvik byte code : "
751 << mir->dalvikInsn.opcode;
752 } else {
753 VLOG(compiler) << "Unsupported extended MIR opcode : "
754 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
755 }
756 return false;
757 }
758 // Check if it invokes a prototype that we cannot support.
759 if (Instruction::INVOKE_VIRTUAL == opcode ||
760 Instruction::INVOKE_SUPER == opcode ||
761 Instruction::INVOKE_DIRECT == opcode ||
762 Instruction::INVOKE_STATIC == opcode ||
763 Instruction::INVOKE_INTERFACE == opcode) {
764 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
765 const char* invoke_method_shorty = dex_file.GetMethodShorty(
766 dex_file.GetMethodId(invoke_method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700767 if (!CanCompileShorty(invoke_method_shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100768 VLOG(compiler) << "Unsupported to invoke '"
769 << PrettyMethod(invoke_method_idx, dex_file)
770 << "' with shorty : " << invoke_method_shorty;
771 return false;
772 }
773 }
774 }
775 }
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100776 }
777 return true;
778}
779
Ian Rogers3d504072014-03-01 09:16:49 -0800780static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000781 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 const DexFile::CodeItem* code_item,
783 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700784 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000785 jobject class_loader, const DexFile& dex_file,
786 void* llvm_compilation_unit) {
Calin Juravle38a09042014-06-23 14:40:31 +0100787 std::string method_name = PrettyMethod(method_idx, dex_file);
788 VLOG(compiler) << "Compiling " << method_name << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700789 if (code_item->insns_size_in_code_units_ >= 0x10000) {
790 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
Calin Juravle38a09042014-06-23 14:40:31 +0100791 << " in " << method_name;
buzbeeb48819d2013-09-14 16:15:25 -0700792 return NULL;
793 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794
Jeff Hao4a200f52014-04-01 14:58:49 -0700795 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000796 return nullptr;
797 }
798
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800800 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801
Ian Rogers3d504072014-03-01 09:16:49 -0800802 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700803 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800804 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -0700805 if (cu.instruction_set == kArm) {
806 cu.instruction_set = kThumb2;
807 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700808 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000809 cu.compiler = compiler;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000810 // TODO: x86_64 & arm64 are not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700811 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100812 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700813 (cu.instruction_set == kX86) ||
814 (cu.instruction_set == kX86_64) ||
815 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700818 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700820 cu.compiler_flip_match = false;
821 bool use_match = !cu.compiler_method_match.empty();
822 bool match = use_match && (cu.compiler_flip_match ^
Calin Juravle38a09042014-06-23 14:40:31 +0100823 (method_name.find(cu.compiler_method_match) != std::string::npos));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700825 cu.disable_opt = kCompilerOptimizerDisableFlags;
826 cu.enable_debug = kCompilerDebugFlags;
827 cu.verbose = VLOG_IS_ON(compiler) ||
828 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 }
830
Mingyao Yang42d65c52014-04-18 16:49:39 -0700831 if (gVerboseMethods.size() != 0) {
832 cu.verbose = false;
833 for (size_t i = 0; i < gVerboseMethods.size(); ++i) {
Calin Juravle38a09042014-06-23 14:40:31 +0100834 if (method_name.find(gVerboseMethods[i])
Mingyao Yang42d65c52014-04-18 16:49:39 -0700835 != std::string::npos) {
836 cu.verbose = true;
837 break;
838 }
839 }
840 }
841
buzbeeb01bf152014-05-13 15:59:07 -0700842 if (cu.verbose) {
843 cu.enable_debug |= (1 << kDebugCodegenDump);
844 }
845
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 /*
847 * TODO: rework handling of optimization and debug flags. Should we split out
848 * MIR and backend flags? Need command-line setting as well.
849 */
850
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000851 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700853 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700855 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 (1 << kLoadStoreElimination) |
857 (1 << kLoadHoisting) |
858 (1 << kSuppressLoads) |
859 (1 << kNullCheckElimination) |
860 (1 << kPromoteRegs) |
861 (1 << kTrackLiveTemps) |
862 (1 << kSafeOptimizations) |
863 (1 << kBBOpt) |
864 (1 << kMatch) |
865 (1 << kPromoteCompilerTemps));
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700866 } else if (cu.instruction_set == kX86_64) {
Dmitry Petrochenkoba279d92014-05-16 18:36:39 +0700867 // TODO(X86_64): enable optimizations once backend is mature enough.
Dmitry Petrochenko699c04a2014-06-28 10:59:19 +0700868 cu.disable_opt |= (1 << kLoadStoreElimination);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700869 } else if (cu.instruction_set == kArm64) {
870 // TODO(Arm64): enable optimizations once backend is mature enough.
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100871 cu.disable_opt = ~((1 << kSuppressMethodInlining) |
Matteo Franchinf1013192014-07-07 13:35:14 +0100872 (1 << kNullCheckElimination) |
873 (1 << kPromoteRegs));
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100874 }
875
buzbeea61f4952013-08-23 14:27:06 -0700876 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700877 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800879 /*
880 * After creation of the MIR graph, also create the code generator.
881 * The reason we do this is that optimizations on the MIR graph may need to get information
882 * that is only available if a CG exists.
883 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000884 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800885
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 /* Gathering opcode stats? */
887 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700888 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889 }
890
891 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700892 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 class_loader, dex_file);
894
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100895 // TODO(Arm64): Remove this when we are able to compile everything.
896 if (!CanCompileMethod(method_idx, dex_file, cu)) {
Calin Juravle38a09042014-06-23 14:40:31 +0100897 VLOG(compiler) << cu.instruction_set << ": Cannot compile method : " << method_name;
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100898 return nullptr;
899 }
900
buzbee1da1e2f2013-11-15 13:37:01 -0800901 cu.NewTimingSplit("MIROpt:CheckFilters");
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700902 std::string skip_message;
903 if (cu.mir_graph->SkipCompilation(&skip_message)) {
904 VLOG(compiler) << cu.instruction_set << ": Skipping method : "
Calin Juravle38a09042014-06-23 14:40:31 +0100905 << method_name << " Reason = " << skip_message;
buzbee33ae5582014-06-12 14:56:32 -0700906 return nullptr;
buzbeeee17e0a2013-07-31 10:47:37 -0700907 }
908
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800909 /* Create the pass driver and launch it */
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700910 PassDriverMEOpts pass_driver(&cu);
Ian Rogers3d504072014-03-01 09:16:49 -0800911 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912
Calin Juravle38a09042014-06-23 14:40:31 +0100913 /* For non-leaf methods check if we should skip compilation when the profiler is enabled. */
914 if (cu.compiler_driver->ProfilePresent()
915 && !cu.mir_graph->MethodIsLeaf()
916 && cu.mir_graph->SkipCompilationByName(method_name)) {
917 return nullptr;
918 }
919
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700920 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
921 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 }
923
924 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700925 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 }
927
buzbee1da1e2f2013-11-15 13:37:01 -0800928 /* Reassociate sreg names with original Dalvik vreg names. */
929 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700930
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000931 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
932 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
933 if (cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
934 MemStats stack_stats(cu.arena_stack.GetPeakStats());
Calin Juravle38a09042014-06-23 14:40:31 +0100935 LOG(INFO) << method_name << " " << Dumpable<MemStats>(stack_stats);
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000936 }
937 }
938 cu.arena_stack.Reset();
939
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 CompiledMethod* result = NULL;
941
buzbee8c7a02a2014-06-14 12:33:09 -0700942 if (cu.mir_graph->PuntToInterpreter()) {
Calin Juravle38a09042014-06-23 14:40:31 +0100943 VLOG(compiler) << cu.instruction_set << ": Punted method to interpreter: " << method_name;
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700944 return nullptr;
buzbee8c7a02a2014-06-14 12:33:09 -0700945 }
946
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700947 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948
buzbee1da1e2f2013-11-15 13:37:01 -0800949 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700950 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800951 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700952
953 if (result) {
Calin Juravle38a09042014-06-23 14:40:31 +0100954 VLOG(compiler) << cu.instruction_set << ": Compiled " << method_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955 } else {
Calin Juravle38a09042014-06-23 14:40:31 +0100956 VLOG(compiler) << cu.instruction_set << ": Deferred " << method_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 }
958
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700959 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000960 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000961 MemStats mem_stats(cu.arena.GetMemStats());
Calin Juravle38a09042014-06-23 14:40:31 +0100962 LOG(INFO) << method_name << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 }
964 }
965
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700966 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
967 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Calin Juravle38a09042014-06-23 14:40:31 +0100968 << " " << method_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 }
970
buzbeea61f4952013-08-23 14:27:06 -0700971 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800972 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973 return result;
974}
975
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000976CompiledMethod* CompileOneMethod(CompilerDriver& driver,
977 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 const DexFile::CodeItem* code_item,
979 uint32_t access_flags,
980 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700981 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 uint32_t method_idx,
983 jobject class_loader,
984 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000985 void* compilation_unit) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000986 return CompileMethod(driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000987 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988}
989
990} // namespace art
991
992extern "C" art::CompiledMethod*
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000993 ArtQuickCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 const art::DexFile::CodeItem* code_item,
995 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700996 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997 const art::DexFile& dex_file) {
998 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000999 art::Compiler* compiler = driver.GetCompiler();
1000 return art::CompileOneMethod(driver, compiler, code_item, access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 class_def_idx, method_idx, class_loader, dex_file,
1002 NULL /* use thread llvm_info */);
1003}