buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 17 | #include "compiler_internals.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 18 | #include "dataflow_iterator.h" |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 19 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 20 | #define NOTVISITED (-1) |
| 21 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 22 | namespace art { |
| 23 | |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame^] | 24 | void MIRGraph::ClearAllVisitedFlags() { |
| 25 | AllNodesIterator iter(this, false /* not iterative */); |
| 26 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 27 | bb->visited = false; |
| 28 | } |
| 29 | } |
| 30 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 31 | BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) { |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 32 | if (bb != NULL) { |
| 33 | if (bb->visited || bb->hidden) { |
| 34 | bb = NULL; |
| 35 | } |
| 36 | } |
| 37 | return bb; |
| 38 | } |
| 39 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 40 | BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb) |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 41 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 42 | BasicBlock* res = NeedsVisit(bb->fall_through); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 43 | if (res == NULL) { |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 44 | res = NeedsVisit(bb->taken); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 45 | if (res == NULL) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 46 | if (bb->successor_block_list.block_list_type != kNotUsed) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 47 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_block_list.blocks); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 48 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 49 | SuccessorBlockInfo *sbi = iterator.Next(); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 50 | if (sbi == NULL) break; |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 51 | res = NeedsVisit(sbi->block); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 52 | if (res != NULL) break; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | return res; |
| 58 | } |
| 59 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 60 | void MIRGraph::MarkPreOrder(BasicBlock* block) |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 61 | { |
| 62 | block->visited = true; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 63 | /* Enqueue the pre_order block id */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 64 | dfs_order_->Insert(block->id); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 65 | } |
| 66 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 67 | void MIRGraph::RecordDFSOrders(BasicBlock* block) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 68 | { |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 69 | std::vector<BasicBlock*> succ; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 70 | MarkPreOrder(block); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 71 | succ.push_back(block); |
| 72 | while (!succ.empty()) { |
| 73 | BasicBlock* curr = succ.back(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 74 | BasicBlock* next_successor = NextUnvisitedSuccessor(curr); |
| 75 | if (next_successor != NULL) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 76 | MarkPreOrder(next_successor); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 77 | succ.push_back(next_successor); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 78 | continue; |
| 79 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 80 | curr->dfs_id = dfs_post_order_->Size(); |
| 81 | dfs_post_order_->Insert(curr->id); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 82 | succ.pop_back(); |
| 83 | } |
| 84 | } |
| 85 | |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 86 | /* Sort the blocks by the Depth-First-Search */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 87 | void MIRGraph::ComputeDFSOrders() |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 88 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 89 | /* Initialize or reset the DFS pre_order list */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 90 | if (dfs_order_ == NULL) { |
| 91 | dfs_order_ = new (arena_) GrowableArray<int>(arena_, GetNumBlocks(), kGrowableArrayDfsOrder); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 92 | } else { |
| 93 | /* Just reset the used length on the counter */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 94 | dfs_order_->Reset(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 95 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 96 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 97 | /* Initialize or reset the DFS post_order list */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 98 | if (dfs_post_order_ == NULL) { |
| 99 | dfs_post_order_ = new (arena_) GrowableArray<int>(arena_, GetNumBlocks(), kGrowableArrayDfsPostOrder); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 100 | } else { |
| 101 | /* Just reset the used length on the counter */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 102 | dfs_post_order_->Reset(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 103 | } |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 104 | |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 105 | // Reset visited flags from all nodes |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame^] | 106 | ClearAllVisitedFlags(); |
| 107 | |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 108 | // Record dfs orders |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 109 | RecordDFSOrders(GetEntryBlock()); |
buzbee | e5f0122 | 2012-06-14 15:19:35 -0700 | [diff] [blame] | 110 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 111 | num_reachable_blocks_ = dfs_order_->Size(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Mark block bit on the per-Dalvik register vector to denote that Dalvik |
| 116 | * register idx is defined in BasicBlock bb. |
| 117 | */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 118 | bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 119 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 120 | if (bb->data_flow_info == NULL) return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 121 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 122 | ArenaBitVector::Iterator iterator(bb->data_flow_info->def_v); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 123 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 124 | int idx = iterator.Next(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 125 | if (idx == -1) break; |
| 126 | /* Block bb defines register idx */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 127 | def_block_matrix_[idx]->SetBit(bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 128 | } |
| 129 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 130 | } |
| 131 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 132 | void MIRGraph::ComputeDefBlockMatrix() |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 133 | { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 134 | int num_registers = cu_->num_dalvik_registers; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 135 | /* Allocate num_dalvik_registers bit vector pointers */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 136 | def_block_matrix_ = static_cast<ArenaBitVector**> |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 137 | (arena_->NewMem(sizeof(ArenaBitVector *) * num_registers, true, |
| 138 | ArenaAllocator::kAllocDFInfo)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 139 | int i; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 140 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 141 | /* Initialize num_register vectors with num_blocks bits each */ |
| 142 | for (i = 0; i < num_registers; i++) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 143 | def_block_matrix_[i] = |
| 144 | new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapBMatrix); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 145 | } |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 146 | AllNodesIterator iter(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 147 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 148 | FindLocalLiveIn(bb); |
| 149 | } |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 150 | AllNodesIterator iter2(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 151 | for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) { |
| 152 | FillDefBlockMatrix(bb); |
| 153 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 154 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 155 | /* |
| 156 | * Also set the incoming parameters as defs in the entry block. |
| 157 | * Only need to handle the parameters for the outer method. |
| 158 | */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 159 | int num_regs = cu_->num_dalvik_registers; |
| 160 | int in_reg = num_regs - cu_->num_ins; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 161 | for (; in_reg < num_regs; in_reg++) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 162 | def_block_matrix_[in_reg]->SetBit(GetEntryBlock()->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 163 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 164 | } |
| 165 | |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame^] | 166 | void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb) { |
| 167 | if (dom_post_order_traversal_ == NULL) { |
| 168 | // First time - create the array. |
| 169 | dom_post_order_traversal_ = |
| 170 | new (arena_) GrowableArray<int>(arena_, num_reachable_blocks_, |
| 171 | kGrowableArrayDomPostOrderTraversal); |
| 172 | } else { |
| 173 | dom_post_order_traversal_->Reset(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 174 | } |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame^] | 175 | ClearAllVisitedFlags(); |
| 176 | std::vector<std::pair<BasicBlock*, ArenaBitVector::Iterator*> > work_stack; |
| 177 | bb->visited = true; |
| 178 | work_stack.push_back(std::make_pair(bb, new (arena_) ArenaBitVector::Iterator(bb->i_dominated))); |
| 179 | while (!work_stack.empty()) { |
| 180 | std::pair<BasicBlock*, ArenaBitVector::Iterator*> curr = work_stack.back(); |
| 181 | BasicBlock* curr_bb = curr.first; |
| 182 | ArenaBitVector::Iterator* curr_idom_iter = curr.second; |
| 183 | int bb_idx = curr_idom_iter->Next(); |
| 184 | while ((bb_idx != -1) && (NeedsVisit(GetBasicBlock(bb_idx)) == NULL)) { |
| 185 | bb_idx = curr_idom_iter->Next(); |
| 186 | } |
| 187 | if (bb_idx != -1) { |
| 188 | BasicBlock* new_bb = GetBasicBlock(bb_idx); |
| 189 | new_bb->visited = true; |
| 190 | work_stack.push_back( |
| 191 | std::make_pair(new_bb, new (arena_) ArenaBitVector::Iterator(new_bb->i_dominated))); |
| 192 | } else { |
| 193 | // no successor/next |
| 194 | dom_post_order_traversal_->Insert(curr_bb->id); |
| 195 | work_stack.pop_back(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 196 | |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame^] | 197 | /* hacky loop detection */ |
| 198 | if (curr_bb->taken && curr_bb->dominators->IsBitSet(curr_bb->taken->id)) { |
| 199 | attributes_ |= METHOD_HAS_LOOP; |
| 200 | } |
| 201 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 202 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 203 | } |
| 204 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 205 | void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb, |
| 206 | const BasicBlock* succ_bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 207 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 208 | /* |
| 209 | * TODO - evaluate whether phi will ever need to be inserted into exit |
| 210 | * blocks. |
| 211 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 212 | if (succ_bb->i_dom != dom_bb && |
| 213 | succ_bb->block_type == kDalvikByteCode && |
| 214 | succ_bb->hidden == false) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 215 | dom_bb->dom_frontier->SetBit(succ_bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 216 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* Worker function to compute the dominance frontier */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 220 | bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 221 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 222 | /* Calculate DF_local */ |
| 223 | if (bb->taken) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 224 | CheckForDominanceFrontier(bb, bb->taken); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 225 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 226 | if (bb->fall_through) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 227 | CheckForDominanceFrontier(bb, bb->fall_through); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 228 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 229 | if (bb->successor_block_list.block_list_type != kNotUsed) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 230 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_block_list.blocks); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 231 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 232 | SuccessorBlockInfo *successor_block_info = iterator.Next(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 233 | if (successor_block_info == NULL) break; |
| 234 | BasicBlock* succ_bb = successor_block_info->block; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 235 | CheckForDominanceFrontier(bb, succ_bb); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 238 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 239 | /* Calculate DF_up */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 240 | ArenaBitVector::Iterator bv_iterator(bb->i_dominated); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 241 | while (true) { |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 242 | //TUNING: hot call to BitVectorIteratorNext |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 243 | int dominated_idx = bv_iterator.Next(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 244 | if (dominated_idx == -1) break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 245 | BasicBlock* dominated_bb = GetBasicBlock(dominated_idx); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 246 | ArenaBitVector::Iterator df_iterator(dominated_bb->dom_frontier); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 247 | while (true) { |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 248 | //TUNING: hot call to BitVectorIteratorNext |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 249 | int df_up_idx = df_iterator.Next(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 250 | if (df_up_idx == -1) break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 251 | BasicBlock* df_up_block = GetBasicBlock(df_up_idx); |
| 252 | CheckForDominanceFrontier(bb, df_up_block); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 253 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 254 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 255 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 256 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* Worker function for initializing domination-related data structures */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 260 | void MIRGraph::InitializeDominationInfo(BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 261 | { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 262 | int num_total_blocks = GetBasicBlockListCount(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 263 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 264 | if (bb->dominators == NULL ) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 265 | bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks, |
| 266 | false /* expandable */, kBitMapDominators); |
| 267 | bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks, |
| 268 | false /* expandable */, kBitMapIDominated); |
| 269 | bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks, |
| 270 | false /* expandable */, kBitMapDomFrontier); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 271 | } else { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 272 | bb->dominators->ClearAllBits(); |
| 273 | bb->i_dominated->ClearAllBits(); |
| 274 | bb->dom_frontier->ClearAllBits(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 275 | } |
| 276 | /* Set all bits in the dominator vector */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 277 | bb->dominators->SetInitialBits(num_total_blocks); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 278 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 279 | return; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 280 | } |
| 281 | |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 282 | /* |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 283 | * Walk through the ordered i_dom list until we reach common parent. |
| 284 | * Given the ordering of i_dom_list, this common parent represents the |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 285 | * last element of the intersection of block1 and block2 dominators. |
| 286 | */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 287 | int MIRGraph::FindCommonParent(int block1, int block2) |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 288 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 289 | while (block1 != block2) { |
| 290 | while (block1 < block2) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 291 | block1 = i_dom_list_[block1]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 292 | DCHECK_NE(block1, NOTVISITED); |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 293 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 294 | while (block2 < block1) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 295 | block2 = i_dom_list_[block2]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 296 | DCHECK_NE(block2, NOTVISITED); |
| 297 | } |
| 298 | } |
| 299 | return block1; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* Worker function to compute each block's immediate dominator */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 303 | bool MIRGraph::ComputeblockIDom(BasicBlock* bb) |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 304 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 305 | /* Special-case entry block */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 306 | if (bb == GetEntryBlock()) { |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 307 | return false; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* Iterate through the predecessors */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 311 | GrowableArray<BasicBlock*>::Iterator iter(bb->predecessors); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 312 | |
| 313 | /* Find the first processed predecessor */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 314 | int idom = -1; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 315 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 316 | BasicBlock* pred_bb = iter.Next(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 317 | CHECK(pred_bb != NULL); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 318 | if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 319 | idom = pred_bb->dfs_id; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /* Scan the rest of the predecessors */ |
| 325 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 326 | BasicBlock* pred_bb = iter.Next(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 327 | if (!pred_bb) break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 328 | if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 329 | continue; |
| 330 | } else { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 331 | idom = FindCommonParent(pred_bb->dfs_id, idom); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | DCHECK_NE(idom, NOTVISITED); |
| 336 | |
| 337 | /* Did something change? */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 338 | if (i_dom_list_[bb->dfs_id] != idom) { |
| 339 | i_dom_list_[bb->dfs_id] = idom; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | return false; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* Worker function to compute each block's domintors */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 346 | bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 347 | { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 348 | if (bb == GetEntryBlock()) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 349 | bb->dominators->ClearAllBits(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 350 | } else { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 351 | bb->dominators->Copy(bb->i_dom->dominators); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 352 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 353 | bb->dominators->SetBit(bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 354 | return false; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 355 | } |
| 356 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 357 | bool MIRGraph::SetDominators(BasicBlock* bb) |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 358 | { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 359 | if (bb != GetEntryBlock()) { |
| 360 | int idom_dfs_idx = i_dom_list_[bb->dfs_id]; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 361 | DCHECK_NE(idom_dfs_idx, NOTVISITED); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 362 | int i_dom_idx = dfs_post_order_->Get(idom_dfs_idx); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 363 | BasicBlock* i_dom = GetBasicBlock(i_dom_idx); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 364 | bb->i_dom = i_dom; |
| 365 | /* Add bb to the i_dominated set of the immediate dominator block */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 366 | i_dom->i_dominated->SetBit(bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 367 | } |
| 368 | return false; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 369 | } |
| 370 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 371 | /* Compute dominators, immediate dominator, and dominance fronter */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 372 | void MIRGraph::ComputeDominators() |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 373 | { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 374 | int num_reachable_blocks = num_reachable_blocks_; |
| 375 | int num_total_blocks = GetBasicBlockListCount(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 376 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 377 | /* Initialize domination-related data structures */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 378 | ReachableNodesIterator iter(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 379 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 380 | InitializeDominationInfo(bb); |
| 381 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 382 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 383 | /* Initalize & Clear i_dom_list */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 384 | if (i_dom_list_ == NULL) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 385 | i_dom_list_ = static_cast<int*>(arena_->NewMem(sizeof(int) * num_reachable_blocks, false, |
| 386 | ArenaAllocator::kAllocDFInfo)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 387 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 388 | for (int i = 0; i < num_reachable_blocks; i++) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 389 | i_dom_list_[i] = NOTVISITED; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 390 | } |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 391 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 392 | /* For post-order, last block is entry block. Set its i_dom to istelf */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 393 | DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1); |
| 394 | i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 395 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 396 | /* Compute the immediate dominators */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 397 | ReversePostOrderDfsIterator iter2(this, true /* iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 398 | bool change = false; |
| 399 | for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) { |
| 400 | change = ComputeblockIDom(bb); |
| 401 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 402 | |
| 403 | /* Set the dominator for the root node */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 404 | GetEntryBlock()->dominators->ClearAllBits(); |
| 405 | GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 406 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 407 | if (temp_block_v_ == NULL) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 408 | temp_block_v_ = new (arena_) ArenaBitVector(arena_, num_total_blocks, |
| 409 | false /* expandable */, kBitMapTmpBlockV); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 410 | } else { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 411 | temp_block_v_->ClearAllBits(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 412 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 413 | GetEntryBlock()->i_dom = NULL; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 414 | |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 415 | ReachableNodesIterator iter3(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 416 | for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) { |
| 417 | SetDominators(bb); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 418 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 419 | |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 420 | ReversePostOrderDfsIterator iter4(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 421 | for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) { |
| 422 | ComputeBlockDominators(bb); |
| 423 | } |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 424 | |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame^] | 425 | // Compute the dominance frontier for each block. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 426 | ComputeDomPostOrderTraversal(GetEntryBlock()); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 427 | PostOrderDOMIterator iter5(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 428 | for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) { |
| 429 | ComputeDominanceFrontier(bb); |
| 430 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Perform dest U= src1 ^ ~src2 |
| 435 | * This is probably not general enough to be placed in BitVector.[ch]. |
| 436 | */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 437 | void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1, |
| 438 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 439 | { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 440 | if (dest->GetStorageSize() != src1->GetStorageSize() || |
| 441 | dest->GetStorageSize() != src2->GetStorageSize() || |
| 442 | dest->IsExpandable() != src1->IsExpandable() || |
| 443 | dest->IsExpandable() != src2->IsExpandable()) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 444 | LOG(FATAL) << "Incompatible set properties"; |
| 445 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 446 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 447 | unsigned int idx; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 448 | for (idx = 0; idx < dest->GetStorageSize(); idx++) { |
| 449 | dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 450 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Iterate through all successor blocks and propagate up the live-in sets. |
| 455 | * The calculated result is used for phi-node pruning - where we only need to |
| 456 | * insert a phi node if the variable is live-in to the block. |
| 457 | */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 458 | bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 459 | { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 460 | ArenaBitVector* temp_dalvik_register_v = temp_dalvik_register_v_; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 461 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 462 | if (bb->data_flow_info == NULL) return false; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 463 | temp_dalvik_register_v->Copy(bb->data_flow_info->live_in_v); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 464 | if (bb->taken && bb->taken->data_flow_info) |
| 465 | ComputeSuccLineIn(temp_dalvik_register_v, bb->taken->data_flow_info->live_in_v, |
| 466 | bb->data_flow_info->def_v); |
| 467 | if (bb->fall_through && bb->fall_through->data_flow_info) |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 468 | ComputeSuccLineIn(temp_dalvik_register_v, bb->fall_through->data_flow_info->live_in_v, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 469 | bb->data_flow_info->def_v); |
| 470 | if (bb->successor_block_list.block_list_type != kNotUsed) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 471 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_block_list.blocks); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 472 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 473 | SuccessorBlockInfo *successor_block_info = iterator.Next(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 474 | if (successor_block_info == NULL) break; |
| 475 | BasicBlock* succ_bb = successor_block_info->block; |
| 476 | if (succ_bb->data_flow_info) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 477 | ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 478 | bb->data_flow_info->def_v); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 479 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 480 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 481 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 482 | if (!temp_dalvik_register_v->Equal(bb->data_flow_info->live_in_v)) { |
| 483 | bb->data_flow_info->live_in_v->Copy(temp_dalvik_register_v); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* Insert phi nodes to for each variable to the dominance frontiers */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 490 | void MIRGraph::InsertPhiNodes() |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 491 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 492 | int dalvik_reg; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 493 | ArenaBitVector* phi_blocks = |
| 494 | new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapPhi); |
| 495 | ArenaBitVector* tmp_blocks = |
| 496 | new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapTmpBlocks); |
| 497 | ArenaBitVector* input_blocks = |
| 498 | new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapInputBlocks); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 499 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 500 | temp_dalvik_register_v_ = |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 501 | new (arena_) ArenaBitVector(arena_, cu_->num_dalvik_registers, false, kBitMapRegisterV); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 502 | |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 503 | PostOrderDfsIterator iter(this, true /* iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 504 | bool change = false; |
| 505 | for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) { |
| 506 | change = ComputeBlockLiveIns(bb); |
| 507 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 508 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 509 | /* Iterate through each Dalvik register */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 510 | for (dalvik_reg = cu_->num_dalvik_registers - 1; dalvik_reg >= 0; dalvik_reg--) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 511 | bool change; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 512 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 513 | input_blocks->Copy(def_block_matrix_[dalvik_reg]); |
| 514 | phi_blocks->ClearAllBits(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 515 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 516 | /* Calculate the phi blocks for each Dalvik register */ |
| 517 | do { |
| 518 | change = false; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 519 | tmp_blocks->ClearAllBits(); |
| 520 | ArenaBitVector::Iterator iterator(input_blocks); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 521 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 522 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 523 | int idx = iterator.Next(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 524 | if (idx == -1) break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 525 | BasicBlock* def_bb = GetBasicBlock(idx); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 526 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 527 | /* Merge the dominance frontier to tmp_blocks */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 528 | //TUNING: hot call to Union(). |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 529 | if (def_bb->dom_frontier != NULL) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 530 | tmp_blocks->Union(def_bb->dom_frontier); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 531 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 532 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 533 | if (!phi_blocks->Equal(tmp_blocks)) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 534 | change = true; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 535 | phi_blocks->Copy(tmp_blocks); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Iterate through the original blocks plus the new ones in |
| 539 | * the dominance frontier. |
| 540 | */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 541 | input_blocks->Copy(phi_blocks); |
| 542 | input_blocks->Union(def_block_matrix_[dalvik_reg]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 543 | } |
| 544 | } while (change); |
| 545 | |
| 546 | /* |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 547 | * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 548 | * register is in the live-in set. |
| 549 | */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 550 | ArenaBitVector::Iterator iterator(phi_blocks); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 551 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 552 | int idx = iterator.Next(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 553 | if (idx == -1) break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 554 | BasicBlock* phi_bb = GetBasicBlock(idx); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 555 | /* Variable will be clobbered before being used - no need for phi */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 556 | if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) continue; |
| 557 | MIR *phi = |
| 558 | static_cast<MIR*>(arena_->NewMem(sizeof(MIR), true, ArenaAllocator::kAllocDFInfo)); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 559 | phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 560 | phi->dalvikInsn.vA = dalvik_reg; |
| 561 | phi->offset = phi_bb->start_offset; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 562 | phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 563 | PrependMIR(phi_bb, phi); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 564 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 565 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Worker function to insert phi-operands with latest SSA names from |
| 570 | * predecessor blocks |
| 571 | */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 572 | bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 573 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 574 | MIR *mir; |
buzbee | 6969d50 | 2012-06-15 16:40:31 -0700 | [diff] [blame] | 575 | std::vector<int> uses; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 576 | std::vector<int> incoming_arc; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 577 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 578 | /* Phi nodes are at the beginning of each block */ |
buzbee | 28c9a83 | 2012-11-21 15:39:13 -0800 | [diff] [blame] | 579 | for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 580 | if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi)) |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 581 | return true; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 582 | int ssa_reg = mir->ssa_rep->defs[0]; |
| 583 | DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 584 | int v_reg = SRegToVReg(ssa_reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 585 | |
buzbee | 6969d50 | 2012-06-15 16:40:31 -0700 | [diff] [blame] | 586 | uses.clear(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 587 | incoming_arc.clear(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 588 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 589 | /* Iterate through the predecessors */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 590 | GrowableArray<BasicBlock*>::Iterator iter(bb->predecessors); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 591 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 592 | BasicBlock* pred_bb = iter.Next(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 593 | if (!pred_bb) break; |
| 594 | int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map[v_reg]; |
| 595 | uses.push_back(ssa_reg); |
| 596 | incoming_arc.push_back(pred_bb->id); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 599 | /* Count the number of SSA registers for a Dalvik register */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 600 | int num_uses = uses.size(); |
| 601 | mir->ssa_rep->num_uses = num_uses; |
| 602 | mir->ssa_rep->uses = |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 603 | static_cast<int*>(arena_->NewMem(sizeof(int) * num_uses, false, |
| 604 | ArenaAllocator::kAllocDFInfo)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 605 | mir->ssa_rep->fp_use = |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 606 | static_cast<bool*>(arena_->NewMem(sizeof(bool) * num_uses, true, |
| 607 | ArenaAllocator::kAllocDFInfo)); |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 608 | int* incoming = |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 609 | static_cast<int*>(arena_->NewMem(sizeof(int) * num_uses, false, |
| 610 | ArenaAllocator::kAllocDFInfo)); |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 611 | // TODO: Ugly, rework (but don't burden each MIR/LIR for Phi-only needs) |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 612 | mir->dalvikInsn.vB = reinterpret_cast<uintptr_t>(incoming); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 613 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 614 | /* Set the uses array for the phi node */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 615 | int *use_ptr = mir->ssa_rep->uses; |
| 616 | for (int i = 0; i < num_uses; i++) { |
| 617 | *use_ptr++ = uses[i]; |
| 618 | *incoming++ = incoming_arc[i]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 623 | } |
| 624 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 625 | void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 626 | { |
| 627 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 628 | if (block->visited || block->hidden) return; |
| 629 | block->visited = true; |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 630 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 631 | /* Process this block */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 632 | DoSSAConversion(block); |
| 633 | int map_size = sizeof(int) * cu_->num_dalvik_registers; |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 634 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 635 | /* Save SSA map snapshot */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 636 | int* saved_ssa_map = |
| 637 | static_cast<int*>(arena_->NewMem(map_size, false, ArenaAllocator::kAllocDalvikToSSAMap)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 638 | memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size); |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 639 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 640 | if (block->fall_through) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 641 | DoDFSPreOrderSSARename(block->fall_through); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 642 | /* Restore SSA map snapshot */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 643 | memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 644 | } |
| 645 | if (block->taken) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 646 | DoDFSPreOrderSSARename(block->taken); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 647 | /* Restore SSA map snapshot */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 648 | memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 649 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 650 | if (block->successor_block_list.block_list_type != kNotUsed) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 651 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(block->successor_block_list.blocks); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 652 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 653 | SuccessorBlockInfo *successor_block_info = iterator.Next(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 654 | if (successor_block_info == NULL) break; |
| 655 | BasicBlock* succ_bb = successor_block_info->block; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 656 | DoDFSPreOrderSSARename(succ_bb); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 657 | /* Restore SSA map snapshot */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 658 | memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size); |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 659 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 660 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 661 | vreg_to_ssa_map_ = saved_ssa_map; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 662 | return; |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 663 | } |
| 664 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 665 | /* Perform SSA transformation for the whole method */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 666 | void MIRGraph::SSATransformation() |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 667 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 668 | /* Compute the DFS order */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 669 | ComputeDFSOrders(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 670 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 671 | /* Compute the dominator info */ |
| 672 | ComputeDominators(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 673 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 674 | /* Allocate data structures in preparation for SSA conversion */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 675 | CompilerInitializeSSAConversion(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 676 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 677 | /* Find out the "Dalvik reg def x block" relation */ |
| 678 | ComputeDefBlockMatrix(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 679 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 680 | /* Insert phi nodes to dominance frontiers for all variables */ |
| 681 | InsertPhiNodes(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 682 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 683 | /* Rename register names by local defs and phi nodes */ |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame^] | 684 | ClearAllVisitedFlags(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 685 | DoDFSPreOrderSSARename(GetEntryBlock()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 686 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 687 | /* |
| 688 | * Shared temp bit vector used by each block to count the number of defs |
| 689 | * from all the predecessor blocks. |
| 690 | */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 691 | temp_ssa_register_v_ = |
| 692 | new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false, kBitMapTempSSARegisterV); |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 693 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 694 | /* Insert phi-operands with latest SSA names from predecessor blocks */ |
| 695 | ReachableNodesIterator iter2(this, false /* not iterative */); |
| 696 | for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) { |
| 697 | InsertPhiNodeOperands(bb); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 698 | } |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 699 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 700 | if (cu_->enable_debug & (1 << kDebugDumpCFG)) { |
| 701 | DumpCFG("/sdcard/3_post_ssa_cfg/", false); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 702 | } |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 703 | if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) { |
| 704 | VerifyDataflow(); |
| 705 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 706 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 707 | |
| 708 | } // namespace art |