blob: a0a8086568879fdf85ed41670cdc707780f9aa3b [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
Brian Carlstrom1db91132013-07-12 18:05:20 -070052 void ToDot(std::string& result) 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.
80 void ToDot(std::string& result) const;
81 // 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 Sbirlea81f79a62013-07-23 14:31:47 -0700128 rpo_number_(NOT_VISITED), idom_(NULL), idominated_set_(), df_(), phi_set_() {}
Brian Carlstrom1db91132013-07-12 18:05:20 -0700129 // Adds @instruction as an instruction node child in the current region.
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700130 void AddChild(sea_ir::InstructionNode* instruction);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 // Returns the last instruction node child of the current region.
132 // This child has the CFG successors pointing to the new regions.
133 SeaNode* GetLastChild() const;
Brian Carlstrom1db91132013-07-12 18:05:20 -0700134 // Returns all the child instructions of this region, in program order.
135 std::vector<InstructionNode*>* GetInstructions() {
136 return &instructions_;
137 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 // Appends to @result a dot language formatted string representing the node and
139 // (by convention) outgoing edges, so that the composition of theToDot() of all nodes
140 // builds a complete dot graph (without prolog and epilog though).
141 virtual void ToDot(std::string& result) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 // Computes Downward Exposed Definitions for the current node.
143 void ComputeDownExposedDefs();
144 const std::map<int, sea_ir::InstructionNode*>* GetDownExposedDefs() const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 // Performs one iteration of the reaching definitions algorithm
146 // and returns true if the reaching definitions set changed.
147 bool UpdateReachingDefs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 // Returns the set of reaching definitions for the current region.
149 std::map<int, std::set<sea_ir::InstructionNode*>* >* GetReachingDefs();
150
Brian Carlstrom1db91132013-07-12 18:05:20 -0700151 void SetRPO(int rpo) {
Dragos Sbirlea81f79a62013-07-23 14:31:47 -0700152 rpo_number_ = rpo;
Brian Carlstrom1db91132013-07-12 18:05:20 -0700153 }
154
155 int GetRPO() {
Dragos Sbirlea81f79a62013-07-23 14:31:47 -0700156 return rpo_number_;
Brian Carlstrom1db91132013-07-12 18:05:20 -0700157 }
158
159 void SetIDominator(Region* dom) {
160 idom_ = dom;
161 }
162
163 Region* GetIDominator() const {
164 return idom_;
165 }
166
167 void AddToIDominatedSet(Region* dominated) {
168 idominated_set_.insert(dominated);
169 }
170
171 const std::set<Region*>* GetIDominatedSet() {
172 return &idominated_set_;
173 }
Brian Carlstrom1db91132013-07-12 18:05:20 -0700174 // Adds @df_reg to the dominance frontier of the current region.
175 void AddToDominanceFrontier(Region* df_reg) {
176 df_.insert(df_reg);
177 }
178 // Returns the dominance frontier of the current region.
179 // Preconditions: SeaGraph.ComputeDominanceFrontier()
180 std::set<Region*>* GetDominanceFrontier() {
181 return &df_;
182 }
183 // Returns true if the region contains a phi function for @reg_no.
184 bool ContainsPhiFor(int reg_no) {
185 return (phi_set_.end() != phi_set_.find(reg_no));
186 }
187 // Returns the phi-functions from the region.
188 std::vector<PhiInstructionNode*>* GetPhiNodes() {
189 return &phi_instructions_;
190 }
191 // Adds a phi-function for @reg_no to this region.
192 // Note: The insertion order does not matter, as phi-functions
193 // are conceptually executed at the same time.
194 bool InsertPhiFor(int reg_no);
195 // Sets the phi-function uses to be as defined in @scoped_table for predecessor @@predecessor.
196 void SetPhiDefinitionsForUses(const utils::ScopedHashtable<int, InstructionNode*>* scoped_table,
197 Region* predecessor);
198
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700199 void Accept(IRVisitor* v) {
200 v->Visit(this);
201 v->Traverse(this);
202 }
203
204 void AddSuccessor(Region* successor) {
205 DCHECK(successor) << "Tried to add NULL successor to SEA node.";
206 successors_.push_back(successor);
207 return;
208 }
209 void AddPredecessor(Region* predecessor) {
210 DCHECK(predecessor) << "Tried to add NULL predecessor to SEA node.";
211 predecessors_.push_back(predecessor);
212 }
213
214 std::vector<sea_ir::Region*>* GetSuccessors() {
215 return &successors_;
216 }
217 std::vector<sea_ir::Region*>* GetPredecessors() {
218 return &predecessors_;
219 }
220
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 private:
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700222 std::vector<sea_ir::Region*> successors_; // CFG successor nodes (regions)
223 std::vector<sea_ir::Region*> predecessors_; // CFG predecessor nodes (instructions/regions)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 std::vector<sea_ir::InstructionNode*> instructions_;
225 std::map<int, sea_ir::InstructionNode*> de_defs_;
226 std::map<int, std::set<sea_ir::InstructionNode*>* > reaching_defs_;
227 int reaching_defs_size_;
Dragos Sbirlea81f79a62013-07-23 14:31:47 -0700228 int rpo_number_; // reverse postorder number of the region
Brian Carlstrom1db91132013-07-12 18:05:20 -0700229 // Immediate dominator node.
230 Region* idom_;
231 // The set of nodes immediately dominated by the region.
232 std::set<Region*> idominated_set_;
233 // Records the dominance frontier.
234 std::set<Region*> df_;
235 // Records the set of register numbers that have phi nodes in this region.
236 std::set<int> phi_set_;
237 std::vector<PhiInstructionNode*> phi_instructions_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238};
239
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700240// A SeaGraph instance corresponds to a source code function.
241// Its main point is to encapsulate the SEA IR representation of it
242// and acts as starting point for visitors (ex: during code generation).
243class SeaGraph: IVisitable {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 public:
245 static SeaGraph* GetCurrentGraph();
Brian Carlstrom1db91132013-07-12 18:05:20 -0700246
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 void CompileMethod(const art::DexFile::CodeItem* code_item,
248 uint32_t class_def_idx, uint32_t method_idx, const art::DexFile& dex_file);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700249 // Returns all regions corresponding to this SeaGraph.
250 std::vector<Region*>* GetRegions() {
251 return &regions_;
252 }
Brian Carlstrom1db91132013-07-12 18:05:20 -0700253 // Returns a string representation of the region and its Instruction children.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 void DumpSea(std::string filename) const;
Brian Carlstrom1db91132013-07-12 18:05:20 -0700255 // Recursively computes the reverse postorder value for @crt_bb and successors.
256 static void ComputeRPO(Region* crt_bb, int& crt_rpo);
257 // Returns the "lowest common ancestor" of @i and @j in the dominator tree.
258 static Region* Intersect(Region* i, Region* j);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700259 //Returns the vector of parameters of the function.
260 std::vector<SignatureNode*>* GetParameterNodes() {
261 return &parameters_;
262 }
263 uint32_t class_def_idx_;
264 uint32_t method_idx_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265
266 private:
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700267 SeaGraph(): class_def_idx_(0), method_idx_(0), regions_(), parameters_() {
268 }
Brian Carlstrom1db91132013-07-12 18:05:20 -0700269 // Registers @childReg as a region belonging to the SeaGraph instance.
270 void AddRegion(Region* childReg);
271 // Returns new region and registers it with the SeaGraph instance.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 Region* GetNewRegion();
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700273 // Adds a (formal) parameter node to the vector of parameters of the function.
274 void AddParameterNode(SignatureNode* parameterNode) {
275 parameters_.push_back(parameterNode);
276 }
Brian Carlstrom1db91132013-07-12 18:05:20 -0700277 // Adds a CFG edge from @src node to @dst node.
278 void AddEdge(Region* src, Region* dst) const;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700279 // Builds the non-SSA sea-ir representation of the function @code_item from @dex_file
280 // with class id @class_def_idx and method id @method_idx.
281 void BuildMethodSeaGraph(const art::DexFile::CodeItem* code_item,
282 const art::DexFile& dex_file, uint32_t class_def_idx, uint32_t method_idx);
Brian Carlstrom1db91132013-07-12 18:05:20 -0700283 // Computes immediate dominators for each region.
284 // Precondition: ComputeMethodSeaGraph()
285 void ComputeIDominators();
286 // Computes Downward Exposed Definitions for all regions in the graph.
287 void ComputeDownExposedDefs();
288 // Computes the reaching definitions set following the equations from
289 // Cooper & Torczon, "Engineering a Compiler", second edition, page 491.
290 // Precondition: ComputeDEDefs()
291 void ComputeReachingDefs();
292 // Computes the reverse-postorder numbering for the region nodes.
293 // Precondition: ComputeDEDefs()
294 void ComputeRPO();
295 // Computes the dominance frontier for all regions in the graph,
296 // following the algorithm from
297 // Cooper & Torczon, "Engineering a Compiler", second edition, page 499.
298 // Precondition: ComputeIDominators()
299 void ComputeDominanceFrontier();
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700300 // Converts the IR to semi-pruned SSA form.
Brian Carlstrom1db91132013-07-12 18:05:20 -0700301 void ConvertToSSA();
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700302 // Performs the renaming phase of the SSA transformation during ConvertToSSA() execution.
303 void RenameAsSSA();
Brian Carlstrom1db91132013-07-12 18:05:20 -0700304 // Identifies the definitions corresponding to uses for region @node
305 // by using the scoped hashtable of names @ scoped_table.
306 void RenameAsSSA(Region* node, utils::ScopedHashtable<int, InstructionNode*>* scoped_table);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700307
308 virtual void Accept(IRVisitor* visitor) {
309 visitor->Initialize(this);
310 visitor->Visit(this);
311 visitor->Traverse(this);
312 }
313
314 virtual ~SeaGraph() {}
315 // Generate LLVM IR for the method.
316 // Precondition: ConvertToSSA().
317 void GenerateLLVM();
Brian Carlstrom1db91132013-07-12 18:05:20 -0700318
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 static SeaGraph graph_;
320 std::vector<Region*> regions_;
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700321 std::vector<SignatureNode*> parameters_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322};
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323} // end namespace sea_ir
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700324#endif // ART_COMPILER_SEA_IR_SEA_H_