1 /*
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 "compiler.h"
18 
19 #include <android-base/logging.h>
20 
21 #include "base/macros.h"
22 #include "base/utils.h"
23 #include "dex/code_item_accessors-inl.h"
24 #include "dex/dex_file.h"
25 #include "oat.h"
26 #include "optimizing/optimizing_compiler.h"
27 
28 namespace art {
29 
Create(const CompilerOptions & compiler_options,CompiledMethodStorage * storage,Compiler::Kind kind)30 Compiler* Compiler::Create(const CompilerOptions& compiler_options,
31                            CompiledMethodStorage* storage,
32                            Compiler::Kind kind) {
33   // Check that oat version when runtime was compiled matches the oat version of the compiler.
34   constexpr std::array<uint8_t, 4> compiler_oat_version = OatHeader::kOatVersion;
35   OatHeader::CheckOatVersion(compiler_oat_version);
36   switch (kind) {
37     case kQuick:
38       // TODO: Remove Quick in options.
39     case kOptimizing:
40       return CreateOptimizingCompiler(compiler_options, storage);
41 
42     default:
43       LOG(FATAL) << "UNREACHABLE";
44       UNREACHABLE();
45   }
46 }
47 
IsPathologicalCase(const dex::CodeItem & code_item,uint32_t method_idx,const DexFile & dex_file)48 bool Compiler::IsPathologicalCase(const dex::CodeItem& code_item,
49                                   uint32_t method_idx,
50                                   const DexFile& dex_file) {
51   /*
52    * Skip compilation for pathologically large methods - either by instruction count or num vregs.
53    * Dalvik uses 16-bit uints for instruction and register counts.  We'll limit to a quarter
54    * of that, which also guarantees we cannot overflow our 16-bit internal Quick SSA name space.
55    */
56   CodeItemDataAccessor accessor(dex_file, &code_item);
57   if (accessor.InsnsSizeInCodeUnits() >= UINT16_MAX / 4) {
58     LOG(INFO) << "Method exceeds compiler instruction limit: "
59               << accessor.InsnsSizeInCodeUnits()
60               << " in " << dex_file.PrettyMethod(method_idx);
61     return true;
62   }
63   if (accessor.RegistersSize() >= UINT16_MAX / 4) {
64     LOG(INFO) << "Method exceeds compiler virtual register limit: "
65               << accessor.RegistersSize() << " in " << dex_file.PrettyMethod(method_idx);
66     return true;
67   }
68   return false;
69 }
70 
71 }  // namespace art
72