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/oat.h"
26 #include "optimizing/optimizing_compiler.h"
27
28 namespace art HIDDEN {
29
Create(const CompilerOptions & compiler_options,CompiledCodeStorage * storage)30 Compiler* Compiler::Create(const CompilerOptions& compiler_options, CompiledCodeStorage* storage) {
31 // Check that oat version when runtime was compiled matches the oat version of the compiler.
32 constexpr std::array<uint8_t, 4> compiler_oat_version = OatHeader::kOatVersion;
33 OatHeader::CheckOatVersion(compiler_oat_version);
34 return CreateOptimizingCompiler(compiler_options, storage);
35 }
36
IsPathologicalCase(const dex::CodeItem & code_item,uint32_t method_idx,const DexFile & dex_file)37 bool Compiler::IsPathologicalCase(const dex::CodeItem& code_item,
38 uint32_t method_idx,
39 const DexFile& dex_file) {
40 /*
41 * Skip compilation for pathologically large methods - either by instruction count or num vregs.
42 * Dalvik uses 16-bit uints for instruction and register counts. We'll limit to a quarter
43 * of that, which also guarantees we cannot overflow our 16-bit internal Quick SSA name space.
44 */
45 CodeItemDataAccessor accessor(dex_file, &code_item);
46 if (accessor.InsnsSizeInCodeUnits() >= UINT16_MAX / 4) {
47 LOG(INFO) << "Method exceeds compiler instruction limit: "
48 << accessor.InsnsSizeInCodeUnits()
49 << " in " << dex_file.PrettyMethod(method_idx);
50 return true;
51 }
52 if (accessor.RegistersSize() >= UINT16_MAX / 4) {
53 LOG(INFO) << "Method exceeds compiler virtual register limit: "
54 << accessor.RegistersSize() << " in " << dex_file.PrettyMethod(method_idx);
55 return true;
56 }
57 return false;
58 }
59
60 } // namespace art
61