blob: b80346286c3daf875442ebaa6864f4f462701f8c [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
Ian Rogerse77493c2014-08-20 15:08:45 -070017#include "base/bit_vector-inl.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080018#include "compiler_internals.h"
Ian Rogers8d3a1172013-06-04 01:13:28 -070019#include "dataflow_iterator-inl.h"
Vladimir Markoc9360ce2014-06-05 20:09:47 +010020#include "utils/scoped_arena_containers.h"
buzbee67bf8852011-08-17 17:51:35 -070021
buzbee1fd33462013-03-25 13:40:45 -070022#define NOTVISITED (-1)
23
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080024namespace art {
25
buzbeea5abf702013-04-12 14:39:29 -070026void MIRGraph::ClearAllVisitedFlags() {
buzbee56c71782013-09-05 17:13:19 -070027 AllNodesIterator iter(this);
buzbeea5abf702013-04-12 14:39:29 -070028 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
29 bb->visited = false;
30 }
31}
32
buzbee311ca162013-02-28 15:56:43 -080033BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) {
buzbeee5f01222012-06-14 15:19:35 -070034 if (bb != NULL) {
35 if (bb->visited || bb->hidden) {
36 bb = NULL;
37 }
38 }
39 return bb;
40}
41
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070042BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb) {
buzbee0d829482013-10-11 15:24:55 -070043 BasicBlock* res = NeedsVisit(GetBasicBlock(bb->fall_through));
buzbeee5f01222012-06-14 15:19:35 -070044 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070045 res = NeedsVisit(GetBasicBlock(bb->taken));
buzbeee5f01222012-06-14 15:19:35 -070046 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070047 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010048 for (SuccessorBlockInfo* sbi : bb->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -070049 res = NeedsVisit(GetBasicBlock(sbi->block));
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070050 if (res != NULL) {
51 break;
52 }
buzbeee5f01222012-06-14 15:19:35 -070053 }
54 }
55 }
56 }
57 return res;
58}
59
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070060void MIRGraph::MarkPreOrder(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070061 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080062 /* Enqueue the pre_order block id */
buzbee0d829482013-10-11 15:24:55 -070063 if (block->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010064 dfs_order_.push_back(block->id);
buzbee0d829482013-10-11 15:24:55 -070065 }
buzbeee5f01222012-06-14 15:19:35 -070066}
67
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070068void MIRGraph::RecordDFSOrders(BasicBlock* block) {
Vladimir Marko7baa6f82014-10-09 18:01:24 +010069 ScopedArenaAllocator allocator(&cu_->arena_stack);
70 ScopedArenaVector<BasicBlock*> succ(allocator.Adapter());
71 succ.reserve(GetNumBlocks());
buzbee311ca162013-02-28 15:56:43 -080072 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070073 succ.push_back(block);
74 while (!succ.empty()) {
75 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080076 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
77 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080078 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080079 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070080 continue;
81 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010082 curr->dfs_id = dfs_post_order_.size();
buzbee0d829482013-10-11 15:24:55 -070083 if (curr->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010084 dfs_post_order_.push_back(curr->id);
buzbee0d829482013-10-11 15:24:55 -070085 }
buzbeee5f01222012-06-14 15:19:35 -070086 succ.pop_back();
87 }
88}
89
buzbee5b537102012-01-17 17:33:47 -080090/* Sort the blocks by the Depth-First-Search */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070091void MIRGraph::ComputeDFSOrders() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010092 /* Clear the DFS pre-order and post-order lists. */
93 dfs_order_.clear();
94 dfs_order_.reserve(GetNumBlocks());
95 dfs_post_order_.clear();
96 dfs_post_order_.reserve(GetNumBlocks());
buzbee5b537102012-01-17 17:33:47 -080097
buzbeee5f01222012-06-14 15:19:35 -070098 // Reset visited flags from all nodes
buzbeea5abf702013-04-12 14:39:29 -070099 ClearAllVisitedFlags();
100
buzbeee5f01222012-06-14 15:19:35 -0700101 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800102 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700103
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100104 num_reachable_blocks_ = dfs_order_.size();
Andreas Gampe44395962014-06-13 13:44:40 -0700105
106 if (num_reachable_blocks_ != num_blocks_) {
Vladimir Markocb873d82014-12-08 15:16:54 +0000107 // Kill all unreachable blocks.
Andreas Gampe44395962014-06-13 13:44:40 -0700108 AllNodesIterator iter(this);
109 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
110 if (!bb->visited) {
Vladimir Markocb873d82014-12-08 15:16:54 +0000111 bb->Kill(this);
Andreas Gampe44395962014-06-13 13:44:40 -0700112 }
113 }
114 }
Vladimir Marko312eb252014-10-07 15:01:57 +0100115 dfs_orders_up_to_date_ = true;
buzbee67bf8852011-08-17 17:51:35 -0700116}
117
118/*
119 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
120 * register idx is defined in BasicBlock bb.
121 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700122bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700123 if (bb->data_flow_info == NULL) {
124 return false;
125 }
buzbee67bf8852011-08-17 17:51:35 -0700126
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100127 for (uint32_t idx : bb->data_flow_info->def_v->Indexes()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 /* Block bb defines register idx */
Vladimir Markof585e542014-11-21 13:41:32 +0000129 temp_.ssa.def_block_matrix[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 }
131 return true;
buzbee67bf8852011-08-17 17:51:35 -0700132}
133
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700134void MIRGraph::ComputeDefBlockMatrix() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700135 int num_registers = GetNumOfCodeAndTempVRs();
136 /* Allocate num_registers bit vector pointers */
Vladimir Marko5229cf12014-10-09 14:57:59 +0100137 DCHECK(temp_scoped_alloc_ != nullptr);
Vladimir Markof585e542014-11-21 13:41:32 +0000138 DCHECK(temp_.ssa.def_block_matrix == nullptr);
139 temp_.ssa.def_block_matrix = static_cast<ArenaBitVector**>(
Vladimir Marko5229cf12014-10-09 14:57:59 +0100140 temp_scoped_alloc_->Alloc(sizeof(ArenaBitVector*) * num_registers, kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 int i;
buzbee67bf8852011-08-17 17:51:35 -0700142
buzbeefa57c472012-11-21 12:06:18 -0800143 /* Initialize num_register vectors with num_blocks bits each */
144 for (i = 0; i < num_registers; i++) {
Vladimir Markof585e542014-11-21 13:41:32 +0000145 temp_.ssa.def_block_matrix[i] = new (temp_scoped_alloc_.get()) ArenaBitVector(
146 arena_, GetNumBlocks(), false, kBitMapBMatrix);
147 temp_.ssa.def_block_matrix[i]->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 }
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700149
buzbee56c71782013-09-05 17:13:19 -0700150 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800151 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
152 FindLocalLiveIn(bb);
153 }
buzbee56c71782013-09-05 17:13:19 -0700154 AllNodesIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800155 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
156 FillDefBlockMatrix(bb);
157 }
buzbee67bf8852011-08-17 17:51:35 -0700158
Bill Buzbeea114add2012-05-03 15:00:40 -0700159 /*
160 * Also set the incoming parameters as defs in the entry block.
161 * Only need to handle the parameters for the outer method.
162 */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700163 int num_regs = GetNumOfCodeVRs();
164 int in_reg = GetFirstInVR();
buzbeefa57c472012-11-21 12:06:18 -0800165 for (; in_reg < num_regs; in_reg++) {
Vladimir Markof585e542014-11-21 13:41:32 +0000166 temp_.ssa.def_block_matrix[in_reg]->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 }
buzbee67bf8852011-08-17 17:51:35 -0700168}
169
buzbeea5abf702013-04-12 14:39:29 -0700170void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100171 // Clear the dominator post-order list.
172 dom_post_order_traversal_.clear();
173 dom_post_order_traversal_.reserve(num_reachable_blocks_);
174
buzbeea5abf702013-04-12 14:39:29 -0700175 ClearAllVisitedFlags();
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100176 DCHECK(temp_scoped_alloc_.get() != nullptr);
177 ScopedArenaVector<std::pair<BasicBlock*, ArenaBitVector::IndexIterator>> work_stack(
178 temp_scoped_alloc_->Adapter());
buzbeea5abf702013-04-12 14:39:29 -0700179 bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100180 work_stack.push_back(std::make_pair(bb, bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700181 while (!work_stack.empty()) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100182 std::pair<BasicBlock*, ArenaBitVector::IndexIterator>* curr = &work_stack.back();
183 BasicBlock* curr_bb = curr->first;
184 ArenaBitVector::IndexIterator* curr_idom_iter = &curr->second;
185 while (!curr_idom_iter->Done() && (NeedsVisit(GetBasicBlock(**curr_idom_iter)) == nullptr)) {
186 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700187 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100188 // NOTE: work_stack.push_back()/pop_back() invalidate curr and curr_idom_iter.
189 if (!curr_idom_iter->Done()) {
190 BasicBlock* new_bb = GetBasicBlock(**curr_idom_iter);
191 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700192 new_bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100193 work_stack.push_back(std::make_pair(new_bb, new_bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700194 } else {
195 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700196 if (curr_bb->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100197 dom_post_order_traversal_.push_back(curr_bb->id);
buzbee0d829482013-10-11 15:24:55 -0700198 }
buzbeea5abf702013-04-12 14:39:29 -0700199 work_stack.pop_back();
buzbee67bf8852011-08-17 17:51:35 -0700200
buzbeea5abf702013-04-12 14:39:29 -0700201 /* hacky loop detection */
buzbee0d829482013-10-11 15:24:55 -0700202 if ((curr_bb->taken != NullBasicBlockId) && curr_bb->dominators->IsBitSet(curr_bb->taken)) {
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000203 curr_bb->nesting_depth++;
buzbeea5abf702013-04-12 14:39:29 -0700204 attributes_ |= METHOD_HAS_LOOP;
205 }
206 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 }
buzbee67bf8852011-08-17 17:51:35 -0700208}
209
buzbee311ca162013-02-28 15:56:43 -0800210void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700211 const BasicBlock* succ_bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 /*
213 * TODO - evaluate whether phi will ever need to be inserted into exit
214 * blocks.
215 */
buzbee0d829482013-10-11 15:24:55 -0700216 if (succ_bb->i_dom != dom_bb->id &&
buzbeefa57c472012-11-21 12:06:18 -0800217 succ_bb->block_type == kDalvikByteCode &&
218 succ_bb->hidden == false) {
buzbee862a7602013-04-05 10:58:54 -0700219 dom_bb->dom_frontier->SetBit(succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700220 }
buzbee67bf8852011-08-17 17:51:35 -0700221}
222
223/* Worker function to compute the dominance frontier */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700224bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 /* Calculate DF_local */
buzbee0d829482013-10-11 15:24:55 -0700226 if (bb->taken != NullBasicBlockId) {
227 CheckForDominanceFrontier(bb, GetBasicBlock(bb->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 }
buzbee0d829482013-10-11 15:24:55 -0700229 if (bb->fall_through != NullBasicBlockId) {
230 CheckForDominanceFrontier(bb, GetBasicBlock(bb->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 }
buzbee0d829482013-10-11 15:24:55 -0700232 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100233 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
234 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
235 CheckForDominanceFrontier(bb, succ_bb);
236 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 }
buzbee67bf8852011-08-17 17:51:35 -0700238
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 /* Calculate DF_up */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100240 for (uint32_t dominated_idx : bb->i_dominated->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700241 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100242 for (uint32_t df_up_block_idx : dominated_bb->dom_frontier->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700243 BasicBlock* df_up_block = GetBasicBlock(df_up_block_idx);
buzbee311ca162013-02-28 15:56:43 -0800244 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700245 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 }
buzbee67bf8852011-08-17 17:51:35 -0700247
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 return true;
buzbee67bf8852011-08-17 17:51:35 -0700249}
250
251/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700252void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800253 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700254
Brian Carlstromdf629502013-07-17 22:39:56 -0700255 if (bb->dominators == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700256 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700257 true /* expandable */, kBitMapDominators);
buzbee862a7602013-04-05 10:58:54 -0700258 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700259 true /* expandable */, kBitMapIDominated);
buzbee862a7602013-04-05 10:58:54 -0700260 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700261 true /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700262 } else {
buzbee862a7602013-04-05 10:58:54 -0700263 bb->dominators->ClearAllBits();
264 bb->i_dominated->ClearAllBits();
265 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 }
267 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700268 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700269
buzbee862a7602013-04-05 10:58:54 -0700270 return;
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 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700278int MIRGraph::FindCommonParent(int block1, int block2) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 while (block1 != block2) {
280 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800281 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700282 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800283 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800285 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 DCHECK_NE(block2, NOTVISITED);
287 }
288 }
289 return block1;
buzbee5b537102012-01-17 17:33:47 -0800290}
291
292/* Worker function to compute each block's immediate dominator */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700293bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700295 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800296 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 }
298
299 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100300 auto it = bb->predecessors.begin(), end = bb->predecessors.end();
Bill Buzbeea114add2012-05-03 15:00:40 -0700301
302 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700303 int idom = -1;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100304 for ( ; ; ++it) {
305 CHECK(it != end);
306 BasicBlock* pred_bb = GetBasicBlock(*it);
307 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800308 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800309 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 break;
311 }
312 }
313
314 /* Scan the rest of the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100315 for ( ; it != end; ++it) {
316 BasicBlock* pred_bb = GetBasicBlock(*it);
317 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800318 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 continue;
320 } else {
buzbee311ca162013-02-28 15:56:43 -0800321 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700322 }
323 }
324
325 DCHECK_NE(idom, NOTVISITED);
326
327 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800328 if (i_dom_list_[bb->dfs_id] != idom) {
329 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 return true;
331 }
332 return false;
buzbee5b537102012-01-17 17:33:47 -0800333}
334
335/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700336bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800337 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700338 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 } else {
buzbee0d829482013-10-11 15:24:55 -0700340 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 }
buzbee862a7602013-04-05 10:58:54 -0700342 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 return false;
buzbee5b537102012-01-17 17:33:47 -0800344}
345
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700346bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800347 if (bb != GetEntryBlock()) {
348 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800349 DCHECK_NE(idom_dfs_idx, NOTVISITED);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100350 int i_dom_idx = dfs_post_order_[idom_dfs_idx];
buzbee311ca162013-02-28 15:56:43 -0800351 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700352 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800353 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700354 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700355 }
356 return false;
buzbee5b537102012-01-17 17:33:47 -0800357}
358
buzbee67bf8852011-08-17 17:51:35 -0700359/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700360void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800361 int num_reachable_blocks = num_reachable_blocks_;
buzbee67bf8852011-08-17 17:51:35 -0700362
Bill Buzbeea114add2012-05-03 15:00:40 -0700363 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700364 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800365 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
366 InitializeDominationInfo(bb);
367 }
buzbee67bf8852011-08-17 17:51:35 -0700368
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700369 /* Initialize & Clear i_dom_list */
370 if (max_num_reachable_blocks_ < num_reachable_blocks_) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700371 i_dom_list_ = static_cast<int*>(arena_->Alloc(sizeof(int) * num_reachable_blocks,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000372 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 }
buzbeefa57c472012-11-21 12:06:18 -0800374 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800375 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 }
buzbee5b537102012-01-17 17:33:47 -0800377
buzbeefa57c472012-11-21 12:06:18 -0800378 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800379 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
380 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800381
Bill Buzbeea114add2012-05-03 15:00:40 -0700382 /* Compute the immediate dominators */
buzbee56c71782013-09-05 17:13:19 -0700383 RepeatingReversePostOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800384 bool change = false;
385 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
386 change = ComputeblockIDom(bb);
387 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700388
389 /* Set the dominator for the root node */
buzbee862a7602013-04-05 10:58:54 -0700390 GetEntryBlock()->dominators->ClearAllBits();
391 GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700392
buzbee0d829482013-10-11 15:24:55 -0700393 GetEntryBlock()->i_dom = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700394
buzbee56c71782013-09-05 17:13:19 -0700395 PreOrderDfsIterator iter3(this);
buzbee311ca162013-02-28 15:56:43 -0800396 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
397 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700398 }
buzbee67bf8852011-08-17 17:51:35 -0700399
buzbee56c71782013-09-05 17:13:19 -0700400 ReversePostOrderDfsIterator iter4(this);
buzbee311ca162013-02-28 15:56:43 -0800401 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
402 ComputeBlockDominators(bb);
403 }
buzbee5b537102012-01-17 17:33:47 -0800404
buzbeea5abf702013-04-12 14:39:29 -0700405 // Compute the dominance frontier for each block.
buzbee311ca162013-02-28 15:56:43 -0800406 ComputeDomPostOrderTraversal(GetEntryBlock());
buzbee56c71782013-09-05 17:13:19 -0700407 PostOrderDOMIterator iter5(this);
buzbee311ca162013-02-28 15:56:43 -0800408 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
409 ComputeDominanceFrontier(bb);
410 }
buzbee67bf8852011-08-17 17:51:35 -0700411}
412
413/*
414 * Perform dest U= src1 ^ ~src2
415 * This is probably not general enough to be placed in BitVector.[ch].
416 */
buzbee311ca162013-02-28 15:56:43 -0800417void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700418 const ArenaBitVector* src2) {
buzbee862a7602013-04-05 10:58:54 -0700419 if (dest->GetStorageSize() != src1->GetStorageSize() ||
420 dest->GetStorageSize() != src2->GetStorageSize() ||
421 dest->IsExpandable() != src1->IsExpandable() ||
422 dest->IsExpandable() != src2->IsExpandable()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 LOG(FATAL) << "Incompatible set properties";
424 }
buzbee67bf8852011-08-17 17:51:35 -0700425
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 unsigned int idx;
buzbee862a7602013-04-05 10:58:54 -0700427 for (idx = 0; idx < dest->GetStorageSize(); idx++) {
428 dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 }
buzbee67bf8852011-08-17 17:51:35 -0700430}
431
432/*
433 * Iterate through all successor blocks and propagate up the live-in sets.
434 * The calculated result is used for phi-node pruning - where we only need to
435 * insert a phi node if the variable is live-in to the block.
436 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700437bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) {
Vladimir Markof585e542014-11-21 13:41:32 +0000438 DCHECK_EQ(temp_.ssa.num_vregs, cu_->mir_graph.get()->GetNumOfCodeAndTempVRs());
439 ArenaBitVector* temp_live_vregs = temp_.ssa.work_live_vregs;
buzbee67bf8852011-08-17 17:51:35 -0700440
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700441 if (bb->data_flow_info == NULL) {
442 return false;
443 }
Vladimir Markof585e542014-11-21 13:41:32 +0000444 temp_live_vregs->Copy(bb->data_flow_info->live_in_v);
buzbee0d829482013-10-11 15:24:55 -0700445 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
446 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
447 if (bb_taken && bb_taken->data_flow_info)
Vladimir Markof585e542014-11-21 13:41:32 +0000448 ComputeSuccLineIn(temp_live_vregs, bb_taken->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800449 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700450 if (bb_fall_through && bb_fall_through->data_flow_info)
Vladimir Markof585e542014-11-21 13:41:32 +0000451 ComputeSuccLineIn(temp_live_vregs, bb_fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800452 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700453 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100454 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -0700455 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbeefa57c472012-11-21 12:06:18 -0800456 if (succ_bb->data_flow_info) {
Vladimir Markof585e542014-11-21 13:41:32 +0000457 ComputeSuccLineIn(temp_live_vregs, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800458 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 }
buzbee67bf8852011-08-17 17:51:35 -0700460 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 }
Vladimir Markof585e542014-11-21 13:41:32 +0000462 if (!temp_live_vregs->Equal(bb->data_flow_info->live_in_v)) {
463 bb->data_flow_info->live_in_v->Copy(temp_live_vregs);
Bill Buzbeea114add2012-05-03 15:00:40 -0700464 return true;
465 }
466 return false;
buzbee67bf8852011-08-17 17:51:35 -0700467}
468
469/* Insert phi nodes to for each variable to the dominance frontiers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700470void MIRGraph::InsertPhiNodes() {
buzbeefa57c472012-11-21 12:06:18 -0800471 int dalvik_reg;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100472 ArenaBitVector* phi_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
473 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapPhi);
474 ArenaBitVector* input_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
475 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700476
buzbee56c71782013-09-05 17:13:19 -0700477 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800478 bool change = false;
479 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
480 change = ComputeBlockLiveIns(bb);
481 }
buzbee67bf8852011-08-17 17:51:35 -0700482
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 /* Iterate through each Dalvik register */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700484 for (dalvik_reg = GetNumOfCodeAndTempVRs() - 1; dalvik_reg >= 0; dalvik_reg--) {
Vladimir Markof585e542014-11-21 13:41:32 +0000485 input_blocks->Copy(temp_.ssa.def_block_matrix[dalvik_reg]);
buzbee862a7602013-04-05 10:58:54 -0700486 phi_blocks->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 do {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100488 // TUNING: When we repeat this, we could skip indexes from the previous pass.
489 for (uint32_t idx : input_blocks->Indexes()) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700490 BasicBlock* def_bb = GetBasicBlock(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100491 if (def_bb->dom_frontier != nullptr) {
492 phi_blocks->Union(def_bb->dom_frontier);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700493 }
494 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100495 } while (input_blocks->Union(phi_blocks));
Bill Buzbeea114add2012-05-03 15:00:40 -0700496
497 /*
buzbeefa57c472012-11-21 12:06:18 -0800498 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 * register is in the live-in set.
500 */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100501 for (uint32_t idx : phi_blocks->Indexes()) {
buzbee311ca162013-02-28 15:56:43 -0800502 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 /* Variable will be clobbered before being used - no need for phi */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700504 if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) {
505 continue;
506 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700507 MIR *phi = NewMIR();
buzbeecbd6d442012-11-17 14:11:25 -0800508 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800509 phi->dalvikInsn.vA = dalvik_reg;
510 phi->offset = phi_bb->start_offset;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700511 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700512 phi_bb->PrependMIR(phi);
buzbee67bf8852011-08-17 17:51:35 -0700513 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 }
buzbee67bf8852011-08-17 17:51:35 -0700515}
516
517/*
518 * Worker function to insert phi-operands with latest SSA names from
519 * predecessor blocks
520 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700521bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700522 /* Phi nodes are at the beginning of each block */
buzbee0d829482013-10-11 15:24:55 -0700523 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800524 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700525 return true;
buzbeefa57c472012-11-21 12:06:18 -0800526 int ssa_reg = mir->ssa_rep->defs[0];
527 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800528 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700529
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100531 size_t num_uses = bb->predecessors.size();
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700532 AllocateSSAUseData(mir, num_uses);
533 int* uses = mir->ssa_rep->uses;
buzbee0d829482013-10-11 15:24:55 -0700534 BasicBlockId* incoming =
535 static_cast<BasicBlockId*>(arena_->Alloc(sizeof(BasicBlockId) * num_uses,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000536 kArenaAllocDFInfo));
buzbee0d829482013-10-11 15:24:55 -0700537 mir->meta.phi_incoming = incoming;
538 int idx = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100539 for (BasicBlockId pred_id : bb->predecessors) {
540 BasicBlock* pred_bb = GetBasicBlock(pred_id);
541 DCHECK(pred_bb != nullptr);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800542 uses[idx] = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100543 incoming[idx] = pred_id;
buzbee0d829482013-10-11 15:24:55 -0700544 idx++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 }
546 }
547
548 return true;
buzbee67bf8852011-08-17 17:51:35 -0700549}
550
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700551void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700552 if (block->visited || block->hidden) {
553 return;
554 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700555 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700556
Bill Buzbeea114add2012-05-03 15:00:40 -0700557 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800558 DoSSAConversion(block);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700559 int map_size = sizeof(int) * GetNumOfCodeAndTempVRs();
buzbeef0cde542011-09-13 14:55:02 -0700560
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 /* Save SSA map snapshot */
Vladimir Marko5d474472014-03-21 17:10:58 +0000562 ScopedArenaAllocator allocator(&cu_->arena_stack);
buzbee862a7602013-04-05 10:58:54 -0700563 int* saved_ssa_map =
Vladimir Marko5d474472014-03-21 17:10:58 +0000564 static_cast<int*>(allocator.Alloc(map_size, kArenaAllocDalvikToSSAMap));
buzbee311ca162013-02-28 15:56:43 -0800565 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700566
buzbee0d829482013-10-11 15:24:55 -0700567 if (block->fall_through != NullBasicBlockId) {
568 DoDFSPreOrderSSARename(GetBasicBlock(block->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700569 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800570 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700571 }
buzbee0d829482013-10-11 15:24:55 -0700572 if (block->taken != NullBasicBlockId) {
573 DoDFSPreOrderSSARename(GetBasicBlock(block->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800575 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 }
buzbee0d829482013-10-11 15:24:55 -0700577 if (block->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100578 for (SuccessorBlockInfo* successor_block_info : block->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -0700579 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800580 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700581 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800582 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700583 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700585 return;
buzbeef0cde542011-09-13 14:55:02 -0700586}
587
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800588} // namespace art