blob: bd8f53cd5a18d692d33936552273b921e07766d1 [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
164 void SetDefaultPasses() {
165 pass_list_ = PassDriver<PassDriverType>::g_default_pass_list;
166 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800167
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700168 protected:
169 virtual void InitializePasses() {
170 SetDefaultPasses();
171 }
172
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800173 /**
174 * @brief Apply a patch: perform start/work/end functions.
175 */
James C Scott4f596682014-05-01 05:52:04 -0700176 virtual void ApplyPass(PassDataHolder* data, const Pass* pass) {
177 pass->Start(data);
178 DispatchPass(pass);
179 pass->End(data);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800180 }
James C Scott4f596682014-05-01 05:52:04 -0700181 /**
182 * @brief Dispatch a patch.
183 * Gives the ability to add logic when running the patch.
184 */
185 virtual void DispatchPass(const Pass* pass) {
186 UNUSED(pass);
187 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800188
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800189 /** @brief List of passes: provides the order to execute the passes. */
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000190 std::vector<const Pass*> pass_list_;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800191
James C Scott4f596682014-05-01 05:52:04 -0700192 /** @brief The number of passes within g_passes. */
193 static const uint16_t g_passes_size;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800194
James C Scott4f596682014-05-01 05:52:04 -0700195 /** @brief The number of passes within g_passes. */
196 static const Pass* const g_passes[];
197
198 /** @brief The default pass list is used to initialize pass_list_. */
199 static std::vector<const Pass*> g_default_pass_list;
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700200
201 /** @brief Do we, by default, want to be printing the log messages? */
202 static bool default_print_passes_;
203
204 /** @brief What are the passes we want to be printing the log messages? */
Vladimir Markoa9f1ce62014-05-28 21:41:35 +0100205 static std::string print_pass_list_;
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700206
207 /** @brief What are the passes we want to be dumping the CFG? */
Vladimir Markoa9f1ce62014-05-28 21:41:35 +0100208 static std::string dump_pass_list_;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800209};
210
211} // namespace art
212#endif // ART_COMPILER_DEX_PASS_DRIVER_H_