blob: d3cf3933686d26505de3476dc66f4930aa7c671c [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>
Andreas Gampe0b9203e2015-01-22 20:39:27 -080021
22#include "base/logging.h"
James C Scott4f596682014-05-01 05:52:04 -070023#include "pass.h"
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +010024#include "compiler_ir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080025#include "safe_map.h"
James C Scott4f596682014-05-01 05:52:04 -070026
27namespace art {
28
29// Forward declarations.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070030class BasicBlock;
James C Scott4f596682014-05-01 05:52:04 -070031struct CompilationUnit;
James C Scott4f596682014-05-01 05:52:04 -070032
33/**
34 * @brief OptimizationFlag is an enumeration to perform certain tasks for a given pass.
35 * @details Each enum should be a power of 2 to be correctly used.
36 */
37enum OptimizationFlag {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070038 kOptimizationBasicBlockChange = 1, /// @brief Has there been a change to a BasicBlock?
39 kOptimizationDefUsesChange = 2, /// @brief Has there been a change to a def-use?
40 kLoopStructureChange = 4, /// @brief Has there been a loop structural change?
James C Scott4f596682014-05-01 05:52:04 -070041};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070042std::ostream& operator<<(std::ostream& os, const OptimizationFlag& rhs);
James C Scott4f596682014-05-01 05:52:04 -070043
44// Data holder class.
45class PassMEDataHolder: public PassDataHolder {
Mathieu Chartier5bdab122015-01-26 18:30:19 -080046 public:
47 CompilationUnit* c_unit;
48 BasicBlock* bb;
49 void* data; /**< @brief Any data the pass wants to use */
50 bool dirty; /**< @brief Has the pass rendered the CFG dirty, requiring post-opt? */
James C Scott4f596682014-05-01 05:52:04 -070051};
52
53enum DataFlowAnalysisMode {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070054 kAllNodes = 0, /// @brief All nodes.
55 kPreOrderDFSTraversal, /// @brief Depth-First-Search / Pre-Order.
56 kRepeatingPreOrderDFSTraversal, /// @brief Depth-First-Search / Repeating Pre-Order.
57 kReversePostOrderDFSTraversal, /// @brief Depth-First-Search / Reverse Post-Order.
58 kRepeatingPostOrderDFSTraversal, /// @brief Depth-First-Search / Repeating Post-Order.
59 kRepeatingReversePostOrderDFSTraversal, /// @brief Depth-First-Search / Repeating Reverse Post-Order.
60 kPostOrderDOMTraversal, /// @brief Dominator tree / Post-Order.
61 kTopologicalSortTraversal, /// @brief Topological Order traversal.
62 kLoopRepeatingTopologicalSortTraversal, /// @brief Loop-repeating Topological Order traversal.
63 kNoNodes, /// @brief Skip BasicBlock traversal.
James C Scott4f596682014-05-01 05:52:04 -070064};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070065std::ostream& operator<<(std::ostream& os, const DataFlowAnalysisMode& rhs);
James C Scott4f596682014-05-01 05:52:04 -070066
67/**
68 * @class Pass
69 * @brief Pass is the Pass structure for the optimizations.
70 * @details The following structure has the different optimization passes that we are going to do.
71 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070072class PassME : public Pass {
James C Scott4f596682014-05-01 05:52:04 -070073 public:
74 explicit PassME(const char* name, DataFlowAnalysisMode type = kAllNodes,
75 unsigned int flags = 0u, const char* dump = "")
76 : Pass(name), traversal_type_(type), flags_(flags), dump_cfg_folder_(dump) {
77 }
78
79 PassME(const char* name, DataFlowAnalysisMode type, const char* dump)
80 : Pass(name), traversal_type_(type), flags_(0), dump_cfg_folder_(dump) {
81 }
82
83 PassME(const char* name, const char* dump)
84 : Pass(name), traversal_type_(kAllNodes), flags_(0), dump_cfg_folder_(dump) {
85 }
86
87 ~PassME() {
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -070088 default_options_.clear();
James C Scott4f596682014-05-01 05:52:04 -070089 }
90
91 virtual DataFlowAnalysisMode GetTraversal() const {
92 return traversal_type_;
93 }
94
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -070095 /**
96 * @return Returns whether the pass has any configurable options.
97 */
98 bool HasOptions() const {
99 return default_options_.size() != 0;
100 }
101
102 /**
103 * @brief Prints the pass options along with default settings if there are any.
104 * @details The printing is done using LOG(INFO).
105 */
106 void PrintPassDefaultOptions() const {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800107 for (const auto& option : default_options_) {
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100108 LOG(INFO) << "\t" << option.first << ":" << option.second;
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700109 }
110 }
111
112 /**
113 * @brief Prints the pass options along with either default or overridden setting.
114 * @param overridden_options The overridden settings for this pass.
115 */
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100116 void PrintPassOptions(SafeMap<const std::string, const OptionContent>& overridden_options) const {
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700117 // We walk through the default options only to get the pass names. We use GetPassOption to
118 // also consider the overridden ones.
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800119 for (const auto& option : default_options_) {
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100120 LOG(INFO) << "\t" << option.first << ":"
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800121 << GetPassOption(option.first, overridden_options);
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700122 }
123 }
124
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100125 /**
126 * @brief Used to obtain the option structure for a pass.
127 * @details Will return the overridden option if it exists or default one otherwise.
128 * @param option_name The name of option whose setting to look for.
129 * @param c_unit The compilation unit currently being handled.
130 * @return Returns the option structure containing the option value.
131 */
132 const OptionContent& GetPassOption(const char* option_name, CompilationUnit* c_unit) const {
133 return GetPassOption(option_name, c_unit->overridden_pass_options);
134 }
135
136 /**
137 * @brief Used to obtain the option for a pass as a string.
138 * @details Will return the overridden option if it exists or default one otherwise.
139 * It will return nullptr if the required option value is not a string.
140 * @param option_name The name of option whose setting to look for.
141 * @param c_unit The compilation unit currently being handled.
142 * @return Returns the overridden option if it exists or the default one otherwise.
143 */
144 const char* GetStringPassOption(const char* option_name, CompilationUnit* c_unit) const {
145 return GetStringPassOption(option_name, c_unit->overridden_pass_options);
146 }
147
148 /**
149 * @brief Used to obtain the pass option value as an integer.
150 * @details Will return the overridden option if it exists or default one otherwise.
151 * It will return 0 if the required option value is not an integer.
152 * @param c_unit The compilation unit currently being handled.
153 * @return Returns the overriden option if it exists or the default one otherwise.
154 */
155 int64_t GetIntegerPassOption(const char* option_name, CompilationUnit* c_unit) const {
156 return GetIntegerPassOption(option_name, c_unit->overridden_pass_options);
157 }
158
James C Scott4f596682014-05-01 05:52:04 -0700159 const char* GetDumpCFGFolder() const {
160 return dump_cfg_folder_;
161 }
162
163 bool GetFlag(OptimizationFlag flag) const {
164 return (flags_ & flag);
165 }
166
167 protected:
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100168 const OptionContent& GetPassOption(const char* option_name,
169 const SafeMap<const std::string, const OptionContent>& overridden_options) const {
170 DCHECK(option_name != nullptr);
171
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700172 // First check if there are any overridden settings.
173 auto overridden_it = overridden_options.find(std::string(option_name));
174 if (overridden_it != overridden_options.end()) {
175 return overridden_it->second;
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100176 } else {
177 // Otherwise, there must be a default value for this option name.
178 auto default_it = default_options_.find(option_name);
179 // An invalid option is being requested.
180 if (default_it == default_options_.end()) {
181 LOG(FATAL) << "Fatal: Cannot find an option named \"" << option_name << "\"";
182 }
183
184 return default_it->second;
185 }
186 }
187
188 const char* GetStringPassOption(const char* option_name,
189 const SafeMap<const std::string, const OptionContent>& overridden_options) const {
190 const OptionContent& option_content = GetPassOption(option_name, overridden_options);
191 if (option_content.type != OptionContent::kString) {
192 return nullptr;
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700193 }
194
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100195 return option_content.GetString();
196 }
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700197
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100198 int64_t GetIntegerPassOption(const char* option_name,
199 const SafeMap<const std::string, const OptionContent>& overridden_options) const {
200 const OptionContent& option_content = GetPassOption(option_name, overridden_options);
201 if (option_content.type != OptionContent::kInteger) {
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700202 return 0;
203 }
204
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100205 return option_content.GetInteger();
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700206 }
207
James C Scott4f596682014-05-01 05:52:04 -0700208 /** @brief Type of traversal: determines the order to execute the pass on the BasicBlocks. */
209 const DataFlowAnalysisMode traversal_type_;
210
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100211 /** @brief Flags for additional directives: used to determine if a particular
212 * post-optimization pass is necessary. */
James C Scott4f596682014-05-01 05:52:04 -0700213 const unsigned int flags_;
214
215 /** @brief CFG Dump Folder: what sub-folder to use for dumping the CFGs post pass. */
216 const char* const dump_cfg_folder_;
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700217
218 /**
219 * @brief Contains a map of options with the default settings.
220 * @details The constructor of the specific pass instance should fill this
221 * with default options.
222 * */
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100223 SafeMap<const char*, const OptionContent> default_options_;
James C Scott4f596682014-05-01 05:52:04 -0700224};
225} // namespace art
226#endif // ART_COMPILER_DEX_PASS_ME_H_