blob: 4a55de689137ecd2d7e84ff5c1c4f7a751edba42 [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) {
48 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
buzbeee5f01222012-06-14 15:19:35 -070049 while (true) {
buzbee862a7602013-04-05 10:58:54 -070050 SuccessorBlockInfo *sbi = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070051 if (sbi == NULL) {
52 break;
53 }
buzbee0d829482013-10-11 15:24:55 -070054 res = NeedsVisit(GetBasicBlock(sbi->block));
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070055 if (res != NULL) {
56 break;
57 }
buzbeee5f01222012-06-14 15:19:35 -070058 }
59 }
60 }
61 }
62 return res;
63}
64
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070065void MIRGraph::MarkPreOrder(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070066 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080067 /* Enqueue the pre_order block id */
buzbee0d829482013-10-11 15:24:55 -070068 if (block->id != NullBasicBlockId) {
69 dfs_order_->Insert(block->id);
70 }
buzbeee5f01222012-06-14 15:19:35 -070071}
72
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070073void MIRGraph::RecordDFSOrders(BasicBlock* block) {
Vladimir Markoc9360ce2014-06-05 20:09:47 +010074 DCHECK(temp_scoped_alloc_.get() != nullptr);
75 ScopedArenaVector<BasicBlock*> succ(temp_scoped_alloc_->Adapter());
buzbee311ca162013-02-28 15:56:43 -080076 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070077 succ.push_back(block);
78 while (!succ.empty()) {
79 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080080 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
81 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080082 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080083 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070084 continue;
85 }
buzbee862a7602013-04-05 10:58:54 -070086 curr->dfs_id = dfs_post_order_->Size();
buzbee0d829482013-10-11 15:24:55 -070087 if (curr->id != NullBasicBlockId) {
88 dfs_post_order_->Insert(curr->id);
89 }
buzbeee5f01222012-06-14 15:19:35 -070090 succ.pop_back();
91 }
92}
93
buzbee5b537102012-01-17 17:33:47 -080094/* Sort the blocks by the Depth-First-Search */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070095void MIRGraph::ComputeDFSOrders() {
buzbeefa57c472012-11-21 12:06:18 -080096 /* Initialize or reset the DFS pre_order list */
buzbee862a7602013-04-05 10:58:54 -070097 if (dfs_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -070098 dfs_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
99 kGrowableArrayDfsOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -0700100 } else {
101 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -0700102 dfs_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 }
buzbee67bf8852011-08-17 17:51:35 -0700104
buzbeefa57c472012-11-21 12:06:18 -0800105 /* Initialize or reset the DFS post_order list */
buzbee862a7602013-04-05 10:58:54 -0700106 if (dfs_post_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -0700107 dfs_post_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
108 kGrowableArrayDfsPostOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 } else {
110 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -0700111 dfs_post_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 }
buzbee5b537102012-01-17 17:33:47 -0800113
buzbeee5f01222012-06-14 15:19:35 -0700114 // Reset visited flags from all nodes
buzbeea5abf702013-04-12 14:39:29 -0700115 ClearAllVisitedFlags();
116
buzbeee5f01222012-06-14 15:19:35 -0700117 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800118 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700119
buzbee862a7602013-04-05 10:58:54 -0700120 num_reachable_blocks_ = dfs_order_->Size();
Andreas Gampe44395962014-06-13 13:44:40 -0700121
122 if (num_reachable_blocks_ != num_blocks_) {
123 // Hide all unreachable blocks.
124 AllNodesIterator iter(this);
125 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
126 if (!bb->visited) {
127 bb->Hide(cu_);
128 }
129 }
130 }
buzbee67bf8852011-08-17 17:51:35 -0700131}
132
133/*
134 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
135 * register idx is defined in BasicBlock bb.
136 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700137bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700138 if (bb->data_flow_info == NULL) {
139 return false;
140 }
buzbee67bf8852011-08-17 17:51:35 -0700141
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100142 for (uint32_t idx : bb->data_flow_info->def_v->Indexes()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 /* Block bb defines register idx */
buzbee862a7602013-04-05 10:58:54 -0700144 def_block_matrix_[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 }
146 return true;
buzbee67bf8852011-08-17 17:51:35 -0700147}
148
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700149void MIRGraph::ComputeDefBlockMatrix() {
buzbee311ca162013-02-28 15:56:43 -0800150 int num_registers = cu_->num_dalvik_registers;
buzbeefa57c472012-11-21 12:06:18 -0800151 /* Allocate num_dalvik_registers bit vector pointers */
buzbee311ca162013-02-28 15:56:43 -0800152 def_block_matrix_ = static_cast<ArenaBitVector**>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700153 (arena_->Alloc(sizeof(ArenaBitVector *) * num_registers,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000154 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700155 int i;
buzbee67bf8852011-08-17 17:51:35 -0700156
buzbeefa57c472012-11-21 12:06:18 -0800157 /* Initialize num_register vectors with num_blocks bits each */
158 for (i = 0; i < num_registers; i++) {
buzbee862a7602013-04-05 10:58:54 -0700159 def_block_matrix_[i] =
160 new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapBMatrix);
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 }
buzbee56c71782013-09-05 17:13:19 -0700162 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800163 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
164 FindLocalLiveIn(bb);
165 }
buzbee56c71782013-09-05 17:13:19 -0700166 AllNodesIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800167 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
168 FillDefBlockMatrix(bb);
169 }
buzbee67bf8852011-08-17 17:51:35 -0700170
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 /*
172 * Also set the incoming parameters as defs in the entry block.
173 * Only need to handle the parameters for the outer method.
174 */
buzbee311ca162013-02-28 15:56:43 -0800175 int num_regs = cu_->num_dalvik_registers;
176 int in_reg = num_regs - cu_->num_ins;
buzbeefa57c472012-11-21 12:06:18 -0800177 for (; in_reg < num_regs; in_reg++) {
buzbee862a7602013-04-05 10:58:54 -0700178 def_block_matrix_[in_reg]->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 }
buzbee67bf8852011-08-17 17:51:35 -0700180}
181
buzbeea5abf702013-04-12 14:39:29 -0700182void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb) {
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700183 if (dom_post_order_traversal_ == NULL || max_num_reachable_blocks_ < num_reachable_blocks_) {
184 // First time or too small - create the array.
buzbeea5abf702013-04-12 14:39:29 -0700185 dom_post_order_traversal_ =
buzbee0d829482013-10-11 15:24:55 -0700186 new (arena_) GrowableArray<BasicBlockId>(arena_, num_reachable_blocks_,
buzbeea5abf702013-04-12 14:39:29 -0700187 kGrowableArrayDomPostOrderTraversal);
188 } else {
189 dom_post_order_traversal_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700190 }
buzbeea5abf702013-04-12 14:39:29 -0700191 ClearAllVisitedFlags();
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100192 DCHECK(temp_scoped_alloc_.get() != nullptr);
193 ScopedArenaVector<std::pair<BasicBlock*, ArenaBitVector::IndexIterator>> work_stack(
194 temp_scoped_alloc_->Adapter());
buzbeea5abf702013-04-12 14:39:29 -0700195 bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100196 work_stack.push_back(std::make_pair(bb, bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700197 while (!work_stack.empty()) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100198 std::pair<BasicBlock*, ArenaBitVector::IndexIterator>* curr = &work_stack.back();
199 BasicBlock* curr_bb = curr->first;
200 ArenaBitVector::IndexIterator* curr_idom_iter = &curr->second;
201 while (!curr_idom_iter->Done() && (NeedsVisit(GetBasicBlock(**curr_idom_iter)) == nullptr)) {
202 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700203 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100204 // NOTE: work_stack.push_back()/pop_back() invalidate curr and curr_idom_iter.
205 if (!curr_idom_iter->Done()) {
206 BasicBlock* new_bb = GetBasicBlock(**curr_idom_iter);
207 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700208 new_bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100209 work_stack.push_back(std::make_pair(new_bb, new_bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700210 } else {
211 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700212 if (curr_bb->id != NullBasicBlockId) {
213 dom_post_order_traversal_->Insert(curr_bb->id);
214 }
buzbeea5abf702013-04-12 14:39:29 -0700215 work_stack.pop_back();
buzbee67bf8852011-08-17 17:51:35 -0700216
buzbeea5abf702013-04-12 14:39:29 -0700217 /* hacky loop detection */
buzbee0d829482013-10-11 15:24:55 -0700218 if ((curr_bb->taken != NullBasicBlockId) && curr_bb->dominators->IsBitSet(curr_bb->taken)) {
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000219 curr_bb->nesting_depth++;
buzbeea5abf702013-04-12 14:39:29 -0700220 attributes_ |= METHOD_HAS_LOOP;
221 }
222 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700223 }
buzbee67bf8852011-08-17 17:51:35 -0700224}
225
buzbee311ca162013-02-28 15:56:43 -0800226void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700227 const BasicBlock* succ_bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 /*
229 * TODO - evaluate whether phi will ever need to be inserted into exit
230 * blocks.
231 */
buzbee0d829482013-10-11 15:24:55 -0700232 if (succ_bb->i_dom != dom_bb->id &&
buzbeefa57c472012-11-21 12:06:18 -0800233 succ_bb->block_type == kDalvikByteCode &&
234 succ_bb->hidden == false) {
buzbee862a7602013-04-05 10:58:54 -0700235 dom_bb->dom_frontier->SetBit(succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 }
buzbee67bf8852011-08-17 17:51:35 -0700237}
238
239/* Worker function to compute the dominance frontier */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700240bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 /* Calculate DF_local */
buzbee0d829482013-10-11 15:24:55 -0700242 if (bb->taken != NullBasicBlockId) {
243 CheckForDominanceFrontier(bb, GetBasicBlock(bb->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 }
buzbee0d829482013-10-11 15:24:55 -0700245 if (bb->fall_through != NullBasicBlockId) {
246 CheckForDominanceFrontier(bb, GetBasicBlock(bb->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 }
buzbee0d829482013-10-11 15:24:55 -0700248 if (bb->successor_block_list_type != kNotUsed) {
249 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700251 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700252 if (successor_block_info == NULL) {
253 break;
254 }
buzbee0d829482013-10-11 15:24:55 -0700255 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800256 CheckForDominanceFrontier(bb, succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 }
258 }
buzbee67bf8852011-08-17 17:51:35 -0700259
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 /* Calculate DF_up */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100261 for (uint32_t dominated_idx : bb->i_dominated->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700262 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100263 for (uint32_t df_up_block_idx : dominated_bb->dom_frontier->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700264 BasicBlock* df_up_block = GetBasicBlock(df_up_block_idx);
buzbee311ca162013-02-28 15:56:43 -0800265 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700266 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700267 }
buzbee67bf8852011-08-17 17:51:35 -0700268
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 return true;
buzbee67bf8852011-08-17 17:51:35 -0700270}
271
272/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700273void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800274 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700275
Brian Carlstromdf629502013-07-17 22:39:56 -0700276 if (bb->dominators == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700277 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
278 false /* expandable */, kBitMapDominators);
279 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
280 false /* expandable */, kBitMapIDominated);
281 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
282 false /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700283 } else {
buzbee862a7602013-04-05 10:58:54 -0700284 bb->dominators->ClearAllBits();
285 bb->i_dominated->ClearAllBits();
286 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700287 }
288 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700289 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700290
buzbee862a7602013-04-05 10:58:54 -0700291 return;
buzbee67bf8852011-08-17 17:51:35 -0700292}
293
buzbee5b537102012-01-17 17:33:47 -0800294/*
buzbeefa57c472012-11-21 12:06:18 -0800295 * Walk through the ordered i_dom list until we reach common parent.
296 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800297 * last element of the intersection of block1 and block2 dominators.
298 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700299int MIRGraph::FindCommonParent(int block1, int block2) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 while (block1 != block2) {
301 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800302 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800304 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800306 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 DCHECK_NE(block2, NOTVISITED);
308 }
309 }
310 return block1;
buzbee5b537102012-01-17 17:33:47 -0800311}
312
313/* Worker function to compute each block's immediate dominator */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700314bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700316 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800317 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700318 }
319
320 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700321 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
Bill Buzbeea114add2012-05-03 15:00:40 -0700322
323 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700324 int idom = -1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700326 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
buzbeefa57c472012-11-21 12:06:18 -0800327 CHECK(pred_bb != NULL);
buzbee311ca162013-02-28 15:56:43 -0800328 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800329 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 break;
331 }
332 }
333
334 /* Scan the rest of the predecessors */
335 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700336 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700337 if (!pred_bb) {
338 break;
339 }
buzbee311ca162013-02-28 15:56:43 -0800340 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 continue;
342 } else {
buzbee311ca162013-02-28 15:56:43 -0800343 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 }
345 }
346
347 DCHECK_NE(idom, NOTVISITED);
348
349 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800350 if (i_dom_list_[bb->dfs_id] != idom) {
351 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 return true;
353 }
354 return false;
buzbee5b537102012-01-17 17:33:47 -0800355}
356
357/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700358bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800359 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700360 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 } else {
buzbee0d829482013-10-11 15:24:55 -0700362 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700363 }
buzbee862a7602013-04-05 10:58:54 -0700364 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700365 return false;
buzbee5b537102012-01-17 17:33:47 -0800366}
367
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700368bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800369 if (bb != GetEntryBlock()) {
370 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800371 DCHECK_NE(idom_dfs_idx, NOTVISITED);
buzbee862a7602013-04-05 10:58:54 -0700372 int i_dom_idx = dfs_post_order_->Get(idom_dfs_idx);
buzbee311ca162013-02-28 15:56:43 -0800373 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700374 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800375 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700376 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 }
378 return false;
buzbee5b537102012-01-17 17:33:47 -0800379}
380
buzbee67bf8852011-08-17 17:51:35 -0700381/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700382void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800383 int num_reachable_blocks = num_reachable_blocks_;
buzbee67bf8852011-08-17 17:51:35 -0700384
Bill Buzbeea114add2012-05-03 15:00:40 -0700385 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700386 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800387 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
388 InitializeDominationInfo(bb);
389 }
buzbee67bf8852011-08-17 17:51:35 -0700390
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700391 /* Initialize & Clear i_dom_list */
392 if (max_num_reachable_blocks_ < num_reachable_blocks_) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700393 i_dom_list_ = static_cast<int*>(arena_->Alloc(sizeof(int) * num_reachable_blocks,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000394 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700395 }
buzbeefa57c472012-11-21 12:06:18 -0800396 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800397 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700398 }
buzbee5b537102012-01-17 17:33:47 -0800399
buzbeefa57c472012-11-21 12:06:18 -0800400 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800401 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
402 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800403
Bill Buzbeea114add2012-05-03 15:00:40 -0700404 /* Compute the immediate dominators */
buzbee56c71782013-09-05 17:13:19 -0700405 RepeatingReversePostOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800406 bool change = false;
407 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
408 change = ComputeblockIDom(bb);
409 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700410
411 /* Set the dominator for the root node */
buzbee862a7602013-04-05 10:58:54 -0700412 GetEntryBlock()->dominators->ClearAllBits();
413 GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700414
buzbee0d829482013-10-11 15:24:55 -0700415 GetEntryBlock()->i_dom = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700416
buzbee56c71782013-09-05 17:13:19 -0700417 PreOrderDfsIterator iter3(this);
buzbee311ca162013-02-28 15:56:43 -0800418 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
419 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700420 }
buzbee67bf8852011-08-17 17:51:35 -0700421
buzbee56c71782013-09-05 17:13:19 -0700422 ReversePostOrderDfsIterator iter4(this);
buzbee311ca162013-02-28 15:56:43 -0800423 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
424 ComputeBlockDominators(bb);
425 }
buzbee5b537102012-01-17 17:33:47 -0800426
buzbeea5abf702013-04-12 14:39:29 -0700427 // Compute the dominance frontier for each block.
buzbee311ca162013-02-28 15:56:43 -0800428 ComputeDomPostOrderTraversal(GetEntryBlock());
buzbee56c71782013-09-05 17:13:19 -0700429 PostOrderDOMIterator iter5(this);
buzbee311ca162013-02-28 15:56:43 -0800430 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
431 ComputeDominanceFrontier(bb);
432 }
buzbee67bf8852011-08-17 17:51:35 -0700433}
434
435/*
436 * Perform dest U= src1 ^ ~src2
437 * This is probably not general enough to be placed in BitVector.[ch].
438 */
buzbee311ca162013-02-28 15:56:43 -0800439void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700440 const ArenaBitVector* src2) {
buzbee862a7602013-04-05 10:58:54 -0700441 if (dest->GetStorageSize() != src1->GetStorageSize() ||
442 dest->GetStorageSize() != src2->GetStorageSize() ||
443 dest->IsExpandable() != src1->IsExpandable() ||
444 dest->IsExpandable() != src2->IsExpandable()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 LOG(FATAL) << "Incompatible set properties";
446 }
buzbee67bf8852011-08-17 17:51:35 -0700447
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 unsigned int idx;
buzbee862a7602013-04-05 10:58:54 -0700449 for (idx = 0; idx < dest->GetStorageSize(); idx++) {
450 dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700451 }
buzbee67bf8852011-08-17 17:51:35 -0700452}
453
454/*
455 * Iterate through all successor blocks and propagate up the live-in sets.
456 * The calculated result is used for phi-node pruning - where we only need to
457 * insert a phi node if the variable is live-in to the block.
458 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700459bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100460 DCHECK_EQ(temp_bit_vector_size_, cu_->num_dalvik_registers);
461 ArenaBitVector* temp_dalvik_register_v = temp_bit_vector_;
buzbee67bf8852011-08-17 17:51:35 -0700462
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700463 if (bb->data_flow_info == NULL) {
464 return false;
465 }
buzbee862a7602013-04-05 10:58:54 -0700466 temp_dalvik_register_v->Copy(bb->data_flow_info->live_in_v);
buzbee0d829482013-10-11 15:24:55 -0700467 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
468 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
469 if (bb_taken && bb_taken->data_flow_info)
470 ComputeSuccLineIn(temp_dalvik_register_v, bb_taken->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800471 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700472 if (bb_fall_through && bb_fall_through->data_flow_info)
473 ComputeSuccLineIn(temp_dalvik_register_v, bb_fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800474 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700475 if (bb->successor_block_list_type != kNotUsed) {
476 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700477 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700478 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700479 if (successor_block_info == NULL) {
480 break;
481 }
buzbee0d829482013-10-11 15:24:55 -0700482 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbeefa57c472012-11-21 12:06:18 -0800483 if (succ_bb->data_flow_info) {
buzbee311ca162013-02-28 15:56:43 -0800484 ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800485 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 }
buzbee67bf8852011-08-17 17:51:35 -0700487 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700488 }
buzbee862a7602013-04-05 10:58:54 -0700489 if (!temp_dalvik_register_v->Equal(bb->data_flow_info->live_in_v)) {
490 bb->data_flow_info->live_in_v->Copy(temp_dalvik_register_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 return true;
492 }
493 return false;
buzbee67bf8852011-08-17 17:51:35 -0700494}
495
496/* Insert phi nodes to for each variable to the dominance frontiers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700497void MIRGraph::InsertPhiNodes() {
buzbeefa57c472012-11-21 12:06:18 -0800498 int dalvik_reg;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100499 ArenaBitVector* phi_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
500 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapPhi);
501 ArenaBitVector* input_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
502 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700503
buzbee56c71782013-09-05 17:13:19 -0700504 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800505 bool change = false;
506 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
507 change = ComputeBlockLiveIns(bb);
508 }
buzbee67bf8852011-08-17 17:51:35 -0700509
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 /* Iterate through each Dalvik register */
buzbee311ca162013-02-28 15:56:43 -0800511 for (dalvik_reg = cu_->num_dalvik_registers - 1; dalvik_reg >= 0; dalvik_reg--) {
buzbee862a7602013-04-05 10:58:54 -0700512 input_blocks->Copy(def_block_matrix_[dalvik_reg]);
513 phi_blocks->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 do {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100515 // TUNING: When we repeat this, we could skip indexes from the previous pass.
516 for (uint32_t idx : input_blocks->Indexes()) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700517 BasicBlock* def_bb = GetBasicBlock(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100518 if (def_bb->dom_frontier != nullptr) {
519 phi_blocks->Union(def_bb->dom_frontier);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700520 }
521 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100522 } while (input_blocks->Union(phi_blocks));
Bill Buzbeea114add2012-05-03 15:00:40 -0700523
524 /*
buzbeefa57c472012-11-21 12:06:18 -0800525 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 * register is in the live-in set.
527 */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100528 for (uint32_t idx : phi_blocks->Indexes()) {
buzbee311ca162013-02-28 15:56:43 -0800529 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 /* Variable will be clobbered before being used - no need for phi */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700531 if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) {
532 continue;
533 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700534 MIR *phi = NewMIR();
buzbeecbd6d442012-11-17 14:11:25 -0800535 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800536 phi->dalvikInsn.vA = dalvik_reg;
537 phi->offset = phi_bb->start_offset;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700538 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700539 phi_bb->PrependMIR(phi);
buzbee67bf8852011-08-17 17:51:35 -0700540 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 }
buzbee67bf8852011-08-17 17:51:35 -0700542}
543
544/*
545 * Worker function to insert phi-operands with latest SSA names from
546 * predecessor blocks
547 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700548bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700549 /* Phi nodes are at the beginning of each block */
buzbee0d829482013-10-11 15:24:55 -0700550 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800551 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700552 return true;
buzbeefa57c472012-11-21 12:06:18 -0800553 int ssa_reg = mir->ssa_rep->defs[0];
554 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800555 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700556
Bill Buzbeea114add2012-05-03 15:00:40 -0700557 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700558 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
559 size_t num_uses = bb->predecessors->Size();
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700560 AllocateSSAUseData(mir, num_uses);
561 int* uses = mir->ssa_rep->uses;
buzbee0d829482013-10-11 15:24:55 -0700562 BasicBlockId* incoming =
563 static_cast<BasicBlockId*>(arena_->Alloc(sizeof(BasicBlockId) * num_uses,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000564 kArenaAllocDFInfo));
buzbee0d829482013-10-11 15:24:55 -0700565 mir->meta.phi_incoming = incoming;
566 int idx = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700567 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700568 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700569 if (!pred_bb) {
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700570 break;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700571 }
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700572 int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
buzbee0d829482013-10-11 15:24:55 -0700573 uses[idx] = ssa_reg;
574 incoming[idx] = pred_bb->id;
575 idx++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 }
577 }
578
579 return true;
buzbee67bf8852011-08-17 17:51:35 -0700580}
581
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700582void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700583 if (block->visited || block->hidden) {
584 return;
585 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700586 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700587
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800589 DoSSAConversion(block);
590 int map_size = sizeof(int) * cu_->num_dalvik_registers;
buzbeef0cde542011-09-13 14:55:02 -0700591
Bill Buzbeea114add2012-05-03 15:00:40 -0700592 /* Save SSA map snapshot */
Vladimir Marko5d474472014-03-21 17:10:58 +0000593 ScopedArenaAllocator allocator(&cu_->arena_stack);
buzbee862a7602013-04-05 10:58:54 -0700594 int* saved_ssa_map =
Vladimir Marko5d474472014-03-21 17:10:58 +0000595 static_cast<int*>(allocator.Alloc(map_size, kArenaAllocDalvikToSSAMap));
buzbee311ca162013-02-28 15:56:43 -0800596 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700597
buzbee0d829482013-10-11 15:24:55 -0700598 if (block->fall_through != NullBasicBlockId) {
599 DoDFSPreOrderSSARename(GetBasicBlock(block->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700600 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800601 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700602 }
buzbee0d829482013-10-11 15:24:55 -0700603 if (block->taken != NullBasicBlockId) {
604 DoDFSPreOrderSSARename(GetBasicBlock(block->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700605 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800606 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 }
buzbee0d829482013-10-11 15:24:55 -0700608 if (block->successor_block_list_type != kNotUsed) {
609 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(block->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700610 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700611 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700612 if (successor_block_info == NULL) {
613 break;
614 }
buzbee0d829482013-10-11 15:24:55 -0700615 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800616 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700617 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800618 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700619 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700620 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700621 return;
buzbeef0cde542011-09-13 14:55:02 -0700622}
623
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800624} // namespace art