1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "compiler_options.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <fstream>
20*795d594fSAndroid Build Coastguard Worker #include <string_view>
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
25*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set_features.h"
26*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "base/runtime_debug.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/variant_map.h"
29*795d594fSAndroid Build Coastguard Worker #include "class_linker.h"
30*795d594fSAndroid Build Coastguard Worker #include "cmdline_parser.h"
31*795d594fSAndroid Build Coastguard Worker #include "compiler_options_map-inl.h"
32*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file-inl.h"
33*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
34*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
35*795d594fSAndroid Build Coastguard Worker #include "simple_compiler_options_map.h"
36*795d594fSAndroid Build Coastguard Worker
37*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
38*795d594fSAndroid Build Coastguard Worker
CompilerOptions()39*795d594fSAndroid Build Coastguard Worker CompilerOptions::CompilerOptions()
40*795d594fSAndroid Build Coastguard Worker : compiler_filter_(CompilerFilter::kDefaultCompilerFilter),
41*795d594fSAndroid Build Coastguard Worker huge_method_threshold_(kDefaultHugeMethodThreshold),
42*795d594fSAndroid Build Coastguard Worker inline_max_code_units_(kUnsetInlineMaxCodeUnits),
43*795d594fSAndroid Build Coastguard Worker instruction_set_(kRuntimeISA == InstructionSet::kArm ? InstructionSet::kThumb2 : kRuntimeISA),
44*795d594fSAndroid Build Coastguard Worker instruction_set_features_(nullptr),
45*795d594fSAndroid Build Coastguard Worker no_inline_from_(),
46*795d594fSAndroid Build Coastguard Worker dex_files_for_oat_file_(),
47*795d594fSAndroid Build Coastguard Worker image_classes_(),
48*795d594fSAndroid Build Coastguard Worker compiler_type_(CompilerType::kAotCompiler),
49*795d594fSAndroid Build Coastguard Worker image_type_(ImageType::kNone),
50*795d594fSAndroid Build Coastguard Worker multi_image_(false),
51*795d594fSAndroid Build Coastguard Worker compile_art_test_(false),
52*795d594fSAndroid Build Coastguard Worker emit_read_barrier_(false),
53*795d594fSAndroid Build Coastguard Worker baseline_(false),
54*795d594fSAndroid Build Coastguard Worker debuggable_(false),
55*795d594fSAndroid Build Coastguard Worker generate_debug_info_(kDefaultGenerateDebugInfo),
56*795d594fSAndroid Build Coastguard Worker generate_mini_debug_info_(kDefaultGenerateMiniDebugInfo),
57*795d594fSAndroid Build Coastguard Worker generate_build_id_(false),
58*795d594fSAndroid Build Coastguard Worker implicit_null_checks_(false),
59*795d594fSAndroid Build Coastguard Worker implicit_so_checks_(true),
60*795d594fSAndroid Build Coastguard Worker implicit_suspend_checks_(false),
61*795d594fSAndroid Build Coastguard Worker compile_pic_(false),
62*795d594fSAndroid Build Coastguard Worker dump_timings_(false),
63*795d594fSAndroid Build Coastguard Worker dump_pass_timings_(false),
64*795d594fSAndroid Build Coastguard Worker dump_stats_(false),
65*795d594fSAndroid Build Coastguard Worker profile_branches_(false),
66*795d594fSAndroid Build Coastguard Worker profile_compilation_info_(nullptr),
67*795d594fSAndroid Build Coastguard Worker verbose_methods_(),
68*795d594fSAndroid Build Coastguard Worker abort_on_hard_verifier_failure_(false),
69*795d594fSAndroid Build Coastguard Worker abort_on_soft_verifier_failure_(false),
70*795d594fSAndroid Build Coastguard Worker init_failure_output_(nullptr),
71*795d594fSAndroid Build Coastguard Worker dump_cfg_file_name_(""),
72*795d594fSAndroid Build Coastguard Worker dump_cfg_append_(false),
73*795d594fSAndroid Build Coastguard Worker force_determinism_(false),
74*795d594fSAndroid Build Coastguard Worker check_linkage_conditions_(false),
75*795d594fSAndroid Build Coastguard Worker crash_on_linkage_violation_(false),
76*795d594fSAndroid Build Coastguard Worker deduplicate_code_(true),
77*795d594fSAndroid Build Coastguard Worker count_hotness_in_compiled_code_(false),
78*795d594fSAndroid Build Coastguard Worker resolve_startup_const_strings_(false),
79*795d594fSAndroid Build Coastguard Worker initialize_app_image_classes_(false),
80*795d594fSAndroid Build Coastguard Worker check_profiled_methods_(ProfileMethodsCheck::kNone),
81*795d594fSAndroid Build Coastguard Worker max_image_block_size_(std::numeric_limits<uint32_t>::max()),
82*795d594fSAndroid Build Coastguard Worker passes_to_run_(nullptr) {
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker
~CompilerOptions()85*795d594fSAndroid Build Coastguard Worker CompilerOptions::~CompilerOptions() {
86*795d594fSAndroid Build Coastguard Worker // Everything done by member destructors.
87*795d594fSAndroid Build Coastguard Worker // The definitions of classes forward-declared in the header have now been #included.
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker
90*795d594fSAndroid Build Coastguard Worker namespace {
91*795d594fSAndroid Build Coastguard Worker
92*795d594fSAndroid Build Coastguard Worker bool kEmitRuntimeReadBarrierChecks = kIsDebugBuild &&
93*795d594fSAndroid Build Coastguard Worker RegisterRuntimeDebugFlag(&kEmitRuntimeReadBarrierChecks);
94*795d594fSAndroid Build Coastguard Worker
95*795d594fSAndroid Build Coastguard Worker } // namespace
96*795d594fSAndroid Build Coastguard Worker
EmitRunTimeChecksInDebugMode() const97*795d594fSAndroid Build Coastguard Worker bool CompilerOptions::EmitRunTimeChecksInDebugMode() const {
98*795d594fSAndroid Build Coastguard Worker // Run-time checks (e.g. Marking Register checks) are only emitted in slow-debug mode.
99*795d594fSAndroid Build Coastguard Worker return kEmitRuntimeReadBarrierChecks;
100*795d594fSAndroid Build Coastguard Worker }
101*795d594fSAndroid Build Coastguard Worker
ParseDumpInitFailures(const std::string & option,std::string * error_msg)102*795d594fSAndroid Build Coastguard Worker bool CompilerOptions::ParseDumpInitFailures(const std::string& option, std::string* error_msg) {
103*795d594fSAndroid Build Coastguard Worker init_failure_output_.reset(new std::ofstream(option));
104*795d594fSAndroid Build Coastguard Worker if (init_failure_output_.get() == nullptr) {
105*795d594fSAndroid Build Coastguard Worker *error_msg = "Failed to construct std::ofstream";
106*795d594fSAndroid Build Coastguard Worker return false;
107*795d594fSAndroid Build Coastguard Worker } else if (init_failure_output_->fail()) {
108*795d594fSAndroid Build Coastguard Worker *error_msg = android::base::StringPrintf(
109*795d594fSAndroid Build Coastguard Worker "Failed to open %s for writing the initialization failures.", option.c_str());
110*795d594fSAndroid Build Coastguard Worker init_failure_output_.reset();
111*795d594fSAndroid Build Coastguard Worker return false;
112*795d594fSAndroid Build Coastguard Worker }
113*795d594fSAndroid Build Coastguard Worker return true;
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker
ParseCompilerOptions(const std::vector<std::string> & options,bool ignore_unrecognized,std::string * error_msg)116*795d594fSAndroid Build Coastguard Worker bool CompilerOptions::ParseCompilerOptions(const std::vector<std::string>& options,
117*795d594fSAndroid Build Coastguard Worker bool ignore_unrecognized,
118*795d594fSAndroid Build Coastguard Worker std::string* error_msg) {
119*795d594fSAndroid Build Coastguard Worker auto parser = CreateSimpleParser(ignore_unrecognized);
120*795d594fSAndroid Build Coastguard Worker CmdlineResult parse_result = parser.Parse(options);
121*795d594fSAndroid Build Coastguard Worker if (!parse_result.IsSuccess()) {
122*795d594fSAndroid Build Coastguard Worker *error_msg = parse_result.GetMessage();
123*795d594fSAndroid Build Coastguard Worker return false;
124*795d594fSAndroid Build Coastguard Worker }
125*795d594fSAndroid Build Coastguard Worker
126*795d594fSAndroid Build Coastguard Worker SimpleParseArgumentMap args = parser.ReleaseArgumentsMap();
127*795d594fSAndroid Build Coastguard Worker return ReadCompilerOptions(args, this, error_msg);
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker
IsImageClass(const char * descriptor) const130*795d594fSAndroid Build Coastguard Worker bool CompilerOptions::IsImageClass(const char* descriptor) const {
131*795d594fSAndroid Build Coastguard Worker // Historical note: We used to hold the set indirectly and there was a distinction between an
132*795d594fSAndroid Build Coastguard Worker // empty set and a null, null meaning to include all classes. However, the distinction has been
133*795d594fSAndroid Build Coastguard Worker // removed; if we don't have a profile, we treat it as an empty set of classes. b/77340429
134*795d594fSAndroid Build Coastguard Worker return image_classes_.find(std::string_view(descriptor)) != image_classes_.end();
135*795d594fSAndroid Build Coastguard Worker }
136*795d594fSAndroid Build Coastguard Worker
IsPreloadedClass(std::string_view pretty_descriptor) const137*795d594fSAndroid Build Coastguard Worker bool CompilerOptions::IsPreloadedClass(std::string_view pretty_descriptor) const {
138*795d594fSAndroid Build Coastguard Worker return preloaded_classes_.find(pretty_descriptor) != preloaded_classes_.end();
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker
ShouldCompileWithClinitCheck(ArtMethod * method) const141*795d594fSAndroid Build Coastguard Worker bool CompilerOptions::ShouldCompileWithClinitCheck(ArtMethod* method) const {
142*795d594fSAndroid Build Coastguard Worker if (method != nullptr &&
143*795d594fSAndroid Build Coastguard Worker Runtime::Current()->IsAotCompiler() &&
144*795d594fSAndroid Build Coastguard Worker method->IsStatic() &&
145*795d594fSAndroid Build Coastguard Worker !method->IsConstructor() &&
146*795d594fSAndroid Build Coastguard Worker // Compiled code for native methods never do a clinit check, so we may put the resolution
147*795d594fSAndroid Build Coastguard Worker // trampoline for native methods. This means that it's possible post zygote fork for the
148*795d594fSAndroid Build Coastguard Worker // entry to be dirtied. We could resolve this by either:
149*795d594fSAndroid Build Coastguard Worker // - Make these methods use the generic JNI entrypoint, but that's not
150*795d594fSAndroid Build Coastguard Worker // desirable for a method that is in the profile.
151*795d594fSAndroid Build Coastguard Worker // - Ensure the declaring class of such native methods are always in the
152*795d594fSAndroid Build Coastguard Worker // preloaded-classes list.
153*795d594fSAndroid Build Coastguard Worker // - Emit the clinit check in the compiled code of native methods.
154*795d594fSAndroid Build Coastguard Worker !method->IsNative()) {
155*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
156*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> cls = method->GetDeclaringClass<kWithoutReadBarrier>();
157*795d594fSAndroid Build Coastguard Worker return cls->IsInBootImageAndNotInPreloadedClasses();
158*795d594fSAndroid Build Coastguard Worker }
159*795d594fSAndroid Build Coastguard Worker return false;
160*795d594fSAndroid Build Coastguard Worker }
161*795d594fSAndroid Build Coastguard Worker
162*795d594fSAndroid Build Coastguard Worker } // namespace art
163