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 20 #include "base/arena_object.h" 21 #include "base/macros.h" 22 #include "nodes.h" 23 #include "optimizing_compiler_stats.h" 24 25 namespace art HIDDEN { 26 27 class CodeGenerator; 28 class DexCompilationUnit; 29 30 /** 31 * Abstraction to implement an optimization pass. 32 */ 33 class HOptimization : public ArenaObject<kArenaAllocOptimization> { 34 public: 35 HOptimization(HGraph* graph, 36 const char* pass_name, 37 OptimizingCompilerStats* stats = nullptr) graph_(graph)38 : graph_(graph), 39 stats_(stats), 40 pass_name_(pass_name) {} 41 ~HOptimization()42 virtual ~HOptimization() {} 43 44 // Return the name of the pass. Pass names for a single HOptimization should be of form 45 // <optimization_name> or <optimization_name>$<pass_name> for common <optimization_name> prefix. 46 // Example: 'instruction_simplifier', 'instruction_simplifier$before_codegen', 47 // 'instruction_simplifier$before_codegen'. GetPassName()48 const char* GetPassName() const { return pass_name_; } 49 50 // Perform the pass or analysis. Returns false if no optimizations occurred or no useful 51 // information was computed (this is best effort, returning true is always ok). 52 virtual bool Run() = 0; 53 54 protected: 55 HGraph* const graph_; 56 // Used to record stats about the optimization. 57 OptimizingCompilerStats* const stats_; 58 59 private: 60 // Optimization pass name. 61 const char* pass_name_; 62 63 DISALLOW_COPY_AND_ASSIGN(HOptimization); 64 }; 65 66 // Optimization passes that can be constructed by the helper method below. An enum 67 // field is preferred over a string lookup at places where performance matters. 68 // TODO: generate this table and lookup methods below automatically? 69 enum class OptimizationPass { 70 kAggressiveInstructionSimplifier, 71 kBoundsCheckElimination, 72 kCHAGuardOptimization, 73 kCodeSinking, 74 kConstantFolding, 75 kConstructorFenceRedundancyElimination, 76 kDeadCodeElimination, 77 kGlobalValueNumbering, 78 kInductionVarAnalysis, 79 kInliner, 80 kInstructionSimplifier, 81 kInvariantCodeMotion, 82 kLoadStoreElimination, 83 kLoopOptimization, 84 kReferenceTypePropagation, 85 kScheduling, 86 kSelectGenerator, 87 kSideEffectsAnalysis, 88 kWriteBarrierElimination, 89 #ifdef ART_ENABLE_CODEGEN_arm 90 kInstructionSimplifierArm, 91 kCriticalNativeAbiFixupArm, 92 #endif 93 #ifdef ART_ENABLE_CODEGEN_arm64 94 kInstructionSimplifierArm64, 95 #endif 96 #ifdef ART_ENABLE_CODEGEN_riscv64 97 kCriticalNativeAbiFixupRiscv64, 98 kInstructionSimplifierRiscv64, 99 #endif 100 #ifdef ART_ENABLE_CODEGEN_x86 101 kPcRelativeFixupsX86, 102 kInstructionSimplifierX86, 103 #endif 104 #ifdef ART_ENABLE_CODEGEN_x86_64 105 kInstructionSimplifierX86_64, 106 #endif 107 #if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64) 108 kX86MemoryOperandGeneration, 109 #endif 110 kNone, 111 kLast = kNone 112 }; 113 114 // Lookup name of optimization pass. 115 const char* OptimizationPassName(OptimizationPass pass); 116 117 // Lookup optimization pass by name. 118 OptimizationPass OptimizationPassByName(const std::string& pass_name); 119 120 // Optimization definition consisting of an optimization pass 121 // an optional alternative name (nullptr denotes default), and 122 // an optional pass dependence (kNone denotes no dependence). 123 struct OptimizationDef { OptimizationDefOptimizationDef124 OptimizationDef(OptimizationPass p, const char* pn, OptimizationPass d) 125 : pass(p), pass_name(pn), depends_on(d) {} 126 OptimizationPass pass; 127 const char* pass_name; 128 OptimizationPass depends_on; 129 }; 130 131 // Helper method for optimization definition array entries. 132 inline OptimizationDef OptDef(OptimizationPass pass, 133 const char* pass_name = nullptr, 134 OptimizationPass depends_on = OptimizationPass::kNone) { 135 return OptimizationDef(pass, pass_name, depends_on); 136 } 137 138 // Helper method to construct series of optimization passes. 139 // The array should consist of the requested optimizations 140 // and optional alternative names for repeated passes. 141 // Example: 142 // { OptPass(kConstantFolding), 143 // OptPass(Inliner), 144 // OptPass(kConstantFolding, "constant_folding$after_inlining") 145 // } 146 ArenaVector<HOptimization*> ConstructOptimizations( 147 const OptimizationDef definitions[], 148 size_t length, 149 ArenaAllocator* allocator, 150 HGraph* graph, 151 OptimizingCompilerStats* stats, 152 CodeGenerator* codegen, 153 const DexCompilationUnit& dex_compilation_unit); 154 155 } // namespace art 156 157 #endif // ART_COMPILER_OPTIMIZING_OPTIMIZATION_H_ 158