blob: 4f8739a66698b59589575ee0d8eb09982c2e0471 [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#include <dlfcn.h>
18
Vladimir Marko75ba13f2014-01-28 12:15:24 +000019#include "base/logging.h"
20#include "base/macros.h"
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080021#include "bb_optimizations.h"
22#include "compiler_internals.h"
23#include "dataflow_iterator.h"
24#include "dataflow_iterator-inl.h"
25#include "pass.h"
26#include "pass_driver.h"
27
28namespace art {
29
Jean Christophe Beyler775c4722014-01-16 08:51:48 -080030namespace { // anonymous namespace
31
32/**
Vladimir Marko75ba13f2014-01-28 12:15:24 +000033 * @brief Helper function to create a single instance of a given Pass and can be shared across
34 * the threads.
Jean Christophe Beyler775c4722014-01-16 08:51:48 -080035 */
36template <typename PassType>
37const Pass* GetPassInstance() {
38 static const PassType pass;
39 return &pass;
40}
41
Vladimir Marko75ba13f2014-01-28 12:15:24 +000042void DoWalkBasicBlocks(CompilationUnit* c_unit, const Pass* pass, DataflowIterator* iterator) {
43 // Paranoid: Check the iterator before walking the BasicBlocks.
44 DCHECK(iterator != nullptr);
45
46 bool change = false;
47 for (BasicBlock *bb = iterator->Next(change); bb != 0; bb = iterator->Next(change)) {
48 change = pass->WalkBasicBlocks(c_unit, bb);
49 }
50}
51
52template <typename Iterator>
53inline void DoWalkBasicBlocks(CompilationUnit* c_unit, const Pass* pass) {
54 Iterator iterator(c_unit->mir_graph.get());
55 DoWalkBasicBlocks(c_unit, pass, &iterator);
56}
57
Jean Christophe Beyler775c4722014-01-16 08:51:48 -080058} // anonymous namespace
59
Vladimir Marko75ba13f2014-01-28 12:15:24 +000060PassDriver::PassDriver(CompilationUnit* cu, bool create_default_passes)
61 : cu_(cu), dump_cfg_folder_("/sdcard/") {
62 DCHECK(cu != nullptr);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080063
64 // If need be, create the default passes.
Vladimir Marko75ba13f2014-01-28 12:15:24 +000065 if (create_default_passes) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080066 CreatePasses();
67 }
68}
69
70PassDriver::~PassDriver() {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080071}
72
Vladimir Marko75ba13f2014-01-28 12:15:24 +000073void PassDriver::InsertPass(const Pass* new_pass) {
74 DCHECK(new_pass != nullptr);
75 DCHECK(new_pass->GetName() != nullptr && new_pass->GetName()[0] != 0);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080076
Vladimir Marko75ba13f2014-01-28 12:15:24 +000077 // It is an error to override an existing pass.
78 DCHECK(GetPass(new_pass->GetName()) == nullptr)
79 << "Pass name " << new_pass->GetName() << " already used.";
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080080
Vladimir Marko75ba13f2014-01-28 12:15:24 +000081 // Now add to the list.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080082 pass_list_.push_back(new_pass);
83}
84
85void PassDriver::CreatePasses() {
86 /*
Vladimir Marko75ba13f2014-01-28 12:15:24 +000087 * Create the pass list. These passes are immutable and are shared across the threads.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080088 *
89 * Advantage is that there will be no race conditions here.
90 * Disadvantage is the passes can't change their internal states depending on CompilationUnit:
91 * - This is not yet an issue: no current pass would require it.
92 */
Vladimir Marko75ba13f2014-01-28 12:15:24 +000093 static const Pass* const passes[] = {
Jean Christophe Beyler775c4722014-01-16 08:51:48 -080094 GetPassInstance<CodeLayout>(),
95 GetPassInstance<SSATransformation>(),
96 GetPassInstance<ConstantPropagation>(),
97 GetPassInstance<InitRegLocations>(),
98 GetPassInstance<MethodUseCount>(),
99 GetPassInstance<NullCheckEliminationAndTypeInferenceInit>(),
100 GetPassInstance<NullCheckEliminationAndTypeInference>(),
101 GetPassInstance<BBCombine>(),
102 GetPassInstance<BBOptimizations>(),
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800103 };
104
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000105 // Insert each pass into the list via the InsertPass method.
106 pass_list_.reserve(arraysize(passes));
107 for (const Pass* pass : passes) {
108 InsertPass(pass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800109 }
110}
111
Jean Christophe Beyler775c4722014-01-16 08:51:48 -0800112void PassDriver::HandlePassFlag(CompilationUnit* c_unit, const Pass* pass) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800113 // Unused parameters for the moment.
114 UNUSED(c_unit);
115 UNUSED(pass);
116}
117
Jean Christophe Beyler775c4722014-01-16 08:51:48 -0800118void PassDriver::DispatchPass(CompilationUnit* c_unit, const Pass* curPass) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800119 LOG(DEBUG) << "Dispatching " << curPass->GetName();
120
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800121 DataFlowAnalysisMode mode = curPass->GetTraversal();
122
123 switch (mode) {
124 case kPreOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000125 DoWalkBasicBlocks<PreOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800126 break;
127 case kRepeatingPreOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000128 DoWalkBasicBlocks<RepeatingPreOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800129 break;
130 case kRepeatingPostOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000131 DoWalkBasicBlocks<RepeatingPostOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800132 break;
133 case kReversePostOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000134 DoWalkBasicBlocks<ReversePostOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800135 break;
136 case kRepeatingReversePostOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000137 DoWalkBasicBlocks<RepeatingReversePostOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800138 break;
139 case kPostOrderDOMTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000140 DoWalkBasicBlocks<PostOrderDOMIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800141 break;
142 case kAllNodes:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000143 DoWalkBasicBlocks<AllNodesIterator>(c_unit, curPass);
144 break;
145 case kNoNodes:
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800146 break;
147 default:
148 LOG(DEBUG) << "Iterator mode not handled in dispatcher: " << mode;
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000149 break;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800150 }
151}
152
Jean Christophe Beyler775c4722014-01-16 08:51:48 -0800153void PassDriver::ApplyPass(CompilationUnit* c_unit, const Pass* curPass) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800154 curPass->Start(c_unit);
155 DispatchPass(c_unit, curPass);
156 curPass->End(c_unit);
157}
158
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000159bool PassDriver::RunPass(CompilationUnit* c_unit, const Pass* pass, bool time_split) {
160 // Paranoid: c_unit and pass cannot be nullptr, and the pass should have a name.
161 DCHECK(c_unit != nullptr);
162 DCHECK(pass != nullptr);
163 DCHECK(pass->GetName() != nullptr && pass->GetName()[0] != 0);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800164
165 // Do we perform a time split
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000166 if (time_split) {
167 c_unit->NewTimingSplit(pass->GetName());
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800168 }
169
170 // Check the pass gate first.
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000171 bool should_apply_pass = pass->Gate(c_unit);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800172
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000173 if (should_apply_pass) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800174 // Applying the pass: first start, doWork, and end calls.
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000175 ApplyPass(c_unit, pass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800176
177 // Clean up if need be.
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000178 HandlePassFlag(c_unit, pass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800179
180 // Do we want to log it?
181 if ((c_unit->enable_debug& (1 << kDebugDumpCFG)) != 0) {
182 // Do we have a pass folder?
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000183 const char* passFolder = pass->GetDumpCFGFolder();
184 DCHECK(passFolder != nullptr);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800185
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000186 if (passFolder[0] != 0) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800187 // Create directory prefix.
188 std::string prefix = GetDumpCFGFolder();
189 prefix += passFolder;
190 prefix += "/";
191
192 c_unit->mir_graph->DumpCFG(prefix.c_str(), false);
193 }
194 }
195 }
196
197 // If the pass gate passed, we can declare success.
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000198 return should_apply_pass;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800199}
200
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000201bool PassDriver::RunPass(CompilationUnit* c_unit, const char* pass_name) {
202 // Paranoid: c_unit cannot be nullptr and we need a pass name.
203 DCHECK(c_unit != nullptr);
204 DCHECK(pass_name != nullptr && pass_name[0] != 0);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800205
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000206 const Pass* cur_pass = GetPass(pass_name);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800207
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000208 if (cur_pass != nullptr) {
209 return RunPass(c_unit, cur_pass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800210 }
211
212 // Return false, we did not find the pass.
213 return false;
214}
215
216void PassDriver::Launch() {
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000217 for (const Pass* cur_pass : pass_list_) {
218 RunPass(cu_, cur_pass, true);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800219 }
220}
221
222void PassDriver::PrintPassNames() const {
223 LOG(INFO) << "Loop Passes are:";
224
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000225 for (const Pass* cur_pass : pass_list_) {
226 LOG(INFO) << "\t-" << cur_pass->GetName();
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800227 }
228}
229
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000230const Pass* PassDriver::GetPass(const char* name) const {
231 for (const Pass* cur_pass : pass_list_) {
232 if (strcmp(name, cur_pass->GetName()) == 0) {
233 return cur_pass;
234 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800235 }
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000236 return nullptr;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800237}
238
239} // namespace art