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