blob: 52674aa0d0aa15119064b11341cb257289d96cc0 [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
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080020namespace art {
21
buzbee311ca162013-02-28 15:56:43 -080022BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) {
buzbeee5f01222012-06-14 15:19:35 -070023 if (bb != NULL) {
24 if (bb->visited || bb->hidden) {
25 bb = NULL;
26 }
27 }
28 return bb;
29}
30
buzbee311ca162013-02-28 15:56:43 -080031BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb)
buzbeee5f01222012-06-14 15:19:35 -070032{
buzbeefa57c472012-11-21 12:06:18 -080033 BasicBlock* res = NeedsVisit(bb->fall_through);
buzbeee5f01222012-06-14 15:19:35 -070034 if (res == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -080035 res = NeedsVisit(bb->taken);
buzbeee5f01222012-06-14 15:19:35 -070036 if (res == NULL) {
buzbeefa57c472012-11-21 12:06:18 -080037 if (bb->successor_block_list.block_list_type != kNotUsed) {
buzbeee5f01222012-06-14 15:19:35 -070038 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -080039 GrowableListIteratorInit(&bb->successor_block_list.blocks,
buzbeee5f01222012-06-14 15:19:35 -070040 &iterator);
41 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -080042 SuccessorBlockInfo *sbi = reinterpret_cast<SuccessorBlockInfo*>
buzbee52a77fc2012-11-20 19:50:46 -080043 (GrowableListIteratorNext(&iterator));
buzbeee5f01222012-06-14 15:19:35 -070044 if (sbi == NULL) break;
buzbee52a77fc2012-11-20 19:50:46 -080045 res = NeedsVisit(sbi->block);
buzbeee5f01222012-06-14 15:19:35 -070046 if (res != NULL) break;
47 }
48 }
49 }
50 }
51 return res;
52}
53
buzbee311ca162013-02-28 15:56:43 -080054void MIRGraph::MarkPreOrder(BasicBlock* block)
buzbeee5f01222012-06-14 15:19:35 -070055{
56 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080057 /* Enqueue the pre_order block id */
buzbee311ca162013-02-28 15:56:43 -080058 InsertGrowableList(cu_, &dfs_order_, block->id);
buzbeee5f01222012-06-14 15:19:35 -070059}
60
buzbee311ca162013-02-28 15:56:43 -080061void MIRGraph::RecordDFSOrders(BasicBlock* block)
buzbee67bf8852011-08-17 17:51:35 -070062{
buzbeee5f01222012-06-14 15:19:35 -070063 std::vector<BasicBlock*> succ;
buzbee311ca162013-02-28 15:56:43 -080064 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070065 succ.push_back(block);
66 while (!succ.empty()) {
67 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080068 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
69 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080070 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080071 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070072 continue;
73 }
buzbee311ca162013-02-28 15:56:43 -080074 curr->dfs_id = dfs_post_order_.num_used;
75 InsertGrowableList(cu_, &dfs_post_order_, curr->id);
buzbeee5f01222012-06-14 15:19:35 -070076 succ.pop_back();
77 }
78}
79
buzbee5b537102012-01-17 17:33:47 -080080/* Sort the blocks by the Depth-First-Search */
buzbee311ca162013-02-28 15:56:43 -080081void MIRGraph::ComputeDFSOrders()
buzbee67bf8852011-08-17 17:51:35 -070082{
buzbeefa57c472012-11-21 12:06:18 -080083 /* Initialize or reset the DFS pre_order list */
buzbee311ca162013-02-28 15:56:43 -080084 if (dfs_order_.elem_list == NULL) {
85 CompilerInitGrowableList(cu_, &dfs_order_, GetNumBlocks(), kListDfsOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -070086 } else {
87 /* Just reset the used length on the counter */
buzbee311ca162013-02-28 15:56:43 -080088 dfs_order_.num_used = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -070089 }
buzbee67bf8852011-08-17 17:51:35 -070090
buzbeefa57c472012-11-21 12:06:18 -080091 /* Initialize or reset the DFS post_order list */
buzbee311ca162013-02-28 15:56:43 -080092 if (dfs_post_order_.elem_list == NULL) {
93 CompilerInitGrowableList(cu_, &dfs_post_order_, GetNumBlocks(), kListDfsPostOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -070094 } else {
95 /* Just reset the used length on the counter */
buzbee311ca162013-02-28 15:56:43 -080096 dfs_post_order_.num_used = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -070097 }
buzbee5b537102012-01-17 17:33:47 -080098
buzbeee5f01222012-06-14 15:19:35 -070099 // Reset visited flags from all nodes
buzbee311ca162013-02-28 15:56:43 -0800100 DataflowIterator iter(this, kAllNodes, false /* not iterative */);
101 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
102 ClearVisitedFlag(bb);
103 }
buzbeee5f01222012-06-14 15:19:35 -0700104 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800105 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700106
buzbee311ca162013-02-28 15:56:43 -0800107 num_reachable_blocks_ = dfs_order_.num_used;
buzbee67bf8852011-08-17 17:51:35 -0700108}
109
110/*
111 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
112 * register idx is defined in BasicBlock bb.
113 */
buzbee311ca162013-02-28 15:56:43 -0800114bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700115{
buzbeefa57c472012-11-21 12:06:18 -0800116 if (bb->data_flow_info == NULL) return false;
buzbee67bf8852011-08-17 17:51:35 -0700117
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 ArenaBitVectorIterator iterator;
buzbee67bf8852011-08-17 17:51:35 -0700119
buzbeefa57c472012-11-21 12:06:18 -0800120 BitVectorIteratorInit(bb->data_flow_info->def_v, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700121 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800122 int idx = BitVectorIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700123 if (idx == -1) break;
124 /* Block bb defines register idx */
buzbee311ca162013-02-28 15:56:43 -0800125 SetBit(cu_, def_block_matrix_[idx], bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700126 }
127 return true;
buzbee67bf8852011-08-17 17:51:35 -0700128}
129
buzbee311ca162013-02-28 15:56:43 -0800130void MIRGraph::ComputeDefBlockMatrix()
buzbee67bf8852011-08-17 17:51:35 -0700131{
buzbee311ca162013-02-28 15:56:43 -0800132 int num_registers = cu_->num_dalvik_registers;
buzbeefa57c472012-11-21 12:06:18 -0800133 /* Allocate num_dalvik_registers bit vector pointers */
buzbee311ca162013-02-28 15:56:43 -0800134 def_block_matrix_ = static_cast<ArenaBitVector**>
135 (NewMem(cu_, sizeof(ArenaBitVector *) * num_registers, true, kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 int i;
buzbee67bf8852011-08-17 17:51:35 -0700137
buzbeefa57c472012-11-21 12:06:18 -0800138 /* Initialize num_register vectors with num_blocks bits each */
139 for (i = 0; i < num_registers; i++) {
buzbee311ca162013-02-28 15:56:43 -0800140 def_block_matrix_[i] = AllocBitVector(cu_, GetNumBlocks(), false, kBitMapBMatrix);
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 }
buzbee311ca162013-02-28 15:56:43 -0800142 DataflowIterator iter(this, kAllNodes, false /* not iterative */);
143 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
144 FindLocalLiveIn(bb);
145 }
146 DataflowIterator iter2(this, kAllNodes, false /* not iterative */);
147 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
148 FillDefBlockMatrix(bb);
149 }
buzbee67bf8852011-08-17 17:51:35 -0700150
Bill Buzbeea114add2012-05-03 15:00:40 -0700151 /*
152 * Also set the incoming parameters as defs in the entry block.
153 * Only need to handle the parameters for the outer method.
154 */
buzbee311ca162013-02-28 15:56:43 -0800155 int num_regs = cu_->num_dalvik_registers;
156 int in_reg = num_regs - cu_->num_ins;
buzbeefa57c472012-11-21 12:06:18 -0800157 for (; in_reg < num_regs; in_reg++) {
buzbee311ca162013-02-28 15:56:43 -0800158 SetBit(cu_, def_block_matrix_[in_reg], GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700159 }
buzbee67bf8852011-08-17 17:51:35 -0700160}
161
162/* Compute the post-order traversal of the CFG */
buzbee311ca162013-02-28 15:56:43 -0800163void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700164{
buzbeefa57c472012-11-21 12:06:18 -0800165 ArenaBitVectorIterator bv_iterator;
166 BitVectorIteratorInit(bb->i_dominated, &bv_iterator);
buzbee67bf8852011-08-17 17:51:35 -0700167
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 /* Iterate through the dominated blocks first */
169 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800170 //TUNING: hot call to BitVectorIteratorNext
buzbeefa57c472012-11-21 12:06:18 -0800171 int bb_idx = BitVectorIteratorNext(&bv_iterator);
172 if (bb_idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800173 BasicBlock* dominated_bb = GetBasicBlock(bb_idx);
174 ComputeDomPostOrderTraversal(dominated_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 }
buzbee67bf8852011-08-17 17:51:35 -0700176
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 /* Enter the current block id */
buzbee311ca162013-02-28 15:56:43 -0800178 InsertGrowableList(cu_, &dom_post_order_traversal_, bb->id);
buzbee67bf8852011-08-17 17:51:35 -0700179
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 /* hacky loop detection */
buzbee52a77fc2012-11-20 19:50:46 -0800181 if (bb->taken && IsBitSet(bb->dominators, bb->taken->id)) {
buzbee311ca162013-02-28 15:56:43 -0800182 cu_->attributes |= METHOD_HAS_LOOP;
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 }
buzbee67bf8852011-08-17 17:51:35 -0700184}
185
buzbee311ca162013-02-28 15:56:43 -0800186void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
187 const BasicBlock* succ_bb)
buzbee67bf8852011-08-17 17:51:35 -0700188{
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 /*
190 * TODO - evaluate whether phi will ever need to be inserted into exit
191 * blocks.
192 */
buzbeefa57c472012-11-21 12:06:18 -0800193 if (succ_bb->i_dom != dom_bb &&
194 succ_bb->block_type == kDalvikByteCode &&
195 succ_bb->hidden == false) {
buzbee311ca162013-02-28 15:56:43 -0800196 SetBit(cu_, dom_bb->dom_frontier, succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700197 }
buzbee67bf8852011-08-17 17:51:35 -0700198}
199
200/* Worker function to compute the dominance frontier */
buzbee311ca162013-02-28 15:56:43 -0800201bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700202{
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 /* Calculate DF_local */
204 if (bb->taken) {
buzbee311ca162013-02-28 15:56:43 -0800205 CheckForDominanceFrontier(bb, bb->taken);
Bill Buzbeea114add2012-05-03 15:00:40 -0700206 }
buzbeefa57c472012-11-21 12:06:18 -0800207 if (bb->fall_through) {
buzbee311ca162013-02-28 15:56:43 -0800208 CheckForDominanceFrontier(bb, bb->fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 }
buzbeefa57c472012-11-21 12:06:18 -0800210 if (bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800212 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 &iterator);
214 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800215 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800216 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800217 if (successor_block_info == NULL) break;
218 BasicBlock* succ_bb = successor_block_info->block;
buzbee311ca162013-02-28 15:56:43 -0800219 CheckForDominanceFrontier(bb, succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700220 }
221 }
buzbee67bf8852011-08-17 17:51:35 -0700222
Bill Buzbeea114add2012-05-03 15:00:40 -0700223 /* Calculate DF_up */
buzbeefa57c472012-11-21 12:06:18 -0800224 ArenaBitVectorIterator bv_iterator;
225 BitVectorIteratorInit(bb->i_dominated, &bv_iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800227 //TUNING: hot call to BitVectorIteratorNext
buzbeefa57c472012-11-21 12:06:18 -0800228 int dominated_idx = BitVectorIteratorNext(&bv_iterator);
229 if (dominated_idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800230 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
buzbeefa57c472012-11-21 12:06:18 -0800231 ArenaBitVectorIterator df_iterator;
232 BitVectorIteratorInit(dominated_bb->dom_frontier, &df_iterator);
buzbee67bf8852011-08-17 17:51:35 -0700233 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800234 //TUNING: hot call to BitVectorIteratorNext
buzbeefa57c472012-11-21 12:06:18 -0800235 int df_up_idx = BitVectorIteratorNext(&df_iterator);
236 if (df_up_idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800237 BasicBlock* df_up_block = GetBasicBlock(df_up_idx);
238 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700239 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 }
buzbee67bf8852011-08-17 17:51:35 -0700241
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 return true;
buzbee67bf8852011-08-17 17:51:35 -0700243}
244
245/* Worker function for initializing domination-related data structures */
buzbee311ca162013-02-28 15:56:43 -0800246bool MIRGraph::InitializeDominationInfo(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700247{
buzbee311ca162013-02-28 15:56:43 -0800248 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700249
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 if (bb->dominators == NULL ) {
buzbee311ca162013-02-28 15:56:43 -0800251 bb->dominators = AllocBitVector(cu_, num_total_blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 false /* expandable */,
253 kBitMapDominators);
buzbee311ca162013-02-28 15:56:43 -0800254 bb->i_dominated = AllocBitVector(cu_, num_total_blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700255 false /* expandable */,
256 kBitMapIDominated);
buzbee311ca162013-02-28 15:56:43 -0800257 bb->dom_frontier = AllocBitVector(cu_, num_total_blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 false /* expandable */,
259 kBitMapDomFrontier);
260 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800261 ClearAllBits(bb->dominators);
buzbeefa57c472012-11-21 12:06:18 -0800262 ClearAllBits(bb->i_dominated);
263 ClearAllBits(bb->dom_frontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700264 }
265 /* Set all bits in the dominator vector */
buzbeefa57c472012-11-21 12:06:18 -0800266 SetInitialBits(bb->dominators, num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700267
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 return true;
buzbee67bf8852011-08-17 17:51:35 -0700269}
270
buzbee5b537102012-01-17 17:33:47 -0800271/*
buzbeefa57c472012-11-21 12:06:18 -0800272 * Walk through the ordered i_dom list until we reach common parent.
273 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800274 * last element of the intersection of block1 and block2 dominators.
275 */
buzbee311ca162013-02-28 15:56:43 -0800276int MIRGraph::FindCommonParent(int block1, int block2)
buzbee5b537102012-01-17 17:33:47 -0800277{
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 while (block1 != block2) {
279 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800280 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800282 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700283 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800284 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 DCHECK_NE(block2, NOTVISITED);
286 }
287 }
288 return block1;
buzbee5b537102012-01-17 17:33:47 -0800289}
290
291/* Worker function to compute each block's immediate dominator */
buzbee311ca162013-02-28 15:56:43 -0800292bool MIRGraph::ComputeblockIDom(BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800293{
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 GrowableListIterator iter;
295 int idom = -1;
buzbee5b537102012-01-17 17:33:47 -0800296
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 /* Special-case entry block */
buzbee311ca162013-02-28 15:56:43 -0800298 if (bb == GetEntryBlock()) {
buzbee5b537102012-01-17 17:33:47 -0800299 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 }
301
302 /* Iterate through the predecessors */
buzbee52a77fc2012-11-20 19:50:46 -0800303 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700304
305 /* Find the first processed predecessor */
306 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800307 BasicBlock* pred_bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
308 CHECK(pred_bb != NULL);
buzbee311ca162013-02-28 15:56:43 -0800309 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800310 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 break;
312 }
313 }
314
315 /* Scan the rest of the predecessors */
316 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800317 BasicBlock* pred_bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
318 if (!pred_bb) break;
buzbee311ca162013-02-28 15:56:43 -0800319 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700320 continue;
321 } else {
buzbee311ca162013-02-28 15:56:43 -0800322 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700323 }
324 }
325
326 DCHECK_NE(idom, NOTVISITED);
327
328 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800329 if (i_dom_list_[bb->dfs_id] != idom) {
330 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 return true;
332 }
333 return false;
buzbee5b537102012-01-17 17:33:47 -0800334}
335
336/* Worker function to compute each block's domintors */
buzbee311ca162013-02-28 15:56:43 -0800337bool MIRGraph::ComputeBlockDominators(BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800338{
buzbee311ca162013-02-28 15:56:43 -0800339 if (bb == GetEntryBlock()) {
buzbee52a77fc2012-11-20 19:50:46 -0800340 ClearAllBits(bb->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 } else {
buzbeefa57c472012-11-21 12:06:18 -0800342 CopyBitVector(bb->dominators, bb->i_dom->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 }
buzbee311ca162013-02-28 15:56:43 -0800344 SetBit(cu_, bb->dominators, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700345 return false;
buzbee5b537102012-01-17 17:33:47 -0800346}
347
buzbee311ca162013-02-28 15:56:43 -0800348bool MIRGraph::SetDominators(BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800349{
buzbee311ca162013-02-28 15:56:43 -0800350 if (bb != GetEntryBlock()) {
351 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800352 DCHECK_NE(idom_dfs_idx, NOTVISITED);
buzbee311ca162013-02-28 15:56:43 -0800353 int i_dom_idx = dfs_post_order_.elem_list[idom_dfs_idx];
354 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
355 if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) {
buzbeefa57c472012-11-21 12:06:18 -0800356 DCHECK_EQ(bb->i_dom->id, i_dom->id);
buzbee5b537102012-01-17 17:33:47 -0800357 }
buzbeefa57c472012-11-21 12:06:18 -0800358 bb->i_dom = i_dom;
359 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee311ca162013-02-28 15:56:43 -0800360 SetBit(cu_, i_dom->i_dominated, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 }
362 return false;
buzbee5b537102012-01-17 17:33:47 -0800363}
364
buzbee67bf8852011-08-17 17:51:35 -0700365/* Compute dominators, immediate dominator, and dominance fronter */
buzbee311ca162013-02-28 15:56:43 -0800366void MIRGraph::ComputeDominators()
buzbee67bf8852011-08-17 17:51:35 -0700367{
buzbee311ca162013-02-28 15:56:43 -0800368 int num_reachable_blocks = num_reachable_blocks_;
369 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700370
Bill Buzbeea114add2012-05-03 15:00:40 -0700371 /* Initialize domination-related data structures */
buzbee311ca162013-02-28 15:56:43 -0800372 DataflowIterator iter(this, kReachableNodes, false /* not iterative */);
373 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
374 InitializeDominationInfo(bb);
375 }
buzbee67bf8852011-08-17 17:51:35 -0700376
buzbeefa57c472012-11-21 12:06:18 -0800377 /* Initalize & Clear i_dom_list */
buzbee311ca162013-02-28 15:56:43 -0800378 if (i_dom_list_ == NULL) {
379 i_dom_list_ = static_cast<int*>(NewMem(cu_, sizeof(int) * num_reachable_blocks,
buzbeecbd6d442012-11-17 14:11:25 -0800380 false, kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700381 }
buzbeefa57c472012-11-21 12:06:18 -0800382 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800383 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 }
buzbee5b537102012-01-17 17:33:47 -0800385
buzbeefa57c472012-11-21 12:06:18 -0800386 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800387 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
388 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800389
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 /* Compute the immediate dominators */
buzbee311ca162013-02-28 15:56:43 -0800391 DataflowIterator iter2(this, kReversePostOrderTraversal, true /* iterative */);
392 bool change = false;
393 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
394 change = ComputeblockIDom(bb);
395 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700396
397 /* Set the dominator for the root node */
buzbee311ca162013-02-28 15:56:43 -0800398 ClearAllBits(GetEntryBlock()->dominators);
399 SetBit(cu_, GetEntryBlock()->dominators, GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700400
buzbee311ca162013-02-28 15:56:43 -0800401 if (temp_block_v_ == NULL) {
402 temp_block_v_ = AllocBitVector(cu_, num_total_blocks, false /* expandable */, kBitMapTmpBlockV);
Bill Buzbeea114add2012-05-03 15:00:40 -0700403 } else {
buzbee311ca162013-02-28 15:56:43 -0800404 ClearAllBits(temp_block_v_);
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 }
buzbee311ca162013-02-28 15:56:43 -0800406 GetEntryBlock()->i_dom = NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700407
buzbee311ca162013-02-28 15:56:43 -0800408 DataflowIterator iter3(this, kReachableNodes, false /* not iterative */);
409 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
410 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700411 }
buzbee67bf8852011-08-17 17:51:35 -0700412
buzbee311ca162013-02-28 15:56:43 -0800413 DataflowIterator iter4(this, kReversePostOrderTraversal, false /* not iterative */);
414 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
415 ComputeBlockDominators(bb);
416 }
buzbee5b537102012-01-17 17:33:47 -0800417
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 /*
419 * Now go ahead and compute the post order traversal based on the
buzbeefa57c472012-11-21 12:06:18 -0800420 * i_dominated sets.
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 */
buzbee311ca162013-02-28 15:56:43 -0800422 if (dom_post_order_traversal_.elem_list == NULL) {
423 CompilerInitGrowableList(cu_, &dom_post_order_traversal_,
buzbeefa57c472012-11-21 12:06:18 -0800424 num_reachable_blocks, kListDomPostOrderTraversal);
Bill Buzbeea114add2012-05-03 15:00:40 -0700425 } else {
buzbee311ca162013-02-28 15:56:43 -0800426 dom_post_order_traversal_.num_used = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 }
buzbee5b537102012-01-17 17:33:47 -0800428
buzbee311ca162013-02-28 15:56:43 -0800429 ComputeDomPostOrderTraversal(GetEntryBlock());
430 DCHECK_EQ(dom_post_order_traversal_.num_used, static_cast<unsigned>(num_reachable_blocks_));
buzbee5b537102012-01-17 17:33:47 -0800431
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 /* Now compute the dominance frontier for each block */
buzbee311ca162013-02-28 15:56:43 -0800433 DataflowIterator iter5(this, kPostOrderDOMTraversal, false /* not iterative */);
434 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
435 ComputeDominanceFrontier(bb);
436 }
buzbee67bf8852011-08-17 17:51:35 -0700437}
438
439/*
440 * Perform dest U= src1 ^ ~src2
441 * This is probably not general enough to be placed in BitVector.[ch].
442 */
buzbee311ca162013-02-28 15:56:43 -0800443void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
444 const ArenaBitVector* src2)
buzbee67bf8852011-08-17 17:51:35 -0700445{
buzbeefa57c472012-11-21 12:06:18 -0800446 if (dest->storage_size != src1->storage_size ||
447 dest->storage_size != src2->storage_size ||
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 dest->expandable != src1->expandable ||
449 dest->expandable != src2->expandable) {
450 LOG(FATAL) << "Incompatible set properties";
451 }
buzbee67bf8852011-08-17 17:51:35 -0700452
Bill Buzbeea114add2012-05-03 15:00:40 -0700453 unsigned int idx;
buzbeefa57c472012-11-21 12:06:18 -0800454 for (idx = 0; idx < dest->storage_size; idx++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700455 dest->storage[idx] |= src1->storage[idx] & ~src2->storage[idx];
456 }
buzbee67bf8852011-08-17 17:51:35 -0700457}
458
459/*
460 * Iterate through all successor blocks and propagate up the live-in sets.
461 * The calculated result is used for phi-node pruning - where we only need to
462 * insert a phi node if the variable is live-in to the block.
463 */
buzbee311ca162013-02-28 15:56:43 -0800464bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700465{
buzbee311ca162013-02-28 15:56:43 -0800466 ArenaBitVector* temp_dalvik_register_v = temp_dalvik_register_v_;
buzbee67bf8852011-08-17 17:51:35 -0700467
buzbeefa57c472012-11-21 12:06:18 -0800468 if (bb->data_flow_info == NULL) return false;
469 CopyBitVector(temp_dalvik_register_v, bb->data_flow_info->live_in_v);
470 if (bb->taken && bb->taken->data_flow_info)
471 ComputeSuccLineIn(temp_dalvik_register_v, bb->taken->data_flow_info->live_in_v,
472 bb->data_flow_info->def_v);
473 if (bb->fall_through && bb->fall_through->data_flow_info)
buzbee311ca162013-02-28 15:56:43 -0800474 ComputeSuccLineIn(temp_dalvik_register_v, bb->fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800475 bb->data_flow_info->def_v);
476 if (bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700477 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800478 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700479 &iterator);
480 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800481 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800482 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800483 if (successor_block_info == NULL) break;
484 BasicBlock* succ_bb = successor_block_info->block;
485 if (succ_bb->data_flow_info) {
buzbee311ca162013-02-28 15:56:43 -0800486 ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800487 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700488 }
buzbee67bf8852011-08-17 17:51:35 -0700489 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700490 }
buzbeefa57c472012-11-21 12:06:18 -0800491 if (CompareBitVectors(temp_dalvik_register_v, bb->data_flow_info->live_in_v)) {
492 CopyBitVector(bb->data_flow_info->live_in_v, temp_dalvik_register_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 return true;
494 }
495 return false;
buzbee67bf8852011-08-17 17:51:35 -0700496}
497
498/* Insert phi nodes to for each variable to the dominance frontiers */
buzbee311ca162013-02-28 15:56:43 -0800499void MIRGraph::InsertPhiNodes()
buzbee67bf8852011-08-17 17:51:35 -0700500{
buzbeefa57c472012-11-21 12:06:18 -0800501 int dalvik_reg;
buzbee311ca162013-02-28 15:56:43 -0800502 ArenaBitVector* phi_blocks = AllocBitVector(cu_, GetNumBlocks(), false, kBitMapPhi);
503 ArenaBitVector* tmp_blocks = AllocBitVector(cu_, GetNumBlocks(), false, kBitMapTmpBlocks);
504 ArenaBitVector* input_blocks = AllocBitVector(cu_, GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700505
buzbee311ca162013-02-28 15:56:43 -0800506 temp_dalvik_register_v_ =
507 AllocBitVector(cu_, cu_->num_dalvik_registers, false, kBitMapRegisterV);
buzbee67bf8852011-08-17 17:51:35 -0700508
buzbee311ca162013-02-28 15:56:43 -0800509 DataflowIterator iter(this, kPostOrderDFSTraversal, true /* iterative */);
510 bool change = false;
511 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
512 change = ComputeBlockLiveIns(bb);
513 }
buzbee67bf8852011-08-17 17:51:35 -0700514
Bill Buzbeea114add2012-05-03 15:00:40 -0700515 /* Iterate through each Dalvik register */
buzbee311ca162013-02-28 15:56:43 -0800516 for (dalvik_reg = cu_->num_dalvik_registers - 1; dalvik_reg >= 0; dalvik_reg--) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 bool change;
518 ArenaBitVectorIterator iterator;
buzbee67bf8852011-08-17 17:51:35 -0700519
buzbee311ca162013-02-28 15:56:43 -0800520 CopyBitVector(input_blocks, def_block_matrix_[dalvik_reg]);
buzbeefa57c472012-11-21 12:06:18 -0800521 ClearAllBits(phi_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700522
Bill Buzbeea114add2012-05-03 15:00:40 -0700523 /* Calculate the phi blocks for each Dalvik register */
524 do {
525 change = false;
buzbeefa57c472012-11-21 12:06:18 -0800526 ClearAllBits(tmp_blocks);
527 BitVectorIteratorInit(input_blocks, &iterator);
buzbee67bf8852011-08-17 17:51:35 -0700528
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800530 int idx = BitVectorIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700531 if (idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800532 BasicBlock* def_bb = GetBasicBlock(idx);
buzbee67bf8852011-08-17 17:51:35 -0700533
buzbeefa57c472012-11-21 12:06:18 -0800534 /* Merge the dominance frontier to tmp_blocks */
buzbee52a77fc2012-11-20 19:50:46 -0800535 //TUNING: hot call to UnifyBitVetors
buzbeefa57c472012-11-21 12:06:18 -0800536 if (def_bb->dom_frontier != NULL) {
537 UnifyBitVetors(tmp_blocks, tmp_blocks, def_bb->dom_frontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 }
buzbee67bf8852011-08-17 17:51:35 -0700539 }
buzbeefa57c472012-11-21 12:06:18 -0800540 if (CompareBitVectors(phi_blocks, tmp_blocks)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 change = true;
buzbeefa57c472012-11-21 12:06:18 -0800542 CopyBitVector(phi_blocks, tmp_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700543
544 /*
545 * Iterate through the original blocks plus the new ones in
546 * the dominance frontier.
547 */
buzbeefa57c472012-11-21 12:06:18 -0800548 CopyBitVector(input_blocks, phi_blocks);
buzbee311ca162013-02-28 15:56:43 -0800549 UnifyBitVetors(input_blocks, input_blocks, def_block_matrix_[dalvik_reg]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700550 }
551 } while (change);
552
553 /*
buzbeefa57c472012-11-21 12:06:18 -0800554 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700555 * register is in the live-in set.
556 */
buzbeefa57c472012-11-21 12:06:18 -0800557 BitVectorIteratorInit(phi_blocks, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700558 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800559 int idx = BitVectorIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700560 if (idx == -1) break;
buzbee311ca162013-02-28 15:56:43 -0800561 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700562 /* Variable will be clobbered before being used - no need for phi */
buzbeefa57c472012-11-21 12:06:18 -0800563 if (!IsBitSet(phi_bb->data_flow_info->live_in_v, dalvik_reg)) continue;
buzbee311ca162013-02-28 15:56:43 -0800564 MIR *phi = static_cast<MIR*>(NewMem(cu_, sizeof(MIR), true, kAllocDFInfo));
buzbeecbd6d442012-11-17 14:11:25 -0800565 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800566 phi->dalvikInsn.vA = dalvik_reg;
567 phi->offset = phi_bb->start_offset;
buzbee311ca162013-02-28 15:56:43 -0800568 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
buzbeefa57c472012-11-21 12:06:18 -0800569 PrependMIR(phi_bb, phi);
buzbee67bf8852011-08-17 17:51:35 -0700570 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700571 }
buzbee67bf8852011-08-17 17:51:35 -0700572}
573
574/*
575 * Worker function to insert phi-operands with latest SSA names from
576 * predecessor blocks
577 */
buzbee311ca162013-02-28 15:56:43 -0800578bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700579{
Bill Buzbeea114add2012-05-03 15:00:40 -0700580 GrowableListIterator iter;
581 MIR *mir;
buzbee6969d502012-06-15 16:40:31 -0700582 std::vector<int> uses;
buzbeefa57c472012-11-21 12:06:18 -0800583 std::vector<int> incoming_arc;
buzbee67bf8852011-08-17 17:51:35 -0700584
Bill Buzbeea114add2012-05-03 15:00:40 -0700585 /* Phi nodes are at the beginning of each block */
buzbee28c9a832012-11-21 15:39:13 -0800586 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800587 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 return true;
buzbeefa57c472012-11-21 12:06:18 -0800589 int ssa_reg = mir->ssa_rep->defs[0];
590 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800591 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700592
buzbee6969d502012-06-15 16:40:31 -0700593 uses.clear();
buzbeefa57c472012-11-21 12:06:18 -0800594 incoming_arc.clear();
buzbee67bf8852011-08-17 17:51:35 -0700595
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 /* Iterate through the predecessors */
buzbee52a77fc2012-11-20 19:50:46 -0800597 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700598 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800599 BasicBlock* pred_bb =
buzbee52a77fc2012-11-20 19:50:46 -0800600 reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
buzbeefa57c472012-11-21 12:06:18 -0800601 if (!pred_bb) break;
602 int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map[v_reg];
603 uses.push_back(ssa_reg);
604 incoming_arc.push_back(pred_bb->id);
buzbee67bf8852011-08-17 17:51:35 -0700605 }
606
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 /* Count the number of SSA registers for a Dalvik register */
buzbeefa57c472012-11-21 12:06:18 -0800608 int num_uses = uses.size();
609 mir->ssa_rep->num_uses = num_uses;
610 mir->ssa_rep->uses =
buzbee311ca162013-02-28 15:56:43 -0800611 static_cast<int*>(NewMem(cu_, sizeof(int) * num_uses, false, kAllocDFInfo));
buzbeefa57c472012-11-21 12:06:18 -0800612 mir->ssa_rep->fp_use =
buzbee311ca162013-02-28 15:56:43 -0800613 static_cast<bool*>(NewMem(cu_, sizeof(bool) * num_uses, true, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700614 int* incoming =
buzbee311ca162013-02-28 15:56:43 -0800615 static_cast<int*>(NewMem(cu_, sizeof(int) * num_uses, false, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700616 // TODO: Ugly, rework (but don't burden each MIR/LIR for Phi-only needs)
buzbeecbd6d442012-11-17 14:11:25 -0800617 mir->dalvikInsn.vB = reinterpret_cast<uintptr_t>(incoming);
Bill Buzbeea114add2012-05-03 15:00:40 -0700618
Bill Buzbeea114add2012-05-03 15:00:40 -0700619 /* Set the uses array for the phi node */
buzbeefa57c472012-11-21 12:06:18 -0800620 int *use_ptr = mir->ssa_rep->uses;
621 for (int i = 0; i < num_uses; i++) {
622 *use_ptr++ = uses[i];
623 *incoming++ = incoming_arc[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700624 }
625 }
626
627 return true;
buzbee67bf8852011-08-17 17:51:35 -0700628}
629
buzbee311ca162013-02-28 15:56:43 -0800630void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block)
buzbeef0cde542011-09-13 14:55:02 -0700631{
632
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 if (block->visited || block->hidden) return;
634 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700635
Bill Buzbeea114add2012-05-03 15:00:40 -0700636 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800637 DoSSAConversion(block);
638 int map_size = sizeof(int) * cu_->num_dalvik_registers;
buzbeef0cde542011-09-13 14:55:02 -0700639
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 /* Save SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800641 int* saved_ssa_map = static_cast<int*>(NewMem(cu_, map_size, false, kAllocDalvikToSSAMap));
642 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700643
buzbeefa57c472012-11-21 12:06:18 -0800644 if (block->fall_through) {
buzbee311ca162013-02-28 15:56:43 -0800645 DoDFSPreOrderSSARename(block->fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800647 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 }
649 if (block->taken) {
buzbee311ca162013-02-28 15:56:43 -0800650 DoDFSPreOrderSSARename(block->taken);
Bill Buzbeea114add2012-05-03 15:00:40 -0700651 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800652 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 }
buzbeefa57c472012-11-21 12:06:18 -0800654 if (block->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800656 GrowableListIteratorInit(&block->successor_block_list.blocks, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700657 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800658 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800659 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800660 if (successor_block_info == NULL) break;
661 BasicBlock* succ_bb = successor_block_info->block;
buzbee311ca162013-02-28 15:56:43 -0800662 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800664 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700665 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 }
buzbee311ca162013-02-28 15:56:43 -0800667 vreg_to_ssa_map_ = saved_ssa_map;
Bill Buzbeea114add2012-05-03 15:00:40 -0700668 return;
buzbeef0cde542011-09-13 14:55:02 -0700669}
670
buzbee67bf8852011-08-17 17:51:35 -0700671/* Perform SSA transformation for the whole method */
buzbee311ca162013-02-28 15:56:43 -0800672void MIRGraph::SSATransformation()
buzbee67bf8852011-08-17 17:51:35 -0700673{
Bill Buzbeea114add2012-05-03 15:00:40 -0700674 /* Compute the DFS order */
buzbee311ca162013-02-28 15:56:43 -0800675 ComputeDFSOrders();
buzbee67bf8852011-08-17 17:51:35 -0700676
buzbee311ca162013-02-28 15:56:43 -0800677 if (!cu_->disable_dataflow) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700678 /* Compute the dominator info */
buzbee311ca162013-02-28 15:56:43 -0800679 ComputeDominators();
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 }
buzbee67bf8852011-08-17 17:51:35 -0700681
Bill Buzbeea114add2012-05-03 15:00:40 -0700682 /* Allocate data structures in preparation for SSA conversion */
buzbee311ca162013-02-28 15:56:43 -0800683 CompilerInitializeSSAConversion();
buzbee67bf8852011-08-17 17:51:35 -0700684
buzbee311ca162013-02-28 15:56:43 -0800685 if (!cu_->disable_dataflow) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700686 /* Find out the "Dalvik reg def x block" relation */
buzbee311ca162013-02-28 15:56:43 -0800687 ComputeDefBlockMatrix();
buzbee67bf8852011-08-17 17:51:35 -0700688
Bill Buzbeea114add2012-05-03 15:00:40 -0700689 /* Insert phi nodes to dominance frontiers for all variables */
buzbee311ca162013-02-28 15:56:43 -0800690 InsertPhiNodes();
Bill Buzbeea114add2012-05-03 15:00:40 -0700691 }
buzbee67bf8852011-08-17 17:51:35 -0700692
Bill Buzbeea114add2012-05-03 15:00:40 -0700693 /* Rename register names by local defs and phi nodes */
buzbee311ca162013-02-28 15:56:43 -0800694 DataflowIterator iter(this, kAllNodes, false /* not iterative */);
695 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
696 ClearVisitedFlag(bb);
697 }
698 DoDFSPreOrderSSARename(GetEntryBlock());
buzbee67bf8852011-08-17 17:51:35 -0700699
buzbee311ca162013-02-28 15:56:43 -0800700 if (!cu_->disable_dataflow) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 /*
702 * Shared temp bit vector used by each block to count the number of defs
703 * from all the predecessor blocks.
704 */
buzbee311ca162013-02-28 15:56:43 -0800705 temp_ssa_register_v_ = AllocBitVector(cu_, GetNumSSARegs(), false, kBitMapTempSSARegisterV);
buzbee2cfc6392012-05-07 14:51:40 -0700706
Bill Buzbeea114add2012-05-03 15:00:40 -0700707 /* Insert phi-operands with latest SSA names from predecessor blocks */
buzbee311ca162013-02-28 15:56:43 -0800708 DataflowIterator iter(this, kReachableNodes, false /* not iterative */);
709 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
710 InsertPhiNodeOperands(bb);
711 }
712 }
713 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
714 DumpCFG("/sdcard/3_post_ssa_cfg/", false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700715 }
buzbee67bf8852011-08-17 17:51:35 -0700716}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800717
718} // namespace art