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 | */ |
Dragos Sbirlea | db06306 | 2013-07-23 16:29:09 -0700 | [diff] [blame^] | 16 | #include "base/stringprintf.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 17 | #include "file_output_stream.h" |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 18 | #include "instruction_tools.h" |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 19 | #include "sea.h" |
| 20 | #include "code_gen.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | |
| 22 | #define MAX_REACHING_DEF_ITERERATIONS (10) |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 23 | // TODO: When development is done, this define should not |
| 24 | // be needed, it is currently used as a cutoff |
| 25 | // for cases where the iterative fixed point algorithm |
| 26 | // does not reach a fixed point because of a bug. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 27 | |
| 28 | namespace sea_ir { |
| 29 | |
| 30 | SeaGraph SeaGraph::graph_; |
| 31 | int SeaNode::current_max_node_id_ = 0; |
| 32 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 33 | void IRVisitor::Traverse(Region* region) { |
| 34 | std::vector<PhiInstructionNode*>* phis = region->GetPhiNodes(); |
| 35 | for (std::vector<PhiInstructionNode*>::const_iterator cit = phis->begin(); |
| 36 | cit != phis->end(); cit++) { |
| 37 | (*cit)->Accept(this); |
| 38 | } |
| 39 | |
| 40 | std::vector<InstructionNode*>* instructions = region->GetInstructions(); |
| 41 | for (std::vector<InstructionNode*>::const_iterator cit = instructions->begin(); |
| 42 | cit != instructions->end(); cit++) { |
| 43 | (*cit)->Accept(this); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void IRVisitor::Traverse(SeaGraph* graph) { |
| 48 | for (std::vector<Region*>::const_iterator cit = ordered_regions_.begin(); |
| 49 | cit != ordered_regions_.end(); cit++ ) { |
| 50 | (*cit)->Accept(this); |
| 51 | } |
| 52 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 53 | |
| 54 | SeaGraph* SeaGraph::GetCurrentGraph() { |
| 55 | return &sea_ir::SeaGraph::graph_; |
| 56 | } |
| 57 | |
| 58 | void SeaGraph::DumpSea(std::string filename) const { |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 59 | LOG(INFO) << "Starting to write SEA string to file."; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 60 | std::string result; |
| 61 | result += "digraph seaOfNodes {\n"; |
| 62 | for (std::vector<Region*>::const_iterator cit = regions_.begin(); cit != regions_.end(); cit++) { |
| 63 | (*cit)->ToDot(result); |
| 64 | } |
| 65 | result += "}\n"; |
| 66 | art::File* file = art::OS::OpenFile(filename.c_str(), true, true); |
| 67 | art::FileOutputStream fos(file); |
| 68 | fos.WriteFully(result.c_str(), result.size()); |
| 69 | LOG(INFO) << "Written SEA string to file."; |
| 70 | } |
| 71 | |
| 72 | void SeaGraph::AddEdge(Region* src, Region* dst) const { |
| 73 | src->AddSuccessor(dst); |
| 74 | dst->AddPredecessor(src); |
| 75 | } |
| 76 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 77 | void SeaGraph::ComputeRPO(Region* current_region, int& current_rpo) { |
| 78 | current_region->SetRPO(VISITING); |
| 79 | std::vector<sea_ir::Region*>* succs = current_region->GetSuccessors(); |
| 80 | for (std::vector<sea_ir::Region*>::iterator succ_it = succs->begin(); |
| 81 | succ_it != succs->end(); ++succ_it) { |
| 82 | if (NOT_VISITED == (*succ_it)->GetRPO()) { |
| 83 | SeaGraph::ComputeRPO(*succ_it, current_rpo); |
| 84 | } |
| 85 | } |
| 86 | current_region->SetRPO(current_rpo--); |
| 87 | } |
| 88 | |
| 89 | void SeaGraph::ComputeIDominators() { |
| 90 | bool changed = true; |
| 91 | while (changed) { |
| 92 | changed = false; |
| 93 | // Entry node has itself as IDOM. |
| 94 | std::vector<Region*>::iterator crt_it; |
| 95 | std::set<Region*> processedNodes; |
| 96 | // Find and mark the entry node(s). |
| 97 | for (crt_it = regions_.begin(); crt_it != regions_.end(); ++crt_it) { |
| 98 | if ((*crt_it)->GetPredecessors()->size() == 0) { |
| 99 | processedNodes.insert(*crt_it); |
| 100 | (*crt_it)->SetIDominator(*crt_it); |
| 101 | } |
| 102 | } |
| 103 | for (crt_it = regions_.begin(); crt_it != regions_.end(); ++crt_it) { |
| 104 | if ((*crt_it)->GetPredecessors()->size() == 0) { |
| 105 | continue; |
| 106 | } |
| 107 | // NewIDom = first (processed) predecessor of b. |
| 108 | Region* new_dom = NULL; |
| 109 | std::vector<Region*>* preds = (*crt_it)->GetPredecessors(); |
| 110 | DCHECK(NULL != preds); |
| 111 | Region* root_pred = NULL; |
| 112 | for (std::vector<Region*>::iterator pred_it = preds->begin(); |
| 113 | pred_it != preds->end(); ++pred_it) { |
| 114 | if (processedNodes.end() != processedNodes.find((*pred_it))) { |
| 115 | root_pred = *pred_it; |
| 116 | new_dom = root_pred; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | // For all other predecessors p of b, if idom is not set, |
| 121 | // then NewIdom = Intersect(p, NewIdom) |
| 122 | for (std::vector<Region*>::const_iterator pred_it = preds->begin(); |
| 123 | pred_it != preds->end(); ++pred_it) { |
| 124 | DCHECK(NULL != *pred_it); |
| 125 | // if IDOMS[p] != UNDEFINED |
| 126 | if ((*pred_it != root_pred) && (*pred_it)->GetIDominator() != NULL) { |
| 127 | DCHECK(NULL != new_dom); |
| 128 | new_dom = SeaGraph::Intersect(*pred_it, new_dom); |
| 129 | } |
| 130 | } |
| 131 | DCHECK(NULL != *crt_it); |
| 132 | if ((*crt_it)->GetIDominator() != new_dom) { |
| 133 | (*crt_it)->SetIDominator(new_dom); |
| 134 | changed = true; |
| 135 | } |
| 136 | processedNodes.insert(*crt_it); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // For easily ordering of regions we need edges dominator->dominated. |
| 141 | for (std::vector<Region*>::iterator region_it = regions_.begin(); |
| 142 | region_it != regions_.end(); region_it++) { |
| 143 | Region* idom = (*region_it)->GetIDominator(); |
| 144 | if (idom != *region_it) { |
| 145 | idom->AddToIDominatedSet(*region_it); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | Region* SeaGraph::Intersect(Region* i, Region* j) { |
| 151 | Region* finger1 = i; |
| 152 | Region* finger2 = j; |
| 153 | while (finger1 != finger2) { |
| 154 | while (finger1->GetRPO() > finger2->GetRPO()) { |
| 155 | DCHECK(NULL != finger1); |
| 156 | finger1 = finger1->GetIDominator(); // should have: finger1 != NULL |
| 157 | DCHECK(NULL != finger1); |
| 158 | } |
| 159 | while (finger1->GetRPO() < finger2->GetRPO()) { |
| 160 | DCHECK(NULL != finger2); |
| 161 | finger2 = finger2->GetIDominator(); // should have: finger1 != NULL |
| 162 | DCHECK(NULL != finger2); |
| 163 | } |
| 164 | } |
| 165 | return finger1; // finger1 should be equal to finger2 at this point. |
| 166 | } |
| 167 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 168 | void SeaGraph::ComputeDownExposedDefs() { |
| 169 | for (std::vector<Region*>::iterator region_it = regions_.begin(); |
| 170 | region_it != regions_.end(); region_it++) { |
| 171 | (*region_it)->ComputeDownExposedDefs(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void SeaGraph::ComputeReachingDefs() { |
| 176 | // Iterate until the reaching definitions set doesn't change anymore. |
| 177 | // (See Cooper & Torczon, "Engineering a Compiler", second edition, page 487) |
| 178 | bool changed = true; |
| 179 | int iteration = 0; |
| 180 | while (changed && (iteration < MAX_REACHING_DEF_ITERERATIONS)) { |
| 181 | iteration++; |
| 182 | changed = false; |
| 183 | // TODO: optimize the ordering if this becomes performance bottleneck. |
| 184 | for (std::vector<Region*>::iterator regions_it = regions_.begin(); |
| 185 | regions_it != regions_.end(); |
| 186 | regions_it++) { |
| 187 | changed |= (*regions_it)->UpdateReachingDefs(); |
| 188 | } |
| 189 | } |
| 190 | DCHECK(!changed) << "Reaching definitions computation did not reach a fixed point."; |
| 191 | } |
| 192 | |
| 193 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 194 | void SeaGraph::BuildMethodSeaGraph(const art::DexFile::CodeItem* code_item, |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 195 | const art::DexFile& dex_file, uint32_t class_def_idx, uint32_t method_idx) { |
| 196 | class_def_idx_ = class_def_idx; |
| 197 | method_idx_ = method_idx; |
| 198 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 199 | const uint16_t* code = code_item->insns_; |
| 200 | const size_t size_in_code_units = code_item->insns_size_in_code_units_; |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 201 | // This maps target instruction pointers to their corresponding region objects. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 202 | std::map<const uint16_t*, Region*> target_regions; |
| 203 | size_t i = 0; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 204 | // Pass: Find the start instruction of basic blocks |
| 205 | // by locating targets and flow-though instructions of branches. |
| 206 | while (i < size_in_code_units) { |
| 207 | const art::Instruction* inst = art::Instruction::At(&code[i]); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 208 | if (inst->IsBranch() || inst->IsUnconditional()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 209 | int32_t offset = inst->GetTargetOffset(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 210 | if (target_regions.end() == target_regions.find(&code[i + offset])) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 211 | Region* region = GetNewRegion(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 212 | target_regions.insert(std::pair<const uint16_t*, Region*>(&code[i + offset], region)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 213 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 214 | if (inst->CanFlowThrough() |
| 215 | && (target_regions.end() == target_regions.find(&code[i + inst->SizeInCodeUnits()]))) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 216 | Region* region = GetNewRegion(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 217 | target_regions.insert( |
| 218 | std::pair<const uint16_t*, Region*>(&code[i + inst->SizeInCodeUnits()], region)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | i += inst->SizeInCodeUnits(); |
| 222 | } |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 223 | |
| 224 | |
| 225 | Region* r = GetNewRegion(); |
| 226 | // Insert one SignatureNode per function argument, |
| 227 | // to serve as placeholder definitions in dataflow analysis. |
| 228 | for (unsigned int crt_offset = 0; crt_offset < code_item->ins_size_; crt_offset++) { |
| 229 | SignatureNode* parameter_def_node = |
| 230 | new sea_ir::SignatureNode(code_item->registers_size_ - 1 - crt_offset); |
| 231 | AddParameterNode(parameter_def_node); |
| 232 | r->AddChild(parameter_def_node); |
| 233 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 234 | // Pass: Assign instructions to region nodes and |
| 235 | // assign branches their control flow successors. |
| 236 | i = 0; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 237 | sea_ir::InstructionNode* last_node = NULL; |
| 238 | sea_ir::InstructionNode* node = NULL; |
| 239 | while (i < size_in_code_units) { |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 240 | const art::Instruction* inst = art::Instruction::At(&code[i]); |
| 241 | std::vector<InstructionNode*> sea_instructions_for_dalvik = sea_ir::InstructionNode::Create(inst); |
| 242 | for (std::vector<InstructionNode*>::const_iterator cit = sea_instructions_for_dalvik.begin(); |
| 243 | sea_instructions_for_dalvik.end() != cit; ++cit) { |
| 244 | last_node = node; |
| 245 | node = *cit; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 246 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 247 | if (inst->IsBranch() || inst->IsUnconditional()) { |
| 248 | int32_t offset = inst->GetTargetOffset(); |
| 249 | std::map<const uint16_t*, Region*>::iterator it = target_regions.find(&code[i + offset]); |
| 250 | DCHECK(it != target_regions.end()); |
| 251 | AddEdge(r, it->second); // Add edge to branch target. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 252 | } |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 253 | |
| 254 | std::map<const uint16_t*, Region*>::iterator it = target_regions.find(&code[i]); |
| 255 | if (target_regions.end() != it) { |
| 256 | // Get the already created region because this is a branch target. |
| 257 | Region* nextRegion = it->second; |
| 258 | if (last_node->GetInstruction()->IsBranch() |
| 259 | && last_node->GetInstruction()->CanFlowThrough()) { |
| 260 | AddEdge(r, it->second); // Add flow-through edge. |
| 261 | } |
| 262 | r = nextRegion; |
| 263 | } |
| 264 | bool definesRegister = (0 != InstructionTools::instruction_attributes_[inst->Opcode()] |
| 265 | && (1 << kDA)); |
| 266 | LOG(INFO)<< inst->GetDexPc(code) << "*** " << inst->DumpString(&dex_file) |
| 267 | << " region:" <<r->StringId() << "Definition?" << definesRegister << std::endl; |
| 268 | r->AddChild(node); |
| 269 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 270 | i += inst->SizeInCodeUnits(); |
| 271 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 272 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 274 | void SeaGraph::ComputeRPO() { |
| 275 | int rpo_id = regions_.size() - 1; |
| 276 | for (std::vector<Region*>::const_iterator crt_it = regions_.begin(); crt_it != regions_.end(); |
| 277 | ++crt_it) { |
| 278 | if ((*crt_it)->GetPredecessors()->size() == 0) { |
| 279 | ComputeRPO(*crt_it, rpo_id); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Performs the renaming phase in traditional SSA transformations. |
| 285 | // See: Cooper & Torczon, "Engineering a Compiler", second edition, page 505.) |
| 286 | void SeaGraph::RenameAsSSA() { |
| 287 | utils::ScopedHashtable<int, InstructionNode*> scoped_table; |
| 288 | scoped_table.OpenScope(); |
| 289 | for (std::vector<Region*>::iterator region_it = regions_.begin(); region_it != regions_.end(); |
| 290 | region_it++) { |
| 291 | if ((*region_it)->GetIDominator() == *region_it) { |
| 292 | RenameAsSSA(*region_it, &scoped_table); |
| 293 | } |
| 294 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 295 | scoped_table.CloseScope(); |
| 296 | } |
| 297 | |
| 298 | void SeaGraph::ConvertToSSA() { |
| 299 | // Pass: find global names. |
| 300 | // The map @block maps registers to the blocks in which they are defined. |
| 301 | std::map<int, std::set<Region*> > blocks; |
| 302 | // The set @globals records registers whose use |
| 303 | // is in a different block than the corresponding definition. |
| 304 | std::set<int> globals; |
| 305 | for (std::vector<Region*>::iterator region_it = regions_.begin(); region_it != regions_.end(); |
| 306 | region_it++) { |
| 307 | std::set<int> var_kill; |
| 308 | std::vector<InstructionNode*>* instructions = (*region_it)->GetInstructions(); |
| 309 | for (std::vector<InstructionNode*>::iterator inst_it = instructions->begin(); |
| 310 | inst_it != instructions->end(); inst_it++) { |
| 311 | std::vector<int> used_regs = (*inst_it)->GetUses(); |
| 312 | for (std::size_t i = 0; i < used_regs.size(); i++) { |
| 313 | int used_reg = used_regs[i]; |
| 314 | if (var_kill.find(used_reg) == var_kill.end()) { |
| 315 | globals.insert(used_reg); |
| 316 | } |
| 317 | } |
| 318 | const int reg_def = (*inst_it)->GetResultRegister(); |
| 319 | if (reg_def != NO_REGISTER) { |
| 320 | var_kill.insert(reg_def); |
| 321 | } |
| 322 | |
| 323 | blocks.insert(std::pair<int, std::set<Region*> >(reg_def, std::set<Region*>())); |
| 324 | std::set<Region*>* reg_def_blocks = &(blocks.find(reg_def)->second); |
| 325 | reg_def_blocks->insert(*region_it); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Pass: Actually add phi-nodes to regions. |
| 330 | for (std::set<int>::const_iterator globals_it = globals.begin(); |
| 331 | globals_it != globals.end(); globals_it++) { |
| 332 | int global = *globals_it; |
| 333 | // Copy the set, because we will modify the worklist as we go. |
| 334 | std::set<Region*> worklist((*(blocks.find(global))).second); |
| 335 | for (std::set<Region*>::const_iterator b_it = worklist.begin(); b_it != worklist.end(); b_it++) { |
| 336 | std::set<Region*>* df = (*b_it)->GetDominanceFrontier(); |
| 337 | for (std::set<Region*>::const_iterator df_it = df->begin(); df_it != df->end(); df_it++) { |
| 338 | if ((*df_it)->InsertPhiFor(global)) { |
| 339 | // Check that the dominance frontier element is in the worklist already |
| 340 | // because we only want to break if the element is actually not there yet. |
| 341 | if (worklist.find(*df_it) == worklist.end()) { |
| 342 | worklist.insert(*df_it); |
| 343 | b_it = worklist.begin(); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | // Pass: Build edges to the definition corresponding to each use. |
| 351 | // (This corresponds to the renaming phase in traditional SSA transformations. |
| 352 | // See: Cooper & Torczon, "Engineering a Compiler", second edition, page 505.) |
| 353 | RenameAsSSA(); |
| 354 | } |
| 355 | |
| 356 | void SeaGraph::RenameAsSSA(Region* crt_region, |
| 357 | utils::ScopedHashtable<int, InstructionNode*>* scoped_table) { |
| 358 | scoped_table->OpenScope(); |
| 359 | // Rename phi nodes defined in the current region. |
| 360 | std::vector<PhiInstructionNode*>* phis = crt_region->GetPhiNodes(); |
| 361 | for (std::vector<PhiInstructionNode*>::iterator phi_it = phis->begin(); |
| 362 | phi_it != phis->end(); phi_it++) { |
| 363 | int reg_no = (*phi_it)->GetRegisterNumber(); |
| 364 | scoped_table->Add(reg_no, (*phi_it)); |
| 365 | } |
| 366 | // Rename operands of instructions from the current region. |
| 367 | std::vector<InstructionNode*>* instructions = crt_region->GetInstructions(); |
| 368 | for (std::vector<InstructionNode*>::const_iterator instructions_it = instructions->begin(); |
| 369 | instructions_it != instructions->end(); instructions_it++) { |
| 370 | InstructionNode* current_instruction = (*instructions_it); |
| 371 | // Rename uses. |
| 372 | std::vector<int> used_regs = current_instruction->GetUses(); |
| 373 | for (std::vector<int>::const_iterator reg_it = used_regs.begin(); |
| 374 | reg_it != used_regs.end(); reg_it++) { |
| 375 | int current_used_reg = (*reg_it); |
| 376 | InstructionNode* definition = scoped_table->Lookup(current_used_reg); |
| 377 | current_instruction->RenameToSSA(current_used_reg, definition); |
| 378 | } |
| 379 | // Update scope table with latest definitions. |
| 380 | std::vector<int> def_regs = current_instruction->GetDefinitions(); |
| 381 | for (std::vector<int>::const_iterator reg_it = def_regs.begin(); |
| 382 | reg_it != def_regs.end(); reg_it++) { |
| 383 | int current_defined_reg = (*reg_it); |
| 384 | scoped_table->Add(current_defined_reg, current_instruction); |
| 385 | } |
| 386 | } |
| 387 | // Fill in uses of phi functions in CFG successor regions. |
| 388 | const std::vector<Region*>* successors = crt_region->GetSuccessors(); |
| 389 | for (std::vector<Region*>::const_iterator successors_it = successors->begin(); |
| 390 | successors_it != successors->end(); successors_it++) { |
| 391 | Region* successor = (*successors_it); |
| 392 | successor->SetPhiDefinitionsForUses(scoped_table, crt_region); |
| 393 | } |
| 394 | |
| 395 | // Rename all successors in the dominators tree. |
| 396 | const std::set<Region*>* dominated_nodes = crt_region->GetIDominatedSet(); |
| 397 | for (std::set<Region*>::const_iterator dominated_nodes_it = dominated_nodes->begin(); |
| 398 | dominated_nodes_it != dominated_nodes->end(); dominated_nodes_it++) { |
| 399 | Region* dominated_node = (*dominated_nodes_it); |
| 400 | RenameAsSSA(dominated_node, scoped_table); |
| 401 | } |
| 402 | scoped_table->CloseScope(); |
| 403 | } |
| 404 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 405 | void SeaGraph::GenerateLLVM() { |
| 406 | // Pass: Generate LLVM IR. |
| 407 | CodeGenPrepassVisitor code_gen_prepass_visitor; |
| 408 | std::cout << "Generating code..." << std::endl; |
| 409 | std::cout << "=== PRE VISITING ===" << std::endl; |
| 410 | Accept(&code_gen_prepass_visitor); |
| 411 | CodeGenVisitor code_gen_visitor(code_gen_prepass_visitor.GetData()); |
| 412 | std::cout << "=== VISITING ===" << std::endl; |
| 413 | Accept(&code_gen_visitor); |
| 414 | std::cout << "=== POST VISITING ===" << std::endl; |
| 415 | CodeGenPostpassVisitor code_gen_postpass_visitor(code_gen_visitor.GetData()); |
| 416 | Accept(&code_gen_postpass_visitor); |
| 417 | code_gen_postpass_visitor.Write(std::string("my_file.llvm")); |
| 418 | } |
| 419 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 420 | void SeaGraph::CompileMethod(const art::DexFile::CodeItem* code_item, |
| 421 | uint32_t class_def_idx, uint32_t method_idx, const art::DexFile& dex_file) { |
| 422 | // Two passes: Builds the intermediate structure (non-SSA) of the sea-ir for the function. |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 423 | BuildMethodSeaGraph(code_item, dex_file, class_def_idx, method_idx); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 424 | //Pass: Compute reverse post-order of regions. |
| 425 | ComputeRPO(); |
| 426 | // Multiple passes: compute immediate dominators. |
| 427 | ComputeIDominators(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 428 | // Pass: compute downward-exposed definitions. |
| 429 | ComputeDownExposedDefs(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 430 | // Multiple Passes (iterative fixed-point algorithm): Compute reaching definitions |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 431 | ComputeReachingDefs(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 432 | // Pass (O(nlogN)): Compute the dominance frontier for region nodes. |
| 433 | ComputeDominanceFrontier(); |
| 434 | // Two Passes: Phi node insertion. |
| 435 | ConvertToSSA(); |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 436 | // Pass: Generate LLVM IR. |
| 437 | GenerateLLVM(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 440 | void SeaGraph::ComputeDominanceFrontier() { |
| 441 | for (std::vector<Region*>::iterator region_it = regions_.begin(); |
| 442 | region_it != regions_.end(); region_it++) { |
| 443 | std::vector<Region*>* preds = (*region_it)->GetPredecessors(); |
| 444 | if (preds->size() > 1) { |
| 445 | for (std::vector<Region*>::iterator pred_it = preds->begin(); |
| 446 | pred_it != preds->end(); pred_it++) { |
| 447 | Region* runner = *pred_it; |
| 448 | while (runner != (*region_it)->GetIDominator()) { |
| 449 | runner->AddToDominanceFrontier(*region_it); |
| 450 | runner = runner->GetIDominator(); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | Region* SeaGraph::GetNewRegion() { |
| 458 | Region* new_region = new Region(); |
| 459 | AddRegion(new_region); |
| 460 | return new_region; |
| 461 | } |
| 462 | |
| 463 | void SeaGraph::AddRegion(Region* r) { |
| 464 | DCHECK(r) << "Tried to add NULL region to SEA graph."; |
| 465 | regions_.push_back(r); |
| 466 | } |
| 467 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 468 | /* |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 469 | void SeaNode::AddSuccessor(Region* successor) { |
| 470 | DCHECK(successor) << "Tried to add NULL successor to SEA node."; |
| 471 | successors_.push_back(successor); |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | void SeaNode::AddPredecessor(Region* predecessor) { |
| 476 | DCHECK(predecessor) << "Tried to add NULL predecessor to SEA node."; |
| 477 | predecessors_.push_back(predecessor); |
| 478 | } |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 479 | */ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 480 | void Region::AddChild(sea_ir::InstructionNode* instruction) { |
| 481 | DCHECK(instruction) << "Tried to add NULL instruction to region node."; |
| 482 | instructions_.push_back(instruction); |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 483 | instruction->SetRegion(this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | SeaNode* Region::GetLastChild() const { |
| 487 | if (instructions_.size() > 0) { |
| 488 | return instructions_.back(); |
| 489 | } |
| 490 | return NULL; |
| 491 | } |
| 492 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 493 | void Region::ToDot(std::string& result) const { |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 494 | result += "\n// Region: \n" + StringId() + " [label=\"region " + StringId() + "(rpo="; |
Dragos Sbirlea | db06306 | 2013-07-23 16:29:09 -0700 | [diff] [blame^] | 495 | result += art::StringPrintf("%d", rpo_number_); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 496 | if (NULL != GetIDominator()) { |
| 497 | result += " dom=" + GetIDominator()->StringId(); |
| 498 | } |
| 499 | result += ")\"];\n"; |
| 500 | |
| 501 | // Save phi-nodes. |
| 502 | for (std::vector<PhiInstructionNode*>::const_iterator cit = phi_instructions_.begin(); |
| 503 | cit != phi_instructions_.end(); cit++) { |
| 504 | (*cit)->ToDot(result); |
| 505 | result += StringId() + " -> " + (*cit)->StringId() + "; // phi-function \n"; |
| 506 | } |
| 507 | |
| 508 | // Save instruction nodes. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 509 | for (std::vector<InstructionNode*>::const_iterator cit = instructions_.begin(); |
| 510 | cit != instructions_.end(); cit++) { |
| 511 | (*cit)->ToDot(result); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 512 | result += StringId() + " -> " + (*cit)->StringId() + "; // region -> instruction \n"; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | for (std::vector<Region*>::const_iterator cit = successors_.begin(); cit != successors_.end(); |
| 516 | cit++) { |
| 517 | DCHECK(NULL != *cit) << "Null successor found for SeaNode" << GetLastChild()->StringId() << "."; |
| 518 | result += GetLastChild()->StringId() + " -> " + (*cit)->StringId() + ";\n\n"; |
| 519 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 520 | // Save reaching definitions. |
| 521 | for (std::map<int, std::set<sea_ir::InstructionNode*>* >::const_iterator cit = |
| 522 | reaching_defs_.begin(); |
| 523 | cit != reaching_defs_.end(); cit++) { |
| 524 | for (std::set<sea_ir::InstructionNode*>::const_iterator |
| 525 | reaching_set_it = (*cit).second->begin(); |
| 526 | reaching_set_it != (*cit).second->end(); |
| 527 | reaching_set_it++) { |
| 528 | result += (*reaching_set_it)->StringId() + |
| 529 | " -> " + StringId() + |
| 530 | " [style=dotted]; // Reaching def.\n"; |
| 531 | } |
| 532 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 533 | // Save dominance frontier. |
| 534 | for (std::set<Region*>::const_iterator cit = df_.begin(); cit != df_.end(); cit++) { |
| 535 | result += StringId() + |
| 536 | " -> " + (*cit)->StringId() + |
| 537 | " [color=gray]; // Dominance frontier.\n"; |
| 538 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 539 | result += "// End Region.\n"; |
| 540 | } |
| 541 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 542 | void Region::ComputeDownExposedDefs() { |
| 543 | for (std::vector<InstructionNode*>::const_iterator inst_it = instructions_.begin(); |
| 544 | inst_it != instructions_.end(); inst_it++) { |
| 545 | int reg_no = (*inst_it)->GetResultRegister(); |
| 546 | std::map<int, InstructionNode*>::iterator res = de_defs_.find(reg_no); |
| 547 | if ((reg_no != NO_REGISTER) && (res == de_defs_.end())) { |
| 548 | de_defs_.insert(std::pair<int, InstructionNode*>(reg_no, *inst_it)); |
| 549 | } else { |
| 550 | res->second = *inst_it; |
| 551 | } |
| 552 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 553 | for (std::map<int, sea_ir::InstructionNode*>::const_iterator cit = de_defs_.begin(); |
| 554 | cit != de_defs_.end(); cit++) { |
| 555 | (*cit).second->MarkAsDEDef(); |
| 556 | } |
| 557 | } |
| 558 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 559 | const std::map<int, sea_ir::InstructionNode*>* Region::GetDownExposedDefs() const { |
| 560 | return &de_defs_; |
| 561 | } |
| 562 | |
| 563 | std::map<int, std::set<sea_ir::InstructionNode*>* >* Region::GetReachingDefs() { |
| 564 | return &reaching_defs_; |
| 565 | } |
| 566 | |
| 567 | bool Region::UpdateReachingDefs() { |
| 568 | std::map<int, std::set<sea_ir::InstructionNode*>* > new_reaching; |
| 569 | for (std::vector<Region*>::const_iterator pred_it = predecessors_.begin(); |
| 570 | pred_it != predecessors_.end(); pred_it++) { |
| 571 | // The reaching_defs variable will contain reaching defs __for current predecessor only__ |
| 572 | std::map<int, std::set<sea_ir::InstructionNode*>* > reaching_defs; |
| 573 | std::map<int, std::set<sea_ir::InstructionNode*>* >* pred_reaching = (*pred_it)->GetReachingDefs(); |
| 574 | const std::map<int, InstructionNode*>* de_defs = (*pred_it)->GetDownExposedDefs(); |
| 575 | |
| 576 | // The definitions from the reaching set of the predecessor |
| 577 | // may be shadowed by downward exposed definitions from the predecessor, |
| 578 | // otherwise the defs from the reaching set are still good. |
| 579 | for (std::map<int, InstructionNode*>::const_iterator de_def = de_defs->begin(); |
| 580 | de_def != de_defs->end(); de_def++) { |
| 581 | std::set<InstructionNode*>* solo_def; |
| 582 | solo_def = new std::set<InstructionNode*>(); |
| 583 | solo_def->insert(de_def->second); |
| 584 | reaching_defs.insert( |
| 585 | std::pair<int const, std::set<InstructionNode*>*>(de_def->first, solo_def)); |
| 586 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 587 | reaching_defs.insert(pred_reaching->begin(), pred_reaching->end()); |
| 588 | |
| 589 | // Now we combine the reaching map coming from the current predecessor (reaching_defs) |
| 590 | // with the accumulated set from all predecessors so far (from new_reaching). |
| 591 | std::map<int, std::set<sea_ir::InstructionNode*>*>::iterator reaching_it = reaching_defs.begin(); |
| 592 | for (; reaching_it != reaching_defs.end(); reaching_it++) { |
| 593 | std::map<int, std::set<sea_ir::InstructionNode*>*>::iterator crt_entry = |
| 594 | new_reaching.find(reaching_it->first); |
| 595 | if (new_reaching.end() != crt_entry) { |
| 596 | crt_entry->second->insert(reaching_it->second->begin(), reaching_it->second->end()); |
| 597 | } else { |
| 598 | new_reaching.insert( |
| 599 | std::pair<int, std::set<sea_ir::InstructionNode*>*>( |
| 600 | reaching_it->first, |
| 601 | reaching_it->second) ); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | bool changed = false; |
| 606 | // Because the sets are monotonically increasing, |
| 607 | // we can compare sizes instead of using set comparison. |
| 608 | // TODO: Find formal proof. |
| 609 | int old_size = 0; |
| 610 | if (-1 == reaching_defs_size_) { |
| 611 | std::map<int, std::set<sea_ir::InstructionNode*>*>::iterator reaching_it = reaching_defs_.begin(); |
| 612 | for (; reaching_it != reaching_defs_.end(); reaching_it++) { |
| 613 | old_size += (*reaching_it).second->size(); |
| 614 | } |
| 615 | } else { |
| 616 | old_size = reaching_defs_size_; |
| 617 | } |
| 618 | int new_size = 0; |
| 619 | std::map<int, std::set<sea_ir::InstructionNode*>*>::iterator reaching_it = new_reaching.begin(); |
| 620 | for (; reaching_it != new_reaching.end(); reaching_it++) { |
| 621 | new_size += (*reaching_it).second->size(); |
| 622 | } |
| 623 | if (old_size != new_size) { |
| 624 | changed = true; |
| 625 | } |
| 626 | if (changed) { |
| 627 | reaching_defs_ = new_reaching; |
| 628 | reaching_defs_size_ = new_size; |
| 629 | } |
| 630 | return changed; |
| 631 | } |
| 632 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 633 | bool Region::InsertPhiFor(int reg_no) { |
| 634 | if (!ContainsPhiFor(reg_no)) { |
| 635 | phi_set_.insert(reg_no); |
| 636 | PhiInstructionNode* new_phi = new PhiInstructionNode(reg_no); |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 637 | new_phi->SetRegion(this); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 638 | phi_instructions_.push_back(new_phi); |
| 639 | return true; |
| 640 | } |
| 641 | return false; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 644 | void Region::SetPhiDefinitionsForUses( |
| 645 | const utils::ScopedHashtable<int, InstructionNode*>* scoped_table, Region* predecessor) { |
| 646 | int predecessor_id = -1; |
| 647 | for (unsigned int crt_pred_id = 0; crt_pred_id < predecessors_.size(); crt_pred_id++) { |
| 648 | if (predecessors_.at(crt_pred_id) == predecessor) { |
| 649 | predecessor_id = crt_pred_id; |
| 650 | } |
| 651 | } |
| 652 | DCHECK_NE(-1, predecessor_id); |
| 653 | for (std::vector<PhiInstructionNode*>::iterator phi_it = phi_instructions_.begin(); |
| 654 | phi_it != phi_instructions_.end(); phi_it++) { |
| 655 | PhiInstructionNode* phi = (*phi_it); |
| 656 | int reg_no = phi->GetRegisterNumber(); |
| 657 | InstructionNode* definition = scoped_table->Lookup(reg_no); |
| 658 | phi->RenameToSSA(reg_no, definition, predecessor_id); |
| 659 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 662 | std::vector<InstructionNode*> InstructionNode::Create(const art::Instruction* in) { |
| 663 | std::vector<InstructionNode*> sea_instructions; |
| 664 | switch (in->Opcode()) { |
| 665 | case art::Instruction::CONST_4: |
| 666 | sea_instructions.push_back(new ConstInstructionNode(in)); |
| 667 | break; |
| 668 | case art::Instruction::RETURN: |
| 669 | sea_instructions.push_back(new ReturnInstructionNode(in)); |
| 670 | break; |
| 671 | case art::Instruction::IF_NE: |
| 672 | sea_instructions.push_back(new IfNeInstructionNode(in)); |
| 673 | break; |
| 674 | case art::Instruction::ADD_INT_LIT8: |
| 675 | sea_instructions.push_back(new UnnamedConstInstructionNode(in, in->VRegB_22b())); |
| 676 | sea_instructions.push_back(new AddIntLitInstructionNode(in)); |
| 677 | break; |
| 678 | case art::Instruction::MOVE_RESULT: |
| 679 | sea_instructions.push_back(new MoveResultInstructionNode(in)); |
| 680 | break; |
| 681 | case art::Instruction::INVOKE_STATIC: |
| 682 | sea_instructions.push_back(new InvokeStaticInstructionNode(in)); |
| 683 | break; |
| 684 | case art::Instruction::ADD_INT: |
| 685 | sea_instructions.push_back(new AddIntInstructionNode(in)); |
| 686 | break; |
| 687 | case art::Instruction::GOTO: |
| 688 | sea_instructions.push_back(new GotoInstructionNode(in)); |
| 689 | break; |
| 690 | case art::Instruction::IF_EQZ: |
| 691 | sea_instructions.push_back(new IfEqzInstructionNode(in)); |
| 692 | break; |
| 693 | default: |
| 694 | // Default, generic IR instruction node; default case should never be reached |
| 695 | // when support for all instructions ahs been added. |
| 696 | sea_instructions.push_back(new InstructionNode(in)); |
| 697 | } |
| 698 | return sea_instructions; |
| 699 | } |
| 700 | |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 701 | void InstructionNode::ToDot(std::string& result) const { |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 702 | result += "// Instruction ("+StringId()+"): \n" + StringId() + |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 703 | " [label=\"" + instruction_->DumpString(NULL) + "\""; |
| 704 | if (de_def_) { |
| 705 | result += "style=bold"; |
| 706 | } |
| 707 | result += "];\n"; |
| 708 | // SSA definitions: |
| 709 | for (std::map<int, InstructionNode* >::const_iterator def_it = definition_edges_.begin(); |
| 710 | def_it != definition_edges_.end(); def_it++) { |
| 711 | if (NULL != def_it->second) { |
| 712 | result += def_it->second->StringId() + " -> " + StringId() +"[color=red,label=\""; |
Dragos Sbirlea | db06306 | 2013-07-23 16:29:09 -0700 | [diff] [blame^] | 713 | result += art::StringPrintf("%d", def_it->first); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 714 | result += "\"] ; // ssa edge\n"; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | void InstructionNode::MarkAsDEDef() { |
| 720 | de_def_ = true; |
| 721 | } |
| 722 | |
| 723 | int InstructionNode::GetResultRegister() const { |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 724 | if (instruction_->HasVRegA() && InstructionTools::IsDefinition(instruction_)) { |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 725 | return instruction_->VRegA(); |
| 726 | } |
| 727 | return NO_REGISTER; |
| 728 | } |
| 729 | |
| 730 | std::vector<int> InstructionNode::GetDefinitions() const { |
| 731 | // TODO: Extend this to handle instructions defining more than one register (if any) |
| 732 | // The return value should be changed to pointer to field then; for now it is an object |
| 733 | // so that we avoid possible memory leaks from allocating objects dynamically. |
| 734 | std::vector<int> definitions; |
| 735 | int result = GetResultRegister(); |
| 736 | if (NO_REGISTER != result) { |
| 737 | definitions.push_back(result); |
| 738 | } |
| 739 | return definitions; |
| 740 | } |
| 741 | |
| 742 | std::vector<int> InstructionNode::GetUses() { |
| 743 | std::vector<int> uses; // Using vector<> instead of set<> because order matters. |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 744 | if (!InstructionTools::IsDefinition(instruction_) && (instruction_->HasVRegA())) { |
| 745 | int vA = instruction_->VRegA(); |
| 746 | uses.push_back(vA); |
| 747 | } |
| 748 | if (instruction_->HasVRegB()) { |
| 749 | int vB = instruction_->VRegB(); |
| 750 | uses.push_back(vB); |
| 751 | } |
| 752 | if (instruction_->HasVRegC()) { |
| 753 | int vC = instruction_->VRegC(); |
| 754 | uses.push_back(vC); |
| 755 | } |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 756 | return uses; |
| 757 | } |
| 758 | |
| 759 | void PhiInstructionNode::ToDot(std::string& result) const { |
| 760 | result += "// PhiInstruction: \n" + StringId() + |
| 761 | " [label=\"" + "PHI("; |
Dragos Sbirlea | 81f79a6 | 2013-07-23 14:31:47 -0700 | [diff] [blame] | 762 | result += art::StringPrintf("%d", register_no_); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 763 | result += ")\""; |
| 764 | result += "];\n"; |
| 765 | |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 766 | for (std::vector<std::vector<InstructionNode*>*>::const_iterator pred_it = definition_edges_.begin(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 767 | pred_it != definition_edges_.end(); pred_it++) { |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 768 | std::vector<InstructionNode*>* defs_from_pred = *pred_it; |
| 769 | for (std::vector<InstructionNode* >::const_iterator def_it = defs_from_pred->begin(); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 770 | def_it != defs_from_pred->end(); def_it++) { |
Dragos Sbirlea | 0e260a3 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 771 | result += (*def_it)->StringId() + " -> " + StringId() +"[color=red,label=\"vR = "; |
Dragos Sbirlea | db06306 | 2013-07-23 16:29:09 -0700 | [diff] [blame^] | 772 | result += art::StringPrintf("%d", GetRegisterNumber()); |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 773 | result += "\"] ; // phi-ssa edge\n"; |
Brian Carlstrom | 1db9113 | 2013-07-12 18:05:20 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 777 | } // end namespace sea_ir |