James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [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 "base/macros.h" |
| 18 | #include "bb_optimizations.h" |
| 19 | #include "compiler_internals.h" |
| 20 | #include "dataflow_iterator.h" |
| 21 | #include "dataflow_iterator-inl.h" |
| 22 | #include "pass_driver_me.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | namespace { // anonymous namespace |
| 27 | |
| 28 | void DoWalkBasicBlocks(PassMEDataHolder* data, const PassME* pass, DataflowIterator* iterator) { |
| 29 | // Paranoid: Check the iterator before walking the BasicBlocks. |
| 30 | DCHECK(iterator != nullptr); |
| 31 | bool change = false; |
| 32 | for (BasicBlock *bb = iterator->Next(change); bb != 0; bb = iterator->Next(change)) { |
| 33 | data->bb = bb; |
| 34 | change = pass->Worker(data); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | template <typename Iterator> |
| 39 | inline void DoWalkBasicBlocks(PassMEDataHolder* data, const PassME* pass) { |
| 40 | DCHECK(data != nullptr); |
| 41 | CompilationUnit* c_unit = data->c_unit; |
| 42 | DCHECK(c_unit != nullptr); |
| 43 | Iterator iterator(c_unit->mir_graph.get()); |
| 44 | DoWalkBasicBlocks(data, pass, &iterator); |
| 45 | } |
| 46 | } // anonymous namespace |
| 47 | |
| 48 | /* |
| 49 | * Create the pass list. These passes are immutable and are shared across the threads. |
| 50 | * |
| 51 | * Advantage is that there will be no race conditions here. |
| 52 | * Disadvantage is the passes can't change their internal states depending on CompilationUnit: |
| 53 | * - This is not yet an issue: no current pass would require it. |
| 54 | */ |
| 55 | // The initial list of passes to be used by the PassDriveME. |
| 56 | template<> |
| 57 | const Pass* const PassDriver<PassDriverME>::g_passes[] = { |
| 58 | GetPassInstance<CacheFieldLoweringInfo>(), |
| 59 | GetPassInstance<CacheMethodLoweringInfo>(), |
| 60 | GetPassInstance<CallInlining>(), |
| 61 | GetPassInstance<CodeLayout>(), |
| 62 | GetPassInstance<SSATransformation>(), |
| 63 | GetPassInstance<ConstantPropagation>(), |
| 64 | GetPassInstance<InitRegLocations>(), |
| 65 | GetPassInstance<MethodUseCount>(), |
| 66 | GetPassInstance<NullCheckEliminationAndTypeInference>(), |
| 67 | GetPassInstance<ClassInitCheckElimination>(), |
| 68 | GetPassInstance<BBCombine>(), |
| 69 | GetPassInstance<BBOptimizations>(), |
| 70 | }; |
| 71 | |
| 72 | // The number of the passes in the initial list of Passes (g_passes). |
| 73 | template<> |
| 74 | uint16_t const PassDriver<PassDriverME>::g_passes_size = arraysize(PassDriver<PassDriverME>::g_passes); |
| 75 | |
| 76 | // The default pass list is used by the PassDriverME instance of PassDriver to initialize pass_list_. |
| 77 | template<> |
| 78 | std::vector<const Pass*> PassDriver<PassDriverME>::g_default_pass_list(PassDriver<PassDriverME>::g_passes, PassDriver<PassDriverME>::g_passes + PassDriver<PassDriverME>::g_passes_size); |
| 79 | |
| 80 | PassDriverME::PassDriverME(CompilationUnit* cu) |
| 81 | : PassDriver(), pass_me_data_holder_(), dump_cfg_folder_("/sdcard/") { |
| 82 | pass_me_data_holder_.bb = nullptr; |
| 83 | pass_me_data_holder_.c_unit = cu; |
| 84 | } |
| 85 | |
| 86 | PassDriverME::~PassDriverME() { |
| 87 | } |
| 88 | |
| 89 | void PassDriverME::DispatchPass(const Pass* pass) { |
| 90 | VLOG(compiler) << "Dispatching " << pass->GetName(); |
| 91 | const PassME* me_pass = down_cast<const PassME*>(pass); |
| 92 | |
| 93 | DataFlowAnalysisMode mode = me_pass->GetTraversal(); |
| 94 | |
| 95 | switch (mode) { |
| 96 | case kPreOrderDFSTraversal: |
| 97 | DoWalkBasicBlocks<PreOrderDfsIterator>(&pass_me_data_holder_, me_pass); |
| 98 | break; |
| 99 | case kRepeatingPreOrderDFSTraversal: |
| 100 | DoWalkBasicBlocks<RepeatingPreOrderDfsIterator>(&pass_me_data_holder_, me_pass); |
| 101 | break; |
| 102 | case kRepeatingPostOrderDFSTraversal: |
| 103 | DoWalkBasicBlocks<RepeatingPostOrderDfsIterator>(&pass_me_data_holder_, me_pass); |
| 104 | break; |
| 105 | case kReversePostOrderDFSTraversal: |
| 106 | DoWalkBasicBlocks<ReversePostOrderDfsIterator>(&pass_me_data_holder_, me_pass); |
| 107 | break; |
| 108 | case kRepeatingReversePostOrderDFSTraversal: |
| 109 | DoWalkBasicBlocks<RepeatingReversePostOrderDfsIterator>(&pass_me_data_holder_, me_pass); |
| 110 | break; |
| 111 | case kPostOrderDOMTraversal: |
| 112 | DoWalkBasicBlocks<PostOrderDOMIterator>(&pass_me_data_holder_, me_pass); |
| 113 | break; |
| 114 | case kAllNodes: |
| 115 | DoWalkBasicBlocks<AllNodesIterator>(&pass_me_data_holder_, me_pass); |
| 116 | break; |
| 117 | case kNoNodes: |
| 118 | break; |
| 119 | default: |
| 120 | LOG(FATAL) << "Iterator mode not handled in dispatcher: " << mode; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | bool PassDriverME::RunPass(const Pass* pass, bool time_split) { |
| 126 | // Paranoid: c_unit and pass cannot be nullptr, and the pass should have a name |
| 127 | DCHECK(pass != nullptr); |
| 128 | DCHECK(pass->GetName() != nullptr && pass->GetName()[0] != 0); |
| 129 | CompilationUnit* c_unit = pass_me_data_holder_.c_unit; |
| 130 | DCHECK(c_unit != nullptr); |
| 131 | |
| 132 | // Do we perform a time split |
| 133 | if (time_split) { |
| 134 | c_unit->NewTimingSplit(pass->GetName()); |
| 135 | } |
| 136 | |
| 137 | // Check the pass gate first. |
| 138 | bool should_apply_pass = pass->Gate(&pass_me_data_holder_); |
| 139 | if (should_apply_pass) { |
| 140 | // Applying the pass: first start, doWork, and end calls. |
| 141 | ApplyPass(&pass_me_data_holder_, pass); |
| 142 | |
| 143 | // Do we want to log it? |
| 144 | if ((c_unit->enable_debug& (1 << kDebugDumpCFG)) != 0) { |
| 145 | // Do we have a pass folder? |
| 146 | const PassME* me_pass = (down_cast<const PassME*>(pass)); |
| 147 | const char* passFolder = me_pass->GetDumpCFGFolder(); |
| 148 | DCHECK(passFolder != nullptr); |
| 149 | |
| 150 | if (passFolder[0] != 0) { |
| 151 | // Create directory prefix. |
| 152 | std::string prefix = GetDumpCFGFolder(); |
| 153 | prefix += passFolder; |
| 154 | prefix += "/"; |
| 155 | |
| 156 | c_unit->mir_graph->DumpCFG(prefix.c_str(), false); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // If the pass gate passed, we can declare success. |
| 162 | return should_apply_pass; |
| 163 | } |
| 164 | |
| 165 | const char* PassDriverME::GetDumpCFGFolder() const { |
| 166 | return dump_cfg_folder_; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | } // namespace art |