blob: cde6ae9330eae5877fe78b583063274de7e6ab76 [file] [log] [blame]
Mathieu Chartier5bdab122015-01-26 18:30:19 -08001/*
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
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000019#include <fstream>
Vladimir Marko2ef01102019-02-05 15:05:10 +000020#include <string_view>
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000021
Andreas Gampe097f34c2017-08-23 08:57:51 -070022#include "android-base/stringprintf.h"
23
Vladimir Markoa0431112018-06-25 09:32:54 +010024#include "arch/instruction_set.h"
25#include "arch/instruction_set_features.h"
Andreas Gampe2a5d7282018-01-02 11:53:35 -080026#include "base/runtime_debug.h"
Vladimir Marko44ca0752019-07-29 10:18:25 +010027#include "base/string_view_cpp20.h"
Andreas Gampe097f34c2017-08-23 08:57:51 -070028#include "base/variant_map.h"
Vladimir Marko2afaff72018-11-30 17:01:50 +000029#include "class_linker.h"
Andreas Gampe097f34c2017-08-23 08:57:51 -070030#include "cmdline_parser.h"
31#include "compiler_options_map-inl.h"
Vladimir Marko2afaff72018-11-30 17:01:50 +000032#include "dex/dex_file-inl.h"
33#include "dex/verification_results.h"
34#include "dex/verified_method.h"
Roland Levillain2b03a1f2017-06-06 16:09:59 +010035#include "runtime.h"
Vladimir Marko2afaff72018-11-30 17:01:50 +000036#include "scoped_thread_state_change-inl.h"
Andreas Gampe097f34c2017-08-23 08:57:51 -070037#include "simple_compiler_options_map.h"
Roland Levillain2b03a1f2017-06-06 16:09:59 +010038
Mathieu Chartier5bdab122015-01-26 18:30:19 -080039namespace art {
40
41CompilerOptions::CompilerOptions()
Richard Uhlerf4b34872016-04-13 11:03:46 -070042 : compiler_filter_(CompilerFilter::kDefaultCompilerFilter),
Mathieu Chartier5bdab122015-01-26 18:30:19 -080043 huge_method_threshold_(kDefaultHugeMethodThreshold),
44 large_method_threshold_(kDefaultLargeMethodThreshold),
Mathieu Chartier5bdab122015-01-26 18:30:19 -080045 num_dex_methods_threshold_(kDefaultNumDexMethodsThreshold),
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000046 inline_max_code_units_(kUnsetInlineMaxCodeUnits),
Vladimir Markoa0431112018-06-25 09:32:54 +010047 instruction_set_(kRuntimeISA == InstructionSet::kArm ? InstructionSet::kThumb2 : kRuntimeISA),
48 instruction_set_features_(nullptr),
Vladimir Markodc4bcce2018-06-21 16:15:42 +010049 no_inline_from_(),
Vladimir Marko213ee2d2018-06-22 11:56:34 +010050 dex_files_for_oat_file_(),
Vladimir Markodc4bcce2018-06-21 16:15:42 +010051 image_classes_(),
Vladimir Marko2afaff72018-11-30 17:01:50 +000052 verification_results_(nullptr),
Vladimir Marko9c4b9702018-11-14 15:09:02 +000053 image_type_(ImageType::kNone),
Vladimir Marko6be1dbd2018-11-13 13:09:51 +000054 compiling_with_core_image_(false),
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +010055 baseline_(false),
Andreas Gampe7b2f09e2015-03-02 14:07:33 -080056 debuggable_(false),
David Srbecky8363c772015-05-28 16:12:43 +010057 generate_debug_info_(kDefaultGenerateDebugInfo),
David Srbecky5b1c2ca2016-01-25 17:32:41 +000058 generate_mini_debug_info_(kDefaultGenerateMiniDebugInfo),
Alexey Alexandrovab40c112016-09-19 09:33:49 -070059 generate_build_id_(false),
Mathieu Chartier5bdab122015-01-26 18:30:19 -080060 implicit_null_checks_(true),
61 implicit_so_checks_(true),
62 implicit_suspend_checks_(false),
63 compile_pic_(false),
Nicolas Geoffray2d8801f2017-11-28 15:50:07 +000064 dump_timings_(false),
Vladimir Marko2da52b02018-05-08 16:31:34 +010065 dump_pass_timings_(false),
Nicolas Geoffray2d8801f2017-11-28 15:50:07 +000066 dump_stats_(false),
Vladimir Markoa0431112018-06-25 09:32:54 +010067 top_k_profile_threshold_(kDefaultTopKProfileThreshold),
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +000068 profile_compilation_info_(nullptr),
Nicolas Geoffray57c47042017-06-29 11:31:39 +010069 verbose_methods_(),
Andreas Gampe6cf49e52015-03-05 13:08:45 -080070 abort_on_hard_verifier_failure_(false),
Andreas Gampef39208f2017-10-19 15:06:59 -070071 abort_on_soft_verifier_failure_(false),
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +000072 init_failure_output_(nullptr),
73 dump_cfg_file_name_(""),
Andreas Gampeace0dc12016-01-20 13:33:13 -080074 dump_cfg_append_(false),
Matthew Gharrity2cd05b72016-08-03 16:57:37 -070075 force_determinism_(false),
Andreas Gampecac31ad2017-11-06 20:01:17 -080076 deduplicate_code_(true),
Nicolas Geoffray8d728322018-01-18 22:44:32 +000077 count_hotness_in_compiled_code_(false),
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -070078 resolve_startup_const_strings_(false),
Mathieu Chartier5132e0d2019-07-10 09:38:48 -070079 initialize_app_image_classes_(false),
Andreas Gampe5c803112018-04-13 17:28:34 -070080 check_profiled_methods_(ProfileMethodsCheck::kNone),
Mathieu Chartier1a842962018-11-13 15:09:51 -080081 max_image_block_size_(std::numeric_limits<uint32_t>::max()),
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070082 register_allocation_strategy_(RegisterAllocator::kRegisterAllocatorDefault),
83 passes_to_run_(nullptr) {
Mathieu Chartier5bdab122015-01-26 18:30:19 -080084}
85
Vladimir Markob163bb72015-03-31 21:49:49 +010086CompilerOptions::~CompilerOptions() {
Vladimir Markodc4bcce2018-06-21 16:15:42 +010087 // Everything done by member destructors.
88 // The definitions of classes forward-declared in the header have now been #included.
Vladimir Markob163bb72015-03-31 21:49:49 +010089}
90
Andreas Gampe2a5d7282018-01-02 11:53:35 -080091namespace {
92
93bool kEmitRuntimeReadBarrierChecks = kIsDebugBuild &&
94 RegisterRuntimeDebugFlag(&kEmitRuntimeReadBarrierChecks);
95
96} // namespace
97
Roland Levillain2b03a1f2017-06-06 16:09:59 +010098bool CompilerOptions::EmitRunTimeChecksInDebugMode() const {
Andreas Gampe2a5d7282018-01-02 11:53:35 -080099 // Run-time checks (e.g. Marking Register checks) are only emitted in slow-debug mode.
100 return kEmitRuntimeReadBarrierChecks;
Roland Levillain2b03a1f2017-06-06 16:09:59 +0100101}
102
Andreas Gampe097f34c2017-08-23 08:57:51 -0700103bool CompilerOptions::ParseDumpInitFailures(const std::string& option, std::string* error_msg) {
104 init_failure_output_.reset(new std::ofstream(option));
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000105 if (init_failure_output_.get() == nullptr) {
Andreas Gampe097f34c2017-08-23 08:57:51 -0700106 *error_msg = "Failed to construct std::ofstream";
107 return false;
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000108 } else if (init_failure_output_->fail()) {
Andreas Gampe097f34c2017-08-23 08:57:51 -0700109 *error_msg = android::base::StringPrintf(
110 "Failed to open %s for writing the initialization failures.", option.c_str());
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000111 init_failure_output_.reset();
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000112 return false;
113 }
114 return true;
115}
116
Andreas Gampe097f34c2017-08-23 08:57:51 -0700117bool CompilerOptions::ParseRegisterAllocationStrategy(const std::string& option,
118 std::string* error_msg) {
119 if (option == "linear-scan") {
120 register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorLinearScan;
121 } else if (option == "graph-color") {
122 register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorGraphColor;
123 } else {
124 *error_msg = "Unrecognized register allocation strategy. Try linear-scan, or graph-color.";
125 return false;
126 }
127 return true;
128}
129
Andreas Gampe097f34c2017-08-23 08:57:51 -0700130bool CompilerOptions::ParseCompilerOptions(const std::vector<std::string>& options,
131 bool ignore_unrecognized,
132 std::string* error_msg) {
133 auto parser = CreateSimpleParser(ignore_unrecognized);
134 CmdlineResult parse_result = parser.Parse(options);
135 if (!parse_result.IsSuccess()) {
136 *error_msg = parse_result.GetMessage();
137 return false;
138 }
139
140 SimpleParseArgumentMap args = parser.ReleaseArgumentsMap();
141 return ReadCompilerOptions(args, this, error_msg);
142}
143
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100144bool CompilerOptions::IsImageClass(const char* descriptor) const {
145 // Historical note: We used to hold the set indirectly and there was a distinction between an
146 // empty set and a null, null meaning to include all classes. However, the distiction has been
147 // removed; if we don't have a profile, we treat it as an empty set of classes. b/77340429
Vladimir Marko2ef01102019-02-05 15:05:10 +0000148 return image_classes_.find(std::string_view(descriptor)) != image_classes_.end();
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100149}
150
Vladimir Marko2afaff72018-11-30 17:01:50 +0000151const VerificationResults* CompilerOptions::GetVerificationResults() const {
152 DCHECK(Runtime::Current()->IsAotCompiler());
153 return verification_results_;
154}
155
156const VerifiedMethod* CompilerOptions::GetVerifiedMethod(const DexFile* dex_file,
157 uint32_t method_idx) const {
158 MethodReference ref(dex_file, method_idx);
159 return verification_results_->GetVerifiedMethod(ref);
160}
161
162bool CompilerOptions::IsMethodVerifiedWithoutFailures(uint32_t method_idx,
163 uint16_t class_def_idx,
164 const DexFile& dex_file) const {
165 const VerifiedMethod* verified_method = GetVerifiedMethod(&dex_file, method_idx);
166 if (verified_method != nullptr) {
167 return !verified_method->HasVerificationFailures();
168 }
169
170 // If we can't find verification metadata, check if this is a system class (we trust that system
171 // classes have their methods verified). If it's not, be conservative and assume the method
172 // has not been verified successfully.
173
174 // TODO: When compiling the boot image it should be safe to assume that everything is verified,
175 // even if methods are not found in the verification cache.
176 const char* descriptor = dex_file.GetClassDescriptor(dex_file.GetClassDef(class_def_idx));
177 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
178 Thread* self = Thread::Current();
179 ScopedObjectAccess soa(self);
180 bool is_system_class = class_linker->FindSystemClass(self, descriptor) != nullptr;
181 if (!is_system_class) {
182 self->ClearException();
183 }
184 return is_system_class;
185}
186
Vladimir Marko038924b2019-02-19 15:09:35 +0000187bool CompilerOptions::IsCoreImageFilename(const std::string& boot_image_filename) {
Vladimir Marko44ca0752019-07-29 10:18:25 +0100188 std::string_view filename(boot_image_filename);
189 size_t colon_pos = filename.find(':');
190 if (colon_pos != std::string_view::npos) {
191 filename = filename.substr(0u, colon_pos);
192 }
Vladimir Marko038924b2019-02-19 15:09:35 +0000193 // Look for "core.art" or "core-*.art".
Vladimir Marko44ca0752019-07-29 10:18:25 +0100194 if (EndsWith(filename, "core.art")) {
Vladimir Marko038924b2019-02-19 15:09:35 +0000195 return true;
196 }
Vladimir Marko44ca0752019-07-29 10:18:25 +0100197 if (!EndsWith(filename, ".art")) {
Vladimir Marko038924b2019-02-19 15:09:35 +0000198 return false;
199 }
Vladimir Marko44ca0752019-07-29 10:18:25 +0100200 size_t slash_pos = filename.rfind('/');
Vladimir Marko038924b2019-02-19 15:09:35 +0000201 if (slash_pos == std::string::npos) {
Vladimir Marko44ca0752019-07-29 10:18:25 +0100202 return StartsWith(filename, "core-");
Vladimir Marko038924b2019-02-19 15:09:35 +0000203 }
Vladimir Marko44ca0752019-07-29 10:18:25 +0100204 return filename.compare(slash_pos + 1, 5u, "core-") == 0;
Vladimir Marko038924b2019-02-19 15:09:35 +0000205}
206
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800207} // namespace art