compiler_options.h revision 2d8801f7b932496d5c2606294ff8fdea60e05b30
1/*
2 * Copyright (C) 2014 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#ifndef ART_COMPILER_DRIVER_COMPILER_OPTIONS_H_
18#define ART_COMPILER_DRIVER_COMPILER_OPTIONS_H_
19
20#include <ostream>
21#include <string>
22#include <vector>
23
24#include "base/macros.h"
25#include "compiler_filter.h"
26#include "globals.h"
27#include "optimizing/register_allocator.h"
28#include "utils.h"
29
30namespace art {
31
32namespace verifier {
33class VerifierDepsTest;
34}  // namespace verifier
35
36class DexFile;
37
38class CompilerOptions FINAL {
39 public:
40  // Guide heuristics to determine whether to compile method if profile data not available.
41  static const size_t kDefaultHugeMethodThreshold = 10000;
42  static const size_t kDefaultLargeMethodThreshold = 600;
43  static const size_t kDefaultSmallMethodThreshold = 60;
44  static const size_t kDefaultTinyMethodThreshold = 20;
45  static const size_t kDefaultNumDexMethodsThreshold = 900;
46  static constexpr double kDefaultTopKProfileThreshold = 90.0;
47  static const bool kDefaultGenerateDebugInfo = false;
48  static const bool kDefaultGenerateMiniDebugInfo = false;
49  static const size_t kDefaultInlineMaxCodeUnits = 32;
50  static constexpr size_t kUnsetInlineMaxCodeUnits = -1;
51
52  CompilerOptions();
53  ~CompilerOptions();
54
55  CompilerFilter::Filter GetCompilerFilter() const {
56    return compiler_filter_;
57  }
58
59  void SetCompilerFilter(CompilerFilter::Filter compiler_filter) {
60    compiler_filter_ = compiler_filter;
61  }
62
63  bool IsAotCompilationEnabled() const {
64    return CompilerFilter::IsAotCompilationEnabled(compiler_filter_);
65  }
66
67  bool IsJniCompilationEnabled() const {
68    return CompilerFilter::IsJniCompilationEnabled(compiler_filter_);
69  }
70
71  bool IsQuickeningCompilationEnabled() const {
72    return CompilerFilter::IsQuickeningCompilationEnabled(compiler_filter_);
73  }
74
75  bool IsVerificationEnabled() const {
76    return CompilerFilter::IsVerificationEnabled(compiler_filter_);
77  }
78
79  bool AssumeClassesAreVerified() const {
80    return compiler_filter_ == CompilerFilter::kAssumeVerified;
81  }
82
83  bool VerifyAtRuntime() const {
84    return compiler_filter_ == CompilerFilter::kExtract;
85  }
86
87  bool IsAnyCompilationEnabled() const {
88    return CompilerFilter::IsAnyCompilationEnabled(compiler_filter_);
89  }
90
91  size_t GetHugeMethodThreshold() const {
92    return huge_method_threshold_;
93  }
94
95  size_t GetLargeMethodThreshold() const {
96    return large_method_threshold_;
97  }
98
99  size_t GetSmallMethodThreshold() const {
100    return small_method_threshold_;
101  }
102
103  size_t GetTinyMethodThreshold() const {
104    return tiny_method_threshold_;
105  }
106
107  bool IsHugeMethod(size_t num_dalvik_instructions) const {
108    return num_dalvik_instructions > huge_method_threshold_;
109  }
110
111  bool IsLargeMethod(size_t num_dalvik_instructions) const {
112    return num_dalvik_instructions > large_method_threshold_;
113  }
114
115  bool IsSmallMethod(size_t num_dalvik_instructions) const {
116    return num_dalvik_instructions > small_method_threshold_;
117  }
118
119  bool IsTinyMethod(size_t num_dalvik_instructions) const {
120    return num_dalvik_instructions > tiny_method_threshold_;
121  }
122
123  size_t GetNumDexMethodsThreshold() const {
124    return num_dex_methods_threshold_;
125  }
126
127  size_t GetInlineMaxCodeUnits() const {
128    return inline_max_code_units_;
129  }
130  void SetInlineMaxCodeUnits(size_t units) {
131    inline_max_code_units_ = units;
132  }
133
134  double GetTopKProfileThreshold() const {
135    return top_k_profile_threshold_;
136  }
137
138  bool GetDebuggable() const {
139    return debuggable_;
140  }
141
142  void SetDebuggable(bool value) {
143    debuggable_ = value;
144  }
145
146  bool GetNativeDebuggable() const {
147    return GetDebuggable() && GetGenerateDebugInfo();
148  }
149
150  // This flag controls whether the compiler collects debugging information.
151  // The other flags control how the information is written to disk.
152  bool GenerateAnyDebugInfo() const {
153    return GetGenerateDebugInfo() || GetGenerateMiniDebugInfo();
154  }
155
156  bool GetGenerateDebugInfo() const {
157    return generate_debug_info_;
158  }
159
160  bool GetGenerateMiniDebugInfo() const {
161    return generate_mini_debug_info_;
162  }
163
164  // Should run-time checks be emitted in debug mode?
165  bool EmitRunTimeChecksInDebugMode() const;
166
167  bool GetGenerateBuildId() const {
168    return generate_build_id_;
169  }
170
171  bool GetImplicitNullChecks() const {
172    return implicit_null_checks_;
173  }
174
175  bool GetImplicitStackOverflowChecks() const {
176    return implicit_so_checks_;
177  }
178
179  bool GetImplicitSuspendChecks() const {
180    return implicit_suspend_checks_;
181  }
182
183  // Are we compiling a boot image?
184  bool IsBootImage() const {
185    return boot_image_;
186  }
187
188  // Are we compiling a core image (small boot image only used for ART testing)?
189  bool IsCoreImage() const {
190    // Ensure that `core_image_` => `boot_image_`.
191    DCHECK(!core_image_ || boot_image_);
192    return core_image_;
193  }
194
195  // Are we compiling an app image?
196  bool IsAppImage() const {
197    return app_image_;
198  }
199
200  void DisableAppImage() {
201    app_image_ = false;
202  }
203
204  // Should the code be compiled as position independent?
205  bool GetCompilePic() const {
206    return compile_pic_;
207  }
208
209  bool HasVerboseMethods() const {
210    return !verbose_methods_.empty();
211  }
212
213  bool IsVerboseMethod(const std::string& pretty_method) const {
214    for (const std::string& cur_method : verbose_methods_) {
215      if (pretty_method.find(cur_method) != std::string::npos) {
216        return true;
217      }
218    }
219    return false;
220  }
221
222  std::ostream* GetInitFailureOutput() const {
223    return init_failure_output_.get();
224  }
225
226  bool AbortOnHardVerifierFailure() const {
227    return abort_on_hard_verifier_failure_;
228  }
229  bool AbortOnSoftVerifierFailure() const {
230    return abort_on_soft_verifier_failure_;
231  }
232
233  const std::vector<const DexFile*>* GetNoInlineFromDexFile() const {
234    return no_inline_from_;
235  }
236
237  bool ParseCompilerOptions(const std::vector<std::string>& options,
238                            bool ignore_unrecognized,
239                            std::string* error_msg);
240
241  void SetNonPic() {
242    compile_pic_ = false;
243  }
244
245  const std::string& GetDumpCfgFileName() const {
246    return dump_cfg_file_name_;
247  }
248
249  bool GetDumpCfgAppend() const {
250    return dump_cfg_append_;
251  }
252
253  bool IsForceDeterminism() const {
254    return force_determinism_;
255  }
256
257  bool DeduplicateCode() const {
258    return deduplicate_code_;
259  }
260
261  RegisterAllocator::Strategy GetRegisterAllocationStrategy() const {
262    return register_allocation_strategy_;
263  }
264
265  const std::vector<std::string>* GetPassesToRun() const {
266    return passes_to_run_;
267  }
268
269  bool GetDumpTimings() const {
270    return dump_timings_;
271  }
272
273  bool GetDumpStats() const {
274    return dump_stats_;
275  }
276
277 private:
278  bool ParseDumpInitFailures(const std::string& option, std::string* error_msg);
279  void ParseDumpCfgPasses(const StringPiece& option, UsageFn Usage);
280  void ParseInlineMaxCodeUnits(const StringPiece& option, UsageFn Usage);
281  void ParseNumDexMethods(const StringPiece& option, UsageFn Usage);
282  void ParseTinyMethodMax(const StringPiece& option, UsageFn Usage);
283  void ParseSmallMethodMax(const StringPiece& option, UsageFn Usage);
284  void ParseLargeMethodMax(const StringPiece& option, UsageFn Usage);
285  void ParseHugeMethodMax(const StringPiece& option, UsageFn Usage);
286  bool ParseRegisterAllocationStrategy(const std::string& option, std::string* error_msg);
287
288  CompilerFilter::Filter compiler_filter_;
289  size_t huge_method_threshold_;
290  size_t large_method_threshold_;
291  size_t small_method_threshold_;
292  size_t tiny_method_threshold_;
293  size_t num_dex_methods_threshold_;
294  size_t inline_max_code_units_;
295
296  // Dex files from which we should not inline code.
297  // This is usually a very short list (i.e. a single dex file), so we
298  // prefer vector<> over a lookup-oriented container, such as set<>.
299  const std::vector<const DexFile*>* no_inline_from_;
300
301  bool boot_image_;
302  bool core_image_;
303  bool app_image_;
304  // When using a profile file only the top K% of the profiled samples will be compiled.
305  double top_k_profile_threshold_;
306  bool debuggable_;
307  bool generate_debug_info_;
308  bool generate_mini_debug_info_;
309  bool generate_build_id_;
310  bool implicit_null_checks_;
311  bool implicit_so_checks_;
312  bool implicit_suspend_checks_;
313  bool compile_pic_;
314  bool dump_timings_;
315  bool dump_stats_;
316
317  // Vector of methods to have verbose output enabled for.
318  std::vector<std::string> verbose_methods_;
319
320  // Abort compilation with an error if we find a class that fails verification with a hard
321  // failure.
322  bool abort_on_hard_verifier_failure_;
323  // Same for soft failures.
324  bool abort_on_soft_verifier_failure_;
325
326  // Log initialization of initialization failures to this stream if not null.
327  std::unique_ptr<std::ostream> init_failure_output_;
328
329  std::string dump_cfg_file_name_;
330  bool dump_cfg_append_;
331
332  // Whether the compiler should trade performance for determinism to guarantee exactly reproducible
333  // outcomes.
334  bool force_determinism_;
335
336  // Whether code should be deduplicated.
337  bool deduplicate_code_;
338
339  RegisterAllocator::Strategy register_allocation_strategy_;
340
341  // If not null, specifies optimization passes which will be run instead of defaults.
342  // Note that passes_to_run_ is not checked for correctness and providing an incorrect
343  // list of passes can lead to unexpected compiler behaviour. This is caused by dependencies
344  // between passes. Failing to satisfy them can for example lead to compiler crashes.
345  // Passing pass names which are not recognized by the compiler will result in
346  // compiler-dependant behavior.
347  const std::vector<std::string>* passes_to_run_;
348
349  friend class Dex2Oat;
350  friend class DexToDexDecompilerTest;
351  friend class CommonCompilerTest;
352  friend class verifier::VerifierDepsTest;
353
354  template <class Base>
355  friend bool ReadCompilerOptions(Base& map, CompilerOptions* options, std::string* error_msg);
356
357  DISALLOW_COPY_AND_ASSIGN(CompilerOptions);
358};
359
360}  // namespace art
361
362#endif  // ART_COMPILER_DRIVER_COMPILER_OPTIONS_H_
363