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 | #ifndef ART_COMPILER_DEX_PASS_H_ |
| 18 | #define ART_COMPILER_DEX_PASS_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | // Forward declarations. |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 25 | struct BasicBlock; |
| 26 | struct CompilationUnit; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 27 | class Pass; |
| 28 | |
| 29 | /** |
| 30 | * @brief OptimizationFlag is an enumeration to perform certain tasks for a given pass. |
| 31 | * @details Each enum should be a power of 2 to be correctly used. |
| 32 | */ |
| 33 | enum OptimizationFlag { |
| 34 | }; |
| 35 | |
| 36 | enum DataFlowAnalysisMode { |
| 37 | kAllNodes = 0, /**< @brief All nodes. */ |
| 38 | kPreOrderDFSTraversal, /**< @brief Depth-First-Search / Pre-Order. */ |
| 39 | kRepeatingPreOrderDFSTraversal, /**< @brief Depth-First-Search / Repeating Pre-Order. */ |
| 40 | kReversePostOrderDFSTraversal, /**< @brief Depth-First-Search / Reverse Post-Order. */ |
| 41 | kRepeatingPostOrderDFSTraversal, /**< @brief Depth-First-Search / Repeating Post-Order. */ |
| 42 | kRepeatingReversePostOrderDFSTraversal, /**< @brief Depth-First-Search / Repeating Reverse Post-Order. */ |
| 43 | kPostOrderDOMTraversal, /**< @brief Dominator tree / Post-Order. */ |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 44 | kNoNodes, /**< @brief Skip BasicBlock traversal. */ |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * @class Pass |
| 49 | * @brief Pass is the Pass structure for the optimizations. |
| 50 | * @details The following structure has the different optimization passes that we are going to do. |
| 51 | */ |
| 52 | class Pass { |
| 53 | public: |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 54 | explicit Pass(const char* name, DataFlowAnalysisMode type = kAllNodes, |
| 55 | unsigned int flags = 0u, const char* dump = "") |
| 56 | : pass_name_(name), traversal_type_(type), flags_(flags), dump_cfg_folder_(dump) { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 59 | Pass(const char* name, DataFlowAnalysisMode type, const char* dump) |
| 60 | : pass_name_(name), traversal_type_(type), flags_(0), dump_cfg_folder_(dump) { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 63 | Pass(const char* name, const char* dump) |
| 64 | : pass_name_(name), traversal_type_(kAllNodes), flags_(0), dump_cfg_folder_(dump) { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 67 | virtual ~Pass() { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 70 | virtual const char* GetName() const { |
| 71 | return pass_name_; |
| 72 | } |
| 73 | |
| 74 | virtual DataFlowAnalysisMode GetTraversal() const { |
| 75 | return traversal_type_; |
| 76 | } |
| 77 | |
| 78 | virtual bool GetFlag(OptimizationFlag flag) const { |
| 79 | return (flags_ & flag); |
| 80 | } |
| 81 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 82 | const char* GetDumpCFGFolder() const { |
| 83 | return dump_cfg_folder_; |
| 84 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * @brief Gate for the pass: determines whether to execute the pass or not considering a CompilationUnit |
| 88 | * @param c_unit the CompilationUnit. |
| 89 | * @return whether or not to execute the pass |
| 90 | */ |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 91 | virtual bool Gate(const CompilationUnit* c_unit) const { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 92 | // Unused parameter. |
| 93 | UNUSED(c_unit); |
| 94 | |
| 95 | // Base class says yes. |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @brief Start of the pass: called before the WalkBasicBlocks function |
| 101 | * @param c_unit the considered CompilationUnit. |
| 102 | */ |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 103 | virtual void Start(CompilationUnit* c_unit) const { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 104 | // Unused parameter. |
| 105 | UNUSED(c_unit); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @brief End of the pass: called after the WalkBasicBlocks function |
| 110 | * @param c_unit the considered CompilationUnit. |
| 111 | */ |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 112 | virtual void End(CompilationUnit* c_unit) const { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 113 | // Unused parameter. |
| 114 | UNUSED(c_unit); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @brief Actually walk the BasicBlocks following a particular traversal type. |
| 119 | * @param c_unit the CompilationUnit. |
| 120 | * @param bb the BasicBlock. |
| 121 | * @return whether or not there is a change when walking the BasicBlock |
| 122 | */ |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 123 | virtual bool WalkBasicBlocks(CompilationUnit* c_unit, BasicBlock* bb) const { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 124 | // Unused parameters. |
| 125 | UNUSED(c_unit); |
| 126 | UNUSED(bb); |
| 127 | |
| 128 | // BasicBlock did not change. |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | protected: |
| 133 | /** @brief The pass name: used for searching for a pass when running a particular pass or debugging. */ |
| 134 | const char* const pass_name_; |
| 135 | |
| 136 | /** @brief Type of traversal: determines the order to execute the pass on the BasicBlocks. */ |
| 137 | const DataFlowAnalysisMode traversal_type_; |
| 138 | |
| 139 | /** @brief Flags for additional directives: used to determine if a particular clean-up is necessary post pass. */ |
| 140 | const unsigned int flags_; |
| 141 | |
| 142 | /** @brief CFG Dump Folder: what sub-folder to use for dumping the CFGs post pass. */ |
| 143 | const char* const dump_cfg_folder_; |
| 144 | |
| 145 | private: |
| 146 | // In order to make the all passes not copy-friendly. |
| 147 | DISALLOW_COPY_AND_ASSIGN(Pass); |
| 148 | }; |
| 149 | } // namespace art |
| 150 | #endif // ART_COMPILER_DEX_PASS_H_ |