blob: 4a515bcc907982ad76be75ec0a350d960c780f21 [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
Vladimir Marko0a516052019-10-14 13:00:44 +000024namespace art {
Roland Levillain75be2832014-10-17 17:02:00 +010025
Aart Bik2ca10eb2017-11-15 15:17:53 -080026class CodeGenerator;
Aart Bik2ca10eb2017-11-15 15:17:53 -080027class DexCompilationUnit;
28
Roland Levillain75be2832014-10-17 17:02:00 +010029/**
30 * Abstraction to implement an optimization pass.
31 */
Vladimir Markof9f64412015-09-02 14:05:49 +010032class HOptimization : public ArenaObject<kArenaAllocOptimization> {
Roland Levillain75be2832014-10-17 17:02:00 +010033 public:
34 HOptimization(HGraph* graph,
Calin Juravleacf735c2015-02-12 15:25:22 +000035 const char* pass_name,
36 OptimizingCompilerStats* stats = nullptr)
Roland Levillain75be2832014-10-17 17:02:00 +010037 : graph_(graph),
Calin Juravleacf735c2015-02-12 15:25:22 +000038 stats_(stats),
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000039 pass_name_(pass_name) {}
Roland Levillain75be2832014-10-17 17:02:00 +010040
41 virtual ~HOptimization() {}
42
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070043 // 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 Levillain75be2832014-10-17 17:02:00 +010047 const char* GetPassName() const { return pass_name_; }
48
Aart Bik24773202018-04-26 10:28:51 -070049 // 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 Levillain75be2832014-10-17 17:02:00 +010052
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,
Aart Bik2ca10eb2017-11-15 15:17:53 -080079 kInvariantCodeMotion,
Aart Bik2ca10eb2017-11-15 15:17:53 -080080 kLoadStoreElimination,
81 kLoopOptimization,
82 kScheduling,
83 kSelectGenerator,
Aart Bik2ca10eb2017-11-15 15:17:53 -080084 kSideEffectsAnalysis,
85#ifdef ART_ENABLE_CODEGEN_arm
86 kInstructionSimplifierArm,
87#endif
88#ifdef ART_ENABLE_CODEGEN_arm64
89 kInstructionSimplifierArm64,
90#endif
Aart Bik2ca10eb2017-11-15 15:17:53 -080091#ifdef ART_ENABLE_CODEGEN_x86
92 kPcRelativeFixupsX86,
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +053093 kInstructionSimplifierX86,
94#endif
95#ifdef ART_ENABLE_CODEGEN_x86_64
96 kInstructionSimplifierX86_64,
Aart Bik2ca10eb2017-11-15 15:17:53 -080097#endif
98#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
99 kX86MemoryOperandGeneration,
100#endif
Aart Bik2e148682018-04-18 16:11:12 -0700101 kNone,
102 kLast = kNone
Aart Bik2ca10eb2017-11-15 15:17:53 -0800103};
104
105// Lookup name of optimization pass.
106const char* OptimizationPassName(OptimizationPass pass);
107
108// Lookup optimization pass by name.
Aart Bik2e148682018-04-18 16:11:12 -0700109OptimizationPass OptimizationPassByName(const std::string& pass_name);
Aart Bik2ca10eb2017-11-15 15:17:53 -0800110
111// Optimization definition consisting of an optimization pass
Aart Bik2e148682018-04-18 16:11:12 -0700112// an optional alternative name (nullptr denotes default), and
113// an optional pass dependence (kNone denotes no dependence).
114struct OptimizationDef {
115 OptimizationDef(OptimizationPass p, const char* pn, OptimizationPass d)
116 : pass(p), pass_name(pn), depends_on(d) {}
117 OptimizationPass pass;
118 const char* pass_name;
119 OptimizationPass depends_on;
120};
Aart Bik2ca10eb2017-11-15 15:17:53 -0800121
122// Helper method for optimization definition array entries.
Aart Bik2e148682018-04-18 16:11:12 -0700123inline OptimizationDef OptDef(OptimizationPass pass,
124 const char* pass_name = nullptr,
125 OptimizationPass depends_on = OptimizationPass::kNone) {
126 return OptimizationDef(pass, pass_name, depends_on);
Aart Bik2ca10eb2017-11-15 15:17:53 -0800127}
128
129// Helper method to construct series of optimization passes.
130// The array should consist of the requested optimizations
131// and optional alternative names for repeated passes.
132// Example:
133// { OptPass(kConstantFolding),
134// OptPass(Inliner),
135// OptPass(kConstantFolding, "constant_folding$after_inlining")
136// }
137ArenaVector<HOptimization*> ConstructOptimizations(
138 const OptimizationDef definitions[],
139 size_t length,
140 ArenaAllocator* allocator,
141 HGraph* graph,
142 OptimizingCompilerStats* stats,
143 CodeGenerator* codegen,
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100144 const DexCompilationUnit& dex_compilation_unit);
Aart Bik2ca10eb2017-11-15 15:17:53 -0800145
Roland Levillain75be2832014-10-17 17:02:00 +0100146} // namespace art
147
148#endif // ART_COMPILER_OPTIMIZING_OPTIMIZATION_H_