blob: d8341e3444748e85299286e44633d67db63414a3 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
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
buzbeeeaf09bc2012-11-15 14:51:41 -080017#include "compiler_internals.h"
buzbee311ca162013-02-28 15:56:43 -080018#include "dataflow_iterator.h"
buzbee67bf8852011-08-17 17:51:35 -070019
buzbee1fd33462013-03-25 13:40:45 -070020#define NOTVISITED (-1)
21
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080022namespace art {
23
buzbee311ca162013-02-28 15:56:43 -080024BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) {
buzbeee5f01222012-06-14 15:19:35 -070025 if (bb != NULL) {
26 if (bb->visited || bb->hidden) {
27 bb = NULL;
28 }
29 }
30 return bb;
31}
32
buzbee311ca162013-02-28 15:56:43 -080033BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb)
buzbeee5f01222012-06-14 15:19:35 -070034{
buzbeefa57c472012-11-21 12:06:18 -080035 BasicBlock* res = NeedsVisit(bb->fall_through);
buzbeee5f01222012-06-14 15:19:35 -070036 if (res == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -080037 res = NeedsVisit(bb->taken);
buzbeee5f01222012-06-14 15:19:35 -070038 if (res == NULL) {
buzbeefa57c472012-11-21 12:06:18 -080039 if (bb->successor_block_list.block_list_type != kNotUsed) {
buzbeee5f01222012-06-14 15:19:35 -070040 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -080041 GrowableListIteratorInit(&bb->successor_block_list.blocks,
buzbeee5f01222012-06-14 15:19:35 -070042 &iterator);
43 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -080044 SuccessorBlockInfo *sbi = reinterpret_cast<SuccessorBlockInfo*>
buzbee52a77fc2012-11-20 19:50:46 -080045 (GrowableListIteratorNext(&iterator));
buzbeee5f01222012-06-14 15:19:35 -070046 if (sbi == NULL) break;
buzbee52a77fc2012-11-20 19:50:46 -080047 res = NeedsVisit(sbi->block);
buzbeee5f01222012-06-14 15:19:35 -070048 if (res != NULL) break;
49 }
50 }
51 }
52 }
53 return res;
54}
55
buzbee311ca162013-02-28 15:56:43 -080056void MIRGraph::MarkPreOrder(BasicBlock* block)
buzbeee5f01222012-06-14 15:19:35 -070057{
58 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080059 /* Enqueue the pre_order block id */
buzbee311ca162013-02-28 15:56:43 -080060 InsertGrowableList(cu_, &dfs_order_, block->id);
buzbeee5f01222012-06-14 15:19:35 -070061}
62
buzbee311ca162013-02-28 15:56:43 -080063void MIRGraph::RecordDFSOrders(BasicBlock* block)
buzbee67bf8852011-08-17 17:51:35 -070064{
buzbeee5f01222012-06-14 15:19:35 -070065 std::vector<BasicBlock*> succ;
buzbee311ca162013-02-28 15:56:43 -080066 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070067 succ.push_back(block);
68 while (!succ.empty()) {
69 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080070 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
71 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080072 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080073 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070074 continue;
75 }
buzbee311ca162013-02-28 15:56:43 -080076 curr->dfs_id = dfs_post_order_.num_used;
77 InsertGrowableList(cu_, &dfs_post_order_, curr->id);
buzbeee5f01222012-06-14 15:19:35 -070078 succ.pop_back();
79 }
80}
81
buzbee5b537102012-01-17 17:33:47 -080082/* Sort the blocks by the Depth-First-Search */
buzbee311ca162013-02-28 15:56:43 -080083void MIRGraph::ComputeDFSOrders()
buzbee67bf8852011-08-17 17:51:35 -070084{
buzbeefa57c472012-11-21 12:06:18 -080085 /* Initialize or reset the DFS pre_order list */
buzbee311ca162013-02-28 15:56:43 -080086 if (dfs_order_.elem_list == NULL) {
87 CompilerInitGrowableList(cu_, &dfs_order_, GetNumBlocks(), kListDfsOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -070088 } else {
89 /* Just reset the used length on the counter */
buzbee311ca162013-02-28 15:56:43 -080090 dfs_order_.num_used = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -070091 }
buzbee67bf8852011-08-17 17:51:35 -070092
buzbeefa57c472012-11-21 12:06:18 -080093 /* Initialize or reset the DFS post_order list */
buzbee311ca162013-02-28 15:56:43 -080094 if (dfs_post_order_.elem_list == NULL) {
95 CompilerInitGrowableList(cu_, &dfs_post_order_, GetNumBlocks(), kListDfsPostOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -070096 } else {
97 /* Just reset the used length on the counter */
buzbee311ca162013-02-28 15:56:43 -080098 dfs_post_order_.num_used = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -070099 }
buzbee5b537102012-01-17 17:33:47 -0800100
buzbeee5f01222012-06-14 15:19:35 -0700101 // Reset visited flags from all nodes
buzbee0665fe02013-03-21 12:32:21 -0700102 AllNodesIterator iter(this, false /* not iterative */);
buzbee311ca162013-02-28 15:56:43 -0800103 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
104 ClearVisitedFlag(bb);
105 }
buzbeee5f01222012-06-14 15:19:35 -0700106 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800107 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700108
buzbee311ca162013-02-28 15:56:43 -0800109 num_reachable_blocks_ = dfs_order_.num_used;
buzbee67bf8852011-08-17 17:51:35 -0700110}
111
112/*
113 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
114 * register idx is defined in BasicBlock bb.
115 */
buzbee311ca162013-02-28 15:56:43 -0800116bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700117{
buzbeefa57c472012-11-21 12:06:18 -0800118 if (bb->data_flow_info == NULL) return false;
buzbee67bf8852011-08-17 17:51:35 -0700119
Bill Buzbeea114add2012-05-03 15:00:40 -0700120 ArenaBitVectorIterator iterator;
buzbee67bf8852011-08-17 17:51:35 -0700121
buzbeefa57c472012-11-21 12:06:18 -0800122 BitVectorIteratorInit(bb->data_flow_info->def_v, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700123 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800124 int idx = BitVectorIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700125 if (idx == -1) break;
126 /* Block bb defines register idx */
buzbee311ca162013-02-28 15:56:43 -0800127 SetBit(cu_, def_block_matrix_[idx], bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 }
129 return true;
buzbee67bf8852011-08-17 17:51:35 -0700130}
131
buzbee311ca162013-02-28 15:56:43 -0800132void MIRGraph::ComputeDefBlockMatrix()
buzbee67bf8852011-08-17 17:51:35 -0700133{
buzbee311ca162013-02-28 15:56:43 -0800134 int num_registers = cu_->num_dalvik_registers;
buzbeefa57c472012-11-21 12:06:18 -0800135 /* Allocate num_dalvik_registers bit vector pointers */
buzbee311ca162013-02-28 15:56:43 -0800136 def_block_matrix_ = static_cast<ArenaBitVector**>
137 (NewMem(cu_, sizeof(ArenaBitVector *) * num_registers, true, kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700138 int i;
buzbee67bf8852011-08-17 17:51:35 -0700139
buzbeefa57c472012-11-21 12:06:18 -0800140 /* Initialize num_register vectors with num_blocks bits each */
141 for (i = 0; i < num_registers; i++) {
buzbee311ca162013-02-28 15:56:43 -0800142 def_block_matrix_[i] = AllocBitVector(cu_, GetNumBlocks(), false, kBitMapBMatrix);
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 }
buzbee0665fe02013-03-21 12:32:21 -0700144 AllNodesIterator iter(this, false /* not iterative */);
buzbee311ca162013-02-28 15:56:43 -0800145 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
146 FindLocalLiveIn(bb);
147 }
buzbee0665fe02013-03-21 12:32:21 -0700148 AllNodesIterator iter2(this, false /* not iterative */);
buzbee311ca162013-02-28 15:56:43 -0800149 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
150 FillDefBlockMatrix(bb);
151 }
buzbee67bf8852011-08-17 17:51:35 -0700152
Bill Buzbeea114add2012-05-03 15:00:40 -0700153 /*
154 * Also set the incoming parameters as defs in the entry block.
155 * Only need to handle the parameters for the outer method.
156 */
buzbee311ca162013-02-28 15:56:43 -0800157 int num_regs = cu_->num_dalvik_registers;
158 int in_reg = num_regs - cu_->num_ins;
buzbeefa57c472012-11-21 12:06:18 -0800159 for (; in_reg < num_regs; in_reg++) {
buzbee311ca162013-02-28 15:56:43 -0800160 SetBit(cu_, def_block_matrix_[in_reg], GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 }
buzbee67bf8852011-08-17 17:51:35 -0700162}
163
164/* Compute the post-order traversal of the CFG */
buzbee311ca162013-02-28 15:56:43 -0800165void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700166{
buzbeefa57c472012-11-21 12:06:18 -0800167 ArenaBitVectorIterator bv_iterator;
168 BitVectorIteratorInit(bb->i_dominated, &bv_iterator);
buzbee67bf8852011-08-17 17:51:35 -0700169
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 /* Iterate through the dominated blocks first */
171 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800172 //TUNING: hot call to BitVectorIteratorNext
buzbeefa57c472012-11-21 12:06:18 -0800173 int bb_idx = BitVectorIteratorNext(&bv_iterator);
174 if (bb_idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800175 BasicBlock* dominated_bb = GetBasicBlock(bb_idx);
176 ComputeDomPostOrderTraversal(dominated_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 }
buzbee67bf8852011-08-17 17:51:35 -0700178
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 /* Enter the current block id */
buzbee311ca162013-02-28 15:56:43 -0800180 InsertGrowableList(cu_, &dom_post_order_traversal_, bb->id);
buzbee67bf8852011-08-17 17:51:35 -0700181
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 /* hacky loop detection */
buzbee52a77fc2012-11-20 19:50:46 -0800183 if (bb->taken && IsBitSet(bb->dominators, bb->taken->id)) {
buzbee1fd33462013-03-25 13:40:45 -0700184 attributes_ |= METHOD_HAS_LOOP;
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 }
buzbee67bf8852011-08-17 17:51:35 -0700186}
187
buzbee311ca162013-02-28 15:56:43 -0800188void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
189 const BasicBlock* succ_bb)
buzbee67bf8852011-08-17 17:51:35 -0700190{
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 /*
192 * TODO - evaluate whether phi will ever need to be inserted into exit
193 * blocks.
194 */
buzbeefa57c472012-11-21 12:06:18 -0800195 if (succ_bb->i_dom != dom_bb &&
196 succ_bb->block_type == kDalvikByteCode &&
197 succ_bb->hidden == false) {
buzbee311ca162013-02-28 15:56:43 -0800198 SetBit(cu_, dom_bb->dom_frontier, succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700199 }
buzbee67bf8852011-08-17 17:51:35 -0700200}
201
202/* Worker function to compute the dominance frontier */
buzbee311ca162013-02-28 15:56:43 -0800203bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700204{
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 /* Calculate DF_local */
206 if (bb->taken) {
buzbee311ca162013-02-28 15:56:43 -0800207 CheckForDominanceFrontier(bb, bb->taken);
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 }
buzbeefa57c472012-11-21 12:06:18 -0800209 if (bb->fall_through) {
buzbee311ca162013-02-28 15:56:43 -0800210 CheckForDominanceFrontier(bb, bb->fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 }
buzbeefa57c472012-11-21 12:06:18 -0800212 if (bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800214 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 &iterator);
216 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800217 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800218 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800219 if (successor_block_info == NULL) break;
220 BasicBlock* succ_bb = successor_block_info->block;
buzbee311ca162013-02-28 15:56:43 -0800221 CheckForDominanceFrontier(bb, succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 }
223 }
buzbee67bf8852011-08-17 17:51:35 -0700224
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 /* Calculate DF_up */
buzbeefa57c472012-11-21 12:06:18 -0800226 ArenaBitVectorIterator bv_iterator;
227 BitVectorIteratorInit(bb->i_dominated, &bv_iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800229 //TUNING: hot call to BitVectorIteratorNext
buzbeefa57c472012-11-21 12:06:18 -0800230 int dominated_idx = BitVectorIteratorNext(&bv_iterator);
231 if (dominated_idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800232 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
buzbeefa57c472012-11-21 12:06:18 -0800233 ArenaBitVectorIterator df_iterator;
234 BitVectorIteratorInit(dominated_bb->dom_frontier, &df_iterator);
buzbee67bf8852011-08-17 17:51:35 -0700235 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800236 //TUNING: hot call to BitVectorIteratorNext
buzbeefa57c472012-11-21 12:06:18 -0800237 int df_up_idx = BitVectorIteratorNext(&df_iterator);
238 if (df_up_idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800239 BasicBlock* df_up_block = GetBasicBlock(df_up_idx);
240 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700241 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 }
buzbee67bf8852011-08-17 17:51:35 -0700243
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 return true;
buzbee67bf8852011-08-17 17:51:35 -0700245}
246
247/* Worker function for initializing domination-related data structures */
buzbee311ca162013-02-28 15:56:43 -0800248bool MIRGraph::InitializeDominationInfo(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700249{
buzbee311ca162013-02-28 15:56:43 -0800250 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700251
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 if (bb->dominators == NULL ) {
buzbee311ca162013-02-28 15:56:43 -0800253 bb->dominators = AllocBitVector(cu_, num_total_blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 false /* expandable */,
255 kBitMapDominators);
buzbee311ca162013-02-28 15:56:43 -0800256 bb->i_dominated = AllocBitVector(cu_, num_total_blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 false /* expandable */,
258 kBitMapIDominated);
buzbee311ca162013-02-28 15:56:43 -0800259 bb->dom_frontier = AllocBitVector(cu_, num_total_blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 false /* expandable */,
261 kBitMapDomFrontier);
262 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800263 ClearAllBits(bb->dominators);
buzbeefa57c472012-11-21 12:06:18 -0800264 ClearAllBits(bb->i_dominated);
265 ClearAllBits(bb->dom_frontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 }
267 /* Set all bits in the dominator vector */
buzbeefa57c472012-11-21 12:06:18 -0800268 SetInitialBits(bb->dominators, num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700269
Bill Buzbeea114add2012-05-03 15:00:40 -0700270 return true;
buzbee67bf8852011-08-17 17:51:35 -0700271}
272
buzbee5b537102012-01-17 17:33:47 -0800273/*
buzbeefa57c472012-11-21 12:06:18 -0800274 * Walk through the ordered i_dom list until we reach common parent.
275 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800276 * last element of the intersection of block1 and block2 dominators.
277 */
buzbee311ca162013-02-28 15:56:43 -0800278int MIRGraph::FindCommonParent(int block1, int block2)
buzbee5b537102012-01-17 17:33:47 -0800279{
Bill Buzbeea114add2012-05-03 15:00:40 -0700280 while (block1 != block2) {
281 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800282 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700283 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800284 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800286 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700287 DCHECK_NE(block2, NOTVISITED);
288 }
289 }
290 return block1;
buzbee5b537102012-01-17 17:33:47 -0800291}
292
293/* Worker function to compute each block's immediate dominator */
buzbee311ca162013-02-28 15:56:43 -0800294bool MIRGraph::ComputeblockIDom(BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800295{
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 GrowableListIterator iter;
297 int idom = -1;
buzbee5b537102012-01-17 17:33:47 -0800298
Bill Buzbeea114add2012-05-03 15:00:40 -0700299 /* Special-case entry block */
buzbee311ca162013-02-28 15:56:43 -0800300 if (bb == GetEntryBlock()) {
buzbee5b537102012-01-17 17:33:47 -0800301 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700302 }
303
304 /* Iterate through the predecessors */
buzbee52a77fc2012-11-20 19:50:46 -0800305 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700306
307 /* Find the first processed predecessor */
308 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800309 BasicBlock* pred_bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
310 CHECK(pred_bb != NULL);
buzbee311ca162013-02-28 15:56:43 -0800311 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800312 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700313 break;
314 }
315 }
316
317 /* Scan the rest of the predecessors */
318 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800319 BasicBlock* pred_bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
320 if (!pred_bb) break;
buzbee311ca162013-02-28 15:56:43 -0800321 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700322 continue;
323 } else {
buzbee311ca162013-02-28 15:56:43 -0800324 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 }
326 }
327
328 DCHECK_NE(idom, NOTVISITED);
329
330 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800331 if (i_dom_list_[bb->dfs_id] != idom) {
332 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 return true;
334 }
335 return false;
buzbee5b537102012-01-17 17:33:47 -0800336}
337
338/* Worker function to compute each block's domintors */
buzbee311ca162013-02-28 15:56:43 -0800339bool MIRGraph::ComputeBlockDominators(BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800340{
buzbee311ca162013-02-28 15:56:43 -0800341 if (bb == GetEntryBlock()) {
buzbee52a77fc2012-11-20 19:50:46 -0800342 ClearAllBits(bb->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 } else {
buzbeefa57c472012-11-21 12:06:18 -0800344 CopyBitVector(bb->dominators, bb->i_dom->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700345 }
buzbee311ca162013-02-28 15:56:43 -0800346 SetBit(cu_, bb->dominators, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700347 return false;
buzbee5b537102012-01-17 17:33:47 -0800348}
349
buzbee311ca162013-02-28 15:56:43 -0800350bool MIRGraph::SetDominators(BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800351{
buzbee311ca162013-02-28 15:56:43 -0800352 if (bb != GetEntryBlock()) {
353 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800354 DCHECK_NE(idom_dfs_idx, NOTVISITED);
buzbee311ca162013-02-28 15:56:43 -0800355 int i_dom_idx = dfs_post_order_.elem_list[idom_dfs_idx];
356 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbeefa57c472012-11-21 12:06:18 -0800357 bb->i_dom = i_dom;
358 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee311ca162013-02-28 15:56:43 -0800359 SetBit(cu_, i_dom->i_dominated, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 }
361 return false;
buzbee5b537102012-01-17 17:33:47 -0800362}
363
buzbee67bf8852011-08-17 17:51:35 -0700364/* Compute dominators, immediate dominator, and dominance fronter */
buzbee311ca162013-02-28 15:56:43 -0800365void MIRGraph::ComputeDominators()
buzbee67bf8852011-08-17 17:51:35 -0700366{
buzbee311ca162013-02-28 15:56:43 -0800367 int num_reachable_blocks = num_reachable_blocks_;
368 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700369
Bill Buzbeea114add2012-05-03 15:00:40 -0700370 /* Initialize domination-related data structures */
buzbee0665fe02013-03-21 12:32:21 -0700371 ReachableNodesIterator iter(this, false /* not iterative */);
buzbee311ca162013-02-28 15:56:43 -0800372 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
373 InitializeDominationInfo(bb);
374 }
buzbee67bf8852011-08-17 17:51:35 -0700375
buzbeefa57c472012-11-21 12:06:18 -0800376 /* Initalize & Clear i_dom_list */
buzbee311ca162013-02-28 15:56:43 -0800377 if (i_dom_list_ == NULL) {
378 i_dom_list_ = static_cast<int*>(NewMem(cu_, sizeof(int) * num_reachable_blocks,
buzbeecbd6d442012-11-17 14:11:25 -0800379 false, kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700380 }
buzbeefa57c472012-11-21 12:06:18 -0800381 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800382 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700383 }
buzbee5b537102012-01-17 17:33:47 -0800384
buzbeefa57c472012-11-21 12:06:18 -0800385 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800386 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
387 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800388
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 /* Compute the immediate dominators */
buzbee0665fe02013-03-21 12:32:21 -0700390 ReversePostOrderDfsIterator iter2(this, true /* iterative */);
buzbee311ca162013-02-28 15:56:43 -0800391 bool change = false;
392 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
393 change = ComputeblockIDom(bb);
394 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700395
396 /* Set the dominator for the root node */
buzbee311ca162013-02-28 15:56:43 -0800397 ClearAllBits(GetEntryBlock()->dominators);
398 SetBit(cu_, GetEntryBlock()->dominators, GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700399
buzbee311ca162013-02-28 15:56:43 -0800400 if (temp_block_v_ == NULL) {
401 temp_block_v_ = AllocBitVector(cu_, num_total_blocks, false /* expandable */, kBitMapTmpBlockV);
Bill Buzbeea114add2012-05-03 15:00:40 -0700402 } else {
buzbee311ca162013-02-28 15:56:43 -0800403 ClearAllBits(temp_block_v_);
Bill Buzbeea114add2012-05-03 15:00:40 -0700404 }
buzbee311ca162013-02-28 15:56:43 -0800405 GetEntryBlock()->i_dom = NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700406
buzbee0665fe02013-03-21 12:32:21 -0700407 ReachableNodesIterator iter3(this, false /* not iterative */);
buzbee311ca162013-02-28 15:56:43 -0800408 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
409 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700410 }
buzbee67bf8852011-08-17 17:51:35 -0700411
buzbee0665fe02013-03-21 12:32:21 -0700412 ReversePostOrderDfsIterator iter4(this, false /* not iterative */);
buzbee311ca162013-02-28 15:56:43 -0800413 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
414 ComputeBlockDominators(bb);
415 }
buzbee5b537102012-01-17 17:33:47 -0800416
Bill Buzbeea114add2012-05-03 15:00:40 -0700417 /*
418 * Now go ahead and compute the post order traversal based on the
buzbeefa57c472012-11-21 12:06:18 -0800419 * i_dominated sets.
Bill Buzbeea114add2012-05-03 15:00:40 -0700420 */
buzbee311ca162013-02-28 15:56:43 -0800421 if (dom_post_order_traversal_.elem_list == NULL) {
422 CompilerInitGrowableList(cu_, &dom_post_order_traversal_,
buzbeefa57c472012-11-21 12:06:18 -0800423 num_reachable_blocks, kListDomPostOrderTraversal);
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 } else {
buzbee311ca162013-02-28 15:56:43 -0800425 dom_post_order_traversal_.num_used = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 }
buzbee5b537102012-01-17 17:33:47 -0800427
buzbee311ca162013-02-28 15:56:43 -0800428 ComputeDomPostOrderTraversal(GetEntryBlock());
429 DCHECK_EQ(dom_post_order_traversal_.num_used, static_cast<unsigned>(num_reachable_blocks_));
buzbee5b537102012-01-17 17:33:47 -0800430
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 /* Now compute the dominance frontier for each block */
buzbee0665fe02013-03-21 12:32:21 -0700432 PostOrderDOMIterator iter5(this, false /* not iterative */);
buzbee311ca162013-02-28 15:56:43 -0800433 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
434 ComputeDominanceFrontier(bb);
435 }
buzbee67bf8852011-08-17 17:51:35 -0700436}
437
438/*
439 * Perform dest U= src1 ^ ~src2
440 * This is probably not general enough to be placed in BitVector.[ch].
441 */
buzbee311ca162013-02-28 15:56:43 -0800442void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
443 const ArenaBitVector* src2)
buzbee67bf8852011-08-17 17:51:35 -0700444{
buzbeefa57c472012-11-21 12:06:18 -0800445 if (dest->storage_size != src1->storage_size ||
446 dest->storage_size != src2->storage_size ||
Bill Buzbeea114add2012-05-03 15:00:40 -0700447 dest->expandable != src1->expandable ||
448 dest->expandable != src2->expandable) {
449 LOG(FATAL) << "Incompatible set properties";
450 }
buzbee67bf8852011-08-17 17:51:35 -0700451
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 unsigned int idx;
buzbeefa57c472012-11-21 12:06:18 -0800453 for (idx = 0; idx < dest->storage_size; idx++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700454 dest->storage[idx] |= src1->storage[idx] & ~src2->storage[idx];
455 }
buzbee67bf8852011-08-17 17:51:35 -0700456}
457
458/*
459 * Iterate through all successor blocks and propagate up the live-in sets.
460 * The calculated result is used for phi-node pruning - where we only need to
461 * insert a phi node if the variable is live-in to the block.
462 */
buzbee311ca162013-02-28 15:56:43 -0800463bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700464{
buzbee311ca162013-02-28 15:56:43 -0800465 ArenaBitVector* temp_dalvik_register_v = temp_dalvik_register_v_;
buzbee67bf8852011-08-17 17:51:35 -0700466
buzbeefa57c472012-11-21 12:06:18 -0800467 if (bb->data_flow_info == NULL) return false;
468 CopyBitVector(temp_dalvik_register_v, bb->data_flow_info->live_in_v);
469 if (bb->taken && bb->taken->data_flow_info)
470 ComputeSuccLineIn(temp_dalvik_register_v, bb->taken->data_flow_info->live_in_v,
471 bb->data_flow_info->def_v);
472 if (bb->fall_through && bb->fall_through->data_flow_info)
buzbee311ca162013-02-28 15:56:43 -0800473 ComputeSuccLineIn(temp_dalvik_register_v, bb->fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800474 bb->data_flow_info->def_v);
475 if (bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800477 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 &iterator);
479 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800480 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800481 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800482 if (successor_block_info == NULL) break;
483 BasicBlock* succ_bb = successor_block_info->block;
484 if (succ_bb->data_flow_info) {
buzbee311ca162013-02-28 15:56:43 -0800485 ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800486 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 }
buzbee67bf8852011-08-17 17:51:35 -0700488 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700489 }
buzbeefa57c472012-11-21 12:06:18 -0800490 if (CompareBitVectors(temp_dalvik_register_v, bb->data_flow_info->live_in_v)) {
491 CopyBitVector(bb->data_flow_info->live_in_v, temp_dalvik_register_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700492 return true;
493 }
494 return false;
buzbee67bf8852011-08-17 17:51:35 -0700495}
496
497/* Insert phi nodes to for each variable to the dominance frontiers */
buzbee311ca162013-02-28 15:56:43 -0800498void MIRGraph::InsertPhiNodes()
buzbee67bf8852011-08-17 17:51:35 -0700499{
buzbeefa57c472012-11-21 12:06:18 -0800500 int dalvik_reg;
buzbee311ca162013-02-28 15:56:43 -0800501 ArenaBitVector* phi_blocks = AllocBitVector(cu_, GetNumBlocks(), false, kBitMapPhi);
502 ArenaBitVector* tmp_blocks = AllocBitVector(cu_, GetNumBlocks(), false, kBitMapTmpBlocks);
503 ArenaBitVector* input_blocks = AllocBitVector(cu_, GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700504
buzbee311ca162013-02-28 15:56:43 -0800505 temp_dalvik_register_v_ =
506 AllocBitVector(cu_, cu_->num_dalvik_registers, false, kBitMapRegisterV);
buzbee67bf8852011-08-17 17:51:35 -0700507
buzbee0665fe02013-03-21 12:32:21 -0700508 PostOrderDfsIterator iter(this, true /* iterative */);
buzbee311ca162013-02-28 15:56:43 -0800509 bool change = false;
510 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
511 change = ComputeBlockLiveIns(bb);
512 }
buzbee67bf8852011-08-17 17:51:35 -0700513
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 /* Iterate through each Dalvik register */
buzbee311ca162013-02-28 15:56:43 -0800515 for (dalvik_reg = cu_->num_dalvik_registers - 1; dalvik_reg >= 0; dalvik_reg--) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700516 bool change;
517 ArenaBitVectorIterator iterator;
buzbee67bf8852011-08-17 17:51:35 -0700518
buzbee311ca162013-02-28 15:56:43 -0800519 CopyBitVector(input_blocks, def_block_matrix_[dalvik_reg]);
buzbeefa57c472012-11-21 12:06:18 -0800520 ClearAllBits(phi_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700521
Bill Buzbeea114add2012-05-03 15:00:40 -0700522 /* Calculate the phi blocks for each Dalvik register */
523 do {
524 change = false;
buzbeefa57c472012-11-21 12:06:18 -0800525 ClearAllBits(tmp_blocks);
526 BitVectorIteratorInit(input_blocks, &iterator);
buzbee67bf8852011-08-17 17:51:35 -0700527
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800529 int idx = BitVectorIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 if (idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800531 BasicBlock* def_bb = GetBasicBlock(idx);
buzbee67bf8852011-08-17 17:51:35 -0700532
buzbeefa57c472012-11-21 12:06:18 -0800533 /* Merge the dominance frontier to tmp_blocks */
buzbee52a77fc2012-11-20 19:50:46 -0800534 //TUNING: hot call to UnifyBitVetors
buzbeefa57c472012-11-21 12:06:18 -0800535 if (def_bb->dom_frontier != NULL) {
536 UnifyBitVetors(tmp_blocks, tmp_blocks, def_bb->dom_frontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 }
buzbee67bf8852011-08-17 17:51:35 -0700538 }
buzbeefa57c472012-11-21 12:06:18 -0800539 if (CompareBitVectors(phi_blocks, tmp_blocks)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700540 change = true;
buzbeefa57c472012-11-21 12:06:18 -0800541 CopyBitVector(phi_blocks, tmp_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700542
543 /*
544 * Iterate through the original blocks plus the new ones in
545 * the dominance frontier.
546 */
buzbeefa57c472012-11-21 12:06:18 -0800547 CopyBitVector(input_blocks, phi_blocks);
buzbee311ca162013-02-28 15:56:43 -0800548 UnifyBitVetors(input_blocks, input_blocks, def_block_matrix_[dalvik_reg]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700549 }
550 } while (change);
551
552 /*
buzbeefa57c472012-11-21 12:06:18 -0800553 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700554 * register is in the live-in set.
555 */
buzbeefa57c472012-11-21 12:06:18 -0800556 BitVectorIteratorInit(phi_blocks, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700557 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800558 int idx = BitVectorIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 if (idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800560 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 /* Variable will be clobbered before being used - no need for phi */
buzbeefa57c472012-11-21 12:06:18 -0800562 if (!IsBitSet(phi_bb->data_flow_info->live_in_v, dalvik_reg)) continue;
buzbee311ca162013-02-28 15:56:43 -0800563 MIR *phi = static_cast<MIR*>(NewMem(cu_, sizeof(MIR), true, kAllocDFInfo));
buzbeecbd6d442012-11-17 14:11:25 -0800564 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800565 phi->dalvikInsn.vA = dalvik_reg;
566 phi->offset = phi_bb->start_offset;
buzbee311ca162013-02-28 15:56:43 -0800567 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
buzbeefa57c472012-11-21 12:06:18 -0800568 PrependMIR(phi_bb, phi);
buzbee67bf8852011-08-17 17:51:35 -0700569 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700570 }
buzbee67bf8852011-08-17 17:51:35 -0700571}
572
573/*
574 * Worker function to insert phi-operands with latest SSA names from
575 * predecessor blocks
576 */
buzbee311ca162013-02-28 15:56:43 -0800577bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700578{
Bill Buzbeea114add2012-05-03 15:00:40 -0700579 GrowableListIterator iter;
580 MIR *mir;
buzbee6969d502012-06-15 16:40:31 -0700581 std::vector<int> uses;
buzbeefa57c472012-11-21 12:06:18 -0800582 std::vector<int> incoming_arc;
buzbee67bf8852011-08-17 17:51:35 -0700583
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 /* Phi nodes are at the beginning of each block */
buzbee28c9a832012-11-21 15:39:13 -0800585 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800586 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700587 return true;
buzbeefa57c472012-11-21 12:06:18 -0800588 int ssa_reg = mir->ssa_rep->defs[0];
589 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800590 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700591
buzbee6969d502012-06-15 16:40:31 -0700592 uses.clear();
buzbeefa57c472012-11-21 12:06:18 -0800593 incoming_arc.clear();
buzbee67bf8852011-08-17 17:51:35 -0700594
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 /* Iterate through the predecessors */
buzbee52a77fc2012-11-20 19:50:46 -0800596 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700597 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800598 BasicBlock* pred_bb =
buzbee52a77fc2012-11-20 19:50:46 -0800599 reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
buzbeefa57c472012-11-21 12:06:18 -0800600 if (!pred_bb) break;
601 int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map[v_reg];
602 uses.push_back(ssa_reg);
603 incoming_arc.push_back(pred_bb->id);
buzbee67bf8852011-08-17 17:51:35 -0700604 }
605
Bill Buzbeea114add2012-05-03 15:00:40 -0700606 /* Count the number of SSA registers for a Dalvik register */
buzbeefa57c472012-11-21 12:06:18 -0800607 int num_uses = uses.size();
608 mir->ssa_rep->num_uses = num_uses;
609 mir->ssa_rep->uses =
buzbee311ca162013-02-28 15:56:43 -0800610 static_cast<int*>(NewMem(cu_, sizeof(int) * num_uses, false, kAllocDFInfo));
buzbeefa57c472012-11-21 12:06:18 -0800611 mir->ssa_rep->fp_use =
buzbee311ca162013-02-28 15:56:43 -0800612 static_cast<bool*>(NewMem(cu_, sizeof(bool) * num_uses, true, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700613 int* incoming =
buzbee311ca162013-02-28 15:56:43 -0800614 static_cast<int*>(NewMem(cu_, sizeof(int) * num_uses, false, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700615 // TODO: Ugly, rework (but don't burden each MIR/LIR for Phi-only needs)
buzbeecbd6d442012-11-17 14:11:25 -0800616 mir->dalvikInsn.vB = reinterpret_cast<uintptr_t>(incoming);
Bill Buzbeea114add2012-05-03 15:00:40 -0700617
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 /* Set the uses array for the phi node */
buzbeefa57c472012-11-21 12:06:18 -0800619 int *use_ptr = mir->ssa_rep->uses;
620 for (int i = 0; i < num_uses; i++) {
621 *use_ptr++ = uses[i];
622 *incoming++ = incoming_arc[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 }
624 }
625
626 return true;
buzbee67bf8852011-08-17 17:51:35 -0700627}
628
buzbee311ca162013-02-28 15:56:43 -0800629void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block)
buzbeef0cde542011-09-13 14:55:02 -0700630{
631
Bill Buzbeea114add2012-05-03 15:00:40 -0700632 if (block->visited || block->hidden) return;
633 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700634
Bill Buzbeea114add2012-05-03 15:00:40 -0700635 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800636 DoSSAConversion(block);
637 int map_size = sizeof(int) * cu_->num_dalvik_registers;
buzbeef0cde542011-09-13 14:55:02 -0700638
Bill Buzbeea114add2012-05-03 15:00:40 -0700639 /* Save SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800640 int* saved_ssa_map = static_cast<int*>(NewMem(cu_, map_size, false, kAllocDalvikToSSAMap));
641 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700642
buzbeefa57c472012-11-21 12:06:18 -0800643 if (block->fall_through) {
buzbee311ca162013-02-28 15:56:43 -0800644 DoDFSPreOrderSSARename(block->fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800646 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700647 }
648 if (block->taken) {
buzbee311ca162013-02-28 15:56:43 -0800649 DoDFSPreOrderSSARename(block->taken);
Bill Buzbeea114add2012-05-03 15:00:40 -0700650 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800651 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700652 }
buzbeefa57c472012-11-21 12:06:18 -0800653 if (block->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700654 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800655 GrowableListIteratorInit(&block->successor_block_list.blocks, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700656 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800657 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800658 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800659 if (successor_block_info == NULL) break;
660 BasicBlock* succ_bb = successor_block_info->block;
buzbee311ca162013-02-28 15:56:43 -0800661 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700662 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800663 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700664 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 }
buzbee311ca162013-02-28 15:56:43 -0800666 vreg_to_ssa_map_ = saved_ssa_map;
Bill Buzbeea114add2012-05-03 15:00:40 -0700667 return;
buzbeef0cde542011-09-13 14:55:02 -0700668}
669
buzbee67bf8852011-08-17 17:51:35 -0700670/* Perform SSA transformation for the whole method */
buzbee311ca162013-02-28 15:56:43 -0800671void MIRGraph::SSATransformation()
buzbee67bf8852011-08-17 17:51:35 -0700672{
Bill Buzbeea114add2012-05-03 15:00:40 -0700673 /* Compute the DFS order */
buzbee311ca162013-02-28 15:56:43 -0800674 ComputeDFSOrders();
buzbee67bf8852011-08-17 17:51:35 -0700675
buzbee1fd33462013-03-25 13:40:45 -0700676 /* Compute the dominator info */
677 ComputeDominators();
buzbee67bf8852011-08-17 17:51:35 -0700678
Bill Buzbeea114add2012-05-03 15:00:40 -0700679 /* Allocate data structures in preparation for SSA conversion */
buzbee311ca162013-02-28 15:56:43 -0800680 CompilerInitializeSSAConversion();
buzbee67bf8852011-08-17 17:51:35 -0700681
buzbee1fd33462013-03-25 13:40:45 -0700682 /* Find out the "Dalvik reg def x block" relation */
683 ComputeDefBlockMatrix();
buzbee67bf8852011-08-17 17:51:35 -0700684
buzbee1fd33462013-03-25 13:40:45 -0700685 /* Insert phi nodes to dominance frontiers for all variables */
686 InsertPhiNodes();
buzbee67bf8852011-08-17 17:51:35 -0700687
Bill Buzbeea114add2012-05-03 15:00:40 -0700688 /* Rename register names by local defs and phi nodes */
buzbee1fd33462013-03-25 13:40:45 -0700689 AllNodesIterator iter1(this, false /* not iterative */);
690 for (BasicBlock* bb = iter1.Next(); bb != NULL; bb = iter1.Next()) {
buzbee311ca162013-02-28 15:56:43 -0800691 ClearVisitedFlag(bb);
692 }
693 DoDFSPreOrderSSARename(GetEntryBlock());
buzbee67bf8852011-08-17 17:51:35 -0700694
buzbee1fd33462013-03-25 13:40:45 -0700695 /*
696 * Shared temp bit vector used by each block to count the number of defs
697 * from all the predecessor blocks.
698 */
699 temp_ssa_register_v_ = AllocBitVector(cu_, GetNumSSARegs(), false, kBitMapTempSSARegisterV);
buzbee2cfc6392012-05-07 14:51:40 -0700700
buzbee1fd33462013-03-25 13:40:45 -0700701 /* Insert phi-operands with latest SSA names from predecessor blocks */
702 ReachableNodesIterator iter2(this, false /* not iterative */);
703 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
704 InsertPhiNodeOperands(bb);
buzbee311ca162013-02-28 15:56:43 -0800705 }
buzbee1fd33462013-03-25 13:40:45 -0700706
buzbee311ca162013-02-28 15:56:43 -0800707 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
708 DumpCFG("/sdcard/3_post_ssa_cfg/", false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 }
buzbee1fd33462013-03-25 13:40:45 -0700710 if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) {
711 VerifyDataflow();
712 }
buzbee67bf8852011-08-17 17:51:35 -0700713}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800714
715} // namespace art