blob: 8d4cb3c5e92beffc44cd72484950bdcbbd7eab5a [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
21#include "compiler.h"
22#include "dex/frontend.h"
23#include "dex/mir_graph.h"
24#include "dex/quick/mir_to_lir.h"
25#include "driver/compiler_driver.h"
26#include "elf_writer_quick.h"
27#include "jni/quick/jni_compiler.h"
28#include "mirror/art_method-inl.h"
29#include "base/logging.h"
30
31// Specific compiler backends.
32#include "dex/quick/arm/backend_arm.h"
33#include "dex/quick/arm64/backend_arm64.h"
34#include "dex/quick/mips/backend_mips.h"
35#include "dex/quick/x86/backend_x86.h"
36
37namespace art {
38
39class QuickCompiler : public Compiler {
40 public:
41 explicit QuickCompiler(CompilerDriver* driver) : Compiler(driver, 100) {}
42
43 void Init() const OVERRIDE;
44
45 void UnInit() const OVERRIDE;
46
47 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
48 OVERRIDE;
49
50 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
51 uint32_t access_flags,
52 InvokeType invoke_type,
53 uint16_t class_def_idx,
54 uint32_t method_idx,
55 jobject class_loader,
56 const DexFile& dex_file) const OVERRIDE;
57
58 CompiledMethod* JniCompile(uint32_t access_flags,
59 uint32_t method_idx,
60 const DexFile& dex_file) const OVERRIDE;
61
62 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
63 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
64
65 bool WriteElf(art::File* file,
66 OatWriter* oat_writer,
67 const std::vector<const art::DexFile*>& dex_files,
68 const std::string& android_root,
69 bool is_host) const
70 OVERRIDE
71 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
72
73 Backend* GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const OVERRIDE;
74
75 void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(QuickCompiler);
79};
80
Andreas Gampe785d2f22014-11-03 22:57:30 -080081static_assert(0U == static_cast<size_t>(kNone), "kNone not 0");
82static_assert(1U == static_cast<size_t>(kArm), "kArm not 1");
83static_assert(2U == static_cast<size_t>(kArm64), "kArm64 not 2");
84static_assert(3U == static_cast<size_t>(kThumb2), "kThumb2 not 3");
85static_assert(4U == static_cast<size_t>(kX86), "kX86 not 4");
86static_assert(5U == static_cast<size_t>(kX86_64), "kX86_64 not 5");
87static_assert(6U == static_cast<size_t>(kMips), "kMips not 6");
88static_assert(7U == static_cast<size_t>(kMips64), "kMips64 not 7");
Andreas Gampe53c913b2014-08-12 23:19:23 -070089
90// Additional disabled optimizations (over generally disabled) per instruction set.
91static constexpr uint32_t kDisabledOptimizationsPerISA[] = {
92 // 0 = kNone.
93 ~0U,
94 // 1 = kArm, unused (will use kThumb2).
95 ~0U,
96 // 2 = kArm64.
97 0,
98 // 3 = kThumb2.
99 0,
100 // 4 = kX86.
101 (1 << kLoadStoreElimination) |
102 0,
103 // 5 = kX86_64.
104 (1 << kLoadStoreElimination) |
105 0,
106 // 6 = kMips.
107 (1 << kLoadStoreElimination) |
108 (1 << kLoadHoisting) |
109 (1 << kSuppressLoads) |
110 (1 << kNullCheckElimination) |
111 (1 << kPromoteRegs) |
112 (1 << kTrackLiveTemps) |
113 (1 << kSafeOptimizations) |
114 (1 << kBBOpt) |
115 (1 << kMatch) |
116 (1 << kPromoteCompilerTemps) |
117 0,
118 // 7 = kMips64.
119 ~0U
120};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800121static_assert(sizeof(kDisabledOptimizationsPerISA) == 8 * sizeof(uint32_t),
122 "kDisabledOpts unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700123
124// Supported shorty types per instruction set. nullptr means that all are available.
125// Z : boolean
126// B : byte
127// S : short
128// C : char
129// I : int
130// J : long
131// F : float
132// D : double
133// L : reference(object, array)
134// V : void
135static const char* kSupportedTypes[] = {
136 // 0 = kNone.
137 "",
138 // 1 = kArm, unused (will use kThumb2).
139 "",
140 // 2 = kArm64.
141 nullptr,
142 // 3 = kThumb2.
143 nullptr,
144 // 4 = kX86.
145 nullptr,
146 // 5 = kX86_64.
147 nullptr,
148 // 6 = kMips.
149 nullptr,
150 // 7 = kMips64.
151 ""
152};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800153static_assert(sizeof(kSupportedTypes) == 8 * sizeof(char*), "kSupportedTypes unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700154
155static int kAllOpcodes[] = {
156 Instruction::NOP,
157 Instruction::MOVE,
158 Instruction::MOVE_FROM16,
159 Instruction::MOVE_16,
160 Instruction::MOVE_WIDE,
161 Instruction::MOVE_WIDE_FROM16,
162 Instruction::MOVE_WIDE_16,
163 Instruction::MOVE_OBJECT,
164 Instruction::MOVE_OBJECT_FROM16,
165 Instruction::MOVE_OBJECT_16,
166 Instruction::MOVE_RESULT,
167 Instruction::MOVE_RESULT_WIDE,
168 Instruction::MOVE_RESULT_OBJECT,
169 Instruction::MOVE_EXCEPTION,
170 Instruction::RETURN_VOID,
171 Instruction::RETURN,
172 Instruction::RETURN_WIDE,
173 Instruction::RETURN_OBJECT,
174 Instruction::CONST_4,
175 Instruction::CONST_16,
176 Instruction::CONST,
177 Instruction::CONST_HIGH16,
178 Instruction::CONST_WIDE_16,
179 Instruction::CONST_WIDE_32,
180 Instruction::CONST_WIDE,
181 Instruction::CONST_WIDE_HIGH16,
182 Instruction::CONST_STRING,
183 Instruction::CONST_STRING_JUMBO,
184 Instruction::CONST_CLASS,
185 Instruction::MONITOR_ENTER,
186 Instruction::MONITOR_EXIT,
187 Instruction::CHECK_CAST,
188 Instruction::INSTANCE_OF,
189 Instruction::ARRAY_LENGTH,
190 Instruction::NEW_INSTANCE,
191 Instruction::NEW_ARRAY,
192 Instruction::FILLED_NEW_ARRAY,
193 Instruction::FILLED_NEW_ARRAY_RANGE,
194 Instruction::FILL_ARRAY_DATA,
195 Instruction::THROW,
196 Instruction::GOTO,
197 Instruction::GOTO_16,
198 Instruction::GOTO_32,
199 Instruction::PACKED_SWITCH,
200 Instruction::SPARSE_SWITCH,
201 Instruction::CMPL_FLOAT,
202 Instruction::CMPG_FLOAT,
203 Instruction::CMPL_DOUBLE,
204 Instruction::CMPG_DOUBLE,
205 Instruction::CMP_LONG,
206 Instruction::IF_EQ,
207 Instruction::IF_NE,
208 Instruction::IF_LT,
209 Instruction::IF_GE,
210 Instruction::IF_GT,
211 Instruction::IF_LE,
212 Instruction::IF_EQZ,
213 Instruction::IF_NEZ,
214 Instruction::IF_LTZ,
215 Instruction::IF_GEZ,
216 Instruction::IF_GTZ,
217 Instruction::IF_LEZ,
218 Instruction::UNUSED_3E,
219 Instruction::UNUSED_3F,
220 Instruction::UNUSED_40,
221 Instruction::UNUSED_41,
222 Instruction::UNUSED_42,
223 Instruction::UNUSED_43,
224 Instruction::AGET,
225 Instruction::AGET_WIDE,
226 Instruction::AGET_OBJECT,
227 Instruction::AGET_BOOLEAN,
228 Instruction::AGET_BYTE,
229 Instruction::AGET_CHAR,
230 Instruction::AGET_SHORT,
231 Instruction::APUT,
232 Instruction::APUT_WIDE,
233 Instruction::APUT_OBJECT,
234 Instruction::APUT_BOOLEAN,
235 Instruction::APUT_BYTE,
236 Instruction::APUT_CHAR,
237 Instruction::APUT_SHORT,
238 Instruction::IGET,
239 Instruction::IGET_WIDE,
240 Instruction::IGET_OBJECT,
241 Instruction::IGET_BOOLEAN,
242 Instruction::IGET_BYTE,
243 Instruction::IGET_CHAR,
244 Instruction::IGET_SHORT,
245 Instruction::IPUT,
246 Instruction::IPUT_WIDE,
247 Instruction::IPUT_OBJECT,
248 Instruction::IPUT_BOOLEAN,
249 Instruction::IPUT_BYTE,
250 Instruction::IPUT_CHAR,
251 Instruction::IPUT_SHORT,
252 Instruction::SGET,
253 Instruction::SGET_WIDE,
254 Instruction::SGET_OBJECT,
255 Instruction::SGET_BOOLEAN,
256 Instruction::SGET_BYTE,
257 Instruction::SGET_CHAR,
258 Instruction::SGET_SHORT,
259 Instruction::SPUT,
260 Instruction::SPUT_WIDE,
261 Instruction::SPUT_OBJECT,
262 Instruction::SPUT_BOOLEAN,
263 Instruction::SPUT_BYTE,
264 Instruction::SPUT_CHAR,
265 Instruction::SPUT_SHORT,
266 Instruction::INVOKE_VIRTUAL,
267 Instruction::INVOKE_SUPER,
268 Instruction::INVOKE_DIRECT,
269 Instruction::INVOKE_STATIC,
270 Instruction::INVOKE_INTERFACE,
271 Instruction::RETURN_VOID_BARRIER,
272 Instruction::INVOKE_VIRTUAL_RANGE,
273 Instruction::INVOKE_SUPER_RANGE,
274 Instruction::INVOKE_DIRECT_RANGE,
275 Instruction::INVOKE_STATIC_RANGE,
276 Instruction::INVOKE_INTERFACE_RANGE,
277 Instruction::UNUSED_79,
278 Instruction::UNUSED_7A,
279 Instruction::NEG_INT,
280 Instruction::NOT_INT,
281 Instruction::NEG_LONG,
282 Instruction::NOT_LONG,
283 Instruction::NEG_FLOAT,
284 Instruction::NEG_DOUBLE,
285 Instruction::INT_TO_LONG,
286 Instruction::INT_TO_FLOAT,
287 Instruction::INT_TO_DOUBLE,
288 Instruction::LONG_TO_INT,
289 Instruction::LONG_TO_FLOAT,
290 Instruction::LONG_TO_DOUBLE,
291 Instruction::FLOAT_TO_INT,
292 Instruction::FLOAT_TO_LONG,
293 Instruction::FLOAT_TO_DOUBLE,
294 Instruction::DOUBLE_TO_INT,
295 Instruction::DOUBLE_TO_LONG,
296 Instruction::DOUBLE_TO_FLOAT,
297 Instruction::INT_TO_BYTE,
298 Instruction::INT_TO_CHAR,
299 Instruction::INT_TO_SHORT,
300 Instruction::ADD_INT,
301 Instruction::SUB_INT,
302 Instruction::MUL_INT,
303 Instruction::DIV_INT,
304 Instruction::REM_INT,
305 Instruction::AND_INT,
306 Instruction::OR_INT,
307 Instruction::XOR_INT,
308 Instruction::SHL_INT,
309 Instruction::SHR_INT,
310 Instruction::USHR_INT,
311 Instruction::ADD_LONG,
312 Instruction::SUB_LONG,
313 Instruction::MUL_LONG,
314 Instruction::DIV_LONG,
315 Instruction::REM_LONG,
316 Instruction::AND_LONG,
317 Instruction::OR_LONG,
318 Instruction::XOR_LONG,
319 Instruction::SHL_LONG,
320 Instruction::SHR_LONG,
321 Instruction::USHR_LONG,
322 Instruction::ADD_FLOAT,
323 Instruction::SUB_FLOAT,
324 Instruction::MUL_FLOAT,
325 Instruction::DIV_FLOAT,
326 Instruction::REM_FLOAT,
327 Instruction::ADD_DOUBLE,
328 Instruction::SUB_DOUBLE,
329 Instruction::MUL_DOUBLE,
330 Instruction::DIV_DOUBLE,
331 Instruction::REM_DOUBLE,
332 Instruction::ADD_INT_2ADDR,
333 Instruction::SUB_INT_2ADDR,
334 Instruction::MUL_INT_2ADDR,
335 Instruction::DIV_INT_2ADDR,
336 Instruction::REM_INT_2ADDR,
337 Instruction::AND_INT_2ADDR,
338 Instruction::OR_INT_2ADDR,
339 Instruction::XOR_INT_2ADDR,
340 Instruction::SHL_INT_2ADDR,
341 Instruction::SHR_INT_2ADDR,
342 Instruction::USHR_INT_2ADDR,
343 Instruction::ADD_LONG_2ADDR,
344 Instruction::SUB_LONG_2ADDR,
345 Instruction::MUL_LONG_2ADDR,
346 Instruction::DIV_LONG_2ADDR,
347 Instruction::REM_LONG_2ADDR,
348 Instruction::AND_LONG_2ADDR,
349 Instruction::OR_LONG_2ADDR,
350 Instruction::XOR_LONG_2ADDR,
351 Instruction::SHL_LONG_2ADDR,
352 Instruction::SHR_LONG_2ADDR,
353 Instruction::USHR_LONG_2ADDR,
354 Instruction::ADD_FLOAT_2ADDR,
355 Instruction::SUB_FLOAT_2ADDR,
356 Instruction::MUL_FLOAT_2ADDR,
357 Instruction::DIV_FLOAT_2ADDR,
358 Instruction::REM_FLOAT_2ADDR,
359 Instruction::ADD_DOUBLE_2ADDR,
360 Instruction::SUB_DOUBLE_2ADDR,
361 Instruction::MUL_DOUBLE_2ADDR,
362 Instruction::DIV_DOUBLE_2ADDR,
363 Instruction::REM_DOUBLE_2ADDR,
364 Instruction::ADD_INT_LIT16,
365 Instruction::RSUB_INT,
366 Instruction::MUL_INT_LIT16,
367 Instruction::DIV_INT_LIT16,
368 Instruction::REM_INT_LIT16,
369 Instruction::AND_INT_LIT16,
370 Instruction::OR_INT_LIT16,
371 Instruction::XOR_INT_LIT16,
372 Instruction::ADD_INT_LIT8,
373 Instruction::RSUB_INT_LIT8,
374 Instruction::MUL_INT_LIT8,
375 Instruction::DIV_INT_LIT8,
376 Instruction::REM_INT_LIT8,
377 Instruction::AND_INT_LIT8,
378 Instruction::OR_INT_LIT8,
379 Instruction::XOR_INT_LIT8,
380 Instruction::SHL_INT_LIT8,
381 Instruction::SHR_INT_LIT8,
382 Instruction::USHR_INT_LIT8,
383 Instruction::IGET_QUICK,
384 Instruction::IGET_WIDE_QUICK,
385 Instruction::IGET_OBJECT_QUICK,
386 Instruction::IPUT_QUICK,
387 Instruction::IPUT_WIDE_QUICK,
388 Instruction::IPUT_OBJECT_QUICK,
389 Instruction::INVOKE_VIRTUAL_QUICK,
390 Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
Fred Shih37f05ef2014-07-16 18:38:08 -0700391 Instruction::IPUT_BOOLEAN_QUICK,
392 Instruction::IPUT_BYTE_QUICK,
393 Instruction::IPUT_CHAR_QUICK,
394 Instruction::IPUT_SHORT_QUICK,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700395 Instruction::UNUSED_EF,
396 Instruction::UNUSED_F0,
397 Instruction::UNUSED_F1,
398 Instruction::UNUSED_F2,
399 Instruction::UNUSED_F3,
400 Instruction::UNUSED_F4,
401 Instruction::UNUSED_F5,
402 Instruction::UNUSED_F6,
403 Instruction::UNUSED_F7,
404 Instruction::UNUSED_F8,
405 Instruction::UNUSED_F9,
406 Instruction::UNUSED_FA,
407 Instruction::UNUSED_FB,
408 Instruction::UNUSED_FC,
409 Instruction::UNUSED_FD,
410 Instruction::UNUSED_FE,
411 Instruction::UNUSED_FF,
412 // ----- ExtendedMIROpcode -----
413 kMirOpPhi,
414 kMirOpCopy,
415 kMirOpFusedCmplFloat,
416 kMirOpFusedCmpgFloat,
417 kMirOpFusedCmplDouble,
418 kMirOpFusedCmpgDouble,
419 kMirOpFusedCmpLong,
420 kMirOpNop,
421 kMirOpNullCheck,
422 kMirOpRangeCheck,
423 kMirOpDivZeroCheck,
424 kMirOpCheck,
425 kMirOpCheckPart2,
426 kMirOpSelect,
427};
428
Zheng Xu5667fdb2014-10-23 18:29:55 +0800429static int kInvokeOpcodes[] = {
430 Instruction::INVOKE_VIRTUAL,
431 Instruction::INVOKE_SUPER,
432 Instruction::INVOKE_DIRECT,
433 Instruction::INVOKE_STATIC,
434 Instruction::INVOKE_INTERFACE,
435 Instruction::INVOKE_VIRTUAL_RANGE,
436 Instruction::INVOKE_SUPER_RANGE,
437 Instruction::INVOKE_DIRECT_RANGE,
438 Instruction::INVOKE_STATIC_RANGE,
439 Instruction::INVOKE_INTERFACE_RANGE,
440 Instruction::INVOKE_VIRTUAL_QUICK,
441 Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
442};
443
Andreas Gampe53c913b2014-08-12 23:19:23 -0700444// Unsupported opcodes. nullptr can be used when everything is supported. Size of the lists is
445// recorded below.
446static const int* kUnsupportedOpcodes[] = {
447 // 0 = kNone.
448 kAllOpcodes,
449 // 1 = kArm, unused (will use kThumb2).
450 kAllOpcodes,
451 // 2 = kArm64.
452 nullptr,
453 // 3 = kThumb2.
454 nullptr,
455 // 4 = kX86.
456 nullptr,
457 // 5 = kX86_64.
458 nullptr,
459 // 6 = kMips.
460 nullptr,
461 // 7 = kMips64.
462 kAllOpcodes
463};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800464static_assert(sizeof(kUnsupportedOpcodes) == 8 * sizeof(int*), "kUnsupportedOpcodes unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700465
466// Size of the arrays stored above.
467static const size_t kUnsupportedOpcodesSize[] = {
468 // 0 = kNone.
469 arraysize(kAllOpcodes),
470 // 1 = kArm, unused (will use kThumb2).
471 arraysize(kAllOpcodes),
472 // 2 = kArm64.
473 0,
474 // 3 = kThumb2.
475 0,
476 // 4 = kX86.
477 0,
478 // 5 = kX86_64.
479 0,
480 // 6 = kMips.
481 0,
482 // 7 = kMips64.
483 arraysize(kAllOpcodes),
484};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800485static_assert(sizeof(kUnsupportedOpcodesSize) == 8 * sizeof(size_t),
486 "kUnsupportedOpcodesSize unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700487
488// The maximum amount of Dalvik register in a method for which we will start compiling. Tries to
489// avoid an abort when we need to manage more SSA registers than we can.
490static constexpr size_t kMaxAllowedDalvikRegisters = INT16_MAX / 2;
491
492static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) {
493 const char* supported_types = kSupportedTypes[instruction_set];
494 if (supported_types == nullptr) {
495 // Everything available.
496 return true;
497 }
498
499 uint32_t shorty_size = strlen(shorty);
500 CHECK_GE(shorty_size, 1u);
501
502 for (uint32_t i = 0; i < shorty_size; i++) {
503 if (strchr(supported_types, shorty[i]) == nullptr) {
504 return false;
505 }
506 }
507 return true;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700508}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700509
510// Skip the method that we do not support currently.
511bool QuickCompiler::CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
512 CompilationUnit* cu) const {
513 // This is a limitation in mir_graph. See MirGraph::SetNumSSARegs.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700514 if (cu->mir_graph->GetNumOfCodeAndTempVRs() > kMaxAllowedDalvikRegisters) {
515 VLOG(compiler) << "Too many dalvik registers : " << cu->mir_graph->GetNumOfCodeAndTempVRs();
Andreas Gampe53c913b2014-08-12 23:19:23 -0700516 return false;
517 }
518
519 // Check whether we do have limitations at all.
520 if (kSupportedTypes[cu->instruction_set] == nullptr &&
521 kUnsupportedOpcodesSize[cu->instruction_set] == 0U) {
522 return true;
523 }
524
525 // Check if we can compile the prototype.
526 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
527 if (!CanCompileShorty(shorty, cu->instruction_set)) {
528 VLOG(compiler) << "Unsupported shorty : " << shorty;
529 return false;
530 }
531
532 const int *unsupport_list = kUnsupportedOpcodes[cu->instruction_set];
533 int unsupport_list_size = kUnsupportedOpcodesSize[cu->instruction_set];
534
535 for (unsigned int idx = 0; idx < cu->mir_graph->GetNumBlocks(); idx++) {
536 BasicBlock* bb = cu->mir_graph->GetBasicBlock(idx);
537 if (bb == NULL) continue;
538 if (bb->block_type == kDead) continue;
539 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
540 int opcode = mir->dalvikInsn.opcode;
541 // Check if we support the byte code.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800542 if (std::find(unsupport_list, unsupport_list + unsupport_list_size, opcode)
543 != unsupport_list + unsupport_list_size) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700544 if (!MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
545 VLOG(compiler) << "Unsupported dalvik byte code : "
546 << mir->dalvikInsn.opcode;
547 } else {
548 VLOG(compiler) << "Unsupported extended MIR opcode : "
549 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
550 }
551 return false;
552 }
553 // Check if it invokes a prototype that we cannot support.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800554 if (std::find(kInvokeOpcodes, kInvokeOpcodes + arraysize(kInvokeOpcodes), opcode)
555 != kInvokeOpcodes + arraysize(kInvokeOpcodes)) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700556 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
557 const char* invoke_method_shorty = dex_file.GetMethodShorty(
558 dex_file.GetMethodId(invoke_method_idx));
559 if (!CanCompileShorty(invoke_method_shorty, cu->instruction_set)) {
560 VLOG(compiler) << "Unsupported to invoke '"
561 << PrettyMethod(invoke_method_idx, dex_file)
562 << "' with shorty : " << invoke_method_shorty;
563 return false;
564 }
565 }
566 }
567 }
568 return true;
569}
570
571void QuickCompiler::InitCompilationUnit(CompilationUnit& cu) const {
572 // Disable optimizations according to instruction set.
573 cu.disable_opt |= kDisabledOptimizationsPerISA[cu.instruction_set];
574}
575
576void QuickCompiler::Init() const {
577 CHECK(GetCompilerDriver()->GetCompilerContext() == nullptr);
578}
579
580void QuickCompiler::UnInit() const {
581 CHECK(GetCompilerDriver()->GetCompilerContext() == nullptr);
582}
583
584CompiledMethod* QuickCompiler::Compile(const DexFile::CodeItem* code_item,
585 uint32_t access_flags,
586 InvokeType invoke_type,
587 uint16_t class_def_idx,
588 uint32_t method_idx,
589 jobject class_loader,
590 const DexFile& dex_file) const {
591 CompiledMethod* method = TryCompileWithSeaIR(code_item,
592 access_flags,
593 invoke_type,
594 class_def_idx,
595 method_idx,
596 class_loader,
597 dex_file);
598 if (method != nullptr) {
599 return method;
600 }
601
602 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use
603 // build default.
604 CompilerDriver* driver = GetCompilerDriver();
605 return CompileOneMethod(driver, this, code_item, access_flags, invoke_type, class_def_idx,
606 method_idx, class_loader, dex_file, nullptr /* use thread llvm_info */);
607}
608
609CompiledMethod* QuickCompiler::JniCompile(uint32_t access_flags,
610 uint32_t method_idx,
611 const DexFile& dex_file) const {
612 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
613}
614
615uintptr_t QuickCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Mathieu Chartier130914e2014-11-18 15:34:09 -0800616 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
617 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
Andreas Gampe53c913b2014-08-12 23:19:23 -0700618}
619
620bool QuickCompiler::WriteElf(art::File* file,
621 OatWriter* oat_writer,
622 const std::vector<const art::DexFile*>& dex_files,
623 const std::string& android_root,
624 bool is_host) const {
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000625 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
626 *GetCompilerDriver());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700627}
628
629Backend* QuickCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700630 UNUSED(compilation_unit);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700631 Mir2Lir* mir_to_lir = nullptr;
632 switch (cu->instruction_set) {
633 case kThumb2:
634 mir_to_lir = ArmCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
635 break;
636 case kArm64:
637 mir_to_lir = Arm64CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
638 break;
639 case kMips:
640 mir_to_lir = MipsCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
641 break;
642 case kX86:
643 // Fall-through.
644 case kX86_64:
645 mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
646 break;
647 default:
648 LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
649 }
650
651 /* The number of compiler temporaries depends on backend so set it up now if possible */
652 if (mir_to_lir) {
653 size_t max_temps = mir_to_lir->GetMaxPossibleCompilerTemps();
654 bool set_max = cu->mir_graph->SetMaxAvailableNonSpecialCompilerTemps(max_temps);
655 CHECK(set_max);
656 }
657 return mir_to_lir;
658}
659
660
661Compiler* CreateQuickCompiler(CompilerDriver* driver) {
662 return new QuickCompiler(driver);
663}
664
665} // namespace art