blob: 8a3eae1486b3a168a30dff5a461cf4bc85a76604 [file] [log] [blame]
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001/*
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 Marko75ba13f2014-01-28 12:15:24 +000020#include <vector>
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080021#include "pass.h"
22#include "safe_map.h"
23
24// Forward Declarations.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080025class Pass;
James C Scott4f596682014-05-01 05:52:04 -070026class PassDriver;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080027namespace art {
James C Scott4f596682014-05-01 05:52:04 -070028/**
29 * @brief Helper function to create a single instance of a given Pass and can be shared across
30 * the threads.
31 */
32template <typename PassType>
33const Pass* GetPassInstance() {
34 static const PassType pass;
35 return &pass;
36}
37
38// Empty holder for the constructor.
39class PassDriverDataHolder {
40};
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080041
42/**
43 * @class PassDriver
James C Scott4f596682014-05-01 05:52:04 -070044 * @brief PassDriver is the wrapper around all Pass instances in order to execute them
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080045 */
James C Scott4f596682014-05-01 05:52:04 -070046template <typename PassDriverType>
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080047class PassDriver {
48 public:
James C Scott4f596682014-05-01 05:52:04 -070049 explicit PassDriver() {
50 InitializePasses();
51 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080052
James C Scott4f596682014-05-01 05:52:04 -070053 virtual ~PassDriver() {
54 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080055
56 /**
57 * @brief Insert a Pass: can warn if multiple passes have the same name.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080058 */
James C Scott4f596682014-05-01 05:52:04 -070059 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 Beyler4e97c532014-01-07 10:07:18 -080070
71 /**
72 * @brief Run a pass using the name as key.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080073 * @return whether the pass was applied.
74 */
James C Scott4f596682014-05-01 05:52:04 -070075 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 Beyler4e97c532014-01-07 10:07:18 -0800125
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 Scott4f596682014-05-01 05:52:04 -0700131 virtual bool RunPass(const Pass* pass, bool time_split = false) = 0;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800132
James C Scott4f596682014-05-01 05:52:04 -0700133 /**
134 * @brief Print the pass names of all the passes available.
135 */
136 static void PrintPassNames() {
137 LOG(INFO) << "Loop Passes are:";
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800138
James C Scott4f596682014-05-01 05:52:04 -0700139 for (const Pass* cur_pass : PassDriver<PassDriverType>::g_default_pass_list) {
140 LOG(INFO) << "\t-" << cur_pass->GetName();
141 }
142 }
143
James C Scott4f596682014-05-01 05:52:04 -0700144 /**
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 Beyler8bcecce2014-04-29 13:42:08 -0700152 static void SetPrintAllPasses() {
153 default_print_passes_ = true;
154 }
155
Vladimir Markoa9f1ce62014-05-28 21:41:35 +0100156 static void SetDumpPassList(const std::string& list) {
157 dump_pass_list_ = list;
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700158 }
159
Vladimir Markoa9f1ce62014-05-28 21:41:35 +0100160 static void SetPrintPassList(const std::string& list) {
161 print_pass_list_ = list;
James C Scott4f596682014-05-01 05:52:04 -0700162 }
163
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700164 /**
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 Scott4f596682014-05-01 05:52:04 -0700175 void SetDefaultPasses() {
176 pass_list_ = PassDriver<PassDriverType>::g_default_pass_list;
177 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800178
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700179 protected:
180 virtual void InitializePasses() {
181 SetDefaultPasses();
182 }
183
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800184 /**
185 * @brief Apply a patch: perform start/work/end functions.
186 */
James C Scott4f596682014-05-01 05:52:04 -0700187 virtual void ApplyPass(PassDataHolder* data, const Pass* pass) {
188 pass->Start(data);
189 DispatchPass(pass);
190 pass->End(data);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800191 }
James C Scott4f596682014-05-01 05:52:04 -0700192 /**
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 Beyler4e97c532014-01-07 10:07:18 -0800199
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800200 /** @brief List of passes: provides the order to execute the passes. */
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000201 std::vector<const Pass*> pass_list_;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800202
James C Scott4f596682014-05-01 05:52:04 -0700203 /** @brief The number of passes within g_passes. */
204 static const uint16_t g_passes_size;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800205
James C Scott4f596682014-05-01 05:52:04 -0700206 /** @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 Beyler8bcecce2014-04-29 13:42:08 -0700211
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 Markoa9f1ce62014-05-28 21:41:35 +0100216 static std::string print_pass_list_;
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700217
218 /** @brief What are the passes we want to be dumping the CFG? */
Vladimir Markoa9f1ce62014-05-28 21:41:35 +0100219 static std::string dump_pass_list_;
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700220
221 /** @brief String of all options that should be overridden for selected passes */
222 static std::string overridden_pass_options_list_;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800223};
224
225} // namespace art
226#endif // ART_COMPILER_DEX_PASS_DRIVER_H_