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 
21 #include "android-base/stringprintf.h"
22 
23 #include "base/runtime_debug.h"
24 #include "base/variant_map.h"
25 #include "cmdline_parser.h"
26 #include "compiler_options_map-inl.h"
27 #include "runtime.h"
28 #include "simple_compiler_options_map.h"
29 
30 namespace art {
31 
CompilerOptions()32 CompilerOptions::CompilerOptions()
33     : compiler_filter_(CompilerFilter::kDefaultCompilerFilter),
34       huge_method_threshold_(kDefaultHugeMethodThreshold),
35       large_method_threshold_(kDefaultLargeMethodThreshold),
36       small_method_threshold_(kDefaultSmallMethodThreshold),
37       tiny_method_threshold_(kDefaultTinyMethodThreshold),
38       num_dex_methods_threshold_(kDefaultNumDexMethodsThreshold),
39       inline_max_code_units_(kUnsetInlineMaxCodeUnits),
40       no_inline_from_(nullptr),
41       boot_image_(false),
42       core_image_(false),
43       app_image_(false),
44       top_k_profile_threshold_(kDefaultTopKProfileThreshold),
45       debuggable_(false),
46       generate_debug_info_(kDefaultGenerateDebugInfo),
47       generate_mini_debug_info_(kDefaultGenerateMiniDebugInfo),
48       generate_build_id_(false),
49       implicit_null_checks_(true),
50       implicit_so_checks_(true),
51       implicit_suspend_checks_(false),
52       compile_pic_(false),
53       dump_timings_(false),
54       dump_stats_(false),
55       verbose_methods_(),
56       abort_on_hard_verifier_failure_(false),
57       abort_on_soft_verifier_failure_(false),
58       init_failure_output_(nullptr),
59       dump_cfg_file_name_(""),
60       dump_cfg_append_(false),
61       force_determinism_(false),
62       deduplicate_code_(true),
63       count_hotness_in_compiled_code_(false),
64       register_allocation_strategy_(RegisterAllocator::kRegisterAllocatorDefault),
65       passes_to_run_(nullptr) {
66 }
67 
~CompilerOptions()68 CompilerOptions::~CompilerOptions() {
69   // The destructor looks empty but it destroys a PassManagerOptions object. We keep it here
70   // because we don't want to include the PassManagerOptions definition from the header file.
71 }
72 
73 namespace {
74 
75 bool kEmitRuntimeReadBarrierChecks = kIsDebugBuild &&
76     RegisterRuntimeDebugFlag(&kEmitRuntimeReadBarrierChecks);
77 
78 }  // namespace
79 
EmitRunTimeChecksInDebugMode() const80 bool CompilerOptions::EmitRunTimeChecksInDebugMode() const {
81   // Run-time checks (e.g. Marking Register checks) are only emitted in slow-debug mode.
82   return kEmitRuntimeReadBarrierChecks;
83 }
84 
ParseDumpInitFailures(const std::string & option,std::string * error_msg)85 bool CompilerOptions::ParseDumpInitFailures(const std::string& option, std::string* error_msg) {
86   init_failure_output_.reset(new std::ofstream(option));
87   if (init_failure_output_.get() == nullptr) {
88     *error_msg = "Failed to construct std::ofstream";
89     return false;
90   } else if (init_failure_output_->fail()) {
91     *error_msg = android::base::StringPrintf(
92         "Failed to open %s for writing the initialization failures.", option.c_str());
93     init_failure_output_.reset();
94     return false;
95   }
96   return true;
97 }
98 
ParseRegisterAllocationStrategy(const std::string & option,std::string * error_msg)99 bool CompilerOptions::ParseRegisterAllocationStrategy(const std::string& option,
100                                                       std::string* error_msg) {
101   if (option == "linear-scan") {
102     register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorLinearScan;
103   } else if (option == "graph-color") {
104     register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorGraphColor;
105   } else {
106     *error_msg = "Unrecognized register allocation strategy. Try linear-scan, or graph-color.";
107     return false;
108   }
109   return true;
110 }
111 
112 #pragma GCC diagnostic push
113 #pragma GCC diagnostic ignored "-Wframe-larger-than="
114 
ParseCompilerOptions(const std::vector<std::string> & options,bool ignore_unrecognized,std::string * error_msg)115 bool CompilerOptions::ParseCompilerOptions(const std::vector<std::string>& options,
116                                            bool ignore_unrecognized,
117                                            std::string* error_msg) {
118   auto parser = CreateSimpleParser(ignore_unrecognized);
119   CmdlineResult parse_result = parser.Parse(options);
120   if (!parse_result.IsSuccess()) {
121     *error_msg = parse_result.GetMessage();
122     return false;
123   }
124 
125   SimpleParseArgumentMap args = parser.ReleaseArgumentsMap();
126   return ReadCompilerOptions(args, this, error_msg);
127 }
128 
129 #pragma GCC diagnostic pop
130 
131 }  // namespace art
132