Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [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_DRIVER_H_ |
| 18 | #define ART_COMPILER_DEX_PASS_DRIVER_H_ |
| 19 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 20 | #include <vector> |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 21 | #include "pass.h" |
| 22 | #include "safe_map.h" |
| 23 | |
| 24 | // Forward Declarations. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 25 | class Pass; |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 26 | class PassDriver; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 27 | namespace art { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 28 | /** |
| 29 | * @brief Helper function to create a single instance of a given Pass and can be shared across |
| 30 | * the threads. |
| 31 | */ |
| 32 | template <typename PassType> |
| 33 | const Pass* GetPassInstance() { |
| 34 | static const PassType pass; |
| 35 | return &pass; |
| 36 | } |
| 37 | |
| 38 | // Empty holder for the constructor. |
| 39 | class PassDriverDataHolder { |
| 40 | }; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * @class PassDriver |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 44 | * @brief PassDriver is the wrapper around all Pass instances in order to execute them |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 45 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 46 | template <typename PassDriverType> |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 47 | class PassDriver { |
| 48 | public: |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 49 | explicit PassDriver() { |
| 50 | InitializePasses(); |
| 51 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 52 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 53 | virtual ~PassDriver() { |
| 54 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * @brief Insert a Pass: can warn if multiple passes have the same name. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 58 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 59 | void InsertPass(const Pass* new_pass) { |
| 60 | DCHECK(new_pass != nullptr); |
| 61 | DCHECK(new_pass->GetName() != nullptr && new_pass->GetName()[0] != 0); |
| 62 | |
| 63 | // It is an error to override an existing pass. |
| 64 | DCHECK(GetPass(new_pass->GetName()) == nullptr) |
| 65 | << "Pass name " << new_pass->GetName() << " already used."; |
| 66 | |
| 67 | // Now add to the list. |
| 68 | pass_list_.push_back(new_pass); |
| 69 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * @brief Run a pass using the name as key. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 73 | * @return whether the pass was applied. |
| 74 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 75 | virtual bool RunPass(const char* pass_name) { |
| 76 | // Paranoid: c_unit cannot be nullptr and we need a pass name. |
| 77 | DCHECK(pass_name != nullptr && pass_name[0] != 0); |
| 78 | |
| 79 | const Pass* cur_pass = GetPass(pass_name); |
| 80 | |
| 81 | if (cur_pass != nullptr) { |
| 82 | return RunPass(cur_pass); |
| 83 | } |
| 84 | |
| 85 | // Return false, we did not find the pass. |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @brief Runs all the passes with the pass_list_. |
| 91 | */ |
| 92 | void Launch() { |
| 93 | for (const Pass* cur_pass : pass_list_) { |
| 94 | RunPass(cur_pass); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @brief Searches for a particular pass. |
| 100 | * @param the name of the pass to be searched for. |
| 101 | */ |
| 102 | const Pass* GetPass(const char* name) const { |
| 103 | for (const Pass* cur_pass : pass_list_) { |
| 104 | if (strcmp(name, cur_pass->GetName()) == 0) { |
| 105 | return cur_pass; |
| 106 | } |
| 107 | } |
| 108 | return nullptr; |
| 109 | } |
| 110 | |
| 111 | static void CreateDefaultPassList(const std::string& disable_passes) { |
| 112 | // Insert each pass from g_passes into g_default_pass_list. |
| 113 | PassDriverType::g_default_pass_list.clear(); |
| 114 | PassDriverType::g_default_pass_list.reserve(PassDriver<PassDriverType>::g_passes_size); |
| 115 | for (uint16_t i = 0; i < PassDriver<PassDriverType>::g_passes_size; ++i) { |
| 116 | const Pass* pass = PassDriver<PassDriverType>::g_passes[i]; |
| 117 | // Check if we should disable this pass. |
| 118 | if (disable_passes.find(pass->GetName()) != std::string::npos) { |
| 119 | LOG(INFO) << "Skipping " << pass->GetName(); |
| 120 | } else { |
| 121 | PassDriver<PassDriverType>::g_default_pass_list.push_back(pass); |
| 122 | } |
| 123 | } |
| 124 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * @brief Run a pass using the Pass itself. |
| 128 | * @param time_split do we want a time split request(default: false)? |
| 129 | * @return whether the pass was applied. |
| 130 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 131 | virtual bool RunPass(const Pass* pass, bool time_split = false) = 0; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 132 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 133 | /** |
| 134 | * @brief Print the pass names of all the passes available. |
| 135 | */ |
| 136 | static void PrintPassNames() { |
| 137 | LOG(INFO) << "Loop Passes are:"; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 138 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 139 | for (const Pass* cur_pass : PassDriver<PassDriverType>::g_default_pass_list) { |
| 140 | LOG(INFO) << "\t-" << cur_pass->GetName(); |
| 141 | } |
| 142 | } |
| 143 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 144 | /** |
| 145 | * @brief Gets the list of passes currently schedule to execute. |
| 146 | * @return pass_list_ |
| 147 | */ |
| 148 | std::vector<const Pass*>& GetPasses() { |
| 149 | return pass_list_; |
| 150 | } |
| 151 | |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 152 | static void SetPrintAllPasses() { |
| 153 | default_print_passes_ = true; |
| 154 | } |
| 155 | |
Vladimir Marko | a9f1ce6 | 2014-05-28 21:41:35 +0100 | [diff] [blame] | 156 | static void SetDumpPassList(const std::string& list) { |
| 157 | dump_pass_list_ = list; |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Vladimir Marko | a9f1ce6 | 2014-05-28 21:41:35 +0100 | [diff] [blame] | 160 | static void SetPrintPassList(const std::string& list) { |
| 161 | print_pass_list_ = list; |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Razvan A Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame] | 164 | /** |
| 165 | * @brief Used to set a string that contains the overridden pass options. |
| 166 | * @details An overridden pass option means that the pass uses this option |
| 167 | * instead of using its default option. |
| 168 | * @param s The string passed by user with overridden options. The string is in format |
| 169 | * Pass1Name:Pass1Option:Pass1Setting,Pass2Name:Pass2Option::Pass2Setting |
| 170 | */ |
| 171 | static void SetOverriddenPassOptions(const std::string& s) { |
| 172 | overridden_pass_options_list_ = s; |
| 173 | } |
| 174 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 175 | void SetDefaultPasses() { |
| 176 | pass_list_ = PassDriver<PassDriverType>::g_default_pass_list; |
| 177 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 178 | |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 179 | protected: |
| 180 | virtual void InitializePasses() { |
| 181 | SetDefaultPasses(); |
| 182 | } |
| 183 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 184 | /** |
| 185 | * @brief Apply a patch: perform start/work/end functions. |
| 186 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 187 | virtual void ApplyPass(PassDataHolder* data, const Pass* pass) { |
| 188 | pass->Start(data); |
| 189 | DispatchPass(pass); |
| 190 | pass->End(data); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 191 | } |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 192 | /** |
| 193 | * @brief Dispatch a patch. |
| 194 | * Gives the ability to add logic when running the patch. |
| 195 | */ |
| 196 | virtual void DispatchPass(const Pass* pass) { |
| 197 | UNUSED(pass); |
| 198 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 199 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 200 | /** @brief List of passes: provides the order to execute the passes. */ |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 201 | std::vector<const Pass*> pass_list_; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 202 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 203 | /** @brief The number of passes within g_passes. */ |
| 204 | static const uint16_t g_passes_size; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 205 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 206 | /** @brief The number of passes within g_passes. */ |
| 207 | static const Pass* const g_passes[]; |
| 208 | |
| 209 | /** @brief The default pass list is used to initialize pass_list_. */ |
| 210 | static std::vector<const Pass*> g_default_pass_list; |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 211 | |
| 212 | /** @brief Do we, by default, want to be printing the log messages? */ |
| 213 | static bool default_print_passes_; |
| 214 | |
| 215 | /** @brief What are the passes we want to be printing the log messages? */ |
Vladimir Marko | a9f1ce6 | 2014-05-28 21:41:35 +0100 | [diff] [blame] | 216 | static std::string print_pass_list_; |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 217 | |
| 218 | /** @brief What are the passes we want to be dumping the CFG? */ |
Vladimir Marko | a9f1ce6 | 2014-05-28 21:41:35 +0100 | [diff] [blame] | 219 | static std::string dump_pass_list_; |
Razvan A Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame] | 220 | |
| 221 | /** @brief String of all options that should be overridden for selected passes */ |
| 222 | static std::string overridden_pass_options_list_; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | } // namespace art |
| 226 | #endif // ART_COMPILER_DEX_PASS_DRIVER_H_ |