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 | |
| 19 | #include "bb_optimizations.h" |
| 20 | #include "compiler_internals.h" |
| 21 | #include "dataflow_iterator.h" |
| 22 | #include "dataflow_iterator-inl.h" |
| 23 | #include "pass.h" |
| 24 | #include "pass_driver.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | PassDriver::PassDriver(CompilationUnit* const cu, bool create_default_passes) : cu_(cu) { |
| 29 | dump_cfg_folder_ = "/sdcard/"; |
| 30 | |
| 31 | // If need be, create the default passes. |
| 32 | if (create_default_passes == true) { |
| 33 | CreatePasses(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | PassDriver::~PassDriver() { |
| 38 | // Clear the map: done to remove any chance of having a pointer after freeing below |
| 39 | pass_map_.clear(); |
| 40 | } |
| 41 | |
| 42 | void PassDriver::InsertPass(Pass* new_pass, bool warn_override) { |
| 43 | assert(new_pass != 0); |
| 44 | |
| 45 | // Get name here to not do it all over the method. |
| 46 | const std::string& name = new_pass->GetName(); |
| 47 | |
| 48 | // Do we want to warn the user about squashing a pass? |
| 49 | if (warn_override == false) { |
| 50 | SafeMap<std::string, Pass* >::iterator it = pass_map_.find(name); |
| 51 | |
| 52 | if (it != pass_map_.end()) { |
| 53 | LOG(INFO) << "Pass name " << name << " already used, overwriting pass"; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Now add to map and list. |
| 58 | pass_map_.Put(name, new_pass); |
| 59 | pass_list_.push_back(new_pass); |
| 60 | } |
| 61 | |
| 62 | void PassDriver::CreatePasses() { |
| 63 | /* |
| 64 | * Create the pass list: |
| 65 | * - These passes are immutable and are shared across the threads: |
| 66 | * - This is achieved via: |
| 67 | * - The UniquePtr used here. |
| 68 | * - DISALLOW_COPY_AND_ASSIGN in the base Pass class. |
| 69 | * |
| 70 | * Advantage is that there will be no race conditions here. |
| 71 | * Disadvantage is the passes can't change their internal states depending on CompilationUnit: |
| 72 | * - This is not yet an issue: no current pass would require it. |
| 73 | */ |
| 74 | static UniquePtr<Pass> *passes[] = { |
| 75 | new UniquePtr<Pass>(new CodeLayout()), |
| 76 | new UniquePtr<Pass>(new SSATransformation()), |
| 77 | new UniquePtr<Pass>(new ConstantPropagation()), |
| 78 | new UniquePtr<Pass>(new InitRegLocations()), |
| 79 | new UniquePtr<Pass>(new MethodUseCount()), |
| 80 | new UniquePtr<Pass>(new NullCheckEliminationAndTypeInferenceInit()), |
| 81 | new UniquePtr<Pass>(new NullCheckEliminationAndTypeInference()), |
| 82 | new UniquePtr<Pass>(new BBCombine()), |
| 83 | new UniquePtr<Pass>(new BBOptimizations()), |
| 84 | }; |
| 85 | |
| 86 | // Get number of elements in the array. |
| 87 | unsigned int nbr = (sizeof(passes) / sizeof(passes[0])); |
| 88 | |
| 89 | // Insert each pass into the map and into the list via the InsertPass method: |
| 90 | // - Map is used for the lookup |
| 91 | // - List is used for the pass walk |
| 92 | for (unsigned int i = 0; i < nbr; i++) { |
| 93 | InsertPass(passes[i]->get()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void PassDriver::HandlePassFlag(CompilationUnit* c_unit, Pass* pass) { |
| 98 | // Unused parameters for the moment. |
| 99 | UNUSED(c_unit); |
| 100 | UNUSED(pass); |
| 101 | } |
| 102 | |
| 103 | void PassDriver::DispatchPass(CompilationUnit* c_unit, Pass* curPass) { |
| 104 | DataflowIterator* iterator = 0; |
| 105 | |
| 106 | LOG(DEBUG) << "Dispatching " << curPass->GetName(); |
| 107 | |
| 108 | MIRGraph* mir_graph = c_unit->mir_graph.get(); |
| 109 | ArenaAllocator *arena = &(c_unit->arena); |
| 110 | |
| 111 | // Let us start by getting the right iterator. |
| 112 | DataFlowAnalysisMode mode = curPass->GetTraversal(); |
| 113 | |
| 114 | switch (mode) { |
| 115 | case kPreOrderDFSTraversal: |
| 116 | iterator = new (arena) PreOrderDfsIterator(mir_graph); |
| 117 | break; |
| 118 | case kRepeatingPreOrderDFSTraversal: |
| 119 | iterator = new (arena) RepeatingPreOrderDfsIterator(mir_graph); |
| 120 | break; |
| 121 | case kRepeatingPostOrderDFSTraversal: |
| 122 | iterator = new (arena) RepeatingPostOrderDfsIterator(mir_graph); |
| 123 | break; |
| 124 | case kReversePostOrderDFSTraversal: |
| 125 | iterator = new (arena) ReversePostOrderDfsIterator(mir_graph); |
| 126 | break; |
| 127 | case kRepeatingReversePostOrderDFSTraversal: |
| 128 | iterator = new (arena) RepeatingReversePostOrderDfsIterator(mir_graph); |
| 129 | break; |
| 130 | case kPostOrderDOMTraversal: |
| 131 | iterator = new (arena) PostOrderDOMIterator(mir_graph); |
| 132 | break; |
| 133 | case kAllNodes: |
| 134 | iterator = new (arena) AllNodesIterator(mir_graph); |
| 135 | break; |
| 136 | default: |
| 137 | LOG(DEBUG) << "Iterator mode not handled in dispatcher: " << mode; |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | // Paranoid: Check the iterator before walking the BasicBlocks. |
| 142 | assert(iterator != 0); |
| 143 | |
| 144 | bool change = false; |
| 145 | for (BasicBlock *bb = iterator->Next(change); bb != 0; bb = iterator->Next(change)) { |
| 146 | change = curPass->WalkBasicBlocks(c_unit, bb); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void PassDriver::ApplyPass(CompilationUnit* c_unit, Pass* curPass) { |
| 151 | curPass->Start(c_unit); |
| 152 | DispatchPass(c_unit, curPass); |
| 153 | curPass->End(c_unit); |
| 154 | } |
| 155 | |
| 156 | bool PassDriver::RunPass(CompilationUnit* c_unit, Pass* curPass, bool time_split) { |
| 157 | // Paranoid: c_unit or curPass cannot be 0, and the pass should have a name. |
| 158 | if (c_unit == 0 || curPass == 0 || (strcmp(curPass->GetName(), "") == 0)) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // Do we perform a time split |
| 163 | if (time_split == true) { |
| 164 | std::string name = "MIROpt:"; |
| 165 | name += curPass->GetName(); |
| 166 | c_unit->NewTimingSplit(name.c_str()); |
| 167 | } |
| 168 | |
| 169 | // Check the pass gate first. |
| 170 | bool shouldApplyPass = curPass->Gate(c_unit); |
| 171 | |
| 172 | if (shouldApplyPass == true) { |
| 173 | // Applying the pass: first start, doWork, and end calls. |
| 174 | ApplyPass(c_unit, curPass); |
| 175 | |
| 176 | // Clean up if need be. |
| 177 | HandlePassFlag(c_unit, curPass); |
| 178 | |
| 179 | // Do we want to log it? |
| 180 | if ((c_unit->enable_debug& (1 << kDebugDumpCFG)) != 0) { |
| 181 | // Do we have a pass folder? |
| 182 | const std::string& passFolder = curPass->GetDumpCFGFolder(); |
| 183 | |
| 184 | if (passFolder != "") { |
| 185 | // Create directory prefix. |
| 186 | std::string prefix = GetDumpCFGFolder(); |
| 187 | prefix += passFolder; |
| 188 | prefix += "/"; |
| 189 | |
| 190 | c_unit->mir_graph->DumpCFG(prefix.c_str(), false); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // If the pass gate passed, we can declare success. |
| 196 | return shouldApplyPass; |
| 197 | } |
| 198 | |
| 199 | bool PassDriver::RunPass(CompilationUnit* c_unit, const std::string& pass_name) { |
| 200 | // Paranoid: c_unit cannot be 0 and we need a pass name. |
| 201 | if (c_unit == 0 || pass_name == "") { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | Pass* curPass = GetPass(pass_name); |
| 206 | |
| 207 | if (curPass != 0) { |
| 208 | return RunPass(c_unit, curPass); |
| 209 | } |
| 210 | |
| 211 | // Return false, we did not find the pass. |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | void PassDriver::Launch() { |
| 216 | for (std::list<Pass* >::iterator it = pass_list_.begin(); it != pass_list_.end(); it++) { |
| 217 | Pass* curPass = *it; |
| 218 | RunPass(cu_, curPass, true); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void PassDriver::PrintPassNames() const { |
| 223 | LOG(INFO) << "Loop Passes are:"; |
| 224 | |
| 225 | for (std::list<Pass* >::const_iterator it = pass_list_.begin(); it != pass_list_.end(); it++) { |
| 226 | const Pass* curPass = *it; |
| 227 | LOG(INFO) << "\t-" << curPass->GetName(); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | Pass* PassDriver::GetPass(const std::string& name) const { |
| 232 | SafeMap<std::string, Pass*>::const_iterator it = pass_map_.find(name); |
| 233 | |
| 234 | if (it != pass_map_.end()) { |
| 235 | return it->second; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | } // namespace art |