blob: a9fafa0864469af82a1a6dd212555b6d3dceab97 [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 Bik24773202018-04-26 10:28:51 -070050 // 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;
Roland Levillain75be2832014-10-17 17:02:00 +010053
Roland Levillain75be2832014-10-17 17:02:00 +010054 protected:
55 HGraph* const graph_;
Calin Juravleacf735c2015-02-12 15:25:22 +000056 // Used to record stats about the optimization.
57 OptimizingCompilerStats* const stats_;
Roland Levillain75be2832014-10-17 17:02:00 +010058
59 private:
Roland Levillain75be2832014-10-17 17:02:00 +010060 // Optimization pass name.
61 const char* pass_name_;
Roland Levillain75be2832014-10-17 17:02:00 +010062
63 DISALLOW_COPY_AND_ASSIGN(HOptimization);
64};
65
Aart Bik2ca10eb2017-11-15 15:17:53 -080066// 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?
69enum class OptimizationPass {
70 kBoundsCheckElimination,
71 kCHAGuardOptimization,
72 kCodeSinking,
73 kConstantFolding,
74 kConstructorFenceRedundancyElimination,
75 kDeadCodeElimination,
76 kGlobalValueNumbering,
77 kInductionVarAnalysis,
78 kInliner,
79 kInstructionSimplifier,
80 kIntrinsicsRecognizer,
81 kInvariantCodeMotion,
82 kLoadStoreAnalysis,
83 kLoadStoreElimination,
84 kLoopOptimization,
85 kScheduling,
86 kSelectGenerator,
87 kSharpening,
88 kSideEffectsAnalysis,
89#ifdef ART_ENABLE_CODEGEN_arm
90 kInstructionSimplifierArm,
91#endif
92#ifdef ART_ENABLE_CODEGEN_arm64
93 kInstructionSimplifierArm64,
94#endif
95#ifdef ART_ENABLE_CODEGEN_mips
96 kPcRelativeFixupsMips,
97 kInstructionSimplifierMips,
98#endif
99#ifdef ART_ENABLE_CODEGEN_x86
100 kPcRelativeFixupsX86,
101#endif
102#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
103 kX86MemoryOperandGeneration,
Gupta Kumar, Sanjiv61908882018-06-29 13:06:35 +0530104 kInstructionSimplifierX86,
Aart Bik2ca10eb2017-11-15 15:17:53 -0800105#endif
Aart Bik2e148682018-04-18 16:11:12 -0700106 kNone,
107 kLast = kNone
Aart Bik2ca10eb2017-11-15 15:17:53 -0800108};
109
110// Lookup name of optimization pass.
111const char* OptimizationPassName(OptimizationPass pass);
112
113// Lookup optimization pass by name.
Aart Bik2e148682018-04-18 16:11:12 -0700114OptimizationPass OptimizationPassByName(const std::string& pass_name);
Aart Bik2ca10eb2017-11-15 15:17:53 -0800115
116// Optimization definition consisting of an optimization pass
Aart Bik2e148682018-04-18 16:11:12 -0700117// an optional alternative name (nullptr denotes default), and
118// an optional pass dependence (kNone denotes no dependence).
119struct 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};
Aart Bik2ca10eb2017-11-15 15:17:53 -0800126
127// Helper method for optimization definition array entries.
Aart Bik2e148682018-04-18 16:11:12 -0700128inline OptimizationDef OptDef(OptimizationPass pass,
129 const char* pass_name = nullptr,
130 OptimizationPass depends_on = OptimizationPass::kNone) {
131 return OptimizationDef(pass, pass_name, depends_on);
Aart Bik2ca10eb2017-11-15 15:17:53 -0800132}
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// }
142ArenaVector<HOptimization*> ConstructOptimizations(
143 const OptimizationDef definitions[],
144 size_t length,
145 ArenaAllocator* allocator,
146 HGraph* graph,
147 OptimizingCompilerStats* stats,
148 CodeGenerator* codegen,
149 CompilerDriver* driver,
150 const DexCompilationUnit& dex_compilation_unit,
151 VariableSizedHandleScope* handles);
152
Roland Levillain75be2832014-10-17 17:02:00 +0100153} // namespace art
154
155#endif // ART_COMPILER_OPTIMIZING_OPTIMIZATION_H_