blob: aa50b458c45b2de16ceb590a67181fc8c042fad3 [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()) {
buzbeea61f4952013-08-23 14:27:06 -0700117 timings.StartSplit(label);
118 }
119}
120
121void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000122 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700123 timings.NewSplit(label);
124 }
125}
126
127void CompilationUnit::EndTiming() {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000128 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700129 timings.EndSplit();
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000130 if (enable_debug & (1 << kDebugTimings)) {
131 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
132 LOG(INFO) << Dumpable<TimingLogger>(timings);
133 }
buzbeea61f4952013-08-23 14:27:06 -0700134 }
135}
136
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100137// Enable opcodes that mostly work, but produce assertion errors (thus breaking libartd.so).
138#define ARM64_USE_EXPERIMENTAL_OPCODES 0
139
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100140// TODO: Remove this when we are able to compile everything.
141int arm64_support_list[] = {
142 Instruction::NOP,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100143 Instruction::MOVE,
144 Instruction::MOVE_FROM16,
145 Instruction::MOVE_16,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800146 Instruction::MOVE_WIDE,
147 Instruction::MOVE_WIDE_FROM16,
148 Instruction::MOVE_WIDE_16,
149 Instruction::MOVE_OBJECT,
150 Instruction::MOVE_OBJECT_FROM16,
151 Instruction::MOVE_OBJECT_16,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100152 Instruction::MOVE_EXCEPTION,
153 Instruction::RETURN_VOID,
154 Instruction::RETURN,
155 Instruction::RETURN_WIDE,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100156 Instruction::RETURN_OBJECT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100157 Instruction::CONST_4,
158 Instruction::CONST_16,
159 Instruction::CONST,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800160 Instruction::CONST_HIGH16,
161 Instruction::CONST_WIDE_16,
162 Instruction::CONST_WIDE_32,
163 Instruction::CONST_WIDE,
164 Instruction::CONST_WIDE_HIGH16,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100165 Instruction::CONST_STRING,
166 Instruction::MONITOR_ENTER,
167 Instruction::MONITOR_EXIT,
168 Instruction::THROW,
169 Instruction::GOTO,
170 Instruction::GOTO_16,
171 Instruction::GOTO_32,
Matteo Franchin5acc8b02014-06-05 15:10:35 +0100172 Instruction::PACKED_SWITCH,
173 Instruction::SPARSE_SWITCH,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800174 Instruction::CMPL_FLOAT,
175 Instruction::CMPG_FLOAT,
176 Instruction::CMPL_DOUBLE,
177 Instruction::CMPG_DOUBLE,
178 Instruction::CMP_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100179 Instruction::IF_EQ,
180 Instruction::IF_NE,
181 Instruction::IF_LT,
182 Instruction::IF_GE,
183 Instruction::IF_GT,
184 Instruction::IF_LE,
185 Instruction::IF_EQZ,
186 Instruction::IF_NEZ,
187 Instruction::IF_LTZ,
188 Instruction::IF_GEZ,
189 Instruction::IF_GTZ,
190 Instruction::IF_LEZ,
191 Instruction::NEG_INT,
192 Instruction::NOT_INT,
193 Instruction::NEG_FLOAT,
194 Instruction::INT_TO_BYTE,
195 Instruction::INT_TO_CHAR,
196 Instruction::INT_TO_SHORT,
197 Instruction::ADD_INT,
198 Instruction::SUB_INT,
199 Instruction::MUL_INT,
200 Instruction::DIV_INT,
201 Instruction::REM_INT,
202 Instruction::AND_INT,
203 Instruction::OR_INT,
204 Instruction::XOR_INT,
205 Instruction::SHL_INT,
206 Instruction::SHR_INT,
207 Instruction::USHR_INT,
208 Instruction::ADD_FLOAT,
209 Instruction::SUB_FLOAT,
210 Instruction::MUL_FLOAT,
211 Instruction::DIV_FLOAT,
212 Instruction::ADD_INT_2ADDR,
213 Instruction::SUB_INT_2ADDR,
214 Instruction::MUL_INT_2ADDR,
215 Instruction::DIV_INT_2ADDR,
216 Instruction::REM_INT_2ADDR,
217 Instruction::AND_INT_2ADDR,
218 Instruction::OR_INT_2ADDR,
219 Instruction::XOR_INT_2ADDR,
220 Instruction::SHL_INT_2ADDR,
221 Instruction::SHR_INT_2ADDR,
222 Instruction::USHR_INT_2ADDR,
223 Instruction::ADD_FLOAT_2ADDR,
224 Instruction::SUB_FLOAT_2ADDR,
225 Instruction::MUL_FLOAT_2ADDR,
226 Instruction::DIV_FLOAT_2ADDR,
227 Instruction::ADD_INT_LIT16,
228 Instruction::RSUB_INT,
229 Instruction::MUL_INT_LIT16,
230 Instruction::DIV_INT_LIT16,
231 Instruction::REM_INT_LIT16,
232 Instruction::AND_INT_LIT16,
233 Instruction::OR_INT_LIT16,
234 Instruction::XOR_INT_LIT16,
235 Instruction::ADD_INT_LIT8,
236 Instruction::RSUB_INT_LIT8,
237 Instruction::MUL_INT_LIT8,
238 Instruction::DIV_INT_LIT8,
239 Instruction::REM_INT_LIT8,
240 Instruction::AND_INT_LIT8,
241 Instruction::OR_INT_LIT8,
242 Instruction::XOR_INT_LIT8,
243 Instruction::SHL_INT_LIT8,
244 Instruction::SHR_INT_LIT8,
245 Instruction::USHR_INT_LIT8,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100246 Instruction::SGET,
247 Instruction::SGET_BOOLEAN,
248 Instruction::SGET_BYTE,
249 Instruction::SGET_CHAR,
250 Instruction::SGET_SHORT,
251 Instruction::SGET_OBJECT,
252 Instruction::SPUT,
253 Instruction::SPUT_OBJECT,
254 Instruction::SPUT_BOOLEAN,
255 Instruction::SPUT_BYTE,
256 Instruction::SPUT_CHAR,
257 Instruction::SPUT_SHORT,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100258 Instruction::CMPL_FLOAT,
259 Instruction::CMPG_FLOAT,
260 Instruction::IGET,
261 Instruction::IGET_OBJECT,
262 Instruction::IGET_BOOLEAN,
263 Instruction::IGET_BYTE,
264 Instruction::IGET_CHAR,
265 Instruction::IGET_SHORT,
266 Instruction::IPUT,
267 Instruction::IPUT_OBJECT,
268 Instruction::IPUT_BOOLEAN,
269 Instruction::IPUT_BYTE,
270 Instruction::IPUT_CHAR,
271 Instruction::IPUT_SHORT,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800272 Instruction::NEG_LONG,
273 Instruction::NOT_LONG,
274 Instruction::NEG_DOUBLE,
275 Instruction::INT_TO_LONG,
276 Instruction::INT_TO_FLOAT,
277 Instruction::INT_TO_DOUBLE,
278 Instruction::LONG_TO_INT,
279 Instruction::LONG_TO_FLOAT,
280 Instruction::LONG_TO_DOUBLE,
281 Instruction::FLOAT_TO_INT,
282 Instruction::FLOAT_TO_LONG,
283 Instruction::FLOAT_TO_DOUBLE,
284 Instruction::DOUBLE_TO_INT,
285 Instruction::DOUBLE_TO_LONG,
286 Instruction::DOUBLE_TO_FLOAT,
287 Instruction::ADD_LONG,
288 Instruction::SUB_LONG,
289 Instruction::MUL_LONG,
290 Instruction::DIV_LONG,
291 Instruction::REM_LONG,
292 Instruction::AND_LONG,
293 Instruction::OR_LONG,
294 Instruction::XOR_LONG,
295 Instruction::SHL_LONG,
296 Instruction::SHR_LONG,
297 Instruction::USHR_LONG,
298 Instruction::REM_FLOAT,
299 Instruction::ADD_DOUBLE,
300 Instruction::SUB_DOUBLE,
301 Instruction::MUL_DOUBLE,
302 Instruction::DIV_DOUBLE,
303 Instruction::REM_DOUBLE,
304 Instruction::ADD_LONG_2ADDR,
305 Instruction::SUB_LONG_2ADDR,
306 Instruction::MUL_LONG_2ADDR,
307 Instruction::DIV_LONG_2ADDR,
308 Instruction::REM_LONG_2ADDR,
309 Instruction::AND_LONG_2ADDR,
310 Instruction::OR_LONG_2ADDR,
311 Instruction::XOR_LONG_2ADDR,
312 Instruction::SHL_LONG_2ADDR,
313 Instruction::SHR_LONG_2ADDR,
314 Instruction::USHR_LONG_2ADDR,
315 Instruction::REM_FLOAT_2ADDR,
316 Instruction::ADD_DOUBLE_2ADDR,
317 Instruction::SUB_DOUBLE_2ADDR,
318 Instruction::MUL_DOUBLE_2ADDR,
319 Instruction::DIV_DOUBLE_2ADDR,
320 Instruction::REM_DOUBLE_2ADDR,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100321 // TODO(Arm64): Enable compiler pass
322 // ----- ExtendedMIROpcode -----
323 kMirOpPhi,
324 kMirOpCopy,
325 kMirOpFusedCmplFloat,
326 kMirOpFusedCmpgFloat,
327 kMirOpFusedCmplDouble,
328 kMirOpFusedCmpgDouble,
329 kMirOpFusedCmpLong,
330 kMirOpNop,
331 kMirOpNullCheck,
332 kMirOpRangeCheck,
333 kMirOpDivZeroCheck,
334 kMirOpCheck,
335 kMirOpCheckPart2,
336 kMirOpSelect,
337
338#if ARM64_USE_EXPERIMENTAL_OPCODES
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100339 // Instruction::MOVE_RESULT,
340 // Instruction::MOVE_RESULT_WIDE,
341 // Instruction::MOVE_RESULT_OBJECT,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100342 // Instruction::CONST_STRING_JUMBO,
343 // Instruction::CONST_CLASS,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100344 // Instruction::CHECK_CAST,
345 // Instruction::INSTANCE_OF,
346 // Instruction::ARRAY_LENGTH,
347 // Instruction::NEW_INSTANCE,
348 // Instruction::NEW_ARRAY,
349 // Instruction::FILLED_NEW_ARRAY,
350 // Instruction::FILLED_NEW_ARRAY_RANGE,
351 // Instruction::FILL_ARRAY_DATA,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100352 // Instruction::UNUSED_3E,
353 // Instruction::UNUSED_3F,
354 // Instruction::UNUSED_40,
355 // Instruction::UNUSED_41,
356 // Instruction::UNUSED_42,
357 // Instruction::UNUSED_43,
358 // Instruction::AGET,
359 // Instruction::AGET_WIDE,
360 // Instruction::AGET_OBJECT,
361 // Instruction::AGET_BOOLEAN,
362 // Instruction::AGET_BYTE,
363 // Instruction::AGET_CHAR,
364 // Instruction::AGET_SHORT,
365 // Instruction::APUT,
366 // Instruction::APUT_WIDE,
367 // Instruction::APUT_OBJECT,
368 // Instruction::APUT_BOOLEAN,
369 // Instruction::APUT_BYTE,
370 // Instruction::APUT_CHAR,
371 // Instruction::APUT_SHORT,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100372 // Instruction::IPUT_WIDE,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100373 // Instruction::IGET_WIDE,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100374 // Instruction::SGET_WIDE,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100375 // Instruction::SPUT_WIDE,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100376 Instruction::INVOKE_VIRTUAL,
377 Instruction::INVOKE_SUPER,
378 Instruction::INVOKE_DIRECT,
379 Instruction::INVOKE_STATIC,
380 Instruction::INVOKE_INTERFACE,
381 // Instruction::RETURN_VOID_BARRIER,
382 // Instruction::INVOKE_VIRTUAL_RANGE,
383 // Instruction::INVOKE_SUPER_RANGE,
384 // Instruction::INVOKE_DIRECT_RANGE,
385 // Instruction::INVOKE_STATIC_RANGE,
386 // Instruction::INVOKE_INTERFACE_RANGE,
387 // Instruction::UNUSED_79,
388 // Instruction::UNUSED_7A,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100389 // Instruction::IGET_QUICK,
390 // Instruction::IGET_WIDE_QUICK,
391 // Instruction::IGET_OBJECT_QUICK,
392 // Instruction::IPUT_QUICK,
393 // Instruction::IPUT_WIDE_QUICK,
394 // Instruction::IPUT_OBJECT_QUICK,
395 // Instruction::INVOKE_VIRTUAL_QUICK,
396 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
397 // Instruction::UNUSED_EB,
398 // Instruction::UNUSED_EC,
399 // Instruction::UNUSED_ED,
400 // Instruction::UNUSED_EE,
401 // Instruction::UNUSED_EF,
402 // Instruction::UNUSED_F0,
403 // Instruction::UNUSED_F1,
404 // Instruction::UNUSED_F2,
405 // Instruction::UNUSED_F3,
406 // Instruction::UNUSED_F4,
407 // Instruction::UNUSED_F5,
408 // Instruction::UNUSED_F6,
409 // Instruction::UNUSED_F7,
410 // Instruction::UNUSED_F8,
411 // Instruction::UNUSED_F9,
412 // Instruction::UNUSED_FA,
413 // Instruction::UNUSED_FB,
414 // Instruction::UNUSED_FC,
415 // Instruction::UNUSED_FD,
416 // Instruction::UNUSED_FE,
417 // Instruction::UNUSED_FF,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100418#endif /* ARM64_USE_EXPERIMENTAL_OPCODES */
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100419};
420
421// TODO: Remove this when we are able to compile everything.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700422int x86_64_support_list[] = {
423 Instruction::NOP,
424 // Instruction::MOVE,
425 // Instruction::MOVE_FROM16,
426 // Instruction::MOVE_16,
427 // Instruction::MOVE_WIDE,
428 // Instruction::MOVE_WIDE_FROM16,
429 // Instruction::MOVE_WIDE_16,
430 // Instruction::MOVE_OBJECT,
431 // Instruction::MOVE_OBJECT_FROM16,
432 // Instruction::MOVE_OBJECT_16,
433 // Instruction::MOVE_RESULT,
434 // Instruction::MOVE_RESULT_WIDE,
435 // Instruction::MOVE_RESULT_OBJECT,
436 // Instruction::MOVE_EXCEPTION,
437 Instruction::RETURN_VOID,
438 Instruction::RETURN,
439 // Instruction::RETURN_WIDE,
440 Instruction::RETURN_OBJECT,
441 // Instruction::CONST_4,
442 // Instruction::CONST_16,
443 // Instruction::CONST,
444 // Instruction::CONST_HIGH16,
445 // Instruction::CONST_WIDE_16,
446 // Instruction::CONST_WIDE_32,
447 // Instruction::CONST_WIDE,
448 // Instruction::CONST_WIDE_HIGH16,
449 // Instruction::CONST_STRING,
450 // Instruction::CONST_STRING_JUMBO,
451 // Instruction::CONST_CLASS,
452 // Instruction::MONITOR_ENTER,
453 // Instruction::MONITOR_EXIT,
454 // Instruction::CHECK_CAST,
455 // Instruction::INSTANCE_OF,
456 // Instruction::ARRAY_LENGTH,
457 // Instruction::NEW_INSTANCE,
458 // Instruction::NEW_ARRAY,
459 // Instruction::FILLED_NEW_ARRAY,
460 // Instruction::FILLED_NEW_ARRAY_RANGE,
461 // Instruction::FILL_ARRAY_DATA,
462 // Instruction::THROW,
463 // Instruction::GOTO,
464 // Instruction::GOTO_16,
465 // Instruction::GOTO_32,
466 // Instruction::PACKED_SWITCH,
467 // Instruction::SPARSE_SWITCH,
468 // Instruction::CMPL_FLOAT,
469 // Instruction::CMPG_FLOAT,
470 // Instruction::CMPL_DOUBLE,
471 // Instruction::CMPG_DOUBLE,
472 // Instruction::CMP_LONG,
473 // Instruction::IF_EQ,
474 // Instruction::IF_NE,
475 // Instruction::IF_LT,
476 // Instruction::IF_GE,
477 // Instruction::IF_GT,
478 // Instruction::IF_LE,
479 // Instruction::IF_EQZ,
480 // Instruction::IF_NEZ,
481 // Instruction::IF_LTZ,
482 // Instruction::IF_GEZ,
483 // Instruction::IF_GTZ,
484 // Instruction::IF_LEZ,
485 // Instruction::UNUSED_3E,
486 // Instruction::UNUSED_3F,
487 // Instruction::UNUSED_40,
488 // Instruction::UNUSED_41,
489 // Instruction::UNUSED_42,
490 // Instruction::UNUSED_43,
491 // Instruction::AGET,
492 // Instruction::AGET_WIDE,
493 // Instruction::AGET_OBJECT,
494 // Instruction::AGET_BOOLEAN,
495 // Instruction::AGET_BYTE,
496 // Instruction::AGET_CHAR,
497 // Instruction::AGET_SHORT,
498 // Instruction::APUT,
499 // Instruction::APUT_WIDE,
500 // Instruction::APUT_OBJECT,
501 // Instruction::APUT_BOOLEAN,
502 // Instruction::APUT_BYTE,
503 // Instruction::APUT_CHAR,
504 // Instruction::APUT_SHORT,
505 // Instruction::IGET,
506 // Instruction::IGET_WIDE,
507 // Instruction::IGET_OBJECT,
508 // Instruction::IGET_BOOLEAN,
509 // Instruction::IGET_BYTE,
510 // Instruction::IGET_CHAR,
511 // Instruction::IGET_SHORT,
512 // Instruction::IPUT,
513 // Instruction::IPUT_WIDE,
514 // Instruction::IPUT_OBJECT,
515 // Instruction::IPUT_BOOLEAN,
516 // Instruction::IPUT_BYTE,
517 // Instruction::IPUT_CHAR,
518 // Instruction::IPUT_SHORT,
519 Instruction::SGET,
520 // Instruction::SGET_WIDE,
521 Instruction::SGET_OBJECT,
522 Instruction::SGET_BOOLEAN,
523 Instruction::SGET_BYTE,
524 Instruction::SGET_CHAR,
525 Instruction::SGET_SHORT,
526 Instruction::SPUT,
527 // Instruction::SPUT_WIDE,
528 Instruction::SPUT_OBJECT,
529 Instruction::SPUT_BOOLEAN,
530 Instruction::SPUT_BYTE,
531 Instruction::SPUT_CHAR,
532 Instruction::SPUT_SHORT,
533 Instruction::INVOKE_VIRTUAL,
534 Instruction::INVOKE_SUPER,
535 Instruction::INVOKE_DIRECT,
536 Instruction::INVOKE_STATIC,
537 Instruction::INVOKE_INTERFACE,
538 // Instruction::RETURN_VOID_BARRIER,
539 // Instruction::INVOKE_VIRTUAL_RANGE,
540 // Instruction::INVOKE_SUPER_RANGE,
541 // Instruction::INVOKE_DIRECT_RANGE,
542 // Instruction::INVOKE_STATIC_RANGE,
543 // Instruction::INVOKE_INTERFACE_RANGE,
544 // Instruction::UNUSED_79,
545 // Instruction::UNUSED_7A,
546 // Instruction::NEG_INT,
547 // Instruction::NOT_INT,
548 // Instruction::NEG_LONG,
549 // Instruction::NOT_LONG,
550 // Instruction::NEG_FLOAT,
551 // Instruction::NEG_DOUBLE,
552 // Instruction::INT_TO_LONG,
553 // Instruction::INT_TO_FLOAT,
554 // Instruction::INT_TO_DOUBLE,
555 // Instruction::LONG_TO_INT,
556 // Instruction::LONG_TO_FLOAT,
557 // Instruction::LONG_TO_DOUBLE,
558 // Instruction::FLOAT_TO_INT,
559 // Instruction::FLOAT_TO_LONG,
560 // Instruction::FLOAT_TO_DOUBLE,
561 // Instruction::DOUBLE_TO_INT,
562 // Instruction::DOUBLE_TO_LONG,
563 // Instruction::DOUBLE_TO_FLOAT,
564 // Instruction::INT_TO_BYTE,
565 // Instruction::INT_TO_CHAR,
566 // Instruction::INT_TO_SHORT,
567 // Instruction::ADD_INT,
568 // Instruction::SUB_INT,
569 // Instruction::MUL_INT,
570 // Instruction::DIV_INT,
571 // Instruction::REM_INT,
572 // Instruction::AND_INT,
573 // Instruction::OR_INT,
574 // Instruction::XOR_INT,
575 // Instruction::SHL_INT,
576 // Instruction::SHR_INT,
577 // Instruction::USHR_INT,
578 // Instruction::ADD_LONG,
579 // Instruction::SUB_LONG,
580 // Instruction::MUL_LONG,
581 // Instruction::DIV_LONG,
582 // Instruction::REM_LONG,
583 // Instruction::AND_LONG,
584 // Instruction::OR_LONG,
585 // Instruction::XOR_LONG,
586 // Instruction::SHL_LONG,
587 // Instruction::SHR_LONG,
588 // Instruction::USHR_LONG,
589 // Instruction::ADD_FLOAT,
590 // Instruction::SUB_FLOAT,
591 // Instruction::MUL_FLOAT,
592 // Instruction::DIV_FLOAT,
593 // Instruction::REM_FLOAT,
594 // Instruction::ADD_DOUBLE,
595 // Instruction::SUB_DOUBLE,
596 // Instruction::MUL_DOUBLE,
597 // Instruction::DIV_DOUBLE,
598 // Instruction::REM_DOUBLE,
599 // Instruction::ADD_INT_2ADDR,
600 // Instruction::SUB_INT_2ADDR,
601 // Instruction::MUL_INT_2ADDR,
602 // Instruction::DIV_INT_2ADDR,
603 // Instruction::REM_INT_2ADDR,
604 // Instruction::AND_INT_2ADDR,
605 // Instruction::OR_INT_2ADDR,
606 // Instruction::XOR_INT_2ADDR,
607 // Instruction::SHL_INT_2ADDR,
608 // Instruction::SHR_INT_2ADDR,
609 // Instruction::USHR_INT_2ADDR,
610 // Instruction::ADD_LONG_2ADDR,
611 // Instruction::SUB_LONG_2ADDR,
612 // Instruction::MUL_LONG_2ADDR,
613 // Instruction::DIV_LONG_2ADDR,
614 // Instruction::REM_LONG_2ADDR,
615 // Instruction::AND_LONG_2ADDR,
616 // Instruction::OR_LONG_2ADDR,
617 // Instruction::XOR_LONG_2ADDR,
618 // Instruction::SHL_LONG_2ADDR,
619 // Instruction::SHR_LONG_2ADDR,
620 // Instruction::USHR_LONG_2ADDR,
621 // Instruction::ADD_FLOAT_2ADDR,
622 // Instruction::SUB_FLOAT_2ADDR,
623 // Instruction::MUL_FLOAT_2ADDR,
624 // Instruction::DIV_FLOAT_2ADDR,
625 // Instruction::REM_FLOAT_2ADDR,
626 // Instruction::ADD_DOUBLE_2ADDR,
627 // Instruction::SUB_DOUBLE_2ADDR,
628 // Instruction::MUL_DOUBLE_2ADDR,
629 // Instruction::DIV_DOUBLE_2ADDR,
630 // Instruction::REM_DOUBLE_2ADDR,
631 // Instruction::ADD_INT_LIT16,
632 // Instruction::RSUB_INT,
633 // Instruction::MUL_INT_LIT16,
634 // Instruction::DIV_INT_LIT16,
635 // Instruction::REM_INT_LIT16,
636 // Instruction::AND_INT_LIT16,
637 // Instruction::OR_INT_LIT16,
638 // Instruction::XOR_INT_LIT16,
639 // Instruction::ADD_INT_LIT8,
640 // Instruction::RSUB_INT_LIT8,
641 // Instruction::MUL_INT_LIT8,
642 // Instruction::DIV_INT_LIT8,
643 // Instruction::REM_INT_LIT8,
644 // Instruction::AND_INT_LIT8,
645 // Instruction::OR_INT_LIT8,
646 // Instruction::XOR_INT_LIT8,
647 // Instruction::SHL_INT_LIT8,
648 // Instruction::SHR_INT_LIT8,
649 // Instruction::USHR_INT_LIT8,
650 // Instruction::IGET_QUICK,
651 // Instruction::IGET_WIDE_QUICK,
652 // Instruction::IGET_OBJECT_QUICK,
653 // Instruction::IPUT_QUICK,
654 // Instruction::IPUT_WIDE_QUICK,
655 // Instruction::IPUT_OBJECT_QUICK,
656 // Instruction::INVOKE_VIRTUAL_QUICK,
657 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
658 // Instruction::UNUSED_EB,
659 // Instruction::UNUSED_EC,
660 // Instruction::UNUSED_ED,
661 // Instruction::UNUSED_EE,
662 // Instruction::UNUSED_EF,
663 // Instruction::UNUSED_F0,
664 // Instruction::UNUSED_F1,
665 // Instruction::UNUSED_F2,
666 // Instruction::UNUSED_F3,
667 // Instruction::UNUSED_F4,
668 // Instruction::UNUSED_F5,
669 // Instruction::UNUSED_F6,
670 // Instruction::UNUSED_F7,
671 // Instruction::UNUSED_F8,
672 // Instruction::UNUSED_F9,
673 // Instruction::UNUSED_FA,
674 // Instruction::UNUSED_FB,
675 // Instruction::UNUSED_FC,
676 // Instruction::UNUSED_FD,
677 // Instruction::UNUSED_FE,
678 // Instruction::UNUSED_FF,
679
680 // ----- ExtendedMIROpcode -----
681 // kMirOpPhi,
682 // kMirOpCopy,
683 // kMirOpFusedCmplFloat,
684 // kMirOpFusedCmpgFloat,
685 // kMirOpFusedCmplDouble,
686 // kMirOpFusedCmpgDouble,
687 // kMirOpFusedCmpLong,
688 // kMirOpNop,
689 // kMirOpNullCheck,
690 // kMirOpRangeCheck,
691 // kMirOpDivZeroCheck,
692 // kMirOpCheck,
693 // kMirOpCheckPart2,
694 // kMirOpSelect,
695 // kMirOpLast,
696};
697
698// Z : boolean
699// B : byte
700// S : short
701// C : char
702// I : int
Zheng Xu48241e72014-05-23 11:52:42 +0800703// J : long
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700704// F : float
705// D : double
706// L : reference(object, array)
707// V : void
708// (ARM64) Current calling conversion only support 32bit softfp
709// which has problems with long, float, double
Serban Constantinescu032d3772014-05-23 17:38:18 +0100710constexpr char arm64_supported_types[] = "ZBSCILVJFD";
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700711constexpr char x86_64_supported_types[] = "ZBSCILVJFD";
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700712
713// TODO: Remove this when we are able to compile everything.
714static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100715 uint32_t shorty_size = strlen(shorty);
716 CHECK_GE(shorty_size, 1u);
717 // Set a limitation on maximum number of parameters.
718 // Note : there is an implied "method*" parameter, and probably "this" as well.
719 // 1 is for the return type. Currently, we only accept 2 parameters at the most.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700720 // (x86_64): For now we have the same limitation. But we might want to split this
721 // check in future into two separate cases for arm64 and x86_64.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700722 if ((shorty_size > (1 + 2)) && (instruction_set != kX86_64)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100723 return false;
724 }
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700725
726 const char* supported_types = arm64_supported_types;
727 if (instruction_set == kX86_64) {
728 supported_types = x86_64_supported_types;
729 }
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100730 for (uint32_t i = 0; i < shorty_size; i++) {
731 if (strchr(supported_types, shorty[i]) == nullptr) {
732 return false;
733 }
734 }
735 return true;
736};
737
738// TODO: Remove this when we are able to compile everything.
739// Skip the method that we do not support currently.
740static bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
741 CompilationUnit& cu) {
742 // There is some limitation with current ARM 64 backend.
Dmitry Petrochenko136aaee2014-06-06 15:18:14 +0700743 if (cu.instruction_set == kArm64) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100744 // Check if we can compile the prototype.
745 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700746 if (!CanCompileShorty(shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100747 VLOG(compiler) << "Unsupported shorty : " << shorty;
748 return false;
749 }
750
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700751 const int *support_list = arm64_support_list;
752 int support_list_size = arraysize(arm64_support_list);
753 if (cu.instruction_set == kX86_64) {
754 support_list = x86_64_support_list;
755 support_list_size = arraysize(x86_64_support_list);
756 }
757
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100758 for (int idx = 0; idx < cu.mir_graph->GetNumBlocks(); idx++) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700759 BasicBlock* bb = cu.mir_graph->GetBasicBlock(idx);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100760 if (bb == NULL) continue;
761 if (bb->block_type == kDead) continue;
762 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
763 int opcode = mir->dalvikInsn.opcode;
764 // Check if we support the byte code.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700765 if (std::find(support_list, support_list + support_list_size,
766 opcode) == support_list + support_list_size) {
buzbee35ba7f32014-05-31 08:59:01 -0700767 if (!cu.mir_graph->IsPseudoMirOp(opcode)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100768 VLOG(compiler) << "Unsupported dalvik byte code : "
769 << mir->dalvikInsn.opcode;
770 } else {
771 VLOG(compiler) << "Unsupported extended MIR opcode : "
772 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
773 }
774 return false;
775 }
776 // Check if it invokes a prototype that we cannot support.
777 if (Instruction::INVOKE_VIRTUAL == opcode ||
778 Instruction::INVOKE_SUPER == opcode ||
779 Instruction::INVOKE_DIRECT == opcode ||
780 Instruction::INVOKE_STATIC == opcode ||
781 Instruction::INVOKE_INTERFACE == opcode) {
782 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
783 const char* invoke_method_shorty = dex_file.GetMethodShorty(
784 dex_file.GetMethodId(invoke_method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700785 if (!CanCompileShorty(invoke_method_shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100786 VLOG(compiler) << "Unsupported to invoke '"
787 << PrettyMethod(invoke_method_idx, dex_file)
788 << "' with shorty : " << invoke_method_shorty;
789 return false;
790 }
791 }
792 }
793 }
794
795 LOG(INFO) << "Using experimental instruction set A64 for "
796 << PrettyMethod(method_idx, dex_file);
797 }
798 return true;
799}
800
Ian Rogers3d504072014-03-01 09:16:49 -0800801static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000802 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 const DexFile::CodeItem* code_item,
804 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700805 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000806 jobject class_loader, const DexFile& dex_file,
807 void* llvm_compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700809 if (code_item->insns_size_in_code_units_ >= 0x10000) {
810 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
811 << " in " << PrettyMethod(method_idx, dex_file);
812 return NULL;
813 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814
Jeff Hao4a200f52014-04-01 14:58:49 -0700815 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000816 return nullptr;
817 }
818
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800820 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821
Ian Rogers3d504072014-03-01 09:16:49 -0800822 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700823 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800824 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -0700825 if (cu.instruction_set == kArm) {
826 cu.instruction_set = kThumb2;
827 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700828 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000829 cu.compiler = compiler;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000830 // TODO: x86_64 & arm64 are not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700831 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100832 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700833 (cu.instruction_set == kX86) ||
834 (cu.instruction_set == kX86_64) ||
835 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700838 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700840 cu.compiler_flip_match = false;
841 bool use_match = !cu.compiler_method_match.empty();
842 bool match = use_match && (cu.compiler_flip_match ^
843 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 std::string::npos));
845 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700846 cu.disable_opt = kCompilerOptimizerDisableFlags;
847 cu.enable_debug = kCompilerDebugFlags;
848 cu.verbose = VLOG_IS_ON(compiler) ||
849 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 }
851
Mingyao Yang42d65c52014-04-18 16:49:39 -0700852 if (gVerboseMethods.size() != 0) {
853 cu.verbose = false;
854 for (size_t i = 0; i < gVerboseMethods.size(); ++i) {
855 if (PrettyMethod(method_idx, dex_file).find(gVerboseMethods[i])
856 != std::string::npos) {
857 cu.verbose = true;
858 break;
859 }
860 }
861 }
862
buzbeeb01bf152014-05-13 15:59:07 -0700863 if (cu.verbose) {
864 cu.enable_debug |= (1 << kDebugCodegenDump);
865 }
866
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 /*
868 * TODO: rework handling of optimization and debug flags. Should we split out
869 * MIR and backend flags? Need command-line setting as well.
870 */
871
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000872 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700874 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700876 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 (1 << kLoadStoreElimination) |
878 (1 << kLoadHoisting) |
879 (1 << kSuppressLoads) |
880 (1 << kNullCheckElimination) |
881 (1 << kPromoteRegs) |
882 (1 << kTrackLiveTemps) |
883 (1 << kSafeOptimizations) |
884 (1 << kBBOpt) |
885 (1 << kMatch) |
886 (1 << kPromoteCompilerTemps));
887 }
888
Dmitry Petrochenkoba279d92014-05-16 18:36:39 +0700889 if (cu.instruction_set == kArm64 || cu.instruction_set == kX86_64) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100890 // TODO(Arm64): enable optimizations once backend is mature enough.
Dmitry Petrochenkoba279d92014-05-16 18:36:39 +0700891 // TODO(X86_64): enable optimizations once backend is mature enough.
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100892 cu.disable_opt = ~(uint32_t)0;
Dmitry Petrochenko30022af2014-06-06 15:18:14 +0700893 if (cu.instruction_set == kArm64) {
894 cu.enable_debug |= (1 << kDebugCodegenDump);
895 }
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100896 }
897
buzbeea61f4952013-08-23 14:27:06 -0700898 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700899 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800901 /*
902 * After creation of the MIR graph, also create the code generator.
903 * The reason we do this is that optimizations on the MIR graph may need to get information
904 * that is only available if a CG exists.
905 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000906 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800907
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 /* Gathering opcode stats? */
909 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700910 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700911 }
912
Calin Juravlec1b643c2014-05-30 23:44:11 +0100913 // Check early if we should skip this compilation if the profiler is enabled.
Dave Allison39c3bfb2014-01-28 18:33:52 -0800914 if (cu.compiler_driver->ProfilePresent()) {
915 std::string methodname = PrettyMethod(method_idx, dex_file);
916 if (cu.mir_graph->SkipCompilation(methodname)) {
917 return NULL;
918 }
919 }
920
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700922 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923 class_loader, dex_file);
924
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100925 // TODO(Arm64): Remove this when we are able to compile everything.
926 if (!CanCompileMethod(method_idx, dex_file, cu)) {
927 VLOG(compiler) << "Cannot compile method : " << PrettyMethod(method_idx, dex_file);
928 return nullptr;
929 }
930
buzbee1da1e2f2013-11-15 13:37:01 -0800931 cu.NewTimingSplit("MIROpt:CheckFilters");
Jeff Hao4a200f52014-04-01 14:58:49 -0700932 if (cu.mir_graph->SkipCompilation()) {
933 return NULL;
buzbeeee17e0a2013-07-31 10:47:37 -0700934 }
935
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800936 /* Create the pass driver and launch it */
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700937 PassDriverMEOpts pass_driver(&cu);
Ian Rogers3d504072014-03-01 09:16:49 -0800938 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700940 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
941 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942 }
943
944 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700945 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946 }
947
buzbee1da1e2f2013-11-15 13:37:01 -0800948 /* Reassociate sreg names with original Dalvik vreg names. */
949 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000951 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
952 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
953 if (cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
954 MemStats stack_stats(cu.arena_stack.GetPeakStats());
955 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
956 }
957 }
958 cu.arena_stack.Reset();
959
Brian Carlstrom7940e442013-07-12 13:46:57 -0700960 CompiledMethod* result = NULL;
961
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700962 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963
buzbee1da1e2f2013-11-15 13:37:01 -0800964 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700965 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800966 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700967
968 if (result) {
969 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
970 } else {
971 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
972 }
973
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700974 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000975 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000976 MemStats mem_stats(cu.arena.GetMemStats());
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000977 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 }
979 }
980
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700981 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
982 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 << " " << PrettyMethod(method_idx, dex_file);
984 }
985
buzbeea61f4952013-08-23 14:27:06 -0700986 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800987 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 return result;
989}
990
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000991CompiledMethod* CompileOneMethod(CompilerDriver& driver,
992 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 const DexFile::CodeItem* code_item,
994 uint32_t access_flags,
995 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700996 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997 uint32_t method_idx,
998 jobject class_loader,
999 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +00001000 void* compilation_unit) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001001 return CompileMethod(driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +00001002 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003}
1004
1005} // namespace art
1006
1007extern "C" art::CompiledMethod*
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001008 ArtQuickCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 const art::DexFile::CodeItem* code_item,
1010 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001011 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 const art::DexFile& dex_file) {
1013 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001014 art::Compiler* compiler = driver.GetCompiler();
1015 return art::CompileOneMethod(driver, compiler, code_item, access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 class_def_idx, method_idx, class_loader, dex_file,
1017 NULL /* use thread llvm_info */);
1018}