blob: 0865afd1a434a5d02c07974adf34d172a755707a [file] [log] [blame]
James C Scott4f596682014-05-01 05:52:04 -07001/*
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_ME_H_
18#define ART_COMPILER_DEX_PASS_ME_H_
19
20#include <string>
21#include "pass.h"
22
23namespace art {
24
25// Forward declarations.
26struct BasicBlock;
27struct CompilationUnit;
28class Pass;
29
30/**
31 * @brief OptimizationFlag is an enumeration to perform certain tasks for a given pass.
32 * @details Each enum should be a power of 2 to be correctly used.
33 */
34enum OptimizationFlag {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070035 kOptimizationBasicBlockChange = 1, /**< @brief Has there been a change to a BasicBlock? */
36 kOptimizationDefUsesChange = 2, /**< @brief Has there been a change to a def-use? */
37 kLoopStructureChange = 4, /**< @brief Has there been a loop structural change? */
James C Scott4f596682014-05-01 05:52:04 -070038};
39
40// Data holder class.
41class PassMEDataHolder: public PassDataHolder {
42 public:
43 CompilationUnit* c_unit;
44 BasicBlock* bb;
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070045 void* data;
James C Scott4f596682014-05-01 05:52:04 -070046};
47
48enum DataFlowAnalysisMode {
49 kAllNodes = 0, /**< @brief All nodes. */
50 kPreOrderDFSTraversal, /**< @brief Depth-First-Search / Pre-Order. */
51 kRepeatingPreOrderDFSTraversal, /**< @brief Depth-First-Search / Repeating Pre-Order. */
52 kReversePostOrderDFSTraversal, /**< @brief Depth-First-Search / Reverse Post-Order. */
53 kRepeatingPostOrderDFSTraversal, /**< @brief Depth-First-Search / Repeating Post-Order. */
54 kRepeatingReversePostOrderDFSTraversal, /**< @brief Depth-First-Search / Repeating Reverse Post-Order. */
55 kPostOrderDOMTraversal, /**< @brief Dominator tree / Post-Order. */
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -070056 kTopologicalSortTraversal, /**< @brief Topological Order traversal. */
57 kRepeatingTopologicalSortTraversal, /**< @brief Repeating Topological Order traversal. */
Vladimir Marko55fff042014-07-10 12:42:52 +010058 kLoopRepeatingTopologicalSortTraversal, /**< @brief Loop-repeating Topological Order traversal. */
James C Scott4f596682014-05-01 05:52:04 -070059 kNoNodes, /**< @brief Skip BasicBlock traversal. */
60};
61
62/**
63 * @class Pass
64 * @brief Pass is the Pass structure for the optimizations.
65 * @details The following structure has the different optimization passes that we are going to do.
66 */
67class PassME: public Pass {
68 public:
69 explicit PassME(const char* name, DataFlowAnalysisMode type = kAllNodes,
70 unsigned int flags = 0u, const char* dump = "")
71 : Pass(name), traversal_type_(type), flags_(flags), dump_cfg_folder_(dump) {
72 }
73
74 PassME(const char* name, DataFlowAnalysisMode type, const char* dump)
75 : Pass(name), traversal_type_(type), flags_(0), dump_cfg_folder_(dump) {
76 }
77
78 PassME(const char* name, const char* dump)
79 : Pass(name), traversal_type_(kAllNodes), flags_(0), dump_cfg_folder_(dump) {
80 }
81
82 ~PassME() {
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -070083 default_options_.clear();
James C Scott4f596682014-05-01 05:52:04 -070084 }
85
86 virtual DataFlowAnalysisMode GetTraversal() const {
87 return traversal_type_;
88 }
89
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -070090 /**
91 * @return Returns whether the pass has any configurable options.
92 */
93 bool HasOptions() const {
94 return default_options_.size() != 0;
95 }
96
97 /**
98 * @brief Prints the pass options along with default settings if there are any.
99 * @details The printing is done using LOG(INFO).
100 */
101 void PrintPassDefaultOptions() const {
102 for (auto option_it = default_options_.begin(); option_it != default_options_.end(); option_it++) {
103 LOG(INFO) << "\t" << option_it->first << ":" << std::dec << option_it->second;
104 }
105 }
106
107 /**
108 * @brief Prints the pass options along with either default or overridden setting.
109 * @param overridden_options The overridden settings for this pass.
110 */
111 void PrintPassOptions(SafeMap<const std::string, int>& overridden_options) const {
112 // We walk through the default options only to get the pass names. We use GetPassOption to
113 // also consider the overridden ones.
114 for (auto option_it = default_options_.begin(); option_it != default_options_.end(); option_it++) {
115 LOG(INFO) << "\t" << option_it->first << ":" << std::dec << GetPassOption(option_it->first, overridden_options);
116 }
117 }
118
119 /**
120 * @brief Used to obtain the option for a pass.
121 * @details Will return the overridden option if it exists or default one.
122 * @param option_name The name of option whose setting to look for.
123 * @param c_unit The compilation unit currently being handled.
124 * @return Returns the setting for the pass option.
125 */
126 int GetPassOption(const char* option_name, CompilationUnit* c_unit) const {
127 return GetPassOption(option_name, c_unit->overridden_pass_options);
128 }
129
James C Scott4f596682014-05-01 05:52:04 -0700130 const char* GetDumpCFGFolder() const {
131 return dump_cfg_folder_;
132 }
133
134 bool GetFlag(OptimizationFlag flag) const {
135 return (flags_ & flag);
136 }
137
138 protected:
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700139 int GetPassOption(const char* option_name, const SafeMap<const std::string, int>& overridden_options) const {
140 // First check if there are any overridden settings.
141 auto overridden_it = overridden_options.find(std::string(option_name));
142 if (overridden_it != overridden_options.end()) {
143 return overridden_it->second;
144 }
145
146 // Next check the default options.
147 auto default_it = default_options_.find(option_name);
148
149 if (default_it == default_options_.end()) {
150 // An invalid option is being requested.
151 DCHECK(false);
152 return 0;
153 }
154
155 return default_it->second;
156 }
157
James C Scott4f596682014-05-01 05:52:04 -0700158 /** @brief Type of traversal: determines the order to execute the pass on the BasicBlocks. */
159 const DataFlowAnalysisMode traversal_type_;
160
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700161 /** @brief Flags for additional directives: used to determine if a particular post-optimization pass is necessary. */
James C Scott4f596682014-05-01 05:52:04 -0700162 const unsigned int flags_;
163
164 /** @brief CFG Dump Folder: what sub-folder to use for dumping the CFGs post pass. */
165 const char* const dump_cfg_folder_;
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700166
167 /**
168 * @brief Contains a map of options with the default settings.
169 * @details The constructor of the specific pass instance should fill this
170 * with default options.
171 * */
172 SafeMap<const char*, int> default_options_;
James C Scott4f596682014-05-01 05:52:04 -0700173};
174} // namespace art
175#endif // ART_COMPILER_DEX_PASS_ME_H_