pass.h revision b48b9eb6d181a1f52e2e605cf26a21505f1d46ed
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
22namespace art {
23
24// Forward declarations.
25struct BasicBlock;
26struct CompilationUnit;
27class 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 */
33enum OptimizationFlag {
34};
35
36enum 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. */
44  kNoNodes,                                /**< @brief Skip BasicBlock traversal. */
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 */
52class Pass {
53 public:
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) {
57  }
58
59  Pass(const char* name, DataFlowAnalysisMode type, const char* dump)
60    : pass_name_(name), traversal_type_(type), flags_(0), dump_cfg_folder_(dump) {
61  }
62
63  Pass(const char* name, const char* dump)
64    : pass_name_(name), traversal_type_(kAllNodes), flags_(0), dump_cfg_folder_(dump) {
65  }
66
67  virtual ~Pass() {
68  }
69
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
82  const char* GetDumpCFGFolder() const {
83    return dump_cfg_folder_;
84  }
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   */
91  virtual bool Gate(const CompilationUnit* c_unit) const {
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   */
103  virtual void Start(CompilationUnit* c_unit) const {
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   */
112  virtual void End(CompilationUnit* c_unit) const {
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   */
123  virtual bool WalkBasicBlocks(CompilationUnit* c_unit, BasicBlock* bb) const {
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_
151