blob: c64703afff62f99827ca27906711148c40ef2a9e [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2013 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070018#ifndef ART_COMPILER_SEA_IR_SEA_H_
19#define ART_COMPILER_SEA_IR_SEA_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070020
21#include <set>
22#include <map>
23
24#include "dex_file.h"
25#include "dex_instruction.h"
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070026#include "instruction_tools.h"
27#include "instruction_nodes.h"
Brian Carlstrom1db91132013-07-12 18:05:20 -070028#include "utils/scoped_hashtable.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029
Brian Carlstrom7940e442013-07-12 13:46:57 -070030namespace sea_ir {
Brian Carlstrom1db91132013-07-12 18:05:20 -070031
Brian Carlstrom1db91132013-07-12 18:05:20 -070032// Reverse post-order numbering constants
33enum RegionNumbering {
34 NOT_VISITED = -1,
35 VISITING = -2
36};
37
Brian Carlstrom7940e442013-07-12 13:46:57 -070038class Region;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070039
Brian Carlstrom1db91132013-07-12 18:05:20 -070040class InstructionNode;
41class PhiInstructionNode;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070042class SignatureNode;
Brian Carlstrom7940e442013-07-12 13:46:57 -070043
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070044// A SignatureNode is a declaration of one parameter in the function signature.
45// This class is used to provide place-holder definitions to which instructions
46// can return from the GetSSAUses() calls, instead of having missing SSA edges.
Brian Carlstrom1db91132013-07-12 18:05:20 -070047class SignatureNode: public InstructionNode {
48 public:
Dragos Sbirleadb063062013-07-23 16:29:09 -070049 explicit SignatureNode(unsigned int parameter_register):InstructionNode(NULL),
50 parameter_register_(parameter_register) { }
Brian Carlstrom7940e442013-07-12 13:46:57 -070051
Dragos Sbirlea6bee4452013-07-26 12:05:03 -070052 void ToDot(std::string& result, const art::DexFile& dex_file) const {
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070053 result += StringId() +" [label=\"signature:";
Dragos Sbirleadb063062013-07-23 16:29:09 -070054 result += art::StringPrintf("r%d", GetResultRegister());
Brian Carlstrom1db91132013-07-12 18:05:20 -070055 result += "\"] // signature node\n";
56 }
57
Brian Carlstrom1db91132013-07-12 18:05:20 -070058 int GetResultRegister() const {
Dragos Sbirleadb063062013-07-23 16:29:09 -070059 return parameter_register_;
Brian Carlstrom1db91132013-07-12 18:05:20 -070060 }
61
62 std::vector<int> GetUses() {
63 return std::vector<int>();
64 }
65
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070066 void Accept(IRVisitor* v) {
67 v->Visit(this);
68 v->Traverse(this);
69 }
70
Brian Carlstrom1db91132013-07-12 18:05:20 -070071 private:
Dragos Sbirleadb063062013-07-23 16:29:09 -070072 unsigned int parameter_register_;
Brian Carlstrom1db91132013-07-12 18:05:20 -070073};
74
Brian Carlstrom1db91132013-07-12 18:05:20 -070075class PhiInstructionNode: public InstructionNode {
76 public:
77 explicit PhiInstructionNode(int register_no):
78 InstructionNode(NULL), register_no_(register_no), definition_edges_() {}
79 // Appends to @result the .dot string representation of the instruction.
Dragos Sbirlea6bee4452013-07-26 12:05:03 -070080 void ToDot(std::string& result, const art::DexFile& dex_file) const;
Brian Carlstrom1db91132013-07-12 18:05:20 -070081 // Returns the register on which this phi-function is used.
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070082 int GetRegisterNumber() const {
Brian Carlstrom1db91132013-07-12 18:05:20 -070083 return register_no_;
84 }
85
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070086 // Renames the use of @reg_no to refer to the instruction @definition.
Brian Carlstrom1db91132013-07-12 18:05:20 -070087 // Phi-functions are different than normal instructions in that they
88 // have multiple predecessor regions; this is why RenameToSSA has
89 // the additional parameter specifying that @parameter_id is the incoming
90 // edge for @definition, essentially creating SSA form.
91 void RenameToSSA(int reg_no, InstructionNode* definition, unsigned int predecessor_id) {
92 DCHECK(NULL != definition) << "Tried to rename to SSA using a NULL definition for "
93 << StringId() << " register " << reg_no;
94 if (definition_edges_.size() < predecessor_id+1) {
95 definition_edges_.resize(predecessor_id+1, NULL);
96 }
Brian Carlstrom1db91132013-07-12 18:05:20 -070097 if (NULL == definition_edges_.at(predecessor_id)) {
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070098 definition_edges_[predecessor_id] = new std::vector<InstructionNode*>();
Brian Carlstrom1db91132013-07-12 18:05:20 -070099 }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700100 definition_edges_[predecessor_id]->push_back(definition);
101 }
102
103 // Returns the instruction that defines the phi register from predecessor
104 // on position @predecessor_pos. Note that the return value is vector<> just
105 // for consistency with the return value of GetSSAUses() on regular instructions,
106 // The returned vector should always have a single element because the IR is SSA.
107 std::vector<InstructionNode*>* GetSSAUses(int predecessor_pos) {
108 return definition_edges_.at(predecessor_pos);
109 }
110
111 void Accept(IRVisitor* v) {
112 v->Visit(this);
113 v->Traverse(this);
Brian Carlstrom1db91132013-07-12 18:05:20 -0700114 }
115
116 private:
117 int register_no_;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700118 std::vector<std::vector<InstructionNode*>*> definition_edges_;
Brian Carlstrom1db91132013-07-12 18:05:20 -0700119};
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700121// This class corresponds to a basic block in traditional compiler IRs.
122// The dataflow analysis relies on this class both during execution and
123// for storing its results.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124class Region : public SeaNode {
125 public:
Brian Carlstrom1db91132013-07-12 18:05:20 -0700126 explicit Region():
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700127 SeaNode(), successors_(), predecessors_(), reaching_defs_size_(0),
Dragos Sbirlea6bee4452013-07-26 12:05:03 -0700128 rpo_number_(NOT_VISITED), idom_(NULL), idominated_set_(), df_(), phi_set_() {
129 string_id_ = "cluster_" + string_id_;
130 }
Brian Carlstrom1db91132013-07-12 18:05:20 -0700131 // Adds @instruction as an instruction node child in the current region.
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700132 void AddChild(sea_ir::InstructionNode* instruction);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 // Returns the last instruction node child of the current region.
134 // This child has the CFG successors pointing to the new regions.
135 SeaNode* GetLastChild() const;
Brian Carlstrom1db91132013-07-12 18:05:20 -0700136 // Returns all the child instructions of this region, in program order.
137 std::vector<InstructionNode*>* GetInstructions() {
138 return &instructions_;
139 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 // Appends to @result a dot language formatted string representing the node and
141 // (by convention) outgoing edges, so that the composition of theToDot() of all nodes
142 // builds a complete dot graph (without prolog and epilog though).
Dragos Sbirlea6bee4452013-07-26 12:05:03 -0700143 virtual void ToDot(std::string& result, const art::DexFile& dex_file) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 // Computes Downward Exposed Definitions for the current node.
145 void ComputeDownExposedDefs();
146 const std::map<int, sea_ir::InstructionNode*>* GetDownExposedDefs() const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 // Performs one iteration of the reaching definitions algorithm
148 // and returns true if the reaching definitions set changed.
149 bool UpdateReachingDefs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 // Returns the set of reaching definitions for the current region.
151 std::map<int, std::set<sea_ir::InstructionNode*>* >* GetReachingDefs();
152
Brian Carlstrom1db91132013-07-12 18:05:20 -0700153 void SetRPO(int rpo) {
Dragos Sbirlea81f79a62013-07-23 14:31:47 -0700154 rpo_number_ = rpo;
Brian Carlstrom1db91132013-07-12 18:05:20 -0700155 }
156
157 int GetRPO() {
Dragos Sbirlea81f79a62013-07-23 14:31:47 -0700158 return rpo_number_;
Brian Carlstrom1db91132013-07-12 18:05:20 -0700159 }
160
161 void SetIDominator(Region* dom) {
162 idom_ = dom;
163 }
164
165 Region* GetIDominator() const {
166 return idom_;
167 }
168
169 void AddToIDominatedSet(Region* dominated) {
170 idominated_set_.insert(dominated);
171 }
172
173 const std::set<Region*>* GetIDominatedSet() {
174 return &idominated_set_;
175 }
Brian Carlstrom1db91132013-07-12 18:05:20 -0700176 // Adds @df_reg to the dominance frontier of the current region.
177 void AddToDominanceFrontier(Region* df_reg) {
178 df_.insert(df_reg);
179 }
180 // Returns the dominance frontier of the current region.
181 // Preconditions: SeaGraph.ComputeDominanceFrontier()
182 std::set<Region*>* GetDominanceFrontier() {
183 return &df_;
184 }
185 // Returns true if the region contains a phi function for @reg_no.
186 bool ContainsPhiFor(int reg_no) {
187 return (phi_set_.end() != phi_set_.find(reg_no));
188 }
189 // Returns the phi-functions from the region.
190 std::vector<PhiInstructionNode*>* GetPhiNodes() {
191 return &phi_instructions_;
192 }
193 // Adds a phi-function for @reg_no to this region.
194 // Note: The insertion order does not matter, as phi-functions
195 // are conceptually executed at the same time.
196 bool InsertPhiFor(int reg_no);
197 // Sets the phi-function uses to be as defined in @scoped_table for predecessor @@predecessor.
198 void SetPhiDefinitionsForUses(const utils::ScopedHashtable<int, InstructionNode*>* scoped_table,
199 Region* predecessor);
200
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700201 void Accept(IRVisitor* v) {
202 v->Visit(this);
203 v->Traverse(this);
204 }
205
206 void AddSuccessor(Region* successor) {
207 DCHECK(successor) << "Tried to add NULL successor to SEA node.";
208 successors_.push_back(successor);
209 return;
210 }
211 void AddPredecessor(Region* predecessor) {
212 DCHECK(predecessor) << "Tried to add NULL predecessor to SEA node.";
213 predecessors_.push_back(predecessor);
214 }
215
216 std::vector<sea_ir::Region*>* GetSuccessors() {
217 return &successors_;
218 }
219 std::vector<sea_ir::Region*>* GetPredecessors() {
220 return &predecessors_;
221 }
222
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 private:
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700224 std::vector<sea_ir::Region*> successors_; // CFG successor nodes (regions)
225 std::vector<sea_ir::Region*> predecessors_; // CFG predecessor nodes (instructions/regions)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226 std::vector<sea_ir::InstructionNode*> instructions_;
227 std::map<int, sea_ir::InstructionNode*> de_defs_;
228 std::map<int, std::set<sea_ir::InstructionNode*>* > reaching_defs_;
229 int reaching_defs_size_;
Dragos Sbirlea81f79a62013-07-23 14:31:47 -0700230 int rpo_number_; // reverse postorder number of the region
Brian Carlstrom1db91132013-07-12 18:05:20 -0700231 // Immediate dominator node.
232 Region* idom_;
233 // The set of nodes immediately dominated by the region.
234 std::set<Region*> idominated_set_;
235 // Records the dominance frontier.
236 std::set<Region*> df_;
237 // Records the set of register numbers that have phi nodes in this region.
238 std::set<int> phi_set_;
239 std::vector<PhiInstructionNode*> phi_instructions_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240};
241
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700242// A SeaGraph instance corresponds to a source code function.
243// Its main point is to encapsulate the SEA IR representation of it
244// and acts as starting point for visitors (ex: during code generation).
245class SeaGraph: IVisitable {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 public:
Dragos Sbirlea6bee4452013-07-26 12:05:03 -0700247 static SeaGraph* GetCurrentGraph(const art::DexFile&);
Brian Carlstrom1db91132013-07-12 18:05:20 -0700248
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 void CompileMethod(const art::DexFile::CodeItem* code_item,
250 uint32_t class_def_idx, uint32_t method_idx, const art::DexFile& dex_file);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700251 // Returns all regions corresponding to this SeaGraph.
252 std::vector<Region*>* GetRegions() {
253 return &regions_;
254 }
Brian Carlstrom1db91132013-07-12 18:05:20 -0700255 // Returns a string representation of the region and its Instruction children.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 void DumpSea(std::string filename) const;
Brian Carlstrom1db91132013-07-12 18:05:20 -0700257 // Recursively computes the reverse postorder value for @crt_bb and successors.
258 static void ComputeRPO(Region* crt_bb, int& crt_rpo);
259 // Returns the "lowest common ancestor" of @i and @j in the dominator tree.
260 static Region* Intersect(Region* i, Region* j);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700261 // Returns the vector of parameters of the function.
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700262 std::vector<SignatureNode*>* GetParameterNodes() {
263 return &parameters_;
264 }
265 uint32_t class_def_idx_;
266 uint32_t method_idx_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267
268 private:
Dragos Sbirlea6bee4452013-07-26 12:05:03 -0700269 explicit SeaGraph(const art::DexFile& df):
270 class_def_idx_(0), method_idx_(0), regions_(), parameters_(), dex_file_(df) {
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700271 }
Brian Carlstrom1db91132013-07-12 18:05:20 -0700272 // Registers @childReg as a region belonging to the SeaGraph instance.
273 void AddRegion(Region* childReg);
274 // Returns new region and registers it with the SeaGraph instance.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 Region* GetNewRegion();
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700276 // Adds a (formal) parameter node to the vector of parameters of the function.
277 void AddParameterNode(SignatureNode* parameterNode) {
278 parameters_.push_back(parameterNode);
279 }
Brian Carlstrom1db91132013-07-12 18:05:20 -0700280 // Adds a CFG edge from @src node to @dst node.
281 void AddEdge(Region* src, Region* dst) const;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700282 // Builds the non-SSA sea-ir representation of the function @code_item from @dex_file
283 // with class id @class_def_idx and method id @method_idx.
284 void BuildMethodSeaGraph(const art::DexFile::CodeItem* code_item,
285 const art::DexFile& dex_file, uint32_t class_def_idx, uint32_t method_idx);
Brian Carlstrom1db91132013-07-12 18:05:20 -0700286 // Computes immediate dominators for each region.
287 // Precondition: ComputeMethodSeaGraph()
288 void ComputeIDominators();
289 // Computes Downward Exposed Definitions for all regions in the graph.
290 void ComputeDownExposedDefs();
291 // Computes the reaching definitions set following the equations from
292 // Cooper & Torczon, "Engineering a Compiler", second edition, page 491.
293 // Precondition: ComputeDEDefs()
294 void ComputeReachingDefs();
295 // Computes the reverse-postorder numbering for the region nodes.
296 // Precondition: ComputeDEDefs()
297 void ComputeRPO();
298 // Computes the dominance frontier for all regions in the graph,
299 // following the algorithm from
300 // Cooper & Torczon, "Engineering a Compiler", second edition, page 499.
301 // Precondition: ComputeIDominators()
302 void ComputeDominanceFrontier();
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700303 // Converts the IR to semi-pruned SSA form.
Brian Carlstrom1db91132013-07-12 18:05:20 -0700304 void ConvertToSSA();
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700305 // Performs the renaming phase of the SSA transformation during ConvertToSSA() execution.
306 void RenameAsSSA();
Brian Carlstrom1db91132013-07-12 18:05:20 -0700307 // Identifies the definitions corresponding to uses for region @node
308 // by using the scoped hashtable of names @ scoped_table.
309 void RenameAsSSA(Region* node, utils::ScopedHashtable<int, InstructionNode*>* scoped_table);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700310
311 virtual void Accept(IRVisitor* visitor) {
312 visitor->Initialize(this);
313 visitor->Visit(this);
314 visitor->Traverse(this);
315 }
316
317 virtual ~SeaGraph() {}
318 // Generate LLVM IR for the method.
319 // Precondition: ConvertToSSA().
320 void GenerateLLVM();
Brian Carlstrom1db91132013-07-12 18:05:20 -0700321
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 static SeaGraph graph_;
323 std::vector<Region*> regions_;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700324 std::vector<SignatureNode*> parameters_;
Dragos Sbirlea6bee4452013-07-26 12:05:03 -0700325 const art::DexFile& dex_file_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326};
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700327} // namespace sea_ir
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700328#endif // ART_COMPILER_SEA_IR_SEA_H_