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> |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 21 | |
| 22 | #include "base/logging.h" |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 23 | #include "pass.h" |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame^] | 24 | #include "pass_manager.h" |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 25 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 26 | namespace art { |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 27 | |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame^] | 28 | class Pass; |
| 29 | class PassDataHolder; |
| 30 | class PassDriver; |
| 31 | class PassManager; |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 32 | |
| 33 | // Empty holder for the constructor. |
| 34 | class PassDriverDataHolder { |
| 35 | }; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * @class PassDriver |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 39 | * @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] | 40 | */ |
| 41 | class PassDriver { |
| 42 | public: |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame^] | 43 | explicit PassDriver(const PassManager* const pass_manager) : pass_manager_(pass_manager) { |
| 44 | pass_list_ = *pass_manager_->GetDefaultPassList(); |
| 45 | DCHECK(!pass_list_.empty()); |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 46 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 47 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 48 | virtual ~PassDriver() { |
| 49 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * @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] | 53 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 54 | void InsertPass(const Pass* new_pass) { |
| 55 | DCHECK(new_pass != nullptr); |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame^] | 56 | DCHECK(new_pass->GetName() != nullptr); |
| 57 | DCHECK_NE(new_pass->GetName()[0], 0); |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 58 | |
| 59 | // It is an error to override an existing pass. |
| 60 | DCHECK(GetPass(new_pass->GetName()) == nullptr) |
| 61 | << "Pass name " << new_pass->GetName() << " already used."; |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 62 | // Now add to the list. |
| 63 | pass_list_.push_back(new_pass); |
| 64 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * @brief Run a pass using the name as key. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 68 | * @return whether the pass was applied. |
| 69 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 70 | virtual bool RunPass(const char* pass_name) { |
| 71 | // Paranoid: c_unit cannot be nullptr and we need a pass name. |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame^] | 72 | DCHECK(pass_name != nullptr); |
| 73 | DCHECK_NE(pass_name[0], 0); |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 74 | |
| 75 | const Pass* cur_pass = GetPass(pass_name); |
| 76 | |
| 77 | if (cur_pass != nullptr) { |
| 78 | return RunPass(cur_pass); |
| 79 | } |
| 80 | |
| 81 | // Return false, we did not find the pass. |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @brief Runs all the passes with the pass_list_. |
| 87 | */ |
| 88 | void Launch() { |
| 89 | for (const Pass* cur_pass : pass_list_) { |
| 90 | RunPass(cur_pass); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @brief Searches for a particular pass. |
| 96 | * @param the name of the pass to be searched for. |
| 97 | */ |
| 98 | const Pass* GetPass(const char* name) const { |
| 99 | for (const Pass* cur_pass : pass_list_) { |
| 100 | if (strcmp(name, cur_pass->GetName()) == 0) { |
| 101 | return cur_pass; |
| 102 | } |
| 103 | } |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 107 | /** |
| 108 | * @brief Run a pass using the Pass itself. |
| 109 | * @param time_split do we want a time split request(default: false)? |
| 110 | * @return whether the pass was applied. |
| 111 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 112 | virtual bool RunPass(const Pass* pass, bool time_split = false) = 0; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 113 | |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 114 | protected: |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 115 | /** |
| 116 | * @brief Apply a patch: perform start/work/end functions. |
| 117 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 118 | virtual void ApplyPass(PassDataHolder* data, const Pass* pass) { |
| 119 | pass->Start(data); |
| 120 | DispatchPass(pass); |
| 121 | pass->End(data); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 122 | } |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame^] | 123 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 124 | /** |
| 125 | * @brief Dispatch a patch. |
| 126 | * Gives the ability to add logic when running the patch. |
| 127 | */ |
| 128 | virtual void DispatchPass(const Pass* pass) { |
| 129 | UNUSED(pass); |
| 130 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 131 | |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame^] | 132 | /** @brief List of passes: provides the order to execute the passes. |
| 133 | * Passes are owned by pass_manager_. */ |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 134 | std::vector<const Pass*> pass_list_; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 135 | |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame^] | 136 | const PassManager* const pass_manager_; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | } // namespace art |
| 140 | #endif // ART_COMPILER_DEX_PASS_DRIVER_H_ |