1 /*
2 * Copyright (C) 2015 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_options.h"
18
19 #include <fstream>
20 #include <string_view>
21
22 #include "android-base/stringprintf.h"
23 #include "android-base/strings.h"
24
25 #include "arch/instruction_set.h"
26 #include "arch/instruction_set_features.h"
27 #include "base/runtime_debug.h"
28 #include "base/variant_map.h"
29 #include "class_linker.h"
30 #include "cmdline_parser.h"
31 #include "compiler_options_map-inl.h"
32 #include "dex/dex_file-inl.h"
33 #include "dex/verification_results.h"
34 #include "dex/verified_method.h"
35 #include "runtime.h"
36 #include "scoped_thread_state_change-inl.h"
37 #include "simple_compiler_options_map.h"
38
39 namespace art {
40
CompilerOptions()41 CompilerOptions::CompilerOptions()
42 : compiler_filter_(CompilerFilter::kDefaultCompilerFilter),
43 huge_method_threshold_(kDefaultHugeMethodThreshold),
44 large_method_threshold_(kDefaultLargeMethodThreshold),
45 small_method_threshold_(kDefaultSmallMethodThreshold),
46 tiny_method_threshold_(kDefaultTinyMethodThreshold),
47 num_dex_methods_threshold_(kDefaultNumDexMethodsThreshold),
48 inline_max_code_units_(kUnsetInlineMaxCodeUnits),
49 instruction_set_(kRuntimeISA == InstructionSet::kArm ? InstructionSet::kThumb2 : kRuntimeISA),
50 instruction_set_features_(nullptr),
51 no_inline_from_(),
52 dex_files_for_oat_file_(),
53 image_classes_(),
54 verification_results_(nullptr),
55 image_type_(ImageType::kNone),
56 compiling_with_core_image_(false),
57 baseline_(false),
58 debuggable_(false),
59 generate_debug_info_(kDefaultGenerateDebugInfo),
60 generate_mini_debug_info_(kDefaultGenerateMiniDebugInfo),
61 generate_build_id_(false),
62 implicit_null_checks_(true),
63 implicit_so_checks_(true),
64 implicit_suspend_checks_(false),
65 compile_pic_(false),
66 dump_timings_(false),
67 dump_pass_timings_(false),
68 dump_stats_(false),
69 top_k_profile_threshold_(kDefaultTopKProfileThreshold),
70 profile_compilation_info_(nullptr),
71 verbose_methods_(),
72 abort_on_hard_verifier_failure_(false),
73 abort_on_soft_verifier_failure_(false),
74 init_failure_output_(nullptr),
75 dump_cfg_file_name_(""),
76 dump_cfg_append_(false),
77 force_determinism_(false),
78 deduplicate_code_(true),
79 count_hotness_in_compiled_code_(false),
80 resolve_startup_const_strings_(false),
81 check_profiled_methods_(ProfileMethodsCheck::kNone),
82 max_image_block_size_(std::numeric_limits<uint32_t>::max()),
83 register_allocation_strategy_(RegisterAllocator::kRegisterAllocatorDefault),
84 passes_to_run_(nullptr) {
85 }
86
~CompilerOptions()87 CompilerOptions::~CompilerOptions() {
88 // Everything done by member destructors.
89 // The definitions of classes forward-declared in the header have now been #included.
90 }
91
92 namespace {
93
94 bool kEmitRuntimeReadBarrierChecks = kIsDebugBuild &&
95 RegisterRuntimeDebugFlag(&kEmitRuntimeReadBarrierChecks);
96
97 } // namespace
98
EmitRunTimeChecksInDebugMode() const99 bool CompilerOptions::EmitRunTimeChecksInDebugMode() const {
100 // Run-time checks (e.g. Marking Register checks) are only emitted in slow-debug mode.
101 return kEmitRuntimeReadBarrierChecks;
102 }
103
ParseDumpInitFailures(const std::string & option,std::string * error_msg)104 bool CompilerOptions::ParseDumpInitFailures(const std::string& option, std::string* error_msg) {
105 init_failure_output_.reset(new std::ofstream(option));
106 if (init_failure_output_.get() == nullptr) {
107 *error_msg = "Failed to construct std::ofstream";
108 return false;
109 } else if (init_failure_output_->fail()) {
110 *error_msg = android::base::StringPrintf(
111 "Failed to open %s for writing the initialization failures.", option.c_str());
112 init_failure_output_.reset();
113 return false;
114 }
115 return true;
116 }
117
ParseRegisterAllocationStrategy(const std::string & option,std::string * error_msg)118 bool CompilerOptions::ParseRegisterAllocationStrategy(const std::string& option,
119 std::string* error_msg) {
120 if (option == "linear-scan") {
121 register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorLinearScan;
122 } else if (option == "graph-color") {
123 register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorGraphColor;
124 } else {
125 *error_msg = "Unrecognized register allocation strategy. Try linear-scan, or graph-color.";
126 return false;
127 }
128 return true;
129 }
130
ParseCompilerOptions(const std::vector<std::string> & options,bool ignore_unrecognized,std::string * error_msg)131 bool CompilerOptions::ParseCompilerOptions(const std::vector<std::string>& options,
132 bool ignore_unrecognized,
133 std::string* error_msg) {
134 auto parser = CreateSimpleParser(ignore_unrecognized);
135 CmdlineResult parse_result = parser.Parse(options);
136 if (!parse_result.IsSuccess()) {
137 *error_msg = parse_result.GetMessage();
138 return false;
139 }
140
141 SimpleParseArgumentMap args = parser.ReleaseArgumentsMap();
142 return ReadCompilerOptions(args, this, error_msg);
143 }
144
IsImageClass(const char * descriptor) const145 bool CompilerOptions::IsImageClass(const char* descriptor) const {
146 // Historical note: We used to hold the set indirectly and there was a distinction between an
147 // empty set and a null, null meaning to include all classes. However, the distiction has been
148 // removed; if we don't have a profile, we treat it as an empty set of classes. b/77340429
149 return image_classes_.find(std::string_view(descriptor)) != image_classes_.end();
150 }
151
GetVerificationResults() const152 const VerificationResults* CompilerOptions::GetVerificationResults() const {
153 DCHECK(Runtime::Current()->IsAotCompiler());
154 return verification_results_;
155 }
156
GetVerifiedMethod(const DexFile * dex_file,uint32_t method_idx) const157 const VerifiedMethod* CompilerOptions::GetVerifiedMethod(const DexFile* dex_file,
158 uint32_t method_idx) const {
159 MethodReference ref(dex_file, method_idx);
160 return verification_results_->GetVerifiedMethod(ref);
161 }
162
IsMethodVerifiedWithoutFailures(uint32_t method_idx,uint16_t class_def_idx,const DexFile & dex_file) const163 bool CompilerOptions::IsMethodVerifiedWithoutFailures(uint32_t method_idx,
164 uint16_t class_def_idx,
165 const DexFile& dex_file) const {
166 const VerifiedMethod* verified_method = GetVerifiedMethod(&dex_file, method_idx);
167 if (verified_method != nullptr) {
168 return !verified_method->HasVerificationFailures();
169 }
170
171 // If we can't find verification metadata, check if this is a system class (we trust that system
172 // classes have their methods verified). If it's not, be conservative and assume the method
173 // has not been verified successfully.
174
175 // TODO: When compiling the boot image it should be safe to assume that everything is verified,
176 // even if methods are not found in the verification cache.
177 const char* descriptor = dex_file.GetClassDescriptor(dex_file.GetClassDef(class_def_idx));
178 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
179 Thread* self = Thread::Current();
180 ScopedObjectAccess soa(self);
181 bool is_system_class = class_linker->FindSystemClass(self, descriptor) != nullptr;
182 if (!is_system_class) {
183 self->ClearException();
184 }
185 return is_system_class;
186 }
187
IsCoreImageFilename(const std::string & boot_image_filename)188 bool CompilerOptions::IsCoreImageFilename(const std::string& boot_image_filename) {
189 // Look for "core.art" or "core-*.art".
190 if (android::base::EndsWith(boot_image_filename, "core.art")) {
191 return true;
192 }
193 if (!android::base::EndsWith(boot_image_filename, ".art")) {
194 return false;
195 }
196 size_t slash_pos = boot_image_filename.rfind('/');
197 if (slash_pos == std::string::npos) {
198 return android::base::StartsWith(boot_image_filename, "core-");
199 }
200 return boot_image_filename.compare(slash_pos + 1, 5u, "core-") == 0;
201 }
202
203 } // namespace art
204