blob: c170f155fac1ee74ecffc82914315776d7978aeb [file] [log] [blame]
Roland Levillain75be2832014-10-17 17:02:00 +01001/*
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 Markoa3a3c592015-06-12 14:30:53 +010020#include "base/arena_object.h"
Roland Levillain75be2832014-10-17 17:02:00 +010021#include "nodes.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "optimizing_compiler_stats.h"
Roland Levillain75be2832014-10-17 17:02:00 +010023
24namespace art {
25
Aart Bik2ca10eb2017-11-15 15:17:53 -080026class CodeGenerator;
27class CompilerDriver;
28class DexCompilationUnit;
29
Roland Levillain75be2832014-10-17 17:02:00 +010030/**
31 * Abstraction to implement an optimization pass.
32 */
Vladimir Markof9f64412015-09-02 14:05:49 +010033class HOptimization : public ArenaObject<kArenaAllocOptimization> {
Roland Levillain75be2832014-10-17 17:02:00 +010034 public:
35 HOptimization(HGraph* graph,
Calin Juravleacf735c2015-02-12 15:25:22 +000036 const char* pass_name,
37 OptimizingCompilerStats* stats = nullptr)
Roland Levillain75be2832014-10-17 17:02:00 +010038 : graph_(graph),
Calin Juravleacf735c2015-02-12 15:25:22 +000039 stats_(stats),
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000040 pass_name_(pass_name) {}
Roland Levillain75be2832014-10-17 17:02:00 +010041
42 virtual ~HOptimization() {}
43
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070044 // 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$after_bce',
47 // 'instruction_simplifier$before_codegen'.
Roland Levillain75be2832014-10-17 17:02:00 +010048 const char* GetPassName() const { return pass_name_; }
49
Aart Bik854a02b2015-07-14 16:07:00 -070050 // Perform the analysis itself.
Roland Levillain75be2832014-10-17 17:02:00 +010051 virtual void Run() = 0;
52
Roland Levillain75be2832014-10-17 17:02:00 +010053 protected:
54 HGraph* const graph_;
Calin Juravleacf735c2015-02-12 15:25:22 +000055 // Used to record stats about the optimization.
56 OptimizingCompilerStats* const stats_;
Roland Levillain75be2832014-10-17 17:02:00 +010057
58 private:
Roland Levillain75be2832014-10-17 17:02:00 +010059 // Optimization pass name.
60 const char* pass_name_;
Roland Levillain75be2832014-10-17 17:02:00 +010061
62 DISALLOW_COPY_AND_ASSIGN(HOptimization);
63};
64
Aart Bik2ca10eb2017-11-15 15:17:53 -080065// 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?
68enum class OptimizationPass {
69 kBoundsCheckElimination,
70 kCHAGuardOptimization,
71 kCodeSinking,
72 kConstantFolding,
73 kConstructorFenceRedundancyElimination,
74 kDeadCodeElimination,
75 kGlobalValueNumbering,
76 kInductionVarAnalysis,
77 kInliner,
78 kInstructionSimplifier,
79 kIntrinsicsRecognizer,
80 kInvariantCodeMotion,
81 kLoadStoreAnalysis,
82 kLoadStoreElimination,
83 kLoopOptimization,
84 kScheduling,
85 kSelectGenerator,
86 kSharpening,
87 kSideEffectsAnalysis,
88#ifdef ART_ENABLE_CODEGEN_arm
89 kInstructionSimplifierArm,
90#endif
91#ifdef ART_ENABLE_CODEGEN_arm64
92 kInstructionSimplifierArm64,
93#endif
94#ifdef ART_ENABLE_CODEGEN_mips
95 kPcRelativeFixupsMips,
96 kInstructionSimplifierMips,
97#endif
98#ifdef ART_ENABLE_CODEGEN_x86
99 kPcRelativeFixupsX86,
100#endif
101#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
102 kX86MemoryOperandGeneration,
103#endif
104};
105
106// Lookup name of optimization pass.
107const char* OptimizationPassName(OptimizationPass pass);
108
109// Lookup optimization pass by name.
110OptimizationPass OptimizationPassByName(const std::string& name);
111
112// Optimization definition consisting of an optimization pass
113// and an optional alternative name (nullptr denotes default).
114typedef std::pair<OptimizationPass, const char*> OptimizationDef;
115
116// Helper method for optimization definition array entries.
117inline OptimizationDef OptDef(OptimizationPass pass, const char* name = nullptr) {
118 return std::make_pair(pass, name);
119}
120
121// Helper method to construct series of optimization passes.
122// The array should consist of the requested optimizations
123// and optional alternative names for repeated passes.
124// Example:
125// { OptPass(kConstantFolding),
126// OptPass(Inliner),
127// OptPass(kConstantFolding, "constant_folding$after_inlining")
128// }
129ArenaVector<HOptimization*> ConstructOptimizations(
130 const OptimizationDef definitions[],
131 size_t length,
132 ArenaAllocator* allocator,
133 HGraph* graph,
134 OptimizingCompilerStats* stats,
135 CodeGenerator* codegen,
136 CompilerDriver* driver,
137 const DexCompilationUnit& dex_compilation_unit,
138 VariableSizedHandleScope* handles);
139
Roland Levillain75be2832014-10-17 17:02:00 +0100140} // namespace art
141
142#endif // ART_COMPILER_OPTIMIZING_OPTIMIZATION_H_