James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace art { |
| 24 | |
| 25 | // Forward declarations. |
| 26 | struct BasicBlock; |
| 27 | struct CompilationUnit; |
| 28 | class 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 | */ |
| 34 | enum OptimizationFlag { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 35 | 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 Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | // Data holder class. |
| 41 | class PassMEDataHolder: public PassDataHolder { |
| 42 | public: |
| 43 | CompilationUnit* c_unit; |
| 44 | BasicBlock* bb; |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 45 | void* data; |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | enum 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 Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 56 | kTopologicalSortTraversal, /**< @brief Topological Order traversal. */ |
| 57 | kRepeatingTopologicalSortTraversal, /**< @brief Repeating Topological Order traversal. */ |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 58 | kLoopRepeatingTopologicalSortTraversal, /**< @brief Loop-repeating Topological Order traversal. */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 59 | 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 | */ |
| 67 | class 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 Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame^] | 83 | default_options_.clear(); |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | virtual DataFlowAnalysisMode GetTraversal() const { |
| 87 | return traversal_type_; |
| 88 | } |
| 89 | |
Razvan A Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame^] | 90 | /** |
| 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 Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 130 | 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 Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame^] | 139 | 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 Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 158 | /** @brief Type of traversal: determines the order to execute the pass on the BasicBlocks. */ |
| 159 | const DataFlowAnalysisMode traversal_type_; |
| 160 | |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 161 | /** @brief Flags for additional directives: used to determine if a particular post-optimization pass is necessary. */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 162 | 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 Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame^] | 166 | |
| 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 Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 173 | }; |
| 174 | } // namespace art |
| 175 | #endif // ART_COMPILER_DEX_PASS_ME_H_ |