Home | History | Annotate | Download | only in optimizing
      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 "nodes.h"
     22 #include "optimizing_compiler_stats.h"
     23 
     24 namespace art {
     25 
     26 class CodeGenerator;
     27 class DexCompilationUnit;
     28 
     29 /**
     30  * Abstraction to implement an optimization pass.
     31  */
     32 class HOptimization : public ArenaObject<kArenaAllocOptimization> {
     33  public:
     34   HOptimization(HGraph* graph,
     35                 const char* pass_name,
     36                 OptimizingCompilerStats* stats = nullptr)
     37       : graph_(graph),
     38         stats_(stats),
     39         pass_name_(pass_name) {}
     40 
     41   virtual ~HOptimization() {}
     42 
     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'.
     47   const char* GetPassName() const { return pass_name_; }
     48 
     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;
     52 
     53  protected:
     54   HGraph* const graph_;
     55   // Used to record stats about the optimization.
     56   OptimizingCompilerStats* const stats_;
     57 
     58  private:
     59   // Optimization pass name.
     60   const char* pass_name_;
     61 
     62   DISALLOW_COPY_AND_ASSIGN(HOptimization);
     63 };
     64 
     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,
     79   kInvariantCodeMotion,
     80   kLoadStoreAnalysis,
     81   kLoadStoreElimination,
     82   kLoopOptimization,
     83   kScheduling,
     84   kSelectGenerator,
     85   kSideEffectsAnalysis,
     86 #ifdef ART_ENABLE_CODEGEN_arm
     87   kInstructionSimplifierArm,
     88 #endif
     89 #ifdef ART_ENABLE_CODEGEN_arm64
     90   kInstructionSimplifierArm64,
     91 #endif
     92 #ifdef ART_ENABLE_CODEGEN_mips
     93   kPcRelativeFixupsMips,
     94   kInstructionSimplifierMips,
     95 #endif
     96 #ifdef ART_ENABLE_CODEGEN_x86
     97   kPcRelativeFixupsX86,
     98   kInstructionSimplifierX86,
     99 #endif
    100 #ifdef ART_ENABLE_CODEGEN_x86_64
    101   kInstructionSimplifierX86_64,
    102 #endif
    103 #if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
    104   kX86MemoryOperandGeneration,
    105 #endif
    106   kNone,
    107   kLast = kNone
    108 };
    109 
    110 // Lookup name of optimization pass.
    111 const char* OptimizationPassName(OptimizationPass pass);
    112 
    113 // Lookup optimization pass by name.
    114 OptimizationPass OptimizationPassByName(const std::string& pass_name);
    115 
    116 // Optimization definition consisting of an optimization pass
    117 // an optional alternative name (nullptr denotes default), and
    118 // an optional pass dependence (kNone denotes no dependence).
    119 struct OptimizationDef {
    120   OptimizationDef(OptimizationPass p, const char* pn, OptimizationPass d)
    121       : pass(p), pass_name(pn), depends_on(d) {}
    122   OptimizationPass pass;
    123   const char* pass_name;
    124   OptimizationPass depends_on;
    125 };
    126 
    127 // Helper method for optimization definition array entries.
    128 inline OptimizationDef OptDef(OptimizationPass pass,
    129                               const char* pass_name = nullptr,
    130                               OptimizationPass depends_on = OptimizationPass::kNone) {
    131   return OptimizationDef(pass, pass_name, depends_on);
    132 }
    133 
    134 // Helper method to construct series of optimization passes.
    135 // The array should consist of the requested optimizations
    136 // and optional alternative names for repeated passes.
    137 // Example:
    138 //    { OptPass(kConstantFolding),
    139 //      OptPass(Inliner),
    140 //      OptPass(kConstantFolding, "constant_folding$after_inlining")
    141 //    }
    142 ArenaVector<HOptimization*> ConstructOptimizations(
    143     const OptimizationDef definitions[],
    144     size_t length,
    145     ArenaAllocator* allocator,
    146     HGraph* graph,
    147     OptimizingCompilerStats* stats,
    148     CodeGenerator* codegen,
    149     const DexCompilationUnit& dex_compilation_unit,
    150     VariableSizedHandleScope* handles);
    151 
    152 }  // namespace art
    153 
    154 #endif  // ART_COMPILER_OPTIMIZING_OPTIMIZATION_H_
    155