blob: dd74976247e2712d126472eb278bbba2c36adc5a [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_) {
107 // Hide all unreachable blocks.
108 AllNodesIterator iter(this);
109 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
110 if (!bb->visited) {
111 bb->Hide(cu_);
112 }
113 }
114 }
buzbee67bf8852011-08-17 17:51:35 -0700115}
116
117/*
118 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
119 * register idx is defined in BasicBlock bb.
120 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700121bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700122 if (bb->data_flow_info == NULL) {
123 return false;
124 }
buzbee67bf8852011-08-17 17:51:35 -0700125
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100126 for (uint32_t idx : bb->data_flow_info->def_v->Indexes()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 /* Block bb defines register idx */
Vladimir Marko5229cf12014-10-09 14:57:59 +0100128 temp_bit_matrix_[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 }
130 return true;
buzbee67bf8852011-08-17 17:51:35 -0700131}
132
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700133void MIRGraph::ComputeDefBlockMatrix() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700134 int num_registers = GetNumOfCodeAndTempVRs();
135 /* Allocate num_registers bit vector pointers */
Vladimir Marko5229cf12014-10-09 14:57:59 +0100136 DCHECK(temp_scoped_alloc_ != nullptr);
137 DCHECK(temp_bit_matrix_ == nullptr);
138 temp_bit_matrix_ = static_cast<ArenaBitVector**>(
139 temp_scoped_alloc_->Alloc(sizeof(ArenaBitVector*) * num_registers, kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700140 int i;
buzbee67bf8852011-08-17 17:51:35 -0700141
buzbeefa57c472012-11-21 12:06:18 -0800142 /* Initialize num_register vectors with num_blocks bits each */
143 for (i = 0; i < num_registers; i++) {
Vladimir Marko5229cf12014-10-09 14:57:59 +0100144 temp_bit_matrix_[i] = new (temp_scoped_alloc_.get()) ArenaBitVector(arena_, GetNumBlocks(),
145 false, kBitMapBMatrix);
146 temp_bit_matrix_[i]->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 }
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700148
buzbee56c71782013-09-05 17:13:19 -0700149 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800150 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
151 FindLocalLiveIn(bb);
152 }
buzbee56c71782013-09-05 17:13:19 -0700153 AllNodesIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800154 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
155 FillDefBlockMatrix(bb);
156 }
buzbee67bf8852011-08-17 17:51:35 -0700157
Bill Buzbeea114add2012-05-03 15:00:40 -0700158 /*
159 * Also set the incoming parameters as defs in the entry block.
160 * Only need to handle the parameters for the outer method.
161 */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700162 int num_regs = GetNumOfCodeVRs();
163 int in_reg = GetFirstInVR();
buzbeefa57c472012-11-21 12:06:18 -0800164 for (; in_reg < num_regs; in_reg++) {
Vladimir Marko5229cf12014-10-09 14:57:59 +0100165 temp_bit_matrix_[in_reg]->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 }
buzbee67bf8852011-08-17 17:51:35 -0700167}
168
buzbeea5abf702013-04-12 14:39:29 -0700169void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100170 // Clear the dominator post-order list.
171 dom_post_order_traversal_.clear();
172 dom_post_order_traversal_.reserve(num_reachable_blocks_);
173
buzbeea5abf702013-04-12 14:39:29 -0700174 ClearAllVisitedFlags();
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100175 DCHECK(temp_scoped_alloc_.get() != nullptr);
176 ScopedArenaVector<std::pair<BasicBlock*, ArenaBitVector::IndexIterator>> work_stack(
177 temp_scoped_alloc_->Adapter());
buzbeea5abf702013-04-12 14:39:29 -0700178 bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100179 work_stack.push_back(std::make_pair(bb, bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700180 while (!work_stack.empty()) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100181 std::pair<BasicBlock*, ArenaBitVector::IndexIterator>* curr = &work_stack.back();
182 BasicBlock* curr_bb = curr->first;
183 ArenaBitVector::IndexIterator* curr_idom_iter = &curr->second;
184 while (!curr_idom_iter->Done() && (NeedsVisit(GetBasicBlock(**curr_idom_iter)) == nullptr)) {
185 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700186 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100187 // NOTE: work_stack.push_back()/pop_back() invalidate curr and curr_idom_iter.
188 if (!curr_idom_iter->Done()) {
189 BasicBlock* new_bb = GetBasicBlock(**curr_idom_iter);
190 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700191 new_bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100192 work_stack.push_back(std::make_pair(new_bb, new_bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700193 } else {
194 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700195 if (curr_bb->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100196 dom_post_order_traversal_.push_back(curr_bb->id);
buzbee0d829482013-10-11 15:24:55 -0700197 }
buzbeea5abf702013-04-12 14:39:29 -0700198 work_stack.pop_back();
buzbee67bf8852011-08-17 17:51:35 -0700199
buzbeea5abf702013-04-12 14:39:29 -0700200 /* hacky loop detection */
buzbee0d829482013-10-11 15:24:55 -0700201 if ((curr_bb->taken != NullBasicBlockId) && curr_bb->dominators->IsBitSet(curr_bb->taken)) {
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000202 curr_bb->nesting_depth++;
buzbeea5abf702013-04-12 14:39:29 -0700203 attributes_ |= METHOD_HAS_LOOP;
204 }
205 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700206 }
buzbee67bf8852011-08-17 17:51:35 -0700207}
208
buzbee311ca162013-02-28 15:56:43 -0800209void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700210 const BasicBlock* succ_bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 /*
212 * TODO - evaluate whether phi will ever need to be inserted into exit
213 * blocks.
214 */
buzbee0d829482013-10-11 15:24:55 -0700215 if (succ_bb->i_dom != dom_bb->id &&
buzbeefa57c472012-11-21 12:06:18 -0800216 succ_bb->block_type == kDalvikByteCode &&
217 succ_bb->hidden == false) {
buzbee862a7602013-04-05 10:58:54 -0700218 dom_bb->dom_frontier->SetBit(succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700219 }
buzbee67bf8852011-08-17 17:51:35 -0700220}
221
222/* Worker function to compute the dominance frontier */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700223bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 /* Calculate DF_local */
buzbee0d829482013-10-11 15:24:55 -0700225 if (bb->taken != NullBasicBlockId) {
226 CheckForDominanceFrontier(bb, GetBasicBlock(bb->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 }
buzbee0d829482013-10-11 15:24:55 -0700228 if (bb->fall_through != NullBasicBlockId) {
229 CheckForDominanceFrontier(bb, GetBasicBlock(bb->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700230 }
buzbee0d829482013-10-11 15:24:55 -0700231 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100232 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
233 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
234 CheckForDominanceFrontier(bb, succ_bb);
235 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 }
buzbee67bf8852011-08-17 17:51:35 -0700237
Bill Buzbeea114add2012-05-03 15:00:40 -0700238 /* Calculate DF_up */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100239 for (uint32_t dominated_idx : bb->i_dominated->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700240 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100241 for (uint32_t df_up_block_idx : dominated_bb->dom_frontier->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700242 BasicBlock* df_up_block = GetBasicBlock(df_up_block_idx);
buzbee311ca162013-02-28 15:56:43 -0800243 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700244 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 }
buzbee67bf8852011-08-17 17:51:35 -0700246
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 return true;
buzbee67bf8852011-08-17 17:51:35 -0700248}
249
250/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700251void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800252 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700253
Brian Carlstromdf629502013-07-17 22:39:56 -0700254 if (bb->dominators == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700255 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700256 true /* expandable */, kBitMapDominators);
buzbee862a7602013-04-05 10:58:54 -0700257 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700258 true /* expandable */, kBitMapIDominated);
buzbee862a7602013-04-05 10:58:54 -0700259 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700260 true /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 } else {
buzbee862a7602013-04-05 10:58:54 -0700262 bb->dominators->ClearAllBits();
263 bb->i_dominated->ClearAllBits();
264 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 }
266 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700267 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700268
buzbee862a7602013-04-05 10:58:54 -0700269 return;
buzbee67bf8852011-08-17 17:51:35 -0700270}
271
buzbee5b537102012-01-17 17:33:47 -0800272/*
buzbeefa57c472012-11-21 12:06:18 -0800273 * Walk through the ordered i_dom list until we reach common parent.
274 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800275 * last element of the intersection of block1 and block2 dominators.
276 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700277int MIRGraph::FindCommonParent(int block1, int block2) {
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 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700292bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700293 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700294 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800295 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 }
297
298 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100299 auto it = bb->predecessors.begin(), end = bb->predecessors.end();
Bill Buzbeea114add2012-05-03 15:00:40 -0700300
301 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700302 int idom = -1;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100303 for ( ; ; ++it) {
304 CHECK(it != end);
305 BasicBlock* pred_bb = GetBasicBlock(*it);
306 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800307 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800308 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 break;
310 }
311 }
312
313 /* Scan the rest of the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100314 for ( ; it != end; ++it) {
315 BasicBlock* pred_bb = GetBasicBlock(*it);
316 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800317 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700318 continue;
319 } else {
buzbee311ca162013-02-28 15:56:43 -0800320 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 }
322 }
323
324 DCHECK_NE(idom, NOTVISITED);
325
326 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800327 if (i_dom_list_[bb->dfs_id] != idom) {
328 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 return true;
330 }
331 return false;
buzbee5b537102012-01-17 17:33:47 -0800332}
333
334/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700335bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800336 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700337 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 } else {
buzbee0d829482013-10-11 15:24:55 -0700339 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700340 }
buzbee862a7602013-04-05 10:58:54 -0700341 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 return false;
buzbee5b537102012-01-17 17:33:47 -0800343}
344
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700345bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800346 if (bb != GetEntryBlock()) {
347 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800348 DCHECK_NE(idom_dfs_idx, NOTVISITED);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100349 int i_dom_idx = dfs_post_order_[idom_dfs_idx];
buzbee311ca162013-02-28 15:56:43 -0800350 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700351 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800352 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700353 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 }
355 return false;
buzbee5b537102012-01-17 17:33:47 -0800356}
357
buzbee67bf8852011-08-17 17:51:35 -0700358/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700359void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800360 int num_reachable_blocks = num_reachable_blocks_;
buzbee67bf8852011-08-17 17:51:35 -0700361
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700363 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800364 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
365 InitializeDominationInfo(bb);
366 }
buzbee67bf8852011-08-17 17:51:35 -0700367
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700368 /* Initialize & Clear i_dom_list */
369 if (max_num_reachable_blocks_ < num_reachable_blocks_) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700370 i_dom_list_ = static_cast<int*>(arena_->Alloc(sizeof(int) * num_reachable_blocks,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000371 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700372 }
buzbeefa57c472012-11-21 12:06:18 -0800373 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800374 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700375 }
buzbee5b537102012-01-17 17:33:47 -0800376
buzbeefa57c472012-11-21 12:06:18 -0800377 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800378 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
379 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800380
Bill Buzbeea114add2012-05-03 15:00:40 -0700381 /* Compute the immediate dominators */
buzbee56c71782013-09-05 17:13:19 -0700382 RepeatingReversePostOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800383 bool change = false;
384 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
385 change = ComputeblockIDom(bb);
386 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700387
388 /* Set the dominator for the root node */
buzbee862a7602013-04-05 10:58:54 -0700389 GetEntryBlock()->dominators->ClearAllBits();
390 GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700391
buzbee0d829482013-10-11 15:24:55 -0700392 GetEntryBlock()->i_dom = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700393
buzbee56c71782013-09-05 17:13:19 -0700394 PreOrderDfsIterator iter3(this);
buzbee311ca162013-02-28 15:56:43 -0800395 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
396 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700397 }
buzbee67bf8852011-08-17 17:51:35 -0700398
buzbee56c71782013-09-05 17:13:19 -0700399 ReversePostOrderDfsIterator iter4(this);
buzbee311ca162013-02-28 15:56:43 -0800400 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
401 ComputeBlockDominators(bb);
402 }
buzbee5b537102012-01-17 17:33:47 -0800403
buzbeea5abf702013-04-12 14:39:29 -0700404 // Compute the dominance frontier for each block.
buzbee311ca162013-02-28 15:56:43 -0800405 ComputeDomPostOrderTraversal(GetEntryBlock());
buzbee56c71782013-09-05 17:13:19 -0700406 PostOrderDOMIterator iter5(this);
buzbee311ca162013-02-28 15:56:43 -0800407 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
408 ComputeDominanceFrontier(bb);
409 }
buzbee67bf8852011-08-17 17:51:35 -0700410}
411
412/*
413 * Perform dest U= src1 ^ ~src2
414 * This is probably not general enough to be placed in BitVector.[ch].
415 */
buzbee311ca162013-02-28 15:56:43 -0800416void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700417 const ArenaBitVector* src2) {
buzbee862a7602013-04-05 10:58:54 -0700418 if (dest->GetStorageSize() != src1->GetStorageSize() ||
419 dest->GetStorageSize() != src2->GetStorageSize() ||
420 dest->IsExpandable() != src1->IsExpandable() ||
421 dest->IsExpandable() != src2->IsExpandable()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 LOG(FATAL) << "Incompatible set properties";
423 }
buzbee67bf8852011-08-17 17:51:35 -0700424
Bill Buzbeea114add2012-05-03 15:00:40 -0700425 unsigned int idx;
buzbee862a7602013-04-05 10:58:54 -0700426 for (idx = 0; idx < dest->GetStorageSize(); idx++) {
427 dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 }
buzbee67bf8852011-08-17 17:51:35 -0700429}
430
431/*
432 * Iterate through all successor blocks and propagate up the live-in sets.
433 * The calculated result is used for phi-node pruning - where we only need to
434 * insert a phi node if the variable is live-in to the block.
435 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700436bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700437 DCHECK_EQ(temp_bit_vector_size_, cu_->mir_graph.get()->GetNumOfCodeAndTempVRs());
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100438 ArenaBitVector* temp_dalvik_register_v = temp_bit_vector_;
buzbee67bf8852011-08-17 17:51:35 -0700439
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700440 if (bb->data_flow_info == NULL) {
441 return false;
442 }
buzbee862a7602013-04-05 10:58:54 -0700443 temp_dalvik_register_v->Copy(bb->data_flow_info->live_in_v);
buzbee0d829482013-10-11 15:24:55 -0700444 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
445 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
446 if (bb_taken && bb_taken->data_flow_info)
447 ComputeSuccLineIn(temp_dalvik_register_v, bb_taken->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800448 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700449 if (bb_fall_through && bb_fall_through->data_flow_info)
450 ComputeSuccLineIn(temp_dalvik_register_v, bb_fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800451 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700452 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100453 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -0700454 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbeefa57c472012-11-21 12:06:18 -0800455 if (succ_bb->data_flow_info) {
buzbee311ca162013-02-28 15:56:43 -0800456 ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800457 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 }
buzbee67bf8852011-08-17 17:51:35 -0700459 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700460 }
buzbee862a7602013-04-05 10:58:54 -0700461 if (!temp_dalvik_register_v->Equal(bb->data_flow_info->live_in_v)) {
462 bb->data_flow_info->live_in_v->Copy(temp_dalvik_register_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 return true;
464 }
465 return false;
buzbee67bf8852011-08-17 17:51:35 -0700466}
467
468/* Insert phi nodes to for each variable to the dominance frontiers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700469void MIRGraph::InsertPhiNodes() {
buzbeefa57c472012-11-21 12:06:18 -0800470 int dalvik_reg;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100471 ArenaBitVector* phi_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
472 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapPhi);
473 ArenaBitVector* input_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
474 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700475
buzbee56c71782013-09-05 17:13:19 -0700476 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800477 bool change = false;
478 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
479 change = ComputeBlockLiveIns(bb);
480 }
buzbee67bf8852011-08-17 17:51:35 -0700481
Bill Buzbeea114add2012-05-03 15:00:40 -0700482 /* Iterate through each Dalvik register */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700483 for (dalvik_reg = GetNumOfCodeAndTempVRs() - 1; dalvik_reg >= 0; dalvik_reg--) {
Vladimir Marko5229cf12014-10-09 14:57:59 +0100484 input_blocks->Copy(temp_bit_matrix_[dalvik_reg]);
buzbee862a7602013-04-05 10:58:54 -0700485 phi_blocks->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 do {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100487 // TUNING: When we repeat this, we could skip indexes from the previous pass.
488 for (uint32_t idx : input_blocks->Indexes()) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700489 BasicBlock* def_bb = GetBasicBlock(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100490 if (def_bb->dom_frontier != nullptr) {
491 phi_blocks->Union(def_bb->dom_frontier);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700492 }
493 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100494 } while (input_blocks->Union(phi_blocks));
Bill Buzbeea114add2012-05-03 15:00:40 -0700495
496 /*
buzbeefa57c472012-11-21 12:06:18 -0800497 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700498 * register is in the live-in set.
499 */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100500 for (uint32_t idx : phi_blocks->Indexes()) {
buzbee311ca162013-02-28 15:56:43 -0800501 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700502 /* Variable will be clobbered before being used - no need for phi */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700503 if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) {
504 continue;
505 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700506 MIR *phi = NewMIR();
buzbeecbd6d442012-11-17 14:11:25 -0800507 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800508 phi->dalvikInsn.vA = dalvik_reg;
509 phi->offset = phi_bb->start_offset;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700510 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700511 phi_bb->PrependMIR(phi);
buzbee67bf8852011-08-17 17:51:35 -0700512 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700513 }
buzbee67bf8852011-08-17 17:51:35 -0700514}
515
516/*
517 * Worker function to insert phi-operands with latest SSA names from
518 * predecessor blocks
519 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700520bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 /* Phi nodes are at the beginning of each block */
buzbee0d829482013-10-11 15:24:55 -0700522 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800523 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 return true;
buzbeefa57c472012-11-21 12:06:18 -0800525 int ssa_reg = mir->ssa_rep->defs[0];
526 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800527 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700528
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100530 size_t num_uses = bb->predecessors.size();
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700531 AllocateSSAUseData(mir, num_uses);
532 int* uses = mir->ssa_rep->uses;
buzbee0d829482013-10-11 15:24:55 -0700533 BasicBlockId* incoming =
534 static_cast<BasicBlockId*>(arena_->Alloc(sizeof(BasicBlockId) * num_uses,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000535 kArenaAllocDFInfo));
buzbee0d829482013-10-11 15:24:55 -0700536 mir->meta.phi_incoming = incoming;
537 int idx = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100538 for (BasicBlockId pred_id : bb->predecessors) {
539 BasicBlock* pred_bb = GetBasicBlock(pred_id);
540 DCHECK(pred_bb != nullptr);
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700541 int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
buzbee0d829482013-10-11 15:24:55 -0700542 uses[idx] = ssa_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