Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Dragos Sbirlea | bfaf44f | 2013-08-06 15:41:44 -0700 | [diff] [blame] | 18 | #ifndef ART_COMPILER_SEA_IR_IR_SEA_H_ |
| 19 | #define ART_COMPILER_SEA_IR_IR_SEA_H_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 20 | |
| 21 | #include <set> |
| 22 | #include <map> |
| 23 | |
Dragos Sbirlea | 597e46b | 2013-08-07 18:15:44 -0700 | [diff] [blame] | 24 | #include "utils/scoped_hashtable.h" |
| 25 | #include "gtest/gtest_prod.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | #include "dex_file.h" |
| 27 | #include "dex_instruction.h" |
Dragos Sbirlea | bfaf44f | 2013-08-06 15:41:44 -0700 | [diff] [blame] | 28 | #include "sea_ir/ir/instruction_tools.h" |
| 29 | #include "sea_ir/ir/instruction_nodes.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | namespace sea_ir { |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 32 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 33 | // Reverse post-order numbering constants |
| 34 | enum RegionNumbering { |
| 35 | NOT_VISITED = -1, |
| 36 | VISITING = -2 |
| 37 | }; |
| 38 | |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 39 | class TypeInference; |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 40 | class CodeGenData; |
Dragos Sbirlea | e224532 | 2013-07-29 14:16:14 -0700 | [diff] [blame] | 41 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 42 | class Region; |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 43 | class InstructionNode; |
| 44 | class PhiInstructionNode; |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 45 | class SignatureNode; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 46 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 47 | // A SignatureNode is a declaration of one parameter in the function signature. |
| 48 | // This class is used to provide place-holder definitions to which instructions |
| 49 | // can return from the GetSSAUses() calls, instead of having missing SSA edges. |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 50 | class SignatureNode: public InstructionNode { |
| 51 | public: |
Dragos Sbirlea | b40eddf | 2013-07-31 13:37:31 -0700 | [diff] [blame] | 52 | // Creates a new signature node representing the initial definition of the |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 53 | // register @register_no which is the @signature_position-th argument to the method. |
| 54 | explicit SignatureNode(unsigned int register_no, unsigned int signature_position): |
| 55 | InstructionNode(NULL), register_no_(register_no), position_(signature_position) { } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 56 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 57 | int GetResultRegister() const { |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 58 | return register_no_; |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 61 | unsigned int GetPositionInSignature() const { |
Dragos Sbirlea | b40eddf | 2013-07-31 13:37:31 -0700 | [diff] [blame] | 62 | return position_; |
| 63 | } |
| 64 | |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 65 | std::vector<int> GetUses() const { |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 66 | return std::vector<int>(); |
| 67 | } |
| 68 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 69 | void Accept(IRVisitor* v) { |
| 70 | v->Visit(this); |
| 71 | v->Traverse(this); |
| 72 | } |
| 73 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 74 | private: |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 75 | const unsigned int register_no_; |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 76 | const unsigned int position_; // The position of this parameter node is |
| 77 | // in the function parameter list. |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 80 | class PhiInstructionNode: public InstructionNode { |
| 81 | public: |
| 82 | explicit PhiInstructionNode(int register_no): |
| 83 | InstructionNode(NULL), register_no_(register_no), definition_edges_() {} |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 84 | // Returns the register on which this phi-function is used. |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 85 | int GetRegisterNumber() const { |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 86 | return register_no_; |
| 87 | } |
| 88 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 89 | // Renames the use of @reg_no to refer to the instruction @definition. |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 90 | // Phi-functions are different than normal instructions in that they |
| 91 | // have multiple predecessor regions; this is why RenameToSSA has |
| 92 | // the additional parameter specifying that @parameter_id is the incoming |
| 93 | // edge for @definition, essentially creating SSA form. |
| 94 | void RenameToSSA(int reg_no, InstructionNode* definition, unsigned int predecessor_id) { |
| 95 | DCHECK(NULL != definition) << "Tried to rename to SSA using a NULL definition for " |
| 96 | << StringId() << " register " << reg_no; |
| 97 | if (definition_edges_.size() < predecessor_id+1) { |
| 98 | definition_edges_.resize(predecessor_id+1, NULL); |
| 99 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 100 | if (NULL == definition_edges_.at(predecessor_id)) { |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 101 | definition_edges_[predecessor_id] = new std::vector<InstructionNode*>(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 102 | } |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 103 | definition_edges_[predecessor_id]->push_back(definition); |
Dragos Sbirlea | e224532 | 2013-07-29 14:16:14 -0700 | [diff] [blame] | 104 | definition->AddSSAUse(this); |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Dragos Sbirlea | 7b89bc0 | 2013-08-05 16:24:57 -0700 | [diff] [blame] | 107 | // Returns the ordered set of Instructions that define the input operands of this instruction. |
| 108 | // Precondition: SeaGraph.ConvertToSSA(). |
| 109 | std::vector<InstructionNode*> GetSSAProducers() { |
| 110 | std::vector<InstructionNode*> producers; |
| 111 | for (std::vector<std::vector<InstructionNode*>*>::const_iterator |
| 112 | cit = definition_edges_.begin(); cit != definition_edges_.end(); cit++) { |
| 113 | producers.insert(producers.end(), (*cit)->begin(), (*cit)->end()); |
| 114 | } |
| 115 | return producers; |
| 116 | } |
| 117 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 118 | // Returns the instruction that defines the phi register from predecessor |
| 119 | // on position @predecessor_pos. Note that the return value is vector<> just |
| 120 | // for consistency with the return value of GetSSAUses() on regular instructions, |
| 121 | // The returned vector should always have a single element because the IR is SSA. |
| 122 | std::vector<InstructionNode*>* GetSSAUses(int predecessor_pos) { |
| 123 | return definition_edges_.at(predecessor_pos); |
| 124 | } |
| 125 | |
| 126 | void Accept(IRVisitor* v) { |
| 127 | v->Visit(this); |
| 128 | v->Traverse(this); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | private: |
| 132 | int register_no_; |
Dragos Sbirlea | 7b89bc0 | 2013-08-05 16:24:57 -0700 | [diff] [blame] | 133 | // This vector has one entry for each predecessors, each with a single |
| 134 | // element, storing the id of the instruction that defines the register |
| 135 | // corresponding to this phi function. |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 136 | std::vector<std::vector<InstructionNode*>*> definition_edges_; |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 137 | }; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 138 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 139 | // This class corresponds to a basic block in traditional compiler IRs. |
| 140 | // The dataflow analysis relies on this class both during execution and |
| 141 | // for storing its results. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 142 | class Region : public SeaNode { |
| 143 | public: |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 144 | explicit Region(): |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 145 | SeaNode(), successors_(), predecessors_(), reaching_defs_size_(0), |
Dragos Sbirlea | 6bee445 | 2013-07-26 12:05:03 -0700 | [diff] [blame] | 146 | rpo_number_(NOT_VISITED), idom_(NULL), idominated_set_(), df_(), phi_set_() { |
| 147 | string_id_ = "cluster_" + string_id_; |
| 148 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 149 | // Adds @instruction as an instruction node child in the current region. |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 150 | void AddChild(sea_ir::InstructionNode* instruction); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 151 | // Returns the last instruction node child of the current region. |
| 152 | // This child has the CFG successors pointing to the new regions. |
| 153 | SeaNode* GetLastChild() const; |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 154 | // Returns all the child instructions of this region, in program order. |
| 155 | std::vector<InstructionNode*>* GetInstructions() { |
| 156 | return &instructions_; |
| 157 | } |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 158 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 159 | // Computes Downward Exposed Definitions for the current node. |
| 160 | void ComputeDownExposedDefs(); |
| 161 | const std::map<int, sea_ir::InstructionNode*>* GetDownExposedDefs() const; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 162 | // Performs one iteration of the reaching definitions algorithm |
| 163 | // and returns true if the reaching definitions set changed. |
| 164 | bool UpdateReachingDefs(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 165 | // Returns the set of reaching definitions for the current region. |
| 166 | std::map<int, std::set<sea_ir::InstructionNode*>* >* GetReachingDefs(); |
| 167 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 168 | void SetRPO(int rpo) { |
Dragos Sbirlea | 81f79a6 | 2013-07-23 14:31:47 -0700 | [diff] [blame] | 169 | rpo_number_ = rpo; |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | int GetRPO() { |
Dragos Sbirlea | 81f79a6 | 2013-07-23 14:31:47 -0700 | [diff] [blame] | 173 | return rpo_number_; |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void SetIDominator(Region* dom) { |
| 177 | idom_ = dom; |
| 178 | } |
| 179 | |
| 180 | Region* GetIDominator() const { |
| 181 | return idom_; |
| 182 | } |
| 183 | |
| 184 | void AddToIDominatedSet(Region* dominated) { |
| 185 | idominated_set_.insert(dominated); |
| 186 | } |
| 187 | |
| 188 | const std::set<Region*>* GetIDominatedSet() { |
| 189 | return &idominated_set_; |
| 190 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 191 | // Adds @df_reg to the dominance frontier of the current region. |
| 192 | void AddToDominanceFrontier(Region* df_reg) { |
| 193 | df_.insert(df_reg); |
| 194 | } |
| 195 | // Returns the dominance frontier of the current region. |
| 196 | // Preconditions: SeaGraph.ComputeDominanceFrontier() |
| 197 | std::set<Region*>* GetDominanceFrontier() { |
| 198 | return &df_; |
| 199 | } |
| 200 | // Returns true if the region contains a phi function for @reg_no. |
| 201 | bool ContainsPhiFor(int reg_no) { |
| 202 | return (phi_set_.end() != phi_set_.find(reg_no)); |
| 203 | } |
| 204 | // Returns the phi-functions from the region. |
| 205 | std::vector<PhiInstructionNode*>* GetPhiNodes() { |
| 206 | return &phi_instructions_; |
| 207 | } |
| 208 | // Adds a phi-function for @reg_no to this region. |
| 209 | // Note: The insertion order does not matter, as phi-functions |
| 210 | // are conceptually executed at the same time. |
| 211 | bool InsertPhiFor(int reg_no); |
| 212 | // Sets the phi-function uses to be as defined in @scoped_table for predecessor @@predecessor. |
| 213 | void SetPhiDefinitionsForUses(const utils::ScopedHashtable<int, InstructionNode*>* scoped_table, |
| 214 | Region* predecessor); |
| 215 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 216 | void Accept(IRVisitor* v) { |
| 217 | v->Visit(this); |
| 218 | v->Traverse(this); |
| 219 | } |
| 220 | |
| 221 | void AddSuccessor(Region* successor) { |
| 222 | DCHECK(successor) << "Tried to add NULL successor to SEA node."; |
| 223 | successors_.push_back(successor); |
| 224 | return; |
| 225 | } |
| 226 | void AddPredecessor(Region* predecessor) { |
| 227 | DCHECK(predecessor) << "Tried to add NULL predecessor to SEA node."; |
| 228 | predecessors_.push_back(predecessor); |
| 229 | } |
| 230 | |
| 231 | std::vector<sea_ir::Region*>* GetSuccessors() { |
| 232 | return &successors_; |
| 233 | } |
| 234 | std::vector<sea_ir::Region*>* GetPredecessors() { |
| 235 | return &predecessors_; |
| 236 | } |
| 237 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 238 | private: |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 239 | std::vector<sea_ir::Region*> successors_; // CFG successor nodes (regions) |
| 240 | std::vector<sea_ir::Region*> predecessors_; // CFG predecessor nodes (instructions/regions) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 241 | std::vector<sea_ir::InstructionNode*> instructions_; |
| 242 | std::map<int, sea_ir::InstructionNode*> de_defs_; |
| 243 | std::map<int, std::set<sea_ir::InstructionNode*>* > reaching_defs_; |
| 244 | int reaching_defs_size_; |
Dragos Sbirlea | 81f79a6 | 2013-07-23 14:31:47 -0700 | [diff] [blame] | 245 | int rpo_number_; // reverse postorder number of the region |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 246 | // Immediate dominator node. |
| 247 | Region* idom_; |
| 248 | // The set of nodes immediately dominated by the region. |
| 249 | std::set<Region*> idominated_set_; |
| 250 | // Records the dominance frontier. |
| 251 | std::set<Region*> df_; |
| 252 | // Records the set of register numbers that have phi nodes in this region. |
| 253 | std::set<int> phi_set_; |
| 254 | std::vector<PhiInstructionNode*> phi_instructions_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 255 | }; |
| 256 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 257 | // A SeaGraph instance corresponds to a source code function. |
| 258 | // Its main point is to encapsulate the SEA IR representation of it |
| 259 | // and acts as starting point for visitors (ex: during code generation). |
| 260 | class SeaGraph: IVisitable { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 261 | public: |
Dragos Sbirlea | a3519a4 | 2013-08-07 14:41:55 -0700 | [diff] [blame] | 262 | static SeaGraph* GetGraph(const art::DexFile&); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 263 | |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 264 | CodeGenData* CompileMethod(const std::string& function_name, |
Ian Rogers | ee39a10 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 265 | const art::DexFile::CodeItem* code_item, uint16_t class_def_idx, |
Dragos Sbirlea | b40eddf | 2013-07-31 13:37:31 -0700 | [diff] [blame] | 266 | uint32_t method_idx, uint32_t method_access_flags, const art::DexFile& dex_file); |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 267 | // Returns all regions corresponding to this SeaGraph. |
| 268 | std::vector<Region*>* GetRegions() { |
| 269 | return ®ions_; |
| 270 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 271 | // Recursively computes the reverse postorder value for @crt_bb and successors. |
| 272 | static void ComputeRPO(Region* crt_bb, int& crt_rpo); |
| 273 | // Returns the "lowest common ancestor" of @i and @j in the dominator tree. |
| 274 | static Region* Intersect(Region* i, Region* j); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 275 | // Returns the vector of parameters of the function. |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 276 | std::vector<SignatureNode*>* GetParameterNodes() { |
| 277 | return ¶meters_; |
| 278 | } |
Dragos Sbirlea | b40eddf | 2013-07-31 13:37:31 -0700 | [diff] [blame] | 279 | |
| 280 | const art::DexFile* GetDexFile() const { |
| 281 | return &dex_file_; |
| 282 | } |
| 283 | |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 284 | virtual void Accept(IRVisitor* visitor) { |
| 285 | visitor->Initialize(this); |
| 286 | visitor->Visit(this); |
| 287 | visitor->Traverse(this); |
| 288 | } |
| 289 | |
| 290 | TypeInference* ti_; |
Ian Rogers | ee39a10 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 291 | uint16_t class_def_idx_; |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 292 | uint32_t method_idx_; |
Dragos Sbirlea | b40eddf | 2013-07-31 13:37:31 -0700 | [diff] [blame] | 293 | uint32_t method_access_flags_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 294 | |
Dragos Sbirlea | 597e46b | 2013-08-07 18:15:44 -0700 | [diff] [blame] | 295 | protected: |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 296 | explicit SeaGraph(const art::DexFile& df); |
Dragos Sbirlea | 597e46b | 2013-08-07 18:15:44 -0700 | [diff] [blame] | 297 | virtual ~SeaGraph() { } |
| 298 | |
| 299 | private: |
| 300 | FRIEND_TEST(RegionsTest, Basics); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 301 | // Registers @childReg as a region belonging to the SeaGraph instance. |
| 302 | void AddRegion(Region* childReg); |
| 303 | // Returns new region and registers it with the SeaGraph instance. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 304 | Region* GetNewRegion(); |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 305 | // Adds a (formal) parameter node to the vector of parameters of the function. |
| 306 | void AddParameterNode(SignatureNode* parameterNode) { |
| 307 | parameters_.push_back(parameterNode); |
| 308 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 309 | // Adds a CFG edge from @src node to @dst node. |
| 310 | void AddEdge(Region* src, Region* dst) const; |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 311 | // Builds the non-SSA sea-ir representation of the function @code_item from @dex_file |
| 312 | // with class id @class_def_idx and method id @method_idx. |
| 313 | void BuildMethodSeaGraph(const art::DexFile::CodeItem* code_item, |
Ian Rogers | ee39a10 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 314 | const art::DexFile& dex_file, uint16_t class_def_idx, |
Dragos Sbirlea | b40eddf | 2013-07-31 13:37:31 -0700 | [diff] [blame] | 315 | uint32_t method_idx, uint32_t method_access_flags); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 316 | // Computes immediate dominators for each region. |
| 317 | // Precondition: ComputeMethodSeaGraph() |
| 318 | void ComputeIDominators(); |
| 319 | // Computes Downward Exposed Definitions for all regions in the graph. |
| 320 | void ComputeDownExposedDefs(); |
| 321 | // Computes the reaching definitions set following the equations from |
| 322 | // Cooper & Torczon, "Engineering a Compiler", second edition, page 491. |
| 323 | // Precondition: ComputeDEDefs() |
| 324 | void ComputeReachingDefs(); |
| 325 | // Computes the reverse-postorder numbering for the region nodes. |
| 326 | // Precondition: ComputeDEDefs() |
| 327 | void ComputeRPO(); |
| 328 | // Computes the dominance frontier for all regions in the graph, |
| 329 | // following the algorithm from |
| 330 | // Cooper & Torczon, "Engineering a Compiler", second edition, page 499. |
| 331 | // Precondition: ComputeIDominators() |
| 332 | void ComputeDominanceFrontier(); |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 333 | // Converts the IR to semi-pruned SSA form. |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 334 | void ConvertToSSA(); |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 335 | // Performs the renaming phase of the SSA transformation during ConvertToSSA() execution. |
| 336 | void RenameAsSSA(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 337 | // Identifies the definitions corresponding to uses for region @node |
| 338 | // by using the scoped hashtable of names @ scoped_table. |
| 339 | void RenameAsSSA(Region* node, utils::ScopedHashtable<int, InstructionNode*>* scoped_table); |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 340 | // Generate LLVM IR for the method. |
| 341 | // Precondition: ConvertToSSA(). |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 342 | CodeGenData* GenerateLLVM(const std::string& function_name, const art::DexFile& dex_file); |
| 343 | // Inserts one SignatureNode for each argument of the function in |
| 344 | void InsertSignatureNodes(const art::DexFile::CodeItem* code_item, Region* r); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 345 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 346 | static SeaGraph graph_; |
| 347 | std::vector<Region*> regions_; |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 348 | std::vector<SignatureNode*> parameters_; |
Dragos Sbirlea | 6bee445 | 2013-07-26 12:05:03 -0700 | [diff] [blame] | 349 | const art::DexFile& dex_file_; |
Dragos Sbirlea | b40eddf | 2013-07-31 13:37:31 -0700 | [diff] [blame] | 350 | const art::DexFile::CodeItem* code_item_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 351 | }; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 352 | } // namespace sea_ir |
Dragos Sbirlea | bfaf44f | 2013-08-06 15:41:44 -0700 | [diff] [blame] | 353 | #endif // ART_COMPILER_SEA_IR_IR_SEA_H_ |