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