blob: ca936cd41ad340e9eccfb2ac2d5f25a77bd2d884 [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
Chao-ying Fucd8ce662014-03-11 14:57:19 -070085/*
86 * Create the pass list. These passes are immutable and are shared across the threads.
87 *
88 * Advantage is that there will be no race conditions here.
89 * Disadvantage is the passes can't change their internal states depending on CompilationUnit:
90 * - This is not yet an issue: no current pass would require it.
91 */
92static const Pass* const gPasses[] = {
93 GetPassInstance<CacheFieldLoweringInfo>(),
94 GetPassInstance<CacheMethodLoweringInfo>(),
Vladimir Marko9820b7c2014-01-02 16:40:37 +000095 GetPassInstance<CallInlining>(),
Chao-ying Fucd8ce662014-03-11 14:57:19 -070096 GetPassInstance<CodeLayout>(),
97 GetPassInstance<SSATransformation>(),
98 GetPassInstance<ConstantPropagation>(),
99 GetPassInstance<InitRegLocations>(),
100 GetPassInstance<MethodUseCount>(),
Chao-ying Fucd8ce662014-03-11 14:57:19 -0700101 GetPassInstance<NullCheckEliminationAndTypeInference>(),
Vladimir Markobfea9c22014-01-17 17:49:33 +0000102 GetPassInstance<ClassInitCheckElimination>(),
Chao-ying Fucd8ce662014-03-11 14:57:19 -0700103 GetPassInstance<BBCombine>(),
104 GetPassInstance<BBOptimizations>(),
105};
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800106
Chao-ying Fucd8ce662014-03-11 14:57:19 -0700107// The default pass list is used by CreatePasses to initialize pass_list_.
108static std::vector<const Pass*> gDefaultPassList(gPasses, gPasses + arraysize(gPasses));
109
110void PassDriver::CreateDefaultPassList(const std::string& disable_passes) {
111 // Insert each pass from gPasses into gDefaultPassList.
112 gDefaultPassList.clear();
113 gDefaultPassList.reserve(arraysize(gPasses));
114 for (const Pass* pass : gPasses) {
115 // Check if we should disable this pass.
116 if (disable_passes.find(pass->GetName()) != std::string::npos) {
117 LOG(INFO) << "Skipping " << pass->GetName();
118 } else {
119 gDefaultPassList.push_back(pass);
120 }
121 }
122}
123
124void PassDriver::CreatePasses() {
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000125 // Insert each pass into the list via the InsertPass method.
Chao-ying Fucd8ce662014-03-11 14:57:19 -0700126 pass_list_.reserve(gDefaultPassList.size());
127 for (const Pass* pass : gDefaultPassList) {
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000128 InsertPass(pass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800129 }
130}
131
Jean Christophe Beyler775c4722014-01-16 08:51:48 -0800132void PassDriver::HandlePassFlag(CompilationUnit* c_unit, const Pass* pass) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800133 // Unused parameters for the moment.
134 UNUSED(c_unit);
135 UNUSED(pass);
136}
137
Jean Christophe Beyler775c4722014-01-16 08:51:48 -0800138void PassDriver::DispatchPass(CompilationUnit* c_unit, const Pass* curPass) {
Brian Carlstromb3558e12014-02-21 00:05:43 -0800139 VLOG(compiler) << "Dispatching " << curPass->GetName();
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800140
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800141 DataFlowAnalysisMode mode = curPass->GetTraversal();
142
143 switch (mode) {
144 case kPreOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000145 DoWalkBasicBlocks<PreOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800146 break;
147 case kRepeatingPreOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000148 DoWalkBasicBlocks<RepeatingPreOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800149 break;
150 case kRepeatingPostOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000151 DoWalkBasicBlocks<RepeatingPostOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800152 break;
153 case kReversePostOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000154 DoWalkBasicBlocks<ReversePostOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800155 break;
156 case kRepeatingReversePostOrderDFSTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000157 DoWalkBasicBlocks<RepeatingReversePostOrderDfsIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800158 break;
159 case kPostOrderDOMTraversal:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000160 DoWalkBasicBlocks<PostOrderDOMIterator>(c_unit, curPass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800161 break;
162 case kAllNodes:
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000163 DoWalkBasicBlocks<AllNodesIterator>(c_unit, curPass);
164 break;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -0700165 case kTopologicalSortTraversal:
166 DoWalkBasicBlocks<TopologicalSortIterator>(c_unit, curPass);
167 break;
168 case kRepeatingTopologicalSortTraversal:
169 DoWalkBasicBlocks<RepeatingTopologicalSortIterator>(c_unit, curPass);
170 break;
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000171 case kNoNodes:
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800172 break;
173 default:
Brian Carlstromb3558e12014-02-21 00:05:43 -0800174 LOG(FATAL) << "Iterator mode not handled in dispatcher: " << mode;
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000175 break;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800176 }
177}
178
Jean Christophe Beyler775c4722014-01-16 08:51:48 -0800179void PassDriver::ApplyPass(CompilationUnit* c_unit, const Pass* curPass) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800180 curPass->Start(c_unit);
181 DispatchPass(c_unit, curPass);
182 curPass->End(c_unit);
183}
184
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000185bool PassDriver::RunPass(CompilationUnit* c_unit, const Pass* pass, bool time_split) {
186 // Paranoid: c_unit and pass cannot be nullptr, and the pass should have a name.
187 DCHECK(c_unit != nullptr);
188 DCHECK(pass != nullptr);
189 DCHECK(pass->GetName() != nullptr && pass->GetName()[0] != 0);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800190
191 // Do we perform a time split
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000192 if (time_split) {
193 c_unit->NewTimingSplit(pass->GetName());
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800194 }
195
196 // Check the pass gate first.
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000197 bool should_apply_pass = pass->Gate(c_unit);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800198
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000199 if (should_apply_pass) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800200 // Applying the pass: first start, doWork, and end calls.
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000201 ApplyPass(c_unit, pass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800202
203 // Clean up if need be.
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000204 HandlePassFlag(c_unit, pass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800205
206 // Do we want to log it?
207 if ((c_unit->enable_debug& (1 << kDebugDumpCFG)) != 0) {
208 // Do we have a pass folder?
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000209 const char* passFolder = pass->GetDumpCFGFolder();
210 DCHECK(passFolder != nullptr);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800211
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000212 if (passFolder[0] != 0) {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800213 // Create directory prefix.
214 std::string prefix = GetDumpCFGFolder();
215 prefix += passFolder;
216 prefix += "/";
217
218 c_unit->mir_graph->DumpCFG(prefix.c_str(), false);
219 }
220 }
221 }
222
223 // If the pass gate passed, we can declare success.
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000224 return should_apply_pass;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800225}
226
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000227bool PassDriver::RunPass(CompilationUnit* c_unit, const char* pass_name) {
228 // Paranoid: c_unit cannot be nullptr and we need a pass name.
229 DCHECK(c_unit != nullptr);
230 DCHECK(pass_name != nullptr && pass_name[0] != 0);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800231
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000232 const Pass* cur_pass = GetPass(pass_name);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800233
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000234 if (cur_pass != nullptr) {
235 return RunPass(c_unit, cur_pass);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800236 }
237
238 // Return false, we did not find the pass.
239 return false;
240}
241
242void PassDriver::Launch() {
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000243 for (const Pass* cur_pass : pass_list_) {
244 RunPass(cu_, cur_pass, true);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800245 }
246}
247
Chao-ying Fucd8ce662014-03-11 14:57:19 -0700248void PassDriver::PrintPassNames() {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800249 LOG(INFO) << "Loop Passes are:";
250
Chao-ying Fucd8ce662014-03-11 14:57:19 -0700251 for (const Pass* cur_pass : gPasses) {
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000252 LOG(INFO) << "\t-" << cur_pass->GetName();
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800253 }
254}
255
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000256const Pass* PassDriver::GetPass(const char* name) const {
257 for (const Pass* cur_pass : pass_list_) {
258 if (strcmp(name, cur_pass->GetName()) == 0) {
259 return cur_pass;
260 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800261 }
Vladimir Marko75ba13f2014-01-28 12:15:24 +0000262 return nullptr;
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800263}
264
265} // namespace art