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