blob: d0b450a3ac4abf6c57c7379565515fe1b1fbc2ff [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.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070026class BasicBlock;
James C Scott4f596682014-05-01 05:52:04 -070027struct 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 {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -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};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070039std::ostream& operator<<(std::ostream& os, const OptimizationFlag& rhs);
James C Scott4f596682014-05-01 05:52:04 -070040
41// Data holder class.
42class PassMEDataHolder: public PassDataHolder {
43 public:
44 CompilationUnit* c_unit;
45 BasicBlock* bb;
Jean Christophe Beyler09321df2014-07-18 15:33:57 -070046 void* data; /**< @brief Any data the pass wants to use */
47 bool dirty; /**< @brief Has the pass rendered the CFG dirty, requiring post-opt? */
James C Scott4f596682014-05-01 05:52:04 -070048};
49
50enum DataFlowAnalysisMode {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070051 kAllNodes = 0, /// @brief All nodes.
52 kPreOrderDFSTraversal, /// @brief Depth-First-Search / Pre-Order.
53 kRepeatingPreOrderDFSTraversal, /// @brief Depth-First-Search / Repeating Pre-Order.
54 kReversePostOrderDFSTraversal, /// @brief Depth-First-Search / Reverse Post-Order.
55 kRepeatingPostOrderDFSTraversal, /// @brief Depth-First-Search / Repeating Post-Order.
56 kRepeatingReversePostOrderDFSTraversal, /// @brief Depth-First-Search / Repeating Reverse Post-Order.
57 kPostOrderDOMTraversal, /// @brief Dominator tree / Post-Order.
58 kTopologicalSortTraversal, /// @brief Topological Order traversal.
59 kLoopRepeatingTopologicalSortTraversal, /// @brief Loop-repeating Topological Order traversal.
60 kNoNodes, /// @brief Skip BasicBlock traversal.
James C Scott4f596682014-05-01 05:52:04 -070061};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070062std::ostream& operator<<(std::ostream& os, const DataFlowAnalysisMode& rhs);
James C Scott4f596682014-05-01 05:52:04 -070063
64/**
65 * @class Pass
66 * @brief Pass is the Pass structure for the optimizations.
67 * @details The following structure has the different optimization passes that we are going to do.
68 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070069class PassME : public Pass {
James C Scott4f596682014-05-01 05:52:04 -070070 public:
71 explicit PassME(const char* name, DataFlowAnalysisMode type = kAllNodes,
72 unsigned int flags = 0u, const char* dump = "")
73 : Pass(name), traversal_type_(type), flags_(flags), dump_cfg_folder_(dump) {
74 }
75
76 PassME(const char* name, DataFlowAnalysisMode type, const char* dump)
77 : Pass(name), traversal_type_(type), flags_(0), dump_cfg_folder_(dump) {
78 }
79
80 PassME(const char* name, const char* dump)
81 : Pass(name), traversal_type_(kAllNodes), flags_(0), dump_cfg_folder_(dump) {
82 }
83
84 ~PassME() {
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -070085 default_options_.clear();
James C Scott4f596682014-05-01 05:52:04 -070086 }
87
88 virtual DataFlowAnalysisMode GetTraversal() const {
89 return traversal_type_;
90 }
91
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -070092 /**
93 * @return Returns whether the pass has any configurable options.
94 */
95 bool HasOptions() const {
96 return default_options_.size() != 0;
97 }
98
99 /**
100 * @brief Prints the pass options along with default settings if there are any.
101 * @details The printing is done using LOG(INFO).
102 */
103 void PrintPassDefaultOptions() const {
104 for (auto option_it = default_options_.begin(); option_it != default_options_.end(); option_it++) {
105 LOG(INFO) << "\t" << option_it->first << ":" << std::dec << option_it->second;
106 }
107 }
108
109 /**
110 * @brief Prints the pass options along with either default or overridden setting.
111 * @param overridden_options The overridden settings for this pass.
112 */
113 void PrintPassOptions(SafeMap<const std::string, int>& overridden_options) const {
114 // We walk through the default options only to get the pass names. We use GetPassOption to
115 // also consider the overridden ones.
116 for (auto option_it = default_options_.begin(); option_it != default_options_.end(); option_it++) {
117 LOG(INFO) << "\t" << option_it->first << ":" << std::dec << GetPassOption(option_it->first, overridden_options);
118 }
119 }
120
121 /**
122 * @brief Used to obtain the option for a pass.
123 * @details Will return the overridden option if it exists or default one.
124 * @param option_name The name of option whose setting to look for.
125 * @param c_unit The compilation unit currently being handled.
126 * @return Returns the setting for the pass option.
127 */
128 int GetPassOption(const char* option_name, CompilationUnit* c_unit) const {
129 return GetPassOption(option_name, c_unit->overridden_pass_options);
130 }
131
James C Scott4f596682014-05-01 05:52:04 -0700132 const char* GetDumpCFGFolder() const {
133 return dump_cfg_folder_;
134 }
135
136 bool GetFlag(OptimizationFlag flag) const {
137 return (flags_ & flag);
138 }
139
140 protected:
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700141 int GetPassOption(const char* option_name, const SafeMap<const std::string, int>& overridden_options) const {
142 // First check if there are any overridden settings.
143 auto overridden_it = overridden_options.find(std::string(option_name));
144 if (overridden_it != overridden_options.end()) {
145 return overridden_it->second;
146 }
147
148 // Next check the default options.
149 auto default_it = default_options_.find(option_name);
150
151 if (default_it == default_options_.end()) {
152 // An invalid option is being requested.
153 DCHECK(false);
154 return 0;
155 }
156
157 return default_it->second;
158 }
159
James C Scott4f596682014-05-01 05:52:04 -0700160 /** @brief Type of traversal: determines the order to execute the pass on the BasicBlocks. */
161 const DataFlowAnalysisMode traversal_type_;
162
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700163 /** @brief Flags for additional directives: used to determine if a particular post-optimization pass is necessary. */
James C Scott4f596682014-05-01 05:52:04 -0700164 const unsigned int flags_;
165
166 /** @brief CFG Dump Folder: what sub-folder to use for dumping the CFGs post pass. */
167 const char* const dump_cfg_folder_;
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700168
169 /**
170 * @brief Contains a map of options with the default settings.
171 * @details The constructor of the specific pass instance should fill this
172 * with default options.
173 * */
174 SafeMap<const char*, int> default_options_;
James C Scott4f596682014-05-01 05:52:04 -0700175};
176} // namespace art
177#endif // ART_COMPILER_DEX_PASS_ME_H_