blob: 671bcecfba334db225720e7480112d75417e94bd [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>
Andreas Gampe0b9203e2015-01-22 20:39:27 -080021
22#include "base/logging.h"
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080023#include "pass.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080024#include "pass_manager.h"
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080025
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080026namespace art {
Andreas Gampe0b9203e2015-01-22 20:39:27 -080027
Mathieu Chartier5bdab122015-01-26 18:30:19 -080028class Pass;
29class PassDataHolder;
30class PassDriver;
31class PassManager;
James C Scott4f596682014-05-01 05:52:04 -070032
33// Empty holder for the constructor.
34class PassDriverDataHolder {
35};
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080036
37/**
38 * @class PassDriver
James C Scott4f596682014-05-01 05:52:04 -070039 * @brief PassDriver is the wrapper around all Pass instances in order to execute them
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080040 */
41class PassDriver {
42 public:
Mathieu Chartier5bdab122015-01-26 18:30:19 -080043 explicit PassDriver(const PassManager* const pass_manager) : pass_manager_(pass_manager) {
44 pass_list_ = *pass_manager_->GetDefaultPassList();
45 DCHECK(!pass_list_.empty());
James C Scott4f596682014-05-01 05:52:04 -070046 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080047
James C Scott4f596682014-05-01 05:52:04 -070048 virtual ~PassDriver() {
49 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080050
51 /**
52 * @brief Insert a Pass: can warn if multiple passes have the same name.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080053 */
James C Scott4f596682014-05-01 05:52:04 -070054 void InsertPass(const Pass* new_pass) {
55 DCHECK(new_pass != nullptr);
Mathieu Chartier5bdab122015-01-26 18:30:19 -080056 DCHECK(new_pass->GetName() != nullptr);
57 DCHECK_NE(new_pass->GetName()[0], 0);
James C Scott4f596682014-05-01 05:52:04 -070058
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 Scott4f596682014-05-01 05:52:04 -070062 // Now add to the list.
63 pass_list_.push_back(new_pass);
64 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080065
66 /**
67 * @brief Run a pass using the name as key.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080068 * @return whether the pass was applied.
69 */
James C Scott4f596682014-05-01 05:52:04 -070070 virtual bool RunPass(const char* pass_name) {
71 // Paranoid: c_unit cannot be nullptr and we need a pass name.
Mathieu Chartier5bdab122015-01-26 18:30:19 -080072 DCHECK(pass_name != nullptr);
73 DCHECK_NE(pass_name[0], 0);
James C Scott4f596682014-05-01 05:52:04 -070074
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 Beyler4e97c532014-01-07 10:07:18 -0800107 /**
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 Scott4f596682014-05-01 05:52:04 -0700112 virtual bool RunPass(const Pass* pass, bool time_split = false) = 0;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800113
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700114 protected:
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800115 /**
116 * @brief Apply a patch: perform start/work/end functions.
117 */
James C Scott4f596682014-05-01 05:52:04 -0700118 virtual void ApplyPass(PassDataHolder* data, const Pass* pass) {
119 pass->Start(data);
120 DispatchPass(pass);
121 pass->End(data);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800122 }
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800123
James C Scott4f596682014-05-01 05:52:04 -0700124 /**
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 Beyler4e97c532014-01-07 10:07:18 -0800131
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800132 /** @brief List of passes: provides the order to execute the passes.
133 * Passes are owned by pass_manager_. */
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000134 std::vector<const Pass*> pass_list_;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800135
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800136 const PassManager* const pass_manager_;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800137};
138
139} // namespace art
140#endif // ART_COMPILER_DEX_PASS_DRIVER_H_