blob: e26745ad5e5b8f6050049c3c5faffadacc8fe0ad [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbeeeaf09bc2012-11-15 14:51:41 -080017#include "compiler_internals.h"
Ian Rogers8d3a1172013-06-04 01:13:28 -070018#include "dataflow_iterator-inl.h"
Vladimir Markoc9360ce2014-06-05 20:09:47 +010019#include "utils/scoped_arena_containers.h"
buzbee67bf8852011-08-17 17:51:35 -070020
buzbee1fd33462013-03-25 13:40:45 -070021#define NOTVISITED (-1)
22
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080023namespace art {
24
buzbeea5abf702013-04-12 14:39:29 -070025void MIRGraph::ClearAllVisitedFlags() {
buzbee56c71782013-09-05 17:13:19 -070026 AllNodesIterator iter(this);
buzbeea5abf702013-04-12 14:39:29 -070027 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
28 bb->visited = false;
29 }
30}
31
buzbee311ca162013-02-28 15:56:43 -080032BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) {
buzbeee5f01222012-06-14 15:19:35 -070033 if (bb != NULL) {
34 if (bb->visited || bb->hidden) {
35 bb = NULL;
36 }
37 }
38 return bb;
39}
40
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070041BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb) {
buzbee0d829482013-10-11 15:24:55 -070042 BasicBlock* res = NeedsVisit(GetBasicBlock(bb->fall_through));
buzbeee5f01222012-06-14 15:19:35 -070043 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070044 res = NeedsVisit(GetBasicBlock(bb->taken));
buzbeee5f01222012-06-14 15:19:35 -070045 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070046 if (bb->successor_block_list_type != kNotUsed) {
47 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
buzbeee5f01222012-06-14 15:19:35 -070048 while (true) {
buzbee862a7602013-04-05 10:58:54 -070049 SuccessorBlockInfo *sbi = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070050 if (sbi == NULL) {
51 break;
52 }
buzbee0d829482013-10-11 15:24:55 -070053 res = NeedsVisit(GetBasicBlock(sbi->block));
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070054 if (res != NULL) {
55 break;
56 }
buzbeee5f01222012-06-14 15:19:35 -070057 }
58 }
59 }
60 }
61 return res;
62}
63
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070064void MIRGraph::MarkPreOrder(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070065 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080066 /* Enqueue the pre_order block id */
buzbee0d829482013-10-11 15:24:55 -070067 if (block->id != NullBasicBlockId) {
68 dfs_order_->Insert(block->id);
69 }
buzbeee5f01222012-06-14 15:19:35 -070070}
71
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070072void MIRGraph::RecordDFSOrders(BasicBlock* block) {
Vladimir Markoc9360ce2014-06-05 20:09:47 +010073 DCHECK(temp_scoped_alloc_.get() != nullptr);
74 ScopedArenaVector<BasicBlock*> succ(temp_scoped_alloc_->Adapter());
buzbee311ca162013-02-28 15:56:43 -080075 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070076 succ.push_back(block);
77 while (!succ.empty()) {
78 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080079 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
80 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080081 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080082 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070083 continue;
84 }
buzbee862a7602013-04-05 10:58:54 -070085 curr->dfs_id = dfs_post_order_->Size();
buzbee0d829482013-10-11 15:24:55 -070086 if (curr->id != NullBasicBlockId) {
87 dfs_post_order_->Insert(curr->id);
88 }
buzbeee5f01222012-06-14 15:19:35 -070089 succ.pop_back();
90 }
91}
92
buzbee5b537102012-01-17 17:33:47 -080093/* Sort the blocks by the Depth-First-Search */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070094void MIRGraph::ComputeDFSOrders() {
buzbeefa57c472012-11-21 12:06:18 -080095 /* Initialize or reset the DFS pre_order list */
buzbee862a7602013-04-05 10:58:54 -070096 if (dfs_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -070097 dfs_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
98 kGrowableArrayDfsOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -070099 } else {
100 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -0700101 dfs_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700102 }
buzbee67bf8852011-08-17 17:51:35 -0700103
buzbeefa57c472012-11-21 12:06:18 -0800104 /* Initialize or reset the DFS post_order list */
buzbee862a7602013-04-05 10:58:54 -0700105 if (dfs_post_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -0700106 dfs_post_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
107 kGrowableArrayDfsPostOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -0700108 } else {
109 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -0700110 dfs_post_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700111 }
buzbee5b537102012-01-17 17:33:47 -0800112
buzbeee5f01222012-06-14 15:19:35 -0700113 // Reset visited flags from all nodes
buzbeea5abf702013-04-12 14:39:29 -0700114 ClearAllVisitedFlags();
115
buzbeee5f01222012-06-14 15:19:35 -0700116 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800117 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700118
buzbee862a7602013-04-05 10:58:54 -0700119 num_reachable_blocks_ = dfs_order_->Size();
Andreas Gampe44395962014-06-13 13:44:40 -0700120
121 if (num_reachable_blocks_ != num_blocks_) {
122 // Hide all unreachable blocks.
123 AllNodesIterator iter(this);
124 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
125 if (!bb->visited) {
126 bb->Hide(cu_);
127 }
128 }
129 }
buzbee67bf8852011-08-17 17:51:35 -0700130}
131
132/*
133 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
134 * register idx is defined in BasicBlock bb.
135 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700136bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700137 if (bb->data_flow_info == NULL) {
138 return false;
139 }
buzbee67bf8852011-08-17 17:51:35 -0700140
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100141 for (uint32_t idx : bb->data_flow_info->def_v->Indexes()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 /* Block bb defines register idx */
buzbee862a7602013-04-05 10:58:54 -0700143 def_block_matrix_[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 }
145 return true;
buzbee67bf8852011-08-17 17:51:35 -0700146}
147
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700148void MIRGraph::ComputeDefBlockMatrix() {
buzbee311ca162013-02-28 15:56:43 -0800149 int num_registers = cu_->num_dalvik_registers;
buzbeefa57c472012-11-21 12:06:18 -0800150 /* Allocate num_dalvik_registers bit vector pointers */
buzbee311ca162013-02-28 15:56:43 -0800151 def_block_matrix_ = static_cast<ArenaBitVector**>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700152 (arena_->Alloc(sizeof(ArenaBitVector *) * num_registers,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000153 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 int i;
buzbee67bf8852011-08-17 17:51:35 -0700155
buzbeefa57c472012-11-21 12:06:18 -0800156 /* Initialize num_register vectors with num_blocks bits each */
157 for (i = 0; i < num_registers; i++) {
buzbee862a7602013-04-05 10:58:54 -0700158 def_block_matrix_[i] =
159 new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapBMatrix);
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 }
buzbee56c71782013-09-05 17:13:19 -0700161 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800162 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
163 FindLocalLiveIn(bb);
164 }
buzbee56c71782013-09-05 17:13:19 -0700165 AllNodesIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800166 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
167 FillDefBlockMatrix(bb);
168 }
buzbee67bf8852011-08-17 17:51:35 -0700169
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 /*
171 * Also set the incoming parameters as defs in the entry block.
172 * Only need to handle the parameters for the outer method.
173 */
buzbee311ca162013-02-28 15:56:43 -0800174 int num_regs = cu_->num_dalvik_registers;
175 int in_reg = num_regs - cu_->num_ins;
buzbeefa57c472012-11-21 12:06:18 -0800176 for (; in_reg < num_regs; in_reg++) {
buzbee862a7602013-04-05 10:58:54 -0700177 def_block_matrix_[in_reg]->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 }
buzbee67bf8852011-08-17 17:51:35 -0700179}
180
buzbeea5abf702013-04-12 14:39:29 -0700181void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb) {
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700182 if (dom_post_order_traversal_ == NULL || max_num_reachable_blocks_ < num_reachable_blocks_) {
183 // First time or too small - create the array.
buzbeea5abf702013-04-12 14:39:29 -0700184 dom_post_order_traversal_ =
buzbee0d829482013-10-11 15:24:55 -0700185 new (arena_) GrowableArray<BasicBlockId>(arena_, num_reachable_blocks_,
buzbeea5abf702013-04-12 14:39:29 -0700186 kGrowableArrayDomPostOrderTraversal);
187 } else {
188 dom_post_order_traversal_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 }
buzbeea5abf702013-04-12 14:39:29 -0700190 ClearAllVisitedFlags();
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100191 DCHECK(temp_scoped_alloc_.get() != nullptr);
192 ScopedArenaVector<std::pair<BasicBlock*, ArenaBitVector::IndexIterator>> work_stack(
193 temp_scoped_alloc_->Adapter());
buzbeea5abf702013-04-12 14:39:29 -0700194 bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100195 work_stack.push_back(std::make_pair(bb, bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700196 while (!work_stack.empty()) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100197 std::pair<BasicBlock*, ArenaBitVector::IndexIterator>* curr = &work_stack.back();
198 BasicBlock* curr_bb = curr->first;
199 ArenaBitVector::IndexIterator* curr_idom_iter = &curr->second;
200 while (!curr_idom_iter->Done() && (NeedsVisit(GetBasicBlock(**curr_idom_iter)) == nullptr)) {
201 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700202 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100203 // NOTE: work_stack.push_back()/pop_back() invalidate curr and curr_idom_iter.
204 if (!curr_idom_iter->Done()) {
205 BasicBlock* new_bb = GetBasicBlock(**curr_idom_iter);
206 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700207 new_bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100208 work_stack.push_back(std::make_pair(new_bb, new_bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700209 } else {
210 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700211 if (curr_bb->id != NullBasicBlockId) {
212 dom_post_order_traversal_->Insert(curr_bb->id);
213 }
buzbeea5abf702013-04-12 14:39:29 -0700214 work_stack.pop_back();
buzbee67bf8852011-08-17 17:51:35 -0700215
buzbeea5abf702013-04-12 14:39:29 -0700216 /* hacky loop detection */
buzbee0d829482013-10-11 15:24:55 -0700217 if ((curr_bb->taken != NullBasicBlockId) && curr_bb->dominators->IsBitSet(curr_bb->taken)) {
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000218 curr_bb->nesting_depth++;
buzbeea5abf702013-04-12 14:39:29 -0700219 attributes_ |= METHOD_HAS_LOOP;
220 }
221 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 }
buzbee67bf8852011-08-17 17:51:35 -0700223}
224
buzbee311ca162013-02-28 15:56:43 -0800225void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700226 const BasicBlock* succ_bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 /*
228 * TODO - evaluate whether phi will ever need to be inserted into exit
229 * blocks.
230 */
buzbee0d829482013-10-11 15:24:55 -0700231 if (succ_bb->i_dom != dom_bb->id &&
buzbeefa57c472012-11-21 12:06:18 -0800232 succ_bb->block_type == kDalvikByteCode &&
233 succ_bb->hidden == false) {
buzbee862a7602013-04-05 10:58:54 -0700234 dom_bb->dom_frontier->SetBit(succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 }
buzbee67bf8852011-08-17 17:51:35 -0700236}
237
238/* Worker function to compute the dominance frontier */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700239bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 /* Calculate DF_local */
buzbee0d829482013-10-11 15:24:55 -0700241 if (bb->taken != NullBasicBlockId) {
242 CheckForDominanceFrontier(bb, GetBasicBlock(bb->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 }
buzbee0d829482013-10-11 15:24:55 -0700244 if (bb->fall_through != NullBasicBlockId) {
245 CheckForDominanceFrontier(bb, GetBasicBlock(bb->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 }
buzbee0d829482013-10-11 15:24:55 -0700247 if (bb->successor_block_list_type != kNotUsed) {
248 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700250 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700251 if (successor_block_info == NULL) {
252 break;
253 }
buzbee0d829482013-10-11 15:24:55 -0700254 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800255 CheckForDominanceFrontier(bb, succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 }
257 }
buzbee67bf8852011-08-17 17:51:35 -0700258
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 /* Calculate DF_up */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100260 for (uint32_t dominated_idx : bb->i_dominated->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700261 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100262 for (uint32_t df_up_block_idx : dominated_bb->dom_frontier->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700263 BasicBlock* df_up_block = GetBasicBlock(df_up_block_idx);
buzbee311ca162013-02-28 15:56:43 -0800264 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700265 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 }
buzbee67bf8852011-08-17 17:51:35 -0700267
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 return true;
buzbee67bf8852011-08-17 17:51:35 -0700269}
270
271/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700272void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800273 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700274
Brian Carlstromdf629502013-07-17 22:39:56 -0700275 if (bb->dominators == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700276 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
277 false /* expandable */, kBitMapDominators);
278 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
279 false /* expandable */, kBitMapIDominated);
280 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
281 false /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700282 } else {
buzbee862a7602013-04-05 10:58:54 -0700283 bb->dominators->ClearAllBits();
284 bb->i_dominated->ClearAllBits();
285 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 }
287 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700288 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700289
buzbee862a7602013-04-05 10:58:54 -0700290 return;
buzbee67bf8852011-08-17 17:51:35 -0700291}
292
buzbee5b537102012-01-17 17:33:47 -0800293/*
buzbeefa57c472012-11-21 12:06:18 -0800294 * Walk through the ordered i_dom list until we reach common parent.
295 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800296 * last element of the intersection of block1 and block2 dominators.
297 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700298int MIRGraph::FindCommonParent(int block1, int block2) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700299 while (block1 != block2) {
300 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800301 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700302 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800303 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700304 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800305 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700306 DCHECK_NE(block2, NOTVISITED);
307 }
308 }
309 return block1;
buzbee5b537102012-01-17 17:33:47 -0800310}
311
312/* Worker function to compute each block's immediate dominator */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700313bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700315 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800316 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 }
318
319 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700320 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
Bill Buzbeea114add2012-05-03 15:00:40 -0700321
322 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700323 int idom = -1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700325 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
buzbeefa57c472012-11-21 12:06:18 -0800326 CHECK(pred_bb != NULL);
buzbee311ca162013-02-28 15:56:43 -0800327 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800328 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 break;
330 }
331 }
332
333 /* Scan the rest of the predecessors */
334 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700335 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700336 if (!pred_bb) {
337 break;
338 }
buzbee311ca162013-02-28 15:56:43 -0800339 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700340 continue;
341 } else {
buzbee311ca162013-02-28 15:56:43 -0800342 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 }
344 }
345
346 DCHECK_NE(idom, NOTVISITED);
347
348 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800349 if (i_dom_list_[bb->dfs_id] != idom) {
350 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 return true;
352 }
353 return false;
buzbee5b537102012-01-17 17:33:47 -0800354}
355
356/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700357bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800358 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700359 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 } else {
buzbee0d829482013-10-11 15:24:55 -0700361 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 }
buzbee862a7602013-04-05 10:58:54 -0700363 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700364 return false;
buzbee5b537102012-01-17 17:33:47 -0800365}
366
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700367bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800368 if (bb != GetEntryBlock()) {
369 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800370 DCHECK_NE(idom_dfs_idx, NOTVISITED);
buzbee862a7602013-04-05 10:58:54 -0700371 int i_dom_idx = dfs_post_order_->Get(idom_dfs_idx);
buzbee311ca162013-02-28 15:56:43 -0800372 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700373 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800374 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700375 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 }
377 return false;
buzbee5b537102012-01-17 17:33:47 -0800378}
379
buzbee67bf8852011-08-17 17:51:35 -0700380/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700381void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800382 int num_reachable_blocks = num_reachable_blocks_;
buzbee67bf8852011-08-17 17:51:35 -0700383
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700385 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800386 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
387 InitializeDominationInfo(bb);
388 }
buzbee67bf8852011-08-17 17:51:35 -0700389
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700390 /* Initialize & Clear i_dom_list */
391 if (max_num_reachable_blocks_ < num_reachable_blocks_) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700392 i_dom_list_ = static_cast<int*>(arena_->Alloc(sizeof(int) * num_reachable_blocks,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000393 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700394 }
buzbeefa57c472012-11-21 12:06:18 -0800395 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800396 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700397 }
buzbee5b537102012-01-17 17:33:47 -0800398
buzbeefa57c472012-11-21 12:06:18 -0800399 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800400 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
401 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800402
Bill Buzbeea114add2012-05-03 15:00:40 -0700403 /* Compute the immediate dominators */
buzbee56c71782013-09-05 17:13:19 -0700404 RepeatingReversePostOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800405 bool change = false;
406 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
407 change = ComputeblockIDom(bb);
408 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700409
410 /* Set the dominator for the root node */
buzbee862a7602013-04-05 10:58:54 -0700411 GetEntryBlock()->dominators->ClearAllBits();
412 GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700413
buzbee0d829482013-10-11 15:24:55 -0700414 GetEntryBlock()->i_dom = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700415
buzbee56c71782013-09-05 17:13:19 -0700416 PreOrderDfsIterator iter3(this);
buzbee311ca162013-02-28 15:56:43 -0800417 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
418 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 }
buzbee67bf8852011-08-17 17:51:35 -0700420
buzbee56c71782013-09-05 17:13:19 -0700421 ReversePostOrderDfsIterator iter4(this);
buzbee311ca162013-02-28 15:56:43 -0800422 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
423 ComputeBlockDominators(bb);
424 }
buzbee5b537102012-01-17 17:33:47 -0800425
buzbeea5abf702013-04-12 14:39:29 -0700426 // Compute the dominance frontier for each block.
buzbee311ca162013-02-28 15:56:43 -0800427 ComputeDomPostOrderTraversal(GetEntryBlock());
buzbee56c71782013-09-05 17:13:19 -0700428 PostOrderDOMIterator iter5(this);
buzbee311ca162013-02-28 15:56:43 -0800429 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
430 ComputeDominanceFrontier(bb);
431 }
buzbee67bf8852011-08-17 17:51:35 -0700432}
433
434/*
435 * Perform dest U= src1 ^ ~src2
436 * This is probably not general enough to be placed in BitVector.[ch].
437 */
buzbee311ca162013-02-28 15:56:43 -0800438void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700439 const ArenaBitVector* src2) {
buzbee862a7602013-04-05 10:58:54 -0700440 if (dest->GetStorageSize() != src1->GetStorageSize() ||
441 dest->GetStorageSize() != src2->GetStorageSize() ||
442 dest->IsExpandable() != src1->IsExpandable() ||
443 dest->IsExpandable() != src2->IsExpandable()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 LOG(FATAL) << "Incompatible set properties";
445 }
buzbee67bf8852011-08-17 17:51:35 -0700446
Bill Buzbeea114add2012-05-03 15:00:40 -0700447 unsigned int idx;
buzbee862a7602013-04-05 10:58:54 -0700448 for (idx = 0; idx < dest->GetStorageSize(); idx++) {
449 dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 }
buzbee67bf8852011-08-17 17:51:35 -0700451}
452
453/*
454 * Iterate through all successor blocks and propagate up the live-in sets.
455 * The calculated result is used for phi-node pruning - where we only need to
456 * insert a phi node if the variable is live-in to the block.
457 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700458bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100459 DCHECK_EQ(temp_bit_vector_size_, cu_->num_dalvik_registers);
460 ArenaBitVector* temp_dalvik_register_v = temp_bit_vector_;
buzbee67bf8852011-08-17 17:51:35 -0700461
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700462 if (bb->data_flow_info == NULL) {
463 return false;
464 }
buzbee862a7602013-04-05 10:58:54 -0700465 temp_dalvik_register_v->Copy(bb->data_flow_info->live_in_v);
buzbee0d829482013-10-11 15:24:55 -0700466 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
467 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
468 if (bb_taken && bb_taken->data_flow_info)
469 ComputeSuccLineIn(temp_dalvik_register_v, bb_taken->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800470 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700471 if (bb_fall_through && bb_fall_through->data_flow_info)
472 ComputeSuccLineIn(temp_dalvik_register_v, bb_fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800473 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700474 if (bb->successor_block_list_type != kNotUsed) {
475 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700477 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700478 if (successor_block_info == NULL) {
479 break;
480 }
buzbee0d829482013-10-11 15:24:55 -0700481 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbeefa57c472012-11-21 12:06:18 -0800482 if (succ_bb->data_flow_info) {
buzbee311ca162013-02-28 15:56:43 -0800483 ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800484 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700485 }
buzbee67bf8852011-08-17 17:51:35 -0700486 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 }
buzbee862a7602013-04-05 10:58:54 -0700488 if (!temp_dalvik_register_v->Equal(bb->data_flow_info->live_in_v)) {
489 bb->data_flow_info->live_in_v->Copy(temp_dalvik_register_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700490 return true;
491 }
492 return false;
buzbee67bf8852011-08-17 17:51:35 -0700493}
494
495/* Insert phi nodes to for each variable to the dominance frontiers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700496void MIRGraph::InsertPhiNodes() {
buzbeefa57c472012-11-21 12:06:18 -0800497 int dalvik_reg;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100498 ArenaBitVector* phi_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
499 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapPhi);
500 ArenaBitVector* input_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
501 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700502
buzbee56c71782013-09-05 17:13:19 -0700503 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800504 bool change = false;
505 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
506 change = ComputeBlockLiveIns(bb);
507 }
buzbee67bf8852011-08-17 17:51:35 -0700508
Bill Buzbeea114add2012-05-03 15:00:40 -0700509 /* Iterate through each Dalvik register */
buzbee311ca162013-02-28 15:56:43 -0800510 for (dalvik_reg = cu_->num_dalvik_registers - 1; dalvik_reg >= 0; dalvik_reg--) {
buzbee862a7602013-04-05 10:58:54 -0700511 input_blocks->Copy(def_block_matrix_[dalvik_reg]);
512 phi_blocks->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700513 do {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100514 // TUNING: When we repeat this, we could skip indexes from the previous pass.
515 for (uint32_t idx : input_blocks->Indexes()) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700516 BasicBlock* def_bb = GetBasicBlock(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100517 if (def_bb->dom_frontier != nullptr) {
518 phi_blocks->Union(def_bb->dom_frontier);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700519 }
520 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100521 } while (input_blocks->Union(phi_blocks));
Bill Buzbeea114add2012-05-03 15:00:40 -0700522
523 /*
buzbeefa57c472012-11-21 12:06:18 -0800524 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700525 * register is in the live-in set.
526 */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100527 for (uint32_t idx : phi_blocks->Indexes()) {
buzbee311ca162013-02-28 15:56:43 -0800528 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 /* Variable will be clobbered before being used - no need for phi */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700530 if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) {
531 continue;
532 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700533 MIR *phi = NewMIR();
buzbeecbd6d442012-11-17 14:11:25 -0800534 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800535 phi->dalvikInsn.vA = dalvik_reg;
536 phi->offset = phi_bb->start_offset;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700537 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700538 phi_bb->PrependMIR(phi);
buzbee67bf8852011-08-17 17:51:35 -0700539 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700540 }
buzbee67bf8852011-08-17 17:51:35 -0700541}
542
543/*
544 * Worker function to insert phi-operands with latest SSA names from
545 * predecessor blocks
546 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700547bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700548 /* Phi nodes are at the beginning of each block */
buzbee0d829482013-10-11 15:24:55 -0700549 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800550 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700551 return true;
buzbeefa57c472012-11-21 12:06:18 -0800552 int ssa_reg = mir->ssa_rep->defs[0];
553 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800554 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700555
Bill Buzbeea114add2012-05-03 15:00:40 -0700556 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700557 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
558 size_t num_uses = bb->predecessors->Size();
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700559 AllocateSSAUseData(mir, num_uses);
560 int* uses = mir->ssa_rep->uses;
buzbee0d829482013-10-11 15:24:55 -0700561 BasicBlockId* incoming =
562 static_cast<BasicBlockId*>(arena_->Alloc(sizeof(BasicBlockId) * num_uses,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000563 kArenaAllocDFInfo));
buzbee0d829482013-10-11 15:24:55 -0700564 mir->meta.phi_incoming = incoming;
565 int idx = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700566 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700567 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700568 if (!pred_bb) {
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700569 break;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700570 }
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700571 int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
buzbee0d829482013-10-11 15:24:55 -0700572 uses[idx] = ssa_reg;
573 incoming[idx] = pred_bb->id;
574 idx++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 }
576 }
577
578 return true;
buzbee67bf8852011-08-17 17:51:35 -0700579}
580
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700581void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700582 if (block->visited || block->hidden) {
583 return;
584 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700585 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700586
Bill Buzbeea114add2012-05-03 15:00:40 -0700587 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800588 DoSSAConversion(block);
589 int map_size = sizeof(int) * cu_->num_dalvik_registers;
buzbeef0cde542011-09-13 14:55:02 -0700590
Bill Buzbeea114add2012-05-03 15:00:40 -0700591 /* Save SSA map snapshot */
Vladimir Marko5d474472014-03-21 17:10:58 +0000592 ScopedArenaAllocator allocator(&cu_->arena_stack);
buzbee862a7602013-04-05 10:58:54 -0700593 int* saved_ssa_map =
Vladimir Marko5d474472014-03-21 17:10:58 +0000594 static_cast<int*>(allocator.Alloc(map_size, kArenaAllocDalvikToSSAMap));
buzbee311ca162013-02-28 15:56:43 -0800595 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700596
buzbee0d829482013-10-11 15:24:55 -0700597 if (block->fall_through != NullBasicBlockId) {
598 DoDFSPreOrderSSARename(GetBasicBlock(block->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700599 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800600 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700601 }
buzbee0d829482013-10-11 15:24:55 -0700602 if (block->taken != NullBasicBlockId) {
603 DoDFSPreOrderSSARename(GetBasicBlock(block->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700604 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800605 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700606 }
buzbee0d829482013-10-11 15:24:55 -0700607 if (block->successor_block_list_type != kNotUsed) {
608 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(block->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700609 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700610 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700611 if (successor_block_info == NULL) {
612 break;
613 }
buzbee0d829482013-10-11 15:24:55 -0700614 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800615 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800617 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700618 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700619 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700620 return;
buzbeef0cde542011-09-13 14:55:02 -0700621}
622
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800623} // namespace art