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 namespace art {
22 
CompilerOptions()23 CompilerOptions::CompilerOptions()
24     : compiler_filter_(CompilerFilter::kDefaultCompilerFilter),
25       huge_method_threshold_(kDefaultHugeMethodThreshold),
26       large_method_threshold_(kDefaultLargeMethodThreshold),
27       small_method_threshold_(kDefaultSmallMethodThreshold),
28       tiny_method_threshold_(kDefaultTinyMethodThreshold),
29       num_dex_methods_threshold_(kDefaultNumDexMethodsThreshold),
30       inline_max_code_units_(kUnsetInlineMaxCodeUnits),
31       no_inline_from_(nullptr),
32       boot_image_(false),
33       app_image_(false),
34       top_k_profile_threshold_(kDefaultTopKProfileThreshold),
35       debuggable_(false),
36       generate_debug_info_(kDefaultGenerateDebugInfo),
37       generate_mini_debug_info_(kDefaultGenerateMiniDebugInfo),
38       generate_build_id_(false),
39       implicit_null_checks_(true),
40       implicit_so_checks_(true),
41       implicit_suspend_checks_(false),
42       compile_pic_(false),
43       verbose_methods_(nullptr),
44       abort_on_hard_verifier_failure_(false),
45       init_failure_output_(nullptr),
46       dump_cfg_file_name_(""),
47       dump_cfg_append_(false),
48       force_determinism_(false),
49       register_allocation_strategy_(RegisterAllocator::kRegisterAllocatorDefault),
50       passes_to_run_(nullptr) {
51 }
52 
~CompilerOptions()53 CompilerOptions::~CompilerOptions() {
54   // The destructor looks empty but it destroys a PassManagerOptions object. We keep it here
55   // because we don't want to include the PassManagerOptions definition from the header file.
56 }
57 
CompilerOptions(CompilerFilter::Filter compiler_filter,size_t huge_method_threshold,size_t large_method_threshold,size_t small_method_threshold,size_t tiny_method_threshold,size_t num_dex_methods_threshold,size_t inline_max_code_units,const std::vector<const DexFile * > * no_inline_from,double top_k_profile_threshold,bool debuggable,bool generate_debug_info,bool implicit_null_checks,bool implicit_so_checks,bool implicit_suspend_checks,bool compile_pic,const std::vector<std::string> * verbose_methods,std::ostream * init_failure_output,bool abort_on_hard_verifier_failure,const std::string & dump_cfg_file_name,bool dump_cfg_append,bool force_determinism,RegisterAllocator::Strategy regalloc_strategy,const std::vector<std::string> * passes_to_run)58 CompilerOptions::CompilerOptions(CompilerFilter::Filter compiler_filter,
59                                  size_t huge_method_threshold,
60                                  size_t large_method_threshold,
61                                  size_t small_method_threshold,
62                                  size_t tiny_method_threshold,
63                                  size_t num_dex_methods_threshold,
64                                  size_t inline_max_code_units,
65                                  const std::vector<const DexFile*>* no_inline_from,
66                                  double top_k_profile_threshold,
67                                  bool debuggable,
68                                  bool generate_debug_info,
69                                  bool implicit_null_checks,
70                                  bool implicit_so_checks,
71                                  bool implicit_suspend_checks,
72                                  bool compile_pic,
73                                  const std::vector<std::string>* verbose_methods,
74                                  std::ostream* init_failure_output,
75                                  bool abort_on_hard_verifier_failure,
76                                  const std::string& dump_cfg_file_name,
77                                  bool dump_cfg_append,
78                                  bool force_determinism,
79                                  RegisterAllocator::Strategy regalloc_strategy,
80                                  const std::vector<std::string>* passes_to_run)
81     : compiler_filter_(compiler_filter),
82       huge_method_threshold_(huge_method_threshold),
83       large_method_threshold_(large_method_threshold),
84       small_method_threshold_(small_method_threshold),
85       tiny_method_threshold_(tiny_method_threshold),
86       num_dex_methods_threshold_(num_dex_methods_threshold),
87       inline_max_code_units_(inline_max_code_units),
88       no_inline_from_(no_inline_from),
89       boot_image_(false),
90       app_image_(false),
91       top_k_profile_threshold_(top_k_profile_threshold),
92       debuggable_(debuggable),
93       generate_debug_info_(generate_debug_info),
94       generate_mini_debug_info_(kDefaultGenerateMiniDebugInfo),
95       generate_build_id_(false),
96       implicit_null_checks_(implicit_null_checks),
97       implicit_so_checks_(implicit_so_checks),
98       implicit_suspend_checks_(implicit_suspend_checks),
99       compile_pic_(compile_pic),
100       verbose_methods_(verbose_methods),
101       abort_on_hard_verifier_failure_(abort_on_hard_verifier_failure),
102       init_failure_output_(init_failure_output),
103       dump_cfg_file_name_(dump_cfg_file_name),
104       dump_cfg_append_(dump_cfg_append),
105       force_determinism_(force_determinism),
106       register_allocation_strategy_(regalloc_strategy),
107       passes_to_run_(passes_to_run) {
108 }
109 
ParseHugeMethodMax(const StringPiece & option,UsageFn Usage)110 void CompilerOptions::ParseHugeMethodMax(const StringPiece& option, UsageFn Usage) {
111   ParseUintOption(option, "--huge-method-max", &huge_method_threshold_, Usage);
112 }
113 
ParseLargeMethodMax(const StringPiece & option,UsageFn Usage)114 void CompilerOptions::ParseLargeMethodMax(const StringPiece& option, UsageFn Usage) {
115   ParseUintOption(option, "--large-method-max", &large_method_threshold_, Usage);
116 }
117 
ParseSmallMethodMax(const StringPiece & option,UsageFn Usage)118 void CompilerOptions::ParseSmallMethodMax(const StringPiece& option, UsageFn Usage) {
119   ParseUintOption(option, "--small-method-max", &small_method_threshold_, Usage);
120 }
121 
ParseTinyMethodMax(const StringPiece & option,UsageFn Usage)122 void CompilerOptions::ParseTinyMethodMax(const StringPiece& option, UsageFn Usage) {
123   ParseUintOption(option, "--tiny-method-max", &tiny_method_threshold_, Usage);
124 }
125 
ParseNumDexMethods(const StringPiece & option,UsageFn Usage)126 void CompilerOptions::ParseNumDexMethods(const StringPiece& option, UsageFn Usage) {
127   ParseUintOption(option, "--num-dex-methods", &num_dex_methods_threshold_, Usage);
128 }
129 
ParseInlineMaxCodeUnits(const StringPiece & option,UsageFn Usage)130 void CompilerOptions::ParseInlineMaxCodeUnits(const StringPiece& option, UsageFn Usage) {
131   ParseUintOption(option, "--inline-max-code-units", &inline_max_code_units_, Usage);
132 }
133 
ParseDumpInitFailures(const StringPiece & option,UsageFn Usage ATTRIBUTE_UNUSED)134 void CompilerOptions::ParseDumpInitFailures(const StringPiece& option,
135                                             UsageFn Usage ATTRIBUTE_UNUSED) {
136   DCHECK(option.starts_with("--dump-init-failures="));
137   std::string file_name = option.substr(strlen("--dump-init-failures=")).data();
138   init_failure_output_.reset(new std::ofstream(file_name));
139   if (init_failure_output_.get() == nullptr) {
140     LOG(ERROR) << "Failed to allocate ofstream";
141   } else if (init_failure_output_->fail()) {
142     LOG(ERROR) << "Failed to open " << file_name << " for writing the initialization "
143                << "failures.";
144     init_failure_output_.reset();
145   }
146 }
147 
ParseRegisterAllocationStrategy(const StringPiece & option,UsageFn Usage)148 void CompilerOptions::ParseRegisterAllocationStrategy(const StringPiece& option,
149                                                       UsageFn Usage) {
150   DCHECK(option.starts_with("--register-allocation-strategy="));
151   StringPiece choice = option.substr(strlen("--register-allocation-strategy=")).data();
152   if (choice == "linear-scan") {
153     register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorLinearScan;
154   } else if (choice == "graph-color") {
155     register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorGraphColor;
156   } else {
157     Usage("Unrecognized register allocation strategy. Try linear-scan, or graph-color.");
158   }
159 }
160 
ParseCompilerOption(const StringPiece & option,UsageFn Usage)161 bool CompilerOptions::ParseCompilerOption(const StringPiece& option, UsageFn Usage) {
162   if (option.starts_with("--compiler-filter=")) {
163     const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
164     if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, &compiler_filter_)) {
165       Usage("Unknown --compiler-filter value %s", compiler_filter_string);
166     }
167   } else if (option == "--compile-pic") {
168     compile_pic_ = true;
169   } else if (option.starts_with("--huge-method-max=")) {
170     ParseHugeMethodMax(option, Usage);
171   } else if (option.starts_with("--large-method-max=")) {
172     ParseLargeMethodMax(option, Usage);
173   } else if (option.starts_with("--small-method-max=")) {
174     ParseSmallMethodMax(option, Usage);
175   } else if (option.starts_with("--tiny-method-max=")) {
176     ParseTinyMethodMax(option, Usage);
177   } else if (option.starts_with("--num-dex-methods=")) {
178     ParseNumDexMethods(option, Usage);
179   } else if (option.starts_with("--inline-max-code-units=")) {
180     ParseInlineMaxCodeUnits(option, Usage);
181   } else if (option == "--generate-debug-info" || option == "-g") {
182     generate_debug_info_ = true;
183   } else if (option == "--no-generate-debug-info") {
184     generate_debug_info_ = false;
185   } else if (option == "--generate-mini-debug-info") {
186     generate_mini_debug_info_ = true;
187   } else if (option == "--no-generate-mini-debug-info") {
188     generate_mini_debug_info_ = false;
189   } else if (option == "--generate-build-id") {
190     generate_build_id_ = true;
191   } else if (option == "--no-generate-build-id") {
192     generate_build_id_ = false;
193   } else if (option == "--debuggable") {
194     debuggable_ = true;
195   } else if (option.starts_with("--top-k-profile-threshold=")) {
196     ParseDouble(option.data(), '=', 0.0, 100.0, &top_k_profile_threshold_, Usage);
197   } else if (option == "--abort-on-hard-verifier-error") {
198     abort_on_hard_verifier_failure_ = true;
199   } else if (option.starts_with("--dump-init-failures=")) {
200     ParseDumpInitFailures(option, Usage);
201   } else if (option.starts_with("--dump-cfg=")) {
202     dump_cfg_file_name_ = option.substr(strlen("--dump-cfg=")).data();
203   } else if (option.starts_with("--dump-cfg-append")) {
204     dump_cfg_append_ = true;
205   } else if (option.starts_with("--register-allocation-strategy=")) {
206     ParseRegisterAllocationStrategy(option, Usage);
207   } else {
208     // Option not recognized.
209     return false;
210   }
211   return true;
212 }
213 
214 }  // namespace art
215