blob: d4ad0c2daa099c7172fe8fa565404116310ca3c2 [file] [log] [blame]
Andreas Gampe53c913b2014-08-12 23:19:23 -07001/*
2 * Copyright (C) 2014 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
17#include "quick_compiler.h"
18
19#include <cstdint>
20
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080021#include "base/dumpable.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080022#include "base/logging.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080023#include "base/macros.h"
24#include "base/timing_logger.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070025#include "compiler.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080026#include "dex_file-inl.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080027#include "dex_file_to_method_inliner_map.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080028#include "dex/compiler_ir.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080029#include "dex/dex_flags.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070030#include "dex/mir_graph.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080031#include "dex/pass_driver_me_opts.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080032#include "dex/pass_driver_me_post_opt.h"
33#include "dex/pass_manager.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070034#include "dex/quick/mir_to_lir.h"
35#include "driver/compiler_driver.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080036#include "driver/compiler_options.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070037#include "elf_writer_quick.h"
38#include "jni/quick/jni_compiler.h"
Andreas Gampe9c462082015-01-27 14:31:40 -080039#include "mir_to_lir.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070040#include "mirror/art_method-inl.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080041#include "mirror/object.h"
42#include "runtime.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070043
44// Specific compiler backends.
45#include "dex/quick/arm/backend_arm.h"
46#include "dex/quick/arm64/backend_arm64.h"
47#include "dex/quick/mips/backend_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010048#include "dex/quick/mips64/backend_mips64.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070049#include "dex/quick/x86/backend_x86.h"
50
51namespace art {
52
Andreas Gampe785d2f22014-11-03 22:57:30 -080053static_assert(0U == static_cast<size_t>(kNone), "kNone not 0");
54static_assert(1U == static_cast<size_t>(kArm), "kArm not 1");
55static_assert(2U == static_cast<size_t>(kArm64), "kArm64 not 2");
56static_assert(3U == static_cast<size_t>(kThumb2), "kThumb2 not 3");
57static_assert(4U == static_cast<size_t>(kX86), "kX86 not 4");
58static_assert(5U == static_cast<size_t>(kX86_64), "kX86_64 not 5");
59static_assert(6U == static_cast<size_t>(kMips), "kMips not 6");
60static_assert(7U == static_cast<size_t>(kMips64), "kMips64 not 7");
Andreas Gampe53c913b2014-08-12 23:19:23 -070061
62// Additional disabled optimizations (over generally disabled) per instruction set.
63static constexpr uint32_t kDisabledOptimizationsPerISA[] = {
64 // 0 = kNone.
65 ~0U,
66 // 1 = kArm, unused (will use kThumb2).
67 ~0U,
68 // 2 = kArm64.
69 0,
70 // 3 = kThumb2.
71 0,
72 // 4 = kX86.
73 (1 << kLoadStoreElimination) |
74 0,
75 // 5 = kX86_64.
76 (1 << kLoadStoreElimination) |
77 0,
78 // 6 = kMips.
79 (1 << kLoadStoreElimination) |
80 (1 << kLoadHoisting) |
81 (1 << kSuppressLoads) |
82 (1 << kNullCheckElimination) |
83 (1 << kPromoteRegs) |
84 (1 << kTrackLiveTemps) |
85 (1 << kSafeOptimizations) |
86 (1 << kBBOpt) |
87 (1 << kMatch) |
88 (1 << kPromoteCompilerTemps) |
89 0,
90 // 7 = kMips64.
Maja Gagic6ea651f2015-02-24 16:55:04 +010091 (1 << kLoadStoreElimination) |
92 (1 << kLoadHoisting) |
93 (1 << kSuppressLoads) |
94 (1 << kNullCheckElimination) |
95 (1 << kPromoteRegs) |
96 (1 << kTrackLiveTemps) |
97 (1 << kSafeOptimizations) |
98 (1 << kBBOpt) |
99 (1 << kMatch) |
100 (1 << kPromoteCompilerTemps) |
101 0
Andreas Gampe53c913b2014-08-12 23:19:23 -0700102};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800103static_assert(sizeof(kDisabledOptimizationsPerISA) == 8 * sizeof(uint32_t),
104 "kDisabledOpts unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700105
106// Supported shorty types per instruction set. nullptr means that all are available.
107// Z : boolean
108// B : byte
109// S : short
110// C : char
111// I : int
112// J : long
113// F : float
114// D : double
115// L : reference(object, array)
116// V : void
117static const char* kSupportedTypes[] = {
118 // 0 = kNone.
119 "",
120 // 1 = kArm, unused (will use kThumb2).
121 "",
122 // 2 = kArm64.
123 nullptr,
124 // 3 = kThumb2.
125 nullptr,
126 // 4 = kX86.
127 nullptr,
128 // 5 = kX86_64.
129 nullptr,
130 // 6 = kMips.
131 nullptr,
132 // 7 = kMips64.
Maja Gagic6ea651f2015-02-24 16:55:04 +0100133 nullptr
Andreas Gampe53c913b2014-08-12 23:19:23 -0700134};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800135static_assert(sizeof(kSupportedTypes) == 8 * sizeof(char*), "kSupportedTypes unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700136
137static int kAllOpcodes[] = {
138 Instruction::NOP,
139 Instruction::MOVE,
140 Instruction::MOVE_FROM16,
141 Instruction::MOVE_16,
142 Instruction::MOVE_WIDE,
143 Instruction::MOVE_WIDE_FROM16,
144 Instruction::MOVE_WIDE_16,
145 Instruction::MOVE_OBJECT,
146 Instruction::MOVE_OBJECT_FROM16,
147 Instruction::MOVE_OBJECT_16,
148 Instruction::MOVE_RESULT,
149 Instruction::MOVE_RESULT_WIDE,
150 Instruction::MOVE_RESULT_OBJECT,
151 Instruction::MOVE_EXCEPTION,
152 Instruction::RETURN_VOID,
153 Instruction::RETURN,
154 Instruction::RETURN_WIDE,
155 Instruction::RETURN_OBJECT,
156 Instruction::CONST_4,
157 Instruction::CONST_16,
158 Instruction::CONST,
159 Instruction::CONST_HIGH16,
160 Instruction::CONST_WIDE_16,
161 Instruction::CONST_WIDE_32,
162 Instruction::CONST_WIDE,
163 Instruction::CONST_WIDE_HIGH16,
164 Instruction::CONST_STRING,
165 Instruction::CONST_STRING_JUMBO,
166 Instruction::CONST_CLASS,
167 Instruction::MONITOR_ENTER,
168 Instruction::MONITOR_EXIT,
169 Instruction::CHECK_CAST,
170 Instruction::INSTANCE_OF,
171 Instruction::ARRAY_LENGTH,
172 Instruction::NEW_INSTANCE,
173 Instruction::NEW_ARRAY,
174 Instruction::FILLED_NEW_ARRAY,
175 Instruction::FILLED_NEW_ARRAY_RANGE,
176 Instruction::FILL_ARRAY_DATA,
177 Instruction::THROW,
178 Instruction::GOTO,
179 Instruction::GOTO_16,
180 Instruction::GOTO_32,
181 Instruction::PACKED_SWITCH,
182 Instruction::SPARSE_SWITCH,
183 Instruction::CMPL_FLOAT,
184 Instruction::CMPG_FLOAT,
185 Instruction::CMPL_DOUBLE,
186 Instruction::CMPG_DOUBLE,
187 Instruction::CMP_LONG,
188 Instruction::IF_EQ,
189 Instruction::IF_NE,
190 Instruction::IF_LT,
191 Instruction::IF_GE,
192 Instruction::IF_GT,
193 Instruction::IF_LE,
194 Instruction::IF_EQZ,
195 Instruction::IF_NEZ,
196 Instruction::IF_LTZ,
197 Instruction::IF_GEZ,
198 Instruction::IF_GTZ,
199 Instruction::IF_LEZ,
200 Instruction::UNUSED_3E,
201 Instruction::UNUSED_3F,
202 Instruction::UNUSED_40,
203 Instruction::UNUSED_41,
204 Instruction::UNUSED_42,
205 Instruction::UNUSED_43,
206 Instruction::AGET,
207 Instruction::AGET_WIDE,
208 Instruction::AGET_OBJECT,
209 Instruction::AGET_BOOLEAN,
210 Instruction::AGET_BYTE,
211 Instruction::AGET_CHAR,
212 Instruction::AGET_SHORT,
213 Instruction::APUT,
214 Instruction::APUT_WIDE,
215 Instruction::APUT_OBJECT,
216 Instruction::APUT_BOOLEAN,
217 Instruction::APUT_BYTE,
218 Instruction::APUT_CHAR,
219 Instruction::APUT_SHORT,
220 Instruction::IGET,
221 Instruction::IGET_WIDE,
222 Instruction::IGET_OBJECT,
223 Instruction::IGET_BOOLEAN,
224 Instruction::IGET_BYTE,
225 Instruction::IGET_CHAR,
226 Instruction::IGET_SHORT,
227 Instruction::IPUT,
228 Instruction::IPUT_WIDE,
229 Instruction::IPUT_OBJECT,
230 Instruction::IPUT_BOOLEAN,
231 Instruction::IPUT_BYTE,
232 Instruction::IPUT_CHAR,
233 Instruction::IPUT_SHORT,
234 Instruction::SGET,
235 Instruction::SGET_WIDE,
236 Instruction::SGET_OBJECT,
237 Instruction::SGET_BOOLEAN,
238 Instruction::SGET_BYTE,
239 Instruction::SGET_CHAR,
240 Instruction::SGET_SHORT,
241 Instruction::SPUT,
242 Instruction::SPUT_WIDE,
243 Instruction::SPUT_OBJECT,
244 Instruction::SPUT_BOOLEAN,
245 Instruction::SPUT_BYTE,
246 Instruction::SPUT_CHAR,
247 Instruction::SPUT_SHORT,
248 Instruction::INVOKE_VIRTUAL,
249 Instruction::INVOKE_SUPER,
250 Instruction::INVOKE_DIRECT,
251 Instruction::INVOKE_STATIC,
252 Instruction::INVOKE_INTERFACE,
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700253 Instruction::RETURN_VOID_NO_BARRIER,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700254 Instruction::INVOKE_VIRTUAL_RANGE,
255 Instruction::INVOKE_SUPER_RANGE,
256 Instruction::INVOKE_DIRECT_RANGE,
257 Instruction::INVOKE_STATIC_RANGE,
258 Instruction::INVOKE_INTERFACE_RANGE,
259 Instruction::UNUSED_79,
260 Instruction::UNUSED_7A,
261 Instruction::NEG_INT,
262 Instruction::NOT_INT,
263 Instruction::NEG_LONG,
264 Instruction::NOT_LONG,
265 Instruction::NEG_FLOAT,
266 Instruction::NEG_DOUBLE,
267 Instruction::INT_TO_LONG,
268 Instruction::INT_TO_FLOAT,
269 Instruction::INT_TO_DOUBLE,
270 Instruction::LONG_TO_INT,
271 Instruction::LONG_TO_FLOAT,
272 Instruction::LONG_TO_DOUBLE,
273 Instruction::FLOAT_TO_INT,
274 Instruction::FLOAT_TO_LONG,
275 Instruction::FLOAT_TO_DOUBLE,
276 Instruction::DOUBLE_TO_INT,
277 Instruction::DOUBLE_TO_LONG,
278 Instruction::DOUBLE_TO_FLOAT,
279 Instruction::INT_TO_BYTE,
280 Instruction::INT_TO_CHAR,
281 Instruction::INT_TO_SHORT,
282 Instruction::ADD_INT,
283 Instruction::SUB_INT,
284 Instruction::MUL_INT,
285 Instruction::DIV_INT,
286 Instruction::REM_INT,
287 Instruction::AND_INT,
288 Instruction::OR_INT,
289 Instruction::XOR_INT,
290 Instruction::SHL_INT,
291 Instruction::SHR_INT,
292 Instruction::USHR_INT,
293 Instruction::ADD_LONG,
294 Instruction::SUB_LONG,
295 Instruction::MUL_LONG,
296 Instruction::DIV_LONG,
297 Instruction::REM_LONG,
298 Instruction::AND_LONG,
299 Instruction::OR_LONG,
300 Instruction::XOR_LONG,
301 Instruction::SHL_LONG,
302 Instruction::SHR_LONG,
303 Instruction::USHR_LONG,
304 Instruction::ADD_FLOAT,
305 Instruction::SUB_FLOAT,
306 Instruction::MUL_FLOAT,
307 Instruction::DIV_FLOAT,
308 Instruction::REM_FLOAT,
309 Instruction::ADD_DOUBLE,
310 Instruction::SUB_DOUBLE,
311 Instruction::MUL_DOUBLE,
312 Instruction::DIV_DOUBLE,
313 Instruction::REM_DOUBLE,
314 Instruction::ADD_INT_2ADDR,
315 Instruction::SUB_INT_2ADDR,
316 Instruction::MUL_INT_2ADDR,
317 Instruction::DIV_INT_2ADDR,
318 Instruction::REM_INT_2ADDR,
319 Instruction::AND_INT_2ADDR,
320 Instruction::OR_INT_2ADDR,
321 Instruction::XOR_INT_2ADDR,
322 Instruction::SHL_INT_2ADDR,
323 Instruction::SHR_INT_2ADDR,
324 Instruction::USHR_INT_2ADDR,
325 Instruction::ADD_LONG_2ADDR,
326 Instruction::SUB_LONG_2ADDR,
327 Instruction::MUL_LONG_2ADDR,
328 Instruction::DIV_LONG_2ADDR,
329 Instruction::REM_LONG_2ADDR,
330 Instruction::AND_LONG_2ADDR,
331 Instruction::OR_LONG_2ADDR,
332 Instruction::XOR_LONG_2ADDR,
333 Instruction::SHL_LONG_2ADDR,
334 Instruction::SHR_LONG_2ADDR,
335 Instruction::USHR_LONG_2ADDR,
336 Instruction::ADD_FLOAT_2ADDR,
337 Instruction::SUB_FLOAT_2ADDR,
338 Instruction::MUL_FLOAT_2ADDR,
339 Instruction::DIV_FLOAT_2ADDR,
340 Instruction::REM_FLOAT_2ADDR,
341 Instruction::ADD_DOUBLE_2ADDR,
342 Instruction::SUB_DOUBLE_2ADDR,
343 Instruction::MUL_DOUBLE_2ADDR,
344 Instruction::DIV_DOUBLE_2ADDR,
345 Instruction::REM_DOUBLE_2ADDR,
346 Instruction::ADD_INT_LIT16,
347 Instruction::RSUB_INT,
348 Instruction::MUL_INT_LIT16,
349 Instruction::DIV_INT_LIT16,
350 Instruction::REM_INT_LIT16,
351 Instruction::AND_INT_LIT16,
352 Instruction::OR_INT_LIT16,
353 Instruction::XOR_INT_LIT16,
354 Instruction::ADD_INT_LIT8,
355 Instruction::RSUB_INT_LIT8,
356 Instruction::MUL_INT_LIT8,
357 Instruction::DIV_INT_LIT8,
358 Instruction::REM_INT_LIT8,
359 Instruction::AND_INT_LIT8,
360 Instruction::OR_INT_LIT8,
361 Instruction::XOR_INT_LIT8,
362 Instruction::SHL_INT_LIT8,
363 Instruction::SHR_INT_LIT8,
364 Instruction::USHR_INT_LIT8,
365 Instruction::IGET_QUICK,
366 Instruction::IGET_WIDE_QUICK,
367 Instruction::IGET_OBJECT_QUICK,
368 Instruction::IPUT_QUICK,
369 Instruction::IPUT_WIDE_QUICK,
370 Instruction::IPUT_OBJECT_QUICK,
371 Instruction::INVOKE_VIRTUAL_QUICK,
372 Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
Fred Shih37f05ef2014-07-16 18:38:08 -0700373 Instruction::IPUT_BOOLEAN_QUICK,
374 Instruction::IPUT_BYTE_QUICK,
375 Instruction::IPUT_CHAR_QUICK,
376 Instruction::IPUT_SHORT_QUICK,
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800377 Instruction::IGET_BOOLEAN_QUICK,
378 Instruction::IGET_BYTE_QUICK,
379 Instruction::IGET_CHAR_QUICK,
380 Instruction::IGET_SHORT_QUICK,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700381 Instruction::UNUSED_F3,
382 Instruction::UNUSED_F4,
383 Instruction::UNUSED_F5,
384 Instruction::UNUSED_F6,
385 Instruction::UNUSED_F7,
386 Instruction::UNUSED_F8,
387 Instruction::UNUSED_F9,
388 Instruction::UNUSED_FA,
389 Instruction::UNUSED_FB,
390 Instruction::UNUSED_FC,
391 Instruction::UNUSED_FD,
392 Instruction::UNUSED_FE,
393 Instruction::UNUSED_FF,
394 // ----- ExtendedMIROpcode -----
395 kMirOpPhi,
396 kMirOpCopy,
397 kMirOpFusedCmplFloat,
398 kMirOpFusedCmpgFloat,
399 kMirOpFusedCmplDouble,
400 kMirOpFusedCmpgDouble,
401 kMirOpFusedCmpLong,
402 kMirOpNop,
403 kMirOpNullCheck,
404 kMirOpRangeCheck,
405 kMirOpDivZeroCheck,
406 kMirOpCheck,
407 kMirOpCheckPart2,
408 kMirOpSelect,
409};
410
Zheng Xu5667fdb2014-10-23 18:29:55 +0800411static int kInvokeOpcodes[] = {
412 Instruction::INVOKE_VIRTUAL,
413 Instruction::INVOKE_SUPER,
414 Instruction::INVOKE_DIRECT,
415 Instruction::INVOKE_STATIC,
416 Instruction::INVOKE_INTERFACE,
417 Instruction::INVOKE_VIRTUAL_RANGE,
418 Instruction::INVOKE_SUPER_RANGE,
419 Instruction::INVOKE_DIRECT_RANGE,
420 Instruction::INVOKE_STATIC_RANGE,
421 Instruction::INVOKE_INTERFACE_RANGE,
422 Instruction::INVOKE_VIRTUAL_QUICK,
423 Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
424};
425
Andreas Gampe53c913b2014-08-12 23:19:23 -0700426// Unsupported opcodes. nullptr can be used when everything is supported. Size of the lists is
427// recorded below.
428static const int* kUnsupportedOpcodes[] = {
429 // 0 = kNone.
430 kAllOpcodes,
431 // 1 = kArm, unused (will use kThumb2).
432 kAllOpcodes,
433 // 2 = kArm64.
434 nullptr,
435 // 3 = kThumb2.
436 nullptr,
437 // 4 = kX86.
438 nullptr,
439 // 5 = kX86_64.
440 nullptr,
441 // 6 = kMips.
442 nullptr,
443 // 7 = kMips64.
Maja Gagic6ea651f2015-02-24 16:55:04 +0100444 nullptr
Andreas Gampe53c913b2014-08-12 23:19:23 -0700445};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800446static_assert(sizeof(kUnsupportedOpcodes) == 8 * sizeof(int*), "kUnsupportedOpcodes unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700447
448// Size of the arrays stored above.
449static const size_t kUnsupportedOpcodesSize[] = {
450 // 0 = kNone.
451 arraysize(kAllOpcodes),
452 // 1 = kArm, unused (will use kThumb2).
453 arraysize(kAllOpcodes),
454 // 2 = kArm64.
455 0,
456 // 3 = kThumb2.
457 0,
458 // 4 = kX86.
459 0,
460 // 5 = kX86_64.
461 0,
462 // 6 = kMips.
463 0,
464 // 7 = kMips64.
Maja Gagic6ea651f2015-02-24 16:55:04 +0100465 0
Andreas Gampe53c913b2014-08-12 23:19:23 -0700466};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800467static_assert(sizeof(kUnsupportedOpcodesSize) == 8 * sizeof(size_t),
468 "kUnsupportedOpcodesSize unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700469
470// The maximum amount of Dalvik register in a method for which we will start compiling. Tries to
471// avoid an abort when we need to manage more SSA registers than we can.
472static constexpr size_t kMaxAllowedDalvikRegisters = INT16_MAX / 2;
473
474static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) {
475 const char* supported_types = kSupportedTypes[instruction_set];
476 if (supported_types == nullptr) {
477 // Everything available.
478 return true;
479 }
480
481 uint32_t shorty_size = strlen(shorty);
482 CHECK_GE(shorty_size, 1u);
483
484 for (uint32_t i = 0; i < shorty_size; i++) {
485 if (strchr(supported_types, shorty[i]) == nullptr) {
486 return false;
487 }
488 }
489 return true;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700490}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700491
492// Skip the method that we do not support currently.
493bool QuickCompiler::CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
494 CompilationUnit* cu) const {
495 // This is a limitation in mir_graph. See MirGraph::SetNumSSARegs.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700496 if (cu->mir_graph->GetNumOfCodeAndTempVRs() > kMaxAllowedDalvikRegisters) {
497 VLOG(compiler) << "Too many dalvik registers : " << cu->mir_graph->GetNumOfCodeAndTempVRs();
Andreas Gampe53c913b2014-08-12 23:19:23 -0700498 return false;
499 }
500
501 // Check whether we do have limitations at all.
502 if (kSupportedTypes[cu->instruction_set] == nullptr &&
503 kUnsupportedOpcodesSize[cu->instruction_set] == 0U) {
504 return true;
505 }
506
507 // Check if we can compile the prototype.
508 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
509 if (!CanCompileShorty(shorty, cu->instruction_set)) {
510 VLOG(compiler) << "Unsupported shorty : " << shorty;
511 return false;
512 }
513
514 const int *unsupport_list = kUnsupportedOpcodes[cu->instruction_set];
515 int unsupport_list_size = kUnsupportedOpcodesSize[cu->instruction_set];
516
517 for (unsigned int idx = 0; idx < cu->mir_graph->GetNumBlocks(); idx++) {
518 BasicBlock* bb = cu->mir_graph->GetBasicBlock(idx);
519 if (bb == NULL) continue;
520 if (bb->block_type == kDead) continue;
521 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
522 int opcode = mir->dalvikInsn.opcode;
523 // Check if we support the byte code.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800524 if (std::find(unsupport_list, unsupport_list + unsupport_list_size, opcode)
525 != unsupport_list + unsupport_list_size) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700526 if (!MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
527 VLOG(compiler) << "Unsupported dalvik byte code : "
528 << mir->dalvikInsn.opcode;
529 } else {
530 VLOG(compiler) << "Unsupported extended MIR opcode : "
531 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
532 }
533 return false;
534 }
535 // Check if it invokes a prototype that we cannot support.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800536 if (std::find(kInvokeOpcodes, kInvokeOpcodes + arraysize(kInvokeOpcodes), opcode)
537 != kInvokeOpcodes + arraysize(kInvokeOpcodes)) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700538 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
539 const char* invoke_method_shorty = dex_file.GetMethodShorty(
540 dex_file.GetMethodId(invoke_method_idx));
541 if (!CanCompileShorty(invoke_method_shorty, cu->instruction_set)) {
542 VLOG(compiler) << "Unsupported to invoke '"
543 << PrettyMethod(invoke_method_idx, dex_file)
544 << "' with shorty : " << invoke_method_shorty;
545 return false;
546 }
547 }
548 }
549 }
550 return true;
551}
552
553void QuickCompiler::InitCompilationUnit(CompilationUnit& cu) const {
554 // Disable optimizations according to instruction set.
555 cu.disable_opt |= kDisabledOptimizationsPerISA[cu.instruction_set];
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800556 if (Runtime::Current()->UseJit()) {
557 // Disable these optimizations for JIT until quickened byte codes are done being implemented.
558 // TODO: Find a cleaner way to do this.
559 cu.disable_opt |= 1u << kLocalValueNumbering;
560 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700561}
562
David Brazdilee690a32014-12-01 17:04:16 +0000563void QuickCompiler::Init() {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700564 CHECK(GetCompilerDriver()->GetCompilerContext() == nullptr);
565}
566
567void QuickCompiler::UnInit() const {
568 CHECK(GetCompilerDriver()->GetCompilerContext() == nullptr);
569}
570
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800571/* Default optimizer/debug setting for the compiler. */
572static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
573 // (1 << kLoadStoreElimination) |
574 // (1 << kLoadHoisting) |
575 // (1 << kSuppressLoads) |
576 // (1 << kNullCheckElimination) |
577 // (1 << kClassInitCheckElimination) |
578 // (1 << kGlobalValueNumbering) |
Vladimir Marko37e05bf2015-02-18 14:06:43 +0000579 (1 << kGvnDeadCodeElimination) |
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800580 // (1 << kLocalValueNumbering) |
581 // (1 << kPromoteRegs) |
582 // (1 << kTrackLiveTemps) |
583 // (1 << kSafeOptimizations) |
584 // (1 << kBBOpt) |
585 // (1 << kSuspendCheckElimination) |
586 // (1 << kMatch) |
587 // (1 << kPromoteCompilerTemps) |
588 // (1 << kSuppressExceptionEdges) |
589 // (1 << kSuppressMethodInlining) |
590 0;
591
592static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
593 // (1 << kDebugDisplayMissingTargets) |
594 // (1 << kDebugVerbose) |
595 // (1 << kDebugDumpCFG) |
596 // (1 << kDebugSlowFieldPath) |
597 // (1 << kDebugSlowInvokePath) |
598 // (1 << kDebugSlowStringPath) |
599 // (1 << kDebugSlowestFieldPath) |
600 // (1 << kDebugSlowestStringPath) |
601 // (1 << kDebugExerciseResolveMethod) |
602 // (1 << kDebugVerifyDataflow) |
603 // (1 << kDebugShowMemoryUsage) |
604 // (1 << kDebugShowNops) |
605 // (1 << kDebugCountOpcodes) |
606 // (1 << kDebugDumpCheckStats) |
607 // (1 << kDebugShowSummaryMemoryUsage) |
608 // (1 << kDebugShowFilterStats) |
609 // (1 << kDebugTimings) |
610 // (1 << kDebugCodegenDump) |
611 0;
612
Andreas Gampe53c913b2014-08-12 23:19:23 -0700613CompiledMethod* QuickCompiler::Compile(const DexFile::CodeItem* code_item,
614 uint32_t access_flags,
615 InvokeType invoke_type,
616 uint16_t class_def_idx,
617 uint32_t method_idx,
618 jobject class_loader,
619 const DexFile& dex_file) const {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700620 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use
621 // build default.
622 CompilerDriver* driver = GetCompilerDriver();
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800623
624 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
625 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
626 return nullptr;
627 }
628
629 DCHECK(driver->GetCompilerOptions().IsCompilationEnabled());
630
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700631 Runtime* const runtime = Runtime::Current();
632 ClassLinker* const class_linker = runtime->GetClassLinker();
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800633 InstructionSet instruction_set = driver->GetInstructionSet();
634 if (instruction_set == kArm) {
635 instruction_set = kThumb2;
636 }
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700637 CompilationUnit cu(runtime->GetArenaPool(), instruction_set, driver, class_linker);
Vladimir Marko20f85592015-03-19 10:07:02 +0000638 cu.dex_file = &dex_file;
639 cu.class_def_idx = class_def_idx;
640 cu.method_idx = method_idx;
641 cu.access_flags = access_flags;
642 cu.invoke_type = invoke_type;
643 cu.shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800644
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800645 CHECK((cu.instruction_set == kThumb2) ||
646 (cu.instruction_set == kArm64) ||
647 (cu.instruction_set == kX86) ||
648 (cu.instruction_set == kX86_64) ||
Maja Gagic6ea651f2015-02-24 16:55:04 +0100649 (cu.instruction_set == kMips) ||
650 (cu.instruction_set == kMips64));
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800651
652 // TODO: set this from command line
653 constexpr bool compiler_flip_match = false;
654 const std::string compiler_method_match = "";
655
656 bool use_match = !compiler_method_match.empty();
657 bool match = use_match && (compiler_flip_match ^
658 (PrettyMethod(method_idx, dex_file).find(compiler_method_match) != std::string::npos));
659 if (!use_match || match) {
660 cu.disable_opt = kCompilerOptimizerDisableFlags;
661 cu.enable_debug = kCompilerDebugFlags;
662 cu.verbose = VLOG_IS_ON(compiler) ||
663 (cu.enable_debug & (1 << kDebugVerbose));
664 }
665
666 if (driver->GetCompilerOptions().HasVerboseMethods()) {
667 cu.verbose = driver->GetCompilerOptions().IsVerboseMethod(PrettyMethod(method_idx, dex_file));
668 }
669
670 if (cu.verbose) {
671 cu.enable_debug |= (1 << kDebugCodegenDump);
672 }
673
674 /*
675 * TODO: rework handling of optimization and debug flags. Should we split out
676 * MIR and backend flags? Need command-line setting as well.
677 */
678
679 InitCompilationUnit(cu);
680
681 cu.StartTimingSplit("BuildMIRGraph");
682 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
683
684 /*
685 * After creation of the MIR graph, also create the code generator.
686 * The reason we do this is that optimizations on the MIR graph may need to get information
687 * that is only available if a CG exists.
688 */
689 cu.cg.reset(GetCodeGenerator(&cu, nullptr));
690
691 /* Gathering opcode stats? */
692 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
693 cu.mir_graph->EnableOpcodeCounting();
694 }
695
696 /* Build the raw MIR graph */
697 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
698 class_loader, dex_file);
699
700 if (!CanCompileMethod(method_idx, dex_file, &cu)) {
701 VLOG(compiler) << cu.instruction_set << ": Cannot compile method : "
702 << PrettyMethod(method_idx, dex_file);
703 cu.EndTiming();
704 return nullptr;
705 }
706
707 cu.NewTimingSplit("MIROpt:CheckFilters");
708 std::string skip_message;
709 if (cu.mir_graph->SkipCompilation(&skip_message)) {
710 VLOG(compiler) << cu.instruction_set << ": Skipping method : "
711 << PrettyMethod(method_idx, dex_file) << " Reason = " << skip_message;
712 cu.EndTiming();
713 return nullptr;
714 }
715
716 /* Create the pass driver and launch it */
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000717 PassDriverMEOpts pass_driver(GetPreOptPassManager(), GetPostOptPassManager(), &cu);
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800718 pass_driver.Launch();
719
720 /* For non-leaf methods check if we should skip compilation when the profiler is enabled. */
721 if (cu.compiler_driver->ProfilePresent()
722 && !cu.mir_graph->MethodIsLeaf()
723 && cu.mir_graph->SkipCompilationByName(PrettyMethod(method_idx, dex_file))) {
724 cu.EndTiming();
725 return nullptr;
726 }
727
728 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
729 cu.mir_graph->DumpCheckStats();
730 }
731
732 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
733 cu.mir_graph->ShowOpcodeStats();
734 }
735
736 /* Reassociate sreg names with original Dalvik vreg names. */
737 cu.mir_graph->RemapRegLocations();
738
739 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
740 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
741 if (cu.arena_stack.PeakBytesAllocated() > 1 * 1024 * 1024) {
742 MemStats stack_stats(cu.arena_stack.GetPeakStats());
743 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
744 }
745 }
746 cu.arena_stack.Reset();
747
748 CompiledMethod* result = nullptr;
749
750 if (cu.mir_graph->PuntToInterpreter()) {
751 VLOG(compiler) << cu.instruction_set << ": Punted method to interpreter: "
752 << PrettyMethod(method_idx, dex_file);
753 cu.EndTiming();
754 return nullptr;
755 }
756
757 cu.cg->Materialize();
758
759 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
760 result = cu.cg->GetCompiledMethod();
761 cu.NewTimingSplit("Cleanup");
762
763 if (result) {
764 VLOG(compiler) << cu.instruction_set << ": Compiled " << PrettyMethod(method_idx, dex_file);
765 } else {
766 VLOG(compiler) << cu.instruction_set << ": Deferred " << PrettyMethod(method_idx, dex_file);
767 }
768
769 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
770 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
771 MemStats mem_stats(cu.arena.GetMemStats());
772 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
773 }
774 }
775
776 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
777 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
778 << " " << PrettyMethod(method_idx, dex_file);
779 }
780
781 cu.EndTiming();
782 driver->GetTimingsLogger()->AddLogger(cu.timings);
783 return result;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700784}
785
786CompiledMethod* QuickCompiler::JniCompile(uint32_t access_flags,
787 uint32_t method_idx,
788 const DexFile& dex_file) const {
789 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
790}
791
792uintptr_t QuickCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Mathieu Chartier130914e2014-11-18 15:34:09 -0800793 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
794 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
Andreas Gampe53c913b2014-08-12 23:19:23 -0700795}
796
797bool QuickCompiler::WriteElf(art::File* file,
798 OatWriter* oat_writer,
799 const std::vector<const art::DexFile*>& dex_files,
800 const std::string& android_root,
801 bool is_host) const {
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000802 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
803 *GetCompilerDriver());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700804}
805
Andreas Gampe9c462082015-01-27 14:31:40 -0800806Mir2Lir* QuickCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700807 UNUSED(compilation_unit);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700808 Mir2Lir* mir_to_lir = nullptr;
809 switch (cu->instruction_set) {
810 case kThumb2:
811 mir_to_lir = ArmCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
812 break;
813 case kArm64:
814 mir_to_lir = Arm64CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
815 break;
816 case kMips:
817 mir_to_lir = MipsCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
818 break;
Maja Gagic6ea651f2015-02-24 16:55:04 +0100819 case kMips64:
820 mir_to_lir = Mips64CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
821 break;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700822 case kX86:
823 // Fall-through.
824 case kX86_64:
825 mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
826 break;
827 default:
828 LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
829 }
830
831 /* The number of compiler temporaries depends on backend so set it up now if possible */
832 if (mir_to_lir) {
833 size_t max_temps = mir_to_lir->GetMaxPossibleCompilerTemps();
834 bool set_max = cu->mir_graph->SetMaxAvailableNonSpecialCompilerTemps(max_temps);
835 CHECK(set_max);
836 }
837 return mir_to_lir;
838}
839
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800840QuickCompiler::QuickCompiler(CompilerDriver* driver) : Compiler(driver, 100) {
841 const auto& compiler_options = driver->GetCompilerOptions();
842 auto* pass_manager_options = compiler_options.GetPassManagerOptions();
843 pre_opt_pass_manager_.reset(new PassManager(*pass_manager_options));
844 CHECK(pre_opt_pass_manager_.get() != nullptr);
845 PassDriverMEOpts::SetupPasses(pre_opt_pass_manager_.get());
846 pre_opt_pass_manager_->CreateDefaultPassList();
847 if (pass_manager_options->GetPrintPassOptions()) {
848 PassDriverMEOpts::PrintPassOptions(pre_opt_pass_manager_.get());
849 }
850 // TODO: Different options for pre vs post opts?
851 post_opt_pass_manager_.reset(new PassManager(PassManagerOptions()));
852 CHECK(post_opt_pass_manager_.get() != nullptr);
853 PassDriverMEPostOpt::SetupPasses(post_opt_pass_manager_.get());
854 post_opt_pass_manager_->CreateDefaultPassList();
855 if (pass_manager_options->GetPrintPassOptions()) {
856 PassDriverMEPostOpt::PrintPassOptions(post_opt_pass_manager_.get());
857 }
858}
859
860QuickCompiler::~QuickCompiler() {
861}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700862
863Compiler* CreateQuickCompiler(CompilerDriver* driver) {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800864 return QuickCompiler::Create(driver);
865}
866
867Compiler* QuickCompiler::Create(CompilerDriver* driver) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700868 return new QuickCompiler(driver);
869}
870
871} // namespace art