blob: c52ddf5f2793c0e5dbc4256fbb49f20f56541051 [file] [log] [blame]
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001/*
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.
25class BasicBlock;
26class 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};
45
46/**
47 * @class Pass
48 * @brief Pass is the Pass structure for the optimizations.
49 * @details The following structure has the different optimization passes that we are going to do.
50 */
51class Pass {
52 public:
53 Pass(const char *name, DataFlowAnalysisMode type, bool freed, const unsigned int f, const char *dump): pass_name_(name), traversal_type_(type), flags_(f), dump_cfg_folder_(dump) {
54 }
55
56 Pass(const char *name, const char *dump): pass_name_(name), traversal_type_(kAllNodes), flags_(0), dump_cfg_folder_(dump) {
57 }
58
59 explicit Pass(const char *name):pass_name_(name), traversal_type_(kAllNodes), flags_(0), dump_cfg_folder_("") {
60 }
61
62 Pass(const char *name, DataFlowAnalysisMode type, const char *dump):pass_name_(name), traversal_type_(type), flags_(false), dump_cfg_folder_(dump) {
63 }
64
65 virtual ~Pass() {}
66
67 virtual const char* GetName() const {
68 return pass_name_;
69 }
70
71 virtual DataFlowAnalysisMode GetTraversal() const {
72 return traversal_type_;
73 }
74
75 virtual bool GetFlag(OptimizationFlag flag) const {
76 return (flags_ & flag);
77 }
78
79 const char* GetDumpCFGFolder() const {return dump_cfg_folder_;}
80
81 /**
82 * @brief Gate for the pass: determines whether to execute the pass or not considering a CompilationUnit
83 * @param c_unit the CompilationUnit.
84 * @return whether or not to execute the pass
85 */
86 virtual bool Gate(const CompilationUnit *c_unit) const {
87 // Unused parameter.
88 UNUSED(c_unit);
89
90 // Base class says yes.
91 return true;
92 }
93
94 /**
95 * @brief Start of the pass: called before the WalkBasicBlocks function
96 * @param c_unit the considered CompilationUnit.
97 */
98 virtual void Start(CompilationUnit *c_unit) const {
99 // Unused parameter.
100 UNUSED(c_unit);
101 }
102
103 /**
104 * @brief End of the pass: called after the WalkBasicBlocks function
105 * @param c_unit the considered CompilationUnit.
106 */
107 virtual void End(CompilationUnit *c_unit) const {
108 // Unused parameter.
109 UNUSED(c_unit);
110 }
111
112 /**
113 * @brief Actually walk the BasicBlocks following a particular traversal type.
114 * @param c_unit the CompilationUnit.
115 * @param bb the BasicBlock.
116 * @return whether or not there is a change when walking the BasicBlock
117 */
118 virtual bool WalkBasicBlocks(CompilationUnit *c_unit, BasicBlock *bb) const {
119 // Unused parameters.
120 UNUSED(c_unit);
121 UNUSED(bb);
122
123 // BasicBlock did not change.
124 return false;
125 }
126
127 protected:
128 /** @brief The pass name: used for searching for a pass when running a particular pass or debugging. */
129 const char* const pass_name_;
130
131 /** @brief Type of traversal: determines the order to execute the pass on the BasicBlocks. */
132 const DataFlowAnalysisMode traversal_type_;
133
134 /** @brief Flags for additional directives: used to determine if a particular clean-up is necessary post pass. */
135 const unsigned int flags_;
136
137 /** @brief CFG Dump Folder: what sub-folder to use for dumping the CFGs post pass. */
138 const char* const dump_cfg_folder_;
139
140 private:
141 // In order to make the all passes not copy-friendly.
142 DISALLOW_COPY_AND_ASSIGN(Pass);
143};
144} // namespace art
145#endif // ART_COMPILER_DEX_PASS_H_