Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 1 | /* |
| 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 Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 19 | #include "base/logging.h" |
| 20 | #include "base/macros.h" |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 21 | #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 | |
| 28 | namespace art { |
| 29 | |
Jean Christophe Beyler | 775c472 | 2014-01-16 08:51:48 -0800 | [diff] [blame] | 30 | namespace { // anonymous namespace |
| 31 | |
| 32 | /** |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 33 | * @brief Helper function to create a single instance of a given Pass and can be shared across |
| 34 | * the threads. |
Jean Christophe Beyler | 775c472 | 2014-01-16 08:51:48 -0800 | [diff] [blame] | 35 | */ |
| 36 | template <typename PassType> |
| 37 | const Pass* GetPassInstance() { |
| 38 | static const PassType pass; |
| 39 | return &pass; |
| 40 | } |
| 41 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 42 | void 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 | |
| 52 | template <typename Iterator> |
| 53 | inline 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 Beyler | 775c472 | 2014-01-16 08:51:48 -0800 | [diff] [blame] | 58 | } // anonymous namespace |
| 59 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 60 | PassDriver::PassDriver(CompilationUnit* cu, bool create_default_passes) |
| 61 | : cu_(cu), dump_cfg_folder_("/sdcard/") { |
| 62 | DCHECK(cu != nullptr); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 63 | |
| 64 | // If need be, create the default passes. |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 65 | if (create_default_passes) { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 66 | CreatePasses(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | PassDriver::~PassDriver() { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 73 | void PassDriver::InsertPass(const Pass* new_pass) { |
| 74 | DCHECK(new_pass != nullptr); |
| 75 | DCHECK(new_pass->GetName() != nullptr && new_pass->GetName()[0] != 0); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 76 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 77 | // 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 Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 80 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 81 | // Now add to the list. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 82 | pass_list_.push_back(new_pass); |
| 83 | } |
| 84 | |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame^] | 85 | /* |
| 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 | */ |
| 92 | static const Pass* const gPasses[] = { |
| 93 | GetPassInstance<CacheFieldLoweringInfo>(), |
| 94 | GetPassInstance<CacheMethodLoweringInfo>(), |
| 95 | GetPassInstance<CodeLayout>(), |
| 96 | GetPassInstance<SSATransformation>(), |
| 97 | GetPassInstance<ConstantPropagation>(), |
| 98 | GetPassInstance<InitRegLocations>(), |
| 99 | GetPassInstance<MethodUseCount>(), |
| 100 | GetPassInstance<NullCheckEliminationAndTypeInferenceInit>(), |
| 101 | GetPassInstance<NullCheckEliminationAndTypeInference>(), |
| 102 | GetPassInstance<BBCombine>(), |
| 103 | GetPassInstance<BBOptimizations>(), |
| 104 | }; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 105 | |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame^] | 106 | // The default pass list is used by CreatePasses to initialize pass_list_. |
| 107 | static std::vector<const Pass*> gDefaultPassList(gPasses, gPasses + arraysize(gPasses)); |
| 108 | |
| 109 | void PassDriver::CreateDefaultPassList(const std::string& disable_passes) { |
| 110 | // Insert each pass from gPasses into gDefaultPassList. |
| 111 | gDefaultPassList.clear(); |
| 112 | gDefaultPassList.reserve(arraysize(gPasses)); |
| 113 | for (const Pass* pass : gPasses) { |
| 114 | // Check if we should disable this pass. |
| 115 | if (disable_passes.find(pass->GetName()) != std::string::npos) { |
| 116 | LOG(INFO) << "Skipping " << pass->GetName(); |
| 117 | } else { |
| 118 | gDefaultPassList.push_back(pass); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void PassDriver::CreatePasses() { |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 124 | // Insert each pass into the list via the InsertPass method. |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame^] | 125 | pass_list_.reserve(gDefaultPassList.size()); |
| 126 | for (const Pass* pass : gDefaultPassList) { |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 127 | InsertPass(pass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Jean Christophe Beyler | 775c472 | 2014-01-16 08:51:48 -0800 | [diff] [blame] | 131 | void PassDriver::HandlePassFlag(CompilationUnit* c_unit, const Pass* pass) { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 132 | // Unused parameters for the moment. |
| 133 | UNUSED(c_unit); |
| 134 | UNUSED(pass); |
| 135 | } |
| 136 | |
Jean Christophe Beyler | 775c472 | 2014-01-16 08:51:48 -0800 | [diff] [blame] | 137 | void PassDriver::DispatchPass(CompilationUnit* c_unit, const Pass* curPass) { |
Brian Carlstrom | b3558e1 | 2014-02-21 00:05:43 -0800 | [diff] [blame] | 138 | VLOG(compiler) << "Dispatching " << curPass->GetName(); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 139 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 140 | DataFlowAnalysisMode mode = curPass->GetTraversal(); |
| 141 | |
| 142 | switch (mode) { |
| 143 | case kPreOrderDFSTraversal: |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 144 | DoWalkBasicBlocks<PreOrderDfsIterator>(c_unit, curPass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 145 | break; |
| 146 | case kRepeatingPreOrderDFSTraversal: |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 147 | DoWalkBasicBlocks<RepeatingPreOrderDfsIterator>(c_unit, curPass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 148 | break; |
| 149 | case kRepeatingPostOrderDFSTraversal: |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 150 | DoWalkBasicBlocks<RepeatingPostOrderDfsIterator>(c_unit, curPass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 151 | break; |
| 152 | case kReversePostOrderDFSTraversal: |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 153 | DoWalkBasicBlocks<ReversePostOrderDfsIterator>(c_unit, curPass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 154 | break; |
| 155 | case kRepeatingReversePostOrderDFSTraversal: |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 156 | DoWalkBasicBlocks<RepeatingReversePostOrderDfsIterator>(c_unit, curPass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 157 | break; |
| 158 | case kPostOrderDOMTraversal: |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 159 | DoWalkBasicBlocks<PostOrderDOMIterator>(c_unit, curPass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 160 | break; |
| 161 | case kAllNodes: |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 162 | DoWalkBasicBlocks<AllNodesIterator>(c_unit, curPass); |
| 163 | break; |
| 164 | case kNoNodes: |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 165 | break; |
| 166 | default: |
Brian Carlstrom | b3558e1 | 2014-02-21 00:05:43 -0800 | [diff] [blame] | 167 | LOG(FATAL) << "Iterator mode not handled in dispatcher: " << mode; |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 168 | break; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Jean Christophe Beyler | 775c472 | 2014-01-16 08:51:48 -0800 | [diff] [blame] | 172 | void PassDriver::ApplyPass(CompilationUnit* c_unit, const Pass* curPass) { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 173 | curPass->Start(c_unit); |
| 174 | DispatchPass(c_unit, curPass); |
| 175 | curPass->End(c_unit); |
| 176 | } |
| 177 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 178 | bool PassDriver::RunPass(CompilationUnit* c_unit, const Pass* pass, bool time_split) { |
| 179 | // Paranoid: c_unit and pass cannot be nullptr, and the pass should have a name. |
| 180 | DCHECK(c_unit != nullptr); |
| 181 | DCHECK(pass != nullptr); |
| 182 | DCHECK(pass->GetName() != nullptr && pass->GetName()[0] != 0); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 183 | |
| 184 | // Do we perform a time split |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 185 | if (time_split) { |
| 186 | c_unit->NewTimingSplit(pass->GetName()); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Check the pass gate first. |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 190 | bool should_apply_pass = pass->Gate(c_unit); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 191 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 192 | if (should_apply_pass) { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 193 | // Applying the pass: first start, doWork, and end calls. |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 194 | ApplyPass(c_unit, pass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 195 | |
| 196 | // Clean up if need be. |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 197 | HandlePassFlag(c_unit, pass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 198 | |
| 199 | // Do we want to log it? |
| 200 | if ((c_unit->enable_debug& (1 << kDebugDumpCFG)) != 0) { |
| 201 | // Do we have a pass folder? |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 202 | const char* passFolder = pass->GetDumpCFGFolder(); |
| 203 | DCHECK(passFolder != nullptr); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 204 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 205 | if (passFolder[0] != 0) { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 206 | // Create directory prefix. |
| 207 | std::string prefix = GetDumpCFGFolder(); |
| 208 | prefix += passFolder; |
| 209 | prefix += "/"; |
| 210 | |
| 211 | c_unit->mir_graph->DumpCFG(prefix.c_str(), false); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // If the pass gate passed, we can declare success. |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 217 | return should_apply_pass; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 220 | bool PassDriver::RunPass(CompilationUnit* c_unit, const char* pass_name) { |
| 221 | // Paranoid: c_unit cannot be nullptr and we need a pass name. |
| 222 | DCHECK(c_unit != nullptr); |
| 223 | DCHECK(pass_name != nullptr && pass_name[0] != 0); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 224 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 225 | const Pass* cur_pass = GetPass(pass_name); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 226 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 227 | if (cur_pass != nullptr) { |
| 228 | return RunPass(c_unit, cur_pass); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Return false, we did not find the pass. |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | void PassDriver::Launch() { |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 236 | for (const Pass* cur_pass : pass_list_) { |
| 237 | RunPass(cu_, cur_pass, true); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame^] | 241 | void PassDriver::PrintPassNames() { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 242 | LOG(INFO) << "Loop Passes are:"; |
| 243 | |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame^] | 244 | for (const Pass* cur_pass : gPasses) { |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 245 | LOG(INFO) << "\t-" << cur_pass->GetName(); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 249 | const Pass* PassDriver::GetPass(const char* name) const { |
| 250 | for (const Pass* cur_pass : pass_list_) { |
| 251 | if (strcmp(name, cur_pass->GetName()) == 0) { |
| 252 | return cur_pass; |
| 253 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 254 | } |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 255 | return nullptr; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | } // namespace art |