blob: 9457d5be769b0f3da8b1635da8899c18ce206bd8 [file] [log] [blame]
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001/*
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_DEX_PASS_H_
18#define ART_COMPILER_DEX_PASS_H_
19
20#include <string>
21
22namespace art {
23
24// Forward declarations.
Ian Rogersb48b9eb2014-02-28 16:20:21 -080025struct BasicBlock;
26struct CompilationUnit;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080027class Pass;
28
29/**
30 * @brief OptimizationFlag is an enumeration to perform certain tasks for a given pass.
31 * @details Each enum should be a power of 2 to be correctly used.
32 */
33enum OptimizationFlag {
34};
35
36enum DataFlowAnalysisMode {
37 kAllNodes = 0, /**< @brief All nodes. */
38 kPreOrderDFSTraversal, /**< @brief Depth-First-Search / Pre-Order. */
39 kRepeatingPreOrderDFSTraversal, /**< @brief Depth-First-Search / Repeating Pre-Order. */
40 kReversePostOrderDFSTraversal, /**< @brief Depth-First-Search / Reverse Post-Order. */
41 kRepeatingPostOrderDFSTraversal, /**< @brief Depth-First-Search / Repeating Post-Order. */
42 kRepeatingReversePostOrderDFSTraversal, /**< @brief Depth-First-Search / Repeating Reverse Post-Order. */
43 kPostOrderDOMTraversal, /**< @brief Dominator tree / Post-Order. */
Vladimir Marko75ba13f2014-01-28 12:15:24 +000044 kNoNodes, /**< @brief Skip BasicBlock traversal. */
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080045};
46
47/**
48 * @class Pass
49 * @brief Pass is the Pass structure for the optimizations.
50 * @details The following structure has the different optimization passes that we are going to do.
51 */
52class Pass {
53 public:
Vladimir Marko75ba13f2014-01-28 12:15:24 +000054 explicit Pass(const char* name, DataFlowAnalysisMode type = kAllNodes,
55 unsigned int flags = 0u, const char* dump = "")
56 : pass_name_(name), traversal_type_(type), flags_(flags), dump_cfg_folder_(dump) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080057 }
58
Vladimir Marko75ba13f2014-01-28 12:15:24 +000059 Pass(const char* name, DataFlowAnalysisMode type, const char* dump)
60 : pass_name_(name), traversal_type_(type), flags_(0), dump_cfg_folder_(dump) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080061 }
62
Vladimir Marko75ba13f2014-01-28 12:15:24 +000063 Pass(const char* name, const char* dump)
64 : pass_name_(name), traversal_type_(kAllNodes), flags_(0), dump_cfg_folder_(dump) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080065 }
66
Vladimir Marko75ba13f2014-01-28 12:15:24 +000067 virtual ~Pass() {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080068 }
69
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080070 virtual const char* GetName() const {
71 return pass_name_;
72 }
73
74 virtual DataFlowAnalysisMode GetTraversal() const {
75 return traversal_type_;
76 }
77
78 virtual bool GetFlag(OptimizationFlag flag) const {
79 return (flags_ & flag);
80 }
81
Vladimir Marko75ba13f2014-01-28 12:15:24 +000082 const char* GetDumpCFGFolder() const {
83 return dump_cfg_folder_;
84 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080085
86 /**
87 * @brief Gate for the pass: determines whether to execute the pass or not considering a CompilationUnit
88 * @param c_unit the CompilationUnit.
89 * @return whether or not to execute the pass
90 */
Vladimir Marko75ba13f2014-01-28 12:15:24 +000091 virtual bool Gate(const CompilationUnit* c_unit) const {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080092 // Unused parameter.
93 UNUSED(c_unit);
94
95 // Base class says yes.
96 return true;
97 }
98
99 /**
100 * @brief Start of the pass: called before the WalkBasicBlocks function
101 * @param c_unit the considered CompilationUnit.
102 */
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000103 virtual void Start(CompilationUnit* c_unit) const {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800104 // Unused parameter.
105 UNUSED(c_unit);
106 }
107
108 /**
109 * @brief End of the pass: called after the WalkBasicBlocks function
110 * @param c_unit the considered CompilationUnit.
111 */
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000112 virtual void End(CompilationUnit* c_unit) const {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800113 // Unused parameter.
114 UNUSED(c_unit);
115 }
116
117 /**
118 * @brief Actually walk the BasicBlocks following a particular traversal type.
119 * @param c_unit the CompilationUnit.
120 * @param bb the BasicBlock.
121 * @return whether or not there is a change when walking the BasicBlock
122 */
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000123 virtual bool WalkBasicBlocks(CompilationUnit* c_unit, BasicBlock* bb) const {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800124 // Unused parameters.
125 UNUSED(c_unit);
126 UNUSED(bb);
127
128 // BasicBlock did not change.
129 return false;
130 }
131
132 protected:
133 /** @brief The pass name: used for searching for a pass when running a particular pass or debugging. */
134 const char* const pass_name_;
135
136 /** @brief Type of traversal: determines the order to execute the pass on the BasicBlocks. */
137 const DataFlowAnalysisMode traversal_type_;
138
139 /** @brief Flags for additional directives: used to determine if a particular clean-up is necessary post pass. */
140 const unsigned int flags_;
141
142 /** @brief CFG Dump Folder: what sub-folder to use for dumping the CFGs post pass. */
143 const char* const dump_cfg_folder_;
144
145 private:
146 // In order to make the all passes not copy-friendly.
147 DISALLOW_COPY_AND_ASSIGN(Pass);
148};
149} // namespace art
150#endif // ART_COMPILER_DEX_PASS_H_