Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 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_OPTIMIZING_OPTIMIZATION_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_OPTIMIZATION_H_ |
| 19 | |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 20 | #include "base/arena_object.h" |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 21 | #include "nodes.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 22 | #include "optimizing_compiler_stats.h" |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 23 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 24 | namespace art { |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 25 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 26 | class CodeGenerator; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 27 | class DexCompilationUnit; |
| 28 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 29 | /** |
| 30 | * Abstraction to implement an optimization pass. |
| 31 | */ |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 32 | class HOptimization : public ArenaObject<kArenaAllocOptimization> { |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 33 | public: |
| 34 | HOptimization(HGraph* graph, |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 35 | const char* pass_name, |
| 36 | OptimizingCompilerStats* stats = nullptr) |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 37 | : graph_(graph), |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 38 | stats_(stats), |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 39 | pass_name_(pass_name) {} |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 40 | |
| 41 | virtual ~HOptimization() {} |
| 42 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 43 | // Return the name of the pass. Pass names for a single HOptimization should be of form |
| 44 | // <optimization_name> or <optimization_name>$<pass_name> for common <optimization_name> prefix. |
| 45 | // Example: 'instruction_simplifier', 'instruction_simplifier$after_bce', |
| 46 | // 'instruction_simplifier$before_codegen'. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 47 | const char* GetPassName() const { return pass_name_; } |
| 48 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 49 | // Perform the pass or analysis. Returns false if no optimizations occurred or no useful |
| 50 | // information was computed (this is best effort, returning true is always ok). |
| 51 | virtual bool Run() = 0; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 52 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 53 | protected: |
| 54 | HGraph* const graph_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 55 | // Used to record stats about the optimization. |
| 56 | OptimizingCompilerStats* const stats_; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 57 | |
| 58 | private: |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 59 | // Optimization pass name. |
| 60 | const char* pass_name_; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(HOptimization); |
| 63 | }; |
| 64 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 65 | // Optimization passes that can be constructed by the helper method below. An enum |
| 66 | // field is preferred over a string lookup at places where performance matters. |
| 67 | // TODO: generate this table and lookup methods below automatically? |
| 68 | enum class OptimizationPass { |
| 69 | kBoundsCheckElimination, |
| 70 | kCHAGuardOptimization, |
| 71 | kCodeSinking, |
| 72 | kConstantFolding, |
| 73 | kConstructorFenceRedundancyElimination, |
| 74 | kDeadCodeElimination, |
| 75 | kGlobalValueNumbering, |
| 76 | kInductionVarAnalysis, |
| 77 | kInliner, |
| 78 | kInstructionSimplifier, |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 79 | kInvariantCodeMotion, |
| 80 | kLoadStoreAnalysis, |
| 81 | kLoadStoreElimination, |
| 82 | kLoopOptimization, |
| 83 | kScheduling, |
| 84 | kSelectGenerator, |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 85 | kSideEffectsAnalysis, |
| 86 | #ifdef ART_ENABLE_CODEGEN_arm |
| 87 | kInstructionSimplifierArm, |
| 88 | #endif |
| 89 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| 90 | kInstructionSimplifierArm64, |
| 91 | #endif |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 92 | #ifdef ART_ENABLE_CODEGEN_x86 |
| 93 | kPcRelativeFixupsX86, |
Shalini Salomi Bodapati | dd121f6 | 2018-10-26 15:03:53 +0530 | [diff] [blame] | 94 | kInstructionSimplifierX86, |
| 95 | #endif |
| 96 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
| 97 | kInstructionSimplifierX86_64, |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 98 | #endif |
| 99 | #if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64) |
| 100 | kX86MemoryOperandGeneration, |
| 101 | #endif |
Aart Bik | 2e14868 | 2018-04-18 16:11:12 -0700 | [diff] [blame] | 102 | kNone, |
| 103 | kLast = kNone |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | // Lookup name of optimization pass. |
| 107 | const char* OptimizationPassName(OptimizationPass pass); |
| 108 | |
| 109 | // Lookup optimization pass by name. |
Aart Bik | 2e14868 | 2018-04-18 16:11:12 -0700 | [diff] [blame] | 110 | OptimizationPass OptimizationPassByName(const std::string& pass_name); |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 111 | |
| 112 | // Optimization definition consisting of an optimization pass |
Aart Bik | 2e14868 | 2018-04-18 16:11:12 -0700 | [diff] [blame] | 113 | // an optional alternative name (nullptr denotes default), and |
| 114 | // an optional pass dependence (kNone denotes no dependence). |
| 115 | struct OptimizationDef { |
| 116 | OptimizationDef(OptimizationPass p, const char* pn, OptimizationPass d) |
| 117 | : pass(p), pass_name(pn), depends_on(d) {} |
| 118 | OptimizationPass pass; |
| 119 | const char* pass_name; |
| 120 | OptimizationPass depends_on; |
| 121 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 122 | |
| 123 | // Helper method for optimization definition array entries. |
Aart Bik | 2e14868 | 2018-04-18 16:11:12 -0700 | [diff] [blame] | 124 | inline OptimizationDef OptDef(OptimizationPass pass, |
| 125 | const char* pass_name = nullptr, |
| 126 | OptimizationPass depends_on = OptimizationPass::kNone) { |
| 127 | return OptimizationDef(pass, pass_name, depends_on); |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Helper method to construct series of optimization passes. |
| 131 | // The array should consist of the requested optimizations |
| 132 | // and optional alternative names for repeated passes. |
| 133 | // Example: |
| 134 | // { OptPass(kConstantFolding), |
| 135 | // OptPass(Inliner), |
| 136 | // OptPass(kConstantFolding, "constant_folding$after_inlining") |
| 137 | // } |
| 138 | ArenaVector<HOptimization*> ConstructOptimizations( |
| 139 | const OptimizationDef definitions[], |
| 140 | size_t length, |
| 141 | ArenaAllocator* allocator, |
| 142 | HGraph* graph, |
| 143 | OptimizingCompilerStats* stats, |
| 144 | CodeGenerator* codegen, |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 145 | const DexCompilationUnit& dex_compilation_unit, |
| 146 | VariableSizedHandleScope* handles); |
| 147 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 148 | } // namespace art |
| 149 | |
| 150 | #endif // ART_COMPILER_OPTIMIZING_OPTIMIZATION_H_ |