blob: bd6bc22531390c332eb68f39e1915cf66dbb8121 [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"
buzbee67bf8852011-08-17 17:51:35 -070019
buzbee1fd33462013-03-25 13:40:45 -070020#define NOTVISITED (-1)
21
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080022namespace art {
23
buzbeea5abf702013-04-12 14:39:29 -070024void MIRGraph::ClearAllVisitedFlags() {
buzbee56c71782013-09-05 17:13:19 -070025 AllNodesIterator iter(this);
buzbeea5abf702013-04-12 14:39:29 -070026 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
27 bb->visited = false;
28 }
29}
30
buzbee311ca162013-02-28 15:56:43 -080031BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) {
buzbeee5f01222012-06-14 15:19:35 -070032 if (bb != NULL) {
33 if (bb->visited || bb->hidden) {
34 bb = NULL;
35 }
36 }
37 return bb;
38}
39
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070040BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb) {
buzbee0d829482013-10-11 15:24:55 -070041 BasicBlock* res = NeedsVisit(GetBasicBlock(bb->fall_through));
buzbeee5f01222012-06-14 15:19:35 -070042 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070043 res = NeedsVisit(GetBasicBlock(bb->taken));
buzbeee5f01222012-06-14 15:19:35 -070044 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070045 if (bb->successor_block_list_type != kNotUsed) {
46 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
buzbeee5f01222012-06-14 15:19:35 -070047 while (true) {
buzbee862a7602013-04-05 10:58:54 -070048 SuccessorBlockInfo *sbi = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070049 if (sbi == NULL) {
50 break;
51 }
buzbee0d829482013-10-11 15:24:55 -070052 res = NeedsVisit(GetBasicBlock(sbi->block));
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070053 if (res != NULL) {
54 break;
55 }
buzbeee5f01222012-06-14 15:19:35 -070056 }
57 }
58 }
59 }
60 return res;
61}
62
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070063void MIRGraph::MarkPreOrder(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070064 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080065 /* Enqueue the pre_order block id */
buzbee0d829482013-10-11 15:24:55 -070066 if (block->id != NullBasicBlockId) {
67 dfs_order_->Insert(block->id);
68 }
buzbeee5f01222012-06-14 15:19:35 -070069}
70
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070071void MIRGraph::RecordDFSOrders(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070072 std::vector<BasicBlock*> succ;
buzbee311ca162013-02-28 15:56:43 -080073 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070074 succ.push_back(block);
75 while (!succ.empty()) {
76 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080077 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
78 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080079 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080080 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070081 continue;
82 }
buzbee862a7602013-04-05 10:58:54 -070083 curr->dfs_id = dfs_post_order_->Size();
buzbee0d829482013-10-11 15:24:55 -070084 if (curr->id != NullBasicBlockId) {
85 dfs_post_order_->Insert(curr->id);
86 }
buzbeee5f01222012-06-14 15:19:35 -070087 succ.pop_back();
88 }
89}
90
buzbee5b537102012-01-17 17:33:47 -080091/* Sort the blocks by the Depth-First-Search */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070092void MIRGraph::ComputeDFSOrders() {
buzbeefa57c472012-11-21 12:06:18 -080093 /* Initialize or reset the DFS pre_order list */
buzbee862a7602013-04-05 10:58:54 -070094 if (dfs_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -070095 dfs_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
96 kGrowableArrayDfsOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -070097 } else {
98 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -070099 dfs_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700100 }
buzbee67bf8852011-08-17 17:51:35 -0700101
buzbeefa57c472012-11-21 12:06:18 -0800102 /* Initialize or reset the DFS post_order list */
buzbee862a7602013-04-05 10:58:54 -0700103 if (dfs_post_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -0700104 dfs_post_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
105 kGrowableArrayDfsPostOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -0700106 } else {
107 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -0700108 dfs_post_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 }
buzbee5b537102012-01-17 17:33:47 -0800110
buzbeee5f01222012-06-14 15:19:35 -0700111 // Reset visited flags from all nodes
buzbeea5abf702013-04-12 14:39:29 -0700112 ClearAllVisitedFlags();
113
buzbeee5f01222012-06-14 15:19:35 -0700114 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800115 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700116
buzbee862a7602013-04-05 10:58:54 -0700117 num_reachable_blocks_ = dfs_order_->Size();
buzbee67bf8852011-08-17 17:51:35 -0700118}
119
120/*
121 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
122 * register idx is defined in BasicBlock bb.
123 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700124bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700125 if (bb->data_flow_info == NULL) {
126 return false;
127 }
buzbee67bf8852011-08-17 17:51:35 -0700128
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100129 for (uint32_t idx : bb->data_flow_info->def_v->Indexes()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 /* Block bb defines register idx */
buzbee862a7602013-04-05 10:58:54 -0700131 def_block_matrix_[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 }
133 return true;
buzbee67bf8852011-08-17 17:51:35 -0700134}
135
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700136void MIRGraph::ComputeDefBlockMatrix() {
buzbee311ca162013-02-28 15:56:43 -0800137 int num_registers = cu_->num_dalvik_registers;
buzbeefa57c472012-11-21 12:06:18 -0800138 /* Allocate num_dalvik_registers bit vector pointers */
buzbee311ca162013-02-28 15:56:43 -0800139 def_block_matrix_ = static_cast<ArenaBitVector**>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700140 (arena_->Alloc(sizeof(ArenaBitVector *) * num_registers,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000141 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 int i;
buzbee67bf8852011-08-17 17:51:35 -0700143
buzbeefa57c472012-11-21 12:06:18 -0800144 /* Initialize num_register vectors with num_blocks bits each */
145 for (i = 0; i < num_registers; i++) {
buzbee862a7602013-04-05 10:58:54 -0700146 def_block_matrix_[i] =
147 new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapBMatrix);
Bill Buzbeea114add2012-05-03 15:00:40 -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 */
buzbee311ca162013-02-28 15:56:43 -0800162 int num_regs = cu_->num_dalvik_registers;
163 int in_reg = num_regs - cu_->num_ins;
buzbeefa57c472012-11-21 12:06:18 -0800164 for (; in_reg < num_regs; in_reg++) {
buzbee862a7602013-04-05 10:58:54 -0700165 def_block_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) {
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700170 if (dom_post_order_traversal_ == NULL || max_num_reachable_blocks_ < num_reachable_blocks_) {
171 // First time or too small - create the array.
buzbeea5abf702013-04-12 14:39:29 -0700172 dom_post_order_traversal_ =
buzbee0d829482013-10-11 15:24:55 -0700173 new (arena_) GrowableArray<BasicBlockId>(arena_, num_reachable_blocks_,
buzbeea5abf702013-04-12 14:39:29 -0700174 kGrowableArrayDomPostOrderTraversal);
175 } else {
176 dom_post_order_traversal_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 }
buzbeea5abf702013-04-12 14:39:29 -0700178 ClearAllVisitedFlags();
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100179 std::vector<std::pair<BasicBlock*, ArenaBitVector::IndexIterator>> work_stack;
buzbeea5abf702013-04-12 14:39:29 -0700180 bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100181 work_stack.push_back(std::make_pair(bb, bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700182 while (!work_stack.empty()) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100183 std::pair<BasicBlock*, ArenaBitVector::IndexIterator>* curr = &work_stack.back();
184 BasicBlock* curr_bb = curr->first;
185 ArenaBitVector::IndexIterator* curr_idom_iter = &curr->second;
186 while (!curr_idom_iter->Done() && (NeedsVisit(GetBasicBlock(**curr_idom_iter)) == nullptr)) {
187 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700188 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100189 // NOTE: work_stack.push_back()/pop_back() invalidate curr and curr_idom_iter.
190 if (!curr_idom_iter->Done()) {
191 BasicBlock* new_bb = GetBasicBlock(**curr_idom_iter);
192 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700193 new_bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100194 work_stack.push_back(std::make_pair(new_bb, new_bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700195 } else {
196 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700197 if (curr_bb->id != NullBasicBlockId) {
198 dom_post_order_traversal_->Insert(curr_bb->id);
199 }
buzbeea5abf702013-04-12 14:39:29 -0700200 work_stack.pop_back();
buzbee67bf8852011-08-17 17:51:35 -0700201
buzbeea5abf702013-04-12 14:39:29 -0700202 /* hacky loop detection */
buzbee0d829482013-10-11 15:24:55 -0700203 if ((curr_bb->taken != NullBasicBlockId) && curr_bb->dominators->IsBitSet(curr_bb->taken)) {
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000204 curr_bb->nesting_depth++;
buzbeea5abf702013-04-12 14:39:29 -0700205 attributes_ |= METHOD_HAS_LOOP;
206 }
207 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 }
buzbee67bf8852011-08-17 17:51:35 -0700209}
210
buzbee311ca162013-02-28 15:56:43 -0800211void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700212 const BasicBlock* succ_bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 /*
214 * TODO - evaluate whether phi will ever need to be inserted into exit
215 * blocks.
216 */
buzbee0d829482013-10-11 15:24:55 -0700217 if (succ_bb->i_dom != dom_bb->id &&
buzbeefa57c472012-11-21 12:06:18 -0800218 succ_bb->block_type == kDalvikByteCode &&
219 succ_bb->hidden == false) {
buzbee862a7602013-04-05 10:58:54 -0700220 dom_bb->dom_frontier->SetBit(succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700221 }
buzbee67bf8852011-08-17 17:51:35 -0700222}
223
224/* Worker function to compute the dominance frontier */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700225bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 /* Calculate DF_local */
buzbee0d829482013-10-11 15:24:55 -0700227 if (bb->taken != NullBasicBlockId) {
228 CheckForDominanceFrontier(bb, GetBasicBlock(bb->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700229 }
buzbee0d829482013-10-11 15:24:55 -0700230 if (bb->fall_through != NullBasicBlockId) {
231 CheckForDominanceFrontier(bb, GetBasicBlock(bb->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 }
buzbee0d829482013-10-11 15:24:55 -0700233 if (bb->successor_block_list_type != kNotUsed) {
234 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700236 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700237 if (successor_block_info == NULL) {
238 break;
239 }
buzbee0d829482013-10-11 15:24:55 -0700240 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800241 CheckForDominanceFrontier(bb, succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 }
243 }
buzbee67bf8852011-08-17 17:51:35 -0700244
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 /* Calculate DF_up */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100246 for (uint32_t dominated_idx : bb->i_dominated->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700247 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100248 for (uint32_t df_up_block_idx : dominated_bb->dom_frontier->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700249 BasicBlock* df_up_block = GetBasicBlock(df_up_block_idx);
buzbee311ca162013-02-28 15:56:43 -0800250 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700251 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 }
buzbee67bf8852011-08-17 17:51:35 -0700253
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 return true;
buzbee67bf8852011-08-17 17:51:35 -0700255}
256
257/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700258void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800259 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700260
Brian Carlstromdf629502013-07-17 22:39:56 -0700261 if (bb->dominators == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700262 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
263 false /* expandable */, kBitMapDominators);
264 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
265 false /* expandable */, kBitMapIDominated);
266 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
267 false /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 } else {
buzbee862a7602013-04-05 10:58:54 -0700269 bb->dominators->ClearAllBits();
270 bb->i_dominated->ClearAllBits();
271 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 }
273 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700274 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700275
buzbee862a7602013-04-05 10:58:54 -0700276 return;
buzbee67bf8852011-08-17 17:51:35 -0700277}
278
buzbee5b537102012-01-17 17:33:47 -0800279/*
buzbeefa57c472012-11-21 12:06:18 -0800280 * Walk through the ordered i_dom list until we reach common parent.
281 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800282 * last element of the intersection of block1 and block2 dominators.
283 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700284int MIRGraph::FindCommonParent(int block1, int block2) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 while (block1 != block2) {
286 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800287 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800289 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700290 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800291 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 DCHECK_NE(block2, NOTVISITED);
293 }
294 }
295 return block1;
buzbee5b537102012-01-17 17:33:47 -0800296}
297
298/* Worker function to compute each block's immediate dominator */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700299bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700301 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800302 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 }
304
305 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700306 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
Bill Buzbeea114add2012-05-03 15:00:40 -0700307
308 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700309 int idom = -1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700311 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
buzbeefa57c472012-11-21 12:06:18 -0800312 CHECK(pred_bb != NULL);
buzbee311ca162013-02-28 15:56:43 -0800313 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800314 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 break;
316 }
317 }
318
319 /* Scan the rest of the predecessors */
320 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700321 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700322 if (!pred_bb) {
323 break;
324 }
buzbee311ca162013-02-28 15:56:43 -0800325 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700326 continue;
327 } else {
buzbee311ca162013-02-28 15:56:43 -0800328 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 }
330 }
331
332 DCHECK_NE(idom, NOTVISITED);
333
334 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800335 if (i_dom_list_[bb->dfs_id] != idom) {
336 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 return true;
338 }
339 return false;
buzbee5b537102012-01-17 17:33:47 -0800340}
341
342/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700343bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800344 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700345 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 } else {
buzbee0d829482013-10-11 15:24:55 -0700347 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 }
buzbee862a7602013-04-05 10:58:54 -0700349 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 return false;
buzbee5b537102012-01-17 17:33:47 -0800351}
352
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700353bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800354 if (bb != GetEntryBlock()) {
355 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800356 DCHECK_NE(idom_dfs_idx, NOTVISITED);
buzbee862a7602013-04-05 10:58:54 -0700357 int i_dom_idx = dfs_post_order_->Get(idom_dfs_idx);
buzbee311ca162013-02-28 15:56:43 -0800358 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700359 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800360 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700361 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 }
363 return false;
buzbee5b537102012-01-17 17:33:47 -0800364}
365
buzbee67bf8852011-08-17 17:51:35 -0700366/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700367void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800368 int num_reachable_blocks = num_reachable_blocks_;
buzbee67bf8852011-08-17 17:51:35 -0700369
Bill Buzbeea114add2012-05-03 15:00:40 -0700370 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700371 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800372 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
373 InitializeDominationInfo(bb);
374 }
buzbee67bf8852011-08-17 17:51:35 -0700375
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700376 /* Initialize & Clear i_dom_list */
377 if (max_num_reachable_blocks_ < num_reachable_blocks_) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700378 i_dom_list_ = static_cast<int*>(arena_->Alloc(sizeof(int) * num_reachable_blocks,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000379 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700380 }
buzbeefa57c472012-11-21 12:06:18 -0800381 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800382 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700383 }
buzbee5b537102012-01-17 17:33:47 -0800384
buzbeefa57c472012-11-21 12:06:18 -0800385 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800386 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
387 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800388
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 /* Compute the immediate dominators */
buzbee56c71782013-09-05 17:13:19 -0700390 RepeatingReversePostOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800391 bool change = false;
392 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
393 change = ComputeblockIDom(bb);
394 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700395
396 /* Set the dominator for the root node */
buzbee862a7602013-04-05 10:58:54 -0700397 GetEntryBlock()->dominators->ClearAllBits();
398 GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700399
buzbee0d829482013-10-11 15:24:55 -0700400 GetEntryBlock()->i_dom = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700401
buzbee56c71782013-09-05 17:13:19 -0700402 PreOrderDfsIterator iter3(this);
buzbee311ca162013-02-28 15:56:43 -0800403 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
404 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 }
buzbee67bf8852011-08-17 17:51:35 -0700406
buzbee56c71782013-09-05 17:13:19 -0700407 ReversePostOrderDfsIterator iter4(this);
buzbee311ca162013-02-28 15:56:43 -0800408 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
409 ComputeBlockDominators(bb);
410 }
buzbee5b537102012-01-17 17:33:47 -0800411
buzbeea5abf702013-04-12 14:39:29 -0700412 // Compute the dominance frontier for each block.
buzbee311ca162013-02-28 15:56:43 -0800413 ComputeDomPostOrderTraversal(GetEntryBlock());
buzbee56c71782013-09-05 17:13:19 -0700414 PostOrderDOMIterator iter5(this);
buzbee311ca162013-02-28 15:56:43 -0800415 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
416 ComputeDominanceFrontier(bb);
417 }
buzbee67bf8852011-08-17 17:51:35 -0700418}
419
420/*
421 * Perform dest U= src1 ^ ~src2
422 * This is probably not general enough to be placed in BitVector.[ch].
423 */
buzbee311ca162013-02-28 15:56:43 -0800424void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700425 const ArenaBitVector* src2) {
buzbee862a7602013-04-05 10:58:54 -0700426 if (dest->GetStorageSize() != src1->GetStorageSize() ||
427 dest->GetStorageSize() != src2->GetStorageSize() ||
428 dest->IsExpandable() != src1->IsExpandable() ||
429 dest->IsExpandable() != src2->IsExpandable()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 LOG(FATAL) << "Incompatible set properties";
431 }
buzbee67bf8852011-08-17 17:51:35 -0700432
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 unsigned int idx;
buzbee862a7602013-04-05 10:58:54 -0700434 for (idx = 0; idx < dest->GetStorageSize(); idx++) {
435 dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 }
buzbee67bf8852011-08-17 17:51:35 -0700437}
438
439/*
440 * Iterate through all successor blocks and propagate up the live-in sets.
441 * The calculated result is used for phi-node pruning - where we only need to
442 * insert a phi node if the variable is live-in to the block.
443 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700444bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100445 DCHECK_EQ(temp_bit_vector_size_, cu_->num_dalvik_registers);
446 ArenaBitVector* temp_dalvik_register_v = temp_bit_vector_;
buzbee67bf8852011-08-17 17:51:35 -0700447
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700448 if (bb->data_flow_info == NULL) {
449 return false;
450 }
buzbee862a7602013-04-05 10:58:54 -0700451 temp_dalvik_register_v->Copy(bb->data_flow_info->live_in_v);
buzbee0d829482013-10-11 15:24:55 -0700452 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
453 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
454 if (bb_taken && bb_taken->data_flow_info)
455 ComputeSuccLineIn(temp_dalvik_register_v, bb_taken->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800456 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700457 if (bb_fall_through && bb_fall_through->data_flow_info)
458 ComputeSuccLineIn(temp_dalvik_register_v, bb_fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800459 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700460 if (bb->successor_block_list_type != kNotUsed) {
461 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700463 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700464 if (successor_block_info == NULL) {
465 break;
466 }
buzbee0d829482013-10-11 15:24:55 -0700467 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbeefa57c472012-11-21 12:06:18 -0800468 if (succ_bb->data_flow_info) {
buzbee311ca162013-02-28 15:56:43 -0800469 ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800470 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 }
buzbee67bf8852011-08-17 17:51:35 -0700472 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700473 }
buzbee862a7602013-04-05 10:58:54 -0700474 if (!temp_dalvik_register_v->Equal(bb->data_flow_info->live_in_v)) {
475 bb->data_flow_info->live_in_v->Copy(temp_dalvik_register_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 return true;
477 }
478 return false;
buzbee67bf8852011-08-17 17:51:35 -0700479}
480
481/* Insert phi nodes to for each variable to the dominance frontiers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700482void MIRGraph::InsertPhiNodes() {
buzbeefa57c472012-11-21 12:06:18 -0800483 int dalvik_reg;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100484 ArenaBitVector* phi_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
485 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapPhi);
486 ArenaBitVector* input_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
487 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700488
buzbee56c71782013-09-05 17:13:19 -0700489 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800490 bool change = false;
491 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
492 change = ComputeBlockLiveIns(bb);
493 }
buzbee67bf8852011-08-17 17:51:35 -0700494
Bill Buzbeea114add2012-05-03 15:00:40 -0700495 /* Iterate through each Dalvik register */
buzbee311ca162013-02-28 15:56:43 -0800496 for (dalvik_reg = cu_->num_dalvik_registers - 1; dalvik_reg >= 0; dalvik_reg--) {
buzbee862a7602013-04-05 10:58:54 -0700497 input_blocks->Copy(def_block_matrix_[dalvik_reg]);
498 phi_blocks->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 do {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100500 // TUNING: When we repeat this, we could skip indexes from the previous pass.
501 for (uint32_t idx : input_blocks->Indexes()) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700502 BasicBlock* def_bb = GetBasicBlock(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100503 if (def_bb->dom_frontier != nullptr) {
504 phi_blocks->Union(def_bb->dom_frontier);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700505 }
506 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100507 } while (input_blocks->Union(phi_blocks));
Bill Buzbeea114add2012-05-03 15:00:40 -0700508
509 /*
buzbeefa57c472012-11-21 12:06:18 -0800510 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700511 * register is in the live-in set.
512 */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100513 for (uint32_t idx : phi_blocks->Indexes()) {
buzbee311ca162013-02-28 15:56:43 -0800514 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700515 /* Variable will be clobbered before being used - no need for phi */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700516 if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) {
517 continue;
518 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700519 MIR *phi = NewMIR();
buzbeecbd6d442012-11-17 14:11:25 -0800520 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800521 phi->dalvikInsn.vA = dalvik_reg;
522 phi->offset = phi_bb->start_offset;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700523 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700524 phi_bb->PrependMIR(phi);
buzbee67bf8852011-08-17 17:51:35 -0700525 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 }
buzbee67bf8852011-08-17 17:51:35 -0700527}
528
529/*
530 * Worker function to insert phi-operands with latest SSA names from
531 * predecessor blocks
532 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700533bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700534 /* Phi nodes are at the beginning of each block */
buzbee0d829482013-10-11 15:24:55 -0700535 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800536 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 return true;
buzbeefa57c472012-11-21 12:06:18 -0800538 int ssa_reg = mir->ssa_rep->defs[0];
539 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800540 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700541
Bill Buzbeea114add2012-05-03 15:00:40 -0700542 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700543 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
544 size_t num_uses = bb->predecessors->Size();
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700545 AllocateSSAUseData(mir, num_uses);
546 int* uses = mir->ssa_rep->uses;
buzbee0d829482013-10-11 15:24:55 -0700547 BasicBlockId* incoming =
548 static_cast<BasicBlockId*>(arena_->Alloc(sizeof(BasicBlockId) * num_uses,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000549 kArenaAllocDFInfo));
buzbee0d829482013-10-11 15:24:55 -0700550 mir->meta.phi_incoming = incoming;
551 int idx = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700552 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700553 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700554 if (!pred_bb) {
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700555 break;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700556 }
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700557 int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
buzbee0d829482013-10-11 15:24:55 -0700558 uses[idx] = ssa_reg;
559 incoming[idx] = pred_bb->id;
560 idx++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 }
562 }
563
564 return true;
buzbee67bf8852011-08-17 17:51:35 -0700565}
566
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700567void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700568 if (block->visited || block->hidden) {
569 return;
570 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700571 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700572
Bill Buzbeea114add2012-05-03 15:00:40 -0700573 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800574 DoSSAConversion(block);
575 int map_size = sizeof(int) * cu_->num_dalvik_registers;
buzbeef0cde542011-09-13 14:55:02 -0700576
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 /* Save SSA map snapshot */
Vladimir Marko5d474472014-03-21 17:10:58 +0000578 ScopedArenaAllocator allocator(&cu_->arena_stack);
buzbee862a7602013-04-05 10:58:54 -0700579 int* saved_ssa_map =
Vladimir Marko5d474472014-03-21 17:10:58 +0000580 static_cast<int*>(allocator.Alloc(map_size, kArenaAllocDalvikToSSAMap));
buzbee311ca162013-02-28 15:56:43 -0800581 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700582
buzbee0d829482013-10-11 15:24:55 -0700583 if (block->fall_through != NullBasicBlockId) {
584 DoDFSPreOrderSSARename(GetBasicBlock(block->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700585 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800586 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700587 }
buzbee0d829482013-10-11 15:24:55 -0700588 if (block->taken != NullBasicBlockId) {
589 DoDFSPreOrderSSARename(GetBasicBlock(block->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700590 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800591 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700592 }
buzbee0d829482013-10-11 15:24:55 -0700593 if (block->successor_block_list_type != kNotUsed) {
594 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(block->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700596 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700597 if (successor_block_info == NULL) {
598 break;
599 }
buzbee0d829482013-10-11 15:24:55 -0700600 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800601 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700602 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800603 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700604 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700605 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700606 return;
buzbeef0cde542011-09-13 14:55:02 -0700607}
608
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800609} // namespace art